summaryrefslogtreecommitdiff
path: root/src/trg-json-widgets.c
blob: ac4fba37c47515fb47146572b9593c2ed2ec6f23 (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
/*
 * transmission-remote-gtk - A GTK RPC client to Transmission
 * 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 <gtk/gtk.h>
#include <json-glib/json-glib.h>

#include "trg-json-widgets.h"
#include "util.h"

/* Functions for creating widgets that load/save their state from/to a JSON
 * object. This is used by the torrent properties and remote settings dialogs.
 * The pattern here is farily similar to that used in local configuration,
 * the widget creation functions take a list as an argument, which gets a
 * trg_json_widget_desc appended to it. This contains the key, and the function
 * pointers for load/save.
 */

void trg_json_widgets_save(GList * list, JsonObject * out)
{
    GList *li;
    for (li = list; li; li = g_list_next(li)) {
        trg_json_widget_desc *wd = (trg_json_widget_desc *) li->data;
        wd->saveFunc(wd->widget, out, wd->key);
    }
}

void trg_json_widget_desc_free(trg_json_widget_desc * wd)
{
    g_free(wd->key);
    g_free(wd);
}

void trg_json_widget_desc_list_free(GList * list)
{
    GList *li;
    for (li = list; li; li = g_list_next(li))
        trg_json_widget_desc_free((trg_json_widget_desc *) li->data);

    g_list_free(list);
}

void toggle_active_arg_is_sensitive(GtkToggleButton * b, gpointer data)
{
    gtk_widget_set_sensitive(GTK_WIDGET(data),
                             gtk_toggle_button_get_active(b));
}

GtkWidget *trg_json_widget_check_new(GList ** wl, JsonObject * obj,
                                     const gchar * key,
                                     const gchar * label,
                                     GtkWidget * toggleDep)
{
    GtkWidget *w = gtk_check_button_new_with_mnemonic(label);
    trg_json_widget_desc *wd = g_new0(trg_json_widget_desc, 1);

    wd->saveFunc = trg_json_widget_check_save;
    wd->key = g_strdup(key);
    wd->widget = w;

    if (toggleDep) {
        gtk_widget_set_sensitive(w,
                                 gtk_toggle_button_get_active
                                 (GTK_TOGGLE_BUTTON(toggleDep)));
        g_signal_connect(G_OBJECT(toggleDep), "toggled",
                         G_CALLBACK(toggle_active_arg_is_sensitive), w);
    }

    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w),
                                 json_object_get_boolean_member(obj, key));

    *wl = g_list_append(*wl, wd);

    return w;
}

GtkWidget *trg_json_widget_entry_new(GList ** wl, JsonObject * obj,
                                     const gchar * key,
                                     GtkWidget * toggleDep)
{
    GtkWidget *w = gtk_entry_new();
    trg_json_widget_desc *wd = g_new0(trg_json_widget_desc, 1);

    wd->saveFunc = trg_json_widget_entry_save;
    wd->key = g_strdup(key);
    wd->widget = w;

    if (toggleDep) {
        gtk_widget_set_sensitive(w,
                                 gtk_toggle_button_get_active
                                 (GTK_TOGGLE_BUTTON(toggleDep)));
        g_signal_connect(G_OBJECT(toggleDep), "toggled",
                         G_CALLBACK(toggle_active_arg_is_sensitive), w);
    }

    gtk_entry_set_text(GTK_ENTRY(w),
                       json_object_get_string_member(obj, key));

    *wl = g_list_append(*wl, wd);

    return w;
}

static GtkWidget *trg_json_widget_spin_common_new(GList ** wl,
                                                  JsonObject * obj,
                                                  const gchar * key,
                                                  GtkWidget * toggleDep,
                                                  trg_json_widget_spin_type
                                                  type, gint min, gint max,
                                                  gdouble step)
{
    GtkWidget *w = gtk_spin_button_new_with_range(min, max, step);
    trg_json_widget_desc *wd = g_new0(trg_json_widget_desc, 1);
    JsonNode *node = json_object_get_member(obj, key);

    if (type == TRG_JSON_WIDGET_SPIN_DOUBLE)
        wd->saveFunc = trg_json_widget_spin_save_double;
    else
        wd->saveFunc = trg_json_widget_spin_save_int;

    wd->key = g_strdup(key);
    wd->widget = w;

    if (toggleDep) {
        gtk_widget_set_sensitive(w,
                                 gtk_toggle_button_get_active
                                 (GTK_TOGGLE_BUTTON(toggleDep)));
        g_signal_connect(G_OBJECT(toggleDep), "toggled",
                         G_CALLBACK(toggle_active_arg_is_sensitive), w);
    }

    gtk_spin_button_set_value(GTK_SPIN_BUTTON(w),
                              json_node_really_get_double(node));

    *wl = g_list_append(*wl, wd);

    return w;
}

GtkWidget *trg_json_widget_spin_new_int(GList ** wl, JsonObject * obj,
                                        const gchar * key,
                                        GtkWidget * toggleDep, gint min,
                                        gint max, gint step)
{
    return trg_json_widget_spin_common_new(wl, obj, key, toggleDep,
                                           TRG_JSON_WIDGET_SPIN_INT, min,
                                           max, (gdouble) step);
}

GtkWidget *trg_json_widget_spin_new_double(GList ** wl, JsonObject * obj,
                                           const gchar * key,
                                           GtkWidget * toggleDep, gint min,
                                           gint max, gdouble step)
{
    return trg_json_widget_spin_common_new(wl, obj, key, toggleDep,
                                           TRG_JSON_WIDGET_SPIN_DOUBLE,
                                           min, max, step);
}

void trg_json_widget_check_save(GtkWidget * widget, JsonObject * obj,
                                gchar * key)
{
    gboolean active =
        gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
    json_object_set_boolean_member(obj, key, active);
}

void trg_json_widget_entry_save(GtkWidget * widget, JsonObject * obj,
                                gchar * key)
{
    json_object_set_string_member(obj, key,
                                  gtk_entry_get_text(GTK_ENTRY(widget)));
}

void trg_json_widget_spin_save_int(GtkWidget * widget, JsonObject * obj,
                                   gchar * key)
{
    json_object_set_int_member(obj, key, (gint)
                               gtk_spin_button_get_value(GTK_SPIN_BUTTON
                                                         (widget)));
}

void trg_json_widget_spin_save_double(GtkWidget * widget, JsonObject * obj,
                                      gchar * key)
{
    json_object_set_double_member(obj, key,
                                  gtk_spin_button_get_value(GTK_SPIN_BUTTON
                                                            (widget)));
}