summaryrefslogtreecommitdiff
path: root/presets.c
blob: f9d5c85c6bc659742194a2a1a819e4f5ba165624 (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * 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;
	GtkListStore *l;
	GHashTable *t;
};

enum {
	COL_INVALID = -1,
	COL_FREQUENCY = 0,
	COL_NAME
};

enum {
	PROP_0,
	PROP_NAME,
	PROP_MODEL,
	PROP_LAST
};

static GParamSpec *properties[PROP_LAST];

static inline gulong round_freq(gulong freq)
{
	const gulong round_to = 100000; /* 0.1 MHz */
	return ((freq + round_to / 2) / round_to) * round_to;
}

static inline gulong ffreq_to_freq(gfloat ffreq)
{
	return round_freq(ffreq * 1000000.0f);
}

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

static inline gpointer freq_to_pointer(gulong freq)
{
	return GUINT_TO_POINTER(freq);
}

static inline gulong pointer_to_freq(gpointer ptr)
{
	return GPOINTER_TO_UINT(ptr);
}

static inline gpointer ffreq_to_pointer(gfloat ffreq)
{
	return freq_to_pointer(ffreq_to_freq(ffreq));
}

static inline gfloat pointer_to_ffreq(gpointer ptr)
{
	return freq_to_ffreq(pointer_to_freq(ptr));
}

static void destroy_iter(gpointer data)
{
	g_slice_free(GtkTreeIter, data);
}

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

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;
}

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 ffreq = g_ascii_strtod(basename, NULL);
		gulong freq = ffreq_to_freq(ffreq);
		const gchar *name = gconf_value_get_string(gconf_entry_get_value(entry));
		GtkTreeIter *iter = g_slice_new(GtkTreeIter);
		gtk_list_store_insert_with_values(priv->l, iter, 0, 
			COL_FREQUENCY, freq,
			COL_NAME, name,
			COL_INVALID
			);
		g_hash_table_insert(priv->t, freq_to_pointer(freq), iter);
	}

	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 ffreq = g_ascii_strtod(basename, NULL);
	gulong freq = ffreq_to_freq(ffreq);
	gpointer t_data = g_hash_table_lookup(priv->t, freq_to_pointer(freq));
	GConfValue *value = gconf_entry_get_value(entry);

	if (value) {
		const gchar *name = gconf_value_get_string(gconf_entry_get_value(entry));
		if (t_data) {
			/* Modifying existing preset's name */
			GtkTreeIter *iter = (GtkTreeIter*)t_data;
			g_debug("Preset '%s' changed to '%s'\n", basename, name);
			gtk_list_store_set(priv->l, iter, COL_NAME, name, COL_INVALID);
		} else {
			GtkTreeIter *iter = g_slice_new(GtkTreeIter);
			g_debug("Preset '%s' set to '%s'\n", basename, name);
			gtk_list_store_insert_with_values(priv->l, iter, 0,
				COL_FREQUENCY, freq,
				COL_NAME, name,
				COL_INVALID
			);
			g_hash_table_insert(priv->t, freq_to_pointer(freq), iter);
		}
		
	} else {
		
		if (t_data) {
			GtkTreeIter *iter = (GtkTreeIter*)t_data;
			g_debug("Preset '%s' removed\n", basename);
			gtk_list_store_remove(priv->l, iter);
			g_hash_table_remove(priv->t, freq_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_value_set_string(value, self->priv->name);
		break;
	case PROP_MODEL:
		g_value_set_object(value, self->priv->l);
		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, destroy_iter);
}

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);

	priv->l = gtk_list_store_new(2, G_TYPE_ULONG, G_TYPE_STRING);
	gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(priv->l), 0, compare_freq,
		NULL, NULL);
	gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(priv->l), 0,
		GTK_SORT_ASCENDING);

	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;
	}
	if (priv->l) {
		g_object_unref(priv->l);
		priv->l = 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);
	param_spec = g_param_spec_object("model",
	                                 "Preset list model",
	                                 "A model containing this set of presets",
	                                 GTK_TYPE_TREE_MODEL,
	                                 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
	properties[PROP_MODEL] = param_spec;
	g_object_class_install_property(gobject_class, PROP_MODEL, 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_ffreq(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_ffreq(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;
}

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

	gpointer t_data = g_hash_table_lookup(priv->t, freq_to_pointer(freq));
	if (t_data) {
		GtkTreeIter *iter = (GtkTreeIter*) t_data;
		gchar *name;
		gtk_tree_model_get(GTK_TREE_MODEL(priv->l), iter,
			COL_NAME, &name, COL_INVALID);
		return name;
	} else {
		return NULL;
	}

	return NULL;
}