From e27d4726ea1ad3f5f773d9faf8fbd696207479f6 Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Thu, 1 Jul 2004 04:09:55 +0000 Subject: [PATCH] * src/gramps_main.py (on_export_activate): Delegate work to DbPrompter. * src/WriteXML.py: Register with the new scheme. * src/DbPrompter.py (SaveAsDbPrompter): Add class. svn: r3246 --- ChangeLog | 6 ++++ src/DbPrompter.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++ src/WriteXML.py | 25 ++++++++++++-- src/gramps_main.py | 54 ++---------------------------- 4 files changed, 114 insertions(+), 54 deletions(-) diff --git a/ChangeLog b/ChangeLog index 418a2c013..111e225d8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2004-06-30 Alex Roitman + * src/gramps_main.py (on_export_activate): Delegate work + to DbPrompter. + * src/WriteXML.py: Register with the new scheme. + * src/DbPrompter.py (SaveAsDbPrompter): Add class. + 2004-06-29 Don Allingham * src/FamilyView.py: fix reordering of children * src/ChooseParents.py: get filters working again diff --git a/src/DbPrompter.py b/src/DbPrompter.py index e87d86746..e3b5fd88c 100644 --- a/src/DbPrompter.py +++ b/src/DbPrompter.py @@ -26,6 +26,7 @@ # #------------------------------------------------------------------------- import os +import shutil from gettext import gettext as _ #------------------------------------------------------------------------- @@ -314,3 +315,85 @@ class NewNativeDbPrompter: while os.path.isfile(os.path.expanduser('~/Untitled_%d.grdb' % ix ) ): ix = ix + 1 return os.path.expanduser('~/Untitled_%d.grdb' % ix ) + +#------------------------------------------------------------------------- +# +# SaveAsDbPrompter +# +#------------------------------------------------------------------------- +class SaveAsDbPrompter: + """ + This class allows to save (export) an existing database. + + Any data format is allowed. The available formats are obtained + from the plugins. If the selected format is non-native (non-grdb) + then corresponding export routine is called. Native save as just + copies file to another name. + + """ + + def __init__(self,parent,parent_window=None): + self.parent = parent + self.parent_window = parent_window + + def chooser(self): + """ + Select the new file. + Return 1 when selection is made and 0 otherwise. + """ + choose = gtk.FileChooserDialog(_('GRAMPS: Export database'), + self.parent_window, + gtk.FILE_CHOOSER_ACTION_SAVE, + (gtk.STOCK_CANCEL, + gtk.RESPONSE_CANCEL, + gtk.STOCK_OPEN, + gtk.RESPONSE_OK)) + choose.set_local_only(gtk.FALSE) + # Always add automatic (macth all files) filter + filter = gtk.FileFilter() + filter.set_name(_('By extension')) + filter.add_pattern('*') + choose.add_filter(filter) + + # Always add native format filter + filter = gtk.FileFilter() + filter.set_name(_('GRAMPS databases')) + filter.add_mime_type('application/x-gramps') + choose.add_filter(filter) + + # Add more data type selections if opening existing db + for (exportData,filter,pattern_list) in Plugins._exports: + choose.add_filter(filter) + + if GrampsCfg.lastfile: + choose.set_filename(GrampsCfg.lastfile) + + response = choose.run() + if response == gtk.RESPONSE_OK: + filename = choose.get_filename() + filename = os.path.normpath(os.path.abspath(filename)) + (junk,the_file) = os.path.split(filename) + the_ext = os.path.splitext(filename)[1] + + if the_ext in ('.grdb', '.GRDB'): + choose.destroy() + try: + shutil.copyfile(self.parent.db.get_save_path(),filename) + return 1 + except shutil.Error, msg: + QuestionDialog.ErrorDialog( _("Could not write file: %s") % filename, + _('System message was: %s') % msg ) + return 0 + + for (exportData,filter,pattern_list) in Plugins._exports: + if the_ext in pattern_list: + choose.destroy() + exportData(self.parent.db,filename) + return 1 + else: + QuestionDialog.ErrorDialog( _("Could not write file: %s") % filename, + _('The type is not in the list of known file types') ) + return 0 + else: + choose.destroy() + return 0 diff --git a/src/WriteXML.py b/src/WriteXML.py index 5c79371b7..51693ad53 100644 --- a/src/WriteXML.py +++ b/src/WriteXML.py @@ -36,6 +36,13 @@ import shutil import os import codecs +#------------------------------------------------------------------------- +# +# load gtk libraries +# +#------------------------------------------------------------------------- +import gtk + #------------------------------------------------------------------------- # # load GRAMPS libraries @@ -66,7 +73,7 @@ except: # # #------------------------------------------------------------------------- -def exportData(database, filename, callback): +def exportData(database, filename, callback=None): if os.path.isfile(filename): try: shutil.copyfile(filename, filename + ".bak") @@ -74,7 +81,7 @@ def exportData(database, filename, callback): except: pass - compress = GrampsCfg.uncompress == 0 and _gzip_ok == 1 + compress = _gzip_ok == 1 try: g = XmlWriter(database,callback,0,compress) @@ -798,3 +805,17 @@ def conf_priv(obj): return ' priv="%d"' % obj.get_privacy() else: return '' + +#------------------------------------------------------------------------- +# +# +# +#------------------------------------------------------------------------- +_mime_type = 'data.gramps' +_filter = gtk.FileFilter() +_filter.set_name(_('GRAMPS XML databases')) +_filter.add_pattern(_mime_type) +_ext_list = '.gramps' + +from Plugins import register_export +register_export(exportData,_filter,_ext_list) diff --git a/src/gramps_main.py b/src/gramps_main.py index 2f8a1a962..636453ee2 100755 --- a/src/gramps_main.py +++ b/src/gramps_main.py @@ -1367,58 +1367,8 @@ class Gramps: prompter.chooser() def on_export_activate(self,obj): - choose = gtk.FileChooserDialog(_('GRAMPS: Export database'), - self.topWindow, - gtk.FILE_CHOOSER_ACTION_SAVE, - (gtk.STOCK_CANCEL, - gtk.RESPONSE_CANCEL, - gtk.STOCK_OPEN, - gtk.RESPONSE_OK)) - - # Always add automatic (macth all files) filter - filter = gtk.FileFilter() - filter.set_name(_('By extension')) - filter.add_pattern('*') - choose.add_filter(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) - - for (exportData,filter,pattern_list) in Plugins._exports: - choose.add_filter(filter) - - if GrampsCfg.lastfile: - choose.set_filename(GrampsCfg.lastfile) - - response = choose.run() - if response == gtk.RESPONSE_OK: - filename = choose.get_filename() - filename = os.path.normpath(os.path.abspath(filename)) - (junk,the_file) = os.path.split(filename) - -# FIXME: Uncomment when we have grdb importer -# -# if filetype == 'application/x-gramps': -# if self.auto_save_load(filename) == 0: -# DbPrompter.DbPrompter(self,0,self.topWindow) -# else: - if True: - opened = 0 - for (exportData,filter,pattern_list) in Plugins._exports: - for pattern in pattern_list: - if filter.filter((filename,None,None,None)): - exportData(self.db,filename) - opened = 1 - break - if not opened: - ErrorDialog( _("Could not write file: %s") % filename, - _('The type is not in the list of known file types') ) - choose.destroy() + prompter = DbPrompter.SaveAsDbPrompter(self,self.topWindow) + prompter.chooser() def on_revert_activate(self,obj): pass