Convert Finch plugins to libsoup.
[pidgin-git.git] / finch / gntsound.c
blob977057cff20cf61e5be999c85af93ceb1b7f5375
1 /*
2 * finch
4 * Finch is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "finch.h"
24 #include <internal.h>
26 #include NCURSES_HEADER
28 #ifdef _WIN32
29 #include <windows.h>
30 #include <mmsystem.h>
31 #endif
33 #ifdef USE_GSTREAMER
34 #include <gst/gst.h>
35 #endif /* USE_GSTREAMER */
37 #include "debug.h"
38 #include "notify.h"
39 #include "prefs.h"
40 #include "sound.h"
41 #include "util.h"
43 #include "gntconv.h"
45 #include "gntbox.h"
46 #include "gntwindow.h"
47 #include "gntcombobox.h"
48 #include "gntlabel.h"
49 #include "gntconv.h"
50 #include "gntsound.h"
51 #include "gntwidget.h"
52 #include "gntentry.h"
53 #include "gntcheckbox.h"
54 #include "gntline.h"
55 #include "gnttree.h"
56 #include "gntfilesel.h"
58 typedef struct {
59 PurpleSoundEventID id;
60 char *label;
61 char *pref;
62 char *def;
63 char *file;
64 } FinchSoundEvent;
66 typedef struct {
67 GntWidget *method;
68 GntWidget *command;
69 GntWidget *conv_focus;
70 GntWidget *while_status;
71 GntWidget *events;
72 GntWidget *window;
73 GntWidget *selector;
75 GntWidget *profiles;
76 GntWidget *new_profile;
77 gchar * original_profile;
78 } SoundPrefDialog;
80 #define DEFAULT_PROFILE "default"
82 static SoundPrefDialog *pref_dialog;
84 #define PLAY_SOUND_TIMEOUT 15000
86 static guint mute_login_sounds_timeout = 0;
87 static gboolean mute_login_sounds = FALSE;
89 #ifdef USE_GSTREAMER
90 static gboolean gst_init_failed;
91 #endif /* USE_GSTREAMER */
93 static FinchSoundEvent sounds[PURPLE_NUM_SOUNDS] = {
94 {PURPLE_SOUND_BUDDY_ARRIVE, N_("Buddy logs in"), "login", "login.wav", NULL},
95 {PURPLE_SOUND_BUDDY_LEAVE, N_("Buddy logs out"), "logout", "logout.wav", NULL},
96 {PURPLE_SOUND_RECEIVE, N_("Message received"), "im_recv", "receive.wav", NULL},
97 {PURPLE_SOUND_FIRST_RECEIVE, N_("Message received begins conversation"), "first_im_recv", "receive.wav", NULL},
98 {PURPLE_SOUND_SEND, N_("Message sent"), "send_im", "send.wav", NULL},
99 {PURPLE_SOUND_CHAT_JOIN, N_("Person enters chat"), "join_chat", "login.wav", NULL},
100 {PURPLE_SOUND_CHAT_LEAVE, N_("Person leaves chat"), "left_chat", "logout.wav", NULL},
101 {PURPLE_SOUND_CHAT_YOU_SAY, N_("You talk in chat"), "send_chat_msg", "send.wav", NULL},
102 {PURPLE_SOUND_CHAT_SAY, N_("Others talk in chat"), "chat_msg_recv", "receive.wav", NULL},
103 {PURPLE_SOUND_POUNCE_DEFAULT, NULL, "pounce_default", "alert.wav", NULL},
104 {PURPLE_SOUND_CHAT_NICK, N_("Someone says your username in chat"), "nick_said", "alert.wav", NULL},
105 {PURPLE_SOUND_GOT_ATTENTION, N_("Attention received"), "got_attention", "alert.wav", NULL}
108 const char *
109 finch_sound_get_active_profile()
111 return purple_prefs_get_string(FINCH_PREFS_ROOT "/sound/actprofile");
114 /* This method creates a pref name based on the current active profile.
115 * So if "Home" is the current active profile the pref name
116 * [FINCH_PREFS_ROOT "/sound/profiles/Home/$NAME"] is created.
118 static gchar *
119 make_pref(const char *name)
121 static char pref_string[512];
122 g_snprintf(pref_string, sizeof(pref_string),
123 FINCH_PREFS_ROOT "/sound/profiles/%s%s", finch_sound_get_active_profile(), name);
124 return pref_string;
128 static gboolean
129 unmute_login_sounds_cb(gpointer data)
131 mute_login_sounds = FALSE;
132 mute_login_sounds_timeout = 0;
133 return FALSE;
136 static gboolean
137 chat_nick_matches_name(PurpleChatConversation *chat, const char *aname)
139 char *nick = NULL;
140 char *name = NULL;
141 gboolean ret = FALSE;
142 PurpleAccount *account;
144 if (chat == NULL)
145 return ret;
147 account = purple_conversation_get_account(PURPLE_CONVERSATION(chat));
148 nick = g_strdup(purple_normalize(account, purple_chat_conversation_get_nick(chat)));
149 name = g_strdup(purple_normalize(account, aname));
151 if (g_utf8_collate(nick, name) == 0)
152 ret = TRUE;
154 g_free(nick);
155 g_free(name);
157 return ret;
161 * play a sound event for a conversation, honoring make_sound flag
162 * of conversation and checking for focus if conv_focus pref is set
164 static void
165 play_conv_event(PurpleConversation *conv, PurpleSoundEventID event)
167 /* If we should not play the sound for some reason, then exit early */
168 if (conv != NULL)
170 FinchConv *gntconv;
171 gboolean has_focus;
173 gntconv = FINCH_CONV(conv);
175 has_focus = purple_conversation_has_focus(conv);
177 if ((gntconv->flags & FINCH_CONV_NO_SOUND) ||
178 (has_focus && !purple_prefs_get_bool(make_pref("/conv_focus"))))
180 return;
184 purple_sound_play_event(event, conv ? purple_conversation_get_account(conv) : NULL);
187 static void
188 buddy_state_cb(PurpleBuddy *buddy, PurpleSoundEventID event)
190 purple_sound_play_event(event, purple_buddy_get_account(buddy));
193 static void
194 im_msg_received_cb(PurpleAccount *account, char *sender,
195 char *message, PurpleConversation *conv,
196 PurpleMessageFlags flags, PurpleSoundEventID event)
198 if (flags & PURPLE_MESSAGE_DELAYED)
199 return;
201 if (conv == NULL) {
202 purple_sound_play_event(PURPLE_SOUND_FIRST_RECEIVE, account);
203 } else {
204 play_conv_event(conv, event);
208 static void
209 im_msg_sent_cb(PurpleAccount *account, PurpleMessage *msg,
210 PurpleSoundEventID event)
212 PurpleIMConversation *im = purple_conversations_find_im_with_account(
213 purple_message_get_recipient(msg), account);
214 play_conv_event(PURPLE_CONVERSATION(im), event);
217 static void
218 chat_user_join_cb(PurpleChatConversation *chat, const char *name,
219 PurpleChatUserFlags flags, gboolean new_arrival,
220 PurpleSoundEventID event)
222 if (new_arrival && !chat_nick_matches_name(chat, name))
223 play_conv_event(PURPLE_CONVERSATION(chat), event);
226 static void
227 chat_user_left_cb(PurpleChatConversation *chat, const char *name,
228 const char *reason, PurpleSoundEventID event)
230 if (!chat_nick_matches_name(chat, name))
231 play_conv_event(PURPLE_CONVERSATION(chat), event);
234 static void
235 chat_msg_sent_cb(PurpleAccount *account, PurpleMessage *msg, int id,
236 PurpleSoundEventID event)
238 PurpleConnection *conn = purple_account_get_connection(account);
239 PurpleChatConversation *chat = NULL;
241 if (conn!=NULL)
242 chat = purple_conversations_find_chat(conn, id);
244 play_conv_event(PURPLE_CONVERSATION(chat), event);
247 static void
248 chat_msg_received_cb(PurpleAccount *account, char *sender,
249 char *message, PurpleChatConversation *chat,
250 PurpleMessageFlags flags, PurpleSoundEventID event)
252 if (flags & PURPLE_MESSAGE_DELAYED)
253 return;
255 g_return_if_fail(chat != NULL);
257 if (purple_chat_conversation_is_ignored_user(chat, sender))
258 return;
260 if (chat_nick_matches_name(chat, sender))
261 return;
263 if (flags & PURPLE_MESSAGE_NICK || purple_utf8_has_word(message, purple_chat_conversation_get_nick(chat)))
264 play_conv_event(PURPLE_CONVERSATION(chat), PURPLE_SOUND_CHAT_NICK);
265 else
266 play_conv_event(PURPLE_CONVERSATION(chat), event);
269 static void
270 got_attention_cb(PurpleAccount *account, const char *who,
271 PurpleConversation *conv, guint type, PurpleSoundEventID event)
273 play_conv_event(conv, event);
278 * We mute sounds for the 10 seconds after you log in so that
279 * you don't get flooded with sounds when the blist shows all
280 * your buddies logging in.
282 static void
283 account_signon_cb(PurpleConnection *gc, gpointer data)
285 if (mute_login_sounds_timeout != 0)
286 g_source_remove(mute_login_sounds_timeout);
287 mute_login_sounds = TRUE;
288 mute_login_sounds_timeout = g_timeout_add_seconds(10, unmute_login_sounds_cb, NULL);
291 static void *
292 finch_sound_get_handle(void)
294 static int handle;
296 return &handle;
300 /* This gets called when the active profile changes */
301 static void
302 initialize_profile(const char *name, PurplePrefType type, gconstpointer val, gpointer null)
304 FinchSoundEvent *event;
305 if (purple_prefs_exists(make_pref("")))
306 return;
308 purple_prefs_add_none(make_pref(""));
309 purple_prefs_add_none(make_pref("/enabled"));
310 purple_prefs_add_none(make_pref("/file"));
312 for (event = sounds; event - sounds < PURPLE_NUM_SOUNDS; event++) {
313 char pref[512];
314 g_snprintf(pref, sizeof(pref), "/enabled/%s", event->pref);
315 purple_prefs_add_bool(make_pref(pref), FALSE);
316 g_snprintf(pref, sizeof(pref), "/file/%s", event->pref);
317 purple_prefs_add_path(make_pref(pref), "");
320 purple_prefs_add_bool(make_pref("/conv_focus"), FALSE);
321 purple_prefs_add_bool(make_pref("/mute"), FALSE);
322 purple_prefs_add_path(make_pref("/command"), "");
323 purple_prefs_add_string(make_pref("/method"), "automatic");
326 static void
327 update_profiles(void)
329 GList *list = finch_sound_get_profiles();
330 for (; list; list = g_list_delete_link(list, list)) {
331 char pname[512];
333 /* got_attention was added in libpurple 2.7.0 */
334 g_snprintf(pname, sizeof(pname), FINCH_PREFS_ROOT "/sound/profiles/%s%s",
335 (char *)list->data, "/enabled/got_attention");
336 purple_prefs_add_bool(pname, FALSE);
337 g_snprintf(pname, sizeof(pname), FINCH_PREFS_ROOT "/sound/profiles/%s%s",
338 (char *)list->data, "/file/got_attention");
339 purple_prefs_add_path(pname, "");
341 g_free(list->data);
345 static void
346 finch_sound_init(void)
348 void *gnt_sound_handle = finch_sound_get_handle();
349 void *blist_handle = purple_blist_get_handle();
350 void *conv_handle = purple_conversations_get_handle();
351 #ifdef USE_GSTREAMER
352 GError *error = NULL;
353 #endif
355 purple_signal_connect(purple_connections_get_handle(), "signed-on",
356 gnt_sound_handle, PURPLE_CALLBACK(account_signon_cb),
357 NULL);
359 purple_prefs_add_none(FINCH_PREFS_ROOT "/sound");
360 purple_prefs_add_string(FINCH_PREFS_ROOT "/sound/actprofile", DEFAULT_PROFILE);
361 purple_prefs_add_none(FINCH_PREFS_ROOT "/sound/profiles");
363 purple_prefs_connect_callback(gnt_sound_handle, FINCH_PREFS_ROOT "/sound/actprofile", initialize_profile, NULL);
364 purple_prefs_trigger_callback(FINCH_PREFS_ROOT "/sound/actprofile");
367 #ifdef USE_GSTREAMER
368 purple_debug_info("sound", "Initializing sound output drivers.\n");
369 #if (GST_VERSION_MAJOR > 0 || \
370 (GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR > 10) || \
371 (GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR == 10 && GST_VERSION_MICRO >= 10))
372 gst_registry_fork_set_enabled(FALSE);
373 #endif
374 if ((gst_init_failed = !gst_init_check(NULL, NULL, &error))) {
375 purple_notify_error(NULL, _("GStreamer Failure"),
376 _("GStreamer failed to initialize."),
377 error ? error->message : "", NULL);
378 if (error) {
379 g_error_free(error);
380 error = NULL;
383 #endif /* USE_GSTREAMER */
385 purple_signal_connect(blist_handle, "buddy-signed-on",
386 gnt_sound_handle, PURPLE_CALLBACK(buddy_state_cb),
387 GINT_TO_POINTER(PURPLE_SOUND_BUDDY_ARRIVE));
388 purple_signal_connect(blist_handle, "buddy-signed-off",
389 gnt_sound_handle, PURPLE_CALLBACK(buddy_state_cb),
390 GINT_TO_POINTER(PURPLE_SOUND_BUDDY_LEAVE));
391 purple_signal_connect(conv_handle, "received-im-msg",
392 gnt_sound_handle, PURPLE_CALLBACK(im_msg_received_cb),
393 GINT_TO_POINTER(PURPLE_SOUND_RECEIVE));
394 purple_signal_connect(conv_handle, "sent-im-msg",
395 gnt_sound_handle, PURPLE_CALLBACK(im_msg_sent_cb),
396 GINT_TO_POINTER(PURPLE_SOUND_SEND));
397 purple_signal_connect(conv_handle, "chat-user-joined",
398 gnt_sound_handle, PURPLE_CALLBACK(chat_user_join_cb),
399 GINT_TO_POINTER(PURPLE_SOUND_CHAT_JOIN));
400 purple_signal_connect(conv_handle, "chat-user-left",
401 gnt_sound_handle, PURPLE_CALLBACK(chat_user_left_cb),
402 GINT_TO_POINTER(PURPLE_SOUND_CHAT_LEAVE));
403 purple_signal_connect(conv_handle, "sent-chat-msg",
404 gnt_sound_handle, PURPLE_CALLBACK(chat_msg_sent_cb),
405 GINT_TO_POINTER(PURPLE_SOUND_CHAT_YOU_SAY));
406 purple_signal_connect(conv_handle, "received-chat-msg",
407 gnt_sound_handle, PURPLE_CALLBACK(chat_msg_received_cb),
408 GINT_TO_POINTER(PURPLE_SOUND_CHAT_SAY));
409 purple_signal_connect(conv_handle, "got-attention",
410 gnt_sound_handle, PURPLE_CALLBACK(got_attention_cb),
411 GINT_TO_POINTER(PURPLE_SOUND_GOT_ATTENTION));
413 update_profiles();
416 static void
417 finch_sound_uninit(void)
419 #ifdef USE_GSTREAMER
420 if (!gst_init_failed)
421 gst_deinit();
422 #endif
424 purple_signals_disconnect_by_handle(finch_sound_get_handle());
427 #if defined(USE_GSTREAMER) && !defined(_WIN32)
428 static gboolean
429 bus_call (GstBus *bus, GstMessage *msg, gpointer data)
431 GstElement *play = data;
432 GError *err = NULL;
434 switch (GST_MESSAGE_TYPE (msg)) {
435 case GST_MESSAGE_ERROR:
436 gst_message_parse_error(msg, &err, NULL);
437 purple_debug_error("gstreamer", "%s\n", err->message);
438 g_error_free(err);
439 /* fall-through and clean up */
440 case GST_MESSAGE_EOS:
441 gst_element_set_state(play, GST_STATE_NULL);
442 gst_object_unref(GST_OBJECT(play));
443 return FALSE;
444 break;
445 case GST_MESSAGE_WARNING:
446 gst_message_parse_warning(msg, &err, NULL);
447 purple_debug_warning("gstreamer", "%s\n", err->message);
448 g_error_free(err);
449 break;
450 default:
451 break;
453 return TRUE;
455 #endif
457 static void
458 finch_sound_play_file(const char *filename)
460 const char *method;
461 #if defined(USE_GSTREAMER) && !defined(_WIN32)
462 char *uri;
463 GstElement *sink = NULL;
464 GstElement *play = NULL;
465 GstBus *bus = NULL;
466 #endif
467 if (purple_prefs_get_bool(make_pref("/mute")))
468 return;
470 method = purple_prefs_get_string(make_pref("/method"));
472 if (purple_strequal(method, "nosound")) {
473 return;
474 } else if (purple_strequal(method, "beep")) {
475 beep();
476 return;
479 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
480 purple_debug_error("gntsound", "sound file (%s) does not exist.\n", filename);
481 return;
484 #ifndef _WIN32
485 if (purple_strequal(method, "custom")) {
486 const char *sound_cmd;
487 char *command;
488 char *esc_filename;
489 GError *error = NULL;
491 sound_cmd = purple_prefs_get_path(make_pref("/command"));
493 if (!sound_cmd || *sound_cmd == '\0') {
494 purple_debug_error("gntsound",
495 "'Command' sound method has been chosen, "
496 "but no command has been set.");
497 return;
500 esc_filename = g_shell_quote(filename);
502 if (strstr(sound_cmd, "%s"))
503 command = purple_strreplace(sound_cmd, "%s", esc_filename);
504 else
505 command = g_strdup_printf("%s %s", sound_cmd, esc_filename);
507 if (!g_spawn_command_line_async(command, &error)) {
508 purple_debug_error("gntsound", "sound command could not be launched: %s\n", error->message);
509 g_error_free(error);
512 g_free(esc_filename);
513 g_free(command);
514 return;
516 #ifdef USE_GSTREAMER
517 if (gst_init_failed) /* Perhaps do beep instead? */
518 return;
519 if (purple_strequal(method, "automatic")) {
520 if (purple_running_gnome()) {
521 sink = gst_element_factory_make("gconfaudiosink", "sink");
523 if (!sink)
524 sink = gst_element_factory_make("autoaudiosink", "sink");
525 if (!sink) {
526 purple_debug_error("sound", "Unable to create GStreamer audiosink.\n");
527 return;
529 } else if (purple_strequal(method, "esd")) {
530 sink = gst_element_factory_make("esdsink", "sink");
531 if (!sink) {
532 purple_debug_error("sound", "Unable to create GStreamer audiosink.\n");
533 return;
535 } else if (purple_strequal(method, "alsa")) {
536 sink = gst_element_factory_make("alsasink", "sink");
537 if (!sink) {
538 purple_debug_error("sound", "Unable to create GStreamer audiosink.\n");
539 return;
541 } else {
542 purple_debug_error("sound", "Unknown sound method '%s'\n", method);
543 return;
546 play = gst_element_factory_make("playbin", "play");
548 if (play == NULL) {
549 return;
552 uri = g_strdup_printf("file://%s", filename);
554 g_object_set(G_OBJECT(play), "uri", uri,
555 "audio-sink", sink, NULL);
557 bus = gst_pipeline_get_bus(GST_PIPELINE(play));
558 gst_bus_add_watch(bus, bus_call, play);
560 gst_element_set_state(play, GST_STATE_PLAYING);
562 gst_object_unref(bus);
563 g_free(uri);
565 #else /* USE_GSTREAMER */
566 beep();
567 return;
568 #endif /* USE_GSTREAMER */
569 #else /* _WIN32 */
570 purple_debug_info("sound", "Playing %s\n", filename);
573 wchar_t *wc_filename = g_utf8_to_utf16(filename,
574 -1, NULL, NULL, NULL);
575 if (!PlaySoundW(wc_filename, NULL, SND_ASYNC | SND_FILENAME))
576 purple_debug(PURPLE_DEBUG_ERROR, "sound", "Error playing sound.\n");
577 g_free(wc_filename);
579 #endif /* _WIN32 */
582 static void
583 finch_sound_play_event(PurpleSoundEventID event)
585 char *enable_pref;
586 char *file_pref;
587 if ((event == PURPLE_SOUND_BUDDY_ARRIVE) && mute_login_sounds)
588 return;
590 if (event >= PURPLE_NUM_SOUNDS ||
591 event >= G_N_ELEMENTS(sounds)) {
592 purple_debug_error("sound", "got request for unknown sound: %d\n", event);
593 return;
596 enable_pref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/enabled/%s",
597 finch_sound_get_active_profile(),
598 sounds[event].pref);
599 file_pref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/file/%s", finch_sound_get_active_profile(), sounds[event].pref);
601 /* check NULL for sounds that don't have an option, ie buddy pounce */
602 if (purple_prefs_get_bool(enable_pref)) {
603 char *filename = g_strdup(purple_prefs_get_path(file_pref));
604 if (!filename || *filename == '\0') {
605 g_free(filename);
606 filename = g_build_filename(PURPLE_DATADIR,
607 "sounds", "purple", sounds[event].def, NULL);
610 purple_sound_play_file(filename, NULL);
611 g_free(filename);
614 g_free(enable_pref);
615 g_free(file_pref);
618 GList *
619 finch_sound_get_profiles()
621 GList *list = NULL, *iter;
622 iter = purple_prefs_get_children_names(FINCH_PREFS_ROOT "/sound/profiles");
623 while (iter) {
624 list = g_list_append(list, g_strdup(strrchr(iter->data, '/') + 1));
625 g_free(iter->data);
626 iter = g_list_delete_link(iter, iter);
628 return list;
631 /* This will also create it if it doesn't exist */
632 void
633 finch_sound_set_active_profile(const char *name)
635 purple_prefs_set_string(FINCH_PREFS_ROOT "/sound/actprofile", name);
638 static gboolean
639 finch_sound_profile_exists(const char *name)
641 gchar * tmp;
642 gboolean ret = purple_prefs_exists(tmp = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s", name));
643 g_free(tmp);
644 return ret;
647 static void
648 save_cb(GntWidget *button, gpointer win)
650 GList * itr;
652 purple_prefs_set_string(make_pref("/method"), gnt_combo_box_get_selected_data(GNT_COMBO_BOX(pref_dialog->method)));
653 purple_prefs_set_path(make_pref("/command"), gnt_entry_get_text(GNT_ENTRY(pref_dialog->command)));
654 purple_prefs_set_bool(make_pref("/conv_focus"), gnt_check_box_get_checked(GNT_CHECK_BOX(pref_dialog->conv_focus)));
655 purple_prefs_set_int("/purple/sound/while_status", GPOINTER_TO_INT(gnt_combo_box_get_selected_data(GNT_COMBO_BOX(pref_dialog->while_status))));
657 for (itr = gnt_tree_get_rows(GNT_TREE(pref_dialog->events)); itr; itr = itr->next) {
658 FinchSoundEvent * event = &sounds[GPOINTER_TO_INT(itr->data)];
659 char * filepref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/file/%s", finch_sound_get_active_profile(), event->pref);
660 char * boolpref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/enabled/%s", finch_sound_get_active_profile(), event->pref);
661 purple_prefs_set_bool(boolpref, gnt_tree_get_choice(GNT_TREE(pref_dialog->events), itr->data));
662 purple_prefs_set_path(filepref, event->file ? event->file : "");
663 g_free(filepref);
664 g_free(boolpref);
666 gnt_widget_destroy(GNT_WIDGET(win));
669 static void
670 file_cb(GntFileSel *w, const char *path, const char *file, gpointer data)
672 FinchSoundEvent *event = data;
674 g_free(event->file);
675 event->file = g_strdup(path);
677 gnt_tree_change_text(GNT_TREE(pref_dialog->events), GINT_TO_POINTER(event->id), 1, file);
678 gnt_tree_set_choice(GNT_TREE(pref_dialog->events), GINT_TO_POINTER(event->id), TRUE);
680 gnt_widget_destroy(GNT_WIDGET(w));
683 static void
684 test_cb(GntWidget *button, gpointer null)
686 PurpleSoundEventID id = GPOINTER_TO_INT(gnt_tree_get_selection_data(GNT_TREE(pref_dialog->events)));
687 FinchSoundEvent * event = &sounds[id];
688 char *enabled, *file, *tmpfile;
689 gboolean temp_value;
691 enabled = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/enabled/%s",
692 finch_sound_get_active_profile(), event->pref);
693 file = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/file/%s",
694 finch_sound_get_active_profile(), event->pref);
696 temp_value = purple_prefs_get_bool(enabled);
697 tmpfile = g_strdup(purple_prefs_get_path(file));
699 purple_prefs_set_path(file, event->file);
700 if (!temp_value) purple_prefs_set_bool(enabled, TRUE);
702 purple_sound_play_event(id, NULL);
704 if (!temp_value) purple_prefs_set_bool(enabled, FALSE);
705 purple_prefs_set_path(file, tmpfile);
707 g_free(enabled);
708 g_free(file);
709 g_free(tmpfile);
712 static void
713 reset_cb(GntWidget *button, gpointer null)
715 /* Don't dereference this pointer ! */
716 gpointer key = gnt_tree_get_selection_data(GNT_TREE(pref_dialog->events));
718 FinchSoundEvent * event = &sounds[GPOINTER_TO_INT(key)];
719 g_free(event->file);
720 event->file = NULL;
721 gnt_tree_change_text(GNT_TREE(pref_dialog->events), key, 1, _("(default)"));
725 static void
726 choose_cb(GntWidget *button, gpointer null)
728 GntWidget *w = gnt_file_sel_new();
729 GntFileSel *sel = GNT_FILE_SEL(w);
730 PurpleSoundEventID id = GPOINTER_TO_INT(gnt_tree_get_selection_data(GNT_TREE(pref_dialog->events)));
731 FinchSoundEvent * event = &sounds[id];
732 char *path = NULL;
734 gnt_box_set_title(GNT_BOX(w), _("Select Sound File..."));
735 gnt_file_sel_set_current_location(sel,
736 (event && event->file) ? (path = g_path_get_dirname(event->file))
737 : purple_home_dir());
739 g_signal_connect(G_OBJECT(sel), "cancelled", G_CALLBACK(gnt_widget_destroy), NULL);
740 g_signal_connect(G_OBJECT(sel), "file_selected", G_CALLBACK(file_cb), event);
741 g_signal_connect_swapped(G_OBJECT(sel), "destroy", G_CALLBACK(g_nullify_pointer), &pref_dialog->selector);
743 /* If there's an already open file-selector, close that one. */
744 if (pref_dialog->selector)
745 gnt_widget_destroy(pref_dialog->selector);
747 pref_dialog->selector = w;
749 gnt_widget_show(w);
750 g_free(path);
753 static void
754 release_pref_dialog(GntBindable *data, gpointer null)
756 GList * itr;
757 for (itr = gnt_tree_get_rows(GNT_TREE(pref_dialog->events)); itr; itr = itr->next) {
758 PurpleSoundEventID id = GPOINTER_TO_INT(itr->data);
759 FinchSoundEvent * e = &sounds[id];
760 g_free(e->file);
761 e->file = NULL;
763 if (pref_dialog->selector)
764 gnt_widget_destroy(pref_dialog->selector);
765 g_free(pref_dialog->original_profile);
766 g_free(pref_dialog);
767 pref_dialog = NULL;
770 static void
771 load_pref_window(const char * profile)
773 gint i;
775 finch_sound_set_active_profile(profile);
777 gnt_combo_box_set_selected(GNT_COMBO_BOX(pref_dialog->method), (gchar *)purple_prefs_get_string(make_pref("/method")));
779 gnt_entry_set_text(GNT_ENTRY(pref_dialog->command), purple_prefs_get_path(make_pref("/command")));
781 gnt_check_box_set_checked(GNT_CHECK_BOX(pref_dialog->conv_focus), purple_prefs_get_bool(make_pref("/conv_focus")));
783 gnt_combo_box_set_selected(GNT_COMBO_BOX(pref_dialog->while_status), GINT_TO_POINTER(purple_prefs_get_int("/purple" "/sound/while_status")));
785 for (i = 0; i < PURPLE_NUM_SOUNDS; i++) {
786 FinchSoundEvent * event = &sounds[i];
787 gchar *boolpref;
788 gchar *filepref, *basename = NULL;
789 const char * profile = finch_sound_get_active_profile();
791 filepref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/file/%s", profile, event->pref);
793 g_free(event->file);
794 event->file = g_strdup(purple_prefs_get_path(filepref));
796 g_free(filepref);
797 if (event->label == NULL) {
798 continue;
801 boolpref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/enabled/%s", profile, event->pref);
803 gnt_tree_change_text(GNT_TREE(pref_dialog->events), GINT_TO_POINTER(i), 0, event->label);
804 gnt_tree_change_text(GNT_TREE(pref_dialog->events), GINT_TO_POINTER(i), 1,
805 event->file[0] ? (basename = g_path_get_basename(event->file)) : _("(default)"));
806 g_free(basename);
808 gnt_tree_set_choice(GNT_TREE(pref_dialog->events), GINT_TO_POINTER(i), purple_prefs_get_bool(boolpref));
810 g_free(boolpref);
813 gnt_tree_set_selected(GNT_TREE(pref_dialog->profiles), (gchar *)finch_sound_get_active_profile());
815 gnt_widget_draw(pref_dialog->window);
818 static void
819 reload_pref_window(const char *profile)
821 if (purple_strequal(profile, finch_sound_get_active_profile()))
822 return;
823 load_pref_window(profile);
826 static void
827 prof_del_cb(GntWidget *button, gpointer null)
829 const char * profile = gnt_entry_get_text(GNT_ENTRY(pref_dialog->new_profile));
830 gchar * pref;
832 if (purple_strequal(profile, DEFAULT_PROFILE))
833 return;
835 pref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s", profile);
836 purple_prefs_remove(pref);
837 g_free(pref);
839 if (purple_strequal(pref_dialog->original_profile, profile)) {
840 g_free(pref_dialog->original_profile);
841 pref_dialog->original_profile = g_strdup(DEFAULT_PROFILE);
844 if(purple_strequal(profile, finch_sound_get_active_profile()))
845 reload_pref_window(DEFAULT_PROFILE);
847 gnt_tree_remove(GNT_TREE(pref_dialog->profiles), (gchar *) profile);
850 static void
851 prof_add_cb(GntButton *button, GntEntry * entry)
853 const char * profile = gnt_entry_get_text(entry);
854 GntTreeRow * row;
855 if (!finch_sound_profile_exists(profile)) {
856 gpointer key = g_strdup(profile);
857 row = gnt_tree_create_row(GNT_TREE(pref_dialog->profiles), profile);
858 gnt_tree_add_row_after(GNT_TREE(pref_dialog->profiles), key,
859 row,
860 NULL, NULL);
861 gnt_entry_set_text(entry, "");
862 gnt_tree_set_selected(GNT_TREE(pref_dialog->profiles), key);
863 finch_sound_set_active_profile(key);
864 } else
865 reload_pref_window(profile);
868 static void
869 prof_load_cb(GntTree *tree, gpointer oldkey, gpointer newkey, gpointer null)
871 reload_pref_window(newkey);
874 static void
875 cancel_cb(GntButton *button, gpointer win)
877 finch_sound_set_active_profile(pref_dialog->original_profile);
878 gnt_widget_destroy(GNT_WIDGET(win));
881 void
882 finch_sounds_show_all(void)
884 GntWidget *box, *tmpbox, *splitbox, *cmbox;
885 GntWidget *entry;
886 GntWidget *chkbox;
887 GntWidget *button;
888 GntWidget *label;
889 GntWidget *tree;
890 GntWidget *win;
892 gint i;
893 GList *itr, *list;
895 if (pref_dialog) {
896 gnt_window_present(pref_dialog->window);
897 return;
900 pref_dialog = g_new0(SoundPrefDialog, 1);
902 pref_dialog->original_profile = g_strdup(finch_sound_get_active_profile());
904 pref_dialog->window = win = gnt_window_box_new(FALSE, TRUE);
905 gnt_box_set_pad(GNT_BOX(win), 0);
906 gnt_box_set_toplevel(GNT_BOX(win), TRUE);
907 gnt_box_set_title(GNT_BOX(win), _("Sound Preferences"));
908 gnt_box_set_fill(GNT_BOX(win), TRUE);
909 gnt_box_set_alignment(GNT_BOX(win), GNT_ALIGN_MID);
911 /* Profiles */
912 splitbox = gnt_hbox_new(FALSE);
913 gnt_box_set_pad(GNT_BOX(splitbox), 0);
914 gnt_box_set_alignment(GNT_BOX(splitbox), GNT_ALIGN_TOP);
916 box = gnt_vbox_new(FALSE);
917 gnt_box_set_pad(GNT_BOX(box), 0);
918 gnt_box_add_widget(GNT_BOX(box), gnt_label_new_with_format(_("Profiles"), GNT_TEXT_FLAG_BOLD));
919 pref_dialog->profiles = tree = gnt_tree_new();
920 gnt_tree_set_hash_fns(GNT_TREE(tree), g_str_hash, g_str_equal, g_free);
921 gnt_tree_set_compare_func(GNT_TREE(tree), (GCompareFunc)g_ascii_strcasecmp);
922 g_signal_connect(G_OBJECT(tree), "selection-changed", G_CALLBACK(prof_load_cb), NULL);
924 itr = list = finch_sound_get_profiles();
925 for (; itr; itr = itr->next) {
926 /* Do not free itr->data. It's the stored as a key for the tree, and will
927 * be freed when the tree is destroyed. */
928 gnt_tree_add_row_after(GNT_TREE(tree), itr->data,
929 gnt_tree_create_row(GNT_TREE(tree), itr->data), NULL, NULL);
931 g_list_free(list);
933 gnt_box_add_widget(GNT_BOX(box), tree);
935 pref_dialog->new_profile = entry = gnt_entry_new("");
936 gnt_box_add_widget(GNT_BOX(box), entry);
938 tmpbox = gnt_hbox_new(FALSE);
939 button = gnt_button_new("Add");
940 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(prof_add_cb), entry);
941 gnt_box_add_widget(GNT_BOX(tmpbox), button);
942 button = gnt_button_new("Delete");
943 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(prof_del_cb), NULL);
944 gnt_box_add_widget(GNT_BOX(tmpbox), button);
945 gnt_box_add_widget(GNT_BOX(box), tmpbox);
946 gnt_box_add_widget(GNT_BOX(splitbox), box);
948 gnt_box_add_widget(GNT_BOX(splitbox), gnt_vline_new());
950 /* Sound method */
952 box = gnt_vbox_new(FALSE);
953 gnt_box_set_pad(GNT_BOX(box), 0);
955 pref_dialog->method = cmbox = gnt_combo_box_new();
956 gnt_tree_set_hash_fns(GNT_TREE(gnt_combo_box_get_dropdown(GNT_COMBO_BOX(cmbox))),
957 g_str_hash, g_str_equal, NULL);
958 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "automatic", _("Automatic"));
959 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "alsa", "ALSA");
960 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "esd", "ESD");
961 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "beep", _("Console Beep"));
962 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "custom", _("Command"));
963 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "nosound", _("No Sound"));
965 label = gnt_label_new_with_format(_("Sound Method"), GNT_TEXT_FLAG_BOLD);
966 gnt_box_add_widget(GNT_BOX(box), label);
967 tmpbox = gnt_hbox_new(TRUE);
968 gnt_box_set_fill(GNT_BOX(tmpbox), FALSE);
969 gnt_box_set_pad(GNT_BOX(tmpbox), 0);
970 gnt_box_add_widget(GNT_BOX(tmpbox), gnt_label_new(_("Method: ")));
971 gnt_box_add_widget(GNT_BOX(tmpbox), cmbox);
972 gnt_box_add_widget(GNT_BOX(box), tmpbox);
974 tmpbox = gnt_hbox_new(TRUE);
975 gnt_box_set_pad(GNT_BOX(tmpbox), 0);
976 gnt_box_set_fill(GNT_BOX(tmpbox), FALSE);
977 gnt_box_add_widget(GNT_BOX(tmpbox), gnt_label_new(_("Sound Command\n(%s for filename)")));
978 pref_dialog->command = entry = gnt_entry_new("");
979 gnt_box_add_widget(GNT_BOX(tmpbox), entry);
980 gnt_box_add_widget(GNT_BOX(box), tmpbox);
982 gnt_box_add_widget(GNT_BOX(box), gnt_line_new(FALSE));
984 /* Sound options */
985 gnt_box_add_widget(GNT_BOX(box), gnt_label_new_with_format(_("Sound Options"), GNT_TEXT_FLAG_BOLD));
986 pref_dialog->conv_focus = chkbox = gnt_check_box_new(_("Sounds when conversation has focus"));
987 gnt_box_add_widget(GNT_BOX(box), chkbox);
989 tmpbox = gnt_hbox_new(TRUE);
990 gnt_box_set_pad(GNT_BOX(tmpbox), 0);
991 gnt_box_set_fill(GNT_BOX(tmpbox), FALSE);
992 gnt_box_add_widget(GNT_BOX(tmpbox), gnt_label_new("Enable Sounds:"));
993 pref_dialog->while_status = cmbox = gnt_combo_box_new();
994 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), GINT_TO_POINTER(3), _("Always"));
995 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), GINT_TO_POINTER(1), _("Only when available"));
996 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), GINT_TO_POINTER(2), _("Only when not available"));
997 gnt_box_add_widget(GNT_BOX(tmpbox), cmbox);
998 gnt_box_add_widget(GNT_BOX(box), tmpbox);
1000 gnt_box_add_widget(GNT_BOX(splitbox), box);
1002 gnt_box_add_widget(GNT_BOX(win), splitbox);
1004 gnt_box_add_widget(GNT_BOX(win), gnt_hline_new());
1006 /* Sound events */
1007 gnt_box_add_widget(GNT_BOX(win), gnt_label_new_with_format(_("Sound Events"), GNT_TEXT_FLAG_BOLD));
1008 pref_dialog->events = tree = gnt_tree_new_with_columns(2);
1009 gnt_tree_set_column_titles(GNT_TREE(tree), _("Event"), _("File"));
1010 gnt_tree_set_show_title(GNT_TREE(tree), TRUE);
1012 for (i = 0; i < PURPLE_NUM_SOUNDS; i++) {
1013 FinchSoundEvent * event = &sounds[i];
1015 if (event->label == NULL) {
1016 continue;
1019 gnt_tree_add_choice(GNT_TREE(tree), GINT_TO_POINTER(i),
1020 gnt_tree_create_row(GNT_TREE(tree), event->label, event->def),
1021 NULL, NULL);
1024 gnt_tree_adjust_columns(GNT_TREE(tree));
1025 gnt_box_add_widget(GNT_BOX(win), tree);
1027 box = gnt_hbox_new(FALSE);
1028 button = gnt_button_new(_("Test"));
1029 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(test_cb), NULL);
1030 gnt_box_add_widget(GNT_BOX(box), button);
1031 button = gnt_button_new(_("Reset"));
1032 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(reset_cb), NULL);
1033 gnt_box_add_widget(GNT_BOX(box), button);
1034 button = gnt_button_new(_("Choose..."));
1035 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(choose_cb), NULL);
1036 gnt_box_add_widget(GNT_BOX(box), button);
1037 gnt_box_add_widget(GNT_BOX(win), box);
1039 gnt_box_add_widget(GNT_BOX(win), gnt_line_new(FALSE));
1041 /* Add new stuff before this */
1042 box = gnt_hbox_new(FALSE);
1043 gnt_box_set_fill(GNT_BOX(box), TRUE);
1044 button = gnt_button_new(_("Save"));
1045 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(save_cb), win);
1046 gnt_box_add_widget(GNT_BOX(box), button);
1047 button = gnt_button_new(_("Cancel"));
1048 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(cancel_cb), win);
1049 gnt_box_add_widget(GNT_BOX(box), button);
1050 gnt_box_add_widget(GNT_BOX(win), box);
1052 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(release_pref_dialog), NULL);
1054 load_pref_window(finch_sound_get_active_profile());
1056 gnt_widget_show(win);
1059 gboolean finch_sound_is_enabled(void)
1061 const char *pref = make_pref("/method");
1062 const char *method = purple_prefs_get_string(pref);
1064 if (!method)
1065 return FALSE;
1066 if (purple_strequal(method, "nosound"))
1067 return FALSE;
1069 return TRUE;
1072 static PurpleSoundUiOps sound_ui_ops =
1074 finch_sound_init,
1075 finch_sound_uninit,
1076 finch_sound_play_file,
1077 finch_sound_play_event,
1078 NULL,
1079 NULL,
1080 NULL,
1081 NULL
1084 PurpleSoundUiOps *
1085 finch_sound_get_ui_ops(void)
1087 return &sound_ui_ops;