summaryrefslogtreecommitdiff
path: root/trunk/dimbola/plugins/folderlist_plugin.py
diff options
context:
space:
mode:
Diffstat (limited to 'trunk/dimbola/plugins/folderlist_plugin.py')
-rw-r--r--trunk/dimbola/plugins/folderlist_plugin.py129
1 files changed, 129 insertions, 0 deletions
diff --git a/trunk/dimbola/plugins/folderlist_plugin.py b/trunk/dimbola/plugins/folderlist_plugin.py
new file mode 100644
index 0000000..1f08de3
--- /dev/null
+++ b/trunk/dimbola/plugins/folderlist_plugin.py
@@ -0,0 +1,129 @@
+# Copyright (C) 2009 Lars Wirzenius <liw@liw.fi>
+#
+# 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 3 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, see <http://www.gnu.org/licenses/>.
+
+
+# This is necessary for running under Python 2.5, which we need to
+# do on Debian, for now.
+from __future__ import with_statement
+
+
+import os
+
+import gobject
+import gtk
+
+import dimbola
+
+
+class FolderList(dimbola.Plugin):
+
+ '''Show list of folders with imported photos in left sidebar.'''
+
+ ID_COL = 0
+ NAME_COL = 1
+
+ def __init__(self, mwc):
+ self.mwc = mwc
+ mwc.connect('setup-widgets', self.setup_widgets)
+ self.tagids = set()
+ self.store = gtk.TreeStore(gobject.TYPE_INT, gobject.TYPE_STRING)
+
+ mwc.new_hook('folder-selection-changed', gobject.TYPE_NONE,
+ (gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT))
+
+ def enable(self):
+ self.mwc.add_to_sidebar('left_sidebar', 'folderlist_expander',
+ weight=dimbola.MIN_WEIGHT,
+ expand=True, fill=True)
+ self.enable_signal(self.mwc, 'db-changed', self.db_changed)
+
+ def disable(self):
+ self.mwc.remove_from_sidebar('left_sidebar', 'folderlist_expander')
+ self.disable_signals()
+
+ def setup_widgets(self, mwc):
+ cr = gtk.CellRendererText()
+ col = gtk.TreeViewColumn()
+ col.pack_start(cr)
+ col.add_attribute(cr, 'text', self.NAME_COL)
+ self.treeview = self.mwc.widgets['folders_treeview']
+ self.treeview.append_column(col)
+ self.treeview.set_model(self.store)
+
+ sel = self.treeview.get_selection()
+ sel.set_mode(gtk.SELECTION_MULTIPLE)
+ sel.connect('changed', self.selection_changed)
+
+ def db_changed(self, mwc):
+ self.refresh_store()
+
+ def refresh_store(self):
+ tb = dimbola.TreeBuilder()
+ fakes = dict()
+ with self.mwc.db:
+ photoids = self.mwc.db.find_photoids()
+ for photoid in photoids:
+ folderid, b, c, d = \
+ self.mwc.db.get_basic_photo_metadata(photoid)
+ foldername = self.mwc.db.get_folder_name(folderid)
+ parentname = os.path.dirname(foldername)
+ parentid = self.mwc.db.find_folder(parentname)
+ if parentid is None:
+ parentid = self.make_fake_parent(fakes, parentname)
+ basename = os.path.basename(foldername)
+ tb.add(folderid, basename, basename, parentid)
+ for fakename in fakes:
+ parentname = os.path.dirname(fakename)
+ parentid = fakes[parentname]
+ fakeid = fakes[fakename]
+ if parentid == fakeid:
+ parentid = None
+ basename = os.path.basename(fakename) or os.sep
+ tb.add(fakeid, basename, basename, parentid)
+ tb.done()
+ self.store.clear()
+ self.populate_treemodel(tb.tree)
+ self.treeview.expand_all()
+
+ def make_fake_parent(self, fakes, parentname):
+ if parentname not in fakes:
+ fakes[parentname] = -len(fakes) - 1
+ self.make_fake_parent(fakes, os.path.dirname(parentname))
+ return fakes[parentname]
+
+ def populate_treemodel(self, nodes, parent_iter=None):
+ for node in nodes:
+ folderid, foldername, children = node
+ it = self.store.append(parent_iter, (folderid, foldername))
+ self.populate_treemodel(children, parent_iter=it)
+
+ def photos_in_selected_folders(self):
+ '''Return photoids for all currently selected folders.'''
+ sel = self.treeview.get_selection()
+ model, paths = sel.get_selected_rows()
+ folderids = list()
+ photoids = list()
+ with self.mwc.db:
+ for path in paths:
+ it = self.store.get_iter(path)
+ folderid = self.store.get_value(it, self.ID_COL)
+ photoids += self.mwc.db.find_photoids_in_folder(folderid)
+ folderids.append(folderid)
+ return folderids, photoids
+
+ def selection_changed(self, *args):
+ folderids, photoids = self.photos_in_selected_folders()
+ self.mwc.emit('folder-selection-changed', folderids, photoids)
+