~dhillon-v10/qa-regression-testing/mago-packages-checking

« back to all changes in this revision

Viewing changes to mago/application/deskex.py

  • Committer: Vikram Dhillon
  • Date: 2010-01-12 02:42:18 UTC
  • Revision ID: dhillonv10@gmail.com-20100112024218-7ntl2wrpbxqjb3kx
Initial commit: getting in the data from mago

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from .main import Application
 
2
from ..utils import show_desktop, get_ldtp_version
 
3
from time import time, sleep
 
4
import tempfile
 
5
import pynotify
 
6
import ldtp, ldtputils
 
7
import os
 
8
import gtk, glib
 
9
from distutils import version
 
10
 
 
11
try:
 
12
    import indicate
 
13
except ImportError:
 
14
    indicate = None
 
15
 
 
16
class IndicatorApplet(Application):
 
17
    """
 
18
    Indicator Applet manages the new indicator messages applet
 
19
    """
 
20
    IA_TOPLEVEL = "embindicator-applet"
 
21
    def __init__(self):
 
22
        Application.__init__(self, 'indicator-applet')
 
23
        self.indicators = []
 
24
        self.server = None
 
25
 
 
26
    def open(self):
 
27
        pass
 
28
    
 
29
    def add_server(self, desktop_file):
 
30
        """
 
31
        Add a new server to the indicator applet.
 
32
 
 
33
        @type desktop_file: string
 
34
        @param desktop_file: The path to the file describing the server.
 
35
            Example of format:
 
36
 
 
37
            [Desktop Entry]
 
38
            Encoding=UTF-8
 
39
            Name=Phony Internet Messenger
 
40
            GenericName=Internet Messenger
 
41
            Comment=Send instant messages over phony protocols
 
42
            Exec=phony
 
43
            StartupNotify=true
 
44
            Terminal=false
 
45
            Type=Application
 
46
            Categories=Network;InstantMessaging;
 
47
 
 
48
        """
 
49
 
 
50
        if not self.server:
 
51
            try:
 
52
                self.server = indicate.indicate_server_ref_default()
 
53
            except AttributeError:
 
54
                raise Exception, \
 
55
                    "no libindicate Python bindings, install python-indicate."
 
56
        self.server.set_type("message.im")
 
57
        self.server.set_desktop_file(desktop_file)
 
58
        self.server.show()
 
59
        while gtk.events_pending():
 
60
            gtk.main_iteration()
 
61
 
 
62
    def show_indicator(self, sender):
 
63
        """
 
64
        It shows a basic indicator without needing to provide a desktop file
 
65
 
 
66
        @type sender: string
 
67
        @param sender: The name of the indicator to be shown in the applet.
 
68
        """
 
69
        def _timeout_cb():
 
70
            gtk.main_quit()
 
71
            return False
 
72
 
 
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)
 
80
        indicator.show()
 
81
        self.indicators.append(indicator)
 
82
        glib.timeout_add_seconds(1, _timeout_cb)
 
83
        gtk.main()
 
84
 
 
85
    def capture_applet_icon(self):
 
86
        """
 
87
        It captures a screenshot of the indicator applet icon
 
88
 
 
89
        @return: The path of the file containing the screenshot
 
90
        """
 
91
        x, y, w, h = ldtp.getobjectsize(self.TOP_PANEL, self.IA_TOPLEVEL)
 
92
        
 
93
        ldtp_one_six = version.StrictVersion('1.6.0')
 
94
        ldtp_current = version.StrictVersion(get_ldtp_version())
 
95
 
 
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)        
 
100
        else:
 
101
            screeny = ldtputils.imagecapture(
 
102
                outFile=tempfile.mktemp('.png', 'ia_'),
 
103
                x=x, y=y, width=w, height=h)        
 
104
 
 
105
        return screeny
 
106
 
 
107
    def is_server_shown(self, sender, already_shown=True):
 
108
        """
 
109
        It says if a server is being shown or not
 
110
 
 
111
        @type sender: string
 
112
        @param sender: The name of the Indicator server to check
 
113
 
 
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
 
120
 
 
121
        @return: True, if the server is being shown; False, otherwise.
 
122
        """
 
123
        if already_shown:
 
124
            return ldtp.objectexist(self.TOP_PANEL, 
 
125
                    'mnu' + sender.replace(' ','') + '1')
 
126
        else:
 
127
            return ldtp.objectexist(self.TOP_PANEL, 
 
128
                    'mnu' + sender.replace(' ',''))
 
129
            
 
130
    def is_indicator_shown(self, sender):
 
131
        """
 
132
        It says if an indicator is being shown or not
 
133
 
 
134
        @type sender: string
 
135
        @param sender: The name of the indicator to check
 
136
        
 
137
        @return: True, if the indicator is being shown; False, otherwise
 
138
        """
 
139
        return ldtp.objectexist(self.TOP_PANEL, 
 
140
                    'mnu' + sender.replace(' ',''))
 
141
 
 
142
    def select_indicator(self, sender):
 
143
        """
 
144
        It selects the menu item of an indicator 
 
145
 
 
146
        @type sender: string
 
147
        @param sender: The name of the indicator to select 
 
148
        """
 
149
        ldtp.selectmenuitem(self.TOP_PANEL, 'mnu' + sender.replace(' ',''))
 
150
 
 
151
    def select_server(self, sender, already_shown=True):
 
152
        """
 
153
        It selects the menu item of a server indicator 
 
154
 
 
155
        @type sender: string
 
156
        @param sender: The name of the Indicator server to select 
 
157
 
 
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
 
164
        """
 
165
 
 
166
        if already_shown:
 
167
            ldtp.selectmenuitem(self.TOP_PANEL, 'mnu' + sender.replace(' ',''), + '1')
 
168
        else:
 
169
            ldtp.selectmenuitem(self.TOP_PANEL, 'mnu' + sender.replace(' ',''))
 
170
 
 
171
    def wait_for_indicator_display(self, sender, timeout=5):
 
172
        handlers = []
 
173
        displayed = [False]
 
174
 
 
175
        def _display_cb(indicator):
 
176
            indicator.hide() # This is just normal behavior, so why not?
 
177
            displayed[0] = True
 
178
            gtk.main_quit()
 
179
            
 
180
        def _timeout_cb():
 
181
            gtk.main_quit()
 
182
            return False
 
183
 
 
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))
 
188
 
 
189
        glib.timeout_add_seconds(timeout, _timeout_cb)
 
190
 
 
191
        gtk.main()
 
192
 
 
193
        for handler, indicator in handlers:
 
194
            indicator.disconnect(handler)
 
195
 
 
196
        return displayed[0]
 
197
 
 
198
    def wait_for_server_display(self, timeout=5):
 
199
        displayed = [False]
 
200
        handler = 0
 
201
 
 
202
        def _display_cb(indicator):
 
203
            indicator.hide() # This is just normal behavior, so why not?
 
204
            displayed[0] = True
 
205
            gtk.main_quit()
 
206
            
 
207
        def _timeout_cb():
 
208
            gtk.main_quit()
 
209
            return False
 
210
 
 
211
        handler = self.server.connect("server-display", _display_cb)
 
212
 
 
213
        glib.timeout_add_seconds(timeout, _timeout_cb)
 
214
 
 
215
        gtk.main()
 
216
 
 
217
        self.server.disconnect(handler)
 
218
 
 
219
        return displayed[0]
 
220
 
 
221
    def close(self):
 
222
        for indicator in self.indicators:
 
223
            indicator.hide()
 
224
        # BUG: 351537
 
225
        # self.server.hide()
 
226
        sleep(1)
 
227
 
 
228
class NotifyOSD(Application):
 
229
    """
 
230
    NotifyOSD class manages the notifications produced by notify-osd
 
231
    """
 
232
 
 
233
    def __init__(self):
 
234
        self.focus_desktop = False
 
235
        self.screenshots = []
 
236
 
 
237
        if not pynotify.init('notify-osd-test'):
 
238
            raise ldtp.LdtpExecutionError, \
 
239
                "Failed to initialize notification connection."
 
240
 
 
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)
 
246
 
 
247
    def open(self, focus_desktop=True):
 
248
        self.focus_desktop = focus_desktop
 
249
 
 
250
        if self.focus_desktop:
 
251
            show_desktop(True)
 
252
 
 
253
    def close(self):
 
254
        if self.focus_desktop:
 
255
            show_desktop(False)
 
256
        for screenshot in self.screenshots:
 
257
            if os.path.exists(screenshot):
 
258
                os.remove(screenshot)
 
259
 
 
260
    def notify(self, summary, body="", icon=None):
 
261
        """
 
262
        Giving a summary, body and icon, it creates a notification bubble
 
263
 
 
264
        @type summary: string
 
265
        @param summary: The header of the notification
 
266
 
 
267
        @type body: string
 
268
        @param body: The text to show as body of the notification
 
269
 
 
270
        @type icon: string
 
271
        @param icon: The name of the icon to show
 
272
        """
 
273
        n = pynotify.Notification (summary, body, icon)
 
274
        n.show ()
 
275
 
 
276
    def notify_synchronous(self, summary, body="", icon=None, value=-1):
 
277
        """
 
278
        Giving a summary, body, icon and value it creates a confirmation bubble
 
279
 
 
280
        @type summary: string
 
281
        @param summary: The header of the notification
 
282
 
 
283
        @type body: string
 
284
        @param body: The text to show as body of the notification
 
285
 
 
286
        @type icon: string
 
287
        @param icon: The name of the icon to show
 
288
 
 
289
        @type value: int
 
290
        @param value: The value of the quantity of the confirmation bubble (i.e. volume)
 
291
        """
 
292
        n = pynotify.Notification (summary, body, icon)
 
293
        n.set_hint("synchronous", "volume")
 
294
        n.set_hint("value", value)
 
295
        n.show ()
 
296
 
 
297
    def grab_image_and_wait(self, summary, timeOut=30):
 
298
        """
 
299
        It waits for a notification to appear and grabs a screenshot
 
300
 
 
301
        @type summary: string
 
302
        @param summary: The summary of the notification to look for
 
303
 
 
304
        @type timeOut: int
 
305
        @param timeOut: The number of seconds to wait for the notification to appear
 
306
 
 
307
        @return: List with the time elapsed and the path to the screenshot
 
308
        """
 
309
        ldtp.waittillguiexist(summary, guiTimeOut=timeOut)
 
310
        start_time = time()
 
311
        sleep(1)
 
312
        x, y, w, h = ldtp.getwindowsize(summary)
 
313
 
 
314
        ldtp_one_six = version.StrictVersion('1.6.0')
 
315
        ldtp_current = version.StrictVersion(get_ldtp_version())
 
316
 
 
317
        if ldtp_current < ldtp_one_six:
 
318
            screenshot = \
 
319
                ldtputils.imagecapture(outFile=tempfile.mktemp('.png', 'nosd_'),
 
320
                                   x=x+3, y=y+3, 
 
321
                                   resolution1=w-6, 
 
322
                                   resolution2=h-6)
 
323
        else:
 
324
            screenshot = \
 
325
                ldtputils.imagecapture(outFile=tempfile.mktemp('.png', 'nosd_'),
 
326
                                   x=x+3, y=y+3, 
 
327
                                   width=w-6, 
 
328
                                   height=h-6)
 
329
        
 
330
        ldtp.waittillguinotexist(summary)
 
331
        end_time = time() - start_time
 
332
        self.screenshots.append(screenshot)
 
333
        return (end_time, screenshot)
 
334
 
 
335
    def get_extents(self, summary, wait=False):
 
336
        """
 
337
        It gets the limits of the bubble
 
338
 
 
339
        @type summary: string
 
340
        @param summary: The summary of the bubble to get the size
 
341
 
 
342
        @return: (x, y, width, height)
 
343
        """
 
344
        if wait:
 
345
            exists = ldtp.waittillguiexist(summary)            
 
346
        else:
 
347
            exists = ldtp.guiexist(summary)            
 
348
            
 
349
        if exists:
 
350
            return ldtp.getwindowsize(summary)
 
351
        else:
 
352
            return -1, -1, -1, -1
 
353
            
 
354
if __name__ == "__main__":
 
355
    from time import sleep
 
356
    test = IndicatorApplet()
 
357
    test.open()
 
358
    test.add_server('/usr/share/applications/transmission.desktop')
 
359
    test.show_indicator('Elmer Fud')
 
360
    print 'sleeping'
 
361
    sleep(20)
 
362
    #print test.wait_for_indicator_display('Elmer Fud', 20)