diff options
author | Alan Fitton <ajf@eth0.org.uk> | 2011-10-21 23:46:04 +0000 |
---|---|---|
committer | Alan Fitton <ajf@eth0.org.uk> | 2011-10-21 23:46:04 +0000 |
commit | 83cc5f391198cc5cb0e65885191d7f92cea3c204 (patch) | |
tree | 823030d2db1f5b63a0b7d01edaa83a24c1186046 /src/trg-destination-combo.c | |
parent | 34c56c7db7c57f5db0184489d01866902e992300 (diff) |
issue 153
Diffstat (limited to 'src/trg-destination-combo.c')
-rw-r--r-- | src/trg-destination-combo.c | 136 |
1 files changed, 73 insertions, 63 deletions
diff --git a/src/trg-destination-combo.c b/src/trg-destination-combo.c index 5fe32b0..75df78c 100644 --- a/src/trg-destination-combo.c +++ b/src/trg-destination-combo.c @@ -26,7 +26,7 @@ #include "util.h" G_DEFINE_TYPE(TrgDestinationCombo, trg_destination_combo, - GTK_TYPE_COMBO_BOX) + GTK_TYPE_COMBO_BOX) #define TRG_DESTINATION_COMBO_GET_PRIVATE(o) \ (G_TYPE_INSTANCE_GET_PRIVATE ((o), TRG_TYPE_DESTINATION_COMBO, TrgDestinationComboPrivate)) typedef struct _TrgDestinationComboPrivate TrgDestinationComboPrivate; @@ -36,16 +36,13 @@ struct _TrgDestinationComboPrivate { }; enum { - PROP_0, - PROP_CLIENT + PROP_0, PROP_CLIENT }; -static void -trg_destination_combo_get_property(GObject * object, guint property_id, - GValue * value, GParamSpec * pspec) -{ +static void trg_destination_combo_get_property(GObject * object, + guint property_id, GValue * value, GParamSpec * pspec) { TrgDestinationComboPrivate *priv = - TRG_DESTINATION_COMBO_GET_PRIVATE(object); + TRG_DESTINATION_COMBO_GET_PRIVATE(object); switch (property_id) { case PROP_CLIENT: g_value_set_pointer(value, priv->client); @@ -55,13 +52,10 @@ trg_destination_combo_get_property(GObject * object, guint property_id, } } -static void -trg_destination_combo_set_property(GObject * object, guint property_id, - const GValue * value, - GParamSpec * pspec) -{ +static void trg_destination_combo_set_property(GObject * object, + guint property_id, const GValue * value, GParamSpec * pspec) { TrgDestinationComboPrivate *priv = - TRG_DESTINATION_COMBO_GET_PRIVATE(object); + TRG_DESTINATION_COMBO_GET_PRIVATE(object); switch (property_id) { case PROP_CLIENT: priv->client = g_value_get_pointer(value); @@ -71,55 +65,76 @@ trg_destination_combo_set_property(GObject * object, guint property_id, } } -static gboolean g_slist_str_set_add(GSList ** list, const gchar * string, gint pos) -{ +static gboolean g_slist_str_set_add(GSList ** list, const gchar * string, + gint pos) { GSList *li; for (li = *list; li; li = g_slist_next(li)) if (!g_strcmp0((gchar *) li->data, string)) return FALSE; if (pos < 0) - *list = g_slist_insert_sorted(*list, (gpointer)string, (GCompareFunc)g_strcmp0); + *list = g_slist_insert_sorted(*list, (gpointer) string, + (GCompareFunc) g_strcmp0); else - *list = g_slist_insert(*list, (gpointer)string, pos); + *list = g_slist_insert(*list, (gpointer) string, pos); return TRUE; } static GObject *trg_destination_combo_constructor(GType type, - guint - n_construct_properties, - GObjectConstructParam - * construct_params) -{ + guint n_construct_properties, GObjectConstructParam * construct_params) { GObject *object = G_OBJECT_CLASS - (trg_destination_combo_parent_class)->constructor(type, - n_construct_properties, - construct_params); + (trg_destination_combo_parent_class)->constructor(type, + n_construct_properties, construct_params); TrgDestinationComboPrivate *priv = - TRG_DESTINATION_COMBO_GET_PRIVATE(object); + TRG_DESTINATION_COMBO_GET_PRIVATE(object); TrgClient *client = priv->client; + TrgPrefs *prefs = trg_client_get_prefs(client); GSList *dirs = NULL; GSList *sli; GList *li; - GList *torrentItemRefs; + GList *list; GtkCellRenderer *renderer; GtkTreeRowReference *rr; GtkTreeModel *model; GtkTreePath *path; GtkListStore *comboModel; + JsonArray *saved_destinations; JsonObject *t; + gchar *defaultDownDir; - gchar *defaultDownDir = - g_strdup(session_get_download_dir(trg_client_get_session(client))); + comboModel = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING); + + defaultDownDir = g_strdup( + session_get_download_dir(trg_client_get_session(client))); rm_trailing_slashes(defaultDownDir); - comboModel = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING); + saved_destinations = trg_prefs_get_array(prefs, TRG_PREFS_KEY_DESTINATIONS, + TRG_PREFS_CONNECTION); + if (saved_destinations) { + list = json_array_get_elements(saved_destinations); + if (list) { + for (li = list; li; li = g_list_next(li)) { + JsonObject *obj = json_node_get_object((JsonNode*) li->data); + gtk_list_store_insert_with_values( + comboModel, + NULL, + INT_MAX, + 0, + json_object_get_string_member(obj, + TRG_PREFS_SUBKEY_LABEL), + 1, + json_object_get_string_member(obj, + TRG_PREFS_KEY_DESTINATIONS_SUBKEY_DIR), -1); + } + g_list_free(list); + } + } trg_client_updatelock(client); - torrentItemRefs = g_hash_table_get_values(trg_client_get_torrent_table(client)); - for (li = torrentItemRefs; li; li = g_list_next(li)) { + list = g_hash_table_get_values(trg_client_get_torrent_table(client)); + for (li = list; li; li = g_list_next(li)) { rr = (GtkTreeRowReference *) li->data; model = gtk_tree_row_reference_get_model(rr); path = gtk_tree_row_reference_get_path(rr); @@ -130,8 +145,8 @@ static GObject *trg_destination_combo_constructor(GType type, if (gtk_tree_model_get_iter(model, &iter, path)) { gchar *dd; gtk_tree_model_get(model, &iter, TORRENT_COLUMN_JSON, &t, - TORRENT_COLUMN_DOWNLOADDIR, &dd, - -1);; + TORRENT_COLUMN_DOWNLOADDIR, &dd, -1); + ; if (dd && g_strcmp0(dd, defaultDownDir)) g_slist_str_set_add(&dirs, dd, -1); else @@ -144,41 +159,38 @@ static GObject *trg_destination_combo_constructor(GType type, trg_client_updateunlock(client); - g_list_free(torrentItemRefs); + g_list_free(list); if (defaultDownDir) g_slist_str_set_add(&dirs, defaultDownDir, 0); for (sli = dirs; sli; sli = g_slist_next(sli)) gtk_list_store_insert_with_values(comboModel, NULL, INT_MAX, 0, - (gchar *) sli->data, - 1, (gchar *) sli->data, -1); + (gchar *) sli->data, 1, (gchar *) sli->data, -1); - gtk_combo_box_set_model(GTK_COMBO_BOX(object), - GTK_TREE_MODEL(comboModel)); + gtk_combo_box_set_model(GTK_COMBO_BOX(object), GTK_TREE_MODEL(comboModel)); renderer = gtk_cell_renderer_text_new(); gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(object), renderer, TRUE); - gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(object), renderer, "text", 0, NULL); + gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(object), renderer, "text", + 0, NULL); g_object_unref(comboModel); - g_slist_foreach(dirs, (GFunc)g_free, NULL); + g_slist_foreach(dirs, (GFunc) g_free, NULL); g_slist_free(dirs); return object; } -gchar *trg_destination_combo_get_dir(TrgDestinationCombo *combo) -{ +gchar *trg_destination_combo_get_dir(TrgDestinationCombo *combo) { GtkTreeIter iter; gchar *value; gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter); - gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(combo)), &iter, 1, &value, -1); + gtk_tree_model_get(gtk_combo_box_get_model(GTK_COMBO_BOX(combo)), &iter, 1, + &value, -1); return value; } -static void -trg_destination_combo_class_init(TrgDestinationComboClass * klass) -{ +static void trg_destination_combo_class_init(TrgDestinationComboClass * klass) { GObjectClass *object_class = G_OBJECT_CLASS(klass); g_type_class_add_private(klass, sizeof(TrgDestinationComboPrivate)); @@ -187,24 +199,22 @@ trg_destination_combo_class_init(TrgDestinationComboClass * klass) object_class->set_property = trg_destination_combo_set_property; object_class->constructor = trg_destination_combo_constructor; - g_object_class_install_property(object_class, - PROP_CLIENT, - g_param_spec_pointer - ("trg-client", "TClient", - "Client", - G_PARAM_READWRITE | - G_PARAM_CONSTRUCT_ONLY | - G_PARAM_STATIC_NAME | - G_PARAM_STATIC_NICK | - G_PARAM_STATIC_BLURB)); + g_object_class_install_property( + object_class, + PROP_CLIENT, + g_param_spec_pointer( + "trg-client", + "TClient", + "Client", + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY + | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK + | G_PARAM_STATIC_BLURB)); } -static void trg_destination_combo_init(TrgDestinationCombo * self) -{ +static void trg_destination_combo_init(TrgDestinationCombo * self) { } -GtkWidget *trg_destination_combo_new(TrgClient * client) -{ +GtkWidget *trg_destination_combo_new(TrgClient * client) { return GTK_WIDGET(g_object_new(TRG_TYPE_DESTINATION_COMBO, - "trg-client", client, NULL)); + "trg-client", client, NULL)); } |