Fix a crash when remote users have certain characters in their
[pidgin-git.git] / libpurple / sound.c
blob4cb4ebb013db14d3154cc90333d2cbea9cac6bfd
1 /*
2 * purple
4 * Purple 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 "internal.h"
25 #include "blist.h"
26 #include "prefs.h"
27 #include "sound.h"
28 #include "sound-theme-loader.h"
29 #include "theme-manager.h"
31 static PurpleSoundUiOps *sound_ui_ops = NULL;
33 #define STATUS_AVAILABLE 1
34 #define STATUS_AWAY 2
36 static time_t last_played[PURPLE_NUM_SOUNDS];
38 static gboolean
39 purple_sound_play_required(const PurpleAccount *account)
41 gint pref_status = purple_prefs_get_int("/purple/sound/while_status");
43 if (pref_status == 3)
45 /* Play sounds: Always */
46 return TRUE;
49 if (account != NULL)
51 PurpleStatus *status = purple_account_get_active_status(account);
53 if (purple_status_is_online(status))
55 gboolean available = purple_status_is_available(status);
56 return (( available && pref_status == STATUS_AVAILABLE) ||
57 (!available && pref_status == STATUS_AWAY));
61 /* We get here a couple of ways. Either the request has been OK'ed
62 * by purple_sound_play_event() and we're here because the UI has
63 * called purple_sound_play_file(), or we're here for something
64 * not related to an account (like testing a sound). */
65 return TRUE;
68 void
69 purple_sound_play_file(const char *filename, const PurpleAccount *account)
71 if (!purple_sound_play_required(account))
72 return;
74 if(sound_ui_ops && sound_ui_ops->play_file)
75 sound_ui_ops->play_file(filename);
78 void
79 purple_sound_play_event(PurpleSoundEventID event, const PurpleAccount *account)
81 if (!purple_sound_play_required(account))
82 return;
84 if (time(NULL) - last_played[event] < 2)
85 return;
86 last_played[event] = time(NULL);
88 if(sound_ui_ops && sound_ui_ops->play_event) {
89 int plugin_return;
91 plugin_return = GPOINTER_TO_INT(purple_signal_emit_return_1(
92 purple_sounds_get_handle(), "playing-sound-event",
93 event, account));
95 if (plugin_return)
96 return;
97 else
98 sound_ui_ops->play_event(event);
102 void
103 purple_sound_set_ui_ops(PurpleSoundUiOps *ops)
105 if(sound_ui_ops && sound_ui_ops->uninit)
106 sound_ui_ops->uninit();
108 sound_ui_ops = ops;
110 if(sound_ui_ops && sound_ui_ops->init)
111 sound_ui_ops->init();
114 PurpleSoundUiOps *
115 purple_sound_get_ui_ops(void)
117 return sound_ui_ops;
120 void
121 purple_sound_init()
123 void *handle = purple_sounds_get_handle();
125 /**********************************************************************
126 * Register signals
127 **********************************************************************/
129 purple_signal_register(handle, "playing-sound-event",
130 purple_marshal_BOOLEAN__INT_POINTER,
131 purple_value_new(PURPLE_TYPE_BOOLEAN), 2,
132 purple_value_new(PURPLE_TYPE_INT),
133 purple_value_new(PURPLE_TYPE_SUBTYPE,
134 PURPLE_SUBTYPE_ACCOUNT));
136 purple_prefs_add_none("/purple/sound");
137 purple_prefs_add_int("/purple/sound/while_status", STATUS_AVAILABLE);
138 memset(last_played, 0, sizeof(last_played));
140 purple_theme_manager_register_type(g_object_new(PURPLE_TYPE_SOUND_THEME_LOADER, "type", "sound", NULL));
143 void
144 purple_sound_uninit()
146 if(sound_ui_ops && sound_ui_ops->uninit)
147 sound_ui_ops->uninit();
149 purple_signals_unregister_by_instance(purple_sounds_get_handle());
152 void *
153 purple_sounds_get_handle()
155 static int handle;
157 return &handle;