From 3038404c4b7327bd7918ee5a7627df746656a659 Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Thu, 25 Nov 2004 05:42:31 +0000 Subject: [PATCH] * src/RecentFiles.py: Add to CVS. Support for recent-files API. * src/Makefile.am: Ship RecentFiles.py. * src/DbPrompter.py: Support for recent-files. * src/ArgHandler.py: Support for recent-files. svn: r3751 --- ChangeLog | 6 ++ src/ArgHandler.py | 15 ++++ src/DbPrompter.py | 19 ++++- src/Makefile.am | 3 +- src/RecentFiles.py | 200 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 src/RecentFiles.py diff --git a/ChangeLog b/ChangeLog index f24d14c5e..493134476 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2004-11-24 Alex Roitman + * src/RecentFiles.py: Add to CVS. Support for recent-files API. + * src/Makefile.am: Ship RecentFiles.py. + * src/DbPrompter.py: Support for recent-files. + * src/ArgHandler.py: Support for recent-files. + 2004-11-24 Don Allingham * src/GrampsDbBase.py: always use external thumbnails * src/GrampsBSDDB.py: always use external thumbnails diff --git a/src/ArgHandler.py b/src/ArgHandler.py index ad78595fe..2e7563e89 100644 --- a/src/ArgHandler.py +++ b/src/ArgHandler.py @@ -33,6 +33,7 @@ Module responsible for handling the command line arguments for GRAMPS. #------------------------------------------------------------------------- import os import getopt +import time from gettext import gettext as _ #------------------------------------------------------------------------- @@ -46,6 +47,7 @@ import GrampsMime import DbPrompter import QuestionDialog import GrampsGconfKeys +import RecentFiles #------------------------------------------------------------------------- # @@ -217,12 +219,14 @@ class ArgHandler: if self.open: # Filename was given. Open a session with that file. Forget # the rest of given arguments. + success = False filename = os.path.abspath(os.path.expanduser(self.open)) filetype = GrampsMime.get_type(filename) if filetype == const.app_gramps: print "Type: GRAMPS database" if self.auto_save_load(filename): print "Opened successfully!" + success = True else: print "Cannot open %s. Exiting..." elif filetype in (const.app_gedcom,"x-directory/normal", @@ -246,6 +250,7 @@ class ArgHandler: elif filetype == const.app_gramps_package: print "Type: GRAMPS package" self.parent.read_pkg(filename) + success = True else: print "Unknown file type: %s" % filetype QuestionDialog.ErrorDialog( @@ -253,6 +258,16 @@ class ArgHandler: _('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) + if success: + rf = RecentFiles.RecentFiles() + item = RecentFiles.RecentItem( + u='file://%s' % filename, + m=filetype, + t=int(time.time()), + p=False, + g=['Gramps']) + rf.add(item) + rf.save() return if self.imports: diff --git a/src/DbPrompter.py b/src/DbPrompter.py index a48d7ef1f..7dd7e40c8 100644 --- a/src/DbPrompter.py +++ b/src/DbPrompter.py @@ -26,6 +26,7 @@ # #------------------------------------------------------------------------- import os +import time from gettext import gettext as _ #------------------------------------------------------------------------- @@ -51,6 +52,7 @@ import GrampsBSDDB import GrampsXMLDB import GrampsGEDDB import GrampsGconfKeys +import RecentFiles #------------------------------------------------------------------------- # @@ -178,6 +180,7 @@ class ExistingDbPrompter: (the_path,the_file) = os.path.split(filename) GrampsGconfKeys.save_last_import_dir(the_path) + ret = False if filetype == const.app_gramps: choose.destroy() self.parent.db = GrampsBSDDB.GrampsBSDDB() @@ -185,16 +188,28 @@ class ExistingDbPrompter: msg_top = msgxml.get_widget('load_message') self.parent.read_file(filename) msg_top.destroy() - return True + ret = True elif filetype == const.app_gramps_xml: choose.destroy() self.parent.db = GrampsXMLDB.GrampsXMLDB() self.parent.read_file(filename) - return True + ret = True elif filetype == const.app_gedcom: choose.destroy() self.parent.db = GrampsGEDDB.GrampsGEDDB() self.parent.read_file(filename) + ret = True + + if ret: + rf = RecentFiles.RecentFiles() + item = RecentFiles.RecentItem( + u='file://%s' % filename, + m=filetype, + t=int(time.time()), + p=False, + g=['Gramps']) + rf.add(item) + rf.save() return True # The above native formats did not work, so we need to diff --git a/src/Makefile.am b/src/Makefile.am index 8ce27e7b3..f69bc3336 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -105,7 +105,8 @@ gdir_PYTHON = \ SelectPerson.py\ ArgHandler.py\ Exporter.py\ - GrampsGconfKeys.py + GrampsGconfKeys.py\ + RecentFiles.py # Could use GNU make's ':=' syntax for nice wildcard use. # If not using GNU make, then list all files individually diff --git a/src/RecentFiles.py b/src/RecentFiles.py new file mode 100644 index 000000000..cfd36405b --- /dev/null +++ b/src/RecentFiles.py @@ -0,0 +1,200 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2004 Donald N. Allingham +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +# $Id$ + +# Written by Alex Roitman + +#------------------------------------------------------------------------- +# +# Standard Python Modules +# +#------------------------------------------------------------------------- +import os +import fcntl +import time +import xml.parsers.expat + +#------------------------------------------------------------------------- +# +# Constants +# +#------------------------------------------------------------------------- +FILENAME = "~/.recently-used" +MAX_ITEMS = 500 + +#------------------------------------------------------------------------- +# +# RecentItem +# +#------------------------------------------------------------------------- +class RecentItem: + """ + Interface to a single recent-items item + """ + + def __init__(self,u="",m="",t="",p=False,g=[]): + self.uri = u + self.mime = m + self.time = t + self.private = p + self.groups = g + + def set_uri(self,val): + self.uri = val + + def get_uri(self): + return self.uri + + def set_mime(self,val): + self.mime = val + + def get_mime(self): + return self.mime + + def set_time(self,val): + self.time = int(val) + + def get_time(self): + return self.time + + def set_private(self,val): + self.private = val + + def get_private(self): + return self.private + + def set_groups(self,val): + self.groups = val[:] + + def get_groups(self): + return self.groups[:] + +#------------------------------------------------------------------------- +# +# RecentFiles +# +#------------------------------------------------------------------------- +class RecentFiles: + """ + Interface to a RecentFiles collection + """ + + def __init__(self): + parser = RecentParser() + self.recent_files = parser.get() + + def add(self,item2add): + for item in self.recent_files: + if item.get_uri() == item2add.get_uri(): + item.set_time(item2add.get_time()) + return + self.recent_files.insert(0,item2add) + + def save(self): + """ + Saves the current RecentFiles collection to the associated file. + """ + xml_file = file(os.path.expanduser(FILENAME),'w') + fcntl.lockf(xml_file,fcntl.LOCK_EX) + xml_file.write("\n") + xml_file.write('\n') + index = 0 + for item in self.recent_files: + if index > MAX_ITEMS: + break + xml_file.write(' \n') + xml_file.write(' %s\n' % item.get_uri()) + xml_file.write(' %s\n' % item.get_mime()) + xml_file.write(' %d\n' % item.get_time()) + if item.get_private(): + xml_file.write(' \n') + xml_file.write(' \n') + for g in item.get_groups(): + xml_file.write(' %s\n' % g) + xml_file.write(' \n') + xml_file.write(' \n') + xml_file.write('\n') + fcntl.lockf(xml_file,fcntl.LOCK_UN) + xml_file.close() + +#------------------------------------------------------------------------- +# +# RecentParser +# +#------------------------------------------------------------------------- +class RecentParser: + """ + Parsing class for the RecentFiles collection. + """ + + def __init__(self): + xml_file = file(os.path.expanduser(FILENAME)) + fcntl.lockf(xml_file,fcntl.LOCK_SH) + + self.recent_files = None + + p = xml.parsers.expat.ParserCreate() + p.StartElementHandler = self.startElement + p.EndElementHandler = self.endElement + p.CharacterDataHandler = self.characters + p.ParseFile(xml_file) + + fcntl.lockf(xml_file,fcntl.LOCK_UN) + xml_file.close() + + def get(self): + return self.recent_files + + def startElement(self,tag,attrs): + """ + Loads the dictionary when an XML tag of 'template' is found. The format + XML tag is