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
|
# -*- coding: utf-8 -*-
# Mozo Menu Editor - Simple fd.o Compliant Menu Editor
# Copyright (C) 2006 Travis Watkins
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
import matemenu
import gi
from collections import Sequence
from gi.repository import GLib, Gtk, Gdk, GdkPixbuf
DESKTOP_GROUP = GLib.KEY_FILE_DESKTOP_GROUP
KEY_FILE_FLAGS = GLib.KeyFileFlags.KEEP_COMMENTS | GLib.KeyFileFlags.KEEP_TRANSLATIONS
def fillKeyFile(keyfile, items):
for key, item in items.iteritems():
if item is None:
continue
if isinstance(item, bool):
keyfile.set_boolean(DESKTOP_GROUP, key, item)
elif isinstance(item, Sequence):
keyfile.set_string_list(DESKTOP_GROUP, key, item)
elif isinstance(item, basestring):
keyfile.set_string(DESKTOP_GROUP, key, item)
def getUniqueFileId(name, extension):
append = 0
while 1:
if append == 0:
filename = name + extension
else:
filename = name + '-' + str(append) + extension
if extension == '.desktop':
path = getUserItemPath()
if not os.path.isfile(os.path.join(path, filename)) and not getItemPath(filename):
break
elif extension == '.directory':
path = getUserDirectoryPath()
if not os.path.isfile(os.path.join(path, filename)) and not getDirectoryPath(filename):
break
append += 1
return filename
def getUniqueRedoFile(filepath):
append = 0
while 1:
new_filepath = filepath + '.redo-' + str(append)
if not os.path.isfile(new_filepath):
break
else:
append += 1
return new_filepath
def getUniqueUndoFile(filepath):
filename, extension = os.path.split(filepath)[1].rsplit('.', 1)
append = 0
while 1:
if extension == 'desktop':
path = getUserItemPath()
elif extension == 'directory':
path = getUserDirectoryPath()
elif extension == 'menu':
path = getUserMenuPath()
new_filepath = os.path.join(path, filename + '.' + extension + '.undo-' + str(append))
if not os.path.isfile(new_filepath):
break
else:
append += 1
return new_filepath
def getUserMenuPath():
menu_dir = None
if os.environ.has_key('XDG_CONFIG_HOME'):
menu_dir = os.path.join(os.environ['XDG_CONFIG_HOME'], 'menus')
else:
menu_dir = os.path.join(os.environ['HOME'], '.config', 'menus')
#move .config out of the way if it's not a dir, it shouldn't be there
if os.path.isfile(os.path.split(menu_dir)[0]):
os.rename(os.path.split(menu_dir)[0], os.path.split(menu_dir)[0] + '.old')
if not os.path.isdir(menu_dir):
os.makedirs(menu_dir)
return menu_dir
def getItemPath(file_id):
if os.environ.has_key('XDG_DATA_DIRS'):
for system_path in os.environ['XDG_DATA_DIRS'].split(':'):
file_path = os.path.join(system_path, 'applications', file_id)
if os.path.isfile(file_path):
return file_path
file_path = os.path.join('/', 'usr', 'share', 'applications', file_id)
if os.path.isfile(file_path):
return file_path
return False
def getUserItemPath():
item_dir = None
if os.environ.has_key('XDG_DATA_HOME'):
item_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'applications')
else:
item_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'applications')
if not os.path.isdir(item_dir):
os.makedirs(item_dir)
return item_dir
def getDirectoryPath(file_id):
home = getUserDirectoryPath()
file_path = os.path.join(home, file_id)
if os.path.isfile(file_path):
return file_path
if os.environ.has_key('XDG_DATA_DIRS'):
for system_path in os.environ['XDG_DATA_DIRS'].split(':'):
file_path = os.path.join(system_path, 'desktop-directories', file_id)
if os.path.isfile(file_path):
return file_path
file_path = os.path.join('/', 'usr', 'share', 'desktop-directories', file_id)
if os.path.isfile(file_path):
return file_path
return False
def getUserDirectoryPath():
menu_dir = None
if os.environ.has_key('XDG_DATA_HOME'):
menu_dir = os.path.join(os.environ['XDG_DATA_HOME'], 'desktop-directories')
else:
menu_dir = os.path.join(os.environ['HOME'], '.local', 'share', 'desktop-directories')
if not os.path.isdir(menu_dir):
os.makedirs(menu_dir)
return menu_dir
def getSystemMenuPath(file_name):
if os.environ.has_key('XDG_CONFIG_DIRS'):
for system_path in os.environ['XDG_CONFIG_DIRS'].split(':'):
file_path = os.path.join(system_path, 'menus', file_name)
if os.path.isfile(file_path):
return file_path
file_path = os.path.join('/', 'etc', 'xdg', 'menus', file_name)
if os.path.isfile(file_path):
return file_path
return False
def getUserMenuXml(tree):
system_file = getSystemMenuPath(tree.get_menu_file())
name = tree.root.get_menu_id()
menu_xml = "<!DOCTYPE Menu PUBLIC '-//freedesktop//DTD Menu 1.0//EN' 'http://standards.freedesktop.org/menu-spec/menu-1.0.dtd'>\n"
menu_xml += "<Menu>\n <Name>" + name + "</Name>\n "
menu_xml += "<MergeFile type=\"parent\">" + system_file + "</MergeFile>\n</Menu>\n"
return menu_xml
def getIcon(item, for_properties=False):
pixbuf, path = None, None
if item == None:
if for_properties:
return None, None
return None
if isinstance(item, str):
iconName = item
else:
iconName = item.get_icon()
if iconName and not '/' in iconName and iconName[-3:] in ('png', 'svg', 'xpm'):
iconName = iconName[:-4]
icon_theme = Gtk.IconTheme.get_default()
try:
pixbuf = icon_theme.load_icon(iconName, 24, 0)
path = icon_theme.lookup_icon(iconName, 24, 0).get_filename()
except:
if iconName and '/' in iconName:
try:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(iconName, 24, 24)
path = iconName
except:
pass
if pixbuf == None:
if for_properties:
return None, None
if item.get_type() == matemenu.TYPE_DIRECTORY:
iconName = 'mate-fs-directory'
elif item.get_type() == matemenu.TYPE_ENTRY:
iconName = 'application-default-icon'
try:
pixbuf = icon_theme.load_icon(iconName, 24, 0)
path = icon_theme.lookup_icon(iconName, 24, 0).get_filename()
except:
return None
if pixbuf == None:
return None
if pixbuf.get_width() != 24 or pixbuf.get_height() != 24:
pixbuf = pixbuf.scale_simple(24, 24, GdkPixbuf.InterpType.HYPER)
if for_properties:
return pixbuf, path
return pixbuf
|