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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/*
* transmission-remote-gtk - Transmission RPC client for GTK
* Copyright (C) 2011 Alan Fitton
* 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 02110-1301 USA.
*/
#include <glib.h>
#include <glib/gstdio.h>
#include <json-glib/json-glib.h>
#include <glib/gi18n.h>
#include <glib/gprintf.h>
#include "util.h"
#include "torrent.h"
#include "trg-client.h"
#include "trg-prefs.h"
/* I replaced GConf with this custom configuration backend for a few reasons.
* 1) Better windows support. No dependency on DBus.
* 2) We're including a JSON parser/writer anyway.
* 3) The GConf API is pretty verbose sometimes, working with lists is awkward.
* 4) Easily switch between profiles, and scope config to either profiles or global.
* 5) Transparently use configurables from the current connection, not current profile.
*/
G_DEFINE_TYPE (TrgPrefs, trg_prefs, G_TYPE_OBJECT)
#define GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), TRG_TYPE_PREFS, TrgPrefsPrivate))
typedef struct _TrgPrefsPrivate TrgPrefsPrivate;
struct _TrgPrefsPrivate {
JsonObject *defaultsObj;
JsonNode *user;
JsonObject *userObj;
JsonObject *connectionObj;
JsonObject *profile;
gchar *file;
};
enum {
PREF_CHANGE,
PREF_PROFILE_CHANGE,
PREFS_SIGNAL_COUNT
};
static guint signals[PREFS_SIGNAL_COUNT] = { 0 };
void trg_prefs_profile_change_emit_signal(TrgPrefs *p)
{
g_signal_emit(p, signals[PREF_PROFILE_CHANGE], 0);
}
void trg_prefs_changed_emit_signal(TrgPrefs *p, const gchar *key)
{
g_signal_emit(p, signals[PREF_CHANGE], 0, key);
}
static void trg_prefs_get_property(GObject *object, guint property_id,
GValue *value, GParamSpec *pspec) {
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void trg_prefs_set_property(GObject *object, guint property_id,
const GValue *value, GParamSpec *pspec) {
switch (property_id) {
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void trg_prefs_dispose(GObject *object) {
G_OBJECT_CLASS (trg_prefs_parent_class)->dispose(object);
}
static void trg_prefs_create_defaults(TrgPrefs *p) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
priv->defaultsObj = json_object_new();
trg_prefs_add_default_string(p, TRG_PREFS_KEY_PROFILE_NAME,
_(TRG_PROFILE_NAME_DEFAULT));
trg_prefs_add_default_int(p, TRG_PREFS_KEY_PORT, 9091);
trg_prefs_add_default_int(p, TRG_PREFS_KEY_UPDATE_INTERVAL, TRG_INTERVAL_DEFAULT);
trg_prefs_add_default_int(p, TRG_PREFS_KEY_MINUPDATE_INTERVAL, TRG_INTERVAL_DEFAULT);
trg_prefs_add_default_int(p, TRG_PREFS_ACTIVEONLY_FULLSYNC_EVERY, 2);
trg_prefs_add_default_int(p, TRG_PREFS_KEY_STATES_PANED_POS, 120);
trg_prefs_add_default_bool_true(p, TRG_PREFS_KEY_FILTER_DIRS);
trg_prefs_add_default_bool_true(p, TRG_PREFS_KEY_FILTER_TRACKERS);
trg_prefs_add_default_bool_true(p, TRG_PREFS_KEY_SHOW_GRAPH);
trg_prefs_add_default_bool_true(p, TRG_PREFS_KEY_ADD_OPTIONS_DIALOG);
trg_prefs_add_default_bool_true(p, TRG_PREFS_KEY_SHOW_STATE_SELECTOR);
trg_prefs_add_default_bool_true(p, TRG_PREFS_KEY_SHOW_NOTEBOOK);
}
static GObject *trg_prefs_constructor(GType type, guint n_construct_properties,
GObjectConstructParam * construct_params) {
GObject *object;
TrgPrefsPrivate *priv;
object = G_OBJECT_CLASS
(trg_prefs_parent_class)->constructor(type, n_construct_properties,
construct_params);
priv = GET_PRIVATE(object);
trg_prefs_create_defaults(TRG_PREFS(object));
priv->file = g_build_filename(g_get_user_config_dir(),
g_get_application_name(), TRG_PREFS_FILENAME, NULL);
return object;
}
static void trg_prefs_class_init(TrgPrefsClass *klass) {
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private(klass, sizeof(TrgPrefsPrivate));
object_class->get_property = trg_prefs_get_property;
object_class->set_property = trg_prefs_set_property;
object_class->dispose = trg_prefs_dispose;
object_class->constructor = trg_prefs_constructor;
signals[PREF_CHANGE] =
g_signal_new("pref-changed",
G_TYPE_FROM_CLASS(object_class),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET(TrgPrefsClass,
pref_changed), NULL,
NULL, g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE, 1, G_TYPE_POINTER);
signals[PREF_PROFILE_CHANGE] =
g_signal_new("pref-profile-changed",
G_TYPE_FROM_CLASS(object_class),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET(TrgPrefsClass,
pref_changed), NULL,
NULL, g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void trg_prefs_init(TrgPrefs *self) {
}
TrgPrefs*
trg_prefs_new(void) {
return g_object_new(TRG_TYPE_PREFS, NULL);
}
static JsonObject *trg_prefs_new_profile_object() {
JsonObject *obj = json_object_new();
return obj;
}
void trg_prefs_add_default_int(TrgPrefs *p, const gchar *key, int value) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
json_object_set_int_member(priv->defaultsObj, key, value);
}
void trg_prefs_add_default_string(TrgPrefs *p, const gchar *key, gchar *value) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
json_object_set_string_member(priv->defaultsObj, key, value);
}
void trg_prefs_add_default_double(TrgPrefs *p, const gchar *key, double value) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
json_object_set_double_member(priv->defaultsObj, key, value);
}
/* Not much point adding a default of FALSE, as that's the fallback */
void trg_prefs_add_default_bool_true(TrgPrefs *p, const gchar *key) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
json_object_set_boolean_member(priv->defaultsObj, key, TRUE);
}
gint trg_prefs_get_profile_id(TrgPrefs *p) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
return (gint) json_object_get_int_member(priv->userObj,
TRG_PREFS_KEY_PROFILE_ID);
}
static JsonNode *trg_prefs_get_value_inner(JsonObject *obj, const gchar *key, int type, int flags)
{
if (json_object_has_member(obj, key)) {
if ((flags & TRG_PREFS_REPLACENODE))
json_object_remove_member(obj, key);
else
return json_object_get_member(obj, key);
}
if ((flags & TRG_PREFS_NEWNODE) || (flags & TRG_PREFS_REPLACENODE)) {
JsonNode *newNode = json_node_new(type);
json_object_set_member(obj, key, newNode);
return newNode;
}
return NULL;
}
JsonNode *trg_prefs_get_value(TrgPrefs *p, const gchar *key, int type, int flags) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
JsonNode *res;
if (priv->profile && (flags & TRG_PREFS_PROFILE)) {
if ((res = trg_prefs_get_value_inner(priv->profile, key, type, flags)))
return res;
} else if (priv->connectionObj && (flags & TRG_PREFS_CONNECTION)) {
if ((res = trg_prefs_get_value_inner(priv->connectionObj, key, type, flags)))
return res;
} else {
if ((res = trg_prefs_get_value_inner(priv->userObj, key, type, flags)))
return res;
}
if (priv->defaultsObj && json_object_has_member(priv->defaultsObj, key))
return json_object_get_member(priv->defaultsObj, key);
return NULL;
}
void trg_prefs_set_connection(TrgPrefs *p, JsonObject *profile)
{
TrgPrefsPrivate *priv = GET_PRIVATE(p);
if (priv->connectionObj)
json_object_unref(priv->connectionObj);
if (profile)
json_object_ref(profile);
priv->connectionObj = profile;
}
gchar *trg_prefs_get_string(TrgPrefs *p, const gchar *key, int flags) {
JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_VALUE, flags);
if (node)
return g_strdup(json_node_get_string(node));
else
return NULL;
}
JsonArray *trg_prefs_get_array(TrgPrefs *p, const gchar *key, int flags)
{
JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_ARRAY, flags);
if (node)
return json_node_get_array(node);
else
return NULL;
}
gint64 trg_prefs_get_int(TrgPrefs *p, const gchar *key, int flags) {
JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_VALUE, flags);
if (node)
return json_node_get_int(node);
else
return 0;
}
gdouble trg_prefs_get_double(TrgPrefs *p, const gchar *key, int flags) {
JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_VALUE, flags);
if (node)
return json_node_get_double(node);
else
return 0.0;
}
gboolean trg_prefs_get_bool(TrgPrefs *p, const gchar *key, int flags) {
JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_VALUE, flags);
if (node)
return json_node_get_boolean(node);
else
return FALSE;
}
void trg_prefs_set_int(TrgPrefs *p, const gchar *key, int value, int flags) {
JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_VALUE, flags | TRG_PREFS_NEWNODE);
json_node_set_int(node, (gint64) value);
trg_prefs_changed_emit_signal(p, key);
}
void trg_prefs_set_string(TrgPrefs *p, const gchar *key, const gchar *value,
int flags) {
JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_VALUE, flags | TRG_PREFS_NEWNODE);
json_node_set_string(node, value);
trg_prefs_changed_emit_signal(p, key);
}
void trg_prefs_set_profile(TrgPrefs *p, JsonObject *profile) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
GList *profiles = json_array_get_elements(trg_prefs_get_profiles(p));
priv->profile = profile;
GList *li;
gint i = 0;
for (li = profiles; li; li = g_list_next(li)) {
if (json_node_get_object((JsonNode*) li->data) == profile) {
trg_prefs_set_int(p, TRG_PREFS_KEY_PROFILE_ID, i, TRG_PREFS_GLOBAL);
break;
}
i++;
}
g_list_free(profiles);
trg_prefs_changed_emit_signal(p, NULL);
trg_prefs_profile_change_emit_signal(p);
}
JsonObject *trg_prefs_new_profile(TrgPrefs *p) {
JsonArray *profiles = trg_prefs_get_profiles(p);
JsonObject *newp = trg_prefs_new_profile_object();
json_array_add_object_element(profiles, newp);
return newp;
}
void trg_prefs_del_profile(TrgPrefs *p, JsonObject *profile) {
JsonArray *profiles = trg_prefs_get_profiles(p);
GList *profilesList = json_array_get_elements(profiles);
GList *li;
JsonNode *node;
int i = 0;
for (li = profilesList; li; li = g_list_next(li)) {
node = (JsonNode*) li->data;
if (profile == (gpointer) json_node_get_object(node)) {
json_array_remove_element(profiles, i);
break;
}
i++;
}
g_list_free(profilesList);
trg_prefs_profile_change_emit_signal(p);
}
JsonObject* trg_prefs_get_profile(TrgPrefs *p) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
return priv->profile;
}
JsonObject* trg_prefs_get_connection(TrgPrefs *p) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
return priv->connectionObj;
}
JsonArray* trg_prefs_get_profiles(TrgPrefs *p) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
return json_object_get_array_member(priv->userObj, TRG_PREFS_KEY_PROFILES);
}
void trg_prefs_set_double(TrgPrefs *p, const gchar *key, gdouble value, int flags) {
JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_VALUE, flags | TRG_PREFS_NEWNODE);
json_node_set_double(node, value);
trg_prefs_changed_emit_signal(p, key);
}
void trg_prefs_set_bool(TrgPrefs *p, const gchar *key, gboolean value, int flags) {
JsonNode *node = trg_prefs_get_value(p, key, JSON_NODE_VALUE, flags | TRG_PREFS_NEWNODE);
json_node_set_boolean(node, value);
trg_prefs_changed_emit_signal(p, key);
}
gboolean trg_prefs_save(TrgPrefs *p) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
JsonGenerator *gen = json_generator_new();
gchar *dirName;
gboolean success = TRUE;
gboolean isNew = TRUE;
dirName = g_path_get_dirname(priv->file);
if (!g_file_test(dirName, G_FILE_TEST_IS_DIR)) {
success = g_mkdir_with_parents(dirName, TRG_PREFS_DEFAULT_DIR_MODE)
== 0;
} else if (g_file_test(priv->file, G_FILE_TEST_IS_REGULAR)) {
isNew = FALSE;
}
g_free(dirName);
if (!success) {
g_error("Problem creating parent directory (permissions?) for: %s\n", priv->file);
return success;
}
g_object_set(G_OBJECT(gen), "pretty", TRUE, NULL);
json_generator_set_root(gen, priv->user);
success = json_generator_to_file(gen, priv->file, NULL);
if (!success)
g_error("Problem writing configuration file (permissions?) to: %s", priv->file);
else if (isNew)
g_chmod(priv->file, 384);
g_object_unref(gen);
return success;
}
JsonObject *trg_prefs_get_root(TrgPrefs *p) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
return priv->userObj;
}
void trg_prefs_empty_init(TrgPrefs *p) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
priv->user = json_node_new(JSON_NODE_OBJECT);
priv->userObj = json_object_new();
json_node_take_object(priv->user, priv->userObj);
priv->profile = trg_prefs_new_profile_object();
JsonArray *profiles = json_array_new();
json_array_add_object_element(profiles, priv->profile);
json_object_set_array_member(priv->userObj, TRG_PREFS_KEY_PROFILES,
profiles);
json_object_set_int_member(priv->userObj, TRG_PREFS_KEY_PROFILE_ID, 0);
}
void trg_prefs_load(TrgPrefs *p) {
TrgPrefsPrivate *priv = GET_PRIVATE(p);
JsonParser *parser = json_parser_new();
JsonNode *root;
guint n_profiles;
JsonArray *profiles;
gboolean parsed = json_parser_load_from_file(parser, priv->file, NULL);
if (!parsed) {
trg_prefs_empty_init(p);
g_object_unref(parser);
return;
}
root = json_parser_get_root(parser);
if (root) {
priv->user = json_node_copy(root);
priv->userObj = json_node_get_object(priv->user);
}
g_object_unref(parser);
if (!root) {
trg_prefs_empty_init(p);
return;
}
if (!json_object_has_member(priv->userObj, TRG_PREFS_KEY_PROFILES)) {
profiles = json_array_new();
json_object_set_array_member(priv->userObj, TRG_PREFS_KEY_PROFILES,
profiles);
} else {
profiles = json_object_get_array_member(priv->userObj,
TRG_PREFS_KEY_PROFILES);
}
n_profiles = json_array_get_length(profiles);
if (n_profiles < 1) {
priv->profile = trg_prefs_new_profile_object();
json_array_add_object_element(profiles, priv->profile);
trg_prefs_set_int(p, TRG_PREFS_KEY_PROFILE_ID, 0, TRG_PREFS_GLOBAL);
} else {
gint profile_id = trg_prefs_get_int(p, TRG_PREFS_KEY_PROFILE_ID,
TRG_PREFS_GLOBAL);
if (profile_id >= n_profiles)
trg_prefs_set_int(p, TRG_PREFS_KEY_PROFILE_ID, profile_id = 0,
TRG_PREFS_GLOBAL);
priv->profile = json_array_get_object_element(profiles, profile_id);
}
}
guint trg_prefs_get_add_flags(TrgPrefs *p)
{
guint flags = 0x00;
if (trg_prefs_get_bool(p, TRG_PREFS_KEY_START_PAUSED, TRG_PREFS_GLOBAL))
flags |= TORRENT_ADD_FLAG_PAUSED;
if (trg_prefs_get_bool(p, TRG_PREFS_KEY_DELETE_LOCAL_TORRENT, TRG_PREFS_GLOBAL))
flags |= TORRENT_ADD_FLAG_DELETE;
return flags;
}
|