2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2009-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
#include "RelocateDialog.h"
|
|
|
|
|
2022-12-29 02:42:20 +00:00
|
|
|
#include "GtkCompat.h"
|
2022-12-26 21:13:21 +00:00
|
|
|
#include "PathButton.h"
|
|
|
|
#include "Prefs.h" /* gtr_pref_string_get */
|
|
|
|
#include "Session.h"
|
|
|
|
#include "Utils.h"
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
#include <glibmm/i18n.h>
|
2022-12-27 01:43:20 +00:00
|
|
|
#include <glibmm/main.h>
|
|
|
|
#include <glibmm/ustring.h>
|
|
|
|
#include <gtkmm/checkbutton.h>
|
|
|
|
#include <gtkmm/messagedialog.h>
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2022-12-26 21:13:21 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2009-05-13 15:54:04 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2022-08-22 02:26:13 +00:00
|
|
|
std::string targetLocation;
|
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:
|
2022-09-07 22:25:04 +00:00
|
|
|
Impl(
|
|
|
|
RelocateDialog& dialog,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
|
|
|
Glib::RefPtr<Session> const& core,
|
|
|
|
std::vector<tr_torrent_id_t> const& torrent_ids);
|
2021-10-18 20:22:31 +00:00
|
|
|
~Impl();
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
TR_DISABLE_COPY_MOVE(Impl)
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
private:
|
|
|
|
void onResponse(int response);
|
|
|
|
bool onTimer();
|
|
|
|
|
|
|
|
void startMovingNextTorrent();
|
|
|
|
|
|
|
|
private:
|
|
|
|
RelocateDialog& dialog_;
|
2021-11-01 00:11:23 +00:00
|
|
|
Glib::RefPtr<Session> const core_;
|
2022-06-17 15:43:04 +00:00
|
|
|
std::vector<tr_torrent_id_t> torrent_ids_;
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
int done_ = 0;
|
|
|
|
bool do_move_ = false;
|
|
|
|
sigc::connection timer_;
|
|
|
|
std::unique_ptr<Gtk::MessageDialog> message_dialog_;
|
2022-10-08 22:50:03 +00:00
|
|
|
PathButton* chooser_ = nullptr;
|
|
|
|
Gtk::CheckButton* 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
|
|
|
{
|
2022-08-22 02:26:13 +00:00
|
|
|
tr_torrentSetLocation(tor, targetLocation.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
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
message_dialog_->set_message(
|
|
|
|
fmt::format(_("Moving '{torrent_name}'"), fmt::arg("torrent_name", 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
|
|
|
{
|
2022-09-10 18:15:01 +00:00
|
|
|
auto d = std::make_shared<Gtk::MessageDialog>(
|
2022-09-10 13:19:54 +00:00
|
|
|
*message_dialog_,
|
|
|
|
_("Couldn't move torrent"),
|
|
|
|
false,
|
|
|
|
TR_GTK_MESSAGE_TYPE(ERROR),
|
|
|
|
TR_GTK_BUTTONS_TYPE(CLOSE),
|
2022-09-10 18:15:01 +00:00
|
|
|
true);
|
|
|
|
|
|
|
|
d->signal_response().connect(
|
|
|
|
[this, d](int /*response*/) mutable
|
|
|
|
{
|
|
|
|
d.reset();
|
2023-01-21 15:56:38 +00:00
|
|
|
message_dialog_.reset();
|
|
|
|
dialog_.close();
|
2022-09-10 18:15:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
d->show();
|
2023-01-21 15:56:38 +00:00
|
|
|
return false;
|
2010-02-11 17:44:11 +00:00
|
|
|
}
|
2023-01-21 15:56:38 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-01-21 15:56:38 +00:00
|
|
|
message_dialog_.reset();
|
2022-10-08 22:50:03 +00:00
|
|
|
dialog_.close();
|
2023-01-21 15:56:38 +00:00
|
|
|
return false;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-21 15:56:38 +00:00
|
|
|
return true;
|
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
|
|
|
{
|
2022-09-10 13:19:54 +00:00
|
|
|
if (response == TR_GTK_RESPONSE_TYPE(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,
|
2022-09-10 13:19:54 +00:00
|
|
|
TR_GTK_MESSAGE_TYPE(INFO),
|
|
|
|
TR_GTK_BUTTONS_TYPE(CLOSE),
|
2021-10-18 20:22:31 +00:00
|
|
|
true);
|
|
|
|
message_dialog_->set_secondary_text(_("This may take a moment…"));
|
2022-09-10 13:19:54 +00:00
|
|
|
message_dialog_->set_response_sensitive(TR_GTK_RESPONSE_TYPE(CLOSE), false);
|
2021-10-18 20:22:31 +00:00
|
|
|
message_dialog_->show();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-22 02:26:13 +00:00
|
|
|
/* remember this location for the next torrent */
|
|
|
|
targetLocation = location;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* remember this location so that it can be the default next time */
|
2022-08-22 02:26:13 +00:00
|
|
|
gtr_save_recent_dir("relocate", core_, location);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* start the move and periodically check its status */
|
2021-10-18 20:22:31 +00:00
|
|
|
done_ = TR_LOC_DONE;
|
2021-11-12 09:12:50 +00:00
|
|
|
timer_ = Glib::signal_timeout().connect_seconds(sigc::mem_fun(*this, &Impl::onTimer), 1);
|
2021-10-18 20:22:31 +00:00
|
|
|
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
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
dialog_.close();
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
RelocateDialog::RelocateDialog(
|
|
|
|
BaseObjectType* cast_item,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::Window& parent,
|
2021-11-01 00:11:23 +00:00
|
|
|
Glib::RefPtr<Session> const& core,
|
2022-09-07 22:25:04 +00:00
|
|
|
std::vector<int> const& torrent_ids)
|
|
|
|
: Gtk::Dialog(cast_item)
|
|
|
|
, impl_(std::make_unique<Impl>(*this, builder, core, torrent_ids))
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
2022-09-07 22:25:04 +00:00
|
|
|
set_transient_for(parent);
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
RelocateDialog::~RelocateDialog() = default;
|
|
|
|
|
|
|
|
std::unique_ptr<RelocateDialog> RelocateDialog::create(
|
2022-06-17 15:43:04 +00:00
|
|
|
Gtk::Window& parent,
|
|
|
|
Glib::RefPtr<Session> const& core,
|
|
|
|
std::vector<tr_torrent_id_t> const& torrent_ids)
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
2022-09-07 22:25:04 +00:00
|
|
|
auto const builder = Gtk::Builder::create_from_resource(gtr_get_full_resource_path("RelocateDialog.ui"));
|
|
|
|
return std::unique_ptr<RelocateDialog>(
|
|
|
|
gtr_get_widget_derived<RelocateDialog>(builder, "RelocateDialog", parent, core, torrent_ids));
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 15:43:04 +00:00
|
|
|
RelocateDialog::Impl::Impl(
|
|
|
|
RelocateDialog& dialog,
|
2022-09-07 22:25:04 +00:00
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
2022-06-17 15:43:04 +00:00
|
|
|
Glib::RefPtr<Session> const& core,
|
|
|
|
std::vector<tr_torrent_id_t> const& torrent_ids)
|
2021-10-18 20:22:31 +00:00
|
|
|
: dialog_(dialog)
|
|
|
|
, core_(core)
|
|
|
|
, torrent_ids_(torrent_ids)
|
2022-10-08 22:50:03 +00:00
|
|
|
, chooser_(gtr_get_widget_derived<PathButton>(builder, "new_location_button"))
|
|
|
|
, move_tb_(gtr_get_widget<Gtk::CheckButton>(builder, "move_data_radio"))
|
2009-05-13 15:54:04 +00:00
|
|
|
{
|
2022-09-10 13:19:54 +00:00
|
|
|
dialog_.set_default_response(TR_GTK_RESPONSE_TYPE(CANCEL));
|
2021-11-12 09:12:50 +00:00
|
|
|
dialog_.signal_response().connect(sigc::mem_fun(*this, &Impl::onResponse));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-22 02:26:13 +00:00
|
|
|
auto recent_dirs = gtr_get_recent_dirs("relocate");
|
|
|
|
if (recent_dirs.empty())
|
|
|
|
{
|
|
|
|
/* default to download dir */
|
2022-10-08 22:50:03 +00:00
|
|
|
chooser_->set_filename(gtr_pref_string_get(TR_KEY_download_dir));
|
2022-08-22 02:26:13 +00:00
|
|
|
}
|
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-22 02:26:13 +00:00
|
|
|
/* set last used as target */
|
2022-10-08 22:50:03 +00:00
|
|
|
chooser_->set_filename(recent_dirs.front());
|
2022-08-22 02:26:13 +00:00
|
|
|
recent_dirs.pop_front();
|
|
|
|
|
|
|
|
/* add remaining as shortcut */
|
2022-10-08 22:50:03 +00:00
|
|
|
chooser_->set_shortcut_folders(recent_dirs);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-05-13 15:54:04 +00:00
|
|
|
}
|