[kupfer: 2/3] openoffice: use FilesystemWatchMixin; read all existing history files



commit 4992e007ff17f4666669240868580141963bf96a
Author: Karol BÄ?dkowski <karol bedkowski gmail com>
Date:   Thu Apr 7 21:09:59 2011 +0200

    openoffice: use FilesystemWatchMixin; read all existing history files
    
    This patch allow to read all configuration files - openoffice and
    libreoffice - when exists. Should help when user have both *offices.

 kupfer/plugin/openoffice.py |  113 +++++++++++++++++++-----------------------
 1 files changed, 51 insertions(+), 62 deletions(-)
---
diff --git a/kupfer/plugin/openoffice.py b/kupfer/plugin/openoffice.py
index 61633d5..fb407d9 100644
--- a/kupfer/plugin/openoffice.py
+++ b/kupfer/plugin/openoffice.py
@@ -3,7 +3,7 @@
 __kupfer_name__ = _("OpenOffice / LibreOffice")
 __kupfer_sources__ = ("RecentsSource", )
 __description__ = _("Recently used documents in OpenOffice/LibreOffice")
-__version__ = "2011-04-02"
+__version__ = "2011-04-07"
 __author__ = "Karol BÄ?dkowski <karol bedkowski gmail com>"
 
 '''
@@ -16,13 +16,13 @@ from xml.etree import cElementTree as ElementTree
 import gio
 
 from kupfer.objects import Source, FileLeaf, UrlLeaf, AppLeaf
-from kupfer.obj.helplib import PicklingHelperMixin
+from kupfer.obj.helplib import FilesystemWatchMixin
 
 
 _HISTORY_FILE = [
+		"~/.libreoffice/3/user/registrymodifications.xcu",
 		"~/.openoffice.org/3/user/registrymodifications.xcu",
 		"~/.openoffice.org/3/user/registry/data/org/openoffice/Office/Histories.xcu",
-		"~/.libreoffice/3/user/registrymodifications.xcu",
 ]
 _NAME_ATTR = "{http://openoffice.org/2001/registry}name";
 _PATH_ATTR = "{http://openoffice.org/2001/registry}path";
@@ -62,7 +62,7 @@ class MultiAppContentMixin (object):
 			return cls()
 
 
-class RecentsSource (MultiAppContentMixin, Source, PicklingHelperMixin):
+class RecentsSource (MultiAppContentMixin, Source, FilesystemWatchMixin):
 	appleaf_content_id = [
 			"openoffice.org-writer",
 			"openoffice.org-base",
@@ -83,64 +83,53 @@ class RecentsSource (MultiAppContentMixin, Source, PicklingHelperMixin):
 	def __init__(self, name=_("OpenOffice/LibreOffice Recent Items")):
 		Source.__init__(self, name)
 
-	def pickle_prepare(self):
-		self.monitor = None
-
 	def initialize(self):
-		hist_file_path = _get_history_file_path()
-		if not hist_file_path:
-			return
-		gfile = gio.File(hist_file_path)
-		self.monitor = gfile.monitor_file(gio.FILE_MONITOR_NONE, None)
-		if self.monitor:
-			self.monitor.connect("changed", self._on_history_changed)
-
-	def _on_history_changed(self, monitor, file1, file2, evt_type):
-		if evt_type in (gio.FILE_MONITOR_EVENT_CREATED,
-				gio.FILE_MONITOR_EVENT_DELETED,
-				gio.FILE_MONITOR_EVENT_CHANGED):
-			self.mark_for_update()
+		hist_file_path = _get_history_files()
+		if hist_file_path:
+			dirs = map(os.path.dirname, hist_file_path)
+			self.monitor_token = self.monitor_directories(*dirs)
+
+	def monitor_include_file(self, gfile):
+		return gfile and gfile.get_basename().endswith('.xcu')
 
 	def get_items(self):
-		hist_file_path = _get_history_file_path()
-		print hist_file_path
-		if not hist_file_path:
-			return
-		try:
-			tree = ElementTree.parse(hist_file_path)
-			node_histories = tree.find('node')
-			if node_histories and node_histories.attrib[_NAME_ATTR] == 'Histories':
-				for list_node in  node_histories.findall('node'):
-					if list_node.attrib[_NAME_ATTR] == 'PickList':
-						items_node = list_node.find('node')
-						if (not items_node \
-								or items_node.attrib[_NAME_ATTR] != 'ItemList'):
-							return
-						for node in items_node.findall('node'):
-							hfile = node.attrib[_NAME_ATTR]  # file://.....
-							leaf = _create_history_leaf(hfile)
-							if leaf:
-								yield leaf
-						break
-			# libreoffice new configuration file
-			for item in tree.getroot().findall('item'):
-				if item.get(_PATH_ATTR) != _HISTORY_NODES:
-					continue
-				node = item.find('node')
-				if not node:
-					continue
-				prop = node.find('prop')
-				if not prop:
-					continue
-				if prop.get(_NAME_ATTR) != 'HistoryItemRef':
-					continue
-				value = prop.find('value')
-				if value is not None:
-					leaf = _create_history_leaf(value.text)
-					if leaf:
-						yield leaf
-		except StandardError, err:
-			self.output_error(err)
+		for hist_file_path in _get_history_files():
+			self.output_debug('reading', hist_file_path)
+			try:
+				tree = ElementTree.parse(hist_file_path)
+				node_histories = tree.find('node')
+				if node_histories and node_histories.attrib[_NAME_ATTR] == 'Histories':
+					for list_node in  node_histories.findall('node'):
+						if list_node.attrib[_NAME_ATTR] == 'PickList':
+							items_node = list_node.find('node')
+							if (not items_node \
+									or items_node.attrib[_NAME_ATTR] != 'ItemList'):
+								return
+							for node in items_node.findall('node'):
+								hfile = node.attrib[_NAME_ATTR]  # file://.....
+								leaf = _create_history_leaf(hfile)
+								if leaf:
+									yield leaf
+							break
+				#  new configuration file
+				for item in tree.getroot().findall('item'):
+					if item.get(_PATH_ATTR) != _HISTORY_NODES:
+						continue
+					node = item.find('node')
+					if not node:
+						continue
+					prop = node.find('prop')
+					if not prop:
+						continue
+					if prop.get(_NAME_ATTR) != 'HistoryItemRef':
+						continue
+					value = prop.find('value')
+					if value is not None:
+						leaf = _create_history_leaf(value.text)
+						if leaf:
+							yield leaf
+			except StandardError, err:
+				self.output_error(err)
 
 	def get_description(self):
 		return _("Recently used documents in OpenOffice/LibreOffice")
@@ -153,12 +142,12 @@ class RecentsSource (MultiAppContentMixin, Source, PicklingHelperMixin):
 		yield UrlLeaf
 
 
-def _get_history_file_path():
+def _get_history_files():
+	''' get all existing files with history '''
 	for file_path in _HISTORY_FILE:
 		path = os.path.expanduser(file_path)
 		if os.path.isfile(path):
-			return path
-	return None
+			yield path
 
 
 def _create_history_leaf(path):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]