1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 __maintainer__ = 'Benjamin Kampmann <benjamin@fluendo.com>'
19
20 from menu_entry_builder import MenuEntryBuilder
21
22 from elisa.core import common
23 from elisa.core.media_uri import MediaUri
24
25 from xdg import Menu, DesktopEntry
26 from xdg.IconTheme import getIconPath
27
28 from twisted.internet import defer, threads
29
30 import os
31
32 -class XdgEntryBuilder(MenuEntryBuilder):
33 """
34 Build MenuEntries for the type 'xdg_menu', 'xdg_desktop_node',
35 'xdg_desktop_path'
36 """
37
39
40 return ['xdg_desktop_path']
41
43 xdg_type = node_xml.get('type')
44
45 if xdg_type == 'xdg_menu':
46 pass
47
48 elif xdg_type == 'xdg_desktop_node':
49 pass
50
51 elif xdg_type == 'xdg_desktop_path':
52 path = node_xml.find('Path')
53 if path == None:
54 self.warning("%s: is missing the Path-Tag. Skipped" %
55 node_xml)
56 return defer.succeed([])
57
58 path = path.text
59 if not os.path.isdir(path):
60 self.warning("'%s' is not a directory!" % path)
61 return defer.succeed([])
62
63 dfr = threads.deferToThread(os.listdir, path)
64 dfr.addCallback(self._got_directory_list, parent, path)
65 return dfr
66
67 return defer.succeed([])
68
70 """
71 returns a Deferred with the model (contaning already an action) as
72 callback result. If the model could not been created the callback
73 result is a None
74 """
75 plugin_registry = common.application.plugin_registry
76
77 def got_action(action, model, d):
78 model.activate_action = action
79 action.executable = d.getExec().split(' ')[0]
80 action.path = d.getPath()
81 return model
82
83 def got_model(model, d):
84 model.text = d.getName()
85 icon = getIconPath(d.getIcon())
86 uri = MediaUri('file://%s' % icon)
87 model.thumbnail_source = uri
88 model.has_children = False
89 action_dfr = defer.maybeDeferred(plugin_registry.create_component,
90 'xmlmenu:spawn_process_action')
91 action_dfr.addCallback(got_action, model, d)
92 return action_dfr
93
94 def got_desktop(d):
95 if d.getNoDisplay() or d.getHidden() or len(d.getExec()) == 0:
96 return None
97
98 model_dfr = defer.maybeDeferred(plugin_registry.create_component,
99 'base:menu_node_model')
100 model_dfr.addCallback(got_model, d)
101 return model_dfr
102
103 dfr = threads.deferToThread(DesktopEntry.DesktopEntry, path)
104 dfr.addCallback(got_desktop)
105
106 return dfr
107
108 - def _got_directory_list(self, list, parent, path):
109 idx = 0
110
111 dfr = defer.Deferred()
112 models = []
113
114 def got_model(model, list):
115 if model != None:
116 parent.children.append(model)
117 models.append(model)
118 next_node(list)
119
120 def next_node(list):
121 if len(list) == 0:
122 dfr.callback(models)
123 return
124
125 next = list.pop(0)
126
127 if not next[-8:] == '.desktop':
128 next_node(list)
129 return
130
131 real_path = "%s/%s" % (path, next)
132 next_dfr = self._node_from_desktop_file(real_path)
133 next_dfr.addCallback(got_model, list)
134
135 if len(list) == 0:
136 return defer.succeed([])
137
138 next_node(list)
139
140 return dfr
141