2007-05-23 00:49:31 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2011-01-19 13:48:47 +00:00
|
|
|
* Copyright (c) Transmission authors and contributors
|
2007-05-23 00:49:31 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <math.h> /* pow () */
|
2013-02-10 23:06:05 +00:00
|
|
|
#include <string.h> /* strlen */
|
2007-05-23 00:49:31 +00:00
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <glib/gi18n.h>
|
2011-08-07 16:42:38 +00:00
|
|
|
#include <gio/gio.h>
|
2007-05-23 00:49:31 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2013-01-25 23:34:20 +00:00
|
|
|
#include <libtransmission/log.h>
|
2009-04-07 20:13:08 +00:00
|
|
|
#include <libtransmission/rpcimpl.h>
|
2008-02-28 20:23:02 +00:00
|
|
|
#include <libtransmission/utils.h> /* tr_free */
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2007-05-23 01:47:42 +00:00
|
|
|
|
2011-08-13 14:19:40 +00:00
|
|
|
#include "actions.h"
|
2007-05-23 04:39:06 +00:00
|
|
|
#include "conf.h"
|
2009-06-11 16:17:48 +00:00
|
|
|
#include "notify.h"
|
2008-02-26 19:58:03 +00:00
|
|
|
#include "tr-core.h"
|
|
|
|
#include "tr-prefs.h"
|
2007-05-23 00:49:31 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2010-06-26 15:55:19 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ADD_ERROR_SIGNAL,
|
|
|
|
ADD_PROMPT_SIGNAL,
|
|
|
|
BLOCKLIST_SIGNAL,
|
|
|
|
BUSY_SIGNAL,
|
|
|
|
PORT_SIGNAL,
|
|
|
|
PREFS_SIGNAL,
|
|
|
|
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2011-08-09 05:47:24 +00:00
|
|
|
static guint signals[LAST_SIGNAL] = { 0 };
|
2010-06-26 15:55:19 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
static void core_maybe_inhibit_hibernation (TrCore * core);
|
2008-05-24 23:23:20 +00:00
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
struct TrCorePrivate
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
GFileMonitor * monitor;
|
|
|
|
gulong monitor_tag;
|
|
|
|
GFile * monitor_dir;
|
|
|
|
GSList * monitor_files;
|
|
|
|
gulong monitor_idle_tag;
|
|
|
|
|
|
|
|
gboolean adding_from_watch_dir;
|
|
|
|
gboolean inhibit_allowed;
|
|
|
|
gboolean have_inhibit_cookie;
|
|
|
|
gboolean dbus_error;
|
|
|
|
guint inhibit_cookie;
|
|
|
|
gint busy_count;
|
|
|
|
GtkTreeModel * raw_model;
|
|
|
|
GtkTreeModel * sorted_model;
|
|
|
|
tr_session * session;
|
|
|
|
GStringChunk * string_chunk;
|
2008-02-09 17:29:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
core_is_disposed (const TrCore * core)
|
2008-02-09 17:29:05 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return !core || !core->priv->sorted_model;
|
2008-02-09 17:29:05 +00:00
|
|
|
}
|
|
|
|
|
2011-08-09 06:31:33 +00:00
|
|
|
G_DEFINE_TYPE (TrCore, tr_core, G_TYPE_OBJECT)
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_dispose (GObject * o)
|
2007-10-16 14:16:01 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
TrCore * core = TR_CORE (o);
|
2007-10-16 14:16:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (core->priv->sorted_model != NULL)
|
2008-02-09 17:29:05 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
g_object_unref (core->priv->sorted_model);
|
|
|
|
core->priv->sorted_model = NULL;
|
|
|
|
core->priv->raw_model = NULL;
|
2011-04-07 04:29:37 +00:00
|
|
|
}
|
2008-02-14 17:18:00 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
G_OBJECT_CLASS (tr_core_parent_class)->dispose (o);
|
2011-04-07 04:29:37 +00:00
|
|
|
}
|
2007-10-16 14:16:01 +00:00
|
|
|
|
2011-04-07 04:29:37 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_finalize (GObject * o)
|
2011-04-07 04:29:37 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
TrCore * core = TR_CORE (o);
|
2011-04-07 04:29:37 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
g_string_chunk_free (core->priv->string_chunk);
|
2011-04-07 04:29:37 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
G_OBJECT_CLASS (tr_core_parent_class)->finalize (o);
|
2007-10-16 14:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_core_class_init (TrCoreClass * core_class)
|
|
|
|
{
|
|
|
|
GObjectClass * gobject_class;
|
|
|
|
GType core_type = G_TYPE_FROM_CLASS (core_class);
|
|
|
|
|
|
|
|
g_type_class_add_private (core_class, sizeof (struct TrCorePrivate));
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (core_class);
|
|
|
|
gobject_class->dispose = core_dispose;
|
|
|
|
gobject_class->finalize = core_finalize;
|
|
|
|
|
|
|
|
signals[ADD_ERROR_SIGNAL] =
|
|
|
|
g_signal_new ("add-error", core_type,
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (TrCoreClass, add_error),
|
|
|
|
NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__UINT_POINTER,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
2, G_TYPE_UINT, G_TYPE_POINTER);
|
|
|
|
|
|
|
|
signals[ADD_PROMPT_SIGNAL] =
|
|
|
|
g_signal_new ("add-prompt", core_type,
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (TrCoreClass, add_prompt),
|
|
|
|
NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__POINTER,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
1, G_TYPE_POINTER);
|
|
|
|
|
|
|
|
signals[BUSY_SIGNAL] =
|
|
|
|
g_signal_new ("busy", core_type,
|
|
|
|
G_SIGNAL_RUN_FIRST,
|
|
|
|
G_STRUCT_OFFSET (TrCoreClass, busy),
|
|
|
|
NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__BOOLEAN,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
1, G_TYPE_BOOLEAN);
|
|
|
|
|
|
|
|
signals[BLOCKLIST_SIGNAL] =
|
|
|
|
g_signal_new ("blocklist-updated", core_type,
|
|
|
|
G_SIGNAL_RUN_FIRST,
|
|
|
|
G_STRUCT_OFFSET (TrCoreClass, blocklist_updated),
|
|
|
|
NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__INT,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
1, G_TYPE_INT);
|
|
|
|
|
|
|
|
signals[PORT_SIGNAL] =
|
|
|
|
g_signal_new ("port-tested", core_type,
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (TrCoreClass, port_tested),
|
|
|
|
NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__BOOLEAN,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
1, G_TYPE_BOOLEAN);
|
|
|
|
|
|
|
|
signals[PREFS_SIGNAL] =
|
|
|
|
g_signal_new ("prefs-changed", core_type,
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
G_STRUCT_OFFSET (TrCoreClass, prefs_changed),
|
|
|
|
NULL, NULL,
|
2012-12-22 20:35:19 +00:00
|
|
|
g_cclosure_marshal_VOID__INT,
|
2012-12-05 17:29:46 +00:00
|
|
|
G_TYPE_NONE,
|
2012-12-31 22:37:18 +00:00
|
|
|
1, G_TYPE_INT);
|
2011-08-07 16:42:38 +00:00
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2011-03-03 07:17:57 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_core_init (TrCore * core)
|
2011-03-03 07:17:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
GtkListStore * store;
|
|
|
|
struct TrCorePrivate * p;
|
2011-03-03 07:17:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* column types for the model used to store torrent information */
|
|
|
|
/* keep this in sync with the enum near the bottom of tr_core.h */
|
|
|
|
GType types[] = { G_TYPE_POINTER, /* collated name */
|
|
|
|
G_TYPE_POINTER, /* tr_torrent* */
|
|
|
|
G_TYPE_INT, /* torrent id */
|
|
|
|
G_TYPE_DOUBLE, /* tr_stat.pieceUploadSpeed_KBps */
|
|
|
|
G_TYPE_DOUBLE, /* tr_stat.pieceDownloadSpeed_KBps */
|
2013-02-08 01:34:59 +00:00
|
|
|
G_TYPE_INT, /* tr_stat.peersGettingFromUs */
|
|
|
|
G_TYPE_INT, /* tr_stat.peersSendingToUs + webseedsSendingToUs */
|
2012-12-05 17:29:46 +00:00
|
|
|
G_TYPE_DOUBLE, /* tr_stat.recheckProgress */
|
|
|
|
G_TYPE_BOOLEAN, /* filter.c:ACTIVITY_FILTER_ACTIVE */
|
|
|
|
G_TYPE_INT, /* tr_stat.activity */
|
|
|
|
G_TYPE_UCHAR, /* tr_stat.finished */
|
|
|
|
G_TYPE_CHAR, /* tr_priority_t */
|
|
|
|
G_TYPE_INT, /* tr_stat.queuePosition */
|
|
|
|
G_TYPE_UINT, /* build_torrent_trackers_hash () */
|
|
|
|
G_TYPE_INT, /* MC_ERROR */
|
|
|
|
G_TYPE_INT }; /* MC_ACTIVE_PEER_COUNT */
|
2011-03-03 07:17:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
p = core->priv = G_TYPE_INSTANCE_GET_PRIVATE (core,
|
|
|
|
TR_CORE_TYPE,
|
|
|
|
struct TrCorePrivate);
|
2011-03-03 07:17:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* create the model used to store torrent data */
|
|
|
|
g_assert (G_N_ELEMENTS (types) == MC_ROW_COUNT);
|
|
|
|
store = gtk_list_store_newv (MC_ROW_COUNT, types);
|
2011-03-03 07:17:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
p->raw_model = GTK_TREE_MODEL (store);
|
|
|
|
p->sorted_model = gtk_tree_model_sort_new_with_model (p->raw_model);
|
|
|
|
p->string_chunk = g_string_chunk_new (2048);
|
|
|
|
g_object_unref (p->raw_model);
|
2011-03-03 07:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
2011-03-04 14:31:23 +00:00
|
|
|
**** EMIT SIGNALS
|
2011-03-03 07:17:57 +00:00
|
|
|
***/
|
|
|
|
|
|
|
|
static inline void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_emit_blocklist_udpated (TrCore * core, int ruleCount)
|
2011-03-03 07:17:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
g_signal_emit (core, signals[BLOCKLIST_SIGNAL], 0, ruleCount);
|
2011-03-03 07:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_emit_port_tested (TrCore * core, gboolean is_open)
|
2011-03-03 07:17:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
g_signal_emit (core, signals[PORT_SIGNAL], 0, is_open);
|
2011-03-03 07:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_emit_err (TrCore * core, enum tr_core_err type, const char * msg)
|
2011-03-03 07:17:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
g_signal_emit (core, signals[ADD_ERROR_SIGNAL], 0, type, msg);
|
2011-03-03 07:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_emit_busy (TrCore * core, gboolean is_busy)
|
2011-03-03 07:17:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
g_signal_emit (core, signals[BUSY_SIGNAL], 0, is_busy);
|
2011-03-03 07:17:57 +00:00
|
|
|
}
|
|
|
|
|
2011-03-04 05:58:32 +00:00
|
|
|
void
|
2012-12-22 20:35:19 +00:00
|
|
|
gtr_core_pref_changed (TrCore * core, const tr_quark key)
|
2011-03-03 07:17:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
g_signal_emit (core, signals[PREFS_SIGNAL], 0, key);
|
2011-03-03 07:17:57 +00:00
|
|
|
}
|
|
|
|
|
2010-06-26 15:55:19 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-01-31 23:01:46 +00:00
|
|
|
static GtkTreeModel *
|
2012-12-05 17:29:46 +00:00
|
|
|
core_raw_model (TrCore * core)
|
2011-01-31 23:01:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return core_is_disposed (core) ? NULL : core->priv->raw_model;
|
2011-01-31 23:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkTreeModel *
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_model (TrCore * core)
|
2011-01-31 23:01:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return core_is_disposed (core) ? NULL : core->priv->sorted_model;
|
2011-01-31 23:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_session *
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_session (TrCore * core)
|
2011-01-31 23:01:46 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return core_is_disposed (core) ? NULL : core->priv->session;
|
2011-01-31 23:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
2011-03-04 14:31:23 +00:00
|
|
|
**** BUSY
|
2011-01-31 23:01:46 +00:00
|
|
|
***/
|
|
|
|
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool
|
2012-12-05 17:29:46 +00:00
|
|
|
core_is_busy (TrCore * core)
|
2010-06-26 15:55:19 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return core->priv->busy_count > 0;
|
2010-06-26 15:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_add_to_busy (TrCore * core, int addMe)
|
2010-06-26 15:55:19 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const bool wasBusy = core_is_busy (core);
|
2010-06-26 15:55:19 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
core->priv->busy_count += addMe;
|
2010-06-26 15:55:19 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (wasBusy != core_is_busy (core))
|
|
|
|
core_emit_busy (core, core_is_busy (core));
|
2010-06-26 15:55:19 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
static void core_inc_busy (TrCore * core) { core_add_to_busy (core, 1); }
|
|
|
|
static void core_dec_busy (TrCore * core) { core_add_to_busy (core, -1); }
|
2010-06-26 15:55:19 +00:00
|
|
|
|
2008-02-14 17:18:00 +00:00
|
|
|
/***
|
2011-03-03 07:17:57 +00:00
|
|
|
****
|
|
|
|
**** SORTING THE MODEL
|
|
|
|
****
|
2008-02-14 17:18:00 +00:00
|
|
|
***/
|
|
|
|
|
2009-02-18 06:03:45 +00:00
|
|
|
static gboolean
|
2012-12-05 17:29:46 +00:00
|
|
|
is_valid_eta (int t)
|
2009-02-18 06:03:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return (t != TR_ETA_NOT_AVAIL) && (t != TR_ETA_UNKNOWN);
|
2009-02-18 06:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_eta (int a, int b)
|
2009-02-18 06:03:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret;
|
2009-02-18 06:03:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
const gboolean a_valid = is_valid_eta (a);
|
|
|
|
const gboolean b_valid = is_valid_eta (b);
|
|
|
|
|
|
|
|
if (!a_valid && !b_valid)
|
|
|
|
ret = 0;
|
|
|
|
else if (!a_valid)
|
|
|
|
ret = -1;
|
|
|
|
else if (!b_valid)
|
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = a < b ? 1 : -1;
|
|
|
|
|
|
|
|
return ret;
|
2009-02-18 06:03:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-19 07:10:47 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_double (double a, double b)
|
2007-12-19 07:10:47 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (a < b)
|
|
|
|
ret = -1;
|
|
|
|
else if (a > b)
|
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
2007-12-19 07:10:47 +00:00
|
|
|
}
|
|
|
|
|
2010-12-01 05:41:58 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_uint64 (uint64_t a, uint64_t b)
|
2010-12-01 05:41:58 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (a < b)
|
|
|
|
ret = -1;
|
|
|
|
else if (a > b)
|
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
2010-12-01 05:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_int (int a, int b)
|
2010-12-01 05:41:58 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (a < b)
|
|
|
|
ret = -1;
|
|
|
|
else if (a > b)
|
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
2010-12-01 05:41:58 +00:00
|
|
|
}
|
|
|
|
|
2008-04-06 14:07:41 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_ratio (double a, double b)
|
2008-04-06 14:07:41 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((int)a == TR_RATIO_INF && (int)b == TR_RATIO_INF)
|
|
|
|
ret = 0;
|
|
|
|
else if ((int)a == TR_RATIO_INF)
|
|
|
|
ret = 1;
|
|
|
|
else if ((int)b == TR_RATIO_INF)
|
|
|
|
ret = -1;
|
|
|
|
else
|
|
|
|
ret = compare_double (a, b);
|
|
|
|
|
|
|
|
return ret;
|
2008-04-06 14:07:41 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 04:41:55 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_time (time_t a, time_t b)
|
2008-06-02 04:41:55 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (a < b)
|
|
|
|
ret = -1;
|
2012-12-26 23:36:23 +00:00
|
|
|
else if (a > b)
|
2012-12-05 17:29:46 +00:00
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
2008-06-02 04:41:55 +00:00
|
|
|
}
|
|
|
|
|
2008-04-06 14:07:41 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_by_name (GtkTreeModel * m,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED)
|
2008-04-06 14:07:41 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const char *ca, *cb;
|
|
|
|
gtk_tree_model_get (m, a, MC_NAME_COLLATED, &ca, -1);
|
|
|
|
gtk_tree_model_get (m, b, MC_NAME_COLLATED, &cb, -1);
|
2013-02-14 15:17:42 +00:00
|
|
|
return g_strcmp0 (ca, cb);
|
2008-04-06 14:07:41 +00:00
|
|
|
}
|
|
|
|
|
2011-08-01 22:24:24 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_by_queue (GtkTreeModel * m,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrent *ta, *tb;
|
|
|
|
const tr_stat *sa, *sb;
|
2011-08-02 03:59:54 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
gtk_tree_model_get (m, a, MC_TORRENT, &ta, -1);
|
|
|
|
sa = tr_torrentStatCached (ta);
|
|
|
|
gtk_tree_model_get (m, b, MC_TORRENT, &tb, -1);
|
|
|
|
sb = tr_torrentStatCached (tb);
|
2011-08-02 03:59:54 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return sb->queuePosition - sa->queuePosition;
|
2011-08-01 22:24:24 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_by_ratio (GtkTreeModel* m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data)
|
2007-10-16 14:16:01 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret = 0;
|
|
|
|
tr_torrent *ta, *tb;
|
|
|
|
const tr_stat *sa, *sb;
|
2007-10-16 14:16:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
gtk_tree_model_get (m, a, MC_TORRENT, &ta, -1);
|
|
|
|
sa = tr_torrentStatCached (ta);
|
|
|
|
gtk_tree_model_get (m, b, MC_TORRENT, &tb, -1);
|
|
|
|
sb = tr_torrentStatCached (tb);
|
2007-10-16 14:32:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = compare_ratio (sa->ratio, sb->ratio);
|
|
|
|
if (!ret)
|
|
|
|
ret = compare_by_queue (m, a, b, user_data);
|
|
|
|
return ret;
|
2007-10-16 14:16:01 +00:00
|
|
|
}
|
|
|
|
|
2007-12-19 02:46:30 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_by_activity (GtkTreeModel * m,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
tr_torrent *ta, *tb;
|
|
|
|
double aUp, aDown, bUp, bDown;
|
|
|
|
|
|
|
|
gtk_tree_model_get (m, a, MC_SPEED_UP, &aUp,
|
|
|
|
MC_SPEED_DOWN, &aDown,
|
|
|
|
MC_TORRENT, &ta,
|
|
|
|
-1);
|
|
|
|
gtk_tree_model_get (m, b, MC_SPEED_UP, &bUp,
|
|
|
|
MC_SPEED_DOWN, &bDown,
|
|
|
|
MC_TORRENT, &tb,
|
|
|
|
-1);
|
2013-01-30 00:20:51 +00:00
|
|
|
|
|
|
|
ret = compare_double (aUp+aDown, bUp+bDown);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
if (!ret)
|
2013-01-30 00:20:51 +00:00
|
|
|
{
|
|
|
|
const tr_stat * const sa = tr_torrentStatCached (ta);
|
|
|
|
const tr_stat * const sb = tr_torrentStatCached (tb);
|
|
|
|
ret = compare_uint64 (sa->peersSendingToUs + sa->peersGettingFromUs,
|
|
|
|
sb->peersSendingToUs + sb->peersGettingFromUs);
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!ret)
|
2013-01-30 00:28:53 +00:00
|
|
|
ret = compare_by_queue (m, a, b, user_data);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
return ret;
|
2007-12-19 02:46:30 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 04:41:55 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_by_age (GtkTreeModel * m,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer u)
|
2008-06-02 04:41:55 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret = 0;
|
|
|
|
tr_torrent *ta, *tb;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
gtk_tree_model_get (m, a, MC_TORRENT, &ta, -1);
|
|
|
|
gtk_tree_model_get (m, b, MC_TORRENT, &tb, -1);
|
2010-12-01 05:41:58 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = compare_time (tr_torrentStatCached (ta)->addedDate,
|
|
|
|
tr_torrentStatCached (tb)->addedDate);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
ret = compare_by_name (m, a, b, u);
|
|
|
|
|
|
|
|
return ret;
|
2008-06-02 04:41:55 +00:00
|
|
|
}
|
|
|
|
|
2009-03-01 00:11:53 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_by_size (GtkTreeModel * m,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer u)
|
2009-03-01 00:11:53 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret = 0;
|
|
|
|
tr_torrent *t;
|
|
|
|
const tr_info *ia, *ib;
|
|
|
|
|
|
|
|
gtk_tree_model_get (m, a, MC_TORRENT, &t, -1);
|
|
|
|
ia = tr_torrentInfo (t);
|
|
|
|
gtk_tree_model_get (m, b, MC_TORRENT, &t, -1);
|
|
|
|
ib = tr_torrentInfo (t);
|
2009-03-01 00:11:53 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = compare_uint64 (ia->totalSize, ib->totalSize);
|
|
|
|
if (!ret)
|
|
|
|
ret = compare_by_name (m, a, b, u);
|
2009-03-01 00:11:53 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return ret;
|
2009-03-01 00:11:53 +00:00
|
|
|
}
|
|
|
|
|
2007-12-19 02:46:30 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_by_progress (GtkTreeModel * m,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer u)
|
2007-12-19 02:46:30 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret = 0;
|
|
|
|
tr_torrent * t;
|
|
|
|
const tr_stat *sa, *sb;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
gtk_tree_model_get (m, a, MC_TORRENT, &t, -1);
|
|
|
|
sa = tr_torrentStatCached (t);
|
|
|
|
gtk_tree_model_get (m, b, MC_TORRENT, &t, -1);
|
|
|
|
sb = tr_torrentStatCached (t);
|
2010-12-01 05:41:58 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = compare_double (sa->percentComplete, sb->percentComplete);
|
|
|
|
if (!ret)
|
|
|
|
ret = compare_double (sa->seedRatioPercentDone, sb->seedRatioPercentDone);
|
|
|
|
if (!ret)
|
|
|
|
ret = compare_by_ratio (m, a, b, u);
|
|
|
|
|
|
|
|
return ret;
|
2007-12-19 02:46:30 +00:00
|
|
|
}
|
|
|
|
|
2009-02-18 06:03:45 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_by_eta (GtkTreeModel * m,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer u)
|
2009-02-18 06:03:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret = 0;
|
|
|
|
tr_torrent *ta, *tb;
|
|
|
|
|
|
|
|
gtk_tree_model_get (m, a, MC_TORRENT, &ta, -1);
|
|
|
|
gtk_tree_model_get (m, b, MC_TORRENT, &tb, -1);
|
2009-02-18 06:03:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = compare_eta (tr_torrentStatCached (ta)->eta,
|
|
|
|
tr_torrentStatCached (tb)->eta);
|
2009-02-18 06:03:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = compare_by_name (m, a, b, u);
|
|
|
|
|
|
|
|
return ret;
|
2009-02-18 06:03:45 +00:00
|
|
|
}
|
|
|
|
|
2007-12-19 07:03:28 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
compare_by_state (GtkTreeModel * m,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer u)
|
2007-12-19 07:03:28 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret = 0;
|
|
|
|
int sa, sb;
|
|
|
|
tr_torrent *ta, *tb;
|
|
|
|
|
|
|
|
gtk_tree_model_get (m, a, MC_ACTIVITY, &sa, MC_TORRENT, &ta, -1);
|
|
|
|
gtk_tree_model_get (m, b, MC_ACTIVITY, &sb, MC_TORRENT, &tb, -1);
|
2008-02-04 18:40:56 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = compare_int (sa, sb);
|
|
|
|
if (!ret)
|
|
|
|
ret = compare_by_queue (m, a, b, u);
|
2008-02-04 18:40:56 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return ret;
|
2007-12-19 07:03:28 +00:00
|
|
|
}
|
|
|
|
|
2007-10-17 18:31:12 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_set_sort_mode (TrCore * core, const char * mode, gboolean is_reversed)
|
|
|
|
{
|
|
|
|
const int col = MC_TORRENT;
|
|
|
|
GtkTreeIterCompareFunc sort_func;
|
|
|
|
GtkSortType type = is_reversed ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING;
|
|
|
|
GtkTreeSortable * sortable = GTK_TREE_SORTABLE (gtr_core_model (core));
|
|
|
|
|
2013-02-10 23:06:05 +00:00
|
|
|
if (!g_strcmp0 (mode, "sort-by-activity"))
|
2012-12-05 17:29:46 +00:00
|
|
|
sort_func = compare_by_activity;
|
2013-02-10 23:06:05 +00:00
|
|
|
else if (!g_strcmp0 (mode, "sort-by-age"))
|
2012-12-05 17:29:46 +00:00
|
|
|
sort_func = compare_by_age;
|
2013-02-10 23:06:05 +00:00
|
|
|
else if (!g_strcmp0 (mode, "sort-by-progress"))
|
2012-12-05 17:29:46 +00:00
|
|
|
sort_func = compare_by_progress;
|
2013-02-10 23:06:05 +00:00
|
|
|
else if (!g_strcmp0 (mode, "sort-by-queue"))
|
2012-12-05 17:29:46 +00:00
|
|
|
sort_func = compare_by_queue;
|
2013-02-10 23:06:05 +00:00
|
|
|
else if (!g_strcmp0 (mode, "sort-by-time-left"))
|
2012-12-05 17:29:46 +00:00
|
|
|
sort_func = compare_by_eta;
|
2013-02-10 23:06:05 +00:00
|
|
|
else if (!g_strcmp0 (mode, "sort-by-ratio"))
|
2012-12-05 17:29:46 +00:00
|
|
|
sort_func = compare_by_ratio;
|
2013-02-10 23:06:05 +00:00
|
|
|
else if (!g_strcmp0 (mode, "sort-by-state"))
|
2012-12-05 17:29:46 +00:00
|
|
|
sort_func = compare_by_state;
|
2013-02-10 23:06:05 +00:00
|
|
|
else if (!g_strcmp0 (mode, "sort-by-size"))
|
2012-12-05 17:29:46 +00:00
|
|
|
sort_func = compare_by_size;
|
|
|
|
else {
|
|
|
|
sort_func = compare_by_name;
|
|
|
|
type = is_reversed ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_tree_sortable_set_sort_func (sortable, col, sort_func, NULL, NULL);
|
|
|
|
gtk_tree_sortable_set_sort_column_id (sortable, col, type);
|
2007-10-17 18:31:12 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 07:17:57 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** WATCHDIR
|
|
|
|
****
|
|
|
|
***/
|
2008-03-09 22:11:36 +00:00
|
|
|
|
2011-03-04 14:31:23 +00:00
|
|
|
static time_t
|
2012-12-05 17:29:46 +00:00
|
|
|
get_file_mtime (GFile * file)
|
2009-10-11 06:23:31 +00:00
|
|
|
{
|
2013-01-20 04:10:47 +00:00
|
|
|
GFileInfo * info;
|
|
|
|
time_t mtime = 0;
|
|
|
|
|
|
|
|
info = g_file_query_info (file, G_FILE_ATTRIBUTE_TIME_MODIFIED, 0, NULL, NULL);
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
|
|
|
mtime = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED);
|
|
|
|
g_object_unref (G_OBJECT (info));
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return mtime;
|
2009-10-11 06:23:31 +00:00
|
|
|
}
|
|
|
|
|
2011-08-13 22:24:31 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
rename_torrent_and_unref_file (GFile * file)
|
2011-08-13 22:24:31 +00:00
|
|
|
{
|
2013-01-20 04:10:47 +00:00
|
|
|
GFileInfo * info;
|
|
|
|
|
|
|
|
info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME, 0, NULL, NULL);
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
2013-01-21 03:18:44 +00:00
|
|
|
GError * error = NULL;
|
2013-01-20 04:10:47 +00:00
|
|
|
const char * old_name;
|
|
|
|
char * new_name;
|
|
|
|
GFile * new_file;
|
|
|
|
|
|
|
|
old_name = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME);
|
|
|
|
new_name = g_strdup_printf ("%s.added", old_name);
|
|
|
|
new_file = g_file_set_display_name (file, new_name, NULL, &error);
|
|
|
|
|
|
|
|
if (error != NULL)
|
|
|
|
{
|
|
|
|
g_message ("Unable to rename \"%s\" as \"%s\": %s", old_name, new_name, error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_file != NULL)
|
|
|
|
g_object_unref (G_OBJECT (new_file));
|
|
|
|
g_free (new_name);
|
|
|
|
g_object_unref (G_OBJECT(info));
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (G_OBJECT(file));
|
2011-08-13 22:24:31 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 22:11:36 +00:00
|
|
|
static gboolean
|
2012-12-05 17:29:46 +00:00
|
|
|
core_watchdir_idle (gpointer gcore)
|
2008-03-09 22:11:36 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
GSList * l;
|
|
|
|
GSList * changing = NULL;
|
|
|
|
GSList * unchanging = NULL;
|
|
|
|
TrCore * core = TR_CORE (gcore);
|
|
|
|
const time_t now = tr_time ();
|
|
|
|
struct TrCorePrivate * p = core->priv;
|
2011-08-13 13:58:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* separate the files into two lists: changing and unchanging */
|
|
|
|
for (l=p->monitor_files; l!=NULL; l=l->next)
|
2011-08-13 13:58:35 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
GFile * file = l->data;
|
|
|
|
const time_t mtime = get_file_mtime (file);
|
|
|
|
if (mtime + 2 >= now)
|
|
|
|
changing = g_slist_prepend (changing, file);
|
|
|
|
else
|
|
|
|
unchanging = g_slist_prepend (unchanging, file);
|
2009-10-11 06:23:31 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* add the files that have stopped changing */
|
|
|
|
if (unchanging != NULL)
|
2011-08-13 13:58:35 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
const gboolean do_start = gtr_pref_flag_get (TR_KEY_start_added_torrents);
|
|
|
|
const gboolean do_prompt = gtr_pref_flag_get (TR_KEY_show_options_window);
|
2012-12-05 17:29:46 +00:00
|
|
|
|
|
|
|
core->priv->adding_from_watch_dir = TRUE;
|
|
|
|
gtr_core_add_files (core, unchanging, do_start, do_prompt, TRUE);
|
|
|
|
g_slist_foreach (unchanging, (GFunc)rename_torrent_and_unref_file, NULL);
|
|
|
|
g_slist_free (unchanging);
|
|
|
|
core->priv->adding_from_watch_dir = FALSE;
|
2011-08-13 13:58:35 +00:00
|
|
|
}
|
2008-03-12 02:11:37 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* keep monitoring the ones that are still changing */
|
|
|
|
g_slist_free (p->monitor_files);
|
|
|
|
p->monitor_files = changing;
|
2009-10-11 06:23:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* if monitor_files is nonempty, keep checking every second */
|
|
|
|
if (core->priv->monitor_files)
|
2013-01-04 16:36:52 +00:00
|
|
|
return G_SOURCE_CONTINUE;
|
2012-12-05 17:29:46 +00:00
|
|
|
core->priv->monitor_idle_tag = 0;
|
2013-01-04 16:36:52 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
2008-03-09 22:11:36 +00:00
|
|
|
}
|
|
|
|
|
2011-08-13 13:58:35 +00:00
|
|
|
/* If this file is a torrent, add it to our list */
|
2008-03-13 01:06:54 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_watchdir_monitor_file (TrCore * core, GFile * file)
|
2008-03-13 01:06:54 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * filename = g_file_get_path (file);
|
|
|
|
const gboolean is_torrent = g_str_has_suffix (filename, ".torrent");
|
2008-03-13 01:06:54 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (is_torrent)
|
2008-03-13 01:06:54 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
GSList * l;
|
|
|
|
struct TrCorePrivate * p = core->priv;
|
|
|
|
|
|
|
|
/* if we're not already watching this file, start watching it now */
|
|
|
|
for (l=p->monitor_files; l!=NULL; l=l->next)
|
|
|
|
if (g_file_equal (file, l->data))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (l == NULL)
|
|
|
|
{
|
|
|
|
g_object_ref (file);
|
|
|
|
p->monitor_files = g_slist_prepend (p->monitor_files, file);
|
|
|
|
if (p->monitor_idle_tag == 0)
|
|
|
|
p->monitor_idle_tag = gdk_threads_add_timeout_seconds (1, core_watchdir_idle, core);
|
2011-08-13 13:58:35 +00:00
|
|
|
}
|
2008-03-13 01:06:54 +00:00
|
|
|
}
|
2011-08-13 13:58:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
g_free (filename);
|
2008-03-13 01:06:54 +00:00
|
|
|
}
|
|
|
|
|
2011-08-13 13:58:35 +00:00
|
|
|
/* GFileMonitor noticed a file was created */
|
2008-03-09 15:27:08 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
on_file_changed_in_watchdir (GFileMonitor * monitor UNUSED,
|
2011-03-03 07:17:57 +00:00
|
|
|
GFile * file,
|
|
|
|
GFile * other_type UNUSED,
|
|
|
|
GFileMonitorEvent event_type,
|
2012-12-05 17:29:46 +00:00
|
|
|
gpointer core)
|
2008-03-09 15:27:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (event_type == G_FILE_MONITOR_EVENT_CREATED)
|
|
|
|
core_watchdir_monitor_file (core, file);
|
2008-03-09 15:27:08 +00:00
|
|
|
}
|
|
|
|
|
2011-08-13 13:58:35 +00:00
|
|
|
/* walk through the pre-existing files in the watchdir */
|
2008-03-09 15:27:08 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_watchdir_scan (TrCore * core)
|
2008-03-09 15:27:08 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
const char * dirname = gtr_pref_string_get (TR_KEY_watch_dir);
|
2012-12-05 17:29:46 +00:00
|
|
|
GDir * dir = g_dir_open (dirname, 0, NULL);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (dir != NULL)
|
2008-03-09 15:27:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * name;
|
|
|
|
while ((name = g_dir_read_name (dir)))
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char * filename = g_build_filename (dirname, name, NULL);
|
|
|
|
GFile * file = g_file_new_for_path (filename);
|
|
|
|
core_watchdir_monitor_file (core, file);
|
|
|
|
g_object_unref (file);
|
|
|
|
g_free (filename);
|
2008-03-13 01:06:54 +00:00
|
|
|
}
|
2011-08-13 13:58:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
g_dir_close (dir);
|
2008-03-09 15:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_watchdir_update (TrCore * core)
|
2008-03-09 15:27:08 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
const gboolean is_enabled = gtr_pref_flag_get (TR_KEY_watch_dir_enabled);
|
|
|
|
GFile * dir = g_file_new_for_path (gtr_pref_string_get (TR_KEY_watch_dir));
|
2012-12-05 17:29:46 +00:00
|
|
|
struct TrCorePrivate * p = core->priv;
|
2008-03-09 15:27:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (p->monitor && (!is_enabled || !g_file_equal (dir, p->monitor_dir)))
|
2008-03-09 15:27:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
g_signal_handler_disconnect (p->monitor, p->monitor_tag);
|
|
|
|
g_file_monitor_cancel (p->monitor);
|
|
|
|
g_object_unref (p->monitor);
|
|
|
|
g_object_unref (p->monitor_dir);
|
|
|
|
|
|
|
|
p->monitor_dir = NULL;
|
|
|
|
p->monitor = NULL;
|
|
|
|
p->monitor_tag = 0;
|
2008-03-09 15:27:08 +00:00
|
|
|
}
|
2008-03-09 22:11:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (is_enabled && !p->monitor)
|
2008-03-09 15:27:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
GFileMonitor * m = g_file_monitor_directory (dir, 0, NULL, NULL);
|
|
|
|
core_watchdir_scan (core);
|
|
|
|
|
|
|
|
g_object_ref (dir);
|
|
|
|
p->monitor = m;
|
|
|
|
p->monitor_dir = dir;
|
|
|
|
p->monitor_tag = g_signal_connect (m, "changed",
|
|
|
|
G_CALLBACK (on_file_changed_in_watchdir), core);
|
2008-03-09 15:27:08 +00:00
|
|
|
}
|
2011-08-13 13:58:35 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
g_object_unref (dir);
|
2008-03-09 15:27:08 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2011-03-03 07:17:57 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-12-19 18:42:33 +00:00
|
|
|
static void
|
2012-12-22 20:35:19 +00:00
|
|
|
on_pref_changed (TrCore * core, const tr_quark key, gpointer data UNUSED)
|
2007-12-19 02:46:30 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
switch (key)
|
2008-03-09 15:27:08 +00:00
|
|
|
{
|
2012-12-22 20:35:19 +00:00
|
|
|
case TR_KEY_sort_mode:
|
|
|
|
case TR_KEY_sort_reversed:
|
|
|
|
{
|
|
|
|
const char * mode = gtr_pref_string_get (TR_KEY_sort_mode);
|
|
|
|
const gboolean is_reversed = gtr_pref_flag_get (TR_KEY_sort_reversed);
|
|
|
|
core_set_sort_mode (core, mode, is_reversed);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TR_KEY_peer_limit_global:
|
|
|
|
tr_sessionSetPeerLimit (gtr_core_session (core), gtr_pref_int_get (key));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_KEY_peer_limit_per_torrent:
|
|
|
|
tr_sessionSetPeerLimitPerTorrent (gtr_core_session (core), gtr_pref_int_get (key));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_KEY_inhibit_desktop_hibernation:
|
|
|
|
core_maybe_inhibit_hibernation (core);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_KEY_watch_dir:
|
|
|
|
case TR_KEY_watch_dir_enabled:
|
|
|
|
core_watchdir_update (core);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2007-05-23 00:49:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-05-23 00:49:31 +00:00
|
|
|
TrCore *
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_new (tr_session * session)
|
2007-05-23 00:49:31 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
TrCore * core = TR_CORE (g_object_new (TR_CORE_TYPE, NULL));
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
core->priv->session = session;
|
2007-12-19 19:16:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* init from prefs & listen to pref changes */
|
2012-12-22 20:35:19 +00:00
|
|
|
on_pref_changed (core, TR_KEY_sort_mode, NULL);
|
|
|
|
on_pref_changed (core, TR_KEY_sort_reversed, NULL);
|
|
|
|
on_pref_changed (core, TR_KEY_watch_dir_enabled, NULL);
|
|
|
|
on_pref_changed (core, TR_KEY_peer_limit_global, NULL);
|
|
|
|
on_pref_changed (core, TR_KEY_inhibit_desktop_hibernation, NULL);
|
2012-12-05 17:29:46 +00:00
|
|
|
g_signal_connect (core, "prefs-changed", G_CALLBACK (on_pref_changed), NULL);
|
2007-12-19 19:16:36 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return core;
|
2007-05-23 00:49:31 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 06:57:39 +00:00
|
|
|
tr_session *
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_close (TrCore * core)
|
2008-04-17 20:35:18 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_session * session = gtr_core_session (core);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (session)
|
2008-04-17 20:35:18 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
core->priv->session = NULL;
|
|
|
|
gtr_pref_save (session);
|
2008-04-17 20:35:18 +00:00
|
|
|
}
|
2013-01-04 06:57:39 +00:00
|
|
|
|
|
|
|
return session;
|
2008-04-17 20:35:18 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 07:17:57 +00:00
|
|
|
/***
|
2011-03-04 14:31:23 +00:00
|
|
|
**** COMPLETENESS CALLBACK
|
2011-03-03 07:17:57 +00:00
|
|
|
***/
|
|
|
|
|
2011-03-03 01:59:25 +00:00
|
|
|
struct notify_callback_data
|
2007-10-16 14:16:01 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
TrCore * core;
|
|
|
|
int torrent_id;
|
2011-03-03 01:59:25 +00:00
|
|
|
};
|
2009-06-11 16:17:48 +00:00
|
|
|
|
2011-03-03 01:59:25 +00:00
|
|
|
static gboolean
|
2012-12-05 17:29:46 +00:00
|
|
|
on_torrent_completeness_changed_idle (gpointer gdata)
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct notify_callback_data * data = gdata;
|
|
|
|
gtr_notify_torrent_completed (data->core, data->torrent_id);
|
|
|
|
g_object_unref (G_OBJECT (data->core));
|
|
|
|
g_free (data);
|
2013-07-20 16:37:59 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
2011-03-03 01:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* this is called in the libtransmission thread, *NOT* the GTK+ thread,
|
|
|
|
so delegate to the GTK+ thread before calling notify's dbus code... */
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
on_torrent_completeness_changed (tr_torrent * tor,
|
2011-03-03 01:59:25 +00:00
|
|
|
tr_completeness completeness,
|
2011-03-22 15:19:54 +00:00
|
|
|
bool was_running,
|
2012-12-05 17:29:46 +00:00
|
|
|
void * gcore)
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (was_running && (completeness != TR_LEECH) && (tr_torrentStat (tor)->sizeWhenDone != 0))
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct notify_callback_data * data = g_new (struct notify_callback_data, 1);
|
|
|
|
data->core = gcore;
|
|
|
|
data->torrent_id = tr_torrentId (tor);
|
|
|
|
g_object_ref (G_OBJECT (data->core));
|
|
|
|
gdk_threads_add_idle (on_torrent_completeness_changed_idle, data);
|
2011-03-03 01:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-04 14:31:23 +00:00
|
|
|
/***
|
2011-04-05 15:41:51 +00:00
|
|
|
**** METADATA CALLBACK
|
2011-03-04 14:31:23 +00:00
|
|
|
***/
|
|
|
|
|
2011-04-07 04:29:37 +00:00
|
|
|
static const char*
|
2012-12-05 17:29:46 +00:00
|
|
|
get_collated_name (TrCore * core, const tr_torrent * tor)
|
2011-03-04 14:31:23 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
char buf[2048];
|
|
|
|
const char * name = tr_torrentName (tor);
|
|
|
|
char * down = g_utf8_strdown (name ? name : "", -1);
|
|
|
|
const tr_info * inf = tr_torrentInfo (tor);
|
|
|
|
g_snprintf (buf, sizeof (buf), "%s\t%s", down, inf->hashString);
|
|
|
|
g_free (down);
|
|
|
|
return g_string_chunk_insert_const (core->priv->string_chunk, buf);
|
2011-03-04 14:31:23 +00:00
|
|
|
}
|
|
|
|
|
2011-04-05 15:41:51 +00:00
|
|
|
struct metadata_callback_data
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
TrCore * core;
|
|
|
|
int torrent_id;
|
2011-04-05 15:41:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
2012-12-05 17:29:46 +00:00
|
|
|
find_row_from_torrent_id (GtkTreeModel * model, int id, GtkTreeIter * setme)
|
2011-04-05 15:41:51 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
gboolean match = FALSE;
|
2011-04-05 15:41:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (gtk_tree_model_iter_children (model, &iter, NULL)) do
|
2011-04-05 15:41:51 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int row_id;
|
|
|
|
gtk_tree_model_get (model, &iter, MC_TORRENT_ID, &row_id, -1);
|
|
|
|
match = id == row_id;
|
2011-04-05 15:41:51 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
while (!match && gtk_tree_model_iter_next (model, &iter));
|
2011-04-05 15:41:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (match)
|
|
|
|
*setme = iter;
|
2011-04-05 15:41:51 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return match;
|
2011-04-05 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-12-05 17:29:46 +00:00
|
|
|
on_torrent_metadata_changed_idle (gpointer gdata)
|
|
|
|
{
|
|
|
|
struct notify_callback_data * data = gdata;
|
|
|
|
tr_session * session = gtr_core_session (data->core);
|
|
|
|
tr_torrent * tor = tr_torrentFindFromId (session, data->torrent_id);
|
|
|
|
|
|
|
|
/* update the torrent's collated name */
|
|
|
|
if (tor != NULL)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel * model = core_raw_model (data->core);
|
|
|
|
if (find_row_from_torrent_id (model, data->torrent_id, &iter))
|
|
|
|
{
|
|
|
|
const char * collated = get_collated_name (data->core, tor);
|
|
|
|
GtkListStore * store = GTK_LIST_STORE (model);
|
|
|
|
gtk_list_store_set (store, &iter, MC_NAME_COLLATED, collated, -1);
|
2011-04-05 15:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* cleanup */
|
|
|
|
g_object_unref (G_OBJECT (data->core));
|
|
|
|
g_free (data);
|
2013-07-20 16:37:59 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
2011-04-05 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* this is called in the libtransmission thread, *NOT* the GTK+ thread,
|
|
|
|
so delegate to the GTK+ thread before changing our list store... */
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
on_torrent_metadata_changed (tr_torrent * tor, void * gcore)
|
2011-04-05 15:41:51 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct notify_callback_data * data = g_new (struct notify_callback_data, 1);
|
|
|
|
data->core = gcore;
|
|
|
|
data->torrent_id = tr_torrentId (tor);
|
|
|
|
g_object_ref (G_OBJECT (data->core));
|
|
|
|
gdk_threads_add_idle (on_torrent_metadata_changed_idle, data);
|
2011-04-05 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** ADDING TORRENTS
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-04-05 01:36:37 +00:00
|
|
|
static unsigned int
|
2012-12-05 17:29:46 +00:00
|
|
|
build_torrent_trackers_hash (tr_torrent * tor)
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-30 22:06:45 +00:00
|
|
|
unsigned int i;
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * pch;
|
|
|
|
uint64_t hash = 0;
|
|
|
|
const tr_info * const inf = tr_torrentInfo (tor);
|
2011-03-03 01:59:25 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (i=0; i<inf->trackerCount; ++i)
|
|
|
|
for (pch=inf->trackers[i].announce; *pch; ++pch)
|
|
|
|
hash = (hash<<4) ^ (hash>>28) ^ *pch;
|
2011-03-03 01:59:25 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return hash;
|
2011-03-03 01:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-12-05 17:29:46 +00:00
|
|
|
is_torrent_active (const tr_stat * st)
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return (st->peersSendingToUs > 0)
|
|
|
|
|| (st->peersGettingFromUs > 0)
|
|
|
|
|| (st->activity == TR_STATUS_CHECK);
|
2011-03-03 01:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_add_torrent (TrCore * core, tr_torrent * tor, gboolean do_notify)
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor != NULL)
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
GtkTreeIter unused;
|
|
|
|
const tr_stat * st = tr_torrentStat (tor);
|
|
|
|
const char * collated = get_collated_name (core, tor);
|
|
|
|
const unsigned int trackers_hash = build_torrent_trackers_hash (tor);
|
|
|
|
GtkListStore * store = GTK_LIST_STORE (core_raw_model (core));
|
|
|
|
|
|
|
|
gtk_list_store_insert_with_values (store, &unused, 0,
|
|
|
|
MC_NAME_COLLATED, collated,
|
|
|
|
MC_TORRENT, tor,
|
|
|
|
MC_TORRENT_ID, tr_torrentId (tor),
|
|
|
|
MC_SPEED_UP, st->pieceUploadSpeed_KBps,
|
|
|
|
MC_SPEED_DOWN, st->pieceDownloadSpeed_KBps,
|
2013-02-08 01:34:59 +00:00
|
|
|
MC_ACTIVE_PEERS_UP, st->peersGettingFromUs,
|
|
|
|
MC_ACTIVE_PEERS_DOWN, st->peersSendingToUs + st->webseedsSendingToUs,
|
2012-12-05 17:29:46 +00:00
|
|
|
MC_RECHECK_PROGRESS, st->recheckProgress,
|
|
|
|
MC_ACTIVE, is_torrent_active (st),
|
|
|
|
MC_ACTIVITY, st->activity,
|
|
|
|
MC_FINISHED, st->finished,
|
|
|
|
MC_PRIORITY, tr_torrentGetPriority (tor),
|
|
|
|
MC_QUEUE_POSITION, st->queuePosition,
|
|
|
|
MC_TRACKERS, trackers_hash,
|
|
|
|
-1);
|
|
|
|
|
|
|
|
if (do_notify)
|
|
|
|
gtr_notify_torrent_added (tr_torrentName (tor));
|
|
|
|
|
|
|
|
tr_torrentSetMetadataCallback (tor, on_torrent_metadata_changed, core);
|
|
|
|
tr_torrentSetCompletenessCallback (tor, on_torrent_completeness_changed, core);
|
2011-03-03 01:59:25 +00:00
|
|
|
}
|
2007-10-16 14:16:01 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 01:59:25 +00:00
|
|
|
static tr_torrent *
|
2012-12-05 17:29:46 +00:00
|
|
|
core_create_new_torrent (TrCore * core, tr_ctor * ctor)
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrent * tor;
|
|
|
|
bool do_trash = false;
|
|
|
|
tr_session * session = gtr_core_session (core);
|
2011-03-03 01:59:25 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* let the gtk client handle the removal, since libT
|
|
|
|
* doesn't have any concept of the glib trash API */
|
|
|
|
tr_ctorGetDeleteSource (ctor, &do_trash);
|
|
|
|
tr_ctorSetDeleteSource (ctor, FALSE);
|
2013-05-22 20:35:38 +00:00
|
|
|
tor = tr_torrentNew (ctor, NULL, NULL);
|
2011-03-03 01:59:25 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor && do_trash)
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const char * config = tr_sessionGetConfigDir (session);
|
|
|
|
const char * source = tr_ctorGetSourceFile (ctor);
|
2011-03-03 01:59:25 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (source != NULL)
|
2012-09-23 15:38:07 +00:00
|
|
|
{
|
|
|
|
/* #1294: don't delete the .torrent file if it's our internal copy */
|
2012-12-05 17:29:46 +00:00
|
|
|
const int is_internal = (strstr (source, config) == source);
|
|
|
|
if (!is_internal)
|
2015-10-23 04:09:40 +00:00
|
|
|
gtr_file_trash_or_remove (source, NULL);
|
2012-09-23 15:38:07 +00:00
|
|
|
}
|
2011-03-03 01:59:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-22 20:35:38 +00:00
|
|
|
return tor;
|
2011-03-03 01:59:25 +00:00
|
|
|
}
|
|
|
|
|
2009-06-11 14:51:21 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
core_add_ctor (TrCore * core, tr_ctor * ctor,
|
|
|
|
gboolean do_prompt, gboolean do_notify)
|
2009-06-11 14:51:21 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_info inf;
|
|
|
|
int err = tr_torrentParse (ctor, &inf);
|
2009-06-11 14:51:21 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (err)
|
2009-06-11 14:51:21 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
case TR_PARSE_ERR:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_PARSE_DUPLICATE:
|
|
|
|
/* don't complain about .torrent files in the watch directory
|
|
|
|
* that have already been added... that gets annoying and we
|
|
|
|
* don't want to be nagging users to clean up their watch dirs */
|
|
|
|
if (!tr_ctorGetSourceFile (ctor) || !core->priv->adding_from_watch_dir)
|
|
|
|
core_emit_err (core, err, inf.name);
|
|
|
|
tr_metainfoFree (&inf);
|
|
|
|
tr_ctorFree (ctor);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (do_prompt)
|
|
|
|
{
|
|
|
|
g_signal_emit (core, signals[ADD_PROMPT_SIGNAL], 0, ctor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gtr_core_add_torrent (core, core_create_new_torrent (core, ctor), do_notify);
|
|
|
|
tr_ctorFree (ctor);
|
|
|
|
}
|
|
|
|
tr_metainfoFree (&inf);
|
|
|
|
break;
|
2009-06-11 14:51:21 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return err;
|
2009-06-11 14:51:21 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 07:17:57 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_apply_defaults (tr_ctor * ctor)
|
2011-03-03 07:17:57 +00:00
|
|
|
{
|
2015-05-31 22:13:31 +00:00
|
|
|
if (!tr_ctorGetPaused (ctor, TR_FORCE, NULL))
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_ctorSetPaused (ctor, TR_FORCE, !gtr_pref_flag_get (TR_KEY_start_added_torrents));
|
2011-03-03 07:17:57 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
if (!tr_ctorGetDeleteSource (ctor, NULL))
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_ctorSetDeleteSource (ctor, gtr_pref_flag_get (TR_KEY_trash_original_torrent_files));
|
2011-03-03 07:17:57 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
if (!tr_ctorGetPeerLimit (ctor, TR_FORCE, NULL))
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_ctorSetPeerLimit (ctor, TR_FORCE, gtr_pref_int_get (TR_KEY_peer_limit_per_torrent));
|
2011-03-03 07:17:57 +00:00
|
|
|
|
2015-05-31 22:13:31 +00:00
|
|
|
if (!tr_ctorGetDownloadDir (ctor, TR_FORCE, NULL))
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_ctorSetDownloadDir (ctor, TR_FORCE, gtr_pref_string_get (TR_KEY_download_dir));
|
2011-03-03 07:17:57 +00:00
|
|
|
}
|
|
|
|
|
2009-09-17 01:28:45 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_add_ctor (TrCore * core, tr_ctor * ctor)
|
2009-09-17 01:28:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const gboolean do_notify = FALSE;
|
2012-12-22 20:35:19 +00:00
|
|
|
const gboolean do_prompt = gtr_pref_flag_get (TR_KEY_show_options_window);
|
2012-12-05 17:29:46 +00:00
|
|
|
core_apply_defaults (ctor);
|
|
|
|
core_add_ctor (core, ctor, do_prompt, do_notify);
|
2009-09-17 01:28:45 +00:00
|
|
|
}
|
|
|
|
|
2009-11-22 16:20:22 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-08-09 02:30:31 +00:00
|
|
|
struct add_from_url_data
|
2009-11-22 16:20:22 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
TrCore * core;
|
|
|
|
tr_ctor * ctor;
|
|
|
|
bool do_prompt;
|
|
|
|
bool do_notify;
|
2009-11-22 16:20:22 +00:00
|
|
|
};
|
|
|
|
|
2011-08-09 02:30:31 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
add_file_async_callback (GObject * file, GAsyncResult * result, gpointer gdata)
|
2009-11-22 16:20:22 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gsize length;
|
|
|
|
char * contents;
|
|
|
|
GError * error = NULL;
|
|
|
|
struct add_from_url_data * data = gdata;
|
2010-06-25 17:40:55 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!g_file_load_contents_finish (G_FILE (file), result, &contents, &length, NULL, &error))
|
2010-06-25 17:40:55 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
g_message (_("Couldn't read \"%s\": %s"), g_file_get_parse_name (G_FILE (file)), error->message);
|
|
|
|
g_error_free (error);
|
2011-08-09 02:30:31 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (!tr_ctorSetMetainfo (data->ctor, (const uint8_t*)contents, length))
|
2011-08-09 02:30:31 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
core_add_ctor (data->core, data->ctor, data->do_prompt, data->do_notify);
|
2010-06-25 17:40:55 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
2010-06-25 17:40:55 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ctorFree (data->ctor);
|
2010-06-25 17:40:55 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
core_dec_busy (data->core);
|
|
|
|
g_free (data);
|
2009-11-22 16:20:22 +00:00
|
|
|
}
|
|
|
|
|
2011-08-09 02:30:31 +00:00
|
|
|
static bool
|
2012-12-05 17:29:46 +00:00
|
|
|
add_file (TrCore * core,
|
2011-08-09 02:30:31 +00:00
|
|
|
GFile * file,
|
|
|
|
gboolean do_start,
|
|
|
|
gboolean do_prompt,
|
2012-12-05 17:29:46 +00:00
|
|
|
gboolean do_notify)
|
2009-11-22 16:20:22 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
bool handled = false;
|
|
|
|
tr_session * session = gtr_core_session (core);
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (session != NULL)
|
2009-11-22 16:20:22 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ctor * ctor;
|
|
|
|
bool tried = false;
|
|
|
|
bool loaded = false;
|
|
|
|
|
|
|
|
ctor = tr_ctorNew (session);
|
|
|
|
core_apply_defaults (ctor);
|
|
|
|
tr_ctorSetPaused (ctor, TR_FORCE, !do_start);
|
|
|
|
|
|
|
|
/* local files... */
|
|
|
|
if (!tried)
|
|
|
|
{
|
|
|
|
char * str = g_file_get_path (file);
|
|
|
|
if ((tried = g_file_test (str, G_FILE_TEST_EXISTS)))
|
|
|
|
loaded = !tr_ctorSetMetainfoFromFile (ctor, str);
|
|
|
|
g_free (str);
|
2010-02-05 13:59:44 +00:00
|
|
|
}
|
2009-11-29 07:42:39 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* magnet links... */
|
|
|
|
if (!tried && g_file_has_uri_scheme (file, "magnet"))
|
|
|
|
{
|
|
|
|
/* GFile mangles the original string with /// so we have to un-mangle */
|
|
|
|
char * str = g_file_get_parse_name (file);
|
|
|
|
char * magnet = g_strdup_printf ("magnet:%s", strchr (str, '?'));
|
|
|
|
tried = true;
|
|
|
|
loaded = !tr_ctorSetMetainfoFromMagnetLink (ctor, magnet);
|
|
|
|
g_free (magnet);
|
|
|
|
g_free (str);
|
2011-08-09 02:30:31 +00:00
|
|
|
}
|
2009-11-22 16:20:22 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* hashcodes that we can turn into magnet links... */
|
|
|
|
if (!tried)
|
|
|
|
{
|
|
|
|
char * str = g_file_get_basename (file);
|
|
|
|
if (gtr_is_hex_hashcode (str))
|
|
|
|
{
|
|
|
|
char * magnet = g_strdup_printf ("magnet:?xt=urn:btih:%s", str);
|
|
|
|
tried = true;
|
|
|
|
loaded = !tr_ctorSetMetainfoFromMagnetLink (ctor, magnet);
|
|
|
|
g_free (magnet);
|
2011-08-09 02:30:31 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
g_free (str);
|
2011-08-09 02:30:31 +00:00
|
|
|
}
|
2009-11-22 16:20:22 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* if we were able to load the metainfo, add the torrent */
|
|
|
|
if (loaded)
|
2011-08-09 02:30:31 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
handled = true;
|
|
|
|
core_add_ctor (core, ctor, do_prompt, do_notify);
|
2011-08-09 02:30:31 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (g_file_has_uri_scheme (file, "http") ||
|
|
|
|
g_file_has_uri_scheme (file, "https") ||
|
|
|
|
g_file_has_uri_scheme (file, "ftp"))
|
2011-08-09 02:30:31 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
struct add_from_url_data * data;
|
2009-11-22 16:20:22 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
data = g_new0 (struct add_from_url_data, 1);
|
|
|
|
data->core = core;
|
|
|
|
data->ctor = ctor;
|
|
|
|
data->do_prompt = do_prompt;
|
|
|
|
data->do_notify = do_notify;
|
2009-11-22 16:20:22 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
handled = true;
|
|
|
|
core_inc_busy (core);
|
|
|
|
g_file_load_contents_async (file, NULL, add_file_async_callback, data);
|
2011-08-09 02:30:31 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
2011-08-09 02:30:31 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ctorFree (ctor);
|
|
|
|
g_message (_("Skipping unknown torrent \"%s\""), g_file_get_parse_name (file));
|
2011-08-09 02:30:31 +00:00
|
|
|
}
|
2009-11-22 16:20:22 +00:00
|
|
|
}
|
2008-12-03 06:01:38 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return handled;
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2011-08-09 02:30:31 +00:00
|
|
|
bool
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_add_from_url (TrCore * core, const char * uri)
|
2007-05-24 10:37:07 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
bool handled;
|
2012-12-22 20:35:19 +00:00
|
|
|
const bool do_start = gtr_pref_flag_get (TR_KEY_start_added_torrents);
|
|
|
|
const bool do_prompt = gtr_pref_flag_get (TR_KEY_show_options_window);
|
2012-12-05 17:29:46 +00:00
|
|
|
const bool do_notify = false;
|
2008-10-20 18:35:12 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
GFile * file = g_file_new_for_uri (uri);
|
|
|
|
handled = add_file (core, file, do_start, do_prompt, do_notify);
|
|
|
|
g_object_unref (file);
|
|
|
|
gtr_core_torrents_added (core);
|
2010-08-02 22:31:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return handled;
|
2007-05-24 10:37:07 +00:00
|
|
|
}
|
|
|
|
|
2010-12-22 06:11:49 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_add_files (TrCore * core,
|
2011-08-09 02:30:31 +00:00
|
|
|
GSList * files,
|
|
|
|
gboolean do_start,
|
|
|
|
gboolean do_prompt,
|
2012-12-05 17:29:46 +00:00
|
|
|
gboolean do_notify)
|
2010-12-22 06:11:49 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
GSList * l;
|
2010-12-22 06:11:49 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (l=files; l!=NULL; l=l->next)
|
|
|
|
add_file (core, l->data, do_start, do_prompt, do_notify);
|
2011-08-09 02:30:31 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_torrents_added (core);
|
2010-12-22 06:11:49 +00:00
|
|
|
}
|
|
|
|
|
2007-05-23 17:59:35 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_torrents_added (TrCore * self)
|
2007-05-23 17:59:35 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_update (self);
|
|
|
|
core_emit_err (self, TR_CORE_ERR_NO_MORE_TORRENTS, NULL);
|
2007-05-23 17:59:35 +00:00
|
|
|
}
|
|
|
|
|
2008-02-19 03:57:03 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_remove_torrent (TrCore * core, int id, gboolean delete_local_data)
|
2008-02-19 03:57:03 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrent * tor = gtr_core_find_torrent (core, id);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor != NULL)
|
2008-07-16 19:38:22 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
/* remove from the gui */
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel * model = core_raw_model (core);
|
|
|
|
if (find_row_from_torrent_id (model, id, &iter))
|
|
|
|
gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
|
|
|
|
|
|
|
|
/* remove the torrent */
|
|
|
|
tr_torrentRemove (tor, delete_local_data, gtr_file_trash_or_remove);
|
2008-07-16 19:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-19 03:57:03 +00:00
|
|
|
|
2011-03-03 07:17:57 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_load (TrCore * self, gboolean forcePaused)
|
2011-03-03 07:17:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int i;
|
|
|
|
tr_ctor * ctor;
|
|
|
|
tr_torrent ** torrents;
|
|
|
|
int count = 0;
|
2011-03-03 07:17:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
ctor = tr_ctorNew (gtr_core_session (self));
|
|
|
|
if (forcePaused)
|
|
|
|
tr_ctorSetPaused (ctor, TR_FORCE, TRUE);
|
|
|
|
tr_ctorSetPeerLimit (ctor, TR_FALLBACK,
|
2012-12-22 20:35:19 +00:00
|
|
|
gtr_pref_int_get (TR_KEY_peer_limit_per_torrent));
|
2011-03-03 07:17:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
torrents = tr_sessionLoadTorrents (gtr_core_session (self), ctor, &count);
|
|
|
|
for (i=0; i<count; ++i)
|
|
|
|
gtr_core_add_torrent (self, torrents[i], FALSE);
|
2011-03-03 07:17:57 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_free (torrents);
|
|
|
|
tr_ctorFree (ctor);
|
2011-03-03 07:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_clear (TrCore * self)
|
2011-03-03 07:17:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gtk_list_store_clear (GTK_LIST_STORE (core_raw_model (self)));
|
2011-03-03 07:17:57 +00:00
|
|
|
}
|
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-03-24 18:32:05 +00:00
|
|
|
static int
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_compare_double (const double a, const double b, int decimal_places)
|
2011-03-24 18:32:05 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int ret;
|
|
|
|
const int64_t ia = (int64_t)(a * pow (10, decimal_places));
|
|
|
|
const int64_t ib = (int64_t)(b * pow (10, decimal_places));
|
|
|
|
|
|
|
|
if (ia < ib)
|
|
|
|
ret = -1;
|
|
|
|
else if (ia > ib)
|
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
2011-03-24 18:32:05 +00:00
|
|
|
}
|
|
|
|
|
2011-04-13 22:16:50 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
update_foreach (GtkTreeModel * model, GtkTreeIter * iter)
|
|
|
|
{
|
|
|
|
int oldActivity, newActivity;
|
|
|
|
int oldActivePeerCount, newActivePeerCount;
|
|
|
|
int oldError, newError;
|
|
|
|
bool oldFinished, newFinished;
|
|
|
|
int oldQueuePosition, newQueuePosition;
|
2013-02-08 01:34:59 +00:00
|
|
|
int oldDownloadPeerCount, newDownloadPeerCount;
|
|
|
|
int oldUploadPeerCount, newUploadPeerCount;
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_priority_t oldPriority, newPriority;
|
|
|
|
unsigned int oldTrackers, newTrackers;
|
|
|
|
double oldUpSpeed, newUpSpeed;
|
|
|
|
double oldDownSpeed, newDownSpeed;
|
|
|
|
double oldRecheckProgress, newRecheckProgress;
|
|
|
|
gboolean oldActive, newActive;
|
|
|
|
const tr_stat * st;
|
|
|
|
tr_torrent * tor;
|
|
|
|
|
|
|
|
/* get the old states */
|
|
|
|
gtk_tree_model_get (model, iter,
|
|
|
|
MC_TORRENT, &tor,
|
|
|
|
MC_ACTIVE, &oldActive,
|
|
|
|
MC_ACTIVE_PEER_COUNT, &oldActivePeerCount,
|
2013-02-08 01:34:59 +00:00
|
|
|
MC_ACTIVE_PEERS_UP, &oldUploadPeerCount,
|
|
|
|
MC_ACTIVE_PEERS_DOWN, &oldDownloadPeerCount,
|
2012-12-05 17:29:46 +00:00
|
|
|
MC_ERROR, &oldError,
|
|
|
|
MC_ACTIVITY, &oldActivity,
|
|
|
|
MC_FINISHED, &oldFinished,
|
|
|
|
MC_PRIORITY, &oldPriority,
|
|
|
|
MC_QUEUE_POSITION, &oldQueuePosition,
|
|
|
|
MC_TRACKERS, &oldTrackers,
|
|
|
|
MC_SPEED_UP, &oldUpSpeed,
|
|
|
|
MC_RECHECK_PROGRESS, &oldRecheckProgress,
|
|
|
|
MC_SPEED_DOWN, &oldDownSpeed,
|
|
|
|
-1);
|
|
|
|
|
|
|
|
/* get the new states */
|
|
|
|
st = tr_torrentStat (tor);
|
|
|
|
newActive = is_torrent_active (st);
|
|
|
|
newActivity = st->activity;
|
|
|
|
newFinished = st->finished;
|
|
|
|
newPriority = tr_torrentGetPriority (tor);
|
|
|
|
newQueuePosition = st->queuePosition;
|
|
|
|
newTrackers = build_torrent_trackers_hash (tor);
|
|
|
|
newUpSpeed = st->pieceUploadSpeed_KBps;
|
|
|
|
newDownSpeed = st->pieceDownloadSpeed_KBps;
|
|
|
|
newRecheckProgress = st->recheckProgress;
|
|
|
|
newActivePeerCount = st->peersSendingToUs + st->peersGettingFromUs + st->webseedsSendingToUs;
|
2013-02-08 01:34:59 +00:00
|
|
|
newDownloadPeerCount = st->peersSendingToUs;
|
|
|
|
newUploadPeerCount = st->peersGettingFromUs + st->webseedsSendingToUs;
|
2012-12-05 17:29:46 +00:00
|
|
|
newError = st->error;
|
|
|
|
|
|
|
|
/* updating the model triggers off resort/refresh,
|
|
|
|
so don't do it unless something's actually changed... */
|
|
|
|
if ((newActive != oldActive)
|
|
|
|
|| (newActivity != oldActivity)
|
|
|
|
|| (newFinished != oldFinished)
|
|
|
|
|| (newPriority != oldPriority)
|
|
|
|
|| (newQueuePosition != oldQueuePosition)
|
|
|
|
|| (newError != oldError)
|
|
|
|
|| (newActivePeerCount != oldActivePeerCount)
|
2013-02-08 01:34:59 +00:00
|
|
|
|| (newDownloadPeerCount != oldDownloadPeerCount)
|
|
|
|
|| (newUploadPeerCount != oldUploadPeerCount)
|
2012-12-05 17:29:46 +00:00
|
|
|
|| (newTrackers != oldTrackers)
|
|
|
|
|| gtr_compare_double (newUpSpeed, oldUpSpeed, 2)
|
|
|
|
|| gtr_compare_double (newDownSpeed, oldDownSpeed, 2)
|
|
|
|
|| gtr_compare_double (newRecheckProgress, oldRecheckProgress, 2))
|
2009-08-07 20:49:06 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gtk_list_store_set (GTK_LIST_STORE (model), iter,
|
|
|
|
MC_ACTIVE, newActive,
|
|
|
|
MC_ACTIVE_PEER_COUNT, newActivePeerCount,
|
2013-02-08 01:34:59 +00:00
|
|
|
MC_ACTIVE_PEERS_UP, newUploadPeerCount,
|
|
|
|
MC_ACTIVE_PEERS_DOWN, newDownloadPeerCount,
|
2012-12-05 17:29:46 +00:00
|
|
|
MC_ERROR, newError,
|
|
|
|
MC_ACTIVITY, newActivity,
|
|
|
|
MC_FINISHED, newFinished,
|
|
|
|
MC_PRIORITY, newPriority,
|
|
|
|
MC_QUEUE_POSITION, newQueuePosition,
|
|
|
|
MC_TRACKERS, newTrackers,
|
|
|
|
MC_SPEED_UP, newUpSpeed,
|
|
|
|
MC_SPEED_DOWN, newDownSpeed,
|
|
|
|
MC_RECHECK_PROGRESS, newRecheckProgress,
|
|
|
|
-1);
|
2009-08-07 20:49:06 +00:00
|
|
|
}
|
2007-11-23 15:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_update (TrCore * core)
|
2007-11-23 15:36:31 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel * model;
|
2011-04-13 22:16:50 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* update the model */
|
|
|
|
model = core_raw_model (core);
|
|
|
|
if (gtk_tree_model_iter_nth_child (model, &iter, NULL, 0)) do
|
|
|
|
update_foreach (model, &iter);
|
|
|
|
while (gtk_tree_model_iter_next (model, &iter));
|
2008-07-02 18:49:07 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* update hibernation */
|
|
|
|
core_maybe_inhibit_hibernation (core);
|
2007-05-23 01:47:42 +00:00
|
|
|
}
|
2007-05-23 17:59:35 +00:00
|
|
|
|
2008-05-24 23:23:20 +00:00
|
|
|
/**
|
|
|
|
*** Hibernate
|
|
|
|
**/
|
|
|
|
|
2011-08-07 16:42:38 +00:00
|
|
|
#define SESSION_MANAGER_SERVICE_NAME "org.gnome.SessionManager"
|
|
|
|
#define SESSION_MANAGER_INTERFACE "org.gnome.SessionManager"
|
|
|
|
#define SESSION_MANAGER_OBJECT_PATH "/org/gnome/SessionManager"
|
2008-05-24 23:23:20 +00:00
|
|
|
|
|
|
|
static gboolean
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_inhibit_hibernation (guint * cookie)
|
|
|
|
{
|
|
|
|
gboolean success;
|
|
|
|
GVariant * response;
|
|
|
|
GDBusConnection * connection;
|
|
|
|
GError * err = NULL;
|
|
|
|
const char * application = "Transmission BitTorrent Client";
|
|
|
|
const char * reason = "BitTorrent Activity";
|
|
|
|
const int toplevel_xid = 0;
|
|
|
|
const int flags = 4; /* Inhibit suspending the session or computer */
|
|
|
|
|
|
|
|
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &err);
|
|
|
|
|
|
|
|
response = g_dbus_connection_call_sync (connection,
|
|
|
|
SESSION_MANAGER_SERVICE_NAME,
|
|
|
|
SESSION_MANAGER_OBJECT_PATH,
|
|
|
|
SESSION_MANAGER_INTERFACE,
|
|
|
|
"Inhibit",
|
|
|
|
g_variant_new ("(susu)", application, toplevel_xid, reason, flags),
|
|
|
|
NULL, G_DBUS_CALL_FLAGS_NONE,
|
|
|
|
1000, NULL, &err);
|
|
|
|
|
|
|
|
if (response != NULL)
|
|
|
|
*cookie = g_variant_get_uint32 (g_variant_get_child_value (response, 0));
|
|
|
|
|
|
|
|
success = (response != NULL) && (err == NULL);
|
|
|
|
|
|
|
|
/* logging */
|
|
|
|
if (success)
|
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddInfo ("%s", _("Inhibiting desktop hibernation"));
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddError (_("Couldn't inhibit desktop hibernation: %s"), err->message);
|
2012-12-05 17:29:46 +00:00
|
|
|
g_error_free (err);
|
2008-05-24 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* cleanup */
|
|
|
|
if (response != NULL)
|
|
|
|
g_variant_unref (response);
|
|
|
|
if (connection != NULL)
|
|
|
|
g_object_unref (connection);
|
2011-10-11 20:32:15 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return success;
|
2008-05-24 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_uninhibit_hibernation (guint inhibit_cookie)
|
|
|
|
{
|
|
|
|
GVariant * response;
|
|
|
|
GDBusConnection * connection;
|
|
|
|
GError * err = NULL;
|
|
|
|
|
|
|
|
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &err);
|
|
|
|
|
|
|
|
response = g_dbus_connection_call_sync (connection,
|
|
|
|
SESSION_MANAGER_SERVICE_NAME,
|
|
|
|
SESSION_MANAGER_OBJECT_PATH,
|
|
|
|
SESSION_MANAGER_INTERFACE,
|
|
|
|
"Uninhibit",
|
|
|
|
g_variant_new ("(u)", inhibit_cookie),
|
|
|
|
NULL, G_DBUS_CALL_FLAGS_NONE,
|
|
|
|
1000, NULL, &err);
|
|
|
|
|
|
|
|
/* logging */
|
|
|
|
if (err == NULL)
|
|
|
|
{
|
2013-01-25 23:34:20 +00:00
|
|
|
tr_logAddInfo ("%s", _("Allowing desktop hibernation"));
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_warning ("Couldn't uninhibit desktop hibernation: %s.", err->message);
|
|
|
|
g_error_free (err);
|
2008-05-24 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
/* cleanup */
|
|
|
|
g_variant_unref (response);
|
|
|
|
g_object_unref (connection);
|
2011-08-07 16:42:38 +00:00
|
|
|
}
|
2008-05-24 23:23:20 +00:00
|
|
|
|
2008-07-02 18:49:07 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_set_hibernation_allowed (TrCore * core, gboolean allowed)
|
2008-05-24 23:23:20 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
g_return_if_fail (core);
|
|
|
|
g_return_if_fail (core->priv);
|
2008-05-24 23:23:20 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
core->priv->inhibit_allowed = allowed != 0;
|
2008-05-24 23:23:20 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (allowed && core->priv->have_inhibit_cookie)
|
2008-05-24 23:23:20 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_uninhibit_hibernation (core->priv->inhibit_cookie);
|
|
|
|
core->priv->have_inhibit_cookie = FALSE;
|
2008-05-24 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!allowed && !core->priv->have_inhibit_cookie
|
|
|
|
&& !core->priv->dbus_error)
|
2008-05-24 23:23:20 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (gtr_inhibit_hibernation (&core->priv->inhibit_cookie))
|
|
|
|
core->priv->have_inhibit_cookie = TRUE;
|
|
|
|
else
|
|
|
|
core->priv->dbus_error = TRUE;
|
2008-05-24 23:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-02 18:49:07 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
core_maybe_inhibit_hibernation (TrCore * core)
|
2008-07-02 18:49:07 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
/* hibernation is allowed if EITHER
|
|
|
|
* (a) the "inhibit" pref is turned off OR
|
|
|
|
* (b) there aren't any active torrents */
|
2012-12-22 20:35:19 +00:00
|
|
|
const gboolean hibernation_allowed = !gtr_pref_flag_get (TR_KEY_inhibit_desktop_hibernation)
|
2012-12-05 17:29:46 +00:00
|
|
|
|| !gtr_core_get_active_torrent_count (core);
|
|
|
|
gtr_core_set_hibernation_allowed (core, hibernation_allowed);
|
2008-07-02 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
/**
|
|
|
|
*** Prefs
|
|
|
|
**/
|
2007-06-27 18:34:38 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
static void
|
2012-12-22 20:35:19 +00:00
|
|
|
core_commit_prefs_change (TrCore * core, const tr_quark key)
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_pref_changed (core, key);
|
|
|
|
gtr_pref_save (gtr_core_session (core));
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2007-05-24 13:55:57 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
void
|
2012-12-22 20:35:19 +00:00
|
|
|
gtr_core_set_pref (TrCore * self, const tr_quark key, const char * newval)
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2013-02-14 15:17:42 +00:00
|
|
|
if (g_strcmp0 (newval, gtr_pref_string_get (key)))
|
2007-05-24 13:55:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_pref_string_set (key, newval);
|
|
|
|
core_commit_prefs_change (self, key);
|
2007-05-24 13:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-22 20:35:19 +00:00
|
|
|
gtr_core_set_pref_bool (TrCore * self, const tr_quark key, gboolean newval)
|
2007-05-24 13:55:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (newval != gtr_pref_flag_get (key))
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_pref_flag_set (key, newval);
|
|
|
|
core_commit_prefs_change (self, key);
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2007-05-24 13:55:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-22 20:35:19 +00:00
|
|
|
gtr_core_set_pref_int (TrCore * self, const tr_quark key, int newval)
|
2007-05-24 13:55:57 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (newval != gtr_pref_int_get (key))
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_pref_int_set (key, newval);
|
|
|
|
core_commit_prefs_change (self, key);
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2007-05-24 13:55:57 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
void
|
2012-12-22 20:35:19 +00:00
|
|
|
gtr_core_set_pref_double (TrCore * self, const tr_quark key, double newval)
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (gtr_compare_double (newval, gtr_pref_double_get (key), 4))
|
2009-02-13 18:23:56 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_pref_double_set (key, newval);
|
|
|
|
core_commit_prefs_change (self, key);
|
2009-02-13 18:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-07 20:13:08 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** RPC Interface
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/* #define DEBUG_RPC */
|
|
|
|
|
|
|
|
static int nextTag = 1;
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
typedef void (server_response_func)(TrCore * core, tr_variant * response, gpointer user_data);
|
2009-04-07 20:13:08 +00:00
|
|
|
|
|
|
|
struct pending_request_data
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
TrCore * core;
|
|
|
|
server_response_func * response_func;
|
|
|
|
gpointer response_func_user_data;
|
2009-04-07 20:13:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static GHashTable * pendingRequests = NULL;
|
|
|
|
|
|
|
|
static gboolean
|
2012-12-05 17:29:46 +00:00
|
|
|
core_read_rpc_response_idle (void * vresponse)
|
2009-04-07 20:13:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int64_t intVal;
|
2015-07-13 00:32:48 +00:00
|
|
|
tr_variant * response = vresponse;
|
2009-04-07 20:13:08 +00:00
|
|
|
|
2015-07-13 00:32:48 +00:00
|
|
|
if (tr_variantDictFindInt (response, TR_KEY_tag, &intVal))
|
2010-09-23 17:05:40 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const int tag = (int)intVal;
|
|
|
|
struct pending_request_data * data = g_hash_table_lookup (pendingRequests, &tag);
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
if (data->response_func)
|
2015-07-13 00:32:48 +00:00
|
|
|
(*data->response_func)(data->core, response, data->response_func_user_data);
|
2012-12-05 17:29:46 +00:00
|
|
|
g_hash_table_remove (pendingRequests, &tag);
|
2010-09-23 17:05:40 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-07 20:13:08 +00:00
|
|
|
|
2015-07-13 00:32:48 +00:00
|
|
|
tr_variantFree (response);
|
|
|
|
tr_free (response);
|
2013-07-20 16:37:59 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
2009-04-07 20:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-07-13 00:32:48 +00:00
|
|
|
core_read_rpc_response (tr_session * session UNUSED,
|
|
|
|
tr_variant * response,
|
|
|
|
void * unused UNUSED)
|
2009-04-07 20:13:08 +00:00
|
|
|
{
|
2015-07-13 00:32:48 +00:00
|
|
|
tr_variant * response_copy = tr_new (tr_variant, 1);
|
|
|
|
|
|
|
|
*response_copy = *response;
|
|
|
|
tr_variantInitBool (response, false);
|
|
|
|
|
|
|
|
gdk_threads_add_idle (core_read_rpc_response_idle, response_copy);
|
2009-04-07 20:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-07-13 00:32:48 +00:00
|
|
|
core_send_rpc_request (TrCore * core, const tr_variant * request, int tag,
|
2011-03-04 14:31:23 +00:00
|
|
|
server_response_func * response_func,
|
2012-12-05 17:29:46 +00:00
|
|
|
void * response_func_user_data)
|
2009-04-07 20:13:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_session * session = gtr_core_session (core);
|
2009-04-07 20:13:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (pendingRequests == NULL)
|
2009-04-07 20:13:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
pendingRequests = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free);
|
2009-04-07 20:13:08 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (session == NULL)
|
2009-04-07 20:13:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
g_error ("GTK+ client doesn't support connections to remote servers yet.");
|
2009-04-07 20:13:08 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else
|
2009-04-07 20:13:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
/* remember this request */
|
|
|
|
struct pending_request_data * data;
|
|
|
|
data = g_new0 (struct pending_request_data, 1);
|
|
|
|
data->core = core;
|
|
|
|
data->response_func = response_func;
|
|
|
|
data->response_func_user_data = response_func_user_data;
|
|
|
|
g_hash_table_insert (pendingRequests, g_memdup (&tag, sizeof (int)), data);
|
|
|
|
|
|
|
|
/* make the request */
|
2009-04-07 20:13:08 +00:00
|
|
|
#ifdef DEBUG_RPC
|
2015-07-13 00:32:48 +00:00
|
|
|
{
|
|
|
|
struct evbuffer * buf = tr_variantToBuf (request, TR_VARIANT_FMT_JSON_LEAN);
|
|
|
|
const size_t buf_len = evbuffer_get_length (buf);
|
|
|
|
g_message ("request: [%*.*s]", (int) buf_len, (int) buf_len, evbuffer_pullup (buf, -1));
|
|
|
|
evbuffer_free (buf);
|
|
|
|
}
|
2009-04-07 20:13:08 +00:00
|
|
|
#endif
|
2015-07-13 00:32:48 +00:00
|
|
|
|
|
|
|
tr_rpc_request_exec_json (session, request, core_read_rpc_response, GINT_TO_POINTER (tag));
|
2009-04-07 20:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** Sending a test-port request via RPC
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
on_port_test_response (TrCore * core, tr_variant * response, gpointer u UNUSED)
|
2009-04-07 20:13:08 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * args;
|
2015-05-09 11:56:35 +00:00
|
|
|
bool is_open;
|
2009-04-07 20:13:08 +00:00
|
|
|
|
2015-05-09 11:56:35 +00:00
|
|
|
if (!tr_variantDictFindDict (response, TR_KEY_arguments, &args) ||
|
|
|
|
!tr_variantDictFindBool (args, TR_KEY_port_is_open, &is_open))
|
|
|
|
is_open = false;
|
2009-04-07 20:13:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
core_emit_port_tested (core, is_open);
|
2009-04-07 20:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_port_test (TrCore * core)
|
2009-04-07 20:13:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const int tag = nextTag++;
|
2015-07-13 00:32:48 +00:00
|
|
|
tr_variant request;
|
|
|
|
|
|
|
|
tr_variantInitDict (&request, 2);
|
|
|
|
tr_variantDictAddStr (&request, TR_KEY_method, "port-test");
|
|
|
|
tr_variantDictAddInt (&request, TR_KEY_tag, tag);
|
|
|
|
core_send_rpc_request (core, &request, tag, on_port_test_response, NULL);
|
|
|
|
tr_variantFree (&request);
|
2009-04-07 20:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** Updating a blocklist via RPC
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2012-12-14 04:34:42 +00:00
|
|
|
on_blocklist_response (TrCore * core, tr_variant * response, gpointer data UNUSED)
|
2009-04-07 20:13:08 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant * args;
|
2015-05-09 11:56:35 +00:00
|
|
|
int64_t ruleCount;
|
2009-04-07 20:13:08 +00:00
|
|
|
|
2015-05-09 11:56:35 +00:00
|
|
|
if (!tr_variantDictFindDict (response, TR_KEY_arguments, &args) ||
|
|
|
|
!tr_variantDictFindInt (args, TR_KEY_blocklist_size, &ruleCount))
|
|
|
|
ruleCount = -1;
|
2009-04-07 20:13:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (ruleCount > 0)
|
2012-12-22 20:35:19 +00:00
|
|
|
gtr_pref_int_set (TR_KEY_blocklist_date, tr_time ());
|
2009-04-07 20:13:08 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
core_emit_blocklist_udpated (core, ruleCount);
|
2009-04-07 20:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_blocklist_update (TrCore * core)
|
2009-04-07 20:13:08 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const int tag = nextTag++;
|
2015-07-13 00:32:48 +00:00
|
|
|
tr_variant request;
|
|
|
|
|
|
|
|
tr_variantInitDict (&request, 2);
|
|
|
|
tr_variantDictAddStr (&request, TR_KEY_method, "blocklist-update");
|
|
|
|
tr_variantDictAddInt (&request, TR_KEY_tag, tag);
|
|
|
|
core_send_rpc_request (core, &request, tag, on_blocklist_response, NULL);
|
|
|
|
tr_variantFree (&request);
|
2009-04-07 20:13:08 +00:00
|
|
|
}
|
2009-04-24 01:37:04 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
2012-12-14 04:34:42 +00:00
|
|
|
gtr_core_exec (TrCore * core, const tr_variant * top)
|
2009-04-24 01:37:04 +00:00
|
|
|
{
|
2015-07-13 00:32:48 +00:00
|
|
|
const int tag = nextTag++;
|
|
|
|
core_send_rpc_request (core, top, tag, NULL, NULL);
|
2009-04-24 01:37:04 +00:00
|
|
|
}
|
2010-03-14 05:13:02 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-12-19 09:33:02 +00:00
|
|
|
size_t
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_get_torrent_count (TrCore * core)
|
2010-04-29 23:08:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return gtk_tree_model_iter_n_children (core_raw_model (core), NULL);
|
2010-04-29 23:08:11 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 09:33:02 +00:00
|
|
|
size_t
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_get_active_torrent_count (TrCore * core)
|
2010-04-29 23:08:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
size_t activeCount = 0;
|
|
|
|
GtkTreeModel * model = core_raw_model (core);
|
2010-04-29 23:08:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (gtk_tree_model_iter_nth_child (model, &iter, NULL, 0)) do
|
2010-04-29 23:08:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int activity;
|
|
|
|
gtk_tree_model_get (model, &iter, MC_ACTIVITY, &activity, -1);
|
2010-04-29 23:08:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (activity != TR_STATUS_STOPPED)
|
|
|
|
++activeCount;
|
2010-04-29 23:08:11 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
while (gtk_tree_model_iter_next (model, &iter));
|
2010-04-29 23:08:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return activeCount;
|
2010-04-29 23:08:11 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 01:59:25 +00:00
|
|
|
tr_torrent *
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_find_torrent (TrCore * core, int id)
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_session * session;
|
|
|
|
tr_torrent * tor = NULL;
|
2011-03-03 01:59:25 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((session = gtr_core_session (core)))
|
|
|
|
tor = tr_torrentFindFromId (session, id);
|
2011-03-03 01:59:25 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return tor;
|
2011-03-03 01:59:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_core_open_folder (TrCore * core, int torrent_id)
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const tr_torrent * tor = gtr_core_find_torrent (core, torrent_id);
|
2011-03-03 01:59:25 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (tor != NULL)
|
2011-03-03 01:59:25 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const gboolean single = tr_torrentInfo (tor)->fileCount == 1;
|
|
|
|
const char * currentDir = tr_torrentGetCurrentDir (tor);
|
|
|
|
|
|
|
|
if (single)
|
|
|
|
{
|
|
|
|
gtr_open_file (currentDir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char * path = g_build_filename (currentDir, tr_torrentName (tor), NULL);
|
|
|
|
gtr_open_file (path);
|
|
|
|
g_free (path);
|
2011-03-03 01:59:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|