2009-05-13 15:54:04 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2009-05-13 15:54:04 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2012-12-05 17:29:46 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2 (b)
|
2009-05-13 15:54:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
2010-10-01 13:33:39 +00:00
|
|
|
* $Id$
|
2009-05-13 15:54:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2010-12-22 06:25:30 +00:00
|
|
|
#include "conf.h" /* gtr_pref_string_get */
|
2009-05-13 15:54:04 +00:00
|
|
|
#include "hig.h"
|
|
|
|
#include "relocate.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2010-04-23 19:37:13 +00:00
|
|
|
#define DATA_KEY "gtr-relocate-data"
|
|
|
|
|
2009-05-13 15:54:04 +00:00
|
|
|
static char * previousLocation = NULL;
|
|
|
|
|
2010-04-23 19:37:13 +00:00
|
|
|
struct relocate_dialog_data
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2009-05-21 00:15:54 +00:00
|
|
|
int done;
|
2011-03-22 15:19:54 +00:00
|
|
|
bool do_move;
|
2010-04-23 19:37:13 +00:00
|
|
|
TrCore * core;
|
|
|
|
GSList * torrent_ids;
|
|
|
|
GtkWidget * message_dialog;
|
|
|
|
GtkWidget * chooser_dialog;
|
2009-05-13 15:54:04 +00:00
|
|
|
};
|
|
|
|
|
2010-04-23 19:37:13 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
data_free (gpointer gdata)
|
2010-04-23 19:37:13 +00:00
|
|
|
{
|
|
|
|
struct relocate_dialog_data * data = gdata;
|
2012-12-05 17:29:46 +00:00
|
|
|
g_slist_free (data->torrent_ids);
|
|
|
|
g_free (data);
|
2010-04-23 19:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
startMovingNextTorrent (struct relocate_dialog_data * data)
|
2010-04-23 19:37:13 +00:00
|
|
|
{
|
|
|
|
char * str;
|
2012-12-05 17:29:46 +00:00
|
|
|
const int id = GPOINTER_TO_INT (data->torrent_ids->data);
|
2010-04-23 19:37:13 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_torrent * tor = gtr_core_find_torrent (data->core, id);
|
|
|
|
if (tor != NULL)
|
|
|
|
tr_torrentSetLocation (tor, previousLocation, data->do_move, NULL, &data->done);
|
2010-04-23 19:37:13 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
data->torrent_ids = g_slist_delete_link (data->torrent_ids,
|
|
|
|
data->torrent_ids);
|
2010-04-23 19:37:13 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
str = g_strdup_printf (_("Moving \"%s\""), tr_torrentName (tor));
|
|
|
|
gtk_message_dialog_set_markup (GTK_MESSAGE_DIALOG (data->message_dialog), str);
|
|
|
|
g_free (str);
|
2010-04-23 19:37:13 +00:00
|
|
|
}
|
|
|
|
|
2009-05-13 15:54:04 +00:00
|
|
|
/* every once in awhile, check to see if the move is done.
|
|
|
|
* if so, delete the dialog */
|
|
|
|
static gboolean
|
2012-12-05 17:29:46 +00:00
|
|
|
onTimer (gpointer gdata)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2010-04-23 19:37:13 +00:00
|
|
|
struct relocate_dialog_data * data = gdata;
|
2010-02-11 17:44:11 +00:00
|
|
|
const int done = data->done;
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (done == TR_LOC_ERROR)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2010-02-11 17:44:11 +00:00
|
|
|
const int flags = GTK_DIALOG_MODAL
|
|
|
|
| GTK_DIALOG_DESTROY_WITH_PARENT;
|
2012-12-05 17:29:46 +00:00
|
|
|
GtkWidget * w = gtk_message_dialog_new (GTK_WINDOW (data->message_dialog),
|
2010-02-11 17:44:11 +00:00
|
|
|
flags,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"%s",
|
2012-12-05 17:29:46 +00:00
|
|
|
_("Couldn't move torrent"));
|
|
|
|
gtk_dialog_run (GTK_DIALOG (w));
|
|
|
|
gtk_widget_destroy (GTK_WIDGET (data->message_dialog));
|
2010-04-23 19:37:13 +00:00
|
|
|
return FALSE;
|
2010-02-11 17:44:11 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
else if (done == TR_LOC_DONE)
|
2010-02-11 17:44:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (data->torrent_ids != NULL)
|
2010-04-23 19:37:13 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
startMovingNextTorrent (data);
|
2010-04-23 19:37:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gtk_widget_destroy (GTK_WIDGET (data->chooser_dialog));
|
2010-04-23 19:37:13 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
|
2010-04-23 19:37:13 +00:00
|
|
|
return TRUE; /* keep looping */
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
onResponse (GtkDialog * dialog, int response, gpointer unused UNUSED)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response == GTK_RESPONSE_APPLY)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
|
|
|
GtkWidget * w;
|
2012-12-05 17:29:46 +00:00
|
|
|
GObject * d = G_OBJECT (dialog);
|
|
|
|
struct relocate_dialog_data * data = g_object_get_data (d, DATA_KEY);
|
|
|
|
GtkFileChooser * chooser = g_object_get_data (d, "chooser");
|
|
|
|
GtkToggleButton * move_tb = g_object_get_data (d, "move_rb");
|
|
|
|
char * location = gtk_file_chooser_get_filename (chooser);
|
2010-04-23 19:37:13 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
data->do_move = gtk_toggle_button_get_active (move_tb);
|
2009-05-13 15:54:04 +00:00
|
|
|
|
|
|
|
/* pop up a dialog saying that the work is in progress */
|
2012-12-05 17:29:46 +00:00
|
|
|
w = gtk_message_dialog_new (GTK_WINDOW (dialog),
|
2009-05-13 15:54:04 +00:00
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL,
|
|
|
|
GTK_MESSAGE_INFO,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
2012-12-05 17:29:46 +00:00
|
|
|
NULL);
|
|
|
|
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (w), _("This may take a moment…"));
|
|
|
|
gtk_dialog_set_response_sensitive (GTK_DIALOG (w), GTK_RESPONSE_CLOSE, FALSE);
|
|
|
|
gtk_widget_show (w);
|
2009-05-13 15:54:04 +00:00
|
|
|
|
|
|
|
/* remember this location so that it can be the default next time */
|
2012-12-05 17:29:46 +00:00
|
|
|
g_free (previousLocation);
|
2009-05-13 15:54:04 +00:00
|
|
|
previousLocation = location;
|
2010-04-23 19:37:13 +00:00
|
|
|
|
|
|
|
/* start the move and periodically check its status */
|
|
|
|
data->message_dialog = w;
|
|
|
|
data->done = TR_LOC_DONE;
|
2012-12-05 17:29:46 +00:00
|
|
|
onTimer (data);
|
|
|
|
gdk_threads_add_timeout_seconds (1, onTimer, data);
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
gtk_widget_destroy (GTK_WIDGET (dialog));
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget*
|
2012-12-05 17:29:46 +00:00
|
|
|
gtr_relocate_dialog_new (GtkWindow * parent,
|
2010-04-23 19:37:13 +00:00
|
|
|
TrCore * core,
|
2012-12-05 17:29:46 +00:00
|
|
|
GSList * torrent_ids)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2011-08-13 21:08:53 +00:00
|
|
|
guint row;
|
2009-05-13 15:54:04 +00:00
|
|
|
GtkWidget * w;
|
|
|
|
GtkWidget * d;
|
|
|
|
GtkWidget * t;
|
2010-04-23 19:37:13 +00:00
|
|
|
struct relocate_dialog_data * data;
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
d = gtk_dialog_new_with_buttons (_("Set Torrent Location"), parent,
|
2009-05-13 15:54:04 +00:00
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT |
|
2010-12-25 02:05:15 +00:00
|
|
|
GTK_DIALOG_MODAL,
|
2009-05-13 15:54:04 +00:00
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
|
2012-12-05 17:29:46 +00:00
|
|
|
NULL);
|
|
|
|
gtk_dialog_set_default_response (GTK_DIALOG (d),
|
|
|
|
GTK_RESPONSE_CANCEL);
|
|
|
|
gtk_dialog_set_alternative_button_order (GTK_DIALOG (d),
|
2009-05-13 15:54:04 +00:00
|
|
|
GTK_RESPONSE_APPLY,
|
|
|
|
GTK_RESPONSE_CANCEL,
|
2012-12-05 17:29:46 +00:00
|
|
|
-1);
|
|
|
|
g_signal_connect (d, "response", G_CALLBACK (onResponse), NULL);
|
2009-05-13 15:54:04 +00:00
|
|
|
|
|
|
|
row = 0;
|
2012-12-05 17:29:46 +00:00
|
|
|
t = hig_workarea_create ();
|
|
|
|
hig_workarea_add_section_title (t, &row, _("Location"));
|
|
|
|
|
|
|
|
if (previousLocation == NULL)
|
|
|
|
previousLocation = g_strdup (gtr_pref_string_get (TR_PREFS_KEY_DOWNLOAD_DIR));
|
|
|
|
w = gtk_file_chooser_button_new (_("Set Torrent Location"), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
|
|
|
|
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (w), previousLocation);
|
|
|
|
g_object_set_data (G_OBJECT (d), "chooser", w);
|
|
|
|
hig_workarea_add_row (t, &row, _("Torrent _location:"), w, NULL);
|
|
|
|
w = gtk_radio_button_new_with_mnemonic (NULL, _("_Move from the current folder"));
|
|
|
|
g_object_set_data (G_OBJECT (d), "move_rb", w);
|
|
|
|
hig_workarea_add_wide_control (t, &row, w);
|
|
|
|
w = gtk_radio_button_new_with_mnemonic_from_widget (GTK_RADIO_BUTTON (w), _("Local data is _already there"));
|
|
|
|
hig_workarea_add_wide_control (t, &row, w);
|
|
|
|
gtr_dialog_set_content (GTK_DIALOG (d), t);
|
|
|
|
|
|
|
|
data = g_new0 (struct relocate_dialog_data, 1);
|
2010-04-23 19:37:13 +00:00
|
|
|
data->core = core;
|
|
|
|
data->torrent_ids = torrent_ids;
|
|
|
|
data->chooser_dialog = d;
|
2012-12-05 17:29:46 +00:00
|
|
|
g_object_set_data_full (G_OBJECT (d), DATA_KEY, data, data_free);
|
2010-04-23 19:37:13 +00:00
|
|
|
|
2009-05-13 15:54:04 +00:00
|
|
|
return d;
|
|
|
|
}
|