summaryrefslogtreecommitdiff
path: root/src/trg-trackers-model.c
blob: 2e7fb9b8a4f0ffde39ce493bd69f5f1e7b2d2a35 (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
/*
 * transmission-remote-gtk - A GTK RPC client to Transmission
 * Copyright (C) 2011-2013  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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gtk/gtk.h>
#include <json-glib/json-glib.h>

#include "config.h"
#include "torrent.h"
#include "trg-client.h"
#include "trg-model.h"
#include "trg-trackers-model.h"

G_DEFINE_TYPE(TrgTrackersModel, trg_trackers_model, GTK_TYPE_LIST_STORE)
#define TRG_TRACKERS_MODEL_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), TRG_TYPE_TRACKERS_MODEL, TrgTrackersModelPrivate))
typedef struct _TrgTrackersModelPrivate TrgTrackersModelPrivate;

struct _TrgTrackersModelPrivate {
    gint64 torrentId;
    gint64 accept;
};

void trg_trackers_model_set_no_selection(TrgTrackersModel * model)
{
    TrgTrackersModelPrivate *priv = TRG_TRACKERS_MODEL_GET_PRIVATE(model);
    priv->torrentId = -1;
}

gint64 trg_trackers_model_get_torrent_id(TrgTrackersModel * model)
{
    TrgTrackersModelPrivate *priv = TRG_TRACKERS_MODEL_GET_PRIVATE(model);
    return priv->torrentId;
}

void
trg_trackers_model_update(TrgTrackersModel * model,
                          gint64 updateSerial, JsonObject * t, gint mode)
{
    TrgTrackersModelPrivate *priv = TRG_TRACKERS_MODEL_GET_PRIVATE(model);

    GtkTreeIter trackIter;
    JsonObject *tracker;
    gint64 trackerId;
    GList *trackers, *li;
    const gchar *announce;
    const gchar *scrape;

    if (mode == TORRENT_GET_MODE_FIRST) {
        gtk_list_store_clear(GTK_LIST_STORE(model));
        priv->torrentId = torrent_get_id(t);
        priv->accept = TRUE;
    } else if (!priv->accept) {
        return;
    }

    trackers = json_array_get_elements(torrent_get_tracker_stats(t));

    for (li = trackers; li; li = g_list_next(li)) {
        tracker = json_node_get_object((JsonNode *) li->data);
        trackerId = tracker_stats_get_id(tracker);
        announce = tracker_stats_get_announce(tracker);
        scrape = tracker_stats_get_scrape(tracker);

        if (mode == TORRENT_GET_MODE_FIRST
            || find_existing_model_item(GTK_TREE_MODEL(model),
                                        TRACKERCOL_ID, trackerId,
                                        &trackIter) == FALSE)
            gtk_list_store_append(GTK_LIST_STORE(model), &trackIter);

#ifdef DEBUG
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_ICON, "network-workgroup", -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_TIER,
                           tracker_stats_get_tier(tracker), -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_ANNOUNCE, announce, -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_SCRAPE, scrape, -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_ID, trackerId, -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_UPDATESERIAL, updateSerial, -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_LAST_ANNOUNCE_RESULT,
                           tracker_stats_get_announce_result(tracker), -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_LAST_ANNOUNCE_TIME,
                           tracker_stats_get_last_announce_time(tracker),
                           -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_LAST_SCRAPE_TIME,
                           tracker_stats_get_last_scrape_time(tracker),
                           -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_HOST,
                           tracker_stats_get_host(tracker), -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_LAST_ANNOUNCE_PEER_COUNT,
                           tracker_stats_get_last_announce_peer_count
                           (tracker), -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_LEECHERCOUNT,
                           tracker_stats_get_leecher_count(tracker), -1);
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_SEEDERCOUNT,
                           tracker_stats_get_seeder_count(tracker), -1);
#else
        gtk_list_store_set(GTK_LIST_STORE(model), &trackIter,
                           TRACKERCOL_ICON, "network-workgroup",
                           TRACKERCOL_ID, trackerId,
                           TRACKERCOL_UPDATESERIAL, updateSerial,
                           TRACKERCOL_TIER,
                           tracker_stats_get_tier(tracker),
                           TRACKERCOL_ANNOUNCE, announce,
                           TRACKERCOL_SCRAPE, scrape, TRACKERCOL_HOST,
                           tracker_stats_get_host(tracker),
                           TRACKERCOL_LAST_ANNOUNCE_RESULT,
                           tracker_stats_get_announce_result(tracker),
                           TRACKERCOL_LAST_ANNOUNCE_TIME,
                           tracker_stats_get_last_announce_time(tracker),
                           TRACKERCOL_LAST_SCRAPE_TIME,
                           tracker_stats_get_last_scrape_time(tracker),
                           TRACKERCOL_LAST_ANNOUNCE_PEER_COUNT,
                           tracker_stats_get_last_announce_peer_count
                           (tracker), TRACKERCOL_LEECHERCOUNT,
                           tracker_stats_get_leecher_count(tracker),
                           TRACKERCOL_SEEDERCOUNT,
                           tracker_stats_get_seeder_count(tracker), -1);
#endif
    }

    g_list_free(trackers);
    trg_model_remove_removed(GTK_LIST_STORE(model),
                             TRACKERCOL_UPDATESERIAL, updateSerial);
}

static void trg_trackers_model_class_init(TrgTrackersModelClass * klass)
{
    g_type_class_add_private(klass, sizeof(TrgTrackersModelPrivate));
}

void
trg_trackers_model_set_accept(TrgTrackersModel * model, gboolean accept)
{
    TrgTrackersModelPrivate *priv = TRG_TRACKERS_MODEL_GET_PRIVATE(model);
    priv->accept = accept;
}

static void trg_trackers_model_init(TrgTrackersModel * self)
{
    TrgTrackersModelPrivate *priv = TRG_TRACKERS_MODEL_GET_PRIVATE(self);

    GType column_types[TRACKERCOL_COLUMNS];

    column_types[TRACKERCOL_ICON] = G_TYPE_STRING;
    column_types[TRACKERCOL_TIER] = G_TYPE_INT64;
    column_types[TRACKERCOL_ANNOUNCE] = G_TYPE_STRING;
    column_types[TRACKERCOL_SCRAPE] = G_TYPE_STRING;
    column_types[TRACKERCOL_ID] = G_TYPE_INT64;
    column_types[TRACKERCOL_LAST_ANNOUNCE_PEER_COUNT] = G_TYPE_INT64;
    column_types[TRACKERCOL_LAST_ANNOUNCE_TIME] = G_TYPE_INT64;
    column_types[TRACKERCOL_LAST_SCRAPE_TIME] = G_TYPE_INT64;
    column_types[TRACKERCOL_SEEDERCOUNT] = G_TYPE_INT64;
    column_types[TRACKERCOL_LEECHERCOUNT] = G_TYPE_INT64;
    column_types[TRACKERCOL_HOST] = G_TYPE_STRING;
    column_types[TRACKERCOL_LAST_ANNOUNCE_RESULT] = G_TYPE_STRING;
    column_types[TRACKERCOL_UPDATESERIAL] = G_TYPE_INT64;

    priv->accept = TRUE;
    priv->torrentId = -1;

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

TrgTrackersModel *trg_trackers_model_new(void)
{
    return g_object_new(TRG_TYPE_TRACKERS_MODEL, NULL);
}