summaryrefslogtreecommitdiff
path: root/presets.c
blob: f35901ac547081efc577896236b9239d2215f272 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * GPL 2
 */

#include <glib.h>
#include <glib-object.h>
#include <gconf/gconf-client.h>

#include "presets.h"

G_DEFINE_TYPE(CFmPresets, cfm_presets, G_TYPE_OBJECT);

#define CFM_PRESETS_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CFM_TYPE_PRESETS, CFmPresetsPrivate))

#define GCONF_KEY_BUFFER_LEN 1024
#define GCONF_PATH           "/apps/maemo/cfmradio/presets"

struct _CFmPresetsPrivate {
	GConfClient *gconf;
	gchar *name;
	gchar *gconf_dir;
	guint gconf_notify;
	GHashTable *t;
};

enum {
	PROP_0,
	PROP_NAME,
	PROP_LAST
};

enum {
	SIGNAL_0,
	SIGNAL_LAST
};

static GParamSpec *properties[PROP_LAST];
static guint signals[SIGNAL_LAST];

static inline gfloat freq_to_float(gulong f)
{
	return f / 1000000.0;
}

static inline gulong float_to_freq(gfloat f)
{
	return f * 1000000.0;
}

static inline gfloat pointer_to_float(gpointer p)
{
	union {
		gfloat f;
		gpointer p;
	} u;
	u.p = p;
	return u.f;
}

static inline gpointer float_to_pointer(gfloat f)
{
	union {
		gfloat f;
		gpointer p;
	} u;
	u.f = f;
	return u.p;
}

static inline gpointer freq_to_pointer(gulong f)
{
	return float_to_pointer(freq_to_float(f));
}

static inline gulong pointer_to_freq(gpointer p)
{
	return float_to_freq(pointer_to_float(p));
}

static void func_gconf_entry_free(gpointer data, gpointer user_data)
{
	GConfEntry *entry = (GConfEntry*) data;
	gconf_entry_free(entry);
}

static void cfm_presets_load(CFmPresets *self)
{
	CFmPresetsPrivate *priv = self->priv;

	g_debug("Loading presets from %s\n", priv->gconf_dir);

	g_hash_table_remove_all(priv->t);

	GSList *i, *l = gconf_client_all_entries(priv->gconf, priv->gconf_dir, NULL);
	for (i = l; i; i = g_slist_next(i)) {
		GConfEntry *entry = (GConfEntry*) i->data;
		const gchar *basename = g_basename(gconf_entry_get_key(entry));
		gfloat freq = g_ascii_strtod(basename, NULL);
		const gchar *name = gconf_value_get_string(gconf_entry_get_value(entry));
		g_hash_table_insert(priv->t, float_to_pointer(freq), g_strdup(name));
	}

	g_slist_foreach(l, func_gconf_entry_free, NULL);
	g_slist_free(l);
}

static void cfm_presets_gconf_notify(GConfClient *gconf, guint cnxn_id,
	GConfEntry *entry, gpointer user_data)
{
	CFmPresets *self = CFM_PRESETS(user_data);
	CFmPresetsPrivate *priv = self->priv;

	if (!entry) {
		return;
	}

	const gchar *basename = g_basename(gconf_entry_get_key(entry));
	gfloat freq = g_ascii_strtod(basename, NULL);
	GConfValue *value = gconf_entry_get_value(entry);

	if (value) {
		const gchar *name = gconf_value_get_string(gconf_entry_get_value(entry));
		g_debug("Preset '%s' changed to '%s'\n", basename, name);
		g_hash_table_insert(priv->t, float_to_pointer(freq), g_strdup(name));
	} else {
		g_debug("Preset '%s' removed\n", basename);
		g_hash_table_remove(priv->t, float_to_pointer(freq));
	}
}

static void cfm_presets_set_property(GObject *object, guint property_id,
	const GValue *value, GParamSpec *pspec)
{
	CFmPresets *self = CFM_PRESETS(object);
	switch (property_id) {
	case PROP_NAME:
		g_free(self->priv->name);
		self->priv->name = g_value_dup_string(value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
		break;
	}
}

static void cfm_presets_get_property(GObject *object, guint property_id,
	GValue *value, GParamSpec *pspec)
{
	CFmPresets *self = CFM_PRESETS(object);
	switch (property_id) {
	case PROP_NAME:
		g_debug("Prop name is now set\n");
		g_value_set_string(value, self->priv->name);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
		break;
	}
}

static void cfm_presets_init(CFmPresets *self)
{
	CFmPresetsPrivate *priv;

	self->priv = priv = CFM_PRESETS_GET_PRIVATE(self);

	priv->gconf = gconf_client_get_default();
	priv->t = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
}

static GObject * cfm_presets_constructor(GType gtype, guint n_properties,
 GObjectConstructParam *properties)
{
	GObject *object = G_OBJECT_CLASS(cfm_presets_parent_class)->constructor(
		gtype, n_properties, properties);
	CFmPresets *self = CFM_PRESETS(object);
	CFmPresetsPrivate *priv = self->priv;

	priv->gconf_dir = g_strdup_printf("%s/%s", GCONF_PATH, priv->name);

	gconf_client_add_dir(priv->gconf, priv->gconf_dir, GCONF_CLIENT_PRELOAD_ONELEVEL,
		NULL);
	priv->gconf_notify = gconf_client_notify_add(priv->gconf, priv->gconf_dir,
		cfm_presets_gconf_notify, self, NULL, NULL);

	cfm_presets_load(self);

	return object;
}

static void cfm_presets_dispose(GObject *object)
{
	CFmPresets *self = CFM_PRESETS(object);
	CFmPresetsPrivate *priv = self->priv;

	if (priv->gconf && priv->gconf_notify) {
		gconf_client_notify_remove(priv->gconf, priv->gconf_notify);
	}
	if (priv->gconf && priv->gconf_dir) {
		gconf_client_remove_dir(priv->gconf, priv->gconf_dir, NULL);
	}
	if (priv->gconf_dir) {
		g_free(priv->gconf_dir);
		priv->gconf_dir = NULL;
	}
	if (priv->gconf) {
		g_object_unref(priv->gconf);
		priv->gconf = NULL;
	}
}

static void cfm_presets_finalize(GObject *object)
{
	CFmPresets *self = CFM_PRESETS(object);
	CFmPresetsPrivate *priv = self->priv;

	g_hash_table_destroy(priv->t);
	g_free(priv->name);
}

static void cfm_presets_class_init(CFmPresetsClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
	GParamSpec *param_spec;

	g_type_class_add_private (klass, sizeof(CFmPresetsPrivate));

	gobject_class->constructor = cfm_presets_constructor;
	gobject_class->set_property = cfm_presets_set_property;
	gobject_class->get_property = cfm_presets_get_property;
	gobject_class->dispose = cfm_presets_dispose;
	gobject_class->finalize = cfm_presets_finalize;

	param_spec = g_param_spec_string("name",
	                                 "Preset set name",
	                                 "This is the name for this set of presets",
	                                 "default",
	                                 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
	                                 G_PARAM_STATIC_STRINGS);
	properties[PROP_NAME] = param_spec;
	g_object_class_install_property(gobject_class, PROP_NAME, param_spec);
}

static gpointer cfm_presets_build_default(gpointer data)
{
	return g_object_new(CFM_TYPE_PRESETS, NULL);
}

CFmPresets* cfm_presets_get_default()
{
	static GOnce once = G_ONCE_INIT;
	g_once(&once, cfm_presets_build_default, NULL);

	return CFM_PRESETS(g_object_ref(G_OBJECT(once.retval)));
}

CFmPresets* cfm_presets_get_for_name(const char * name)
{
	return g_object_new(CFM_TYPE_PRESETS, "name", name, NULL);
}

void cfm_presets_set_preset(CFmPresets *self, gulong freq, const gchar *name)
{
	CFmPresetsPrivate *priv = self->priv;
	GError *error = NULL;
	gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
	gchar *key = g_strdup_printf("%s/%s", priv->gconf_dir,
		g_ascii_formatd(buf, sizeof(buf), "%.1f", freq_to_float(freq)));
	if (!gconf_client_set_string(priv->gconf, key, name, &error)) {
		g_warning("Failed to store preset '%s' ('%s'): %s\n", key, name,
			error->message);
	}
	g_free(key);
}

void cfm_presets_remove_preset(CFmPresets *self, gulong freq)
{
	CFmPresetsPrivate *priv = self->priv;
	GError *error = NULL;
	gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
	gchar *key = g_strdup_printf("%s/%s", priv->gconf_dir,
		g_ascii_formatd(buf, sizeof(buf), "%.1f", freq_to_float(freq)));
	if (!gconf_client_unset(priv->gconf, key, &error)) {
		g_warning("Failed to remove preset '%s': %s\n", key, error->message);
	}
	g_free(key);
}

gboolean cfm_presets_is_preset(CFmPresets *self, gulong freq)
{
	CFmPresetsPrivate *priv = self->priv;

	return g_hash_table_lookup(priv->t, freq_to_pointer(freq)) ? TRUE : FALSE;
}

const gchar * cfm_presets_get_preset(CFmPresets *self, gulong freq)
{
	CFmPresetsPrivate *priv = self->priv;

	gpointer found = g_hash_table_lookup(priv->t, freq_to_pointer(freq));
	if (found) {
		return (const gchar *) found;
	} else {
		return NULL;
	}

	return NULL;
}

static void preset_to_list_store(gpointer key, gpointer value, gpointer user_data)
{
	GtkListStore *l = GTK_LIST_STORE(user_data);
	GtkTreeIter iter;

	gulong freq = pointer_to_freq(key);
	gtk_list_store_insert_with_values(l, &iter, 0, 0, freq, 1, value, -1);
}

static gint compare_freq(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
	gpointer user_data)
{
	gulong freq_a, freq_b;
	gtk_tree_model_get(model, a, 0, &freq_a, -1);
	gtk_tree_model_get(model, b, 0, &freq_b, -1);

	if (freq_a > freq_b) return 1;
	else if (freq_a < freq_b) return -1;
	else return 0;
}

GtkListStore* cfm_presets_get_all(CFmPresets *self)
{
	CFmPresetsPrivate *priv = self->priv;
	GtkListStore *l = gtk_list_store_new(2, G_TYPE_ULONG, G_TYPE_STRING);
	g_hash_table_foreach(priv->t, preset_to_list_store, l);

	gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(l), 0, compare_freq,
		NULL, NULL);
	gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(l), 0,
		GTK_SORT_ASCENDING);

	return l;
}