~erigami/onehundredscopes/unity-lens-pidgin

« back to all changes in this revision

Viewing changes to src/unity-lens-pidgin

  • Committer: Sean Dague
  • Date: 2012-05-10 00:22:19 UTC
  • Revision ID: sean@dague.net-20120510002219-73zwd2b56i1ol06f
updated svg to be the 16/stock_person from Humanity theme

massive refactor on unity-lens-pidgin

Show diffs side-by-side

added added

removed removed

Lines of Context:
92
92
        buddies = self._pidgin.search(search)
93
93
 
94
94
        for buddy in buddies:
95
 
            model.append("pidgin://%s/%s" % (buddy["accountid"], buddy["name"]),
96
 
                         "file://%s" % buddy["icon"],
 
95
            icon = "stock_person"
 
96
            if buddy.icon:
 
97
                icon = "file://%s" % buddy.icon
 
98
 
 
99
            model.append("pidgin://%s/%s" % (buddy.account, buddy.name),
 
100
                         icon,
97
101
                         0,
98
102
                         "text/html",
99
 
                         buddy["alias"],
100
 
                         "Buddy Name",
101
 
                         "file://%s" % buddy["icon"],
 
103
                         buddy.alias,
 
104
                         buddy.name,
 
105
                         icon
102
106
                         )
103
107
 
104
108
 
 
109
class Buddy:
 
110
    icon = None
 
111
 
 
112
    def __init__(self, bid, account, name, alias):
 
113
        self.id = bid
 
114
        self.account = account
 
115
        self.name = name
 
116
        self.alias = alias
 
117
 
 
118
 
105
119
class Pidgin:
106
120
 
107
121
    def __init__(self):
108
 
        self._parse_blist()
 
122
        # self._parse_blist()
109
123
        self._bus = dbus.SessionBus()
110
124
        obj = self._bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
111
125
        self._purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
113
127
    def _gather_buddies(self):
114
128
        self._buddies = []
115
129
        for account in self._purple.PurpleAccountsGetAllActive():
116
 
            accountname = self._purple.PurpleAccountGetUsername(account)
117
130
            buddylist = self._purple.PurpleFindBuddies(account, '')
118
131
            for buddyid in buddylist:
119
 
                buddy =  {}
120
 
                buddy["id"] = buddyid
121
 
                buddy["accountid"] = account
122
 
                buddy["account"] = accountname
123
 
                buddy["name"] = self._purple.PurpleBuddyGetName(buddyid)
124
 
                buddy["online"] = self._purple.PurpleBuddyIsOnline(buddyid)
125
 
                buddy["alias"] = self._purple.PurpleBuddyGetAlias(buddyid)
126
 
                if buddy["online"] != 0:
127
 
                    self._buddies.append(buddy)
 
132
                online = self._purple.PurpleBuddyIsOnline(buddyid)
 
133
                if online != 0:
 
134
                    name = self._purple.PurpleBuddyGetName(buddyid)
 
135
                    alias = self._purple.PurpleBuddyGetAlias(buddyid)
 
136
                    self._buddies.append(
 
137
                        Buddy(
 
138
                            buddyid,
 
139
                            account,
 
140
                            name,
 
141
                            alias)
 
142
                        )
128
143
 
129
144
        return self._buddies
130
145
 
131
 
    def _parse_blist(self):
132
 
        self._cache = {}
133
 
        fp = open("/home/sdague/.purple/blist.xml")
134
 
        element = xml.etree.ElementTree.parse(fp).getroot()
135
 
        buddies = element.findall("blist/group/contact/buddy")
136
 
        for buddy in buddies:
137
 
            account = buddy.attrib["account"]
138
 
            if not account in self._cache:
139
 
                self._cache[account] = {}
140
 
 
141
 
            name = buddy.getchildren()[0].text
142
 
            if not name in self._cache[account]:
143
 
                self._cache[account][name] = {}
144
 
 
145
 
            for elem in buddy.getchildren():
146
 
                if elem.tag == "alias":
147
 
                    self._cache[account][name]["alias"] = elem.text
148
 
                if elem.tag == "setting" and elem.attrib["name"] == "buddy_icon":
149
 
                    self._cache[account][name]["icon"] = elem.text
150
 
        fp.close()
151
 
 
152
 
    def _icon_for_buddy(self, buddy):
153
 
        if "icon" in self._cache[buddy["account"]][buddy["name"]]:
154
 
            icon = self._cache[buddy["account"]][buddy["name"]]["icon"]
155
 
            return "%s/.purple/icons/%s" % (os.environ["HOME"], icon)
156
 
        else:
157
 
            return "%s/.purple/icons/%s" % (os.environ["HOME"], "")
158
 
 
159
146
    def new_message(self, account, buddy):
160
147
        self._purple.PurpleConversationNew(1, int(account), buddy)
161
148
 
 
149
    def fill(self, buddy):
 
150
        icon = self._purple.PurpleBuddyGetIcon(buddy.id)
 
151
        if icon > 0:
 
152
            buddy.icon = self._purple.PurpleBuddyIconGetFullPath(icon)
 
153
        else:
 
154
            buddy.icon = None
 
155
 
 
156
 
 
157
    def is_active(self, buddy):
 
158
        presense = self._purple.PurpleBuddyGetPresence(buddy.id)
 
159
        status = self._purple.PurplePresenceGetActiveStatus(presense)
 
160
        return self._purple.PurpleStatusGetId(status) == "available"
 
161
 
162
162
    def search(self,  string):
163
163
        buddies = self._gather_buddies()
164
164
        results = []
 
165
        active = []
 
166
        rest = []
165
167
        for buddy in buddies:
166
 
            if ((buddy["name"].lower().find(string) >=0) or
167
 
                (buddy["alias"].lower().find(string) >=0) ):
168
 
 
169
 
                buddy["icon"] = self._icon_for_buddy(buddy)
170
 
                results.append(buddy)
171
 
        return results
172
 
 
 
168
            if ((buddy.name.lower().find(string) >=0) or
 
169
                (buddy.alias.lower().find(string) >=0) ):
 
170
                self.fill(buddy)
 
171
 
 
172
                if self.is_active(buddy):
 
173
                    active.append(buddy)
 
174
                else:
 
175
                    rest.append(buddy)
 
176
 
 
177
        active.extend(rest)
 
178
        return active
173
179
 
174
180
 
175
181
if __name__ == "__main__":