Merge lp://qastaging/~cszikszoy/do-plugins/pidgin-bonjour into lp://qastaging/do-plugins

Proposed by Chris S.
Status: Merged
Approved by: Alex Launi
Approved revision: 589
Merged at revision: 595
Proposed branch: lp://qastaging/~cszikszoy/do-plugins/pidgin-bonjour
Merge into: lp://qastaging/do-plugins
Diff against target: None lines
To merge this branch: bzr merge lp://qastaging/~cszikszoy/do-plugins/pidgin-bonjour
Reviewer Review Type Date Requested Status
Alex Launi (community) Approve
Review via email: mp+5220@code.qastaging.launchpad.net
To post a comment you must log in.
Revision history for this message
Chris S. (cszikszoy) wrote :

Support link-local (bonjour) buddies and some random clean-up.

Revision history for this message
Alex Launi (alexlauni) wrote :

use whitespace to make your methods more readable, in particular im thinking of Pidgin.cs:GetBuddyIconPath, after you check icon == 0, put the return null on the next line indented, and remove the extra spaces after the icon variable assignment.

Why are you using arrays in these methods? Wouldn't it be cleaner to use IEnumerables? You could then in FindBuddies, removed the loop and just return prpl.PurpleFindBuddies.

Can you arrange the assignments in CreateBuddy so that they're in order based on length? It'll pretty up the source a bit. The same thing in PidginSavedStatusItemSource.cs

in CreateBuddy (XmlNode buddyNode) where you check name == null || protos.Values.Count () <= 0) put the return null on the next line. Do this similar thing anywhere you notice that I missed as well.

seems to work well though, fix those issues and merge.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Pidgin/Resources/Pidgin.addin.xml'
2--- Pidgin/Resources/Pidgin.addin.xml 2009-03-16 02:41:13 +0000
3+++ Pidgin/Resources/Pidgin.addin.xml 2009-04-03 22:27:17 +0000
4@@ -1,7 +1,7 @@
5 <Addin
6 id="Pidgin"
7 namespace="Do"
8- version="2.0"
9+ version="2.1"
10 name="Pidgin"
11 description="Search buddies and initiate chats in Pidgin."
12 author="David Siegel, Alex Launi, Chris Szikszoy"
13
14=== modified file 'Pidgin/src/Pidgin.cs'
15--- Pidgin/src/Pidgin.cs 2009-03-20 19:56:08 +0000
16+++ Pidgin/src/Pidgin.cs 2009-04-03 22:12:46 +0000
17@@ -40,17 +40,21 @@
18 public interface IPurpleObject
19 {
20 int[] PurpleAccountsGetAll ();
21- int [] PurpleAccountsGetAllActive ();
22+ int PurpleBuddyGetIcon (int buddy);
23+ int[] PurpleAccountsGetAllActive ();
24 bool PurpleBuddyIsOnline (int buddy);
25 int PurpleBuddyGetAccount (int buddy);
26 string PurpleAccountGetAlias (int account);
27 bool PurpleAccountIsConnected (int account);
28+ string PurpleBuddyGetLocalAlias (int buddy);
29 string PurpleBuddyGetServerAlias (int buddy);
30+ string PurpleBuddyIconGetFullPath (int icon);
31 string PurpleAccountGetUsername (int account);
32 int PurpleFindBuddy (int account, string name);
33 string PurpleAccountGetProtocolName (int account);
34+ int[] PurpleFindBuddies (int account, string name);
35 int PurpleAccountsFindConnected (string account, string proto);
36- void PurpleAccountSetEnabled (int account, string ui, int value);
37+ void PurpleAccountSetEnabled (int account, string ui, int val);
38
39 int PurpleSavedstatusGetCurrent ();
40 int [] PurpleSavedstatusesGetAll ();
41@@ -68,11 +72,13 @@
42 void PurpleConversationPresent (int conversation);
43 int PurpleConversationNew (int type, int account, string name);
44
45+
46+
47 #region Pidgin < 2.5.4 compatibility methods
48
49 int PurpleSavedstatusNew (string title, uint type);
50 int PurpleConversationNew (uint type, int account, string name);
51- void PurpleAccountSetEnabled (int account, string ui, uint value);
52+ void PurpleAccountSetEnabled (int account, string ui, uint val);
53
54 #endregion
55 }
56@@ -91,10 +97,20 @@
57 if (parts.Length >= 2) {
58 if (parts[0] == "prpl")
59 proto = parts[1];
60- icon = Path.Combine ("/usr/share/pixmaps/pidgin/protocols/48", proto + ".png");
61 }
62+ icon = Path.Combine ("/usr/share/pixmaps/pidgin/protocols/48", proto + ".png");
63 return File.Exists (icon) ? icon : Pidgin.ChatIcon;
64 }
65+
66+ public static string GetBuddyIconPath (int buddyID)
67+ {
68+ IPurpleObject prpl = GetPurpleObject ();
69+
70+ int icon = prpl.PurpleBuddyGetIcon (buddyID);
71+ if (icon == 0) return null;
72+ string iconPath = prpl.PurpleBuddyIconGetFullPath (icon);
73+ return (File.Exists (iconPath)) ? iconPath : null;
74+ }
75
76 public static IPurpleObject GetPurpleObject ()
77 {
78@@ -105,14 +121,12 @@
79 return null;
80 }
81 }
82-
83+
84 private static int [] ConnectedAccounts {
85 get {
86- List<int> connected;
87- IPurpleObject prpl;
88+ List<int> connected = new List<int> ();
89+ IPurpleObject prpl = GetPurpleObject ();
90
91- prpl = GetPurpleObject ();
92- connected = new List<int> ();
93 try {
94 foreach (int account in prpl.PurpleAccountsGetAllActive ()) {
95 if (prpl.PurpleAccountIsConnected (account))
96@@ -123,6 +137,42 @@
97 }
98 }
99
100+ public static int GetAccountID (string name, string proto)
101+ {
102+ IPurpleObject prpl = GetPurpleObject ();
103+ int account;
104+ try {
105+ account = prpl.PurpleAccountsFindConnected (name, proto);
106+ } catch {
107+ account = -1;
108+ }
109+ return account;
110+ }
111+
112+ public static int[] FindBuddies (int account, string name)
113+ {
114+ List<int> buddies = new List<int> ();
115+ IPurpleObject prpl = GetPurpleObject ();
116+
117+ foreach (int buddy in prpl.PurpleFindBuddies (account, name))
118+ buddies.Add (buddy);
119+
120+ return buddies.ToArray ();
121+ }
122+
123+ public static string GetBuddyLocalAlias (int buddy)
124+ {
125+ IPurpleObject prpl = GetPurpleObject ();
126+
127+ if (!InstanceIsRunning)
128+ return null;
129+
130+ //don't need to do too much error checking here,
131+ //this method should always return something and it's only used
132+ //for bonjour, which _will_ always return what I'm looking for.
133+ return prpl.PurpleBuddyGetLocalAlias (buddy);
134+ }
135+
136 public static string GetBuddyServerAlias (string name)
137 {
138 IPurpleObject prpl = GetPurpleObject ();
139@@ -136,10 +186,20 @@
140 buddy = prpl.PurpleFindBuddy (account, name);
141 if (buddy == 0) continue;
142 alias = prpl.PurpleBuddyGetServerAlias (buddy);
143- return (string.IsNullOrEmpty (alias)) ? null : alias;
144+ return string.IsNullOrEmpty (alias) ? null : alias;
145 }
146 return null;
147 }
148+
149+ public static string GetBuddyServerAlias (int buddy)
150+ {
151+ IPurpleObject prpl = GetPurpleObject ();
152+
153+ if (!InstanceIsRunning)
154+ return null;
155+ string alias = prpl.PurpleBuddyGetServerAlias (buddy);
156+ return string.IsNullOrEmpty (alias) ? null : alias;
157+ }
158
159 public static bool BuddyIsOnline (string name)
160 {
161
162=== modified file 'Pidgin/src/PidginAccountItem.cs'
163--- Pidgin/src/PidginAccountItem.cs 2009-01-15 19:07:08 +0000
164+++ Pidgin/src/PidginAccountItem.cs 2009-04-03 22:12:46 +0000
165@@ -50,11 +50,7 @@
166 }
167
168 public override string Icon {
169- get {
170- string icon = Path.Combine (
171- "/usr/share/pixmaps/pidgin/protocols/48", Proto + ".png");
172- return File.Exists (icon) ? icon : "internet-group-chat";
173- }
174+ get { return Pidgin.GetProtocolIcon (this.Proto); }
175 }
176
177 }
178
179=== modified file 'Pidgin/src/PidginAccountItemSource.cs'
180--- Pidgin/src/PidginAccountItemSource.cs 2009-01-15 22:39:03 +0000
181+++ Pidgin/src/PidginAccountItemSource.cs 2009-04-03 22:12:46 +0000
182@@ -62,17 +62,18 @@
183 Pidgin.IPurpleObject prpl;
184 prpl = Pidgin.GetPurpleObject ();
185 string name, proto;
186-
187- items.Clear ();
188- try {
189- foreach (int account in prpl.PurpleAccountsGetAll ()) {
190- proto = prpl.PurpleAccountGetProtocolName (account);
191- name = prpl.PurpleAccountGetUsername (account);
192- items.Add (new PidginAccountItem (name, proto, account));
193+ if (Pidgin.InstanceIsRunning) {
194+ items.Clear ();
195+ try {
196+ foreach (int account in prpl.PurpleAccountsGetAll ()) {
197+ proto = prpl.PurpleAccountGetProtocolName (account);
198+ name = prpl.PurpleAccountGetUsername (account);
199+ items.Add (new PidginAccountItem (name, proto, account));
200+ }
201+ } catch (Exception e) {
202+ Log<PidginAccountItemSource>.Error ("Could not get Pidgin accounts: {0}", e.Message);
203+ Log<PidginAccountItemSource>.Debug (e.StackTrace);
204 }
205- } catch (Exception e) {
206- Log<PidginAccountItemSource>.Error ("Could not get Pidgin accounts: {0}", e.Message);
207- Log<PidginAccountItemSource>.Debug (e.StackTrace);
208 }
209 }
210 }
211
212=== modified file 'Pidgin/src/PidginContactItemSource.cs'
213--- Pidgin/src/PidginContactItemSource.cs 2009-03-20 07:02:03 +0000
214+++ Pidgin/src/PidginContactItemSource.cs 2009-04-04 05:49:31 +0000
215@@ -99,8 +99,16 @@
216 // Add buddies as they are encountered to this hash so we don't create duplicates.
217 Dictionary<ContactItem, bool> buddies_seen;
218
219+ //remove pidgin related keys from the buddies
220+ foreach (ContactItem buddy in buddies) {
221+ foreach (string key in buddy.Details.Where (d => d.Contains ("prpl")).ToArray ())
222+ buddy[key] = "";
223+ }
224+
225+
226 buddies.Clear ();
227 buddies_seen = new Dictionary<ContactItem, bool> ();
228+ //load buddies from xml file
229 blist = new XmlDocument ();
230 try {
231 blist.Load (BuddyListFile);
232@@ -117,21 +125,64 @@
233 Log<PidginContactItemSource>.Error ("Error reading Pidgin buddy list file: {0}", e.Message);
234 Log<PidginContactItemSource>.Debug (e.StackTrace);
235 }
236+ //if Pidgin is running, find bonjour buddies from dbus
237+ //link-local (bonjour) buddies do not get added to the xml file
238+ try {
239+ int bonjourAccount = Pidgin.GetAccountID ("","prpl-bonjour");
240+ if (bonjourAccount != -1) {
241+ foreach (int buddyID in Pidgin.FindBuddies (bonjourAccount, "")) {
242+ ContactItem buddy;
243+
244+ buddy = CreateBuddy (buddyID);
245+ buddies_seen [buddy] = true;
246+ }
247+ }
248+ } catch (Exception e) {
249+ Log<PidginContactItemSource>.Error ("Could not get Pidgin accounts: {0}", e.Message);
250+ Log<PidginContactItemSource>.Debug (e.StackTrace);
251+ }
252 foreach (ContactItem buddy in buddies_seen.Keys) {
253 buddies.Add (buddy);
254 }
255 }
256
257+ //We're only using this to get link-local (bonjour) buddies
258+ ContactItem CreateBuddy (int buddyID)
259+ {
260+ ContactItem buddy;
261+ string alias, proto, icon, accountAlias;
262+
263+ alias = Pidgin.GetBuddyServerAlias(buddyID);
264+ accountAlias = Pidgin.GetBuddyLocalAlias (buddyID);
265+ icon = Pidgin.GetBuddyIconPath (buddyID);
266+ proto = "prpl-bonjour";
267+ buddy = ContactItem.Create (alias);
268+
269+ //if for some reason this buddy has multiple prpl-bonjour accounts associated with it
270+ //make sure we add them all in this fashion: prpl-bonjour, prpl-bonjour-1, etc.
271+ int similarProtos = buddy.Details.Where (k => k.StartsWith (proto)).Count ();
272+ if (similarProtos > 0)
273+ proto = string.Format ("{0}-{1}", proto, similarProtos.ToString ());
274+
275+ buddy[proto] = accountAlias;
276+ if (!string.IsNullOrEmpty (icon))
277+ buddy[iconPrefix+proto] = icon;
278+ if (string.IsNullOrEmpty (buddy["photo"]))
279+ buddy["photo"] = icon;
280+
281+ return buddy;
282+ }
283+
284
285 ContactItem CreateBuddy(XmlNode buddyNode)
286 {
287 ContactItem buddy;
288- string name, alias, icon, proto;
289+ string name, alias, proto;
290
291 Dictionary<string, string> icons = new Dictionary<string, string> ();
292 Dictionary<string, string> protos = new Dictionary<string, string> ();
293
294- alias = icon = name = null;
295+ alias = name = null;
296
297 //we favor aliases in this order: metacontact alias, local alias, server alias
298 //metacontact alias
299@@ -188,25 +239,22 @@
300 name = alias ?? protos.Values.FirstOrDefault ();
301
302 // If crucial details are missing, we can't make a buddy.
303- if (name == null || protos.Values.Count () < 0) return null;
304+ if (name == null || protos.Values.Count () <= 0) return null;
305
306 // Create a new buddy, add the details we have.
307 buddy = ContactItem.Create (alias ?? name);
308
309- //remove old pidgin-related keys here
310- foreach (string key in buddy.Details.Where (d => d.Contains ("prpl")).ToArray ())
311- buddy[key] = "";
312-
313 //assign the default buddy icon as the ContactItem's photo
314 if (icons.Keys.Contains ("default"))
315 buddy["photo"] = icons["default"];
316
317 //add all of the protocol handles we found for this buddy
318- foreach (string k in protos.Keys)
319+ //skip bonjour keys, they'll get picked up later
320+ foreach (string k in protos.Keys.Where (k => !k.Contains ("bonjour")))
321 buddy[k] = protos[k];
322
323 //add the icons keys to create individual icons for childitems
324- foreach (string k in icons.Keys.Where (k => k != "default"))
325+ foreach (string k in icons.Keys.Where (k => k != "default" && !k.Contains ("bonjour")))
326 buddy[k] = icons[k];
327
328 return buddy;
329
330=== modified file 'Pidgin/src/PidginSavedStatusItemSource.cs'
331--- Pidgin/src/PidginSavedStatusItemSource.cs 2009-03-20 19:49:34 +0000
332+++ Pidgin/src/PidginSavedStatusItemSource.cs 2009-04-03 22:12:46 +0000
333@@ -70,27 +70,29 @@
334 {
335 Pidgin.IPurpleObject prpl;
336 int [] rawStatuses;
337- try {
338- prpl = Pidgin.GetPurpleObject ();
339- foreach (Item status in statuses.Where (i => i is PidginSavedStatusItem).ToArray ())
340- statuses.Remove (status);
341- rawStatuses = prpl.PurpleSavedstatusesGetAll ();
342- foreach (int status in rawStatuses) {
343- if (!prpl.PurpleSavedstatusIsTransient (status)) {
344- string title, message;
345- int id, statId;
346-
347- title = prpl.PurpleSavedstatusGetTitle (status);
348- message = prpl.PurpleSavedstatusGetMessage (status);
349- id = prpl.PurpleSavedstatusFind (title);
350- statId = prpl.PurpleSavedstatusGetType (status);
351-
352- statuses.Add (new PidginSavedStatusItem (title,message,id,statId));
353+ if (Pidgin.InstanceIsRunning) {
354+ try {
355+ prpl = Pidgin.GetPurpleObject ();
356+ foreach (Item status in statuses.Where (i => i is PidginSavedStatusItem).ToArray ())
357+ statuses.Remove (status);
358+ rawStatuses = prpl.PurpleSavedstatusesGetAll ();
359+ foreach (int status in rawStatuses) {
360+ if (!prpl.PurpleSavedstatusIsTransient (status)) {
361+ string title, message;
362+ int id, statId;
363+
364+ title = prpl.PurpleSavedstatusGetTitle (status);
365+ message = prpl.PurpleSavedstatusGetMessage (status);
366+ id = prpl.PurpleSavedstatusFind (title);
367+ statId = prpl.PurpleSavedstatusGetType (status);
368+
369+ statuses.Add (new PidginSavedStatusItem (title,message,id,statId));
370+ }
371 }
372+ } catch (Exception e) {
373+ Log<PidginSavedStatusItemSource>.Error ("Could not read saved statuses: {0}", e.Message);
374+ Log<PidginSavedStatusItemSource>.Debug (e.StackTrace);
375 }
376- } catch (Exception e) {
377- Log<PidginSavedStatusItemSource>.Error ("Could not read saved statuses: {0}", e.Message);
378- Log<PidginSavedStatusItemSource>.Debug (e.StackTrace);
379 }
380 }
381 }

Subscribers

People subscribed via source and target branches