[kupfer: 2/12] zim: search for default notebook when adding new page



commit 5116aa0845f903b3c4389f107c1edbe5d5367b48
Author: Karol BÄ?dkowski <karol bedkowsk+gh gmail com>
Date:   Sat Feb 6 22:39:58 2010 +0100

    zim: search for default notebook when adding new page
    
    Old version of zim (pre-pyzim) don't acccept '' as default notebook, so
    we must found default notebook (marked as _default_) or get first defined.

 kupfer/plugin/zim.py |   25 ++++++++++++++++++++++++-
 1 files changed, 24 insertions(+), 1 deletions(-)
---
diff --git a/kupfer/plugin/zim.py b/kupfer/plugin/zim.py
index 2844daa..1c6e189 100644
--- a/kupfer/plugin/zim.py
+++ b/kupfer/plugin/zim.py
@@ -67,7 +67,8 @@ class CreateZimPage(Action):
 		Action.__init__(self, _('Create Zim Page'))
 
 	def activate(self, leaf):
-		_start_zim("", ":" + leaf.object.strip(':'))
+		notebook = _get_default_notebook()
+		_start_zim(notebook, ":" + leaf.object.strip(':'))
 
 	def get_description(self):
 		return _("Create page in default notebook")
@@ -153,6 +154,28 @@ def _read_zim_notebooks_old(zim_notebooks_file):
 				notebook_path = os.path.expanduser(notebook_path)
 				yield (notebook_name, notebook_path)
 
+
+def _get_default_notebook():
+	''' Find default notebook '''
+	zim_notebooks_file = config.get_config_file("notebooks.list", package="zim")
+	if not zim_notebooks_file:
+		pretty.print_error(__name__, "Zim notebooks.list not found")
+		return None
+	with open(zim_notebooks_file, 'r') as notebooks_file:
+		for line in notebooks_file.readlines():
+			if line.strip() == "[NotebookList]":
+				# new file format == pyzim
+				return ''
+			if line.strip() != '_default_': # when no default notebook
+				notebook_name, notebook_path = line.strip().split('\t', 2)
+				if notebook_name == '_default_':
+					# _default_ is pointing at name of the default notebook
+					return notebook_path.decode("UTF-8", "replace")
+				else:
+					# assume first notebook as default
+					return notebook_name.decode("UTF-8", "replace")
+
+
 def _read_zim_notebook_name(notebook_path):
 	npath = os.path.join(notebook_path, "notebook.zim")
 	with open(npath, "r") as notebook_file:



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