~ubuntu-branches/ubuntu/maverick/awn-extras-applets/maverick

« back to all changes in this revision

Viewing changes to applets/unmaintained/stacks/stacks_backend.py

  • Committer: Bazaar Package Importer
  • Author(s): Julien Lavergne
  • Date: 2010-08-29 14:29:52 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: james.westby@ubuntu.com-20100829142952-rhvuetyms9bv5uu7
Tags: upstream-0.4.0+bzr1372
ImportĀ upstreamĀ versionĀ 0.4.0+bzr1372

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
 
 
3
2
# Copyright (c) 2007 Timon ter Braak
4
3
#
5
4
# This library is free software; you can redistribute it and/or
17
16
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
17
# Boston, MA 02111-1307, USA.
19
18
 
20
 
import sys
21
19
import os
22
 
import gobject
 
20
import random
 
21
 
23
22
import gtk
24
23
from gtk import gdk
25
 
import random
26
 
import gnome.ui
27
 
import gnomevfs
28
 
import gnomedesktop
 
24
 
29
25
from awn.extras import _
30
26
from desktopagnostic.config import GROUP_DEFAULT
 
27
from desktopagnostic import fdo, vfs
 
28
 
 
29
import gio
 
30
import gobject
31
31
 
32
32
from stacks_vfs import VfsUri, Monitor
33
33
from stacks_icons import IconFactory, Thumbnailer
58
58
COL_ICON = 5
59
59
COL_BUTTON = 6
60
60
 
 
61
 
61
62
class Backend(gobject.GObject):
62
63
 
63
64
    applet = None           # ref to the applet
93
94
        else:
94
95
                self.store.set_sort_func(COL_URI, self._file_sort)
95
96
 
96
 
 
97
97
    # we use a sorted liststore.
98
98
    # this sort function sorts:
99
99
    # -directories first
102
102
    def _file_sort(self, model, iter1, iter2):
103
103
        t1 = model.get_value(iter1, COL_TYPE)
104
104
        t2 = model.get_value(iter2, COL_TYPE)
105
 
        if self.applet.config['sort_folders_before_files'] and t1 == gnomevfs.FILE_TYPE_DIRECTORY and not \
106
 
                t2 == gnomevfs.FILE_TYPE_DIRECTORY:
 
105
        if self.applet.config['sort_folders_before_files'] and t1 == gio.FILE_TYPE_DIRECTORY and not \
 
106
                t2 == gio.FILE_TYPE_DIRECTORY:
107
107
            return -1
108
 
        elif self.applet.config['sort_folders_before_files'] and t2 == gnomevfs.FILE_TYPE_DIRECTORY and not \
109
 
                t1 == gnomevfs.FILE_TYPE_DIRECTORY:
 
108
        elif self.applet.config['sort_folders_before_files'] and t2 == gio.FILE_TYPE_DIRECTORY and not \
 
109
                t1 == gio.FILE_TYPE_DIRECTORY:
110
110
            return 1
111
111
        else:
112
112
            n1 = model.get_value(iter1, COL_LABEL)
118
118
            else:
119
119
                return cmp(n2, n1)
120
120
 
121
 
 
122
121
    # this sort function sorts:
123
122
    # -directories first
124
123
    # -sort by date
125
124
    def _file_sort_date(self, model, iter1, iter2):
126
125
        t1 = model.get_value(iter1, COL_TYPE)
127
126
        t2 = model.get_value(iter2, COL_TYPE)
128
 
        if self.applet.config['sort_folders_before_files'] and t1 == gnomevfs.FILE_TYPE_DIRECTORY and not \
129
 
                t2 == gnomevfs.FILE_TYPE_DIRECTORY:
 
127
        if self.applet.config['sort_folders_before_files'] and t1 == gio.FILE_TYPE_DIRECTORY and not \
 
128
                t2 == gio.FILE_TYPE_DIRECTORY:
130
129
            return -1
131
 
        elif self.applet.config['sort_folders_before_files'] and t2 == gnomevfs.FILE_TYPE_DIRECTORY and not \
132
 
                t1 == gnomevfs.FILE_TYPE_DIRECTORY:
 
130
        elif self.applet.config['sort_folders_before_files'] and t2 == gio.FILE_TYPE_DIRECTORY and not \
 
131
                t1 == gio.FILE_TYPE_DIRECTORY:
133
132
            return 1
134
133
        else:
135
 
                        u1 = model.get_value(iter1, COL_URI)
136
 
                        u2 = model.get_value(iter2, COL_URI)
137
 
                        i1 = gnomevfs.get_file_info(
138
 
                                        u1.uri,
139
 
                                        gnomevfs.FILE_INFO_DEFAULT |
140
 
                                        gnomevfs.FILE_INFO_FOLLOW_LINKS )
141
 
                        c1 = i1.mtime
142
 
                        i2 = gnomevfs.get_file_info(
143
 
                                        u2.uri,
144
 
                                        gnomevfs.FILE_INFO_DEFAULT |
145
 
                                        gnomevfs.FILE_INFO_FOLLOW_LINKS )
146
 
                        c2 = i2.mtime
 
134
            u1 = model.get_value(iter1, COL_URI)
 
135
            u2 = model.get_value(iter2, COL_URI)
 
136
            i1 = u1.uri.query_info(gio.FILE_ATTRIBUTE_TIME_MODIFIED, 0)
 
137
            c1 = i1.get_modification_time()
 
138
            i2 = u2.uri.query_info(gio.FILE_ATTRIBUTE_TIME_MODIFIED, 0)
 
139
            c2 = i2.get_modification_time()
147
140
 
148
 
                        if self.applet.config['sort_direction'] == BACKEND_SORT_ASCENDING:
149
 
                                return cmp(c1, c2)
150
 
                        else:
151
 
                                return cmp(c2, c1)                      
 
141
            if self.applet.config['sort_direction'] == BACKEND_SORT_ASCENDING:
 
142
                return cmp(c1, c2)
 
143
            else:
 
144
                return cmp(c2, c1)
152
145
 
153
146
    # emits attention signal
154
147
    def _get_attention(self):
155
148
        self.emit("attention", self.get_type())
156
149
 
157
 
 
158
150
    def _created_cb(self, widget, vfs_uri):
159
151
        assert isinstance(vfs_uri, VfsUri)
160
152
        if self.add([vfs_uri]):
161
153
             self._get_attention()
162
154
 
163
155
 
164
 
    def _deleted_cb(self, widget, vfs_uri):
 
156
    def _deleted_cb(self, monitor, vfs_uri, is_dir_monitor=None):
165
157
        assert isinstance(vfs_uri, VfsUri)
 
158
        if not is_dir_monitor:
 
159
            monitor.close()
166
160
        if self.remove([vfs_uri]):
167
161
             self._get_attention()
168
162
 
176
170
        for vfs_uri in vfs_uris:
177
171
            uri = vfs_uri.as_uri()
178
172
            path = vfs_uri.as_string()
179
 
            name = uri.short_name
 
173
            name = uri.get_basename()
180
174
            mime_type = ""
181
175
            pixbuf = None
182
 
            
183
 
            
184
176
 
185
177
            # check for existence:
186
 
            if uri.scheme == "file" and not gnomevfs.exists(uri):
 
178
            if not uri.query_exists():
187
179
                continue
188
180
 
189
181
            # check for duplicates
199
191
 
200
192
            # check for desktop item
201
193
            if name.endswith(".desktop"):
202
 
                item = gnomedesktop.item_new_from_uri(
203
 
                        path, gnomedesktop.LOAD_ONLY_IF_EXISTS)
204
 
                if not item:
 
194
                file = vfs.File.for_uri(path)
 
195
 
 
196
                if file is None or not file.exists():
205
197
                    continue
206
 
                command = item.get_string(gnomedesktop.KEY_EXEC)
207
 
                name = item.get_localestring(gnomedesktop.KEY_NAME)
208
 
                mime_type = item.get_localestring(gnomedesktop.KEY_MIME_TYPE)
209
 
                type = gnomevfs.FILE_TYPE_REGULAR
210
 
                icon_name = item.get_localestring(gnomedesktop.KEY_ICON)
211
 
                icon_uri = None
 
198
 
 
199
                entry = fdo.DesktopEntry.for_file (file)
 
200
 
 
201
                name = entry.get_name()
 
202
                icon_name = entry.get_icon() or "image-missing"
 
203
                mime_type = ""
 
204
                type = gio.FILE_TYPE_REGULAR
 
205
 
212
206
                if icon_name:
213
 
                    icon_uri = gnomedesktop.find_icon(
214
 
                                        gtk.icon_theme_get_default(),
215
 
                                        icon_name,
216
 
                                        self.icon_size,
217
 
                                        0)
218
 
                    if not icon_uri:
219
 
                        icon_uri = path
 
207
                    icon_info = gtk.icon_theme_get_default().lookup_icon(icon_name, self.icon_size, 0)
 
208
                    icon_uri = icon_info.get_filename() if icon_info else path
220
209
                    pixbuf = IconFactory().load_icon(icon_uri, self.icon_size)
221
 
                    if pixbuf:
222
 
                        pixbuf.add_alpha (True, '\0', '\0', '\0')
 
210
                if pixbuf:
 
211
                    pixbuf.add_alpha (True, '\0', '\0', '\0')
223
212
            else:
224
213
                # get file info
225
214
                try:
226
 
                    fileinfo = gnomevfs.get_file_info(
227
 
                            path,
228
 
                            gnomevfs.FILE_INFO_DEFAULT |
229
 
                            gnomevfs.FILE_INFO_GET_MIME_TYPE |
230
 
                            gnomevfs.FILE_INFO_FORCE_SLOW_MIME_TYPE |
231
 
                            gnomevfs.FILE_INFO_FOLLOW_LINKS )
232
 
                    type = fileinfo.type
233
 
                    mime_type = fileinfo.mime_type
234
 
                except gnomevfs.NotFoundError:
 
215
                    fileinfo = uri.query_info(','.join([
 
216
                        gio.FILE_ATTRIBUTE_STANDARD_TYPE,
 
217
                        gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
 
218
                        gio.FILE_ATTRIBUTE_STANDARD_IS_HIDDEN,
 
219
                        gio.FILE_ATTRIBUTE_STANDARD_IS_BACKUP]))
 
220
                    if fileinfo.get_is_hidden() or fileinfo.get_is_backup():
 
221
                        continue
 
222
                    type = fileinfo.get_file_type()
 
223
                    mime_type = fileinfo.get_content_type()
 
224
                except:
235
225
                    continue
236
226
                # get pixbuf for icon
237
227
                pixbuf = Thumbnailer(path, mime_type).get_icon(self.icon_size)
238
228
                if pixbuf:
239
229
                        pixbuf.add_alpha (True, '\0', '\0', '\0')
240
 
            
 
230
 
241
231
            # create monitor
242
232
            try:
243
233
                monitor = Monitor(vfs_uri)
244
234
                monitor.connect("deleted", self._deleted_cb)
245
 
            except gnomevfs.NotSupportedError:
 
235
            except:
246
236
                monitor = None
247
237
 
248
 
            
249
238
            # add to store
250
 
            
251
239
            iter = self.store.append([vfs_uri, monitor, type, name, mime_type, pixbuf, None])
252
 
                
 
240
 
253
241
            if self.store.iter_is_valid(iter):
254
242
                self.emit("item-created", iter)
255
243
            else:
256
244
                print "ERROR in STACK: iter is NOK (stacks_backend.py)"
257
 
            
258
245
 
259
246
            # return pixbuf later?
260
 
            if pixbuf: retval = pixbuf
261
 
            
262
 
            
 
247
            if pixbuf:
 
248
                retval = pixbuf
263
249
 
264
250
        # restructure of dialog needed
265
251
        return (retval is not None)
266
252
 
267
 
 
268
253
    # remove file from store
269
254
    def remove(self, vfs_uris):
270
255
        changed = False
282
267
    def read(self):
283
268
        return
284
269
 
285
 
 
286
270
    def clear(self):
287
271
        self.store.clear()
288
272
        self.emit("item-removed", None)
289
273
 
290
 
 
291
274
    def open(self):
292
275
        return
293
276
 
294
 
 
295
277
    def is_empty(self):
296
278
        iter = self.store.get_iter_first()
297
279
        return not (iter and self.store.iter_is_valid(iter))
298
280
 
299
 
 
300
281
    def get_title(self):
301
282
        title = self.applet.client.get_string(GROUP_DEFAULT, "title")
302
283
 
304
285
            title = _("Stacks")
305
286
        return title;
306
287
 
307
 
 
308
288
    def get_number_items(self):
309
289
        return self.store.iter_n_children(None)
310
290
 
311
 
 
312
291
    def get_menu_items(self):
313
292
        return []
314
293
 
315
 
 
316
294
    def get_type(self):
317
295
        return stacksbackend.BACKEND_TYPE_INVALID
318
296
 
319
 
 
320
297
    def get_random_pixbuf(self):
321
298
        max = self.get_number_items()
322
299
        rand = random.Random()
325
302
        if not iter: return None
326
303
        return self.store.get_value(iter, COL_ICON)
327
304
 
328
 
 
329
305
    def get_store(self):
330
306
        return self.store
331
307
 
332
 
 
333
308
    def destroy(self):
334
309
        return