summaryrefslogtreecommitdiff
path: root/libpurple/sound.c
blob: 4cb4ebb013db14d3154cc90333d2cbea9cac6bfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 */
#include "internal.h"

#include "blist.h"
#include "prefs.h"
#include "sound.h"
#include "sound-theme-loader.h"
#include "theme-manager.h"

static PurpleSoundUiOps *sound_ui_ops = NULL;

#define STATUS_AVAILABLE 1
#define STATUS_AWAY 2

static time_t last_played[PURPLE_NUM_SOUNDS];

static gboolean
purple_sound_play_required(const PurpleAccount *account)
{
	gint pref_status = purple_prefs_get_int("/purple/sound/while_status");

	if (pref_status == 3)
	{
		/* Play sounds: Always */
		return TRUE;
	}

	if (account != NULL)
	{
		PurpleStatus *status = purple_account_get_active_status(account);

		if (purple_status_is_online(status))
		{
			gboolean available = purple_status_is_available(status);
			return (( available && pref_status == STATUS_AVAILABLE) ||
			        (!available && pref_status == STATUS_AWAY));
		}
	}

	/* We get here a couple of ways.  Either the request has been OK'ed
	 * by purple_sound_play_event() and we're here because the UI has
	 * called purple_sound_play_file(), or we're here for something
	 * not related to an account (like testing a sound). */
	return TRUE;
}

void
purple_sound_play_file(const char *filename, const PurpleAccount *account)
{
	if (!purple_sound_play_required(account))
		return;

	if(sound_ui_ops && sound_ui_ops->play_file)
		sound_ui_ops->play_file(filename);
}

void
purple_sound_play_event(PurpleSoundEventID event, const PurpleAccount *account)
{
	if (!purple_sound_play_required(account))
		return;

	if (time(NULL) - last_played[event] < 2)
		return;
	last_played[event] = time(NULL);

	if(sound_ui_ops && sound_ui_ops->play_event) {
		int plugin_return;

		plugin_return = GPOINTER_TO_INT(purple_signal_emit_return_1(
			purple_sounds_get_handle(), "playing-sound-event",
			event, account));

		if (plugin_return)
			return;
		else
			sound_ui_ops->play_event(event);
	}
}

void
purple_sound_set_ui_ops(PurpleSoundUiOps *ops)
{
	if(sound_ui_ops && sound_ui_ops->uninit)
		sound_ui_ops->uninit();

	sound_ui_ops = ops;

	if(sound_ui_ops && sound_ui_ops->init)
		sound_ui_ops->init();
}

PurpleSoundUiOps *
purple_sound_get_ui_ops(void)
{
	return sound_ui_ops;
}

void
purple_sound_init()
{
	void *handle = purple_sounds_get_handle();

	/**********************************************************************
	 * Register signals
	**********************************************************************/

	purple_signal_register(handle, "playing-sound-event",
	                     purple_marshal_BOOLEAN__INT_POINTER,
	                     purple_value_new(PURPLE_TYPE_BOOLEAN), 2,
	                     purple_value_new(PURPLE_TYPE_INT),
	                     purple_value_new(PURPLE_TYPE_SUBTYPE,
	                                    PURPLE_SUBTYPE_ACCOUNT));

	purple_prefs_add_none("/purple/sound");
	purple_prefs_add_int("/purple/sound/while_status", STATUS_AVAILABLE);
	memset(last_played, 0, sizeof(last_played));

	purple_theme_manager_register_type(g_object_new(PURPLE_TYPE_SOUND_THEME_LOADER, "type", "sound", NULL));
}

void
purple_sound_uninit()
{
	if(sound_ui_ops && sound_ui_ops->uninit)
		sound_ui_ops->uninit();

	purple_signals_unregister_by_instance(purple_sounds_get_handle());
}

void *
purple_sounds_get_handle()
{
	static int handle;

	return &handle;
}