Mercurial > pidgin-audacious
annotate pidgin-audacious.c @ 18:dc3aa0bf24c0
moved to mpris access
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Sat, 04 Oct 2008 04:19:46 +0900 |
parents | 14de631fa929 |
children | 12d809123d69 |
rev | line source |
---|---|
13 | 1 /* Pidgin-Audacious plugin. |
0 | 2 * |
3 * This program is free software; you can redistribute it and/or | |
4 * modify it under the terms of the GNU General Public License as | |
5 * published by the Free Software Foundation; either version 2 of the | |
6 * License, or (at your option) any later version. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, but | |
9 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 * General Public License for more details. | |
12 * | |
13 * You should have received a copy of the GNU General Public License | |
14 * along with this program; if not, write to the Free Software | |
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
16 * 02111-1307, USA. | |
17 */ | |
18 #define PURPLE_PLUGINS 1 | |
19 | |
20 #include <stdio.h> | |
21 #include <stdlib.h> | |
22 #include <string.h> | |
23 #include <glib.h> | |
18 | 24 #include <dbus/dbus.h> |
25 #include <dbus/dbus-glib.h> | |
0 | 26 |
27 #include "gtkplugin.h" | |
28 #include "util.h" | |
29 #include "debug.h" | |
30 #include "connection.h" | |
31 #include "version.h" | |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
32 #include "cmds.h" |
18 | 33 #include "savedstatuses.h" |
0 | 34 |
13 | 35 #define PIDGINAUD_PLUGIN_ID "pidgin_audacious" |
0 | 36 |
11
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
37 #define OPT_PIDGINAUD "/plugins/pidgin_audacious" |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
38 #define OPT_PROCESS_STATUS OPT_PIDGINAUD "/process_status" |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
39 #define OPT_PROCESS_USERINFO OPT_PIDGINAUD "/process_userinfo" |
18 | 40 #define OPT_SONG_TEMPLATE OPT_PIDGINAUD "/song_template" |
11
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
41 #define OPT_PASTE_TEMPLATE OPT_PIDGINAUD "/paste_template" |
0 | 42 |
18 | 43 #define DEFAULT_SONG_TEMPLATE "%artist - %title" |
13 | 44 #define SONG_TOKEN "%song" |
45 #define NO_SONG_MESSAGE "No song being played." | |
0 | 46 |
18 | 47 #define DBUS_TYPE_G_STRING_VALUE_HASHTABLE (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)) |
48 | |
13 | 49 #define aud_debug(fmt, ...) purple_debug(PURPLE_DEBUG_INFO, \ |
50 "Pidgin-Audacious", \ | |
51 fmt, ## __VA_ARGS__); | |
52 #define aud_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, \ | |
53 "Pidgin-Audacious", \ | |
54 fmt, ## __VA_ARGS__); | |
18 | 55 /* xxx move up */ |
56 #define TITLE "%title" | |
57 #define ARTIST "%artist" | |
58 #define ALBUM "%album" | |
59 #define GENRE "%genre" | |
0 | 60 |
18 | 61 typedef struct song_tuple { |
62 gchar *title; | |
63 gchar *artist; | |
64 gchar *album; | |
65 gchar *genre; | |
66 } song_tuple; | |
67 | |
0 | 68 |
18 | 69 /* globals */ |
70 static GHashTable *seed_status; | |
71 static GHashTable *seed_userinfo; | |
72 static GHashTable *pushed_status; | |
73 static GHashTable *pushed_userinfo; | |
74 static DBusGConnection *connection = NULL; | |
75 static DBusGProxy *session = NULL; | |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
76 static PurpleCmdId cmdid_paste_current_song; |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
77 |
18 | 78 /* prototypes */ |
79 extern gchar *botch_utf(const gchar *msg, gsize len, gsize *newlen) __attribute__ ((weak)); | |
0 | 80 static void aud_process(gchar *aud_info); |
18 | 81 static void track_signal_cb(DBusGProxy *player_proxy, GHashTable *table, gpointer data); |
82 static void status_signal_cb(DBusGProxy *player_proxy, gint status, gpointer data); | |
83 static gboolean is_app_running(void); | |
84 static GHashTable *get_song_table(void); | |
85 static song_tuple *get_song_tuple(GHashTable *table); | |
86 | |
87 | |
88 /* implementation */ | |
0 | 89 |
12
79f081cdfb19
- fixed potential memory leaks.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
11
diff
changeset
|
90 static DBusGProxy * |
18 | 91 get_dbus_session(void) |
0 | 92 { |
93 GError *error = NULL; | |
18 | 94 DBusGProxy *proxy; |
0 | 95 connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error); |
96 g_clear_error(&error); | |
97 | |
18 | 98 proxy = dbus_g_proxy_new_for_name(connection, |
99 "org.mpris.audacious", | |
100 "/Player", | |
101 "org.freedesktop.MediaPlayer"); | |
0 | 102 |
103 g_clear_error(&error); | |
18 | 104 return proxy; |
105 } | |
106 | |
107 | |
108 static void | |
109 connect_dbus_signals() | |
110 { | |
111 dbus_g_proxy_add_signal(session, | |
112 "TrackChange", | |
113 DBUS_TYPE_G_STRING_VALUE_HASHTABLE, | |
114 G_TYPE_INVALID); | |
115 | |
116 dbus_g_proxy_connect_signal(session, | |
117 "TrackChange", | |
118 G_CALLBACK(track_signal_cb), | |
119 NULL, NULL); | |
120 | |
121 dbus_g_proxy_add_signal(session, | |
122 "StatusChange", | |
123 G_TYPE_INT, G_TYPE_INVALID); | |
124 | |
125 dbus_g_proxy_connect_signal(session, | |
126 "StatusChange", | |
127 G_CALLBACK(status_signal_cb), | |
128 NULL, NULL); | |
129 } | |
130 | |
131 static GHashTable * | |
132 get_song_table(void) | |
133 { | |
134 GHashTable *table = NULL; | |
135 | |
136 status_signal_cb(NULL, -1, NULL); | |
137 | |
138 if(is_app_running()) { | |
139 dbus_g_proxy_call(session, | |
140 "GetMetadata", | |
141 NULL, | |
142 G_TYPE_INVALID, | |
143 DBUS_TYPE_G_STRING_VALUE_HASHTABLE, | |
144 &table, | |
145 G_TYPE_INVALID); | |
146 } | |
147 | |
148 return table; | |
149 } | |
150 | |
151 static song_tuple * | |
152 get_song_tuple(GHashTable *table) | |
153 { | |
154 song_tuple *t = NULL; | |
155 GValue *value; | |
156 | |
157 if(!table) | |
158 return NULL; | |
159 | |
160 t = g_new0(song_tuple, 1); | |
161 | |
162 value = (GValue *) g_hash_table_lookup(table, "title"); | |
163 if (value && G_VALUE_HOLDS_STRING(value)) { | |
164 t->title = g_value_dup_string(value); | |
165 } | |
166 value = (GValue *) g_hash_table_lookup(table, "artist"); | |
167 if (value != NULL && G_VALUE_HOLDS_STRING(value)) { | |
168 t->artist = g_value_dup_string(value); | |
169 } | |
170 value = (GValue *) g_hash_table_lookup(table, "album"); | |
171 if (value != NULL && G_VALUE_HOLDS_STRING(value)) { | |
172 t->album = g_value_dup_string(value); | |
173 } | |
174 value = (GValue *) g_hash_table_lookup(table, "genre"); | |
175 if (value != NULL && G_VALUE_HOLDS_STRING(value)) { | |
176 t->genre = g_value_dup_string(value); | |
177 } | |
178 | |
179 return t; | |
0 | 180 } |
181 | |
18 | 182 void |
183 free_song_tuple(song_tuple *t) | |
184 { | |
185 g_free(t->title); | |
186 g_free(t->artist); | |
187 g_free(t->album); | |
188 g_free(t->genre); | |
189 | |
190 g_free(t); | |
191 } | |
192 | |
193 | |
194 static gchar * | |
195 format_song_info(song_tuple *tuple) | |
196 { | |
197 gchar *song_info = NULL, *tmp = NULL; | |
198 | |
199 if(!tuple) | |
200 return NULL; | |
201 | |
202 song_info = g_strdup(purple_prefs_get_string(OPT_SONG_TEMPLATE)); | |
203 if(!song_info) | |
204 return NULL; | |
205 | |
206 if(tuple->title && strstr(song_info, TITLE)) { | |
207 tmp = purple_strreplace(song_info, TITLE, tuple->title); | |
208 g_free(song_info); | |
209 song_info = tmp; | |
210 } | |
211 if(tuple->artist && strstr(song_info, ARTIST)) { | |
212 tmp = purple_strreplace(song_info, ARTIST, tuple->artist); | |
213 g_free(song_info); | |
214 song_info = tmp; | |
215 } | |
216 if(tuple->album && strstr(song_info, ALBUM)) { | |
217 tmp = purple_strreplace(song_info, ALBUM, tuple->album); | |
218 g_free(song_info); | |
219 song_info = tmp; | |
220 } | |
221 if(tuple->genre && strstr(song_info, GENRE)) { | |
222 tmp = purple_strreplace(song_info, GENRE, tuple->genre); | |
223 g_free(song_info); | |
224 song_info = tmp; | |
225 } | |
226 | |
227 return song_info; | |
228 } | |
229 | |
230 | |
231 | |
232 static void | |
233 track_signal_cb(DBusGProxy *player_proxy, GHashTable *table, gpointer data) | |
234 { | |
235 gchar *song_info = NULL; | |
236 song_tuple *tuple = get_song_tuple(table); | |
237 | |
238 /* set current song */ | |
239 purple_util_set_current_song(tuple->title ? tuple->title : "", | |
240 tuple->artist ? tuple->artist : "", | |
241 tuple->album ? tuple->album : ""); | |
242 | |
243 song_info = format_song_info(tuple); | |
244 | |
245 aud_process(song_info); | |
246 free_song_tuple(tuple); | |
247 g_free(song_info); | |
248 } | |
249 | |
250 | |
251 static void | |
252 status_signal_cb(DBusGProxy *player_proxy, gint status, gpointer data) | |
253 { | |
254 aud_debug("StatusChange %d\n", status); | |
255 | |
256 if (status != 0) { | |
257 aud_process(""); | |
258 | |
259 /* clear current song */ | |
260 purple_util_set_current_song(NULL, NULL, NULL); | |
261 | |
262 } | |
263 } | |
264 | |
265 | |
266 #if 0 | |
0 | 267 static gboolean |
268 watchdog_func(void) | |
269 { | |
13 | 270 gint playpos = 0; |
271 gchar *song = NULL, *tmp = NULL; | |
0 | 272 |
13 | 273 gboolean rv = TRUE; |
274 size_t dummy; | |
0 | 275 |
14
89bd98990fff
now it tries to get dbus proxy if session is NULL.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
13
diff
changeset
|
276 if(!session) { |
18 | 277 session = get_dbus_session(); |
14
89bd98990fff
now it tries to get dbus proxy if session is NULL.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
13
diff
changeset
|
278 } |
89bd98990fff
now it tries to get dbus proxy if session is NULL.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
13
diff
changeset
|
279 |
0 | 280 aud_debug("session = %p\n", session); |
281 aud_debug("is_playing = %d\n", audacious_remote_is_playing(session)); | |
282 | |
13 | 283 if(!audacious_remote_is_playing(session)) { /* audacious isn't playing */ |
0 | 284 aud_process(NULL); |
16
9e37b72309d4
- clear current song if audacious is not playing.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
15
diff
changeset
|
285 purple_util_set_current_song(NULL, NULL, NULL); |
0 | 286 return rv; |
287 } | |
288 | |
289 playpos = audacious_remote_get_playlist_pos(session); | |
290 tmp = audacious_remote_get_playlist_title(session, playpos); | |
291 if(tmp) { | |
13 | 292 if(botch_utf) /* if function exists */ |
0 | 293 song = (gchar *) botch_utf(tmp, strlen(tmp), &dummy); |
294 else | |
295 song = g_strdup(tmp); | |
296 } | |
297 g_free(tmp); | |
298 tmp = NULL; | |
299 | |
300 aud_process(song); | |
301 g_free(song); | |
302 song = NULL; | |
15
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
303 |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
304 /* preliminary support for purple_util_set_current_song() */ |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
305 gchar *artist = NULL, *title = NULL, *album = NULL; |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
306 |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
307 artist = audacious_get_tuple_field_data(session, "artist", playpos); |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
308 title = audacious_get_tuple_field_data(session, "title", playpos); |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
309 album = audacious_get_tuple_field_data(session, "album", playpos); |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
310 |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
311 purple_util_set_current_song(title ? title : "", |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
312 artist ? artist : "", |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
313 album ? album : ""); |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
314 |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
315 g_free(artist); |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
316 g_free(title); |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
317 g_free(album); |
b88a5f934224
preliminary support for purple_util_set_current_song()
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
14
diff
changeset
|
318 |
13 | 319 return rv; |
0 | 320 } |
18 | 321 #endif |
322 | |
323 | |
324 | |
325 | |
0 | 326 |
327 static void | |
328 aud_process_status(PurpleConnection *gc, gchar *aud_info) | |
329 { | |
13 | 330 gchar *new = NULL, *key = NULL; |
331 const gchar *current, *seed, *pushed, *proto; | |
332 PurpleAccount *account; | |
333 PurplePresence *presence; | |
334 PurplePlugin *prpl; | |
335 PurplePluginProtocolInfo *prpl_info; | |
336 PurpleStatus *status; | |
0 | 337 |
13 | 338 account = purple_connection_get_account(gc); |
339 presence = purple_account_get_presence(account); | |
0 | 340 |
13 | 341 proto = purple_account_get_protocol_id(account); |
342 prpl = purple_find_prpl(proto); | |
343 g_return_if_fail(prpl != NULL); | |
0 | 344 |
13 | 345 prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl); |
346 g_return_if_fail(prpl_info != NULL && prpl_info->set_status != NULL); | |
0 | 347 |
13 | 348 status = purple_presence_get_active_status(presence); |
349 g_return_if_fail(status != NULL); | |
0 | 350 |
13 | 351 /* generate key for hash table */ |
352 key = g_strdup_printf("%s %s", account->username, account->protocol_id); | |
0 | 353 |
1
46071692f191
implement new replace logic to status and userinfo.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
0
diff
changeset
|
354 /* retrieve current user status */ |
13 | 355 current = purple_status_get_attr_string(status, "message"); |
356 aud_debug("status current = %s\n", current); | |
0 | 357 |
1
46071692f191
implement new replace logic to status and userinfo.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
0
diff
changeset
|
358 /* invalidate pushded status on auto away etc. */ |
13 | 359 if(current == NULL || strlen(current) == 0) { |
360 g_hash_table_replace(pushed_status, g_strdup(key), g_strdup("")); | |
361 g_free(key); | |
1
46071692f191
implement new replace logic to status and userinfo.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
0
diff
changeset
|
362 return; |
46071692f191
implement new replace logic to status and userinfo.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
0
diff
changeset
|
363 } |
46071692f191
implement new replace logic to status and userinfo.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
0
diff
changeset
|
364 |
46071692f191
implement new replace logic to status and userinfo.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
0
diff
changeset
|
365 /* pop pushed_status */ |
46071692f191
implement new replace logic to status and userinfo.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
0
diff
changeset
|
366 pushed = (gchar *)g_hash_table_lookup(pushed_status, key); |
46071692f191
implement new replace logic to status and userinfo.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
0
diff
changeset
|
367 |
13 | 368 /* if current status differs from pushed_status or |
369 contains token, replace hashes with current. */ | |
370 if((pushed && g_ascii_strcasecmp(current, pushed)) || | |
371 strstr(current, SONG_TOKEN)) { | |
372 g_hash_table_replace(seed_status, g_strdup(key), g_strdup(current)); | |
1
46071692f191
implement new replace logic to status and userinfo.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
0
diff
changeset
|
373 g_hash_table_replace(pushed_status, g_strdup(key), g_strdup(current)); |
0 | 374 } |
375 | |
13 | 376 /* construct new status message */ |
377 seed = (gchar *)g_hash_table_lookup(seed_status, key); | |
378 if(!seed) { | |
379 g_free(key); | |
380 return; | |
381 } | |
382 aud_debug("status seed = %s\n", seed); | |
0 | 383 |
13 | 384 if(strstr(seed, SONG_TOKEN)) { |
385 if(aud_info){ | |
386 new = purple_strreplace(seed, SONG_TOKEN, aud_info); | |
387 } | |
388 else { | |
389 new = g_strdup(NO_SONG_MESSAGE); | |
390 } | |
391 } | |
12
79f081cdfb19
- fixed potential memory leaks.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
11
diff
changeset
|
392 |
13 | 393 if(!new) { |
394 g_free(key); | |
395 return; | |
396 } | |
0 | 397 |
13 | 398 /* set status message only if text has been changed */ |
399 pushed = (gchar *)g_hash_table_lookup(pushed_status, key); | |
400 aud_debug("status pushed = %s\n", pushed); | |
0 | 401 |
13 | 402 if (!pushed || g_ascii_strcasecmp(pushed, new)) { |
403 g_hash_table_replace(pushed_status, g_strdup(key), g_strdup(new)); | |
404 purple_status_set_attr_string(status, "message", new); | |
405 prpl_info->set_status(account, status); | |
406 } | |
407 g_free(key); | |
408 g_free(new); | |
0 | 409 } |
410 | |
411 | |
412 static void | |
413 aud_process_userinfo(PurpleConnection *gc, gchar *aud_info) | |
414 { | |
13 | 415 gchar *new = NULL, *key = NULL; |
416 const gchar *current, *seed, *pushed, *proto; | |
417 PurpleAccount *account; | |
418 PurplePlugin *prpl; | |
419 PurplePluginProtocolInfo *prpl_info; | |
0 | 420 |
13 | 421 account = purple_connection_get_account(gc); |
0 | 422 |
13 | 423 proto = purple_account_get_protocol_id(account); |
424 prpl = purple_find_prpl(proto); | |
425 g_return_if_fail(prpl != NULL); | |
0 | 426 |
13 | 427 prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl); |
428 g_return_if_fail(prpl_info != NULL && prpl_info->set_info != NULL); | |
0 | 429 |
13 | 430 /* generate key for hash table*/ |
431 key = g_strdup_printf("%s %s", account->username, account->protocol_id); | |
0 | 432 |
13 | 433 /* retrieve current user info */ |
434 current = purple_account_get_user_info(account); /* from account.xml */ | |
435 aud_debug("userinfo current = %s\n", current); | |
1
46071692f191
implement new replace logic to status and userinfo.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
0
diff
changeset
|
436 |
13 | 437 /* invalidate pushded status on auto away etc. */ |
438 if(current == NULL || strlen(current) == 0) { | |
439 g_hash_table_replace(pushed_userinfo, g_strdup(key), g_strdup("")); | |
440 g_free(key); | |
441 return; | |
442 } | |
1
46071692f191
implement new replace logic to status and userinfo.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
0
diff
changeset
|
443 |
13 | 444 /* pop pushed_userinfo */ |
445 pushed = g_hash_table_lookup(pushed_userinfo, key); | |
0 | 446 |
17
14de631fa929
drop outdated alias support
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
16
diff
changeset
|
447 /* if current userinfo differs from pushed_userinfo or contains |
14de631fa929
drop outdated alias support
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
16
diff
changeset
|
448 token, replace seed with this. */ |
13 | 449 if((pushed && g_ascii_strcasecmp(current, pushed)) || |
450 strstr(current, SONG_TOKEN)) { | |
451 g_hash_table_replace(seed_userinfo, g_strdup(key), | |
452 g_strdup(current)); | |
453 g_hash_table_replace(pushed_userinfo, g_strdup(key), | |
454 g_strdup(current)); | |
455 } | |
0 | 456 |
13 | 457 /* construct new status message */ |
458 seed = (gchar *)g_hash_table_lookup(seed_userinfo, key); | |
459 if(!seed) { | |
460 g_free(key); | |
461 return; | |
462 } | |
463 aud_debug("userinfo seed = %s\n", seed); | |
0 | 464 |
13 | 465 if(strstr(seed, SONG_TOKEN)) { |
466 if(aud_info){ | |
467 new = purple_strreplace(seed, SONG_TOKEN, aud_info); | |
468 } | |
469 else { | |
470 new = g_strdup(NO_SONG_MESSAGE); | |
471 } | |
472 } | |
12
79f081cdfb19
- fixed potential memory leaks.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
11
diff
changeset
|
473 |
13 | 474 if(!new) { |
475 g_free(key); | |
476 return; | |
477 } | |
0 | 478 |
13 | 479 /* set user info only if text has been changed */ |
480 pushed = (gchar *)g_hash_table_lookup(pushed_userinfo, key); | |
481 aud_debug("userinfo pushed = %s\n", pushed); | |
0 | 482 |
13 | 483 if (!pushed || g_ascii_strcasecmp(pushed, new) != 0) { |
484 g_hash_table_replace(pushed_userinfo, g_strdup(key), | |
485 g_strdup(new)); | |
486 prpl_info->set_info(gc, new); | |
487 } | |
488 g_free(key); | |
489 g_free(new); | |
0 | 490 } |
491 | |
492 | |
493 static void | |
494 aud_process(gchar *aud_info) | |
495 { | |
13 | 496 GList *l; |
497 PurpleConnection *gc; | |
0 | 498 |
13 | 499 for (l = purple_connections_get_all(); l != NULL; l = l->next) { |
500 gc = (PurpleConnection *) l->data; | |
0 | 501 |
13 | 502 /* make sure we're connected */ |
503 if (purple_connection_get_state(gc) != PURPLE_CONNECTED) { | |
504 continue; | |
505 } | |
0 | 506 |
13 | 507 if (purple_prefs_get_bool(OPT_PROCESS_USERINFO)) { |
508 aud_process_userinfo(gc, aud_info); | |
509 } | |
0 | 510 |
13 | 511 if (purple_prefs_get_bool(OPT_PROCESS_STATUS)) { |
512 aud_process_status(gc, aud_info); | |
513 } | |
18 | 514 } |
0 | 515 |
516 } | |
18 | 517 |
0 | 518 static void |
519 removekey(gpointer data) | |
520 { | |
13 | 521 g_free(data); |
0 | 522 } |
523 | |
524 static void | |
525 removeval(gpointer data) | |
526 { | |
13 | 527 g_free(data); |
0 | 528 } |
529 | |
18 | 530 |
531 | |
532 | |
533 | |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
534 static PurpleCmdRet |
13 | 535 paste_current_song(PurpleConversation *conv, const gchar *cmd, |
536 gchar **args, gchar **error, void *data) | |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
537 { |
13 | 538 gchar *song = NULL, *tmp = NULL, *tmp2 = NULL; |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
539 PurpleConversationType type = purple_conversation_get_type(conv); |
13 | 540 size_t dummy; |
11
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
541 const gchar *template = NULL; |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
542 |
13 | 543 /* audacious isn't playing */ |
18 | 544 if(!is_app_running()) { |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
545 return PURPLE_CMD_RET_OK; |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
546 } |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
547 |
18 | 548 /* dbus lookup */ |
549 GHashTable *table = get_song_table(); | |
550 song_tuple *tuple = get_song_tuple(table); | |
551 tmp = format_song_info(tuple); | |
552 free_song_tuple(tuple); | |
553 g_hash_table_destroy(table); | |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
554 |
11
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
555 template = purple_prefs_get_string(OPT_PASTE_TEMPLATE); |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
556 |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
557 if(template && strstr(template, SONG_TOKEN)) { |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
558 tmp2 = purple_strreplace(template, SONG_TOKEN, tmp); |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
559 g_free(tmp); |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
560 tmp = NULL; |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
561 } |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
562 else { |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
563 tmp2 = tmp; |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
564 } |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
565 |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
566 if(tmp2) { |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
567 if(botch_utf) { |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
568 song = (gchar *) botch_utf(tmp2, strlen(tmp2), &dummy); |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
569 g_free(tmp2); |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
570 tmp2 = NULL; |
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
571 } |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
572 else |
11
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
573 song = tmp2; |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
574 } |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
575 |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
576 if(type == PURPLE_CONV_TYPE_CHAT) { |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
577 PurpleConvChat *chat = purple_conversation_get_chat_data(conv); |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
578 if (chat && song) |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
579 purple_conv_chat_send(chat, song); |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
580 } |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
581 else if(type == PURPLE_CONV_TYPE_IM) { |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
582 PurpleConvIm *im = purple_conversation_get_im_data(conv); |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
583 if(im && song) |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
584 purple_conv_im_send(im, song); |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
585 } |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
586 |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
587 g_free(song); |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
588 return PURPLE_CMD_RET_OK; |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
589 } |
0 | 590 |
18 | 591 |
592 | |
593 | |
594 | |
595 static gboolean | |
596 is_app_running(void) | |
597 { | |
598 gchar *player_name = g_strconcat("org.mpris.", "audacious", NULL); | |
599 | |
600 #if 0 | |
601 if(g_strcasecmp(pidginmpris->player_name, player_name) != 0) { | |
602 pidginmpris->player_name = g_strdup(player_name); | |
603 g_object_unref(pidginmpris->player); | |
604 mpris_connect_dbus_signals(); | |
605 } | |
606 #endif | |
607 | |
608 DBusGProxy *player = | |
609 dbus_g_proxy_new_for_name_owner(connection, | |
610 player_name, | |
611 "/Player", | |
612 "org.freedesktop.MediaPlayer", | |
613 NULL); | |
614 | |
615 if(!player) | |
616 return FALSE; | |
617 | |
618 g_object_unref(player); | |
619 g_free(player_name); | |
620 return TRUE; | |
621 } | |
622 | |
623 static void | |
624 signed_on_cb(PurpleConnection *gc, void *data) | |
625 { | |
626 gchar *song_info = NULL; | |
627 GHashTable *table = NULL; | |
628 song_tuple *tuple = NULL; | |
629 | |
630 table = get_song_table(); | |
631 tuple = get_song_tuple(table); | |
632 | |
633 song_info = format_song_info(tuple); | |
634 free_song_tuple(tuple); | |
635 g_hash_table_destroy(table); | |
636 | |
637 if(song_info) | |
638 aud_process(song_info); | |
639 | |
640 g_free(song_info); | |
641 } | |
642 | |
643 | |
644 static void prefs_cb(const char *name, PurplePrefType type, | |
645 gconstpointer value, gpointer data) | |
646 { | |
647 aud_debug("settings change detected at %s\n", name); | |
648 signed_on_cb(NULL, NULL); | |
649 } | |
650 | |
0 | 651 static gboolean |
652 load_plugin(PurplePlugin *plugin) | |
653 { | |
13 | 654 seed_status = g_hash_table_new_full(g_str_hash, g_str_equal, |
655 removekey, removeval); | |
656 seed_userinfo = g_hash_table_new_full(g_str_hash, g_str_equal, | |
657 removekey, removeval); | |
0 | 658 |
13 | 659 pushed_status = g_hash_table_new_full(g_str_hash, g_str_equal, |
660 removekey, removeval); | |
661 pushed_userinfo = g_hash_table_new_full(g_str_hash, g_str_equal, | |
662 removekey, removeval); | |
0 | 663 |
18 | 664 |
665 session = get_dbus_session(); | |
666 | |
667 /* connect to mpris signals */ | |
668 connect_dbus_signals(); | |
669 | |
670 /* connect to purple signals */ | |
671 purple_signal_connect(purple_connections_get_handle(), | |
672 "signed-on", | |
673 plugin, | |
674 PURPLE_CALLBACK(signed_on_cb), | |
675 NULL); | |
0 | 676 |
18 | 677 purple_signal_connect(purple_savedstatuses_get_handle(), |
678 "savedstatus-changed", | |
679 plugin, | |
680 PURPLE_CALLBACK(signed_on_cb), | |
681 NULL); | |
682 | |
683 purple_prefs_connect_callback(purple_prefs_get_handle(), | |
684 OPT_PIDGINAUD, | |
685 prefs_cb, | |
686 NULL); | |
687 | |
688 status_signal_cb(NULL, -1, NULL); | |
689 | |
690 | |
691 /* register /song command */ | |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
692 cmdid_paste_current_song = |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
693 purple_cmd_register("song", "", PURPLE_CMD_P_DEFAULT, |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
694 PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_CHAT, |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
695 NULL, paste_current_song, |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
696 "song: Paste currently plaing song", NULL); |
0 | 697 |
13 | 698 return TRUE; |
0 | 699 } |
700 | |
701 static gboolean | |
702 unload_plugin(PurplePlugin *plugin) | |
703 { | |
13 | 704 aud_debug("pidgin-audacious unload called\n"); |
0 | 705 |
13 | 706 g_hash_table_destroy(seed_status); |
707 g_hash_table_destroy(seed_userinfo); | |
0 | 708 |
13 | 709 g_hash_table_destroy(pushed_status); |
710 g_hash_table_destroy(pushed_userinfo); | |
0 | 711 |
10
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
712 purple_cmd_unregister(cmdid_paste_current_song); |
7c9624c8a109
added /song command which sends name of currently playing song.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
9
diff
changeset
|
713 |
18 | 714 if(session) { |
715 g_object_unref(session); | |
716 session = NULL; | |
717 } | |
14
89bd98990fff
now it tries to get dbus proxy if session is NULL.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
13
diff
changeset
|
718 |
16
9e37b72309d4
- clear current song if audacious is not playing.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
15
diff
changeset
|
719 if(connection) { |
9e37b72309d4
- clear current song if audacious is not playing.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
15
diff
changeset
|
720 dbus_g_connection_unref(connection); |
9e37b72309d4
- clear current song if audacious is not playing.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
15
diff
changeset
|
721 connection = NULL; |
9e37b72309d4
- clear current song if audacious is not playing.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
15
diff
changeset
|
722 } |
9e37b72309d4
- clear current song if audacious is not playing.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
15
diff
changeset
|
723 |
13 | 724 return TRUE; |
0 | 725 } |
726 | |
727 static PurplePluginPrefFrame * | |
728 get_plugin_pref_frame(PurplePlugin *plugin) | |
729 { | |
13 | 730 PurplePluginPref *pref; |
731 PurplePluginPrefFrame *frame = purple_plugin_pref_frame_new(); | |
0 | 732 |
13 | 733 /* create gtk elements for the plugin preferences */ |
734 pref = purple_plugin_pref_new_with_label( | |
735 "Pidgin-Audacious Configuration"); | |
736 purple_plugin_pref_frame_add(frame, pref); | |
9
d5702f04b19c
Fixed a bug that disabling this plugin while watch dog function has been hooked causes crash.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
8
diff
changeset
|
737 |
13 | 738 pref = purple_plugin_pref_new_with_name_and_label( |
739 OPT_PROCESS_STATUS, | |
740 "Expand " SONG_TOKEN " to song info in the status message"); | |
741 purple_plugin_pref_frame_add(frame, pref); | |
9
d5702f04b19c
Fixed a bug that disabling this plugin while watch dog function has been hooked causes crash.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
8
diff
changeset
|
742 |
13 | 743 pref = purple_plugin_pref_new_with_name_and_label( |
744 OPT_PROCESS_USERINFO, | |
745 "Expand " SONG_TOKEN " to song info in the user info"); | |
746 purple_plugin_pref_frame_add(frame, pref); | |
0 | 747 |
18 | 748 /* song template */ |
749 pref = purple_plugin_pref_new_with_name_and_label( | |
750 OPT_SONG_TEMPLATE, | |
751 "Song template"); | |
752 purple_plugin_pref_frame_add(frame, pref); | |
753 | |
11
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
754 /* paste template */ |
13 | 755 pref = purple_plugin_pref_new_with_name_and_label( |
756 OPT_PASTE_TEMPLATE, | |
757 "Paste template"); | |
758 purple_plugin_pref_frame_add(frame, pref); | |
11
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
759 |
13 | 760 return frame; |
0 | 761 } |
762 | |
763 static PurplePluginUiInfo pref_info = | |
764 { | |
13 | 765 get_plugin_pref_frame |
0 | 766 }; |
767 | |
768 static PurplePluginInfo info = | |
769 { | |
13 | 770 PURPLE_PLUGIN_MAGIC, |
771 PURPLE_MAJOR_VERSION, | |
772 PURPLE_MINOR_VERSION, | |
773 PURPLE_PLUGIN_STANDARD, /**< type */ | |
774 NULL, /**< ui_req */ | |
775 0, /**< flags */ | |
776 NULL, /**< deps */ | |
777 PURPLE_PRIORITY_DEFAULT, /**< priority */ | |
778 PIDGINAUD_PLUGIN_ID, /**< id */ | |
779 "Pidgin-Audacious", /**< name */ | |
18 | 780 "3.0.0d2", /**< version */ |
13 | 781 "Automatically updates your Pidgin status info with the currently " |
782 "playing music in Audacious.", /** summary */ | |
783 "Automatically updates your Pidgin status info with the currently " | |
784 "playing music in Audacious.", /** desc */ | |
785 "Yoshiki Yazawa (yaz@honeyplanet.jp)", /**< author */ | |
786 "http://www.honeyplanet.jp", /**< homepage */ | |
787 load_plugin, /**< load */ | |
788 unload_plugin, /**< unload */ | |
789 NULL, /**< destroy */ | |
790 NULL, /**< ui_info */ | |
791 NULL, /**< extra_info */ | |
792 &pref_info, /**< pref info */ | |
793 NULL | |
0 | 794 }; |
795 | |
796 static void | |
797 init_plugin(PurplePlugin *plugin) | |
798 { | |
13 | 799 g_type_init(); |
0 | 800 |
13 | 801 /* add plugin preferences */ |
802 purple_prefs_add_none(OPT_PIDGINAUD); | |
803 purple_prefs_add_bool(OPT_PROCESS_STATUS, TRUE); | |
804 purple_prefs_add_bool(OPT_PROCESS_USERINFO, TRUE); | |
18 | 805 purple_prefs_add_string(OPT_SONG_TEMPLATE, DEFAULT_SONG_TEMPLATE); |
11
43cb653de212
paste template has been implemented.
Yoshiki Yazawa <yaz@cc.rim.or.jp>
parents:
10
diff
changeset
|
806 purple_prefs_add_string(OPT_PASTE_TEMPLATE, SONG_TOKEN); |
0 | 807 |
808 } | |
809 | |
810 PURPLE_INIT_PLUGIN(pidgin_audacious, init_plugin, info) |