forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pidgin.py
90 lines (70 loc) · 2.55 KB
/
pidgin.py
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
"""Open Pidgin chats.
Matching contacts will be suggested.
Synopsis: <trigger> <filter>"""
import dbus
from albertv0 import *
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "Pidgin"
__version__ = "1.0"
__author__ = "Greizgh"
__trigger__ = "pidgin "
__dependencies__ = ["dbus"]
iconPath = iconLookup("pidgin")
bus = dbus.SessionBus()
class ContactHandler:
"""Handle pidgin contact list"""
_purple = None
_contacts = []
def __init__(self):
self.refresh()
def refresh(self):
"""Refresh both dbus connection and pidgin contact list"""
try:
self._contacts = []
self._purple = bus.get_object(
"im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject"
)
accounts = self._purple.PurpleAccountsGetAllActive()
for account in accounts:
buddies = self._purple.PurpleFindBuddies(account, "")
for buddy in buddies:
# if purple.PurpleBuddyIsOnline(buddy):
name = self._purple.PurpleBuddyGetAlias(buddy)
self._contacts.append((name, account))
except dbus.DBusException:
critical("Could not connect to pidgin service")
def isReady(self):
"""Check that this handler is ready to communicate"""
return self._purple is not None
def chatWith(self, account, name):
"""Open a pidgin chat window"""
self._purple.PurpleConversationNew(1, account, name)
def getMatch(self, query):
"""Get buddies matching query"""
normalized = query.lower()
return [item for item in self._contacts if normalized in item[0].lower()]
handler = ContactHandler()
def handleQuery(query):
if not handler.isReady():
handler.refresh()
if query.isTriggered:
target = query.string.strip()
if target:
items = []
for match in handler.getMatch(target):
items.append(
Item(
id=__prettyname__,
icon=iconPath,
text="Chat with {}".format(match[0]),
subtext="Open a pidgin chat window",
completion=match[0],
actions=[
FuncAction(
"Open chat window",
lambda: handler.chatWith(match[1], match[0]),
)
],
)
)
return items