From 58a0a66508b9b3c6a5d58b018797077bd8f7d7da Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Mon, 23 Aug 2004 22:05:55 +0000 Subject: [PATCH] * src/Plugins.py: Add native_format flag to import plugin registration. * src/ReadXML.py, src/ReadGedcom.py: Register as native formats to prevent loading twice on File->Open. * src/data/gramps.schemas: Add keys for last import and export dirs. * src/GrampsCfg.py (get_last_import_dir, save_last_import_dir, get_last_export_dir, save_last_export_dir): Add functions. * src/Exportder.py (suggest_filename): Try last export and last import folders before falling back to Home; (save): Save export folder. * src/Utils.py (get_new_filename): Add optional folder argument. * src/DbPrompter.py (ExistingDbPrompter.chooser): Only add importers for non-native formats, the rest is already taken care of; Try last file, last import, last export, then home folders; (ImportDbPrompter.chooser): Save import folder; Try last import, last file, last export, then home folders. (NewNativeDbPrompter): Try last file, last import, last export folders, then fall back to home. svn: r3493 --- gramps2/ChangeLog | 17 +++++++++ gramps2/src/DbPrompter.py | 80 +++++++++++++++++++++++++++++---------- gramps2/src/Exporter.py | 16 ++++++-- gramps2/src/GrampsCfg.py | 12 ++++++ gramps2/src/Plugins.py | 4 +- gramps2/src/ReadGedcom.py | 2 +- gramps2/src/ReadXML.py | 2 +- gramps2/src/Utils.py | 9 ++--- 8 files changed, 110 insertions(+), 32 deletions(-) diff --git a/gramps2/ChangeLog b/gramps2/ChangeLog index 64c0620ec..d8c72d3a9 100644 --- a/gramps2/ChangeLog +++ b/gramps2/ChangeLog @@ -19,6 +19,23 @@ * src/plugins/WebPage.py: Use gramps ID for naming the files and creating links. + * src/Plugins.py: Add native_format flag to import plugin registration. + * src/ReadXML.py, src/ReadGedcom.py: Register as native formats + to prevent loading twice on File->Open. + * src/data/gramps.schemas: Add keys for last import and export dirs. + * src/GrampsCfg.py (get_last_import_dir, save_last_import_dir, + get_last_export_dir, save_last_export_dir): Add functions. + * src/Exportder.py (suggest_filename): Try last export and last + import folders before falling back to Home; (save): Save export folder. + * src/Utils.py (get_new_filename): Add optional folder argument. + * src/DbPrompter.py (ExistingDbPrompter.chooser): Only add + importers for non-native formats, the rest is already taken care of; + Try last file, last import, last export, then home folders; + (ImportDbPrompter.chooser): Save import folder; Try last import, + last file, last export, then home folders. + (NewNativeDbPrompter): Try last file, last import, last export folders, + then fall back to home. + 2004-08-23 Tim Waugh * install-sh: Removed this generated file. diff --git a/gramps2/src/DbPrompter.py b/gramps2/src/DbPrompter.py index d53304545..8a6c9984e 100644 --- a/gramps2/src/DbPrompter.py +++ b/gramps2/src/DbPrompter.py @@ -51,6 +51,7 @@ import Plugins import GrampsBSDDB import GrampsXMLDB import GrampsGEDDB +import GrampsCfg #------------------------------------------------------------------------- # @@ -147,36 +148,54 @@ class ExistingDbPrompter: # Always add native format filter mime_filter = gtk.FileFilter() - mime_filter.set_name(_('GEDCOM')) + mime_filter.set_name(_('GEDCOM files')) mime_filter.add_mime_type(const.app_gedcom) choose.add_filter(mime_filter) # Add more data type selections if opening existing db - for (importData,mime_filter,mime_type) in Plugins._imports: - choose.add_filter(mime_filter) + for (importData,mime_filter,mime_type,native_format) in Plugins._imports: + if not native_format: + choose.add_filter(mime_filter) + # Suggested folder: try last open file, last import, last export, + # then home. + default_dir = os.path.split(GrampsCfg.get_lastfile())[0] + os.path.sep + if len(default_dir)<=1: + default_dir = GrampsCfg.get_last_import_dir() + if len(default_dir)<=1: + default_dir = GrampsCfg.get_last_export_dir() + if len(default_dir)<=1: + default_dir = '~/' + + choose.set_current_folder(default_dir) response = choose.run() if response == gtk.RESPONSE_OK: filename = choose.get_filename() filetype = gnome.vfs.get_mime_type(filename) - + + (the_path,the_file) = os.path.split(filename) + GrampsCfg.save_last_import_dir(the_path) + if filetype == const.app_gramps: choose.destroy() self.parent.db = GrampsBSDDB.GrampsBSDDB() self.parent.read_file(filename) return 1 - if filetype == const.app_gramps_xml: + elif filetype == const.app_gramps_xml: choose.destroy() self.parent.db = GrampsXMLDB.GrampsXMLDB() self.parent.read_file(filename) return 1 - if filetype == const.app_gedcom: + elif filetype == const.app_gedcom: choose.destroy() self.parent.db = GrampsGEDDB.GrampsGEDDB() self.parent.read_file(filename) return 1 - (junk,the_file) = os.path.split(filename) - for (importData,mime_filter,mime_type) in Plugins._imports: + + # The above native formats did not work, so we need to + # look up the importer for this format + # and create an empty native database to import data in + for (importData,mime_filter,mime_type,native_format) in Plugins._imports: if filetype == mime_type or the_file == mime_type: choose.destroy() QuestionDialog.OkDialog( _("Opening non-native format"), @@ -240,17 +259,27 @@ class ImportDbPrompter: choose.add_filter(mime_filter) # FIXME: Uncomment when we have grdb importer -# # # Always add native format filter -# filter = gtk.FileFilter() -# filter.set_name(_('GRAMPS databases')) -# filter.add_mime_type('application/x-gramps') -# choose.add_filter(filter) +# mime_filter = gtk.FileFilter() +# mime_filter.set_name(_('GRAMPS databases')) +# mime_filter.add_mime_type(const.app_gramps) +# choose.add_filter(mime_filter) # Add more data type selections if opening existing db - for (importData,mime_filter,mime_type) in Plugins._imports: + for (importData,mime_filter,mime_type,native_format) in Plugins._imports: choose.add_filter(mime_filter) + # Suggested folder: try last open file, import, then last export, + # then home. + default_dir = GrampsCfg.get_last_import_dir() + if len(default_dir)<=1: + default_dir = os.path.split(GrampsCfg.get_lastfile())[0] + os.path.sep + if len(default_dir)<=1: + default_dir = GrampsCfg.get_last_export_dir() + if len(default_dir)<=1: + default_dir = '~/' + + choose.set_current_folder(default_dir) response = choose.run() if response == gtk.RESPONSE_OK: filename = choose.get_filename() @@ -261,8 +290,10 @@ class ImportDbPrompter: # choose.destroy() # self.parent.read_file(filename) # return 1 - (junk,the_file) = os.path.split(filename) - for (importData,mime_filter,mime_type) in Plugins._imports: + + (the_path,the_file) = os.path.split(filename) + GrampsCfg.save_last_import_dir(the_path) + for (importData,mime_filter,mime_type,native_format) in Plugins._imports: if filetype == mime_type or the_file == mime_type: choose.destroy() importData(self.parent.db,filename) @@ -276,7 +307,6 @@ class ImportDbPrompter: choose.destroy() return 0 - #------------------------------------------------------------------------- # # NewNativeDbPrompter @@ -316,12 +346,22 @@ class NewNativeDbPrompter: # Always add native format filter mime_filter = gtk.FileFilter() mime_filter.set_name(_('GRAMPS databases')) - mime_filter.add_mime_type('application/x-gramps') + mime_filter.add_mime_type(const.app_gramps) choose.add_filter(mime_filter) - new_filename = Utils.get_new_filename('grdb') + # Suggested folder: try last open file, import, then last export, + # then home. + default_dir = os.path.split(GrampsCfg.get_lastfile())[0] + os.path.sep + if len(default_dir)<=1: + default_dir = GrampsCfg.get_last_import_dir() + if len(default_dir)<=1: + default_dir = GrampsCfg.get_last_export_dir() + if len(default_dir)<=1: + default_dir = '~/' + + new_filename = Utils.get_new_filename('grdb',default_dir) - choose.set_filename(new_filename) + choose.set_current_folder(default_dir) choose.set_current_name(os.path.split(new_filename)[1]) response = choose.run() diff --git a/gramps2/src/Exporter.py b/gramps2/src/Exporter.py index 278c5623f..48954b3bb 100644 --- a/gramps2/src/Exporter.py +++ b/gramps2/src/Exporter.py @@ -50,6 +50,7 @@ import const import Utils import Plugins import QuestionDialog +import GrampsCfg #------------------------------------------------------------------------- # @@ -203,6 +204,7 @@ class Exporter: Depending on the success status, set the text for the final page. """ filename = self.chooser.get_filename() + GrampsCfg.save_last_export_dir(os.path.split(filename)[0]) ix = self.get_selected_format_index() if self.exports[ix][3]: success = self.exports[ix][0](self.parent.db,filename,self.person, @@ -329,11 +331,19 @@ class Exporter: """ ix = self.get_selected_format_index() ext = self.exports[ix][4] + + # Suggested folder: try last export, then last import, then home. + default_dir = GrampsCfg.get_last_export_dir() + if len(default_dir)<=1: + default_dir = GrampsCfg.get_last_import_dir() + if len(default_dir)<=1: + default_dir = '~/' + if ext == 'gramps': - new_filename = os.path.expanduser('~/data.gramps') + new_filename = os.path.expanduser(default_dir + 'data.gramps') else: - new_filename = Utils.get_new_filename(ext) - self.chooser.set_filename(new_filename) + new_filename = Utils.get_new_filename(ext,default_dir) + self.chooser.set_current_folder(default_dir) self.chooser.set_current_name(os.path.split(new_filename)[1]) def get_selected_format_index(self): diff --git a/gramps2/src/GrampsCfg.py b/gramps2/src/GrampsCfg.py index 61fbac5ad..ca7aaf746 100644 --- a/gramps2/src/GrampsCfg.py +++ b/gramps2/src/GrampsCfg.py @@ -189,6 +189,18 @@ def get_lastfile(): def save_last_file(val): set_string("/apps/gramps/paths/recent-file",val) +def get_last_import_dir(): + return get_string("/apps/gramps/paths/recent-import-dir") + +def save_last_import_dir(val): + set_string_as_path("/apps/gramps/paths/recent-import-dir",val) + +def get_last_export_dir(): + return get_string("/apps/gramps/paths/recent-export-dir") + +def save_last_export_dir(val): + set_string_as_path("/apps/gramps/paths/recent-export-dir",val) + def get_report_dir(): return get_string("/apps/gramps/paths/report-directory") diff --git a/gramps2/src/Plugins.py b/gramps2/src/Plugins.py index 8722c5a83..a4cf7f852 100644 --- a/gramps2/src/Plugins.py +++ b/gramps2/src/Plugins.py @@ -468,10 +468,10 @@ def register_export(exportData,_title,_description='',_config=None,_filename='') if _description and _filename: _exports.append((exportData,_title,_description,_config,_filename)) -def register_import(task, ffilter, mime=None): +def register_import(task, ffilter, mime=None, native_format=0): """Register an import filter, taking the task and file filter""" if mime: - _imports.append((task, ffilter, mime)) + _imports.append((task, ffilter, mime, native_format)) def register_report(task, name, category=_("Uncategorized"), diff --git a/gramps2/src/ReadGedcom.py b/gramps2/src/ReadGedcom.py index c2d9fb20c..1cf4357cb 100644 --- a/gramps2/src/ReadGedcom.py +++ b/gramps2/src/ReadGedcom.py @@ -1860,4 +1860,4 @@ _filter.add_mime_type(_mime_type) # #------------------------------------------------------------------------- from Plugins import register_import -register_import(importData,_filter,_mime_type) +register_import(importData,_filter,_mime_type,1) diff --git a/gramps2/src/ReadXML.py b/gramps2/src/ReadXML.py index 0ddb4c423..f8bb96df3 100644 --- a/gramps2/src/ReadXML.py +++ b/gramps2/src/ReadXML.py @@ -1221,4 +1221,4 @@ _filter.set_name(_('GRAMPS XML databases')) _filter.add_pattern(_mime_type) from Plugins import register_import -register_import(importData,_filter,_mime_type) +register_import(importData,_filter,_mime_type,1) diff --git a/gramps2/src/Utils.py b/gramps2/src/Utils.py index 530ab557b..cae776995 100644 --- a/gramps2/src/Utils.py +++ b/gramps2/src/Utils.py @@ -551,11 +551,10 @@ def create_id(): # # #------------------------------------------------------------------------- -_NEW_NAME_PATTERN = '~/Untitled_%d.%s' +_NEW_NAME_PATTERN = '%sUntitled_%d.%s' -def get_new_filename(ext): +def get_new_filename(ext,folder='~/'): ix = 1 - while os.path.isfile(os.path.expanduser(_NEW_NAME_PATTERN % (ix,ext) )): + while os.path.isfile(os.path.expanduser(_NEW_NAME_PATTERN % (folder,ix,ext) )): ix = ix + 1 - return os.path.expanduser(_NEW_NAME_PATTERN % (ix,ext)) - + return os.path.expanduser(_NEW_NAME_PATTERN % (folder,ix,ext))