2009-05-13 15:54:04 +00:00
|
|
|
/*
|
2021-10-18 20:22:31 +00:00
|
|
|
* This file Copyright (C) 2009-2021 Mnemosyne LLC
|
2009-05-13 15:54:04 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-05-13 15:54:04 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <glibmm.h>
|
|
|
|
#include <glibmm/i18n.h>
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2009-05-13 15:54:04 +00:00
|
|
|
|
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"
|
2021-10-18 20:22:31 +00:00
|
|
|
#include "tr-core.h"
|
2009-05-13 15:54:04 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
std::string previousLocation;
|
2010-04-23 19:37:13 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
class RelocateDialog::Impl
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
public:
|
|
|
|
Impl(RelocateDialog& dialog, Glib::RefPtr<TrCore> const& core, std::vector<int> const& torrent_ids);
|
|
|
|
~Impl();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void onResponse(int response);
|
|
|
|
bool onTimer();
|
|
|
|
|
|
|
|
void startMovingNextTorrent();
|
|
|
|
|
|
|
|
private:
|
|
|
|
RelocateDialog& dialog_;
|
|
|
|
Glib::RefPtr<TrCore> const core_;
|
|
|
|
std::vector<int> torrent_ids_;
|
|
|
|
|
|
|
|
int done_ = 0;
|
|
|
|
bool do_move_ = false;
|
|
|
|
sigc::connection timer_;
|
|
|
|
std::unique_ptr<Gtk::MessageDialog> message_dialog_;
|
|
|
|
Gtk::FileChooserButton* chooser_ = nullptr;
|
|
|
|
Gtk::RadioButton* move_tb_ = nullptr;
|
2009-05-13 15:54:04 +00:00
|
|
|
};
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
RelocateDialog::Impl::~Impl()
|
2010-04-23 19:37:13 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
timer_.disconnect();
|
2010-04-23 19:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void RelocateDialog::Impl::startMovingNextTorrent()
|
2010-04-23 19:37:13 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto* const tor = core_->find_torrent(torrent_ids_.back());
|
2010-04-23 19:37:13 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tor != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
tr_torrentSetLocation(tor, previousLocation.c_str(), do_move_, nullptr, &done_);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
torrent_ids_.pop_back();
|
2010-04-23 19:37:13 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
message_dialog_->set_message(gtr_sprintf(_("Moving \"%s\""), tr_torrentName(tor)), true);
|
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 */
|
2021-10-18 20:22:31 +00:00
|
|
|
bool RelocateDialog::Impl::onTimer()
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
if (done_ == TR_LOC_ERROR)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::MessageDialog(*message_dialog_, _("Couldn't move torrent"), false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE, true)
|
|
|
|
.run();
|
|
|
|
message_dialog_.reset();
|
2010-02-11 17:44:11 +00:00
|
|
|
}
|
2021-10-18 20:22:31 +00:00
|
|
|
else if (done_ == TR_LOC_DONE)
|
2010-02-11 17:44:11 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
if (!torrent_ids_.empty())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
startMovingNextTorrent();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
dialog_.hide();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return G_SOURCE_CONTINUE;
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void RelocateDialog::Impl::onResponse(int response)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
if (response == Gtk::RESPONSE_APPLY)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
auto const location = chooser_->get_filename();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
do_move_ = move_tb_->get_active();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* pop up a dialog saying that the work is in progress */
|
2021-10-18 20:22:31 +00:00
|
|
|
message_dialog_ = std::make_unique<Gtk::MessageDialog>(
|
|
|
|
dialog_,
|
|
|
|
Glib::ustring(),
|
|
|
|
false,
|
|
|
|
Gtk::MESSAGE_INFO,
|
|
|
|
Gtk::BUTTONS_CLOSE,
|
|
|
|
true);
|
|
|
|
message_dialog_->set_secondary_text(_("This may take a moment…"));
|
|
|
|
message_dialog_->set_response_sensitive(Gtk::RESPONSE_CLOSE, false);
|
|
|
|
message_dialog_->show();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* remember this location so that it can be the default next time */
|
|
|
|
previousLocation = location;
|
|
|
|
|
|
|
|
/* start the move and periodically check its status */
|
2021-10-18 20:22:31 +00:00
|
|
|
done_ = TR_LOC_DONE;
|
|
|
|
timer_ = Glib::signal_timeout().connect_seconds(sigc::mem_fun(this, &Impl::onTimer), 1);
|
|
|
|
onTimer();
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
dialog_.hide();
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
std::unique_ptr<RelocateDialog> RelocateDialog::create(
|
|
|
|
Gtk::Window& parent,
|
|
|
|
Glib::RefPtr<TrCore> const& core,
|
|
|
|
std::vector<int> const& torrent_ids)
|
|
|
|
{
|
|
|
|
return std::unique_ptr<RelocateDialog>(new RelocateDialog(parent, core, torrent_ids));
|
|
|
|
}
|
|
|
|
|
|
|
|
RelocateDialog::RelocateDialog(Gtk::Window& parent, Glib::RefPtr<TrCore> const& core, std::vector<int> const& torrent_ids)
|
|
|
|
: Gtk::Dialog(_("Set Torrent Location"), parent, true)
|
|
|
|
, impl_(std::make_unique<Impl>(*this, core, torrent_ids))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RelocateDialog::~RelocateDialog() = default;
|
|
|
|
|
|
|
|
RelocateDialog::Impl::Impl(RelocateDialog& dialog, Glib::RefPtr<TrCore> const& core, std::vector<int> const& torrent_ids)
|
|
|
|
: dialog_(dialog)
|
|
|
|
, core_(core)
|
|
|
|
, torrent_ids_(torrent_ids)
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
guint row;
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
dialog_.add_button(_("_Cancel"), Gtk::RESPONSE_CANCEL);
|
|
|
|
dialog_.add_button(_("_Apply"), Gtk::RESPONSE_APPLY);
|
|
|
|
dialog_.set_default_response(Gtk::RESPONSE_CANCEL);
|
|
|
|
dialog_.signal_response().connect(sigc::mem_fun(this, &Impl::onResponse));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
row = 0;
|
2021-10-18 20:22:31 +00:00
|
|
|
auto* t = Gtk::make_managed<HigWorkarea>();
|
|
|
|
t->add_section_title(row, _("Location"));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (previousLocation.empty())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
previousLocation = gtr_pref_string_get(TR_KEY_download_dir);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
chooser_ = Gtk::make_managed<Gtk::FileChooserButton>(_("Set Torrent Location"), Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
|
|
|
|
chooser_->set_current_folder(previousLocation);
|
|
|
|
t->add_row(row, _("Torrent _location:"), *chooser_);
|
|
|
|
|
|
|
|
Gtk::RadioButton::Group group;
|
|
|
|
|
|
|
|
move_tb_ = Gtk::make_managed<Gtk::RadioButton>(group, _("_Move from the current folder"), true);
|
|
|
|
t->add_wide_control(row, *move_tb_);
|
|
|
|
|
|
|
|
t->add_wide_control(row, *Gtk::make_managed<Gtk::RadioButton>(group, _("Local data is _already there"), true));
|
|
|
|
|
|
|
|
gtr_dialog_set_content(dialog_, *t);
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|