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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
|
"""
A python version of the main functions to use Snarl
(http://www.fullphat.net/snarl)
Version 1.0
This module can be used in two ways. One is the normal way
the other snarl interfaces work. This means you can call snShowMessage
and get an ID back for manipulations.
The other way is there is a class this module exposes called SnarlMessage.
This allows you to keep track of the message as a python object. If you
use the send without specifying False as the argument it will set the ID
to what the return of the last SendMessage was. This is of course only
useful for the SHOW message.
Requires one of:
pywin32 extensions from http://pywin32.sourceforge.net
ctypes (included in Python 2.5, downloadable for earlier versions)
Creator: Sam Listopad II (samlii@users.sourceforge.net)
Copyright 2006-2008 Samuel Listopad II
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
"""
import array, struct
def LOWORD(dword):
"""Return the low WORD of the passed in integer"""
return dword & 0x0000ffff
#get the hi word
def HIWORD(dword):
"""Return the high WORD of the passed in integer"""
return dword >> 16
class Win32FuncException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class Win32Funcs:
"""Just a little class to hide the details of finding and using the
correct win32 functions. The functions may throw a UnicodeEncodeError if
there is not a unicode version and it is sent a unicode string that cannot
be converted to ASCII."""
WM_USER = 0x400
WM_COPYDATA = 0x4a
#Type of String the functions are expecting.
#Used like function(myWin32Funcs.strType(param)).
__strType = str
#FindWindow function to use
__FindWindow = None
#FindWindow function to use
__FindWindowEx = None
#SendMessage function to use
__SendMessage = None
#SendMessageTimeout function to use
__SendMessageTimeout = None
#IsWindow function to use
__IsWindow = None
#RegisterWindowMessage to use
__RegisterWindowMessage = None
#GetWindowText to use
__GetWindowText = None
def FindWindow(self, lpClassName, lpWindowName):
"""Wraps the windows API call of FindWindow"""
if lpClassName is not None:
lpClassName = self.__strType(lpClassName)
if lpWindowName is not None:
lpWindowName = self.__strType(lpWindowName)
return self.__FindWindow(lpClassName, lpWindowName)
def FindWindowEx(self, hwndParent, hwndChildAfter, lpClassName, lpWindowName):
"""Wraps the windows API call of FindWindow"""
if lpClassName is not None:
lpClassName = self.__strType(lpClassName)
if lpWindowName is not None:
lpWindowName = self.__strType(lpWindowName)
return self.__FindWindowEx(hwndParent, hwndChildAfter, lpClassName, lpWindowName)
def SendMessage(self, hWnd, Msg, wParam, lParam):
"""Wraps the windows API call of SendMessage"""
return self.__SendMessage(hWnd, Msg, wParam, lParam)
def SendMessageTimeout(self, hWnd, Msg,
wParam, lParam, fuFlags,
uTimeout, lpdwResult = None):
"""Wraps the windows API call of SendMessageTimeout"""
idToRet = None
try:
idFromMsg = array.array('I', [0])
result = idFromMsg.buffer_info()[0]
response = self.__SendMessageTimeout(hWnd, Msg, wParam,
lParam, fuFlags,
uTimeout, result)
if response == 0:
raise Win32FuncException, "SendMessageTimeout TimedOut"
idToRet = idFromMsg[0]
except TypeError:
idToRet = self.__SendMessageTimeout(hWnd, Msg, wParam,
lParam, fuFlags,
uTimeout)
if lpdwResult is not None and lpdwResult.typecode == 'I':
lpdwResult[0] = idToRet
return idToRet
def IsWindow(self, hWnd):
"""Wraps the windows API call of IsWindow"""
return self.__IsWindow(hWnd)
def RegisterWindowMessage(self, lpString):
"""Wraps the windows API call of RegisterWindowMessage"""
return self.__RegisterWindowMessage(self.__strType(lpString))
def GetWindowText(self, hWnd, lpString = None, nMaxCount = None):
"""Wraps the windows API call of SendMessageTimeout"""
text = ''
if hWnd == 0:
return text
if nMaxCount is None:
nMaxCount = 1025
try:
arrayType = 'c'
if self.__strType == unicode:
arrayType = 'u'
path_string = array.array(arrayType, self.__strType('\x00') * nMaxCount)
path_buffer = path_string.buffer_info()[0]
result = self.__GetWindowText(hWnd,
path_buffer,
nMaxCount)
if result > 0:
if self.__strType == unicode:
text = path_string[0:result].tounicode()
else:
text = path_string[0:result].tostring()
except TypeError:
text = self.__GetWindowText(hWnd)
if lpString is not None and lpString.typecode == 'c':
lpdwResult[0:len(text)] = array.array('c', str(text));
if lpString is not None and lpString.typecode == 'u':
lpdwResult[0:len(text)] = array.array('u', unicode(text));
return text
def __init__(self):
"""Load up my needed functions"""
# First see if they already have win32gui imported. If so use it.
# This has to be checked first since the auto check looks for ctypes
# first.
try:
self.__FindWindow = win32gui.FindWindow
self.__FindWindowEx = win32gui.FindWindowEx
self.__GetWindowText = win32gui.GetWindowText
self.__IsWindow = win32gui.IsWindow
self.__SendMessage = win32gui.SendMessage
self.__SendMessageTimeout = win32gui.SendMessageTimeout
self.__RegisterWindowMessage = win32gui.RegisterWindowMessage
self.__strType = unicode
#Something threw a NameError, most likely the win32gui lines
#so do auto check
except NameError:
try:
from ctypes import windll
self.__FindWindow = windll.user32.FindWindowW
self.__FindWindowEx = windll.user32.FindWindowExW
self.__GetWindowText = windll.user32.GetWindowTextW
self.__IsWindow = windll.user32.IsWindow
self.__SendMessage = windll.user32.SendMessageW
self.__SendMessageTimeout = windll.user32.SendMessageTimeoutW
self.__RegisterWindowMessage = windll.user32.RegisterWindowMessageW
self.__strType = unicode
#FindWindowW wasn't found, look for FindWindowA
except AttributeError:
try:
self.__FindWindow = windll.user32.FindWindowA
self.__FindWindowEx = windll.user32.FindWindowExA
self.__GetWindowText = windll.user32.GetWindowTextA
self.__IsWindow = windll.user32.IsWindow
self.__SendMessage = windll.user32.SendMessageA
self.__SendMessageTimeout = windll.user32.SendMessageTimeoutA
self.__RegisterWindowMessage = windll.user32.RegisterWindowMessageA
# Couldn't find either so Die and tell user why.
except AttributeError:
import sys
sys.stderr.write("Your Windows TM setup seems to be corrupt."+
" No FindWindow found in user32.\n")
sys.stderr.flush()
sys.exit(3)
except ImportError:
try:
import win32gui
self.__FindWindow = win32gui.FindWindow
self.__FindWindowEx = win32gui.FindWindowEx
self.__GetWindowText = win32gui.GetWindowText
self.__IsWindow = win32gui.IsWindow
self.__SendMessage = win32gui.SendMessage
self.__SendMessageTimeout = win32gui.SendMessageTimeout
self.__RegisterWindowMessage = win32gui.RegisterWindowMessage
self.__strType = unicode
except ImportError:
import sys
sys.stderr.write("You need to have either"+
" ctypes or pywin32 installed.\n")
sys.stderr.flush()
#sys.exit(2)
myWin32Funcs = Win32Funcs()
SHOW = 1
HIDE = 2
UPDATE = 3
IS_VISIBLE = 4
GET_VERSION = 5
REGISTER_CONFIG_WINDOW = 6
REVOKE_CONFIG_WINDOW = 7
REGISTER_ALERT = 8
REVOKE_ALERT = 9
REGISTER_CONFIG_WINDOW_2 = 10
GET_VERSION_EX = 11
SET_TIMEOUT = 12
EX_SHOW = 32
GLOBAL_MESSAGE = "SnarlGlobalMessage"
GLOBAL_MSG = "SnarlGlobalEvent"
#Messages That may be received from Snarl
SNARL_LAUNCHED = 1
SNARL_QUIT = 2
SNARL_ASK_APPLET_VER = 3
SNARL_SHOW_APP_UI = 4
SNARL_NOTIFICATION_CLICKED = 32 #notification was right-clicked by user
SNARL_NOTIFICATION_CANCELLED = SNARL_NOTIFICATION_CLICKED #Name clarified
SNARL_NOTIFICATION_TIMED_OUT = 33
SNARL_NOTIFICATION_ACK = 34 #notification was left-clicked by user
#Snarl Test Message
WM_SNARLTEST = myWin32Funcs.WM_USER + 237
M_ABORTED = 0x80000007L
M_ACCESS_DENIED = 0x80000009L
M_ALREADY_EXISTS = 0x8000000CL
M_BAD_HANDLE = 0x80000006L
M_BAD_POINTER = 0x80000005L
M_FAILED = 0x80000008L
M_INVALID_ARGS = 0x80000003L
M_NO_INTERFACE = 0x80000004L
M_NOT_FOUND = 0x8000000BL
M_NOT_IMPLEMENTED = 0x80000001L
M_OK = 0x00000000L
M_OUT_OF_MEMORY = 0x80000002L
M_TIMED_OUT = 0x8000000AL
ErrorCodeRev = {
0x80000007L : "M_ABORTED",
0x80000009L : "M_ACCESS_DENIED",
0x8000000CL : "M_ALREADY_EXISTS",
0x80000006L : "M_BAD_HANDLE",
0x80000005L : "M_BAD_POINTER",
0x80000008L : "M_FAILED",
0x80000003L : "M_INVALID_ARGS",
0x80000004L : "M_NO_INTERFACE",
0x8000000BL : "M_NOT_FOUND",
0x80000001L : "M_NOT_IMPLEMENTED",
0x00000000L : "M_OK",
0x80000002L : "M_OUT_OF_MEMORY",
0x8000000AL : "M_TIMED_OUT"
}
class SnarlMessage(object):
"""The main Snarl interface object.
ID = Snarl Message ID for most operations. See SDK for more info
as to other values to put here.
type = Snarl Message Type. Valid values are : SHOW, HIDE, UPDATE,
IS_VISIBLE, GET_VERSION, REGISTER_CONFIG_WINDOW, REVOKE_CONFIG_WINDOW
all which are constants in the PySnarl module.
timeout = Timeout in seconds for the Snarl Message
data = Snarl Message data. This is dependant upon message type. See SDK
title = Snarl Message title.
text = Snarl Message text.
icon = Path to the icon to display in the Snarl Message.
"""
__msgType = 0
__msgID = 0
__msgTimeout = 0
__msgData = 0
__msgTitle = ""
__msgText = ""
__msgIcon = ""
__msgClass = ""
__msgExtra = ""
__msgExtra2 = ""
__msgRsvd1 = 0
__msgRsvd2 = 0
__msgHWnd = 0
lastKnownHWnd = 0
def getType(self):
"""Type Attribute getter."""
return self.__msgType
def setType(self, value):
"""Type Attribute setter."""
if( isinstance(value, (int, long)) ):
self.__msgType = value
type = property(getType, setType, doc="The Snarl Message Type")
def getID(self):
"""ID Attribute getter."""
return self.__msgID
def setID(self, value):
"""ID Attribute setter."""
if( isinstance(value, (int, long)) ):
self.__msgID = value
ID = property(getID, setID, doc="The Snarl Message ID")
def getTimeout(self):
"""Timeout Attribute getter."""
return self.__msgTimeout
def updateTimeout(self, value):
"""Timeout Attribute setter."""
if( isinstance(value, (int, long)) ):
self.__msgTimeout = value
timeout = property(getTimeout, updateTimeout,
doc="The Snarl Message Timeout")
def getData(self):
"""Data Attribute getter."""
return self.__msgData
def setData(self, value):
"""Data Attribute setter."""
if( isinstance(value, (int, long)) ):
self.__msgData = value
data = property(getData, setData, doc="The Snarl Message Data")
def getTitle(self):
"""Title Attribute getter."""
return self.__msgTitle
def setTitle(self, value):
"""Title Attribute setter."""
if( isinstance(value, basestring) ):
self.__msgTitle = value
title = property(getTitle, setTitle, doc="The Snarl Message Title")
def getText(self):
"""Text Attribute getter."""
return self.__msgText
def setText(self, value):
"""Text Attribute setter."""
if( isinstance(value, basestring) ):
self.__msgText = value
text = property(getText, setText, doc="The Snarl Message Text")
def getIcon(self):
"""Icon Attribute getter."""
return self.__msgIcon
def setIcon(self, value):
"""Icon Attribute setter."""
if( isinstance(value, basestring) ):
self.__msgIcon = value
icon = property(getIcon, setIcon, doc="The Snarl Message Icon")
def getClass(self):
"""Class Attribute getter."""
return self.__msgClass
def setClass(self, value):
"""Class Attribute setter."""
if( isinstance(value, basestring) ):
self.__msgClass = value
msgclass = property(getClass, setClass, doc="The Snarl Message Class")
def getExtra(self):
"""Extra Attribute getter."""
return self.__msgExtra
def setExtra(self, value):
"""Extra Attribute setter."""
if( isinstance(value, basestring) ):
self.__msgExtra = value
extra = property(getExtra, setExtra, doc="Extra Info for the Snarl Message")
def getExtra2(self):
"""Extra2 Attribute getter."""
return self.__msgExtra2
def setExtra2(self, value):
"""Extra2 Attribute setter."""
if( isinstance(value, basestring) ):
self.__msgExtra2 = value
extra2 = property(getExtra2, setExtra2,
doc="More Extra Info for the Snarl Message")
def getRsvd1(self):
"""Rsvd1 Attribute getter."""
return self.__msgRsvd1
def setRsvd1(self, value):
"""Rsvd1 Attribute setter."""
if( isinstance(value, (int, long)) ):
self.__msgRsvd1 = value
rsvd1 = property(getRsvd1, setRsvd1, doc="The Snarl Message Field Rsvd1")
def getRsvd2(self):
"""Rsvd2 Attribute getter."""
return self.__msgRsvd2
def setRsvd2(self, value):
"""Rsvd2 Attribute setter."""
if( isinstance(value, (int, long)) ):
self.__msgRsvd2 = value
rsvd2 = property(getRsvd2, setRsvd2, doc="The Snarl Message Field Rsvd2")
def getHwnd(self):
"""hWnd Attribute getter."""
return self.__msgHWnd
def setHwnd(self, value):
"""hWnd Attribute setter."""
if( isinstance(value, (int, long)) ):
self.__msgHWnd = value
hWnd = property(getHwnd, setHwnd, doc="The hWnd of the window this message is being sent from")
def __init__(self, title="", text="", icon="", msg_type=1, msg_id=0):
self.__msgTimeout = 0
self.__msgData = 0
self.__msgClass = ""
self.__msgExtra = ""
self.__msgExtra2 = ""
self.__msgRsvd1 = 0
self.__msgRsvd2 = 0
self.__msgType = msg_type
self.__msgText = text
self.__msgTitle = title
self.__msgIcon = icon
self.__msgID = msg_id
def createCopyStruct(self):
"""Creates the struct to send as the copyData in the message."""
return struct.pack("ILLL1024s1024s1024s1024s1024s1024sLL",
self.__msgType,
self.__msgID,
self.__msgTimeout,
self.__msgData,
self.__msgTitle.encode('utf-8'),
self.__msgText.encode('utf-8'),
self.__msgIcon.encode('utf-8'),
self.__msgClass.encode('utf-8'),
self.__msgExtra.encode('utf-8'),
self.__msgExtra2.encode('utf-8'),
self.__msgRsvd1,
self.__msgRsvd2
)
__lpData = None
__cds = None
def packData(self, dwData):
"""This packs the data in the necessary format for a
WM_COPYDATA message."""
self.__lpData = None
self.__cds = None
item = self.createCopyStruct()
self.__lpData = array.array('c', item)
lpData_ad = self.__lpData.buffer_info()[0]
cbData = self.__lpData.buffer_info()[1]
self.__cds = array.array('c',
struct.pack("IIP",
dwData,
cbData,
lpData_ad)
)
cds_ad = self.__cds.buffer_info()[0]
return cds_ad
def reset(self):
"""Reset this SnarlMessage to the default state."""
self.__msgType = 0
self.__msgID = 0
self.__msgTimeout = 0
self.__msgData = 0
self.__msgTitle = ""
self.__msgText = ""
self.__msgIcon = ""
self.__msgClass = ""
self.__msgExtra = ""
self.__msgExtra2 = ""
self.__msgRsvd1 = 0
self.__msgRsvd2 = 0
def send(self, setid=True):
"""Send this SnarlMessage to the Snarl window.
Args:
setid - Boolean defining whether or not to set the ID
of this SnarlMessage to the return value of
the SendMessage call. Default is True to
make simple case of SHOW easy.
"""
hwnd = myWin32Funcs.FindWindow(None, "Snarl")
if myWin32Funcs.IsWindow(hwnd):
if self.type == REGISTER_CONFIG_WINDOW or self.type == REGISTER_CONFIG_WINDOW_2:
self.hWnd = self.data
try:
response = myWin32Funcs.SendMessageTimeout(hwnd,
myWin32Funcs.WM_COPYDATA,
self.hWnd, self.packData(2),
2, 500)
except Win32FuncException:
return False
idFromMsg = response
if setid:
self.ID = idFromMsg
return True
else:
return idFromMsg
print "No snarl window found"
return False
def hide(self):
"""Hide this message. Type will revert to type before calling hide
to allow for better reuse of object."""
oldType = self.__msgType
self.__msgType = HIDE
retVal = bool(self.send(False))
self.__msgType = oldType
return retVal
def isVisible(self):
"""Is this message visible. Type will revert to type before calling
hide to allow for better reuse of object."""
oldType = self.__msgType
self.__msgType = IS_VISIBLE
retVal = bool(self.send(False))
self.__msgType = oldType
return retVal
def update(self, title=None, text=None, icon=None):
"""Update this message with given title and text. Type will revert
to type before calling hide to allow for better reuse of object."""
oldType = self.__msgType
self.__msgType = UPDATE
if text:
self.__msgText = text
if title:
self.__msgTitle = title
if icon:
self.__msgIcon = icon
retVal = self.send(False)
self.__msgType = oldType
return retVal
def setTimeout(self, timeout):
"""Set the timeout in seconds of the message"""
oldType = self.__msgType
oldData = self.__msgData
self.__msgType = SET_TIMEOUT
#self.timeout = timeout
#self.__msgData = self.__msgTimeout
self.__msgData = timeout
retVal = self.send(False)
self.__msgType = oldType
self.__msgData = oldData
return retVal
def show(self, timeout=None, title=None,
text=None, icon=None,
replyWindow=None, replyMsg=None, msgclass=None, soundPath=None):
"""Show a message"""
oldType = self.__msgType
oldTimeout = self.__msgTimeout
self.__msgType = SHOW
if text:
self.__msgText = text
if title:
self.__msgTitle = title
if timeout:
self.__msgTimeout = timeout
if icon:
self.__msgIcon = icon
if replyWindow:
self.__msgID = replyMsg
if replyMsg:
self.__msgData = replyWindow
if soundPath:
self.__msgExtra = soundPath
if msgclass:
self.__msgClass = msgclass
if ((self.__msgClass and self.__msgClass != "") or
(self.__msgExtra and self.__msgExtra != "")):
self.__msgType = EX_SHOW
retVal = bool(self.send())
self.__msgType = oldType
self.__msgTimeout = oldTimeout
return retVal
def snGetVersion():
""" Get the version of Snarl that is running as a tuple. (Major, Minor)
If Snarl is not running or there was an error it will
return False."""
msg = SnarlMessage(msg_type=GET_VERSION)
version = msg.send(False)
if not version:
return False
return (HIWORD(version), LOWORD(version))
def snGetVersionEx():
""" Get the internal version of Snarl that is running.
If Snarl is not running or there was an error it will
return False."""
sm = SnarlMessage(msg_type=GET_VERSION_EX)
verNum = sm.send(False)
if not verNum:
return False
return verNum
def snGetGlobalMessage():
"""Get the Snarl global message id from windows."""
return myWin32Funcs.RegisterWindowMessage(GLOBAL_MSG)
def snShowMessage(title, text, timeout=0, iconPath="",
replyWindow=0, replyMsg=0):
"""Show a message using Snarl and return its ID. See SDK for arguments."""
sm = SnarlMessage( title, text, iconPath, msg_id=replyMsg)
sm.data = replyWindow
if sm.show(timeout):
return sm.ID
else:
return False
def snShowMessageEx(msgClass, title, text, timeout=0, iconPath="",
replyWindow=0, replyMsg=0, soundFile=None, hWndFrom=None):
"""Show a message using Snarl and return its ID. See SDK for arguments.
One added argument is hWndFrom that allows one to make the messages appear
to come from a specific window. This window should be the one you registered
earlier with RegisterConfig"""
sm = SnarlMessage( title, text, iconPath, msg_id=replyMsg)
sm.data = replyWindow
if hWndFrom is not None:
sm.hWnd = hWndFrom
else:
sm.hWnd = SnarlMessage.lastKnownHWnd
if sm.show(timeout, msgclass=msgClass, soundPath=soundFile):
return sm.ID
else:
return False
def snUpdateMessage(msgId, msgTitle, msgText, icon=None):
"""Update a message"""
sm = SnarlMessage(msg_id=msgId)
if icon:
sm.icon = icon
return sm.update(msgTitle, msgText)
def snHideMessage(msgId):
"""Hide a message"""
return SnarlMessage(msg_id=msgId).hide()
def snSetTimeout(msgId, timeout):
"""Update the timeout of a message already shown."""
sm = SnarlMessage(msg_id=msgId)
return sm.setTimeout(timeout)
def snIsMessageVisible(msgId):
"""Returns True if the message is visible False otherwise."""
return SnarlMessage(msg_id=msgId).isVisible()
def snRegisterConfig(replyWnd, appName, replyMsg):
"""Register a config window. See SDK for more info."""
global lastRegisteredSnarlMsg
sm = SnarlMessage(msg_type=REGISTER_CONFIG_WINDOW,
title=appName,
msg_id=replyMsg)
sm.data = replyWnd
SnarlMessage.lastKnownHWnd = replyWnd
return sm.send(False)
def snRegisterConfig2(replyWnd, appName, replyMsg, icon):
"""Register a config window. See SDK for more info."""
global lastRegisteredSnarlMsg
sm = SnarlMessage(msg_type=REGISTER_CONFIG_WINDOW_2,
title=appName,
msg_id=replyMsg,
icon=icon)
sm.data = replyWnd
SnarlMessage.lastKnownHWnd = replyWnd
return sm.send(False)
def snRegisterAlert(appName, classStr) :
"""Register an alert for an already registered config. See SDK for more info."""
sm = SnarlMessage(msg_type=REGISTER_ALERT,
title=appName,
text=classStr)
return sm.send(False)
def snRevokeConfig(replyWnd):
"""Revoke a config window"""
sm = SnarlMessage(msg_type=REVOKE_CONFIG_WINDOW)
sm.data = replyWnd
if replyWnd == SnarlMessage.lastKnownHWnd:
SnarlMessage.lastKnownHWnd = 0
return sm.send(False)
def snGetSnarlWindow():
"""Returns the hWnd of the snarl window"""
return myWin32Funcs.FindWindow(None, "Snarl")
def snGetAppPath():
"""Returns the application path of the currently running snarl window"""
app_path = None
snarl_handle = snGetSnarlWindow()
if snarl_handle != 0:
pathwin_handle = myWin32Funcs.FindWindowEx(snarl_handle,
0,
"static",
None)
if pathwin_handle != 0:
try:
result = myWin32Funcs.GetWindowText(pathwin_handle)
app_path = result
except Win32FuncException:
pass
return app_path
def snGetIconsPath():
"""Returns the path to the icons of the program"""
s = snGetAppPath()
if s is None:
return ""
else:
return s + "etc\\icons\\"
def snSendTestMessage(data=None):
"""Sends a test message to Snarl. Used to make sure the
api is connecting"""
param = 0
command = 0
if data:
param = struct.pack("I", data)
command = 1
myWin32Funcs.SendMessage(snGetSnarlWindow(), WM_SNARLTEST, command, param)
|