From 416310dc17712ff58d11d30be291e18c05c87caf Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Mon, 28 Jun 2004 18:16:42 +0000 Subject: [PATCH] * src/DbPrompter.py: Add new class for new, existing, and import prompters. Enable opening any format on the startup. * src/gramps_main.py: Use new DbPrompter classes. * src/ArgHandler.py: Use new DbPrompter classes. svn: r3239 --- gramps2/ChangeLog | 8 +- gramps2/src/ArgHandler.py | 52 +++++----- gramps2/src/DbPrompter.py | 193 +++++++++++++++++++++++++++---------- gramps2/src/gramps_main.py | 187 ++--------------------------------- 4 files changed, 180 insertions(+), 260 deletions(-) diff --git a/gramps2/ChangeLog b/gramps2/ChangeLog index b852ddde5..ced6034d6 100644 --- a/gramps2/ChangeLog +++ b/gramps2/ChangeLog @@ -1,8 +1,8 @@ 2004-06-28 Alex Roitman - * src/DbPrompter.py: Add NewNativeDbPrompter class for setting up - a native database. Enable opening any format on the startup. - * src/gramps_main.py: Use NewNativeDbPrompter when opening - non-native data. + * src/DbPrompter.py: Add new class for new, existing, and import + prompters. Enable opening any format on the startup. + * src/gramps_main.py: Use new DbPrompter classes. + * src/ArgHandler.py: Use new DbPrompter classes. 2004-06-27 Don Allingham * src/gramps_main.py: fix import problem diff --git a/gramps2/src/ArgHandler.py b/gramps2/src/ArgHandler.py index 78034d8ff..ae691a6ae 100644 --- a/gramps2/src/ArgHandler.py +++ b/gramps2/src/ArgHandler.py @@ -201,36 +201,34 @@ class ArgHandler: print "Opened successfully!" else: print "Cannot open %s. Exiting..." - elif filetype == "application/x-gedcom": - print "Type: GEDCOM" + elif filetype in ("application/x-gedcom","x-directory/normal", + "application/x-gramps-package"): QuestionDialog.OkDialog( _("Opening non-native format"), - _("New gramps database has to be set up when opening non-native formats. The following dialog will let you select the new database."), + _("New GRAMPS database has to be set up when opening non-native formats. The following dialog will let you select the new database."), self.parent.topWindow) - DbPrompter.DbPrompter(self.parent,1,self.parent.topWindow,filename) - self.parent.read_gedcom(filename) - elif filetype == "x-directory/normal": - print "Type: GRAMPS XML" - QuestionDialog.OkDialog( _("Opening non-native format"), - _("New gramps database has to be set up when opening non-native formats. The following dialog will let you select the new database."), - self.parent.topWindow) - DbPrompter.DbPrompter(self.parent,1,self.parent.topWindow,filename) - self.parent.read_xml(filename) - elif filetype == "application/x-gramps-package": - print "Type: GRAMPS package" - QuestionDialog.OkDialog( _("Opening non-native format"), - _("New gramps database has to be set up when opening non-native formats. The following dialog will let you select the new database."), - self.parent.topWindow) - DbPrompter.DbPrompter(self.parent,1,self.parent.topWindow,filename) - self.parent.read_pkg(filename) - else: + prompter = DbPrompter.NewNativeDbPrompter(self.parent) + if not prompter.chooser(): + QuestionDialog.ErrorDialog( + _("New GRAMPS database was not set up"), + _('GRAMPS cannot open non-native data without setting up new GRAMPS database.')) + print "Cannot continue without native database. Exiting..." + os._exit(1) + elif filetype == "application/x-gedcom": + print "Type: GEDCOM" + self.parent.read_gedcom(filename) + elif filetype == "x-directory/normal": + print "Type: GRAMPS XML" + self.parent.read_xml(filename) + elif filetype == "application/x-gramps-package": + print "Type: GRAMPS package" + self.parent.read_pkg(filename) + else: print "Unknown file type: %s" % filetype QuestionDialog.ErrorDialog( - _("Cannot open file: unknown type"), - _('File type "%s" is unknown to GRAMPS.\n\nValid types are: GRAMPS database, GRAMPS XML, GRAMPS package, and GEDCOM.') % filetype, - self.parent.topWindow) + _("Cannot open file: unknown type"), + _('File type "%s" is unknown to GRAMPS.\n\nValid types are: GRAMPS database, GRAMPS XML, GRAMPS package, and GEDCOM.') % filetype) print "Exiting..." - os._exit(1) - + os._exit(1) return if self.imports: @@ -287,9 +285,9 @@ class ArgHandler: self.parent.import_tool_callback() elif GrampsCfg.lastfile and GrampsCfg.autoload: if self.parent.auto_save_load(GrampsCfg.lastfile) == 0: - DbPrompter.DbPrompter(self.parent,0,self.parent.topWindow) + DbPrompter.DbPrompter(self.parent,0) else: - DbPrompter.DbPrompter(self.parent,0,self.parent.topWindow) + DbPrompter.DbPrompter(self.parent,0) #------------------------------------------------------------------------- diff --git a/gramps2/src/DbPrompter.py b/gramps2/src/DbPrompter.py index 617b4f711..e87d86746 100644 --- a/gramps2/src/DbPrompter.py +++ b/gramps2/src/DbPrompter.py @@ -57,10 +57,9 @@ import Plugins class DbPrompter: """Make sure a database is opened""" - def __init__(self,parent,want_new,parent_window=None,file_hint=None): + def __init__(self,parent,want_new,parent_window=None): self.parent = parent self.parent_window = parent_window - self.file_hint = file_hint opendb = gtk.glade.XML(const.gladeFile, "opendb","gramps") top = opendb.get_widget('opendb') if parent_window: @@ -75,7 +74,11 @@ class DbPrompter: while 1: response = top.run() if response == gtk.RESPONSE_OK: - if self.chooser(new.get_active()): + if new.get_active(): + prompter = NewNativeDbPrompter(self.parent,self.parent_window) + else: + prompter = ExistingDbPrompter(self.parent,self.parent_window) + if prompter.chooser(): break elif response == gtk.RESPONSE_CANCEL: break @@ -89,18 +92,31 @@ class DbPrompter: if response == gtk.RESPONSE_CANCEL: gtk.main_quit() - def chooser(self,save): - if save: - choose = gtk.FileChooserDialog(_('GRAMPS: Create database'), - self.parent_window, - gtk.FILE_CHOOSER_ACTION_SAVE, - (gtk.STOCK_CANCEL, - gtk.RESPONSE_CANCEL, - gtk.STOCK_OPEN, - gtk.RESPONSE_OK)) - self.parent.clear_database() - else: - choose = gtk.FileChooserDialog(_('GRAMPS: Open database'), +#------------------------------------------------------------------------- +# +# ExistingDbPrompter +# +#------------------------------------------------------------------------- +class ExistingDbPrompter: + """ + This class allows to open 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 the call is made to set up a new native grdb database. + + """ + + 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: Open database'), self.parent_window, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, @@ -120,68 +136,141 @@ class DbPrompter: filter.add_mime_type('application/x-gramps') choose.add_filter(filter) - if save: - # Set the suggested filename for the newly created file - if self.file_hint: - choose.set_filename(self.file_hint) - elif GrampsCfg.lastfile: - choose.set_filename(GrampsCfg.lastfile) - else: - # Add more data type selections if opening existing db - for (importData,filter,mime_type) in Plugins._imports: - choose.add_filter(filter) + # Add more data type selections if opening existing db + for (importData,filter,mime_type) in Plugins._imports: + choose.add_filter(filter) response = choose.run() if response == gtk.RESPONSE_OK: filename = choose.get_filename() - if save: - if os.path.splitext(filename)[1] != ".grdb": - filename = filename + ".grdb" + filetype = gnome.vfs.get_mime_type(filename) + if filetype == 'application/x-gramps': choose.destroy() self.parent.read_file(filename) return 1 - else: - filetype = gnome.vfs.get_mime_type(filename) - if filetype == 'application/x-gramps': + (junk,the_file) = os.path.split(filename) + for (importData,filter,mime_type) in Plugins._imports: + if filetype == mime_type or the_file == mime_type: choose.destroy() - self.parent.read_file(filename) - return 1 - (junk,the_file) = os.path.split(filename) - for (importData,filter,mime_type) in Plugins._imports: - if filetype == mime_type or the_file == mime_type: - choose.destroy() - QuestionDialog.OkDialog( _("Opening non-native format"), + QuestionDialog.OkDialog( _("Opening non-native format"), _("New gramps database has to be set up " "when opening non-native formats. The " "following dialog will let you select " "the new database."), self.parent_window) - NewNativeDbPrompter(self.parent,self.parent_window,filename) + prompter = NewNativeDbPrompter(self.parent,self.parent_window) + if prompter.chooser(): importData(self.parent.db,filename) self.parent.import_tool_callback() return 1 - QuestionDialog.ErrorDialog( _("Could not open file: %s") % filename, - _('The type "%s" is not in the list of known file types') % filetype ) + else: + return 0 + QuestionDialog.ErrorDialog( _("Could not open file: %s") % filename, + _('The type "%s" is not in the list of known file types') % filetype ) + return 0 else: choose.destroy() return 0 +#------------------------------------------------------------------------- +# +# ImportDbPrompter +# +#------------------------------------------------------------------------- +class ImportDbPrompter: + """ + This class allows to import a database. The data is imported into + the currently opened database, so we don't have to worry about + setting up the native database. + + Any data format is allowed. The available formats are obtained + from the plugins. + + """ + + 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: Import database'), + self.parent_window, + gtk.FILE_CHOOSER_ACTION_OPEN, + (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(_('Automatic')) + 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) + + # Add more data type selections if opening existing db + for (importData,filter,mime_type) in Plugins._imports: + choose.add_filter(filter) + + response = choose.run() + if response == gtk.RESPONSE_OK: + filename = choose.get_filename() + filetype = gnome.vfs.get_mime_type(filename) +# FIXME: Uncomment when we have grdb importer +# +# if filetype == 'application/x-gramps': +# choose.destroy() +# self.parent.read_file(filename) +# return 1 + (junk,the_file) = os.path.split(filename) + for (importData,filter,mime_type) in Plugins._imports: + if filetype == mime_type or the_file == mime_type: + choose.destroy() + importData(self.parent.db,filename) + self.parent.import_tool_callback() + return 1 + QuestionDialog.ErrorDialog( _("Could not open file: %s") % filename, + _('The type "%s" is not in the list of known file types') % filetype ) + return 0 + + else: + choose.destroy() + return 0 + + #------------------------------------------------------------------------- # # NewNativeDbPrompter # #------------------------------------------------------------------------- class NewNativeDbPrompter: - """Set up a new empty native database.""" + """ + This class allows to set up a new empty native (grdb) database. + The filename is forced to have an '.grdb' extension. If not given, + it is appended. + """ - def __init__(self,parent,parent_window=None,file_hint=None): + def __init__(self,parent,parent_window=None): self.parent = parent self.parent_window = parent_window - self.file_hint = file_hint - self.chooser() def chooser(self): + """ + Select the new file. Suggest the Untitled_X.grdb name. + Return 1 when selection is made and 0 otherwise. + """ choose = gtk.FileChooserDialog(_('GRAMPS: Create GRAMPS database'), self.parent_window, gtk.FILE_CHOOSER_ACTION_SAVE, @@ -203,10 +292,10 @@ class NewNativeDbPrompter: filter.add_mime_type('application/x-gramps') choose.add_filter(filter) - if self.file_hint: - choose.set_filename(self.file_hint) - elif GrampsCfg.lastfile: - choose.set_filename(GrampsCfg.lastfile) + new_filename = self.get_new_filename() + + choose.set_filename(new_filename) + choose.set_current_name(os.path.split(new_filename)[1]) response = choose.run() if response == gtk.RESPONSE_OK: @@ -219,3 +308,9 @@ class NewNativeDbPrompter: else: choose.destroy() return 0 + + def get_new_filename(self): + ix = 1 + while os.path.isfile(os.path.expanduser('~/Untitled_%d.grdb' % ix ) ): + ix = ix + 1 + return os.path.expanduser('~/Untitled_%d.grdb' % ix ) diff --git a/gramps2/src/gramps_main.py b/gramps2/src/gramps_main.py index 1d8d01814..e7863fc45 100755 --- a/gramps2/src/gramps_main.py +++ b/gramps2/src/gramps_main.py @@ -313,7 +313,6 @@ class Gramps: "on_home_clicked" : self.on_home_clicked, "on_new_clicked" : self.on_new_clicked, "on_notebook1_switch_page" : self.on_views_switch_page, - "on_ok_button1_clicked" : self.on_ok_button1_clicked, "on_open_activate" : self.on_open_activate, "on_import_activate" : self.on_import_activate, "on_export_activate" : self.on_export_activate, @@ -906,7 +905,9 @@ class Gramps: def on_new_clicked(self,obj): """Prompt for permission to close the current database""" - DbPrompter.DbPrompter(self,1,self.topWindow) + self.db.close() + prompter = DbPrompter.NewNativeDbPrompter(self,self.topWindow) + prompter.chooser() def clear_database(self): """Clear out the database if permission was granted""" @@ -959,16 +960,6 @@ class Gramps: if self.active_person: Plugins.ReportPlugins(self,self.db,self.active_person) - def on_ok_button1_clicked(self,obj): - filename = self.filesel.get_filename() - if filename == "" or filename == None: - return - filename = os.path.normpath(os.path.abspath(filename)) - self.filesel.destroy() - self.clear_database() - if self.auto_save_load(filename) == 0: - DbPrompter.DbPrompter(self,0,self.topWindow) - def on_help_dbopen_clicked(self,obj): """Display the relevant portion of GRAMPS manual""" try: @@ -1096,14 +1087,6 @@ class Gramps: self.goto_active_person() return 1 - def on_ok_button2_clicked(self,obj): - filename = obj.get_filename() - filename = os.path.normpath(os.path.abspath(filename)) - if filename: - Utils.destroy_passed_object(obj) - self.save_media(filename) - self.save_file(filename,_("No Comment Provided")) - def save_media(self,filename): import RelImage missmedia_action = 0 @@ -1202,53 +1185,6 @@ class Gramps: elif missmedia_action == 3: select_clicked() - def save_file(self,filename,comment): - - path = filename - filename = os.path.normpath(os.path.abspath(filename)) - self.status_text(_("Saving %s ...") % filename) - - if os.path.exists(filename): - if not os.path.isdir(filename): - DbPrompter.DbPrompter(self,0,self.topWindow) - self.displayError(_("Database could not be opened"), - _("%s is not a directory.") % filename + ' ' + \ - _("You should select a directory that contains a " - "data.gramps file.")) - return - else: - try: - os.mkdir(filename) - except (OSError,IOError), msg: - emsg = _("Could not create %s") % filename - ErrorDialog(emsg,_("An error was detected while attempting to create the file. ", - 'The operating system reported "%s"' % str(msg))) - return - except: - ErrorDialog(_("Could not create %s") % filename, - _("An error was detected while trying to create the file")) - return - - old_file = filename - filename = "%s/%s" % (filename,self.db.get_base()) - try: - self.db.clear_added_media_objects() - except (OSError,IOError), msg: - emsg = _("Could not create %s") % filename - ErrorDialog(emsg,msg) - return - - self.db.set_save_path(old_file) - GrampsCfg.save_last_file(old_file) - - filename = self.db.get_save_path() - if filename[-1] == '/': - filename = filename[:-1] - name = os.path.basename(filename) - self.topWindow.set_title("%s - GRAMPS" % name) - self.status_text("") - self.statusbar.set_progress_percentage(0.0) - def load_selected_people(self,obj): """Display the selected people in the EditPerson display""" mlist = self.people_view.get_selected_objects() @@ -1427,121 +1363,12 @@ class Gramps: self.filesel.destroy() def on_open_activate(self,obj): - - choose = gtk.FileChooserDialog(_('GRAMPS: Open database'), - self.topWindow, - gtk.FILE_CHOOSER_ACTION_OPEN, - (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(_('Automatic')) - filter.add_pattern('*') - choose.add_filter(filter) + prompter = DbPrompter.ExistingDbPrompter(self,self.topWindow) + prompter.chooser() - # 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 (importData,filter,mime_type) in Plugins._imports: - 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)) - self.clear_database() - filetype = gnome.vfs.get_mime_type(filename) - (junk,the_file) = os.path.split(filename) - - if filetype == 'application/x-gramps': - if self.auto_save_load(filename) == 0: - DbPrompter.DbPrompter(self,0,self.topWindow) - else: - opened = 0 - for (importData,filter,mime_type) in Plugins._imports: - if filetype == mime_type or the_file == mime_type: - OkDialog( _("Opening non-native format"), - _("New gramps database has to be set up " - "when opening non-native formats. The " - "following dialog will let you select " - "the new database."), - self.topWindow) - DbPrompter.NewNativeDbPrompter(self,self.topWindow,filename) - importData(self.db,filename) - self.import_tool_callback() - opened = 1 - break - if not opened: - ErrorDialog( _("Could not open file: %s") % filename, - _('The type "%s" is not in the list of known file types') % filetype ) - choose.destroy() - def on_import_activate(self,obj): - choose = gtk.FileChooserDialog(_('GRAMPS: Import database'), - self.topWindow, - gtk.FILE_CHOOSER_ACTION_OPEN, - (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(_('Automatic')) - 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 (importData,filter,mime_type) in Plugins._imports: - 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)) - filetype = gnome.vfs.get_mime_type(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 (importData,filter,mime_type) in Plugins._imports: - if filetype == mime_type or the_file == mime_type: - print mime_type - print "Keys before:", self.db.get_person_keys() - importData(self.db,filename) - self.import_tool_callback() - opened = 1 - print "Keys after:", self.db.get_person_keys() - break - if not opened: - ErrorDialog( _("Could not open file: %s") % filename, - _('The type "%s" is not in the list of known file types') % filetype ) - choose.destroy() + prompter = DbPrompter.ImportDbPrompter(self,self.topWindow) + prompter.chooser() def on_export_activate(self,obj): choose = gtk.FileChooserDialog(_('GRAMPS: Export database'),