/
FileManager.py
217 lines (171 loc) · 6.32 KB
/
FileManager.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
""" Manage the ListStore behind the main icon view and all related functions
* file system browsing
* querying the index
* thumbnailing
"""
import os
import gtk
import re
import gobject
import urllib
import cgi
from Indexer import Indexer;
from GeneratorTask import GeneratorTask;
class FileManager:
COL_TITLE = 0
COL_PATH = 1
COL_PIXBUF = 2
COL_TYPE = 3
store = None
root = None
index = None
thumbnailer = None
def __init__(self,root):
self.store = gtk.ListStore(str, str, gtk.gdk.Pixbuf, str)
self.root = os.path.abspath(root)
self.index = Indexer(self.root)
def search(self,query):
""" Search the index for the given query
query needs to be something the Whoosh query parser can parse,
otherwise Whoosh exceptions are bubbled up
"""
self.stop_thumbnailer()
self.store.clear()
results = self.index.search(unicode(query))
for i, fields in enumerate(results):
title = fields['title']
if(title == ''):
title = os.path.basename(fields['path']);
self.store.append([
title,
fields['path'],
self.get_icon(gtk.STOCK_FILE),
'jpg'])
self.start_thumbnailer()
def browse(self,folder):
""" Browse the given folder
folder needs to exist and be relative to the library root,
otherwise a NoDirException is thrown
"""
self.stop_thumbnailer()
self.store.clear()
folder = folder.replace('..','')
full = os.path.join(self.root,folder)
full = os.path.abspath(full)
imgre = re.compile('\.jpe?g$',re.IGNORECASE)
if(not os.path.isdir(full)):
raise NoDirException("No such directory in library: "+folder)
# add upper dir
if(folder):
upper = os.path.dirname(folder);
self.store.append([
'..',
upper,
self.get_icon(gtk.STOCK_GO_UP),
'dir'])
for fl in os.listdir(full):
if fl[0] == '.':
continue; #skip hidden files
fn = os.path.join(full,fl)
rel = os.path.relpath(fn,self.root)
title = os.path.basename(fn)
if(os.path.isdir(fn)):
self.store.append([
title,
rel,
self.get_icon(gtk.STOCK_DIRECTORY),
'dir'])
elif(imgre.search(fn)):
self.store.append([
title,
rel,
self.get_icon(gtk.STOCK_FILE),
'jpg'])
self.start_thumbnailer()
def start_thumbnailer(self):
""" Start thumbnailing for the current ListStore
Thumbnailing is done in a separate thread
"""
self.stop_thumbnailer()
self.thumbnailer = GeneratorTask(self._create_thumbnails)
self.thumbnailer.start()
def stop_thumbnailer(self):
""" Stop any running thumbnailer
Always call this before the ListStore is cleared!
"""
if self.thumbnailer is not None:
self.thumbnailer.stop()
self.thumbnailer.wait()
self.thumbnailer = None
def _create_thumbnails(self):
""" The thumbnailing process
FIXME: reading addtional image info from exif might be
sensible here
"""
for row in self.store:
path = row[self.COL_PATH]
ftype = row[self.COL_TYPE]
fn = os.path.join(self.root,path)
if(ftype == 'jpg'):
buf = gtk.gdk.pixbuf_new_from_file_at_size(fn, 48, 48)
row[self.COL_PIXBUF] = buf
yield None
def get_itemat(self,pos):
""" Return the item at the given postion """
row = self.store[pos];
path = row[self.COL_PATH]
ftype = row[self.COL_TYPE]
fn = os.path.join(self.root,path)
return {'fn':fn, 'ft': ftype}
def get_nextimagepos(self,pos):
""" Get the position of the next image (not dir) after the given
position. If the given positon is None, the search sats at the
beginning of the store
FIXME there is probably a much more elegant way doing the
whole iteration stuff, but I can't figure it out
"""
if(pos == None):
pos = 0;
else:
pos = pos+1;
try:
rowiter = self.store.get_iter(pos);
while rowiter != None:
if(self.store.get_value(rowiter,self.COL_TYPE) == 'jpg'):
return self.store.get_path(rowiter)[0];
self.store.iter_next(rowiter);
except ValueError: # we're out of range
pass
return None
def get_previmagepos(self,pos):
""" Get the position of the next image (not dir) before the given
position.
FIXME there is probably a much more elegant way. And I have no
idea how to iterate backwards anyway
"""
while (pos >=0):
pos -= 1;
try:
rowiter = self.store.get_iter(pos);
if(self.store.get_value(rowiter,self.COL_TYPE) == 'jpg'):
return self.store.get_path(rowiter)[0];
except ValueError: # we're out of range
pass
return None
def get_icon(self, name):
""" Helper to load a stock icon """
theme = gtk.icon_theme_get_default()
return theme.load_icon(name, 48, 0)
def get_tagcloudstring(self):
tags = self.index.tagcloud()
cloud = ''
for tag in sorted(tags):
# style = 'size="%d" underline="none" foreground="blck"' % (5 + (tags[tag]*10))
# cloud += '<a href="tags:'+urllib.quote('"'+tag+'"')+'" underline="none" foreground="black">'+cgi.escape(tag)+'</a> ';
cloud += '<a href="%s"><span size="%d" underline="none" foreground="black">%s</span></a> ' % (
urllib.quote('"'+tag+'"'),
(10 + (tags[tag]*5))*1000,
cgi.escape(tag) )
return cloud
class NoDirException (StandardError):
pass