summaryrefslogtreecommitdiff
path: root/src/trg-torrent-model.c
blob: 4c8668233863df9cb547c735b428bd974069eefd (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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
 * 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 <string.h>
#include <gtk/gtk.h>
#include <json-glib/json-glib.h>
#include <glib/gi18n.h>

#include "config.h"
#include "torrent.h"
#include "json.h"
#include "trg-torrent-model.h"
#include "protocol-constants.h"
#include "trg-model.h"
#include "util.h"

/* An extension of TrgModel (which is an extension of GtkListStore) which
 * updates from a JSON torrent-get response. It handles a number of different
 * update modes.
 *   1) The first update.
 *   2) A full update.
 *   3) An active-only update.
 *   4) Individual torrent updates.
 *
 * Other stuff it does.
 *   1) Populates a stats struct with speeds/state counts as it works through the
 *      response.
 *   2) Emits signals if something is added or removed. This is used by the state
 *      selector so it doesn't have to refresh itself on every update.
 *   3) Added or completed signals, for libnotify notifications.
 *   4) Maintains the torrent hash table (by ID).
 *      (and provide a lookup function which outputs an iter and/or JSON object.)
 *   5) If the download directory is new/changed, create a short version if there
 *      is one (duplicate it not) for the state selector to filter/populate against.
 *   6) Shorten the tracker announce URL.
 */

enum {
    TMODEL_TORRENT_COMPLETED,
    TMODEL_TORRENT_ADDED,
    TMODEL_UPDATE_FILTERS,
    TMODEL_SIGNAL_COUNT
};

#define PROP_REMOVE_IN_PROGRESS "remove-in-progress"

static guint signals[TMODEL_SIGNAL_COUNT] = { 0 };

G_DEFINE_TYPE(TrgTorrentModel, trg_torrent_model, GTK_TYPE_LIST_STORE)
#define TRG_TORRENT_MODEL_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), TRG_TYPE_TORRENT_MODEL, TrgTorrentModelPrivate))
typedef struct _TrgTorrentModelPrivate TrgTorrentModelPrivate;

struct _TrgTorrentModelPrivate {
    GHashTable *ht;
    GRegex *urlHostRegex;
    trg_torrent_model_update_stats stats;
};

static void trg_torrent_model_dispose(GObject * object) {
    TrgTorrentModelPrivate *priv = TRG_TORRENT_MODEL_GET_PRIVATE(object);
    g_hash_table_destroy(priv->ht);
    G_OBJECT_CLASS(trg_torrent_model_parent_class)->dispose(object);
}

static void
update_torrent_iter(TrgTorrentModel * model, TrgClient *tc, gint64 rpcv,
        gint64 serial, GtkTreeIter * iter, JsonObject * t,
        trg_torrent_model_update_stats * stats, gboolean *updateFilters);

static void trg_torrent_model_class_init(TrgTorrentModelClass * klass) {
    GObjectClass *object_class = G_OBJECT_CLASS(klass);

    g_type_class_add_private(klass, sizeof(TrgTorrentModelPrivate));
    object_class->dispose = trg_torrent_model_dispose;

    signals[TMODEL_TORRENT_COMPLETED] = g_signal_new("torrent-completed",
            G_TYPE_FROM_CLASS(object_class),
            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
            G_STRUCT_OFFSET(TrgTorrentModelClass,
                    torrent_completed), NULL, NULL,
            g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);

    signals[TMODEL_TORRENT_ADDED] = g_signal_new("torrent-added",
            G_TYPE_FROM_CLASS(object_class),
            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
            G_STRUCT_OFFSET(TrgTorrentModelClass,
                    torrent_added), NULL, NULL,
            g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);

    signals[TMODEL_UPDATE_FILTERS] = g_signal_new("update-filters",
            G_TYPE_FROM_CLASS(object_class),
            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
            G_STRUCT_OFFSET(TrgTorrentModelClass,
                    torrent_removed), NULL, NULL, g_cclosure_marshal_VOID__VOID,
            G_TYPE_NONE, 0);
}

trg_torrent_model_update_stats *trg_torrent_model_get_stats(TrgTorrentModel * model)
{
    TrgTorrentModelPrivate *priv = TRG_TORRENT_MODEL_GET_PRIVATE(model);
    return &(priv->stats);
}

static void trg_torrent_model_count_peers(TrgTorrentModel * model,
        GtkTreeIter * iter, JsonObject * t) {
    GList *trackersList = json_array_get_elements(torrent_get_tracker_stats(t));
    gint seeders = 0;
    gint leechers = 0;
    gint downloads = 0;
    GList *li;

    for (li = trackersList; li; li = g_list_next(li)) {
        JsonObject *tracker = json_node_get_object((JsonNode *) li->data);

        seeders += tracker_stats_get_seeder_count(tracker);
        leechers += tracker_stats_get_leecher_count(tracker);
        downloads += tracker_stats_get_download_count(tracker);
    }

    g_list_free(trackersList);

    gtk_list_store_set(GTK_LIST_STORE(model), iter, TORRENT_COLUMN_SEEDS,
            seeders, TORRENT_COLUMN_LEECHERS, leechers,
            TORRENT_COLUMN_DOWNLOADS, downloads, -1);
}

static void trg_torrent_model_ref_free(gpointer data) {
    GtkTreeRowReference *rr = (GtkTreeRowReference *) data;
    GtkTreeModel *model = gtk_tree_row_reference_get_model(rr);
    GtkTreePath *path = gtk_tree_row_reference_get_path(rr);
    if (path) {
        GtkTreeIter iter;
        JsonObject *json;
        if (gtk_tree_model_get_iter(model, &iter, path)) {
            gtk_tree_model_get(model, &iter, TORRENT_COLUMN_JSON, &json, -1);
            json_object_unref(json);
            g_object_set_data(G_OBJECT(model), PROP_REMOVE_IN_PROGRESS,
                    GINT_TO_POINTER(TRUE));
            gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
            g_object_set_data(G_OBJECT(model), PROP_REMOVE_IN_PROGRESS,
                    GINT_TO_POINTER(FALSE));
        }

        gtk_tree_path_free(path);
    }

    gtk_tree_row_reference_free(rr);
}

static void trg_torrent_model_init(TrgTorrentModel * self) {
    TrgTorrentModelPrivate *priv = TRG_TORRENT_MODEL_GET_PRIVATE(self);

    GType column_types[TORRENT_COLUMN_COLUMNS];

    column_types[TORRENT_COLUMN_ICON] = G_TYPE_STRING;
    column_types[TORRENT_COLUMN_NAME] = G_TYPE_STRING;
    column_types[TORRENT_COLUMN_SIZE] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_DONE] = G_TYPE_DOUBLE;
    column_types[TORRENT_COLUMN_STATUS] = G_TYPE_STRING;
    column_types[TORRENT_COLUMN_SEEDS] = G_TYPE_INT;
    column_types[TORRENT_COLUMN_LEECHERS] = G_TYPE_INT;
    column_types[TORRENT_COLUMN_DOWNLOADS] = G_TYPE_INT;
    column_types[TORRENT_COLUMN_DOWNSPEED] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_ADDED] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_UPSPEED] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_ETA] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_UPLOADED] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_DOWNLOADED] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_RATIO] = G_TYPE_DOUBLE;
    column_types[TORRENT_COLUMN_ID] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_JSON] = G_TYPE_POINTER;
    column_types[TORRENT_COLUMN_UPDATESERIAL] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_FLAGS] = G_TYPE_INT;
    column_types[TORRENT_COLUMN_DOWNLOADDIR] = G_TYPE_STRING;
    column_types[TORRENT_COLUMN_DOWNLOADDIR_SHORT] = G_TYPE_STRING;
    column_types[TORRENT_COLUMN_BANDWIDTH_PRIORITY] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_DONE_DATE] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_FROMPEX] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_FROMDHT] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_FROMTRACKERS] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_FROMLTEP] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_FROMRESUME] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_FROMINCOMING] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_PEER_SOURCES] = G_TYPE_STRING;
    column_types[TORRENT_COLUMN_PEERS_CONNECTED] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_PEERS_FROM_US] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_PEERS_TO_US] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_TRACKERHOST] = G_TYPE_STRING;
    column_types[TORRENT_COLUMN_QUEUE_POSITION] = G_TYPE_INT64;
    column_types[TORRENT_COLUMN_LASTACTIVE] = G_TYPE_INT64;

    gtk_list_store_set_column_types(GTK_LIST_STORE(self),
            TORRENT_COLUMN_COLUMNS, column_types);

    priv->ht = g_hash_table_new_full(g_int64_hash, g_int64_equal,
            (GDestroyNotify) g_free, trg_torrent_model_ref_free);

    g_object_set_data(G_OBJECT(self), PROP_REMOVE_IN_PROGRESS,
            GINT_TO_POINTER(FALSE));

    priv->urlHostRegex = trg_uri_host_regex_new();
}

gboolean trg_torrent_model_is_remove_in_progress(TrgTorrentModel * model) {
    return (gboolean) GPOINTER_TO_INT(g_object_get_data
            (G_OBJECT(model), PROP_REMOVE_IN_PROGRESS));
}

static gboolean trg_torrent_model_reload_dir_aliases_foreachfunc(
        GtkTreeModel * model, GtkTreePath * path G_GNUC_UNUSED,
        GtkTreeIter * iter, gpointer gdata) {
    gchar *downloadDir, *shortDownloadDir;

    gtk_tree_model_get(model, iter, TORRENT_COLUMN_DOWNLOADDIR, &downloadDir,
            -1);

    shortDownloadDir = shorten_download_dir((TrgClient*) gdata, downloadDir);

    gtk_list_store_set(GTK_LIST_STORE(model), iter,
            TORRENT_COLUMN_DOWNLOADDIR_SHORT, shortDownloadDir, -1);

    g_free(downloadDir);
    g_free(shortDownloadDir);

    return FALSE;
}

void trg_torrent_model_reload_dir_aliases(TrgClient *tc, GtkTreeModel *model)
{
    gtk_tree_model_foreach(model,
            trg_torrent_model_reload_dir_aliases_foreachfunc, tc);
}

static gboolean trg_torrent_model_stats_scan_foreachfunc(GtkTreeModel * model,
        GtkTreePath * path G_GNUC_UNUSED, GtkTreeIter * iter, gpointer gdata) {
    trg_torrent_model_update_stats *stats =
            (trg_torrent_model_update_stats *) gdata;
    guint flags;

    gtk_tree_model_get(model, iter, TORRENT_COLUMN_FLAGS, &flags, -1);

    if (flags & TORRENT_FLAG_SEEDING
        )
        stats->seeding++;
    else if (flags & TORRENT_FLAG_DOWNLOADING
        )
        stats->down++;
    else if (flags & TORRENT_FLAG_PAUSED
        )
        stats->paused++;

    if (flags & TORRENT_FLAG_ERROR
        )
        stats->error++;

    stats->count++;

    return FALSE;
}

void trg_torrent_model_remove_all(TrgTorrentModel *model) {
    TrgTorrentModelPrivate *priv = TRG_TORRENT_MODEL_GET_PRIVATE(model);
    g_hash_table_remove_all(priv->ht);
    gtk_list_store_clear(GTK_LIST_STORE(model));
}

gchar *shorten_download_dir(TrgClient *tc, const gchar *downloadDir) {
    TrgPrefs *prefs = trg_client_get_prefs(tc);
    JsonArray *labels = trg_prefs_get_array(prefs, TRG_PREFS_KEY_DESTINATIONS,
            TRG_PREFS_CONNECTION);
    JsonObject *session = trg_client_get_session(tc);
    const gchar *defaultDownloadDir = session_get_download_dir(session);
    gchar *shortDownloadDir = NULL;

    if (labels) {
        GList *labelsList = json_array_get_elements(labels);
        if (labelsList) {
            GList *li;
            for (li = labelsList; li; li = g_list_next(li)) {
                JsonObject *labelObj = json_node_get_object(
                        (JsonNode*) li->data);
                const gchar *labelDir = json_object_get_string_member(labelObj,
                        TRG_PREFS_KEY_DESTINATIONS_SUBKEY_DIR);
                if (!g_strcmp0(downloadDir, labelDir)) {
                    const gchar *labelLabel = json_object_get_string_member(
                            labelObj, TRG_PREFS_SUBKEY_LABEL);
                    shortDownloadDir = g_strdup(labelLabel);
                    break;
                }
            }
            g_list_free(labelsList);
        }
    }

    if (shortDownloadDir) {
        return shortDownloadDir;
    } else {
        if (!g_strcmp0(defaultDownloadDir, downloadDir))
            return g_strdup(_("Default"));

        if (g_str_has_prefix(downloadDir, defaultDownloadDir)) {
            int offset = strlen(defaultDownloadDir);
            if (*(downloadDir + offset) == '/')
                offset++;

            if (offset < strlen(downloadDir))
                return g_strdup(downloadDir + offset);
        }
    }

    return g_strdup(downloadDir);
}

static inline void update_torrent_iter(TrgTorrentModel * model, TrgClient *tc,
        gint64 rpcv, gint64 serial, GtkTreeIter * iter, JsonObject * t,
        trg_torrent_model_update_stats * stats, gboolean *updateFilters) {
    TrgTorrentModelPrivate *priv = TRG_TORRENT_MODEL_GET_PRIVATE(model);
    GtkListStore *ls = GTK_LIST_STORE(model);
    guint lastFlags, newFlags;
    JsonObject *lastJson, *pf;
    JsonArray *trackerStats;
    gchar *statusString, *statusIcon, *downloadDir;
    gint64 downRate, upRate, downloaded, uploaded, id, status, lpd;
    gchar *firstTrackerHost = NULL;
    gchar *peerSources = NULL;
    gchar *lastDownloadDir = NULL;

    downRate = torrent_get_rate_down(t);
    stats->downRateTotal += downRate;

    upRate = torrent_get_rate_up(t);
    stats->upRateTotal += upRate;

    uploaded = torrent_get_uploaded(t);
    downloaded = torrent_get_downloaded(t);

    downloadDir = (gchar*) torrent_get_download_dir(t);
    rm_trailing_slashes(downloadDir);

    id = torrent_get_id(t);
    status = torrent_get_status(t);
    newFlags = torrent_get_flags(t, rpcv, status, downRate, upRate);
    statusString = torrent_get_status_string(rpcv, status, newFlags);
    statusIcon = torrent_get_status_icon(rpcv, newFlags);
    pf = torrent_get_peersfrom(t);
    trackerStats = torrent_get_tracker_stats(t);

    gtk_tree_model_get(GTK_TREE_MODEL(model), iter, TORRENT_COLUMN_FLAGS,
            &lastFlags, TORRENT_COLUMN_JSON, &lastJson,
            TORRENT_COLUMN_DOWNLOADDIR, &lastDownloadDir, -1);

    json_object_ref(t);

    if (json_array_get_length(trackerStats) > 0) {
        JsonObject *firstTracker = json_array_get_object_element(trackerStats,
                0);
        firstTrackerHost = trg_gregex_get_first(priv->urlHostRegex,
                tracker_stats_get_host(firstTracker));
    }

    lpd = peerfrom_get_lpd(pf);
    if (newFlags & TORRENT_FLAG_ACTIVE) {
        if (lpd >= 0) {
            peerSources = g_strdup_printf(
                    "%ld / %ld / %ld / %ld / %ld / %ld / %ld",
                    peerfrom_get_trackers(pf), peerfrom_get_incoming(pf),
                    peerfrom_get_ltep(pf), peerfrom_get_dht(pf),
                    peerfrom_get_pex(pf), lpd, peerfrom_get_resume(pf));
        } else {
            peerSources = g_strdup_printf(
                    "%ld / %ld / %ld / %ld / %ld / N/A / %ld",
                    peerfrom_get_trackers(pf), peerfrom_get_incoming(pf),
                    peerfrom_get_ltep(pf), peerfrom_get_dht(pf),
                    peerfrom_get_pex(pf), peerfrom_get_resume(pf));
        }
    }

#ifdef DEBUG
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_ICON, statusIcon, -1);
    gtk_list_store_set(ls, iter,
            TORRENT_COLUMN_NAME, torrent_get_name(t), -1);
    gtk_list_store_set(ls, iter,
            TORRENT_COLUMN_SIZE, torrent_get_size(t), -1);
    gtk_list_store_set(ls, iter,
            TORRENT_COLUMN_DONE, (newFlags & TORRENT_FLAG_CHECKING) ?
            torrent_get_recheck_progress(t)
            : torrent_get_percent_done(t),
            -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_STATUS, statusString, -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_DOWNSPEED, downRate, -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_FLAGS, newFlags, -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_UPSPEED, upRate, -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_ETA,
            torrent_get_eta(t), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_UPLOADED, uploaded, -1);
    gtk_list_store_set(ls, iter,
            TORRENT_COLUMN_DOWNLOADED, downloaded, -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_RATIO,
            uploaded > 0
            && downloaded >
            0 ? (double) uploaded / (double) downloaded : 0,
            -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_ID, id, -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_JSON, t, -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_UPDATESERIAL, serial, -1);
    gtk_list_store_set(ls, iter,
            TORRENT_COLUMN_ADDED, torrent_get_added_date(t),
            -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_DOWNLOADDIR,
            downloadDir, -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_BANDWIDTH_PRIORITY,
            torrent_get_bandwidth_priority(t), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_DONE_DATE,
            torrent_get_done_date(t), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_FROMPEX,
            peerfrom_get_pex(pf), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_FROMDHT,
            peerfrom_get_dht(pf), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_FROMTRACKERS,
            peerfrom_get_trackers(pf), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_FROMLTEP,
            peerfrom_get_ltep(pf), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_FROMRESUME,
            peerfrom_get_resume(pf), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_FROMINCOMING,
            peerfrom_get_incoming(pf), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_PEER_SOURCES,
            peerSources, -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_PEERS_CONNECTED,
            torrent_get_peers_connected(t), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_PEERS_TO_US,
            torrent_get_peers_sending_to_us(t), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_PEERS_FROM_US,
            torrent_get_peers_getting_from_us(t), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_TRACKERHOST,
            firstTrackerHost ? firstTrackerHost : "", -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_QUEUE_POSITION, torrent_get_queue_position(t), -1);
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_LASTACTIVE, torrent_get_activity_date(t), -1);
#else
    gtk_list_store_set(ls, iter, TORRENT_COLUMN_ICON, statusIcon,
            TORRENT_COLUMN_ADDED, torrent_get_added_date(t),
            TORRENT_COLUMN_DONE_DATE, torrent_get_done_date(t),
            TORRENT_COLUMN_NAME, torrent_get_name(t), TORRENT_COLUMN_SIZE,
            torrent_get_size(t), TORRENT_COLUMN_DONE,
            (newFlags & TORRENT_FLAG_CHECKING) ?
            torrent_get_recheck_progress(t)
            : torrent_get_percent_done(t),
            TORRENT_COLUMN_STATUS, statusString,
            TORRENT_COLUMN_DOWNSPEED, downRate,
            TORRENT_COLUMN_FLAGS, newFlags,
            TORRENT_COLUMN_UPSPEED, upRate,
            TORRENT_COLUMN_ETA, torrent_get_eta(t),
            TORRENT_COLUMN_UPLOADED, uploaded,
            TORRENT_COLUMN_DOWNLOADED, downloaded,
            TORRENT_COLUMN_FROMPEX, peerfrom_get_pex(pf),
            TORRENT_COLUMN_FROMDHT, peerfrom_get_dht(pf),
            TORRENT_COLUMN_FROMTRACKERS, peerfrom_get_trackers(pf),
            TORRENT_COLUMN_FROMLTEP, peerfrom_get_ltep(pf),
            TORRENT_COLUMN_FROMRESUME, peerfrom_get_resume(pf),
            TORRENT_COLUMN_FROMINCOMING, peerfrom_get_incoming(pf),
            TORRENT_COLUMN_PEER_SOURCES, peerSources,
            TORRENT_COLUMN_PEERS_CONNECTED, torrent_get_peers_connected(t),
            TORRENT_COLUMN_PEERS_TO_US, torrent_get_peers_sending_to_us(t),
            TORRENT_COLUMN_PEERS_FROM_US, torrent_get_peers_getting_from_us(t),
            TORRENT_COLUMN_QUEUE_POSITION, torrent_get_queue_position(t),
            TORRENT_COLUMN_LASTACTIVE, torrent_get_activity_date(t),
            TORRENT_COLUMN_RATIO,
            uploaded >
            0
            && downloaded >
            0 ? (double) uploaded / (double) downloaded : 0,
            TORRENT_COLUMN_DOWNLOADDIR,
            downloadDir,
            TORRENT_COLUMN_BANDWIDTH_PRIORITY,
            torrent_get_bandwidth_priority(t),
            TORRENT_COLUMN_ID, id, TORRENT_COLUMN_JSON, t,
            TORRENT_COLUMN_TRACKERHOST,
            firstTrackerHost ? firstTrackerHost : "",
            TORRENT_COLUMN_UPDATESERIAL, serial, -1);
#endif

    if (!lastDownloadDir || g_strcmp0(downloadDir, lastDownloadDir)) {
        gchar *shortDownloadDir = shorten_download_dir(tc, downloadDir);
        gtk_list_store_set(ls, iter, TORRENT_COLUMN_DOWNLOADDIR_SHORT,
                shortDownloadDir, -1);
        g_free(shortDownloadDir);
        *updateFilters = TRUE;
    }

    if (lastJson)
        json_object_unref(lastJson);

    if ((lastFlags & TORRENT_FLAG_DOWNLOADING)
            && (newFlags & TORRENT_FLAG_COMPLETE))
        g_signal_emit(model, signals[TMODEL_TORRENT_COMPLETED], 0, iter);

    trg_torrent_model_count_peers(model, iter, t);

    if (firstTrackerHost)
        g_free(firstTrackerHost);

    if (peerSources)
        g_free(peerSources);

    g_free(lastDownloadDir);
    g_free(statusString);
    g_free(statusIcon);
}

TrgTorrentModel *trg_torrent_model_new(void) {
    return g_object_new(TRG_TYPE_TORRENT_MODEL, NULL);
}

struct TrgModelRemoveData {
    GList *toRemove;
    gint64 currentSerial;
};

GHashTable *get_torrent_table(TrgTorrentModel * model) {
    TrgTorrentModelPrivate *priv = TRG_TORRENT_MODEL_GET_PRIVATE(model);
    return priv->ht;
}

gboolean trg_model_find_removed_foreachfunc(GtkTreeModel * model,
        GtkTreePath * path G_GNUC_UNUSED, GtkTreeIter * iter, gpointer gdata) {
    struct TrgModelRemoveData *args = (struct TrgModelRemoveData *) gdata;
    gint64 rowSerial;

    gtk_tree_model_get(model, iter, TORRENT_COLUMN_UPDATESERIAL, &rowSerial,
            -1);

    if (rowSerial != args->currentSerial) {
        gint64 *id = g_new(gint64, 1);
        gtk_tree_model_get(model, iter, TORRENT_COLUMN_ID, id, -1);
        args->toRemove = g_list_append(args->toRemove, id);
    }

    return FALSE;
}

GList *trg_torrent_model_find_removed(GtkTreeModel * model,
        gint64 currentSerial) {
    struct TrgModelRemoveData args;
    args.toRemove = NULL;
    args.currentSerial = currentSerial;

    gtk_tree_model_foreach(GTK_TREE_MODEL(model),
            trg_model_find_removed_foreachfunc, &args);

    return args.toRemove;
}

gboolean get_torrent_data(GHashTable * table, gint64 id, JsonObject ** t,
        GtkTreeIter * out_iter) {
    gpointer result = g_hash_table_lookup(table, &id);
    gboolean found = FALSE;

    if (result) {
        GtkTreeRowReference *rr = (GtkTreeRowReference *) result;
        GtkTreePath *path = gtk_tree_row_reference_get_path(rr);
        GtkTreeIter iter;
        if (path) {
            GtkTreeModel *model = gtk_tree_row_reference_get_model(rr);
            gtk_tree_model_get_iter(model, &iter, path);
            if (out_iter)
                *out_iter = iter;
            if (t)
                gtk_tree_model_get(model, &iter, TORRENT_COLUMN_JSON, t, -1);
            found = TRUE;
            gtk_tree_path_free(path);
        }
    }

    return found;
}

trg_torrent_model_update_stats* trg_torrent_model_update(TrgTorrentModel * model, TrgClient * tc,
        JsonObject * response,
        gint mode) {
    TrgTorrentModelPrivate *priv = TRG_TORRENT_MODEL_GET_PRIVATE(model);

    GList *torrentList;
    JsonObject *args, *t;
    GList *li;
    gint64 id;
    gint64 *idCopy;
    JsonArray *removedTorrents;
    GtkTreeIter iter;
    GtkTreePath *path;
    GtkTreeRowReference *rr;
    gpointer *result;
    gboolean updateFilters = FALSE;

    gint64 rpcv = trg_client_get_rpc_version(tc);

    args = get_arguments(response);
    torrentList = json_array_get_elements(get_torrents(args));

    memset(&(priv->stats), 0, sizeof(trg_torrent_model_update_stats));

    for (li = torrentList; li; li = g_list_next(li)) {
        t = json_node_get_object((JsonNode *) li->data);
        id = torrent_get_id(t);

        result =
                mode == TORRENT_GET_MODE_FIRST ? NULL :
                        g_hash_table_lookup(priv->ht, &id);

        if (!result) {
            gtk_list_store_append(GTK_LIST_STORE(model), &iter);

            update_torrent_iter(model, tc, rpcv, trg_client_get_serial(tc),
                    &iter, t, &(priv->stats), &updateFilters);

            path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), &iter);
            rr = gtk_tree_row_reference_new(GTK_TREE_MODEL(model), path);
            idCopy = g_new(gint64, 1);
            *idCopy = id;
            g_hash_table_insert(priv->ht, idCopy, rr);
            gtk_tree_path_free(path);
            if (mode != TORRENT_GET_MODE_FIRST)
                g_signal_emit(model, signals[TMODEL_TORRENT_ADDED], 0, &iter);
        } else {
            path = gtk_tree_row_reference_get_path(
                    (GtkTreeRowReference *) result);
            if (path) {
                if (gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter,
                        path)) {
                    update_torrent_iter(model, tc, rpcv,
                            trg_client_get_serial(tc), &iter, t, &(priv->stats), &updateFilters);
                }
                gtk_tree_path_free(path);
            }
        }
    }

    g_list_free(torrentList);

    if (mode == TORRENT_GET_MODE_UPDATE) {
        GList *hitlist = trg_torrent_model_find_removed(GTK_TREE_MODEL(model),
                trg_client_get_serial(tc));
        if (hitlist) {
            for (li = hitlist; li; li = g_list_next(li)) {
                g_hash_table_remove(priv->ht, li->data);
                g_free(li->data);
            }
            updateFilters = TRUE;
            g_list_free(hitlist);
        }
    } else if (mode > TORRENT_GET_MODE_FIRST) {
        removedTorrents = get_torrents_removed(args);
        if (removedTorrents) {
            GList *hitlist = json_array_get_elements(removedTorrents);
            for (li = hitlist; li; li = g_list_next(li)) {
                id = json_node_get_int((JsonNode *) li->data);
                g_hash_table_remove(priv->ht, &id);
                updateFilters = TRUE;
            }
            g_list_free(hitlist);
        }
    }

    if (updateFilters)
        g_signal_emit(model, signals[TMODEL_UPDATE_FILTERS], 0);

    gtk_tree_model_foreach(GTK_TREE_MODEL(model),
            trg_torrent_model_stats_scan_foreachfunc, &(priv->stats));

    return &(priv->stats);
}