From 0d121bca70bb7c0d8b7bb44e6078c4305beb53d4 Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Mon, 29 Nov 2004 05:15:41 +0000 Subject: [PATCH] * src/RecentFiles.py: Add support for Gramps' own recent items. * src/DbPrompter.py: Support for Gramps' own recent items. * src/ArgHandler.py: Support for Gramps' own recent items. * src/gramps.glade: Add Open Recent submenu. * src/gramps_main.py: Preliminary support for Open Recent submenu. svn: r3766 --- gramps2/ChangeLog | 6 + gramps2/src/ArgHandler.py | 11 +- gramps2/src/DbPrompter.py | 21 +--- gramps2/src/RecentFiles.py | 232 ++++++++++++++++++++++++++++++---- gramps2/src/gramps.glade | 247 +++++++++++++++++++------------------ gramps2/src/gramps_main.py | 23 ++++ 6 files changed, 370 insertions(+), 170 deletions(-) diff --git a/gramps2/ChangeLog b/gramps2/ChangeLog index 5435e1d18..4856c8dc5 100644 --- a/gramps2/ChangeLog +++ b/gramps2/ChangeLog @@ -2,6 +2,12 @@ * src/plugins/PatchNames.py: Typo. * src/plugins/ChangeNames.py: Correct description. + * src/RecentFiles.py: Add support for Gramps' own recent items. + * src/DbPrompter.py: Support for Gramps' own recent items. + * src/ArgHandler.py: Support for Gramps' own recent items. + * src/gramps.glade: Add Open Recent submenu. + * src/gramps_main.py: Preliminary support for Open Recent submenu. + 2004-11-28 Don Allingham * src/EditPerson.py: Allow the photo to accept events, allowing us to display a popup menu similar to galleries diff --git a/gramps2/src/ArgHandler.py b/gramps2/src/ArgHandler.py index 2e7563e89..366c2033b 100644 --- a/gramps2/src/ArgHandler.py +++ b/gramps2/src/ArgHandler.py @@ -259,15 +259,8 @@ class ArgHandler: 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() + # Add the file to the recent items + RecentFiles.recent_files(filename,filetype) return if self.imports: diff --git a/gramps2/src/DbPrompter.py b/gramps2/src/DbPrompter.py index 72427747a..82f743369 100644 --- a/gramps2/src/DbPrompter.py +++ b/gramps2/src/DbPrompter.py @@ -26,7 +26,6 @@ # #------------------------------------------------------------------------- import os -import time from gettext import gettext as _ #------------------------------------------------------------------------- @@ -202,15 +201,7 @@ class ExistingDbPrompter: if success: # Add the file to the recent items - 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() + RecentFiles.recent_files(filename,filetype) return True # The above native formats did not work, so we need to @@ -390,15 +381,7 @@ class NewNativeDbPrompter: choose.destroy() self.parent.read_file(filename) # Add the file to the recent items - rf = RecentFiles.RecentFiles() - item = RecentFiles.RecentItem( - u='file://%s' % filename, - m=const.app_gramps, - t=int(time.time()), - p=False, - g=['Gramps']) - rf.add(item) - rf.save() + RecentFiles.recent_files(filename,const.app_gramps) return True else: choose.destroy() diff --git a/gramps2/src/RecentFiles.py b/gramps2/src/RecentFiles.py index 0f6cd0d02..4a792d653 100644 --- a/gramps2/src/RecentFiles.py +++ b/gramps2/src/RecentFiles.py @@ -37,20 +37,23 @@ import xml.parsers.expat # Constants # #------------------------------------------------------------------------- -FILENAME = "~/.recently-used" -MAX_ITEMS = 500 +GNOME_FILENAME = "~/.recently-used" +MAX_GNOME_ITEMS = 500 + +GRAMPS_FILENAME = "~/.gramps/recent-files.xml" +MAX_GRAMPS_ITEMS = 10 #------------------------------------------------------------------------- # -# RecentItem +# GnomeRecentItem # #------------------------------------------------------------------------- -class RecentItem: +class GnomeRecentItem: """ - Interface to a single recent-items item + Interface to a single GNOME recent-items item """ - def __init__(self,u="",m="",t="",p=False,g=[]): + def __init__(self,u="",m="",t=0,p=False,g=[]): self.uri = u self.mime = m self.time = t @@ -93,22 +96,58 @@ class RecentItem: #------------------------------------------------------------------------- # -# RecentFiles +# GnomeRecentItem # #------------------------------------------------------------------------- -class RecentFiles: +class GrampsRecentItem: """ - Interface to a RecentFiles collection + Interface to a single GRAMPS recent-items item + """ + + def __init__(self,p="",m="",t=0): + self.path = p + self.mime = m + self.time = t + + def set_path(self,val): + self.path = val + + def get_path(self): + return self.path + + 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 __cmp__(self,other_item): + return cmp(self.time,other_item.time) + +#------------------------------------------------------------------------- +# +# GnomeRecentFiles +# +#------------------------------------------------------------------------- +class GnomeRecentFiles: + """ + Interface to a GnomeRecentFiles collection """ def __init__(self): - parser = RecentParser() - self.recent_files = parser.get() + gnome_parser = GnomeRecentParser() + self.gnome_recent_files = gnome_parser.get() def add(self,item2add): # First we need to walk the existing items to see # if our item is already there - for item in self.recent_files: + for item in self.gnome_recent_files: if item.get_uri() == item2add.get_uri(): # Found it -- modify timestamp and add all groups # to the item's groups @@ -118,11 +157,11 @@ class RecentFiles: return # At this point we walked the items and not found one, # so simply inserting a new item in the beginning - self.recent_files.insert(0,item2add) + self.gnome_recent_files.insert(0,item2add) def save(self): """ - Attempt saving into XML. + Attempt saving into XML, both for GNOME-wide and GRAMPS files. The trick is not to fail under any circumstances. """ try: @@ -132,16 +171,17 @@ class RecentFiles: def do_save(self): """ - Saves the current RecentFiles collection to the associated file. + Saves the current GNOME RecentFiles collection to the associated file. """ - xml_file = file(os.path.expanduser(FILENAME),'w') + xml_file = file(os.path.expanduser(GNOME_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: + for item in self.gnome_recent_files: + if index > MAX_GNOME_ITEMS: break + index = index + 1 xml_file.write(' \n') xml_file.write(' %s\n' % item.get_uri()) xml_file.write(' %s\n' % item.get_mime()) @@ -159,19 +199,78 @@ class RecentFiles: #------------------------------------------------------------------------- # -# RecentParser +# GrampsRecentFiles # #------------------------------------------------------------------------- -class RecentParser: +class GrampsRecentFiles: """ - Parsing class for the RecentFiles collection. + Interface to a GrampsRecentFiles collection + """ + + def __init__(self): + gramps_parser = GrampsRecentParser() + self.gramps_recent_files = gramps_parser.get() + + def add(self,item2add): + # First we need to walk the existing items to see + # if our item is already there + for item in self.gramps_recent_files: + if item.get_path() == item2add.get_path(): + # Found it -- modify timestamp and add all groups + # to the item's groups + item.set_time(item2add.get_time()) + return + # At this point we walked the items and not found one, + # so simply inserting a new item in the beginning + self.gramps_recent_files.insert(0,item2add) + + def save(self): + """ + Attempt saving into XML. + The trick is not to fail under any circumstances. + """ + try: + self.do_save() + except: + pass + + def do_save(self): + """ + Saves the current GRAMPS RecentFiles collection to the associated file. + """ + xml_file = file(os.path.expanduser(GRAMPS_FILENAME),'w') + fcntl.lockf(xml_file,fcntl.LOCK_EX) + xml_file.write("\n") + xml_file.write('\n') + index = 0 + for item in self.gramps_recent_files: + index = index + 1 + if index > MAX_GRAMPS_ITEMS: + break + xml_file.write(' \n') + xml_file.write(' %s\n' % item.get_path()) + xml_file.write(' %s\n' % item.get_mime()) + xml_file.write(' %d\n' % item.get_time()) + xml_file.write(' \n') + xml_file.write('\n') + fcntl.lockf(xml_file,fcntl.LOCK_UN) + xml_file.close() + +#------------------------------------------------------------------------- +# +# GnomeRecentParser +# +#------------------------------------------------------------------------- +class GnomeRecentParser: + """ + Parsing class for the GnomeRecentParser collection. """ def __init__(self): self.recent_files = [] try: - xml_file = open(os.path.expanduser(FILENAME)) + xml_file = open(os.path.expanduser(GNOME_FILENAME)) fcntl.lockf(xml_file,fcntl.LOCK_SH) p = xml.parsers.expat.ParserCreate() @@ -191,7 +290,7 @@ class RecentParser: def startElement(self,tag,attrs): self.tlist = [] if tag == "RecentItem": - self.item = RecentItem() + self.item = GnomeRecentItem() elif tag == "Groups": self.groups = [] @@ -216,3 +315,90 @@ class RecentParser: def characters(self, data): self.tlist.append(data) + +#------------------------------------------------------------------------- +# +# GrampsRecentParser +# +#------------------------------------------------------------------------- +class GrampsRecentParser: + """ + Parsing class for the GrampsRecentFiles collection. + """ + + def __init__(self): + self.recent_files = [] + + try: + xml_file = open(os.path.expanduser(GRAMPS_FILENAME)) + fcntl.lockf(xml_file,fcntl.LOCK_SH) + + 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() + except: + pass + + def get(self): + return self.recent_files + + def startElement(self,tag,attrs): + self.tlist = [] + if tag == "RecentItem": + self.item = GrampsRecentItem() + + def endElement(self,tag): + + text = ''.join(self.tlist) + + if tag == "RecentItem": + self.recent_files.append(self.item) + elif tag == "Path": + self.item.set_path(text) + elif tag == "Mime-Type": + self.item.set_mime(text) + elif tag == "Timestamp": + self.item.set_time(int(text)) + + def characters(self, data): + self.tlist.append(data) + +#------------------------------------------------------------------------- +# +# Helper function +# +#------------------------------------------------------------------------- +def recent_files(filename,filetype): + """ + Add an entry to both GNOME and GRAMPS recent-items storages. + """ + + the_time = int(time.time()) + # Add the file to the recent items + gnome_rf = GnomeRecentFiles() + gnome_item = GnomeRecentItem( + u='file://%s' % filename, + m=filetype, + t=the_time, + p=False, + g=['Gramps']) + gnome_rf.add(gnome_item) + gnome_rf.save() + + gramps_rf = GrampsRecentFiles() + gramps_item = GrampsRecentItem( + p=filename, + m=filetype, + t=the_time) + gramps_rf.add(gramps_item) + gramps_rf.save() + + + + + diff --git a/gramps2/src/gramps.glade b/gramps2/src/gramps.glade index 8ec647a68..a5c41c755 100644 --- a/gramps2/src/gramps.glade +++ b/gramps2/src/gramps.glade @@ -56,7 +56,7 @@ - + True gtk-new 1 @@ -78,7 +78,7 @@ - + True gtk-open 1 @@ -91,6 +91,15 @@ + + + True + Open _Recent + True + + + + True @@ -106,7 +115,7 @@ - + True gtk-convert 1 @@ -128,7 +137,7 @@ - + True gtk-save-as 1 @@ -165,7 +174,7 @@ - + True gtk-quit 1 @@ -200,7 +209,7 @@ - + True gtk-undo 1 @@ -237,7 +246,7 @@ - + True gtk-add 1 @@ -260,7 +269,7 @@ - + True gtk-remove 1 @@ -298,7 +307,7 @@ - + True gtk-convert 1 @@ -325,7 +334,7 @@ - + True gtk-preferences 1 @@ -346,7 +355,7 @@ - + True gtk-properties 1 @@ -367,7 +376,7 @@ - + True gtk-home 1 @@ -453,7 +462,7 @@ - + True gtk-index 1 @@ -475,7 +484,7 @@ - + True gnome-stock-book-open 1 @@ -548,7 +557,7 @@ - + True gtk-help 1 @@ -569,7 +578,7 @@ - + True gnome-stock-book-open 1 @@ -596,7 +605,7 @@ - + True gtk-jump-to 1 @@ -617,7 +626,7 @@ - + True gnome-stock-mail 1 @@ -671,7 +680,7 @@ - + True gnome-stock-about 1 @@ -1296,7 +1305,7 @@ 0 True - * + * False @@ -4308,7 +4317,7 @@ Other 0 True - * + * False @@ -4501,7 +4510,7 @@ Other 0 True - * + * False @@ -4975,7 +4984,7 @@ Other 0 True - * + * False @@ -5155,7 +5164,7 @@ Other 0 True - * + * False @@ -5567,7 +5576,7 @@ Other 0 True - * + * False @@ -5588,7 +5597,7 @@ Other 0 True - * + * False @@ -5634,7 +5643,7 @@ Other 0 True - * + * False @@ -5655,7 +5664,7 @@ Other 0 True - * + * False @@ -6647,7 +6656,7 @@ Other 0 True - * + * False @@ -6893,7 +6902,7 @@ Other 0 True - * + * False @@ -7365,7 +7374,7 @@ Other 0 True - * + * True @@ -7518,7 +7527,7 @@ Other 0 True - * + * False @@ -7689,7 +7698,7 @@ Other 0 True - * + * False @@ -8383,7 +8392,7 @@ Other 0 True - * + * False @@ -8405,7 +8414,7 @@ Other 0 True - * + * False @@ -8427,7 +8436,7 @@ Other 0 True - * + * False @@ -8599,7 +8608,7 @@ Other 0 True - * + * False @@ -8722,7 +8731,7 @@ Other 0 True - * + * False @@ -8743,7 +8752,7 @@ Other 0 True - * + * False @@ -8869,7 +8878,7 @@ Other 0 True - * + * False @@ -8924,7 +8933,7 @@ Other 0 True - * + * False @@ -8947,7 +8956,7 @@ Other 0 True - * + * False @@ -9000,7 +9009,7 @@ Other 0 True - * + * False @@ -9134,7 +9143,7 @@ Other 0 True - * + * False @@ -12566,7 +12575,7 @@ Other 0 True - * + * False @@ -12690,7 +12699,7 @@ Other 0 True - * + * False @@ -12836,7 +12845,7 @@ Other 0 True - * + * False @@ -12857,7 +12866,7 @@ Other 0 True - * + * False @@ -12968,7 +12977,7 @@ Other 0 True - * + * False @@ -13083,7 +13092,7 @@ Other 0 True - * + * False @@ -13463,7 +13472,7 @@ Other 0 True - * + * False @@ -15196,7 +15205,7 @@ Other 0 True - * + * False @@ -15711,7 +15720,7 @@ Other 0 True - * + * False @@ -15732,7 +15741,7 @@ Other 0 True - * + * False @@ -15753,7 +15762,7 @@ Other 0 True - * + * False @@ -15774,7 +15783,7 @@ Other 0 True - * + * False @@ -15795,7 +15804,7 @@ Other 0 True - * + * False @@ -15816,7 +15825,7 @@ Other 0 True - * + * False @@ -15837,7 +15846,7 @@ Other 0 True - * + * False @@ -15858,7 +15867,7 @@ Other 0 True - * + * False @@ -15928,7 +15937,7 @@ Other 0 True - * + * False @@ -15974,7 +15983,7 @@ Other 0 True - * + * False @@ -18816,7 +18825,7 @@ Other 0 True - * + * False @@ -18837,7 +18846,7 @@ Other 0 True - * + * False @@ -18858,7 +18867,7 @@ Other 0 True - * + * False @@ -18879,7 +18888,7 @@ Other 0 True - * + * False @@ -18900,7 +18909,7 @@ Other 0 True - * + * False @@ -18921,7 +18930,7 @@ Other 0 True - * + * False @@ -18942,7 +18951,7 @@ Other 0 True - * + * False @@ -18963,7 +18972,7 @@ Other 0 True - * + * False @@ -19181,7 +19190,7 @@ Other 0 I True - * + * False @@ -19202,7 +19211,7 @@ Other 0 F True - * + * False @@ -19223,7 +19232,7 @@ Other 0 P True - * + * False @@ -19244,7 +19253,7 @@ Other 0 S True - * + * False @@ -19265,7 +19274,7 @@ Other 0 O True - * + * False @@ -19717,7 +19726,7 @@ Other 0 True - * + * False @@ -20086,7 +20095,7 @@ Other 0 True - * + * False @@ -21384,7 +21393,7 @@ Other 0 True - * + * False @@ -22052,7 +22061,7 @@ Other 0 True - * + * False @@ -22073,7 +22082,7 @@ Other 0 True - * + * False @@ -22094,7 +22103,7 @@ Other 0 True - * + * False @@ -22439,7 +22448,7 @@ Other 0 True - * + * False @@ -22773,7 +22782,7 @@ Other 0 True - * + * False @@ -23303,7 +23312,7 @@ Other 0 True - * + * False @@ -23324,7 +23333,7 @@ Other 0 True - * + * False @@ -23345,7 +23354,7 @@ Other 0 True - * + * False @@ -23366,7 +23375,7 @@ Other 0 True - * + * False @@ -24585,7 +24594,7 @@ Other 0 True - * + * False @@ -24655,7 +24664,7 @@ Other 0 True - * + * False @@ -24682,7 +24691,7 @@ Other 0 True - * + * False @@ -26937,7 +26946,7 @@ Other 0 True - * + * False @@ -26958,7 +26967,7 @@ Other 0 True - * + * False @@ -26979,7 +26988,7 @@ Other 0 True - * + * False @@ -27000,7 +27009,7 @@ Other 0 True - * + * False @@ -28053,7 +28062,7 @@ Other 0 True - * + * False @@ -28723,7 +28732,7 @@ Other 0 True - * + * False @@ -28744,7 +28753,7 @@ Other 0 True - * + * False @@ -28765,7 +28774,7 @@ Other 0 True - * + * False @@ -28786,7 +28795,7 @@ Other 0 True - * + * False @@ -28807,7 +28816,7 @@ Other 0 True - * + * False @@ -28853,7 +28862,7 @@ Other 0 True - * + * False @@ -28899,7 +28908,7 @@ Other 0 True - * + * False @@ -29257,7 +29266,7 @@ Other 0 True - * + * False @@ -29278,7 +29287,7 @@ Other 0 True - * + * False @@ -29299,7 +29308,7 @@ Other 0 True - * + * False @@ -29320,7 +29329,7 @@ Other 0 True - * + * False @@ -29341,7 +29350,7 @@ Other 0 True - * + * False @@ -29362,7 +29371,7 @@ Other 0 True - * + * False @@ -29408,7 +29417,7 @@ Other 0 True - * + * False @@ -30041,7 +30050,7 @@ Other 0 True - * + * False @@ -30063,7 +30072,7 @@ Other 0 True - * + * False @@ -30393,7 +30402,7 @@ Other 0 True - * + * False @@ -30414,7 +30423,7 @@ Other 0 True - * + * False @@ -30435,7 +30444,7 @@ Other 0 True - * + * False @@ -30456,7 +30465,7 @@ Other 0 True - * + * False @@ -30546,7 +30555,7 @@ Other 0 True - * + * False @@ -30626,7 +30635,7 @@ Other 0 True - * + * False @@ -31343,7 +31352,7 @@ Family name Given name 0 True - * + * False @@ -32414,7 +32423,7 @@ Family name Given name 0 True - * + * False diff --git a/gramps2/src/gramps_main.py b/gramps2/src/gramps_main.py index a540658dc..185a93732 100755 --- a/gramps2/src/gramps_main.py +++ b/gramps2/src/gramps_main.py @@ -73,6 +73,7 @@ import TipOfDay import ArgHandler import Exporter import RelImage +import RecentFiles from QuestionDialog import * @@ -269,6 +270,7 @@ class Gramps: self.spouse_tab = self.gtop.get_widget("spouse_tab") self.undolabel = self.gtop.get_widget('undolabel') self.redolabel = self.gtop.get_widget('redolabel') + self.open_recent = self.gtop.get_widget('open_recent1') self.db.set_undo_callback(self.undo_callback) self.db.set_redo_callback(self.redo_callback) @@ -282,6 +284,8 @@ class Gramps: self.build_plugin_menus() self.init_filters() + self.build_recent_menu() + self.toolbar.set_style(GrampsCfg.get_toolbar_style()) self.views.set_show_tabs(0) @@ -404,6 +408,25 @@ class Gramps: self.back = gtk.ImageMenuItem(gtk.STOCK_GO_BACK) self.forward = gtk.ImageMenuItem(gtk.STOCK_GO_FORWARD) + def build_recent_menu(self): + gramps_rf = RecentFiles.GrampsRecentFiles() + gramps_rf.gramps_recent_files.sort() + gramps_rf.gramps_recent_files.reverse() + recent_menu = gtk.Menu() + recent_menu.show() + index = 0 + for item in gramps_rf.gramps_recent_files: + index = index + 1 + name = os.path.basename(item.get_path()) + menu_item = gtk.MenuItem(name,False) + menu_item.connect("activate",self.recent_callback,item.get_path()) + menu_item.show() + recent_menu.append(menu_item) + self.open_recent.set_submenu(recent_menu) + + def recent_callback(self,obj,filename): + print "Will open %s when finished" % filename + def undo_callback(self,text): if text == None: self.undolabel.set_sensitive(0)