Minor optimization
[pidgin-git.git] / finch / gntsound.c
blob296fee1ef31d729b87c99dc783e38493eb6e03c9
1 /**
2 * @file gntsound.c GNT Sound API
3 * @ingroup finch
4 */
6 /* finch
8 * Finch is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "finch.h"
27 #include <internal.h>
29 #ifdef _WIN32
30 #include <windows.h>
31 #include <mmsystem.h>
32 #endif
34 #ifdef USE_GSTREAMER
35 #include <gst/gst.h>
36 #endif /* USE_GSTREAMER */
38 #include "debug.h"
39 #include "notify.h"
40 #include "prefs.h"
41 #include "sound.h"
42 #include "util.h"
44 #include "gntconv.h"
46 #include "gntbox.h"
47 #include "gntwindow.h"
48 #include "gntcombobox.h"
49 #include "gntlabel.h"
50 #include "gntconv.h"
51 #include "gntsound.h"
52 #include "gntwidget.h"
53 #include "gntentry.h"
54 #include "gntcheckbox.h"
55 #include "gntline.h"
56 #include "gntslider.h"
57 #include "gnttree.h"
58 #include "gntfilesel.h"
60 typedef struct {
61 PurpleSoundEventID id;
62 char *label;
63 char *pref;
64 char *def;
65 char *file;
66 } FinchSoundEvent;
68 typedef struct {
69 GntWidget *method;
70 GntWidget *command;
71 GntWidget *conv_focus;
72 GntWidget *while_status;
73 GntWidget *volume;
74 GntWidget *events;
75 GntWidget *window;
76 GntWidget *selector;
78 GntWidget *profiles;
79 GntWidget *new_profile;
80 gchar * original_profile;
81 } SoundPrefDialog;
83 #define DEFAULT_PROFILE "default"
85 static SoundPrefDialog *pref_dialog;
87 #define PLAY_SOUND_TIMEOUT 15000
89 static guint mute_login_sounds_timeout = 0;
90 static gboolean mute_login_sounds = FALSE;
92 #ifdef USE_GSTREAMER
93 static gboolean gst_init_failed;
94 #endif /* USE_GSTREAMER */
96 static FinchSoundEvent sounds[PURPLE_NUM_SOUNDS] = {
97 {PURPLE_SOUND_BUDDY_ARRIVE, N_("Buddy logs in"), "login", "login.wav", NULL},
98 {PURPLE_SOUND_BUDDY_LEAVE, N_("Buddy logs out"), "logout", "logout.wav", NULL},
99 {PURPLE_SOUND_RECEIVE, N_("Message received"), "im_recv", "receive.wav", NULL},
100 {PURPLE_SOUND_FIRST_RECEIVE, N_("Message received begins conversation"), "first_im_recv", "receive.wav", NULL},
101 {PURPLE_SOUND_SEND, N_("Message sent"), "send_im", "send.wav", NULL},
102 {PURPLE_SOUND_CHAT_JOIN, N_("Person enters chat"), "join_chat", "login.wav", NULL},
103 {PURPLE_SOUND_CHAT_LEAVE, N_("Person leaves chat"), "left_chat", "logout.wav", NULL},
104 {PURPLE_SOUND_CHAT_YOU_SAY, N_("You talk in chat"), "send_chat_msg", "send.wav", NULL},
105 {PURPLE_SOUND_CHAT_SAY, N_("Others talk in chat"), "chat_msg_recv", "receive.wav", NULL},
106 {PURPLE_SOUND_POUNCE_DEFAULT, NULL, "pounce_default", "alert.wav", NULL},
107 {PURPLE_SOUND_CHAT_NICK, N_("Someone says your username in chat"), "nick_said", "alert.wav", NULL},
108 {PURPLE_SOUND_GOT_ATTENTION, N_("Attention received"), "got_attention", "alert.wav", NULL}
111 const char *
112 finch_sound_get_active_profile()
114 return purple_prefs_get_string(FINCH_PREFS_ROOT "/sound/actprofile");
117 /* This method creates a pref name based on the current active profile.
118 * So if "Home" is the current active profile the pref name
119 * [FINCH_PREFS_ROOT "/sound/profiles/Home/$NAME"] is created.
121 static gchar *
122 make_pref(const char *name)
124 static char pref_string[512];
125 g_snprintf(pref_string, sizeof(pref_string),
126 FINCH_PREFS_ROOT "/sound/profiles/%s%s", finch_sound_get_active_profile(), name);
127 return pref_string;
131 static gboolean
132 unmute_login_sounds_cb(gpointer data)
134 mute_login_sounds = FALSE;
135 mute_login_sounds_timeout = 0;
136 return FALSE;
139 static gboolean
140 chat_nick_matches_name(PurpleConversation *conv, const char *aname)
142 PurpleConvChat *chat = NULL;
143 char *nick = NULL;
144 char *name = NULL;
145 gboolean ret = FALSE;
146 PurpleAccount *account;
148 chat = purple_conversation_get_chat_data(conv);
149 if (chat == NULL)
150 return ret;
152 account = purple_conversation_get_account(conv);
153 nick = g_strdup(purple_normalize(account, chat->nick));
154 name = g_strdup(purple_normalize(account, aname));
156 if (g_utf8_collate(nick, name) == 0)
157 ret = TRUE;
159 g_free(nick);
160 g_free(name);
162 return ret;
166 * play a sound event for a conversation, honoring make_sound flag
167 * of conversation and checking for focus if conv_focus pref is set
169 static void
170 play_conv_event(PurpleConversation *conv, PurpleSoundEventID event)
172 /* If we should not play the sound for some reason, then exit early */
173 if (conv != NULL)
175 FinchConv *gntconv;
176 gboolean has_focus;
178 gntconv = FINCH_CONV(conv);
180 has_focus = purple_conversation_has_focus(conv);
182 if ((gntconv->flags & FINCH_CONV_NO_SOUND) ||
183 (has_focus && !purple_prefs_get_bool(make_pref("/conv_focus"))))
185 return;
189 purple_sound_play_event(event, conv ? purple_conversation_get_account(conv) : NULL);
192 static void
193 buddy_state_cb(PurpleBuddy *buddy, PurpleSoundEventID event)
195 purple_sound_play_event(event, purple_buddy_get_account(buddy));
198 static void
199 im_msg_received_cb(PurpleAccount *account, char *sender,
200 char *message, PurpleConversation *conv,
201 PurpleMessageFlags flags, PurpleSoundEventID event)
203 if (flags & PURPLE_MESSAGE_DELAYED)
204 return;
206 if (conv == NULL) {
207 purple_sound_play_event(PURPLE_SOUND_FIRST_RECEIVE, account);
208 } else {
209 play_conv_event(conv, event);
213 static void
214 im_msg_sent_cb(PurpleAccount *account, const char *receiver,
215 const char *message, PurpleSoundEventID event)
217 PurpleConversation *conv = purple_find_conversation_with_account(
218 PURPLE_CONV_TYPE_IM, receiver, account);
219 play_conv_event(conv, event);
222 static void
223 chat_buddy_join_cb(PurpleConversation *conv, const char *name,
224 PurpleConvChatBuddyFlags flags, gboolean new_arrival,
225 PurpleSoundEventID event)
227 if (new_arrival && !chat_nick_matches_name(conv, name))
228 play_conv_event(conv, event);
231 static void
232 chat_buddy_left_cb(PurpleConversation *conv, const char *name,
233 const char *reason, PurpleSoundEventID event)
235 if (!chat_nick_matches_name(conv, name))
236 play_conv_event(conv, event);
239 static void
240 chat_msg_sent_cb(PurpleAccount *account, const char *message,
241 int id, PurpleSoundEventID event)
243 PurpleConnection *conn = purple_account_get_connection(account);
244 PurpleConversation *conv = NULL;
246 if (conn!=NULL)
247 conv = purple_find_chat(conn, id);
249 play_conv_event(conv, event);
252 static void
253 chat_msg_received_cb(PurpleAccount *account, char *sender,
254 char *message, PurpleConversation *conv,
255 PurpleMessageFlags flags, PurpleSoundEventID event)
257 PurpleConvChat *chat;
259 if (flags & PURPLE_MESSAGE_DELAYED)
260 return;
262 chat = purple_conversation_get_chat_data(conv);
263 g_return_if_fail(chat != NULL);
265 if (purple_conv_chat_is_user_ignored(chat, sender))
266 return;
268 if (chat_nick_matches_name(conv, sender))
269 return;
271 if (flags & PURPLE_MESSAGE_NICK || purple_utf8_has_word(message, chat->nick))
272 play_conv_event(conv, PURPLE_SOUND_CHAT_NICK);
273 else
274 play_conv_event(conv, event);
277 static void
278 got_attention_cb(PurpleAccount *account, const char *who,
279 PurpleConversation *conv, guint type, PurpleSoundEventID event)
281 play_conv_event(conv, event);
286 * We mute sounds for the 10 seconds after you log in so that
287 * you don't get flooded with sounds when the blist shows all
288 * your buddies logging in.
290 static void
291 account_signon_cb(PurpleConnection *gc, gpointer data)
293 if (mute_login_sounds_timeout != 0)
294 g_source_remove(mute_login_sounds_timeout);
295 mute_login_sounds = TRUE;
296 mute_login_sounds_timeout = purple_timeout_add_seconds(10, unmute_login_sounds_cb, NULL);
299 static void *
300 finch_sound_get_handle(void)
302 static int handle;
304 return &handle;
308 /* This gets called when the active profile changes */
309 static void
310 initialize_profile(const char *name, PurplePrefType type, gconstpointer val, gpointer null)
312 FinchSoundEvent *event;
313 if (purple_prefs_exists(make_pref("")))
314 return;
316 purple_prefs_add_none(make_pref(""));
317 purple_prefs_add_none(make_pref("/enabled"));
318 purple_prefs_add_none(make_pref("/file"));
320 for (event = sounds; event - sounds < PURPLE_NUM_SOUNDS; event++) {
321 char pref[512];
322 g_snprintf(pref, sizeof(pref), "/enabled/%s", event->pref);
323 purple_prefs_add_bool(make_pref(pref), FALSE);
324 g_snprintf(pref, sizeof(pref), "/file/%s", event->pref);
325 purple_prefs_add_path(make_pref(pref), "");
328 purple_prefs_add_bool(make_pref("/conv_focus"), FALSE);
329 purple_prefs_add_bool(make_pref("/mute"), FALSE);
330 purple_prefs_add_path(make_pref("/command"), "");
331 purple_prefs_add_string(make_pref("/method"), "automatic");
332 purple_prefs_add_int(make_pref("/volume"), 50);
335 static void
336 update_profiles(void)
338 GList *list = finch_sound_get_profiles();
339 for (; list; list = g_list_delete_link(list, list)) {
340 char pname[512];
342 /* got_attention was added in libpurple 2.7.0 */
343 g_snprintf(pname, sizeof(pname), FINCH_PREFS_ROOT "/sound/profiles/%s%s",
344 (char *)list->data, "/enabled/got_attention");
345 purple_prefs_add_bool(pname, FALSE);
346 g_snprintf(pname, sizeof(pname), FINCH_PREFS_ROOT "/sound/profiles/%s%s",
347 (char *)list->data, "/file/got_attention");
348 purple_prefs_add_path(pname, "");
350 g_free(list->data);
354 static void
355 finch_sound_init(void)
357 void *gnt_sound_handle = finch_sound_get_handle();
358 void *blist_handle = purple_blist_get_handle();
359 void *conv_handle = purple_conversations_get_handle();
360 #ifdef USE_GSTREAMER
361 GError *error = NULL;
362 #endif
364 purple_signal_connect(purple_connections_get_handle(), "signed-on",
365 gnt_sound_handle, PURPLE_CALLBACK(account_signon_cb),
366 NULL);
368 purple_prefs_add_none(FINCH_PREFS_ROOT "/sound");
369 purple_prefs_add_string(FINCH_PREFS_ROOT "/sound/actprofile", DEFAULT_PROFILE);
370 purple_prefs_add_none(FINCH_PREFS_ROOT "/sound/profiles");
372 purple_prefs_connect_callback(gnt_sound_handle, FINCH_PREFS_ROOT "/sound/actprofile", initialize_profile, NULL);
373 purple_prefs_trigger_callback(FINCH_PREFS_ROOT "/sound/actprofile");
376 #ifdef USE_GSTREAMER
377 purple_debug_info("sound", "Initializing sound output drivers.\n");
378 #if (GST_VERSION_MAJOR > 0 || \
379 (GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR > 10) || \
380 (GST_VERSION_MAJOR == 0 && GST_VERSION_MINOR == 10 && GST_VERSION_MICRO >= 10))
381 gst_registry_fork_set_enabled(FALSE);
382 #endif
383 if ((gst_init_failed = !gst_init_check(NULL, NULL, &error))) {
384 purple_notify_error(NULL, _("GStreamer Failure"),
385 _("GStreamer failed to initialize."),
386 error ? error->message : "");
387 if (error) {
388 g_error_free(error);
389 error = NULL;
392 #endif /* USE_GSTREAMER */
394 purple_signal_connect(blist_handle, "buddy-signed-on",
395 gnt_sound_handle, PURPLE_CALLBACK(buddy_state_cb),
396 GINT_TO_POINTER(PURPLE_SOUND_BUDDY_ARRIVE));
397 purple_signal_connect(blist_handle, "buddy-signed-off",
398 gnt_sound_handle, PURPLE_CALLBACK(buddy_state_cb),
399 GINT_TO_POINTER(PURPLE_SOUND_BUDDY_LEAVE));
400 purple_signal_connect(conv_handle, "received-im-msg",
401 gnt_sound_handle, PURPLE_CALLBACK(im_msg_received_cb),
402 GINT_TO_POINTER(PURPLE_SOUND_RECEIVE));
403 purple_signal_connect(conv_handle, "sent-im-msg",
404 gnt_sound_handle, PURPLE_CALLBACK(im_msg_sent_cb),
405 GINT_TO_POINTER(PURPLE_SOUND_SEND));
406 purple_signal_connect(conv_handle, "chat-buddy-joined",
407 gnt_sound_handle, PURPLE_CALLBACK(chat_buddy_join_cb),
408 GINT_TO_POINTER(PURPLE_SOUND_CHAT_JOIN));
409 purple_signal_connect(conv_handle, "chat-buddy-left",
410 gnt_sound_handle, PURPLE_CALLBACK(chat_buddy_left_cb),
411 GINT_TO_POINTER(PURPLE_SOUND_CHAT_LEAVE));
412 purple_signal_connect(conv_handle, "sent-chat-msg",
413 gnt_sound_handle, PURPLE_CALLBACK(chat_msg_sent_cb),
414 GINT_TO_POINTER(PURPLE_SOUND_CHAT_YOU_SAY));
415 purple_signal_connect(conv_handle, "received-chat-msg",
416 gnt_sound_handle, PURPLE_CALLBACK(chat_msg_received_cb),
417 GINT_TO_POINTER(PURPLE_SOUND_CHAT_SAY));
418 purple_signal_connect(conv_handle, "got-attention",
419 gnt_sound_handle, PURPLE_CALLBACK(got_attention_cb),
420 GINT_TO_POINTER(PURPLE_SOUND_GOT_ATTENTION));
422 update_profiles();
425 static void
426 finch_sound_uninit(void)
428 #ifdef USE_GSTREAMER
429 if (!gst_init_failed)
430 gst_deinit();
431 #endif
433 purple_signals_disconnect_by_handle(finch_sound_get_handle());
436 #ifdef USE_GSTREAMER
437 static gboolean
438 bus_call (GstBus *bus, GstMessage *msg, gpointer data)
440 GstElement *play = data;
441 GError *err = NULL;
443 switch (GST_MESSAGE_TYPE (msg)) {
444 case GST_MESSAGE_ERROR:
445 gst_message_parse_error(msg, &err, NULL);
446 purple_debug_error("gstreamer", "%s\n", err->message);
447 g_error_free(err);
448 /* fall-through and clean up */
449 case GST_MESSAGE_EOS:
450 gst_element_set_state(play, GST_STATE_NULL);
451 gst_object_unref(GST_OBJECT(play));
452 break;
453 case GST_MESSAGE_WARNING:
454 gst_message_parse_warning(msg, &err, NULL);
455 purple_debug_warning("gstreamer", "%s\n", err->message);
456 g_error_free(err);
457 break;
458 default:
459 break;
461 return TRUE;
463 #endif
465 static void
466 finch_sound_play_file(const char *filename)
468 const char *method;
469 #ifdef USE_GSTREAMER
470 float volume;
471 char *uri;
472 GstElement *sink = NULL;
473 GstElement *play = NULL;
474 GstBus *bus = NULL;
475 #endif
476 if (purple_prefs_get_bool(make_pref("/mute")))
477 return;
479 method = purple_prefs_get_string(make_pref("/method"));
481 if (!strcmp(method, "nosound")) {
482 return;
483 } else if (!strcmp(method, "beep")) {
484 beep();
485 return;
488 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
489 purple_debug_error("gntsound", "sound file (%s) does not exist.\n", filename);
490 return;
493 #ifndef _WIN32
494 if (!strcmp(method, "custom")) {
495 const char *sound_cmd;
496 char *command;
497 char *esc_filename;
498 GError *error = NULL;
500 sound_cmd = purple_prefs_get_path(make_pref("/command"));
502 if (!sound_cmd || *sound_cmd == '\0') {
503 purple_debug_error("gntsound",
504 "'Command' sound method has been chosen, "
505 "but no command has been set.");
506 return;
509 esc_filename = g_shell_quote(filename);
511 if (strstr(sound_cmd, "%s"))
512 command = purple_strreplace(sound_cmd, "%s", esc_filename);
513 else
514 command = g_strdup_printf("%s %s", sound_cmd, esc_filename);
516 if (!g_spawn_command_line_async(command, &error)) {
517 purple_debug_error("gntsound", "sound command could not be launched: %s\n", error->message);
518 g_error_free(error);
521 g_free(esc_filename);
522 g_free(command);
523 return;
525 #ifdef USE_GSTREAMER
526 if (gst_init_failed) /* Perhaps do beep instead? */
527 return;
528 volume = (float)(CLAMP(purple_prefs_get_int(make_pref("/volume")), 0, 100)) / 50;
529 if (!strcmp(method, "automatic")) {
530 if (purple_running_gnome()) {
531 sink = gst_element_factory_make("gconfaudiosink", "sink");
533 if (!sink)
534 sink = gst_element_factory_make("autoaudiosink", "sink");
535 if (!sink) {
536 purple_debug_error("sound", "Unable to create GStreamer audiosink.\n");
537 return;
539 } else if (!strcmp(method, "esd")) {
540 sink = gst_element_factory_make("esdsink", "sink");
541 if (!sink) {
542 purple_debug_error("sound", "Unable to create GStreamer audiosink.\n");
543 return;
545 } else if (!strcmp(method, "alsa")) {
546 sink = gst_element_factory_make("alsasink", "sink");
547 if (!sink) {
548 purple_debug_error("sound", "Unable to create GStreamer audiosink.\n");
549 return;
551 } else {
552 purple_debug_error("sound", "Unknown sound method '%s'\n", method);
553 return;
556 play = gst_element_factory_make("playbin", "play");
558 if (play == NULL) {
559 return;
562 uri = g_strdup_printf("file://%s", filename);
564 g_object_set(G_OBJECT(play), "uri", uri,
565 "volume", volume,
566 "audio-sink", sink, NULL);
568 bus = gst_pipeline_get_bus(GST_PIPELINE(play));
569 gst_bus_add_watch(bus, bus_call, play);
571 gst_element_set_state(play, GST_STATE_PLAYING);
573 gst_object_unref(bus);
574 g_free(uri);
576 #else /* USE_GSTREAMER */
577 beep();
578 return;
579 #endif /* USE_GSTREAMER */
580 #else /* _WIN32 */
581 purple_debug_info("sound", "Playing %s\n", filename);
584 wchar_t *wc_filename = g_utf8_to_utf16(filename,
585 -1, NULL, NULL, NULL);
586 if (!PlaySoundW(wc_filename, NULL, SND_ASYNC | SND_FILENAME))
587 purple_debug(PURPLE_DEBUG_ERROR, "sound", "Error playing sound.\n");
588 g_free(wc_filename);
590 #endif /* _WIN32 */
593 static void
594 finch_sound_play_event(PurpleSoundEventID event)
596 char *enable_pref;
597 char *file_pref;
598 if ((event == PURPLE_SOUND_BUDDY_ARRIVE) && mute_login_sounds)
599 return;
601 if (event >= PURPLE_NUM_SOUNDS ||
602 event >= G_N_ELEMENTS(sounds)) {
603 purple_debug_error("sound", "got request for unknown sound: %d\n", event);
604 return;
607 enable_pref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/enabled/%s",
608 finch_sound_get_active_profile(),
609 sounds[event].pref);
610 file_pref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/file/%s", finch_sound_get_active_profile(), sounds[event].pref);
612 /* check NULL for sounds that don't have an option, ie buddy pounce */
613 if (purple_prefs_get_bool(enable_pref)) {
614 char *filename = g_strdup(purple_prefs_get_path(file_pref));
615 if (!filename || !strlen(filename)) {
616 g_free(filename);
617 /* XXX Consider creating a constant for "sounds/purple" to be shared with Pidgin */
618 filename = g_build_filename(DATADIR, "sounds", "purple", sounds[event].def, NULL);
621 purple_sound_play_file(filename, NULL);
622 g_free(filename);
625 g_free(enable_pref);
626 g_free(file_pref);
629 GList *
630 finch_sound_get_profiles()
632 GList *list = NULL, *iter;
633 iter = purple_prefs_get_children_names(FINCH_PREFS_ROOT "/sound/profiles");
634 while (iter) {
635 list = g_list_append(list, g_strdup(strrchr(iter->data, '/') + 1));
636 g_free(iter->data);
637 iter = g_list_delete_link(iter, iter);
639 return list;
642 /* This will also create it if it doesn't exist */
643 void
644 finch_sound_set_active_profile(const char *name)
646 purple_prefs_set_string(FINCH_PREFS_ROOT "/sound/actprofile", name);
649 static gboolean
650 finch_sound_profile_exists(const char *name)
652 gchar * tmp;
653 gboolean ret = purple_prefs_exists(tmp = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s", name));
654 g_free(tmp);
655 return ret;
658 static void
659 save_cb(GntWidget *button, gpointer win)
661 GList * itr;
663 purple_prefs_set_string(make_pref("/method"), gnt_combo_box_get_selected_data(GNT_COMBO_BOX(pref_dialog->method)));
664 purple_prefs_set_path(make_pref("/command"), gnt_entry_get_text(GNT_ENTRY(pref_dialog->command)));
665 purple_prefs_set_bool(make_pref("/conv_focus"), gnt_check_box_get_checked(GNT_CHECK_BOX(pref_dialog->conv_focus)));
666 purple_prefs_set_int("/purple/sound/while_status", GPOINTER_TO_INT(gnt_combo_box_get_selected_data(GNT_COMBO_BOX(pref_dialog->while_status))));
667 purple_prefs_set_int(make_pref("/volume"), gnt_slider_get_value(GNT_SLIDER(pref_dialog->volume)));
669 for (itr = gnt_tree_get_rows(GNT_TREE(pref_dialog->events)); itr; itr = itr->next) {
670 FinchSoundEvent * event = &sounds[GPOINTER_TO_INT(itr->data)];
671 char * filepref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/file/%s", finch_sound_get_active_profile(), event->pref);
672 char * boolpref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/enabled/%s", finch_sound_get_active_profile(), event->pref);
673 purple_prefs_set_bool(boolpref, gnt_tree_get_choice(GNT_TREE(pref_dialog->events), itr->data));
674 purple_prefs_set_path(filepref, event->file ? event->file : "");
675 g_free(filepref);
676 g_free(boolpref);
678 gnt_widget_destroy(GNT_WIDGET(win));
681 static void
682 file_cb(GntFileSel *w, const char *path, const char *file, gpointer data)
684 FinchSoundEvent *event = data;
686 g_free(event->file);
687 event->file = g_strdup(path);
689 gnt_tree_change_text(GNT_TREE(pref_dialog->events), GINT_TO_POINTER(event->id), 1, file);
690 gnt_tree_set_choice(GNT_TREE(pref_dialog->events), GINT_TO_POINTER(event->id), TRUE);
692 gnt_widget_destroy(GNT_WIDGET(w));
695 static void
696 test_cb(GntWidget *button, gpointer null)
698 PurpleSoundEventID id = GPOINTER_TO_INT(gnt_tree_get_selection_data(GNT_TREE(pref_dialog->events)));
699 FinchSoundEvent * event = &sounds[id];
700 char *enabled, *file, *tmpfile, *volpref;
701 gboolean temp_value;
702 int volume;
704 enabled = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/enabled/%s",
705 finch_sound_get_active_profile(), event->pref);
706 file = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/file/%s",
707 finch_sound_get_active_profile(), event->pref);
708 volpref = g_strdup(make_pref("/volume"));
710 temp_value = purple_prefs_get_bool(enabled);
711 tmpfile = g_strdup(purple_prefs_get_path(file));
712 volume = purple_prefs_get_int(volpref);
714 purple_prefs_set_path(file, event->file);
715 if (!temp_value) purple_prefs_set_bool(enabled, TRUE);
716 purple_prefs_set_int(volpref, gnt_slider_get_value(GNT_SLIDER(pref_dialog->volume)));
718 purple_sound_play_event(id, NULL);
720 if (!temp_value) purple_prefs_set_bool(enabled, FALSE);
721 purple_prefs_set_path(file, tmpfile);
722 purple_prefs_set_int(volpref, volume);
724 g_free(enabled);
725 g_free(file);
726 g_free(tmpfile);
727 g_free(volpref);
730 static void
731 reset_cb(GntWidget *button, gpointer null)
733 /* Don't dereference this pointer ! */
734 gpointer key = gnt_tree_get_selection_data(GNT_TREE(pref_dialog->events));
736 FinchSoundEvent * event = &sounds[GPOINTER_TO_INT(key)];
737 g_free(event->file);
738 event->file = NULL;
739 gnt_tree_change_text(GNT_TREE(pref_dialog->events), key, 1, _("(default)"));
743 static void
744 choose_cb(GntWidget *button, gpointer null)
746 GntWidget *w = gnt_file_sel_new();
747 GntFileSel *sel = GNT_FILE_SEL(w);
748 PurpleSoundEventID id = GPOINTER_TO_INT(gnt_tree_get_selection_data(GNT_TREE(pref_dialog->events)));
749 FinchSoundEvent * event = &sounds[id];
750 char *path = NULL;
752 gnt_box_set_title(GNT_BOX(w), _("Select Sound File ..."));
753 gnt_file_sel_set_current_location(sel,
754 (event && event->file) ? (path = g_path_get_dirname(event->file))
755 : purple_home_dir());
757 g_signal_connect_swapped(G_OBJECT(sel->cancel), "activate", G_CALLBACK(gnt_widget_destroy), sel);
758 g_signal_connect(G_OBJECT(sel), "file_selected", G_CALLBACK(file_cb), event);
759 g_signal_connect_swapped(G_OBJECT(sel), "destroy", G_CALLBACK(g_nullify_pointer), &pref_dialog->selector);
761 /* If there's an already open file-selector, close that one. */
762 if (pref_dialog->selector)
763 gnt_widget_destroy(pref_dialog->selector);
765 pref_dialog->selector = w;
767 gnt_widget_show(w);
768 g_free(path);
771 static void
772 release_pref_dialog(GntBindable *data, gpointer null)
774 GList * itr;
775 for (itr = gnt_tree_get_rows(GNT_TREE(pref_dialog->events)); itr; itr = itr->next) {
776 PurpleSoundEventID id = GPOINTER_TO_INT(itr->data);
777 FinchSoundEvent * e = &sounds[id];
778 g_free(e->file);
779 e->file = NULL;
781 if (pref_dialog->selector)
782 gnt_widget_destroy(pref_dialog->selector);
783 g_free(pref_dialog->original_profile);
784 g_free(pref_dialog);
785 pref_dialog = NULL;
788 static void
789 load_pref_window(const char * profile)
791 gint i;
793 finch_sound_set_active_profile(profile);
795 gnt_combo_box_set_selected(GNT_COMBO_BOX(pref_dialog->method), (gchar *)purple_prefs_get_string(make_pref("/method")));
797 gnt_entry_set_text(GNT_ENTRY(pref_dialog->command), purple_prefs_get_path(make_pref("/command")));
799 gnt_check_box_set_checked(GNT_CHECK_BOX(pref_dialog->conv_focus), purple_prefs_get_bool(make_pref("/conv_focus")));
801 gnt_combo_box_set_selected(GNT_COMBO_BOX(pref_dialog->while_status), GINT_TO_POINTER(purple_prefs_get_int("/purple" "/sound/while_status")));
803 gnt_slider_set_value(GNT_SLIDER(pref_dialog->volume), CLAMP(purple_prefs_get_int(make_pref("/volume")), 0, 100));
805 for (i = 0; i < PURPLE_NUM_SOUNDS; i++) {
806 FinchSoundEvent * event = &sounds[i];
807 gchar *boolpref;
808 gchar *filepref, *basename = NULL;
809 const char * profile = finch_sound_get_active_profile();
811 filepref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/file/%s", profile, event->pref);
813 g_free(event->file);
814 event->file = g_strdup(purple_prefs_get_path(filepref));
816 g_free(filepref);
817 if (event->label == NULL) {
818 continue;
821 boolpref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s/enabled/%s", profile, event->pref);
823 gnt_tree_change_text(GNT_TREE(pref_dialog->events), GINT_TO_POINTER(i), 0, event->label);
824 gnt_tree_change_text(GNT_TREE(pref_dialog->events), GINT_TO_POINTER(i), 1,
825 event->file[0] ? (basename = g_path_get_basename(event->file)) : _("(default)"));
826 g_free(basename);
828 gnt_tree_set_choice(GNT_TREE(pref_dialog->events), GINT_TO_POINTER(i), purple_prefs_get_bool(boolpref));
830 g_free(boolpref);
833 gnt_tree_set_selected(GNT_TREE(pref_dialog->profiles), (gchar *)finch_sound_get_active_profile());
835 gnt_widget_draw(pref_dialog->window);
838 static void
839 reload_pref_window(const char *profile)
841 if (!strcmp(profile, finch_sound_get_active_profile()))
842 return;
843 load_pref_window(profile);
846 static void
847 prof_del_cb(GntWidget *button, gpointer null)
849 const char * profile = gnt_entry_get_text(GNT_ENTRY(pref_dialog->new_profile));
850 gchar * pref;
852 if (!strcmp(profile, DEFAULT_PROFILE))
853 return;
855 pref = g_strdup_printf(FINCH_PREFS_ROOT "/sound/profiles/%s", profile);
856 purple_prefs_remove(pref);
857 g_free(pref);
859 if (!strcmp(pref_dialog->original_profile, profile)) {
860 g_free(pref_dialog->original_profile);
861 pref_dialog->original_profile = g_strdup(DEFAULT_PROFILE);
864 if(!strcmp(profile, finch_sound_get_active_profile()))
865 reload_pref_window(DEFAULT_PROFILE);
867 gnt_tree_remove(GNT_TREE(pref_dialog->profiles), (gchar *) profile);
870 static void
871 prof_add_cb(GntButton *button, GntEntry * entry)
873 const char * profile = gnt_entry_get_text(entry);
874 GntTreeRow * row;
875 if (!finch_sound_profile_exists(profile)) {
876 gpointer key = g_strdup(profile);
877 row = gnt_tree_create_row(GNT_TREE(pref_dialog->profiles), profile);
878 gnt_tree_add_row_after(GNT_TREE(pref_dialog->profiles), key,
879 row,
880 NULL, NULL);
881 gnt_entry_set_text(entry, "");
882 gnt_tree_set_selected(GNT_TREE(pref_dialog->profiles), key);
883 finch_sound_set_active_profile(key);
884 } else
885 reload_pref_window(profile);
888 static void
889 prof_load_cb(GntTree *tree, gpointer oldkey, gpointer newkey, gpointer null)
891 reload_pref_window(newkey);
894 static void
895 cancel_cb(GntButton *button, gpointer win)
897 finch_sound_set_active_profile(pref_dialog->original_profile);
898 gnt_widget_destroy(GNT_WIDGET(win));
901 void
902 finch_sounds_show_all(void)
904 GntWidget *box, *tmpbox, *splitbox, *cmbox, *slider;
905 GntWidget *entry;
906 GntWidget *chkbox;
907 GntWidget *button;
908 GntWidget *label;
909 GntWidget *tree;
910 GntWidget *win;
912 gint i;
913 GList *itr, *list;
915 if (pref_dialog) {
916 gnt_window_present(pref_dialog->window);
917 return;
920 pref_dialog = g_new0(SoundPrefDialog, 1);
922 pref_dialog->original_profile = g_strdup(finch_sound_get_active_profile());
924 pref_dialog->window = win = gnt_window_box_new(FALSE, TRUE);
925 gnt_box_set_pad(GNT_BOX(win), 0);
926 gnt_box_set_toplevel(GNT_BOX(win), TRUE);
927 gnt_box_set_title(GNT_BOX(win), _("Sound Preferences"));
928 gnt_box_set_fill(GNT_BOX(win), TRUE);
929 gnt_box_set_alignment(GNT_BOX(win), GNT_ALIGN_MID);
931 /* Profiles */
932 splitbox = gnt_hbox_new(FALSE);
933 gnt_box_set_pad(GNT_BOX(splitbox), 0);
934 gnt_box_set_alignment(GNT_BOX(splitbox), GNT_ALIGN_TOP);
936 box = gnt_vbox_new(FALSE);
937 gnt_box_set_pad(GNT_BOX(box), 0);
938 gnt_box_add_widget(GNT_BOX(box), gnt_label_new_with_format(_("Profiles"), GNT_TEXT_FLAG_BOLD));
939 pref_dialog->profiles = tree = gnt_tree_new();
940 gnt_tree_set_hash_fns(GNT_TREE(tree), g_str_hash, g_str_equal, g_free);
941 gnt_tree_set_compare_func(GNT_TREE(tree), (GCompareFunc)g_ascii_strcasecmp);
942 g_signal_connect(G_OBJECT(tree), "selection-changed", G_CALLBACK(prof_load_cb), NULL);
944 itr = list = finch_sound_get_profiles();
945 for (; itr; itr = itr->next) {
946 /* Do not free itr->data. It's the stored as a key for the tree, and will
947 * be freed when the tree is destroyed. */
948 gnt_tree_add_row_after(GNT_TREE(tree), itr->data,
949 gnt_tree_create_row(GNT_TREE(tree), itr->data), NULL, NULL);
951 g_list_free(list);
953 gnt_box_add_widget(GNT_BOX(box), tree);
955 pref_dialog->new_profile = entry = gnt_entry_new("");
956 gnt_box_add_widget(GNT_BOX(box), entry);
958 tmpbox = gnt_hbox_new(FALSE);
959 button = gnt_button_new("Add");
960 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(prof_add_cb), entry);
961 gnt_box_add_widget(GNT_BOX(tmpbox), button);
962 button = gnt_button_new("Delete");
963 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(prof_del_cb), NULL);
964 gnt_box_add_widget(GNT_BOX(tmpbox), button);
965 gnt_box_add_widget(GNT_BOX(box), tmpbox);
966 gnt_box_add_widget(GNT_BOX(splitbox), box);
968 gnt_box_add_widget(GNT_BOX(splitbox), gnt_vline_new());
970 /* Sound method */
972 box = gnt_vbox_new(FALSE);
973 gnt_box_set_pad(GNT_BOX(box), 0);
975 pref_dialog->method = cmbox = gnt_combo_box_new();
976 gnt_tree_set_hash_fns(GNT_TREE(GNT_COMBO_BOX(cmbox)->dropdown), g_str_hash, g_str_equal, NULL);
977 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "automatic", _("Automatic"));
978 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "alsa", "ALSA");
979 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "esd", "ESD");
980 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "beep", _("Console Beep"));
981 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "custom", _("Command"));
982 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), "nosound", _("No Sound"));
984 label = gnt_label_new_with_format(_("Sound Method"), GNT_TEXT_FLAG_BOLD);
985 gnt_box_add_widget(GNT_BOX(box), label);
986 tmpbox = gnt_hbox_new(TRUE);
987 gnt_box_set_fill(GNT_BOX(tmpbox), FALSE);
988 gnt_box_set_pad(GNT_BOX(tmpbox), 0);
989 gnt_box_add_widget(GNT_BOX(tmpbox), gnt_label_new(_("Method: ")));
990 gnt_box_add_widget(GNT_BOX(tmpbox), cmbox);
991 gnt_box_add_widget(GNT_BOX(box), tmpbox);
993 tmpbox = gnt_hbox_new(TRUE);
994 gnt_box_set_pad(GNT_BOX(tmpbox), 0);
995 gnt_box_set_fill(GNT_BOX(tmpbox), FALSE);
996 gnt_box_add_widget(GNT_BOX(tmpbox), gnt_label_new(_("Sound Command\n(%s for filename)")));
997 pref_dialog->command = entry = gnt_entry_new("");
998 gnt_box_add_widget(GNT_BOX(tmpbox), entry);
999 gnt_box_add_widget(GNT_BOX(box), tmpbox);
1001 gnt_box_add_widget(GNT_BOX(box), gnt_line_new(FALSE));
1003 /* Sound options */
1004 gnt_box_add_widget(GNT_BOX(box), gnt_label_new_with_format(_("Sound Options"), GNT_TEXT_FLAG_BOLD));
1005 pref_dialog->conv_focus = chkbox = gnt_check_box_new(_("Sounds when conversation has focus"));
1006 gnt_box_add_widget(GNT_BOX(box), chkbox);
1008 tmpbox = gnt_hbox_new(TRUE);
1009 gnt_box_set_pad(GNT_BOX(tmpbox), 0);
1010 gnt_box_set_fill(GNT_BOX(tmpbox), FALSE);
1011 gnt_box_add_widget(GNT_BOX(tmpbox), gnt_label_new("Enable Sounds:"));
1012 pref_dialog->while_status = cmbox = gnt_combo_box_new();
1013 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), GINT_TO_POINTER(3), _("Always"));
1014 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), GINT_TO_POINTER(1), _("Only when available"));
1015 gnt_combo_box_add_data(GNT_COMBO_BOX(cmbox), GINT_TO_POINTER(2), _("Only when not available"));
1016 gnt_box_add_widget(GNT_BOX(tmpbox), cmbox);
1017 gnt_box_add_widget(GNT_BOX(box), tmpbox);
1019 tmpbox = gnt_hbox_new(TRUE);
1020 gnt_box_set_pad(GNT_BOX(tmpbox), 0);
1021 gnt_box_set_fill(GNT_BOX(tmpbox), FALSE);
1022 gnt_box_add_widget(GNT_BOX(tmpbox), gnt_label_new(_("Volume(0-100):")));
1024 pref_dialog->volume = slider = gnt_slider_new(FALSE, 100, 0);
1025 gnt_slider_set_step(GNT_SLIDER(slider), 5);
1026 gnt_slider_set_small_step(GNT_SLIDER(slider), 1);
1027 gnt_slider_set_large_step(GNT_SLIDER(slider), 20);
1028 label = gnt_label_new("");
1029 gnt_slider_reflect_label(GNT_SLIDER(slider), GNT_LABEL(label));
1030 gnt_box_set_pad(GNT_BOX(tmpbox), 1);
1031 gnt_box_add_widget(GNT_BOX(tmpbox), slider);
1032 gnt_box_add_widget(GNT_BOX(tmpbox), label);
1033 gnt_box_add_widget(GNT_BOX(box), tmpbox);
1034 gnt_box_add_widget(GNT_BOX(splitbox), box);
1036 gnt_box_add_widget(GNT_BOX(win), splitbox);
1038 gnt_box_add_widget(GNT_BOX(win), gnt_hline_new());
1040 /* Sound events */
1041 gnt_box_add_widget(GNT_BOX(win), gnt_label_new_with_format(_("Sound Events"), GNT_TEXT_FLAG_BOLD));
1042 pref_dialog->events = tree = gnt_tree_new_with_columns(2);
1043 gnt_tree_set_column_titles(GNT_TREE(tree), _("Event"), _("File"));
1044 gnt_tree_set_show_title(GNT_TREE(tree), TRUE);
1046 for (i = 0; i < PURPLE_NUM_SOUNDS; i++) {
1047 FinchSoundEvent * event = &sounds[i];
1049 if (event->label == NULL) {
1050 continue;
1053 gnt_tree_add_choice(GNT_TREE(tree), GINT_TO_POINTER(i),
1054 gnt_tree_create_row(GNT_TREE(tree), event->label, event->def),
1055 NULL, NULL);
1058 gnt_tree_adjust_columns(GNT_TREE(tree));
1059 gnt_box_add_widget(GNT_BOX(win), tree);
1061 box = gnt_hbox_new(FALSE);
1062 button = gnt_button_new(_("Test"));
1063 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(test_cb), NULL);
1064 gnt_box_add_widget(GNT_BOX(box), button);
1065 button = gnt_button_new(_("Reset"));
1066 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(reset_cb), NULL);
1067 gnt_box_add_widget(GNT_BOX(box), button);
1068 button = gnt_button_new(_("Choose..."));
1069 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(choose_cb), NULL);
1070 gnt_box_add_widget(GNT_BOX(box), button);
1071 gnt_box_add_widget(GNT_BOX(win), box);
1073 gnt_box_add_widget(GNT_BOX(win), gnt_line_new(FALSE));
1075 /* Add new stuff before this */
1076 box = gnt_hbox_new(FALSE);
1077 gnt_box_set_fill(GNT_BOX(box), TRUE);
1078 button = gnt_button_new(_("Save"));
1079 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(save_cb), win);
1080 gnt_box_add_widget(GNT_BOX(box), button);
1081 button = gnt_button_new(_("Cancel"));
1082 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(cancel_cb), win);
1083 gnt_box_add_widget(GNT_BOX(box), button);
1084 gnt_box_add_widget(GNT_BOX(win), box);
1086 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(release_pref_dialog), NULL);
1088 load_pref_window(finch_sound_get_active_profile());
1090 gnt_widget_show(win);
1093 gboolean finch_sound_is_enabled(void)
1095 const char *pref = make_pref("/method");
1096 const char *method = purple_prefs_get_string(pref);
1098 if (!method)
1099 return FALSE;
1100 if (strcmp(method, "nosound") == 0)
1101 return FALSE;
1102 if (purple_prefs_get_int(make_pref("/volume")) <= 0)
1103 return FALSE;
1105 return TRUE;
1108 static PurpleSoundUiOps sound_ui_ops =
1110 finch_sound_init,
1111 finch_sound_uninit,
1112 finch_sound_play_file,
1113 finch_sound_play_event,
1114 NULL,
1115 NULL,
1116 NULL,
1117 NULL
1120 PurpleSoundUiOps *
1121 finch_sound_get_ui_ops(void)
1123 return &sound_ui_ops;