1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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)
|