summaryrefslogtreecommitdiff
path: root/src/trg-files-model.c
blob: 5d4cf0af3a01fb2e95b2594a7ed0db53ec7b0430 (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
/*
 * 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 <gtk/gtk.h>
#include <json-glib/json-glib.h>

#include "trg-files-model.h"
#include "trg-client.h"
#include "torrent.h"
#include "util.h"

#include "trg-files-model.h"

G_DEFINE_TYPE(TrgFilesModel, trg_files_model, GTK_TYPE_LIST_STORE)
#define TRG_FILES_MODEL_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), TRG_TYPE_FILES_MODEL, TrgFilesModelPrivate))
typedef struct _TrgFilesModelPrivate TrgFilesModelPrivate;

struct _TrgFilesModelPrivate {
    gint64 torrentId;
    JsonArray *wanted;
    JsonArray *priorities;
    gboolean accept;
};

static void trg_files_model_iter_new(TrgFilesModel * model,
                                     GtkTreeIter * iter, JsonObject * file,
                                     int id)
{
    gchar *mimetype;

    gtk_list_store_append(GTK_LIST_STORE(model), iter);
    gtk_list_store_set(GTK_LIST_STORE(model), iter,
                       FILESCOL_NAME, file_get_name(file),
                       FILESCOL_SIZE, file_get_length(file),
                       FILESCOL_ID, id, -1);

    mimetype = g_content_type_guess(file_get_name(file), NULL, 0, NULL);
    if (mimetype) {
        GIcon *icon = g_content_type_get_icon (mimetype);
        if (icon) {
            gtk_list_store_set(GTK_LIST_STORE(model), iter,
                               FILESCOL_ICON, icon, -1);
            g_object_unref(icon);
        }
    }

    g_free(mimetype);
}

void trg_files_model_set_accept(TrgFilesModel * model, gboolean accept)
{
    TrgFilesModelPrivate *priv = TRG_FILES_MODEL_GET_PRIVATE(model);
    priv->accept = accept;
}

static void
trg_files_model_iter_update(TrgFilesModel * model,
                            GtkTreeIter * filesIter, JsonObject * file,
                            JsonArray * wantedArray,
                            JsonArray * prioritiesArray, int id)
{
    TrgFilesModelPrivate *priv = TRG_FILES_MODEL_GET_PRIVATE(model);

    gboolean wanted = json_node_get_int(json_array_get_element
                                        (wantedArray, id)) == 1;
    gint64 priority =
        json_node_get_int(json_array_get_element(prioritiesArray, id));
    gdouble progress = file_get_progress(file);

    gtk_list_store_set(GTK_LIST_STORE(model), filesIter,
                       FILESCOL_PROGRESS, progress, -1);

    if (priv->accept) {
        gtk_list_store_set(GTK_LIST_STORE(model), filesIter,
                           FILESCOL_WANTED,
                           wanted ? GTK_STOCK_APPLY : GTK_STOCK_CANCEL,
                           FILESCOL_PRIORITY, priority, -1);
    }
}

static void trg_files_model_class_init(TrgFilesModelClass * klass)
{
    g_type_class_add_private(klass, sizeof(TrgFilesModelPrivate));
}

static void trg_files_model_init(TrgFilesModel * self)
{
    TrgFilesModelPrivate *priv = TRG_FILES_MODEL_GET_PRIVATE(self);
    GType column_types[FILESCOL_COLUMNS];

    priv->accept = TRUE;

    column_types[FILESCOL_ICON] = G_TYPE_ICON;
    column_types[FILESCOL_NAME] = G_TYPE_STRING;
    column_types[FILESCOL_SIZE] = G_TYPE_INT64;
    column_types[FILESCOL_PROGRESS] = G_TYPE_DOUBLE;
    column_types[FILESCOL_ID] = G_TYPE_INT;
    column_types[FILESCOL_WANTED] = G_TYPE_STRING;
    column_types[FILESCOL_PRIORITY] = G_TYPE_INT64;

    gtk_list_store_set_column_types(GTK_LIST_STORE(self),
                                    FILESCOL_COLUMNS, column_types);
}

gboolean
trg_files_model_update_foreach(GtkListStore * model,
                               GtkTreePath * path G_GNUC_UNUSED,
                               GtkTreeIter * iter, GList * files)
{
    TrgFilesModelPrivate *priv = TRG_FILES_MODEL_GET_PRIVATE(model);
    JsonObject *file;
    gint id;

    gtk_tree_model_get(GTK_TREE_MODEL(model), iter, FILESCOL_ID, &id, -1);

    file = json_node_get_object(g_list_nth_data(files, id));
    trg_files_model_iter_update(TRG_FILES_MODEL(model), iter, file,
                                priv->wanted, priv->priorities, id);

    return FALSE;
}

void
trg_files_model_update(TrgFilesModel * model, gint64 updateSerial,
                       JsonObject * t, gint mode)
{
    TrgFilesModelPrivate *priv = TRG_FILES_MODEL_GET_PRIVATE(model);
    GList *filesList, *li;
    GtkTreeIter filesIter;
    JsonObject *file;
    gint j = 0;

    priv->torrentId = torrent_get_id(t);
    priv->priorities = torrent_get_priorities(t);
    priv->wanted = torrent_get_wanted(t);

    filesList = json_array_get_elements(torrent_get_files(t));

    if (mode == TORRENT_GET_MODE_FIRST) {
        gtk_list_store_clear(GTK_LIST_STORE(model));
        priv->accept = TRUE;
        for (li = filesList; li; li = g_list_next(li)) {
            file = json_node_get_object((JsonNode *) li->data);

            trg_files_model_iter_new(model, &filesIter, file, j);
            trg_files_model_iter_update(model, &filesIter,
                                        file, priv->wanted,
                                        priv->priorities, j);
            j++;
        }
    } else {
        gint n_existing = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(model), NULL);
        guint n_updates = g_list_length(filesList);
        gtk_tree_model_foreach(GTK_TREE_MODEL(model),
                               (GtkTreeModelForeachFunc)
                               trg_files_model_update_foreach, filesList);
        if (n_updates > n_existing) {
            gint n_new = n_updates - n_existing;
            for (j = n_updates - n_new; j < n_updates; j++)
            {
                file = json_node_get_object(g_list_nth_data(filesList, j));
                trg_files_model_iter_new(model, &filesIter, file, j);
                trg_files_model_iter_update(model, &filesIter,
                                            file, priv->wanted,
                                            priv->priorities, j);
            }
        }
    }

    g_list_free(filesList);
}

gint64 trg_files_model_get_torrent_id(TrgFilesModel * model)
{
    TrgFilesModelPrivate *priv = TRG_FILES_MODEL_GET_PRIVATE(model);
    return priv->torrentId;
}

TrgFilesModel *trg_files_model_new(void)
{
    return g_object_new(TRG_TYPE_FILES_MODEL, NULL);
}