1
from .main import Application
2
from ..utils import show_desktop, get_ldtp_version
3
from time import time, sleep
9
from distutils import version
16
class IndicatorApplet(Application):
18
Indicator Applet manages the new indicator messages applet
20
IA_TOPLEVEL = "embindicator-applet"
22
Application.__init__(self, 'indicator-applet')
29
def add_server(self, desktop_file):
31
Add a new server to the indicator applet.
33
@type desktop_file: string
34
@param desktop_file: The path to the file describing the server.
39
Name=Phony Internet Messenger
40
GenericName=Internet Messenger
41
Comment=Send instant messages over phony protocols
46
Categories=Network;InstantMessaging;
52
self.server = indicate.indicate_server_ref_default()
53
except AttributeError:
55
"no libindicate Python bindings, install python-indicate."
56
self.server.set_type("message.im")
57
self.server.set_desktop_file(desktop_file)
59
while gtk.events_pending():
62
def show_indicator(self, sender):
64
It shows a basic indicator without needing to provide a desktop file
67
@param sender: The name of the indicator to be shown in the applet.
73
indicator = indicate.IndicatorMessage()
74
indicator.set_property("subtype", "im")
75
indicator.set_property("sender", sender)
76
indicator.set_property_time("time", time())
77
pixbuf = gtk.gdk.pixbuf_new_from_file(
78
"/usr/share/icons/hicolor/22x22/apps/gnome-freecell.png")
79
indicator.set_property_icon("icon", pixbuf)
81
self.indicators.append(indicator)
82
glib.timeout_add_seconds(1, _timeout_cb)
85
def capture_applet_icon(self):
87
It captures a screenshot of the indicator applet icon
89
@return: The path of the file containing the screenshot
91
x, y, w, h = ldtp.getobjectsize(self.TOP_PANEL, self.IA_TOPLEVEL)
93
ldtp_one_six = version.StrictVersion('1.6.0')
94
ldtp_current = version.StrictVersion(get_ldtp_version())
96
if ldtp_current < ldtp_one_six:
97
screeny = ldtputils.imagecapture(
98
outFile=tempfile.mktemp('.png', 'ia_'),
99
x=x, y=y, resolution1=w, resolution2=h)
101
screeny = ldtputils.imagecapture(
102
outFile=tempfile.mktemp('.png', 'ia_'),
103
x=x, y=y, width=w, height=h)
107
def is_server_shown(self, sender, already_shown=True):
109
It says if a server is being shown or not
112
@param sender: The name of the Indicator server to check
114
@type already_shown: boolean
115
@param already_shown: We need a way to distinguish between the normal menu and the indicator-applet menu
116
Workaround in the mean time:
117
Set already_shown as True, if there is already a menu with the same name
118
(in that case we will look for mnuServer1
119
or to False, if there is no already a menu with the same name
121
@return: True, if the server is being shown; False, otherwise.
124
return ldtp.objectexist(self.TOP_PANEL,
125
'mnu' + sender.replace(' ','') + '1')
127
return ldtp.objectexist(self.TOP_PANEL,
128
'mnu' + sender.replace(' ',''))
130
def is_indicator_shown(self, sender):
132
It says if an indicator is being shown or not
135
@param sender: The name of the indicator to check
137
@return: True, if the indicator is being shown; False, otherwise
139
return ldtp.objectexist(self.TOP_PANEL,
140
'mnu' + sender.replace(' ',''))
142
def select_indicator(self, sender):
144
It selects the menu item of an indicator
147
@param sender: The name of the indicator to select
149
ldtp.selectmenuitem(self.TOP_PANEL, 'mnu' + sender.replace(' ',''))
151
def select_server(self, sender, already_shown=True):
153
It selects the menu item of a server indicator
156
@param sender: The name of the Indicator server to select
158
@type already_shown: boolean
159
@param already_shown: We need a way to distinguish between the normal menu and the indicator-applet menu
160
Workaround in the mean time:
161
Set already_shown as True, if there is already a menu with the same name
162
(in that case we will look for mnuServer1
163
or to False, if there is no already a menu with the same name
167
ldtp.selectmenuitem(self.TOP_PANEL, 'mnu' + sender.replace(' ',''), + '1')
169
ldtp.selectmenuitem(self.TOP_PANEL, 'mnu' + sender.replace(' ',''))
171
def wait_for_indicator_display(self, sender, timeout=5):
175
def _display_cb(indicator):
176
indicator.hide() # This is just normal behavior, so why not?
184
for indicator in self.indicators:
185
if sender == indicator.get_property("sender"):
186
handler = indicator.connect("user-display", _display_cb)
187
handlers.append((handler, indicator))
189
glib.timeout_add_seconds(timeout, _timeout_cb)
193
for handler, indicator in handlers:
194
indicator.disconnect(handler)
198
def wait_for_server_display(self, timeout=5):
202
def _display_cb(indicator):
203
indicator.hide() # This is just normal behavior, so why not?
211
handler = self.server.connect("server-display", _display_cb)
213
glib.timeout_add_seconds(timeout, _timeout_cb)
217
self.server.disconnect(handler)
222
for indicator in self.indicators:
228
class NotifyOSD(Application):
230
NotifyOSD class manages the notifications produced by notify-osd
234
self.focus_desktop = False
235
self.screenshots = []
237
if not pynotify.init('notify-osd-test'):
238
raise ldtp.LdtpExecutionError, \
239
"Failed to initialize notification connection."
241
info = pynotify.get_server_info()
242
if info.get('name', None) != 'notify-osd':
243
raise ldtp.LdtpExecutionError, \
244
"The notify service is '%s', expected 'notify-osd'" % \
245
info.get('name', None)
247
def open(self, focus_desktop=True):
248
self.focus_desktop = focus_desktop
250
if self.focus_desktop:
254
if self.focus_desktop:
256
for screenshot in self.screenshots:
257
if os.path.exists(screenshot):
258
os.remove(screenshot)
260
def notify(self, summary, body="", icon=None):
262
Giving a summary, body and icon, it creates a notification bubble
264
@type summary: string
265
@param summary: The header of the notification
268
@param body: The text to show as body of the notification
271
@param icon: The name of the icon to show
273
n = pynotify.Notification (summary, body, icon)
276
def notify_synchronous(self, summary, body="", icon=None, value=-1):
278
Giving a summary, body, icon and value it creates a confirmation bubble
280
@type summary: string
281
@param summary: The header of the notification
284
@param body: The text to show as body of the notification
287
@param icon: The name of the icon to show
290
@param value: The value of the quantity of the confirmation bubble (i.e. volume)
292
n = pynotify.Notification (summary, body, icon)
293
n.set_hint("synchronous", "volume")
294
n.set_hint("value", value)
297
def grab_image_and_wait(self, summary, timeOut=30):
299
It waits for a notification to appear and grabs a screenshot
301
@type summary: string
302
@param summary: The summary of the notification to look for
305
@param timeOut: The number of seconds to wait for the notification to appear
307
@return: List with the time elapsed and the path to the screenshot
309
ldtp.waittillguiexist(summary, guiTimeOut=timeOut)
312
x, y, w, h = ldtp.getwindowsize(summary)
314
ldtp_one_six = version.StrictVersion('1.6.0')
315
ldtp_current = version.StrictVersion(get_ldtp_version())
317
if ldtp_current < ldtp_one_six:
319
ldtputils.imagecapture(outFile=tempfile.mktemp('.png', 'nosd_'),
325
ldtputils.imagecapture(outFile=tempfile.mktemp('.png', 'nosd_'),
330
ldtp.waittillguinotexist(summary)
331
end_time = time() - start_time
332
self.screenshots.append(screenshot)
333
return (end_time, screenshot)
335
def get_extents(self, summary, wait=False):
337
It gets the limits of the bubble
339
@type summary: string
340
@param summary: The summary of the bubble to get the size
342
@return: (x, y, width, height)
345
exists = ldtp.waittillguiexist(summary)
347
exists = ldtp.guiexist(summary)
350
return ldtp.getwindowsize(summary)
352
return -1, -1, -1, -1
354
if __name__ == "__main__":
355
from time import sleep
356
test = IndicatorApplet()
358
test.add_server('/usr/share/applications/transmission.desktop')
359
test.show_indicator('Elmer Fud')
362
#print test.wait_for_indicator_display('Elmer Fud', 20)