~gilir/menulibre/gi-repository-fix

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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) 2012 Sean Davis <smd.seandavis@gmail.com>
# This program is free software: you can redistribute it and/or modify it 
# under the terms of the GNU General Public License version 3, as published 
# by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
# MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
### END LICENSE

import os

from gi.repository import Gtk

home = os.getenv('HOME')

default_application = """
[Desktop Entry]
Version=1.0
Type=Application
Name=New Menu Item
Comment=A small descriptive blurb about this application.
Icon=application-default-icon
Exec=
Path=
Terminal=false
StartupNotify=false
Categories=
"""

sudo = os.getuid() == 0

class Application:
	"""Application class that uses data from .desktop files installed 
	both system-wide (/usr/share/applications) and locally 
	(/home/USERNAME/.local/share/applications)."""
    def __init__(self, filename):
        if not os.path.isfile(filename):
            self.new(filename)
        else:
            self.new_from_file(filename)

    def new(self, filename):
		"""Create a new application instance for a non-existing file."""
        self.filename = filename
        self.icon = 'application-default-icon'
        self.name = 'New Menu Item'
        self.comment = 'A small descriptive blurb about this application.'
        self.genericname = ''
        self.command = ''
        self.executable = ''
        self.path = ''
        self.terminal = False
        self.hidden = False
        self.quicklist_format = ""
        self.startupnotify = False
        self.categories = []
        self.actions = None
        self.id = None
        self.TreeViewPath = None
        self.original = default_application

    def new_from_file(self, filename):
		"""Create a new application instance for an existing filename."""
        self.filename = filename
        self.id = 0

        desktop_file = open(filename, 'r')
        settings = read_desktop_file( filename, desktop_file.read() )
        desktop_file.close()
        
        self.name = settings['name']
        self.icon = settings['icon']
        self.comment = settings['comment']
        self.genericname = settings['genericname']
        self.command = settings['command']
        self.executable = settings['executable']
        self.path = settings['path']
        self.terminal = settings['terminal']
        self.startupnotify = settings['startupnotify']
        self.hidden = settings['hidden']
        self.categories = settings['categories']
        self.quicklist_format = settings['quicklist_format']
        self.actions = settings['quicklists']
        self.original = settings['text']

    def set_filename(self, filename):
		"""Set the application filename."""
        self.filename = filename

    def get_filename(self):
		"""Return the application filename."""
        return self.filename

    def set_icon(self, name):
		"""Set the application icon name."""
        self.icon = name

    def get_icon(self):
		"""Return the application icon name."""
        return self.icon

    def set_name(self, name):
		"""Set the application proper name."""
        self.name = name

    def get_name(self):
		"""Return the application proper name."""
        return self.name

    def set_comment(self, comment):
		"""Set the application comment."""
        self.comment = comment

    def get_comment(self):
		"""Return the application comment."""
        return self.comment
        
    def set_genericname(self, name):
        self.genericname = name
        
    def get_genericname(self):
        return self.genericname

    def set_exec(self, command):
		"""Set the application command."""
        self.command = command

    def get_exec(self):
		"""Return the application command."""
        return self.command
        
    def set_executable(self, executable):
        self.executable = executable
        
    def get_executable(self):
        return self.executable

    def set_path(self, path):
		"""Set the application working directory."""
        self.path = path

    def get_path(self):
		"""Return the application working directory."""
        return self.path

    def set_terminal(self, terminal):
		"""Set whether the application runs in the terminal."""
        self.terminal = terminal

    def get_terminal(self):
		"""Return whether the application runs in the terminal."""
        return self.terminal

    def set_startupnotify(self, startupnotify):
		"""Set whether the application should notify on startup."""
        self.startupnotify = startupnotify

    def get_startupnotify(self):
		"""Return whether the application should notify on startup."""
        return self.startupnotify

    def set_categories(self, categories):
		"""Set the application categories."""
        self.categories = categories

    def get_categories(self):
		"""Return the application categories."""
        return self.categories
            
    def set_hidden(self, hidden):
		"""Set whether the application menu item is hidden."""
        self.hidden = hidden
        
    def get_hidden(self):
		"""Return whether the application menu item is hidden."""
        return self.hidden
        
    def set_quicklist_format(self, qformat):
		"""Set the application quicklist format, commonly 'Actions' or 
		'X-Ayatana-Desktop-Shortcuts'."""
        self.quicklist_format = qformat
        
    def get_quicklist_format(self):
		"""Return the application quicklist format."""
        return self.quicklist_format

    def set_actions(self, actions):
		"""Set the application quicklist items."""
        self.actions = actions

    def get_actions(self):
		"""Return the application quicklist items."""
        return self.actions

    def set_id(self, id):
		"""Set the application ID, used for identifying the launcher in 
		a selection window."""
        self.id = id

    def get_id(self):
		"""Return the application ID."""
        return self.id

    def set_original(self, original):
		"""Set the application original .desktop contents."""
        self.original = original

    def get_original(self):
		"""Return the application original .desktop contents."""
        return self.original
        
def get_applications():
	"""Return all installed applications for the current user.  If the
	program is started as root, only show system launchers."""
    applications = dict()
    app_counter = 1
    filenames = []
    if not sudo:
        for (path, dirs, files) in os.walk( os.path.join( home, '.local', 'share', 'applications' ) ):
            for filename in files:
                if os.path.splitext( filename )[1] == '.desktop':
                    filenames.append(['local', filename])
                    app = Application(os.path.join( path, filename ))
                    app.id = filename
                    app_counter += 1
                    applications[app.id] = app
    for (path, dirs, files) in os.walk( '/usr/share/applications' ):
        for filename in files:
            if os.path.splitext( filename )[1] == '.desktop':
                if ['local', filename] not in filenames:
                    filenames.append(['system', filename])
                    app = Application(os.path.join( path, filename ))
                    app.id = filename
                    app_counter += 1
                    applications[app.id] = app
    return applications
    
defaults = {'filename': '', 'icon': 'application-default-icon', 'name': '', 
            'comment': '', 'command': '', 'path': '', 'terminal': False, 
            'startupnotify': False, 'hidden': False, 'categories': [], 
            'quicklists': dict(), 'quicklist_format': 'actions', 'id': 0, 
            'text': default_application}
    
def read_desktop_file(filename, contents):
    """Return the settings pulled from the application .desktop file."""
    settings = {'filename': '', 'icon': 'application-default-icon', 'name': '', 
            'comment': '', 'genericname': '', 'command': '', 'executable': '', 
            'path': '', 'terminal': False, 'startupnotify': False, 
            'hidden': False, 'categories': [], 'quicklists': dict(), 
            'quicklist_format': 'actions', 'id': 0, 'text': default_application}
    settings['text'] = contents
    settings['filename'] = filename
    quicklist_key = None
    action_order = 0
    for line in contents.split('\n'):
        try:
            if line.lower().startswith('icon='):
                settings['icon'] = line[5:]
            elif line.lower().startswith('name='):
                if settings['name'] == '':
                    settings['name'] = line[5:]
                else:
                    settings['quicklists'][quicklist_key]['name'] = line[5:]
            elif line.lower().startswith('comment='):
                settings['comment'] = line[8:]
            elif line.lower().startswith('genericname='):
                settings['genericname'] = line[12:]
            elif line.lower().startswith('exec='):
                if settings['command'] == '':
                    settings['command'] = line[5:]
                    settings['executable'] = line[5:].split(' ')[0]
                else:
                    settings['quicklists'][quicklist_key]['command'] = line[5:]
            elif line.lower().startswith('path='):
                settings['path'] = line[5:]
            elif line.lower().startswith('terminal='):
                settings['terminal'] = 'true' in line[9:]
            elif line.lower().startswith('startupnotify='):
                settings['startupnotify'] = 'true' in line[14:]
            elif line.lower().startswith('nodisplay='):
                settings['hidden'] = 'true' in line[10:]
            elif line.lower().startswith('categories='):
                settings['categories'] = line[11:].split(';')
                try:
                    settings['categories'].remove('')
                except ValueError:
                    pass
            elif line.lower().startswith('actions=') or line.lower().startswith('x-ayatana-desktop-shortcuts'):
                settings['quicklist_format'], enabled = line.split('=')
                enabled = enabled.split(';')
            elif line.startswith('[') and not line.lower().startswith('[desktop entry]'):
                if '[desktop action ' in line.lower():
                    quicklist_key = line[16:].replace(']', '')
                elif ' shortcut group]' in line.lower():
                    quicklist_key = line[1:][:len(line)-17]
                settings['quicklists'][quicklist_key] = dict()
                settings['quicklists'][quicklist_key]['order'] = action_order
                action_order += 1
                settings['quicklists'][quicklist_key]['enabled'] = quicklist_key in enabled
        except (IndexError, UnboundLocalError):
            pass
    # Check for uncategorized WINE applications...
    if len(settings['categories']) == 0:
        if 'wine' in settings['filename'].lower():
            settings['categories'].append('Wine')
    return settings