This repository has been archived by the owner on Mar 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
purpleim-lens.py
executable file
·131 lines (100 loc) · 3.87 KB
/
purpleim-lens.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
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
#! /usr/bin/python
from singlet.lens import SingleScopeLens, IconViewCategory, ListViewCategory
from singlet.utils import run_lens
import dbus
from dbus.mainloop.glib import DBusGMainLoop
def first(iterable, default=None):
if iterable:
for item in iterable:
return item
return default
class PurpleAdapter:
IM_CONVERSATION_TYPE = 1
def __init__(self, purpleInterface):
self.purple = purpleInterface
self.__buddies = {}
self.__accounts = []
self.reload()
def openConversation(self, buddyId):
p = self.purple
acc = p.PurpleBuddyGetAccount(buddyId)
buddyName = self.__buddies[buddyId].name
c = None
for conv in p.PurpleGetIms():
convName = p.PurpleConversationGetName(conv)
if convName == buddyName:
c = conv
break
if not c:
c = p.PurpleConversationNew(self.IM_CONVERSATION_TYPE, acc, buddyName)
p.PurpleConversationPresent(c)
def reloadAccounts(self):
self.__accounts = self.purple.PurpleAccountsGetAllActive()
def buildBuddy(self, buddyId):
b = Buddy(buddyId,
self.purple.PurpleBuddyGetAlias(buddyId),
self.purple.PurpleBuddyGetName(buddyId),
self.purple.PurpleBuddyGetIcon(buddyId),
self.purple.PurpleBuddyGetAccount(buddyId))
b.iconPath = self.purple.PurpleBuddyIconGetFullPath(b.icon) if b.icon > 0 else None
return b
def reloadBuddies(self):
self.__buddies = dict([ (x, self.buildBuddy(x)) for x in self.purple.PurpleBlistGetBuddies() ])
def reload(self):
self.reloadAccounts()
self.reloadBuddies()
def searchBuddies(self, term):
self.reload()
if type(term) == unicode:
term = term.encode('raw_unicode_escape')
uTerm = term.decode('utf-8')
result = ( item for item in self.__buddies.itervalues()
if item.account in self.__accounts and
uTerm.upper() in item.aliasUpper )
return result
class Buddy:
def __init__(self, bId, alias, name, icon, account):
self.id = bId
self.name = name
self.alias = alias
self.aliasUpper = self.alias.upper()
self.icon = icon
self.account = account
self.iconPath = None
class PurpleBuddyLens(SingleScopeLens):
class Meta:
description = "Pidgin lens"
name = 'purpleim'
icon = 'purpleim-lens.svg'
cat = ListViewCategory("Pidgin Contacts", "hint")
uriPrefix = "purple-contact://local"
mimeType = 'application/x-purple-contact'
purple = None
def setupPurple(self):
try:
main_loop = DBusGMainLoop()
session_bus = dbus.SessionBus(mainloop = main_loop)
obj = session_bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
self.purple = PurpleAdapter(dbus.Interface(obj, "im.pidgin.purple.PurpleInterface"))
except Exception as x:
print x, x.message
def getBuddyResult(self, buddy):
return ('%s/%d' % (self.uriPrefix, buddy.id),
buddy.iconPath if buddy.iconPath else "stock_person",
self.cat,
self.mimeType,
buddy.alias,
buddy.name)
def search(self, phrase, results):
if not self.purple:
self.setupPurple()
if self.purple:
buddies = self.purple.searchBuddies(phrase)
for buddy in buddies:
results.append(*self.getBuddyResult(buddy))
def handle_uri(self, scope, uri):
self.purple.openConversation(int(uri.split('/')[-1]))
return self.hide_dash_response()
if __name__ == "__main__":
import sys
run_lens(PurpleBuddyLens, sys.argv)