~ubuntu-branches/ubuntu/maverick/backintime/maverick

« back to all changes in this revision

Viewing changes to gnomefileicons.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Wiltshire
  • Date: 2009-05-16 23:04:32 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090516230432-orrutvtufbtuxsc6
Tags: 0.9.24-1
* New upstream version (closes: #527447):
  - backintime is no longer aware of 'backintime-gnome' and 'backintime-kde4'
    (you need run 'backintime-gnome' for GNOME version and 'backintime-kde4'
    for KDE4 version)
  - fix a bug that crashes the program after taking a snapshot
* Update homepage field in debian/control (closes: #527595)
* Refactor packaging to fit new upstream build system (an almost entire 
  re-write of debian/rules)
* Make configure scripts use /bin/sh instead of /bin/bash (they don't use
  bash features)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#    Back In Time
2
 
#    Copyright (C) 2008-2009 Oprea Dan
3
 
#
4
 
#    This program is free software; you can redistribute it and/or modify
5
 
#    it under the terms of the GNU General Public License as published by
6
 
#    the Free Software Foundation; either version 2 of the License, or
7
 
#    (at your option) any later version.
8
 
#
9
 
#    This program is distributed in the hope that it will be useful,
10
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
#    GNU General Public License for more details.
13
 
#
14
 
#    You should have received a copy of the GNU General Public License along
15
 
#    with this program; if not, write to the Free Software Foundation, Inc.,
16
 
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 
 
18
 
import os.path
19
 
import pygtk
20
 
pygtk.require("2.0")
21
 
import gtk
22
 
import gnomevfs
23
 
 
24
 
 
25
 
class GnomeFileIcons:
26
 
        def __init__( self ):
27
 
                self.all_icons = gtk.icon_theme_get_default().list_icons()
28
 
                self.cache = {}
29
 
                self.user_path = os.path.expanduser( '~' )
30
 
                self.special_folders = {}
31
 
 
32
 
                self.update_special_folder_icons()
33
 
 
34
 
        def add_special_folder_( self, path, icon_name ):
35
 
                if len( path ) <= 0:
36
 
                        return
37
 
 
38
 
                if not icon_name in self.all_icons:
39
 
                        icon_name = 'folder'
40
 
 
41
 
                self.special_folders[ path ] = icon_name
42
 
 
43
 
        def get_special_folder_path( self, name ):
44
 
                path = ''
45
 
 
46
 
                try:
47
 
                        pipe = os.popen( "cat %s/.config/user-dirs.dirs 2>/dev/null | grep %s=" % ( self.user_path, name ), 'r' )
48
 
                        path = pipe.read()
49
 
                        pipe.close()
50
 
                except:
51
 
                        pass
52
 
 
53
 
                if len( path ) <= 0:
54
 
                        return ''
55
 
 
56
 
                path = path[ len( name ) + 1 : ]
57
 
                if path[ 0 ] == '"':
58
 
                        path = path[ 1 : -2 ]
59
 
 
60
 
                path = path.replace( '$HOME', self.user_path )
61
 
                return path
62
 
 
63
 
        def update_special_folder_icons( self ):
64
 
                self.special_folders = {}
65
 
 
66
 
                #add desktop
67
 
                self.add_special_folder_( self.get_special_folder_path( 'XDG_DESKTOP_DIR' ), 'user-desktop' )
68
 
                
69
 
                #add home
70
 
                self.add_special_folder_( self.user_path, 'user-home' )
71
 
                
72
 
        def get_icon( self, path ):
73
 
                if not os.path.exists(path):
74
 
                        return gtk.STOCK_FILE
75
 
 
76
 
                #check if it is a special folder
77
 
                if path in self.special_folders:
78
 
                        return self.special_folders[path]
79
 
 
80
 
                #get mime type
81
 
                mime_type = gnomevfs.get_mime_type( gnomevfs.escape_path_string( path ) )
82
 
                mime_type = mime_type.replace( '/', '-' )
83
 
 
84
 
                #search in the cache
85
 
                if mime_type in self.cache:
86
 
                        return self.cache[mime_type]
87
 
 
88
 
                #print "path: " + path
89
 
                #print "mime: " + mime_type
90
 
 
91
 
                #try gnome mime
92
 
                items = mime_type.split('-')
93
 
                for aux in xrange(len(items)-1):
94
 
                        icon_name = "gnome-mime-" + '-'.join(items[:len(items)-aux])
95
 
                        if icon_name in self.all_icons:
96
 
                                #print "icon: " + icon_name
97
 
                                self.cache[mime_type] = icon_name
98
 
                                return icon_name
99
 
 
100
 
                #try folder
101
 
                if os.path.isdir( path ):
102
 
                        icon_name = 'folder'
103
 
                        if icon_name in self.all_icons:
104
 
                                #print "icon: " + icon_name
105
 
                                self.cache[mime_type] = icon_name
106
 
                                return icon_name
107
 
 
108
 
                        #print "icon: " + icon_name
109
 
                        icon_name = gtk.STOCK_DIRECTORY
110
 
                        self.cache[mime_type] = icon_name
111
 
                        return icon_name
112
 
 
113
 
                #try simple mime
114
 
                for aux in xrange(len(items)-1):
115
 
                        icon_name = '-'.join(items[:len(items)-aux])
116
 
                        if icon_name in self.all_icons:
117
 
                                #print "icon: " + icon_name
118
 
                                self.cache[mime_type] = icon_name
119
 
                                return icon_name
120
 
 
121
 
                #file icon
122
 
                icon_name = gtk.STOCK_FILE
123
 
                self.cache[mime_type] = icon_name
124
 
                return icon_name
125