transmission/gtk/main.c

1039 lines
31 KiB
C
Raw Normal View History

2006-01-12 18:32:29 +00:00
/*
Copyright (c) 2005-2006 Joshua Elsasser. All rights reserved.
2006-01-12 18:32:29 +00:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
2006-01-12 18:40:47 +00:00
#include <sys/param.h>
2006-01-12 18:32:29 +00:00
#include <assert.h>
2006-01-12 18:40:47 +00:00
#include <errno.h>
2006-01-12 18:32:29 +00:00
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "defines.h"
2006-01-12 18:32:29 +00:00
#include "conf.h"
#include "dialogs.h"
2006-01-12 18:32:29 +00:00
#include "transmission.h"
#include "trcellrenderertorrent.h"
2006-01-12 18:32:29 +00:00
#include "util.h"
2006-01-12 18:47:30 +00:00
#define TRACKER_EXIT_TIMEOUT 5
2006-01-12 18:32:29 +00:00
struct cbdata {
tr_handle_t *tr;
2006-01-12 18:40:47 +00:00
GtkWindow *wind;
2006-01-12 18:32:29 +00:00
GtkListStore *model;
GtkTreeView *view;
2006-01-12 18:47:30 +00:00
GtkStatusbar *bar;
GtkWidget **buttons;
2006-01-12 18:32:29 +00:00
guint timer;
gboolean prefsopen;
2006-01-12 18:32:29 +00:00
};
struct exitdata {
struct cbdata *cbdata;
time_t started;
guint timer;
};
struct pieces {
char p[120];
};
void
maketypes(void);
gpointer
tr_pieces_copy(gpointer);
void
tr_pieces_free(gpointer);
void
makewind(GtkWidget *wind, tr_handle_t *tr, GList *saved);
gboolean
winclose(GtkWidget *widget, GdkEvent *event, gpointer gdata);
gboolean
exitcheck(gpointer gdata);
2006-01-12 19:04:05 +00:00
void
stoptransmission(void *tr);
2006-01-12 18:32:29 +00:00
GtkWidget *
makewind_toolbar(struct cbdata *data);
GtkWidget *
makewind_list(struct cbdata *data);
2006-01-12 18:56:00 +00:00
static void
stylekludge(GObject *obj, GParamSpec *spec, gpointer gdata);
2006-01-12 18:47:30 +00:00
void
fixbuttons(GtkTreeSelection *sel, gpointer gdata);
void
dfname(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model,
GtkTreeIter *iter, gpointer gdata);
void
dfprog(GtkTreeViewColumn *col, GtkCellRenderer *rend, GtkTreeModel *model,
GtkTreeIter *iter, gpointer gdata);
2006-01-12 18:32:29 +00:00
gboolean
updatemodel(gpointer gdata);
gboolean
listclick(GtkWidget *widget, GdkEventButton *event, gpointer gdata);
gboolean
2006-01-12 18:47:30 +00:00
listpopup(GtkWidget *widget, gpointer gdata);
2006-01-12 18:32:29 +00:00
void
2006-01-12 19:00:58 +00:00
dopopupmenu(GdkEventButton *event, struct cbdata *data,
GList *ids, int status);
void
killmenu(GtkWidget *menu, gpointer *gdata SHUTUP);
2006-01-12 18:32:29 +00:00
void
actionclick(GtkWidget *widget, gpointer gdata);
2006-01-12 19:04:05 +00:00
gint
intrevcmp(gconstpointer a, gconstpointer b);
2006-01-12 19:00:58 +00:00
void
doubleclick(GtkWidget *widget, GtkTreePath *path, GtkTreeViewColumn *col,
gpointer gdata);
2006-01-12 18:32:29 +00:00
gboolean
2006-01-12 18:40:47 +00:00
addtorrent(tr_handle_t *tr, GtkWindow *parentwind, const char *torrent,
const char *dir, gboolean paused, GList **errs);
2006-01-12 18:32:29 +00:00
void
2006-01-12 18:55:27 +00:00
addedtorrents(void *vdata);
2006-01-12 18:32:29 +00:00
gboolean
2006-01-12 18:40:47 +00:00
savetorrents(tr_handle_t *tr, GtkWindow *wind, int count, tr_stat_t *stat);
2006-01-12 19:00:58 +00:00
void
orstatus(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
gpointer gdata);
void
makeidlist(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter,
gpointer gdata);
2006-01-12 18:32:29 +00:00
#define TR_TYPE_PIECES_NAME "tr-type-pieces"
#define TR_TYPE_PIECES ((const GType)tr_type_pieces)
#define TR_PIECES(ptr) ((struct pieces*)ptr)
GType tr_type_pieces;
#define LIST_ACTION "torrent-list-action"
2006-01-12 18:40:47 +00:00
enum listact { ACT_OPEN, ACT_START, ACT_STOP, ACT_DELETE, ACT_INFO, ACT_PREF };
2006-01-12 18:32:29 +00:00
#define LIST_ACTION_FROM "torrent-list-action-from"
enum listfrom { FROM_BUTTON, FROM_POPUP };
2006-01-12 19:00:58 +00:00
#define LIST_INDEX "torrent-list-indexes"
#define LIST_MENU_WIDGET "torrent-list-popup-menu-widget"
2006-01-12 18:32:29 +00:00
2006-01-12 18:47:30 +00:00
struct { const gchar *name; const gchar *id; enum listact act; gboolean nomenu;
int avail; const char *ttext; const char *tpriv; }
2006-01-12 18:32:29 +00:00
actionitems[] = {
{N_("Add"), GTK_STOCK_ADD, ACT_OPEN, FALSE, 0,
N_("Add a new torrent"), "XXX"},
{N_("Start"), GTK_STOCK_EXECUTE, ACT_START, FALSE,
2006-01-12 18:47:30 +00:00
(TR_STATUS_STOPPING | TR_STATUS_PAUSE),
N_("Start a torrent that is not running"), "XXX"},
{N_("Stop"), GTK_STOCK_STOP, ACT_STOP, FALSE,
2006-01-12 18:47:30 +00:00
~(TR_STATUS_STOPPING | TR_STATUS_PAUSE),
N_("Stop a torrent that is running"), "XXX"},
{N_("Remove"), GTK_STOCK_REMOVE, ACT_DELETE, FALSE, ~0,
N_("Remove a torrent"), "XXX"},
{N_("Properties"), GTK_STOCK_PROPERTIES, ACT_INFO, FALSE, ~0,
N_("Show additional information about a torrent"), "XXX"},
{N_("Preferences"), GTK_STOCK_PREFERENCES, ACT_PREF, TRUE, 0,
N_("Customize application behavior"), "XXX"},
2006-01-12 18:32:29 +00:00
};
#define CBDATA_PTR "callback-data-pointer"
int
main(int argc, char **argv) {
GtkWidget *mainwind, *preferr, *stateerr;
char *err;
tr_handle_t *tr;
GList *saved;
2006-01-12 18:40:47 +00:00
const char *pref;
long intval;
2006-01-12 18:32:29 +00:00
gtk_init(&argc, &argv);
bindtextdomain("transmission-gtk", LOCALEDIR);
textdomain("transmission-gtk");
g_set_application_name(_("Transmission"));
2006-01-12 18:32:29 +00:00
tr = tr_init();
2006-01-12 19:04:05 +00:00
setuphandlers(stoptransmission, tr);
gtk_rc_parse_string(
"style \"transmission-standard\" {\n"
" GtkDialog::action-area-border = 6\n"
" GtkDialog::button-spacing = 12\n"
" GtkDialog::content-area-border = 6\n"
"}\n"
"widget \"TransmissionDialog\" style \"transmission-standard\"\n");
2006-01-12 18:58:57 +00:00
if(cf_init(tr_getPrefsDirectory(), &err)) {
2006-01-12 18:32:29 +00:00
if(cf_lock(&err)) {
/* create main window now so any error dialogs can be it's children */
mainwind = gtk_window_new(GTK_WINDOW_TOPLEVEL);
preferr = NULL;
stateerr = NULL;
if(!cf_loadprefs(&err)) {
2006-01-12 18:40:47 +00:00
preferr = errmsg(GTK_WINDOW(mainwind), "%s", err);
2006-01-12 18:32:29 +00:00
g_free(err);
}
saved = cf_loadstate(&err);
if(NULL != err) {
2006-01-12 18:40:47 +00:00
stateerr = errmsg(GTK_WINDOW(mainwind), "%s", err);
2006-01-12 18:32:29 +00:00
g_free(err);
}
2006-01-12 18:40:47 +00:00
/* set the upload limit */
setlimit(tr);
2006-01-12 18:32:29 +00:00
2006-01-12 18:40:47 +00:00
/* set the listening port */
if(NULL != (pref = cf_getpref(PREF_PORT)) &&
0 < (intval = strtol(pref, NULL, 10)) && 0xffff >= intval)
tr_setBindPort(tr, intval);
2006-01-12 18:32:29 +00:00
maketypes();
makewind(mainwind, tr, saved);
if(NULL != preferr)
gtk_widget_show_all(preferr);
if(NULL != stateerr)
gtk_widget_show_all(stateerr);
} else {
2006-01-12 19:04:05 +00:00
gtk_widget_show(errmsg_full(NULL, (callbackfunc_t)gtk_main_quit,
2006-01-12 18:40:47 +00:00
NULL, "%s", err));
2006-01-12 18:32:29 +00:00
g_free(err);
}
} else {
2006-01-12 19:04:05 +00:00
gtk_widget_show(errmsg_full(NULL, (callbackfunc_t)gtk_main_quit,
2006-01-12 18:40:47 +00:00
NULL, "%s", err));
2006-01-12 18:32:29 +00:00
g_free(err);
}
gtk_main();
return 0;
}
void
maketypes(void) {
tr_type_pieces = g_boxed_type_register_static(
TR_TYPE_PIECES_NAME, tr_pieces_copy, tr_pieces_free);
}
gpointer
tr_pieces_copy(gpointer data) {
return g_memdup(data, sizeof(struct pieces));
}
void
tr_pieces_free(gpointer data) {
g_free(data);
}
void
makewind(GtkWidget *wind, tr_handle_t *tr, GList *saved) {
GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL);
2006-01-12 18:47:30 +00:00
GtkWidget *status = gtk_statusbar_new();
2006-01-12 18:32:29 +00:00
struct cbdata *data = g_new0(struct cbdata, 1);
GtkWidget *list;
GtkRequisition req;
GList *loaderrs, *ii;
2006-01-12 18:32:29 +00:00
struct cf_torrentstate *ts;
2006-01-12 18:48:53 +00:00
gint height;
char *str;
2006-01-12 18:32:29 +00:00
data->tr = tr;
2006-01-12 18:40:47 +00:00
data->wind = GTK_WINDOW(wind);
2006-01-12 18:32:29 +00:00
data->timer = -1;
/* filled in by makewind_list */
data->model = NULL;
data->view = NULL;
2006-01-12 18:47:30 +00:00
data->bar = GTK_STATUSBAR(status);
data->buttons = NULL;
data->prefsopen = FALSE;
2006-01-12 18:32:29 +00:00
2006-01-12 18:48:53 +00:00
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_NEVER,
GTK_POLICY_AUTOMATIC);
2006-01-12 18:32:29 +00:00
gtk_box_pack_start(GTK_BOX(vbox), makewind_toolbar(data), FALSE, FALSE, 0);
list = makewind_list(data);
gtk_container_add(GTK_CONTAINER(scroll), list);
2006-01-12 18:47:30 +00:00
gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 0);
gtk_statusbar_push(GTK_STATUSBAR(status), 0, "");
gtk_box_pack_start(GTK_BOX(vbox), status, FALSE, FALSE, 0);
2006-01-12 18:32:29 +00:00
gtk_container_add(GTK_CONTAINER(wind), vbox);
gtk_window_set_title(data->wind, g_get_application_name());
2006-01-12 18:32:29 +00:00
g_signal_connect(G_OBJECT(wind), "delete_event", G_CALLBACK(winclose), data);
loaderrs = NULL;
2006-01-12 18:32:29 +00:00
for(ii = g_list_first(saved); NULL != ii; ii = ii->next) {
ts = ii->data;
addtorrent(tr, GTK_WINDOW(wind), ts->ts_torrent, ts->ts_directory,
ts->ts_paused, &loaderrs);
2006-01-12 18:32:29 +00:00
cf_freestate(ts);
}
g_list_free(saved);
if(NULL != loaderrs) {
str = joinstrlist(loaderrs, "\n");
errmsg(GTK_WINDOW(wind), ngettext("Failed to load the torrent file %s",
"Failed to load the torrent files:\n%s",
g_list_length(loaderrs)), str);
g_list_foreach(loaderrs, (GFunc)g_free, NULL);
g_list_free(loaderrs);
g_free(str);
savetorrents(tr, GTK_WINDOW(wind), -1, NULL);
}
2006-01-12 18:32:29 +00:00
data->timer = g_timeout_add(500, updatemodel, data);
updatemodel(data);
2006-01-12 18:48:53 +00:00
gtk_widget_show_all(vbox);
gtk_widget_realize(wind);
gtk_widget_size_request(list, &req);
2006-01-12 18:48:53 +00:00
height = req.height;
gtk_widget_size_request(scroll, &req);
height -= req.height;
gtk_widget_size_request(wind, &req);
2006-01-12 18:48:53 +00:00
height += req.height;
gtk_window_set_default_size(GTK_WINDOW(wind), -1, (height > req.width ?
MIN(height, req.width * 8 / 5) : MAX(height, req.width * 5 / 8)));
2006-01-12 18:48:53 +00:00
gtk_widget_show(wind);
2006-01-12 18:32:29 +00:00
}
/* XXX is this the right thing to do? */
#define TR_TORRENT_NEEDS_STOP(t) \
((t) & TR_STATUS_CHECK || (t) & TR_STATUS_DOWNLOAD || (t) & TR_STATUS_SEED)
gboolean
winclose(GtkWidget *widget SHUTUP, GdkEvent *event SHUTUP, gpointer gdata) {
struct cbdata *data = gdata;
struct exitdata *edata;
tr_stat_t *st;
int ii;
if(0 >= data->timer)
g_source_remove(data->timer);
data->timer = -1;
2006-01-12 19:04:05 +00:00
blocksigs();
2006-01-12 18:32:29 +00:00
for(ii = tr_torrentStat(data->tr, &st); 0 < ii; ii--) {
if(TR_TORRENT_NEEDS_STOP(st[ii-1].status)) {
2006-01-12 18:48:53 +00:00
/*fprintf(stderr, "quit: stopping %i %s\n", ii, st[ii-1].info.name);*/
2006-01-12 18:32:29 +00:00
tr_torrentStop(data->tr, ii - 1);
} else {
2006-01-12 18:48:53 +00:00
/*fprintf(stderr, "quit: closing %i %s\n", ii, st[ii-1].info.name);*/
2006-01-12 18:32:29 +00:00
tr_torrentClose(data->tr, ii - 1);
}
}
free(st);
2006-01-12 19:04:05 +00:00
unblocksigs();
2006-01-12 18:32:29 +00:00
/* XXX should disable widgets or something */
/* try to wait until torrents stop before exiting */
edata = g_new0(struct exitdata, 1);
edata->cbdata = data;
edata->started = time(NULL);
edata->timer = g_timeout_add(500, exitcheck, edata);
2006-01-12 18:48:53 +00:00
/*fprintf(stderr, "quit: starting timeout at %i\n", edata->started);*/
2006-01-12 18:32:29 +00:00
/* returning FALSE means to destroy the window */
return TRUE;
}
gboolean
exitcheck(gpointer gdata) {
struct exitdata *data = gdata;
tr_stat_t *st;
int ii;
2006-01-12 19:04:05 +00:00
blocksigs();
2006-01-12 18:32:29 +00:00
for(ii = tr_torrentStat(data->cbdata->tr, &st); 0 < ii; ii--) {
2006-01-12 19:04:05 +00:00
if(TR_STATUS_PAUSE & st[ii-1].status) {
2006-01-12 18:48:53 +00:00
/*fprintf(stderr, "quit: closing %i %s\n", ii, st[ii-1].info.name);*/
2006-01-12 18:32:29 +00:00
tr_torrentClose(data->cbdata->tr, ii - 1);
}
}
free(st);
2006-01-12 18:48:53 +00:00
/*fprintf(stderr, "quit: %i torrents left at %i\n",
tr_torrentCount(data->cbdata->tr), time(NULL));*/
2006-01-12 18:32:29 +00:00
/* keep going if we still have torrents and haven't hit the exit timeout */
if(0 < tr_torrentCount(data->cbdata->tr) &&
time(NULL) - data->started < TRACKER_EXIT_TIMEOUT) {
updatemodel(data->cbdata);
2006-01-12 19:04:05 +00:00
unblocksigs();
2006-01-12 18:32:29 +00:00
return TRUE;
}
/* exit otherwise */
if(0 >= data->timer)
g_source_remove(data->timer);
data->timer = -1;
2006-01-12 19:04:05 +00:00
/*fprintf(stderr, "quit: giving up on %i torrents\n",
tr_torrentCount(data->cbdata->tr));*/
stoptransmission(data->cbdata->tr);
clearhandlers();
unblocksigs();
2006-01-12 18:40:47 +00:00
gtk_widget_destroy(GTK_WIDGET(data->cbdata->wind));
2006-01-12 18:32:29 +00:00
g_free(data->cbdata);
g_free(data);
gtk_main_quit();
return FALSE;
}
2006-01-12 19:04:05 +00:00
void
stoptransmission(void *tr) {
while(0 < tr_torrentCount(tr))
tr_torrentClose(tr, 0);
tr_close(tr);
}
2006-01-12 18:32:29 +00:00
GtkWidget *
makewind_toolbar(struct cbdata *data) {
GtkWidget *bar = gtk_toolbar_new();
GtkToolItem *item;
unsigned int ii;
gtk_toolbar_set_tooltips(GTK_TOOLBAR(bar), TRUE);
2006-01-12 18:48:53 +00:00
gtk_toolbar_set_show_arrow(GTK_TOOLBAR(bar), FALSE);
2006-01-12 18:32:29 +00:00
gtk_toolbar_set_style(GTK_TOOLBAR(bar), GTK_TOOLBAR_BOTH);
2006-01-12 18:47:30 +00:00
data->buttons = g_new(GtkWidget*, ALEN(actionitems));
2006-01-12 18:40:47 +00:00
for(ii = 0; ii < ALEN(actionitems); ii++) {
2006-01-12 18:32:29 +00:00
item = gtk_tool_button_new_from_stock(actionitems[ii].id);
2006-01-12 18:47:30 +00:00
data->buttons[ii] = GTK_WIDGET(item);
gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), gettext(actionitems[ii].name));
2006-01-12 18:32:29 +00:00
gtk_tool_item_set_tooltip(GTK_TOOL_ITEM(item), GTK_TOOLBAR(bar)->tooltips,
gettext(actionitems[ii].ttext), actionitems[ii].tpriv);
2006-01-12 18:32:29 +00:00
g_object_set_data(G_OBJECT(item), LIST_ACTION,
GINT_TO_POINTER(actionitems[ii].act));
g_object_set_data(G_OBJECT(item), LIST_ACTION_FROM,
GINT_TO_POINTER(FROM_BUTTON));
g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(actionclick), data);
2006-01-12 18:47:30 +00:00
gtk_toolbar_insert(GTK_TOOLBAR(bar), item, -1);
2006-01-12 18:32:29 +00:00
}
return bar;
}
2006-01-12 18:47:30 +00:00
/* XXX check for unused data in model */
2006-01-12 18:32:29 +00:00
enum {MC_NAME, MC_SIZE, MC_STAT, MC_ERR, MC_PROG, MC_DRATE, MC_URATE,
MC_ETA, MC_PEERS, MC_UPEERS, MC_DPEERS, MC_PIECES, MC_DOWN, MC_UP,
MC_ROW_INDEX, MC_ROW_COUNT};
GtkWidget *
makewind_list(struct cbdata *data) {
GType types[] = {
/* info->name, info->totalSize, status, error, progress */
2006-01-12 18:47:30 +00:00
G_TYPE_STRING, G_TYPE_UINT64, G_TYPE_INT, G_TYPE_STRING, G_TYPE_FLOAT,
2006-01-12 18:32:29 +00:00
/* rateDownload, rateUpload, eta, peersTotal, peersUploading */
G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT,
/* peersDownloading, pieces, downloaded, uploaded */
G_TYPE_INT, TR_TYPE_PIECES, G_TYPE_UINT64, G_TYPE_UINT64,
/* index into the torrent array */
G_TYPE_INT};
GtkListStore *model;
GtkWidget *view;
2006-01-12 18:47:30 +00:00
GtkTreeViewColumn *col;
2006-01-12 18:32:29 +00:00
GtkTreeSelection *sel;
2006-01-12 18:47:30 +00:00
GtkCellRenderer *namerend, *progrend;
char *str;
2006-01-12 18:32:29 +00:00
2006-01-12 18:40:47 +00:00
assert(MC_ROW_COUNT == ALEN(types));
2006-01-12 18:32:29 +00:00
model = gtk_list_store_newv(MC_ROW_COUNT, types);
view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
/* XXX do I need to worry about reference counts anywhere else? */
g_object_unref(G_OBJECT(model));
data->model = model;
data->view = GTK_TREE_VIEW(view);
2006-01-12 18:47:30 +00:00
namerend = gtk_cell_renderer_text_new();
col = gtk_tree_view_column_new_with_attributes(_("Name"), namerend, NULL);
2006-01-12 18:47:30 +00:00
gtk_tree_view_column_set_cell_data_func(col, namerend, dfname, NULL, NULL);
gtk_tree_view_column_set_expand(col, TRUE);
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
progrend = tr_cell_renderer_torrent_new();
/* this string is only used to determing the size of the progress bar */
str = g_markup_printf_escaped("<big>%s</big>", _(" fnord fnord "));
g_object_set(progrend, "label", str, NULL);
g_free(str);
col = gtk_tree_view_column_new_with_attributes(_("Progress"), progrend, NULL);
2006-01-12 18:47:30 +00:00
gtk_tree_view_column_set_cell_data_func(col, progrend, dfprog, NULL, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
2006-01-12 18:32:29 +00:00
2006-01-12 18:56:00 +00:00
/* XXX this shouldn't be necessary */
g_signal_connect(view, "notify", G_CALLBACK(stylekludge), progrend);
2006-01-12 18:32:29 +00:00
gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), TRUE);
sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
2006-01-12 19:00:58 +00:00
gtk_tree_selection_set_mode(GTK_TREE_SELECTION(sel), GTK_SELECTION_MULTIPLE);
2006-01-12 18:47:30 +00:00
g_signal_connect(G_OBJECT(sel), "changed", G_CALLBACK(fixbuttons), data);
2006-01-12 18:32:29 +00:00
g_signal_connect(G_OBJECT(view), "button-press-event",
G_CALLBACK(listclick), data);
g_signal_connect(G_OBJECT(view), "popup-menu", G_CALLBACK(listpopup), data);
2006-01-12 19:00:58 +00:00
g_signal_connect(G_OBJECT(view), "row-activated",
G_CALLBACK(doubleclick), data);
2006-01-12 18:32:29 +00:00
gtk_widget_show_all(view);
return view;
}
2006-01-12 18:56:00 +00:00
/* kludge to have the progress bars notice theme changes */
static void
stylekludge(GObject *obj, GParamSpec *spec, gpointer gdata) {
if(0 == strcmp("style", spec->name)) {
tr_cell_renderer_torrent_reset_style(TR_CELL_RENDERER_TORRENT(gdata));
2006-01-12 18:56:00 +00:00
gtk_widget_queue_draw(GTK_WIDGET(obj));
}
}
2006-01-12 18:47:30 +00:00
/* disable buttons the user shouldn't be able to click on */
void
fixbuttons(GtkTreeSelection *sel, gpointer gdata) {
struct cbdata *data = gdata;
gboolean selected;
unsigned int ii;
int status;
if(NULL == sel)
sel = gtk_tree_view_get_selection(data->view);
2006-01-12 19:00:58 +00:00
status = 0;
gtk_tree_selection_selected_foreach(sel, orstatus, &status);
selected = (0 < gtk_tree_selection_count_selected_rows(sel));
2006-01-12 18:47:30 +00:00
for(ii = 0; ii < ALEN(actionitems); ii++)
if(actionitems[ii].avail)
gtk_widget_set_sensitive(data->buttons[ii],
(selected && (actionitems[ii].avail & status)));
}
void
dfname(GtkTreeViewColumn *col SHUTUP, GtkCellRenderer *rend,
GtkTreeModel *model, GtkTreeIter *iter, gpointer gdata SHUTUP) {
char *name, *mb, *err, *str, *top, *bottom;
guint64 size;
gfloat prog;
int status, eta, tpeers, upeers, dpeers;
/* XXX should I worry about gtk_tree_model_get failing? */
gtk_tree_model_get(model, iter, MC_NAME, &name, MC_STAT, &status,
MC_SIZE, &size, MC_PROG, &prog, MC_ETA, &eta, MC_PEERS, &tpeers,
MC_UPEERS, &upeers, MC_DPEERS, &dpeers, -1);
if(0 > eta)
eta = 0;
if(0 > tpeers)
tpeers = 0;
if(0 > upeers)
upeers = 0;
if(0 > dpeers)
dpeers = 0;
mb = readablesize(size, 1);
prog *= 100;
if(status & TR_STATUS_CHECK)
top = g_strdup_printf(_("Checking existing files (%.1f%%)"), prog);
2006-01-12 18:47:30 +00:00
else if(status & TR_STATUS_DOWNLOAD)
top = g_strdup_printf(_("Finishing in %02i:%02i:%02i (%.1f%%)"),
2006-01-12 18:47:30 +00:00
eta / 60 / 60, eta / 60 % 60, eta % 60, prog);
else if(status & TR_STATUS_SEED)
top = g_strdup_printf(ngettext("Seeding, uploading to %d of %d peer",
"Seeding, uploading to %d of %d peers",
tpeers), dpeers, tpeers);
2006-01-12 18:47:30 +00:00
else if(status & TR_STATUS_STOPPING)
top = g_strdup(_("Stopping..."));
2006-01-12 18:47:30 +00:00
else if(status & TR_STATUS_PAUSE)
top = g_strdup_printf(_("Stopped (%.1f%%)"), prog);
2006-01-12 18:47:30 +00:00
else {
top = g_strdup("");
assert("XXX unknown status");
}
if(status & TR_TRACKER_ERROR) {
gtk_tree_model_get(model, iter, MC_ERR, &err, -1);
bottom = g_strconcat(_("Error: "), err, NULL);
2006-01-12 18:47:30 +00:00
g_free(err);
}
else if(status & TR_STATUS_DOWNLOAD)
bottom = g_strdup_printf(ngettext("Downloading from %i of %i peer",
"Downloading from %i of %i peers",
tpeers), upeers, tpeers);
2006-01-12 18:47:30 +00:00
else
bottom = NULL;
str = g_markup_printf_escaped("<big>%s (%s)</big>\n<small>%s\n%s</small>",
name, mb, top, (NULL == bottom ? "" : bottom));
g_object_set(rend, "markup", str, NULL);
g_free(name);
g_free(mb);
g_free(str);
g_free(top);
g_free(bottom);
}
void
dfprog(GtkTreeViewColumn *col SHUTUP, GtkCellRenderer *rend,
GtkTreeModel *model, GtkTreeIter *iter, gpointer gdata SHUTUP) {
char *dlstr, *ulstr, *str, *marked;
2006-01-12 18:47:30 +00:00
gfloat prog, dl, ul;
/* XXX should I worry about gtk_tree_model_get failing? */
gtk_tree_model_get(model, iter, MC_PROG, &prog,
MC_DRATE, &dl, MC_URATE, &ul, -1);
if(0.0 > prog)
prog = 0.0;
else if(1.0 < prog)
prog = 1.0;
dlstr = readablesize(dl * 1024.0, 2);
ulstr = readablesize(ul * 1024.0, 2);
str = g_strdup_printf(_("DL: %s/s\nUL: %s/s"), dlstr, ulstr);
marked = g_markup_printf_escaped("<small>%s</small>", str);
2006-01-12 18:47:30 +00:00
g_object_set(rend, "text", str, "value", prog, NULL);
g_free(dlstr);
g_free(ulstr);
g_free(str);
g_free(marked);
2006-01-12 18:47:30 +00:00
}
2006-01-12 18:32:29 +00:00
gboolean
updatemodel(gpointer gdata) {
struct cbdata *data = gdata;
tr_stat_t *st;
2006-01-12 18:47:30 +00:00
int ii, max;
2006-01-12 18:32:29 +00:00
GtkTreeIter iter;
2006-01-12 18:47:30 +00:00
float up, down;
char *upstr, *downstr, *str;
2006-01-12 18:32:29 +00:00
2006-01-12 19:04:05 +00:00
blocksigs();
2006-01-12 18:32:29 +00:00
max = tr_torrentStat(data->tr, &st);
for(ii = 0; ii < max; ii++) {
if(!(ii ? gtk_tree_model_iter_next(GTK_TREE_MODEL(data->model), &iter) :
gtk_tree_model_get_iter_first(GTK_TREE_MODEL(data->model), &iter)))
gtk_list_store_append(data->model, &iter);
/* XXX find out if setting the same data emits changed signal */
2006-01-12 18:47:30 +00:00
gtk_list_store_set(
data->model, &iter, MC_ROW_INDEX, ii,
MC_NAME, st[ii].info.name, MC_SIZE, st[ii].info.totalSize,
MC_STAT, st[ii].status, MC_ERR, st[ii].error, MC_PROG, st[ii].progress,
MC_DRATE, st[ii].rateDownload, MC_URATE, st[ii].rateUpload,
MC_ETA, st[ii].eta, MC_PEERS, st[ii].peersTotal,
2006-01-12 18:32:29 +00:00
MC_UPEERS, st[ii].peersUploading, MC_DPEERS, st[ii].peersDownloading,
2006-01-12 18:40:47 +00:00
MC_DOWN, st[ii].downloaded, MC_UP, st[ii].uploaded, -1);
2006-01-12 18:32:29 +00:00
}
free(st);
2006-01-12 18:45:41 +00:00
/* remove any excess rows */
2006-01-12 18:48:53 +00:00
if(ii ? gtk_tree_model_iter_next(GTK_TREE_MODEL(data->model), &iter) :
gtk_tree_model_get_iter_first(GTK_TREE_MODEL(data->model), &iter))
2006-01-12 18:45:41 +00:00
while(gtk_list_store_remove(data->model, &iter))
;
2006-01-12 18:47:30 +00:00
/* update the status bar */
tr_torrentRates(data->tr, &up, &down);
downstr = readablesize(down * 1024.0, 2);
upstr = readablesize(up * 1024.0, 2);
str = g_strdup_printf(_(" Total DL: %s/s Total UL: %s/s"),
upstr, downstr);
2006-01-12 18:47:30 +00:00
gtk_statusbar_pop(data->bar, 0);
gtk_statusbar_push(data->bar, 0, str);
g_free(str);
g_free(upstr);
g_free(downstr);
/* the status of the selected item may have changed, so update the buttons */
fixbuttons(NULL, data);
2006-01-12 19:04:05 +00:00
unblocksigs();
2006-01-12 18:32:29 +00:00
return TRUE;
}
2006-01-12 19:00:58 +00:00
/* show a popup menu for a right-click on the list */
2006-01-12 18:32:29 +00:00
gboolean
2006-01-12 18:47:30 +00:00
listclick(GtkWidget *widget SHUTUP, GdkEventButton *event, gpointer gdata) {
2006-01-12 18:32:29 +00:00
struct cbdata *data = gdata;
2006-01-12 19:00:58 +00:00
GtkTreeSelection *sel = gtk_tree_view_get_selection(data->view);
2006-01-12 18:47:30 +00:00
GtkTreePath *path;
GtkTreeIter iter;
2006-01-12 19:00:58 +00:00
int index, status;
GList *ids;
2006-01-12 18:32:29 +00:00
if(GDK_BUTTON_PRESS == event->type && 3 == event->button) {
2006-01-12 19:00:58 +00:00
/* find what row, if any, the user clicked on */
2006-01-12 18:47:30 +00:00
if(!gtk_tree_view_get_path_at_pos(data->view, event->x, event->y, &path,
NULL, NULL, NULL))
2006-01-12 19:00:58 +00:00
/* no row was clicked, do the popup with no torrent IDs or status */
dopopupmenu(event, data, NULL, 0);
2006-01-12 18:47:30 +00:00
else {
2006-01-12 19:00:58 +00:00
if(gtk_tree_model_get_iter(GTK_TREE_MODEL(data->model), &iter, path)) {
/* get ID and status for the right-clicked row */
gtk_tree_model_get(GTK_TREE_MODEL(data->model), &iter,
MC_ROW_INDEX, &index, MC_STAT, &status, -1);
/* get a list of selected IDs */
ids = NULL;
gtk_tree_selection_selected_foreach(sel, makeidlist, &ids);
/* is the clicked row selected? */
if(NULL == g_list_find(ids, GINT_TO_POINTER(index))) {
/* no, do the popup for just the clicked row */
g_list_free(ids);
dopopupmenu(event, data, g_list_append(NULL, GINT_TO_POINTER(index)),
status);
} else {
/* yes, do the popup for all the selected rows */
gtk_tree_selection_selected_foreach(sel, orstatus, &status);
dopopupmenu(event, data, ids, status);
}
}
2006-01-12 18:47:30 +00:00
gtk_tree_path_free(path);
}
2006-01-12 18:32:29 +00:00
return TRUE;
}
return FALSE;
}
gboolean
2006-01-12 18:47:30 +00:00
listpopup(GtkWidget *widget SHUTUP, gpointer gdata) {
struct cbdata *data = gdata;
GtkTreeSelection *sel = gtk_tree_view_get_selection(data->view);
GtkTreeModel *model;
2006-01-12 19:00:58 +00:00
GList *ids;
int status;
2006-01-12 18:47:30 +00:00
2006-01-12 19:00:58 +00:00
if(0 >= gtk_tree_selection_count_selected_rows(sel))
dopopupmenu(NULL, data, NULL, 0);
2006-01-12 18:47:30 +00:00
else {
assert(model == GTK_TREE_MODEL(data->model));
2006-01-12 19:00:58 +00:00
status = 0;
gtk_tree_selection_selected_foreach(sel, orstatus, &status);
ids = NULL;
gtk_tree_selection_selected_foreach(sel, makeidlist, &ids);
dopopupmenu(NULL, data, ids, status);
2006-01-12 18:47:30 +00:00
}
2006-01-12 18:32:29 +00:00
return TRUE;
}
void
2006-01-12 19:00:58 +00:00
dopopupmenu(GdkEventButton *event, struct cbdata *data,
GList *ids, int status) {
2006-01-12 18:32:29 +00:00
GtkWidget *menu = gtk_menu_new();
GtkWidget *item;
unsigned int ii;
2006-01-12 18:40:47 +00:00
for(ii = 0; ii < ALEN(actionitems); ii++) {
2006-01-12 18:47:30 +00:00
if(actionitems[ii].nomenu ||
(actionitems[ii].avail &&
2006-01-12 19:00:58 +00:00
(NULL == ids || !(actionitems[ii].avail & status))))
2006-01-12 18:47:30 +00:00
continue;
item = gtk_menu_item_new_with_label(gettext(actionitems[ii].name));
2006-01-12 19:00:58 +00:00
/* set the action for the menu item */
2006-01-12 18:32:29 +00:00
g_object_set_data(G_OBJECT(item), LIST_ACTION,
GINT_TO_POINTER(actionitems[ii].act));
2006-01-12 19:00:58 +00:00
/* show that this action came from a popup menu */
2006-01-12 18:32:29 +00:00
g_object_set_data(G_OBJECT(item), LIST_ACTION_FROM,
GINT_TO_POINTER(FROM_POPUP));
2006-01-12 19:00:58 +00:00
/* set a glist of selected torrent's IDs */
g_object_set_data(G_OBJECT(item), LIST_INDEX, ids);
/* set the menu widget, so the activate handler can destroy it */
g_object_set_data(G_OBJECT(item), LIST_MENU_WIDGET, menu);
2006-01-12 18:32:29 +00:00
g_signal_connect(G_OBJECT(item), "activate",
G_CALLBACK(actionclick), data);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
}
2006-01-12 19:00:58 +00:00
/* set up the glist to be freed when the menu is destroyed */
g_object_set_data_full(G_OBJECT(menu), LIST_INDEX, ids,
(GDestroyNotify)g_list_free);
/* destroy the menu if the user doesn't select anything */
g_signal_connect(menu, "selection-done", G_CALLBACK(killmenu), NULL);
2006-01-12 18:32:29 +00:00
gtk_widget_show_all(menu);
gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
(NULL == event ? 0 : event->button),
gdk_event_get_time((GdkEvent*)event));
}
2006-01-12 19:00:58 +00:00
void
killmenu(GtkWidget *menu, gpointer *gdata SHUTUP) {
gtk_widget_destroy(menu);
}
2006-01-12 18:32:29 +00:00
void
actionclick(GtkWidget *widget, gpointer gdata) {
struct cbdata *data = gdata;
GtkTreeSelection *sel = gtk_tree_view_get_selection(data->view);
enum listact act =
GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), LIST_ACTION));
enum listfrom from =
GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), LIST_ACTION_FROM));
2006-01-12 19:00:58 +00:00
int index, count;
unsigned int actindex;
2006-01-12 18:32:29 +00:00
tr_stat_t *sb;
2006-01-12 19:00:58 +00:00
GList *ids, *ii;
gboolean updatesave;
/* destroy the popup menu, if any */
if(FROM_POPUP == from)
gtk_widget_destroy(g_object_get_data(G_OBJECT(widget), LIST_MENU_WIDGET));
2006-01-12 18:32:29 +00:00
2006-01-12 18:40:47 +00:00
switch(act) {
case ACT_OPEN:
2006-01-12 18:55:27 +00:00
makeaddwind(addtorrent, data->wind, data->tr, addedtorrents, data);
2006-01-12 18:40:47 +00:00
return;
case ACT_PREF:
if(!data->prefsopen)
makeprefwindow(data->wind, data->tr, &data->prefsopen);
2006-01-12 18:40:47 +00:00
return;
default:
break;
2006-01-12 18:32:29 +00:00
}
switch(from) {
case FROM_BUTTON:
2006-01-12 19:00:58 +00:00
ids = NULL;
gtk_tree_selection_selected_foreach(sel, makeidlist, &ids);
2006-01-12 18:32:29 +00:00
/* XXX should I assert(0 <= index) to insure a row was selected? */
break;
case FROM_POPUP:
2006-01-12 19:00:58 +00:00
ids = g_object_get_data(G_OBJECT(widget), LIST_INDEX);
2006-01-12 18:32:29 +00:00
break;
default:
assert(!"unknown action source");
break;
}
2006-01-12 19:00:58 +00:00
for(actindex = 0; actindex < ALEN(actionitems); actindex++)
if(actionitems[actindex].act == act)
break;
assert(actindex < ALEN(actionitems));
2006-01-12 19:04:05 +00:00
blocksigs();
2006-01-12 19:00:58 +00:00
updatesave = FALSE;
count = tr_torrentStat(data->tr, &sb);
2006-01-12 19:04:05 +00:00
for(ii = g_list_sort(ids, intrevcmp); NULL != ii; ii = ii->next) {
2006-01-12 19:00:58 +00:00
index = GPOINTER_TO_INT(ii->data);
if(index >= count) {
assert(!"illegal torrent id");
continue;
}
/* check if this action is valid for this torrent */
if(actionitems[actindex].nomenu ||
(actionitems[actindex].avail &&
!(actionitems[actindex].avail & sb[index].status)))
continue;
2006-01-12 18:32:29 +00:00
switch(act) {
case ACT_START:
2006-01-12 19:01:41 +00:00
tr_torrentStart(data->tr, index);
2006-01-12 19:00:58 +00:00
updatesave = TRUE;
2006-01-12 18:32:29 +00:00
break;
case ACT_STOP:
tr_torrentStop(data->tr, index);
2006-01-12 19:00:58 +00:00
updatesave = TRUE;
2006-01-12 18:32:29 +00:00
break;
case ACT_DELETE:
if(TR_TORRENT_NEEDS_STOP(sb[index].status))
tr_torrentStop(data->tr, index);
tr_torrentClose(data->tr, index);
2006-01-12 19:00:58 +00:00
updatesave = TRUE;
2006-01-12 19:04:05 +00:00
/* XXX should only unselect deleted rows */
gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(data->view));
2006-01-12 18:32:29 +00:00
break;
case ACT_INFO:
2006-01-12 19:00:58 +00:00
makeinfowind(data->wind, data->tr, index);
2006-01-12 18:32:29 +00:00
break;
default:
assert(!"unknown type");
break;
}
}
2006-01-12 19:00:58 +00:00
free(sb);
if(updatesave) {
savetorrents(data->tr, data->wind, -1, NULL);
updatemodel(data);
}
2006-01-12 19:04:05 +00:00
unblocksigs();
2006-01-12 19:00:58 +00:00
if(FROM_BUTTON == from)
g_list_free(ids);
}
2006-01-12 19:04:05 +00:00
gint
intrevcmp(gconstpointer a, gconstpointer b) {
int aint = GPOINTER_TO_INT(a);
int bint = GPOINTER_TO_INT(b);
if(bint > aint)
return 1;
else if(bint < aint)
return -1;
else
return 0;
}
2006-01-12 19:00:58 +00:00
void
doubleclick(GtkWidget *widget SHUTUP, GtkTreePath *path,
GtkTreeViewColumn *col SHUTUP, gpointer gdata) {
struct cbdata *data = gdata;
GtkTreeIter iter;
int index;
if(gtk_tree_model_get_iter(GTK_TREE_MODEL(data->model), &iter, path)) {
gtk_tree_model_get(GTK_TREE_MODEL(data->model), &iter,
MC_ROW_INDEX, &index, -1);
makeinfowind(data->wind, data->tr, index);
}
2006-01-12 18:32:29 +00:00
}
gboolean
2006-01-12 18:40:47 +00:00
addtorrent(tr_handle_t *tr, GtkWindow *parentwind, const char *torrent,
const char *dir, gboolean paused, GList **errs) {
2006-01-12 18:32:29 +00:00
char *wd;
2006-01-12 18:55:27 +00:00
if(NULL == dir && NULL != (dir = cf_getpref(PREF_DIR))) {
2006-01-12 18:40:47 +00:00
if(!mkdir_p(dir, 0777)) {
errmsg(parentwind, _("Failed to create the directory %s:\n%s"),
2006-01-12 18:40:47 +00:00
dir, strerror(errno));
return FALSE;
}
}
2006-01-12 19:04:05 +00:00
blocksigs();
2006-01-12 18:32:29 +00:00
if(0 != tr_torrentInit(tr, torrent)) {
2006-01-12 19:04:05 +00:00
unblocksigs();
2006-01-12 18:32:29 +00:00
/* XXX would be nice to have errno strings, are they printed to stdout? */
if(NULL == errs)
errmsg(parentwind, _("Failed to load the torrent file %s"), torrent);
else
*errs = g_list_append(*errs, g_strdup(torrent));
2006-01-12 18:32:29 +00:00
return FALSE;
}
if(NULL != dir)
tr_torrentSetFolder(tr, tr_torrentCount(tr) - 1, dir);
else {
wd = g_new(char, MAXPATHLEN + 1);
if(NULL == getcwd(wd, MAXPATHLEN + 1))
tr_torrentSetFolder(tr, tr_torrentCount(tr) - 1, ".");
else {
tr_torrentSetFolder(tr, tr_torrentCount(tr) - 1, wd);
free(wd);
}
}
if(!paused)
tr_torrentStart(tr, tr_torrentCount(tr) - 1);
2006-01-12 19:04:05 +00:00
unblocksigs();
2006-01-12 18:32:29 +00:00
return TRUE;
}
void
2006-01-12 18:55:27 +00:00
addedtorrents(void *vdata) {
struct cbdata *data = vdata;
2006-01-12 18:32:29 +00:00
2006-01-12 18:55:27 +00:00
updatemodel(data);
savetorrents(data->tr, data->wind, -1, NULL);
2006-01-12 18:32:29 +00:00
}
gboolean
2006-01-12 18:40:47 +00:00
savetorrents(tr_handle_t *tr, GtkWindow *wind, int count, tr_stat_t *stat) {
2006-01-12 18:32:29 +00:00
char *errstr;
tr_stat_t *st;
gboolean ret;
assert(NULL != tr || 0 <= count);
if(0 <= count)
ret = cf_savestate(count, stat, &errstr);
else {
2006-01-12 19:04:05 +00:00
blocksigs();
2006-01-12 18:32:29 +00:00
count = tr_torrentStat(tr, &st);
2006-01-12 19:04:05 +00:00
unblocksigs();
2006-01-12 18:32:29 +00:00
ret = cf_savestate(count, st, &errstr);
free(st);
}
if(!ret) {
errmsg(wind, "%s", errstr);
g_free(errstr);
}
return ret;
}
2006-01-12 19:00:58 +00:00
/* use with gtk_tree_selection_selected_foreach to | status of selected rows */
void
orstatus(GtkTreeModel *model, GtkTreePath *path SHUTUP, GtkTreeIter *iter,
gpointer gdata) {
int *allstatus = gdata;
int status;
gtk_tree_model_get(model, iter, MC_STAT, &status, -1);
*allstatus |= status;
}
void
makeidlist(GtkTreeModel *model, GtkTreePath *path SHUTUP, GtkTreeIter *iter,
gpointer gdata) {
GList **ids = gdata;
int index;
gtk_tree_model_get(model, iter, MC_ROW_INDEX, &index, -1);
*ids = g_list_append(*ids, GINT_TO_POINTER(index));
}