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
|
/*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#include <gtk/gtk.h>
#include <json-glib/json-glib.h>
#include <gio/gio.h>
#include <glib/gstdio.h>
#ifdef HAVE_GEOIP
#include <GeoIP.h>
#endif
#include "torrent.h"
#include "trg-client.h"
#include "tpeer.h"
#include "trg-peers-model.h"
#include "trg-model.h"
#include "util.h"
G_DEFINE_TYPE(TrgPeersModel, trg_peers_model, GTK_TYPE_LIST_STORE)
#define TRG_PEERS_MODEL_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), TRG_TYPE_PEERS_MODEL, TrgPeersModelPrivate))
#ifdef HAVE_GEOIP
typedef struct _TrgPeersModelPrivate TrgPeersModelPrivate;
struct _TrgPeersModelPrivate {
GeoIP *geoip;
};
#endif
static void
trg_peers_model_class_init(TrgPeersModelClass * klass G_GNUC_UNUSED)
{
#ifdef HAVE_GEOIP
g_type_class_add_private(klass, sizeof(TrgPeersModelPrivate));
#endif
}
gboolean
find_existing_peer_item_foreachfunc(GtkTreeModel * model,
GtkTreePath * path G_GNUC_UNUSED,
GtkTreeIter * iter, gpointer data)
{
struct peerAndIter *pi;
gchar *ip;
pi = (struct peerAndIter *) data;
gtk_tree_model_get(model, iter, PEERSCOL_IP, &ip, -1);
if (g_strcmp0(ip, pi->ip) == 0) {
pi->iter = *iter;
pi->found = TRUE;
}
g_free(ip);
return pi->found;
}
gboolean
find_existing_peer_item(TrgPeersModel * model, JsonObject * p,
GtkTreeIter * iter)
{
struct peerAndIter pi;
pi.ip = peer_get_address(p);
pi.found = FALSE;
gtk_tree_model_foreach(GTK_TREE_MODEL(model),
find_existing_peer_item_foreachfunc, &pi);
if (pi.found == TRUE)
*iter = pi.iter;
return pi.found;
}
static void resolved_dns_cb(GObject * source_object,
GAsyncResult * res, gpointer data)
{
GtkTreeRowReference *treeRef;
GtkTreeModel *model;
GtkTreePath *path;
treeRef = (GtkTreeRowReference *) data;
model = gtk_tree_row_reference_get_model(treeRef);
path = gtk_tree_row_reference_get_path(treeRef);
if (path != NULL) {
gchar *rdns =
g_resolver_lookup_by_address_finish(G_RESOLVER(source_object),
res, NULL);
if (rdns != NULL) {
GtkTreeIter iter;
if (gtk_tree_model_get_iter(model, &iter, path) == TRUE) {
gdk_threads_enter();
gtk_list_store_set(GTK_LIST_STORE(model),
&iter, PEERSCOL_HOST, rdns, -1);
gdk_threads_leave();
}
g_free(rdns);
}
gtk_tree_path_free(path);
}
gtk_tree_row_reference_free(treeRef);
}
void trg_peers_model_update(TrgPeersModel * model, gint64 updateSerial,
JsonObject * t, gint mode)
{
#ifdef HAVE_GEOIP
TrgPeersModelPrivate *priv = TRG_PEERS_MODEL_GET_PRIVATE(model);
#endif
JsonArray *peers;
GtkTreeIter peerIter;
GList *li;
gboolean isNew;
peers = torrent_get_peers(t);
if (mode == TORRENT_GET_MODE_FIRST)
gtk_list_store_clear(GTK_LIST_STORE(model));
for (li = json_array_get_elements(peers); li; li = g_list_next(li)) {
JsonObject *peer = json_node_get_object((JsonNode *) li->data);
const gchar *address = NULL, *flagStr;
#ifdef HAVE_GEOIP
const gchar *country = NULL;
#endif
if (mode == TORRENT_GET_MODE_FIRST
|| find_existing_peer_item(model, peer, &peerIter) == FALSE) {
gtk_list_store_append(GTK_LIST_STORE(model), &peerIter);
address = peer_get_address(peer);
#ifdef HAVE_GEOIP
if (priv->geoip != NULL)
country = GeoIP_country_name_by_addr(priv->geoip, address);
#endif
gtk_list_store_set(GTK_LIST_STORE(model), &peerIter,
PEERSCOL_ICON, GTK_STOCK_NETWORK,
PEERSCOL_IP, address,
#ifdef HAVE_GEOIP
PEERSCOL_COUNTRY,
country != NULL ? country : "",
#endif
PEERSCOL_CLIENT, peer_get_client_name(peer),
-1);
isNew = TRUE;
} else {
isNew = FALSE;
}
flagStr = peer_get_flagstr(peer);
gtk_list_store_set(GTK_LIST_STORE(model), &peerIter,
PEERSCOL_FLAGS, flagStr,
PEERSCOL_PROGRESS,
peer_get_progress(peer),
PEERSCOL_DOWNSPEED,
peer_get_rate_to_client(peer),
PEERSCOL_UPSPEED,
peer_get_rate_to_peer(peer),
PEERSCOL_UPDATESERIAL, updateSerial, -1);
if (isNew == TRUE) {
GtkTreePath *path;
GtkTreeRowReference *treeRef;
GInetAddress *inetAddr;
GResolver *resolver;
path =
gtk_tree_model_get_path(GTK_TREE_MODEL(model), &peerIter);
treeRef =
gtk_tree_row_reference_new(GTK_TREE_MODEL(model), path);
gtk_tree_path_free(path);
inetAddr = g_inet_address_new_from_string(address);
resolver = g_resolver_get_default();
g_resolver_lookup_by_address_async(resolver,
inetAddr, NULL,
resolved_dns_cb, treeRef);
g_object_unref(resolver);
g_object_unref(inetAddr);
}
}
if (mode != TORRENT_GET_MODE_FIRST)
trg_model_remove_removed(GTK_LIST_STORE(model),
PEERSCOL_UPDATESERIAL, updateSerial);
}
static void trg_peers_model_init(TrgPeersModel * self)
{
#ifdef HAVE_GEOIP
TrgPeersModelPrivate *priv = TRG_PEERS_MODEL_GET_PRIVATE(self);
#endif
GType column_types[PEERSCOL_COLUMNS];
column_types[PEERSCOL_ICON] = G_TYPE_STRING;
column_types[PEERSCOL_IP] = G_TYPE_STRING;
#ifdef HAVE_GEOIP
column_types[PEERSCOL_COUNTRY] = G_TYPE_STRING;
#endif
column_types[PEERSCOL_HOST] = G_TYPE_STRING;
column_types[PEERSCOL_FLAGS] = G_TYPE_STRING;
column_types[PEERSCOL_PROGRESS] = G_TYPE_DOUBLE;
column_types[PEERSCOL_DOWNSPEED] = G_TYPE_INT64;
column_types[PEERSCOL_UPSPEED] = G_TYPE_INT64;
column_types[PEERSCOL_CLIENT] = G_TYPE_STRING;
column_types[PEERSCOL_UPDATESERIAL] = G_TYPE_INT64;
gtk_list_store_set_column_types(GTK_LIST_STORE(self),
PEERSCOL_COLUMNS, column_types);
#ifdef HAVE_GEOIP
if (g_file_test(TRG_GEOIP_DATABASE, G_FILE_TEST_EXISTS) == TRUE)
priv->geoip =
GeoIP_open(TRG_GEOIP_DATABASE,
GEOIP_STANDARD | GEOIP_CHECK_CACHE);
#endif
}
TrgPeersModel *trg_peers_model_new()
{
GObject *obj = g_object_new(TRG_TYPE_PEERS_MODEL, NULL);
return TRG_PEERS_MODEL(obj);
}
|