Merged pidgin/main into default
[pidgin-git.git] / libpurple / sound.c
blobfa0709787d9b30fa9af4e4628032e949d9763665
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 "buddylist.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;
32 static time_t last_played[PURPLE_NUM_SOUNDS];
34 static gboolean
35 purple_sound_play_required(const PurpleAccount *account)
37 gint pref_status = purple_prefs_get_int("/purple/sound/while_status");
39 if (pref_status == PURPLE_SOUND_STATUS_ALWAYS)
41 /* Play sounds: Always */
42 return TRUE;
45 if (account != NULL)
47 PurpleStatus *status = purple_account_get_active_status(account);
49 if (purple_status_is_online(status))
51 gboolean available = purple_status_is_available(status);
52 return (( available && pref_status == PURPLE_SOUND_STATUS_AVAILABLE) ||
53 (!available && pref_status == PURPLE_SOUND_STATUS_AWAY));
57 /* We get here a couple of ways. Either the request has been OK'ed
58 * by purple_sound_play_event() and we're here because the UI has
59 * called purple_sound_play_file(), or we're here for something
60 * not related to an account (like testing a sound). */
61 return TRUE;
64 void
65 purple_sound_play_file(const char *filename, const PurpleAccount *account)
67 if (!purple_sound_play_required(account))
68 return;
70 if(sound_ui_ops && sound_ui_ops->play_file)
71 sound_ui_ops->play_file(filename);
74 void
75 purple_sound_play_event(PurpleSoundEventID event, const PurpleAccount *account)
77 if (!purple_sound_play_required(account))
78 return;
80 g_return_if_fail(event < PURPLE_NUM_SOUNDS);
82 if (time(NULL) - last_played[event] < 2)
83 return;
84 last_played[event] = time(NULL);
86 if(sound_ui_ops && sound_ui_ops->play_event) {
87 int plugin_return;
89 plugin_return = GPOINTER_TO_INT(purple_signal_emit_return_1(
90 purple_sounds_get_handle(), "playing-sound-event",
91 event, account));
93 if (plugin_return)
94 return;
95 else
96 sound_ui_ops->play_event(event);
100 static PurpleSoundUiOps *
101 purple_sound_ui_ops_copy(PurpleSoundUiOps *ops)
103 PurpleSoundUiOps *ops_new;
105 g_return_val_if_fail(ops != NULL, NULL);
107 ops_new = g_new(PurpleSoundUiOps, 1);
108 *ops_new = *ops;
110 return ops_new;
113 GType
114 purple_sound_ui_ops_get_type(void)
116 static GType type = 0;
118 if (type == 0) {
119 type = g_boxed_type_register_static("PurpleSoundUiOps",
120 (GBoxedCopyFunc)purple_sound_ui_ops_copy,
121 (GBoxedFreeFunc)g_free);
124 return type;
127 void
128 purple_sound_set_ui_ops(PurpleSoundUiOps *ops)
130 if(sound_ui_ops && sound_ui_ops->uninit)
131 sound_ui_ops->uninit();
133 sound_ui_ops = ops;
135 if(sound_ui_ops && sound_ui_ops->init)
136 sound_ui_ops->init();
139 PurpleSoundUiOps *
140 purple_sound_get_ui_ops(void)
142 return sound_ui_ops;
145 void
146 purple_sound_init()
148 void *handle = purple_sounds_get_handle();
150 /**********************************************************************
151 * Register signals
152 **********************************************************************/
154 purple_signal_register(handle, "playing-sound-event",
155 purple_marshal_BOOLEAN__INT_POINTER,
156 G_TYPE_BOOLEAN, 2, G_TYPE_INT, PURPLE_TYPE_ACCOUNT);
158 purple_prefs_add_none("/purple/sound");
159 purple_prefs_add_int("/purple/sound/while_status", PURPLE_SOUND_STATUS_AVAILABLE);
160 memset(last_played, 0, sizeof(last_played));
162 purple_theme_manager_register_type(g_object_new(PURPLE_TYPE_SOUND_THEME_LOADER, "type", "sound", NULL));
165 void
166 purple_sound_uninit()
168 if(sound_ui_ops && sound_ui_ops->uninit)
169 sound_ui_ops->uninit();
171 purple_signals_unregister_by_instance(purple_sounds_get_handle());
174 void *
175 purple_sounds_get_handle()
177 static int handle;
179 return &handle;