2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-2022 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.
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <future>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2022-08-02 19:46:08 +00:00
|
|
|
#include <string_view>
|
2022-08-17 16:08:36 +00:00
|
|
|
#include <utility>
|
2022-01-13 02:13:58 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
#include <glibmm.h>
|
|
|
|
#include <glibmm/i18n.h>
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2022-03-15 14:52:16 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
#include <libtransmission/error.h>
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/makemeta.h>
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <libtransmission/utils.h> /* tr_formatter_mem_B() */
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
#include "PathButton.h"
|
2021-11-01 00:11:23 +00:00
|
|
|
#include "MakeDialog.h"
|
|
|
|
#include "PrefsDialog.h"
|
|
|
|
#include "Session.h"
|
|
|
|
#include "Utils.h"
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
#if GTKMM_CHECK_VERSION(4, 0, 0)
|
|
|
|
using FileListValue = Glib::Value<GSList*>;
|
|
|
|
using FileListHandler = Glib::SListHandler<Glib::RefPtr<Gio::File>>;
|
|
|
|
#endif
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
namespace
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
class MakeProgressDialog : public Gtk::Dialog
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
public:
|
|
|
|
MakeProgressDialog(
|
2022-09-07 22:25:04 +00:00
|
|
|
BaseObjectType* cast_item,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
|
|
|
tr_metainfo_builder& metainfo_builder,
|
2022-08-02 19:46:08 +00:00
|
|
|
std::future<tr_error*> future,
|
2022-11-15 19:30:32 +00:00
|
|
|
std::string_view target,
|
2021-11-01 00:11:23 +00:00
|
|
|
Glib::RefPtr<Session> const& core);
|
2021-10-24 15:55:51 +00:00
|
|
|
~MakeProgressDialog() override;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
TR_DISABLE_COPY_MOVE(MakeProgressDialog)
|
|
|
|
|
2022-11-15 19:30:32 +00:00
|
|
|
static std::unique_ptr<MakeProgressDialog> create(
|
|
|
|
std::string_view target,
|
|
|
|
tr_metainfo_builder& metainfo_builder,
|
|
|
|
std::future<tr_error*> future,
|
|
|
|
Glib::RefPtr<Session> const& core);
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
[[nodiscard]] bool success() const
|
|
|
|
{
|
|
|
|
return success_;
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
private:
|
|
|
|
bool onProgressDialogRefresh();
|
|
|
|
void onProgressDialogResponse(int response);
|
|
|
|
|
|
|
|
void addTorrent();
|
|
|
|
|
|
|
|
private:
|
|
|
|
tr_metainfo_builder& builder_;
|
2022-08-02 19:46:08 +00:00
|
|
|
std::future<tr_error*> future_;
|
2021-10-18 20:22:31 +00:00
|
|
|
std::string const target_;
|
2021-11-01 00:11:23 +00:00
|
|
|
Glib::RefPtr<Session> const core_;
|
2022-08-02 19:46:08 +00:00
|
|
|
bool success_ = false;
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
sigc::connection progress_tag_;
|
|
|
|
Gtk::Label* progress_label_ = nullptr;
|
|
|
|
Gtk::ProgressBar* progress_bar_ = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
class MakeDialog::Impl
|
2009-09-17 01:28:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
public:
|
2022-09-07 22:25:04 +00:00
|
|
|
Impl(MakeDialog& dialog, Glib::RefPtr<Gtk::Builder> const& builder, Glib::RefPtr<Session> const& core);
|
2022-11-13 17:36:16 +00:00
|
|
|
~Impl() = default;
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
TR_DISABLE_COPY_MOVE(Impl)
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
private:
|
2022-11-09 20:46:23 +00:00
|
|
|
void onSourceToggled(Gtk::CheckButton* tb, PathButton* chooser);
|
2022-10-08 22:50:03 +00:00
|
|
|
void onChooserChosen(PathButton* chooser);
|
2021-10-18 20:22:31 +00:00
|
|
|
void onResponse(int response);
|
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
#if GTKMM_CHECK_VERSION(4, 0, 0)
|
|
|
|
bool on_drag_data_received(Glib::ValueBase const& value, double x, double y);
|
|
|
|
#else
|
2021-10-18 20:22:31 +00:00
|
|
|
void on_drag_data_received(
|
|
|
|
Glib::RefPtr<Gdk::DragContext> const& drag_context,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
Gtk::SelectionData const& selection_data,
|
|
|
|
guint info,
|
|
|
|
guint time_);
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bool set_dropped_source_path(std::string const& filename);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
void updatePiecesLabel();
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
void setFilename(std::string_view filename);
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-11-15 19:30:32 +00:00
|
|
|
void configurePieceSizeScale(uint32_t piece_size);
|
2022-06-01 14:11:30 +00:00
|
|
|
void onPieceSizeUpdated();
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
MakeDialog& dialog_;
|
2021-11-01 00:11:23 +00:00
|
|
|
Glib::RefPtr<Session> const core_;
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
Gtk::CheckButton* file_radio_ = nullptr;
|
|
|
|
PathButton* file_chooser_ = nullptr;
|
|
|
|
Gtk::CheckButton* folder_radio_ = nullptr;
|
|
|
|
PathButton* folder_chooser_ = nullptr;
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::Label* pieces_lb_ = nullptr;
|
2022-06-01 14:11:30 +00:00
|
|
|
Gtk::Scale* piece_size_scale_ = nullptr;
|
2022-10-08 22:50:03 +00:00
|
|
|
PathButton* destination_chooser_ = nullptr;
|
2021-10-18 20:22:31 +00:00
|
|
|
Gtk::CheckButton* comment_check_ = nullptr;
|
|
|
|
Gtk::Entry* comment_entry_ = nullptr;
|
|
|
|
Gtk::CheckButton* private_check_ = nullptr;
|
2021-10-18 23:05:39 +00:00
|
|
|
Gtk::CheckButton* source_check_ = nullptr;
|
|
|
|
Gtk::Entry* source_entry_ = nullptr;
|
2021-10-18 20:22:31 +00:00
|
|
|
std::unique_ptr<MakeProgressDialog> progress_dialog_;
|
|
|
|
Glib::RefPtr<Gtk::TextBuffer> announce_text_buffer_;
|
2022-08-02 19:46:08 +00:00
|
|
|
std::optional<tr_metainfo_builder> builder_;
|
2021-10-18 20:22:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool MakeProgressDialog::onProgressDialogRefresh()
|
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const is_done = !future_.valid() || future_.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
if (is_done)
|
2013-01-04 19:45:39 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
progress_tag_.disconnect();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
// progress value
|
|
|
|
auto percent_done = 1.0;
|
|
|
|
auto piece_index = tr_piece_index_t{};
|
|
|
|
|
|
|
|
if (!is_done)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const [current, total] = builder_.checksumStatus();
|
|
|
|
percent_done = static_cast<double>(current) / total;
|
|
|
|
piece_index = current;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-08-02 19:46:08 +00:00
|
|
|
|
|
|
|
// progress text
|
|
|
|
auto str = std::string{};
|
|
|
|
auto success = false;
|
|
|
|
auto const base = Glib::path_get_basename(builder_.top());
|
|
|
|
if (!is_done)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
str = fmt::format(_("Creating '{path}'"), fmt::arg("path", base));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
tr_error* error = future_.get();
|
|
|
|
|
|
|
|
if (error == nullptr)
|
|
|
|
{
|
|
|
|
builder_.save(target_, &error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error == nullptr)
|
|
|
|
{
|
|
|
|
str = fmt::format(_("Created '{path}'"), fmt::arg("path", base));
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = fmt::format(
|
|
|
|
_("Couldn't create '{path}': {error} ({error_code})"),
|
|
|
|
fmt::arg("path", base),
|
|
|
|
fmt::arg("error", error->message),
|
|
|
|
fmt::arg("error_code", error->code));
|
|
|
|
tr_error_free(error);
|
|
|
|
}
|
2009-12-07 04:06:14 +00:00
|
|
|
}
|
2009-09-17 01:28:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
gtr_label_set_text(*progress_label_, str);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* progress bar */
|
2022-08-02 19:46:08 +00:00
|
|
|
if (piece_index == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
str.clear();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
2013-01-04 19:45:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* how much data we've scanned through to generate checksums */
|
2022-03-15 14:52:16 +00:00
|
|
|
str = fmt::format(
|
|
|
|
_("Scanned {file_size}"),
|
2022-08-02 19:46:08 +00:00
|
|
|
fmt::arg("file_size", tr_strlsize(static_cast<uint64_t>(piece_index) * builder_.pieceSize())));
|
2009-09-17 01:28:45 +00:00
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
progress_bar_->set_fraction(percent_done);
|
2021-10-18 20:22:31 +00:00
|
|
|
progress_bar_->set_text(str);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* buttons */
|
2022-09-10 13:19:54 +00:00
|
|
|
set_response_sensitive(TR_GTK_RESPONSE_TYPE(CANCEL), !is_done);
|
|
|
|
set_response_sensitive(TR_GTK_RESPONSE_TYPE(CLOSE), is_done);
|
|
|
|
set_response_sensitive(TR_GTK_RESPONSE_TYPE(ACCEPT), is_done && success);
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
success_ = success;
|
2021-10-18 20:22:31 +00:00
|
|
|
return true;
|
2009-09-17 01:28:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
MakeProgressDialog::~MakeProgressDialog()
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
progress_tag_.disconnect();
|
2008-01-28 04:27:22 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MakeProgressDialog::addTorrent()
|
2008-01-28 04:27:22 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
tr_ctor* ctor = tr_ctorNew(core_->get_session());
|
2021-12-26 16:25:07 +00:00
|
|
|
tr_ctorSetMetainfoFromFile(ctor, target_.c_str(), nullptr);
|
2022-08-02 19:46:08 +00:00
|
|
|
tr_ctorSetDownloadDir(ctor, TR_FORCE, Glib::path_get_dirname(builder_.top()).c_str());
|
2021-10-18 20:22:31 +00:00
|
|
|
core_->add_ctor(ctor);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MakeProgressDialog::onProgressDialogResponse(int response)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (response)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2022-09-10 13:19:54 +00:00
|
|
|
case TR_GTK_RESPONSE_TYPE(CANCEL):
|
2022-08-02 19:46:08 +00:00
|
|
|
builder_.cancelChecksums();
|
2022-10-08 22:50:03 +00:00
|
|
|
close();
|
2013-01-04 19:45:39 +00:00
|
|
|
break;
|
|
|
|
|
2022-09-10 13:19:54 +00:00
|
|
|
case TR_GTK_RESPONSE_TYPE(ACCEPT):
|
2021-10-18 20:22:31 +00:00
|
|
|
addTorrent();
|
2021-10-06 17:24:02 +00:00
|
|
|
[[fallthrough]];
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-09-10 13:19:54 +00:00
|
|
|
case TR_GTK_RESPONSE_TYPE(CLOSE):
|
2022-10-08 22:50:03 +00:00
|
|
|
close();
|
2013-01-04 19:45:39 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
g_assert(0 && "unhandled response");
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
MakeProgressDialog::MakeProgressDialog(
|
2022-09-07 22:25:04 +00:00
|
|
|
BaseObjectType* cast_item,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
|
|
|
tr_metainfo_builder& metainfo_builder,
|
2022-08-02 19:46:08 +00:00
|
|
|
std::future<tr_error*> future,
|
2022-11-15 19:30:32 +00:00
|
|
|
std::string_view target,
|
2021-11-01 00:11:23 +00:00
|
|
|
Glib::RefPtr<Session> const& core)
|
2022-09-07 22:25:04 +00:00
|
|
|
: Gtk::Dialog(cast_item)
|
|
|
|
, builder_(metainfo_builder)
|
2022-08-02 19:46:08 +00:00
|
|
|
, future_{ std::move(future) }
|
2022-09-07 22:25:04 +00:00
|
|
|
, target_(target)
|
|
|
|
, core_(core)
|
|
|
|
, progress_label_(gtr_get_widget<Gtk::Label>(builder, "progress_label"))
|
|
|
|
, progress_bar_(gtr_get_widget<Gtk::ProgressBar>(builder, "progress_bar"))
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
signal_response().connect(sigc::mem_fun(*this, &MakeProgressDialog::onProgressDialogResponse));
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
progress_tag_ = Glib::signal_timeout().connect_seconds(
|
2021-11-12 09:12:50 +00:00
|
|
|
sigc::mem_fun(*this, &MakeProgressDialog::onProgressDialogRefresh),
|
2021-10-18 20:22:31 +00:00
|
|
|
SECONDARY_WINDOW_REFRESH_INTERVAL_SECONDS);
|
|
|
|
onProgressDialogRefresh();
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 19:30:32 +00:00
|
|
|
std::unique_ptr<MakeProgressDialog> MakeProgressDialog::create(
|
|
|
|
std::string_view target,
|
|
|
|
tr_metainfo_builder& metainfo_builder,
|
|
|
|
std::future<tr_error*> future,
|
|
|
|
Glib::RefPtr<Session> const& core)
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2022-09-07 22:25:04 +00:00
|
|
|
auto const builder = Gtk::Builder::create_from_resource(gtr_get_full_resource_path("MakeProgressDialog.ui"));
|
2022-11-15 19:30:32 +00:00
|
|
|
return std::unique_ptr<MakeProgressDialog>(gtr_get_widget_derived<MakeProgressDialog>(
|
2022-09-07 22:25:04 +00:00
|
|
|
builder,
|
|
|
|
"MakeProgressDialog",
|
2022-11-15 19:30:32 +00:00
|
|
|
metainfo_builder,
|
2022-09-07 22:25:04 +00:00
|
|
|
std::move(future),
|
|
|
|
target,
|
2022-11-15 19:30:32 +00:00
|
|
|
core));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MakeDialog::Impl::onResponse(int response)
|
|
|
|
{
|
|
|
|
if (response == TR_GTK_RESPONSE_TYPE(CLOSE))
|
|
|
|
{
|
|
|
|
dialog_.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response != TR_GTK_RESPONSE_TYPE(ACCEPT) || !builder_.has_value())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// destination file
|
|
|
|
auto const dir = destination_chooser_->get_filename();
|
|
|
|
auto const base = Glib::path_get_basename(builder_->top());
|
|
|
|
auto const target = fmt::format("{:s}/{:s}.torrent", dir, base);
|
|
|
|
|
|
|
|
// build the announce list
|
|
|
|
auto trackers = tr_announce_list{};
|
|
|
|
trackers.parse(announce_text_buffer_->get_text(false).raw());
|
|
|
|
builder_->setAnnounceList(std::move(trackers));
|
|
|
|
|
|
|
|
// comment
|
|
|
|
if (comment_check_->get_active())
|
|
|
|
{
|
|
|
|
builder_->setComment(comment_entry_->get_text().raw());
|
|
|
|
}
|
|
|
|
|
|
|
|
// source
|
|
|
|
if (source_check_->get_active())
|
|
|
|
{
|
|
|
|
builder_->setSource(source_entry_->get_text().raw());
|
|
|
|
}
|
|
|
|
|
|
|
|
builder_->setPrivate(private_check_->get_active());
|
|
|
|
|
|
|
|
// build the .torrent
|
|
|
|
progress_dialog_ = MakeProgressDialog::create(target, *builder_, builder_->makeChecksums(), core_);
|
|
|
|
progress_dialog_->set_transient_for(dialog_);
|
2022-10-08 22:50:03 +00:00
|
|
|
gtr_window_on_close(
|
|
|
|
*progress_dialog_,
|
2021-10-18 20:22:31 +00:00
|
|
|
[this]()
|
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
auto const success = progress_dialog_->success();
|
2021-10-18 20:22:31 +00:00
|
|
|
progress_dialog_.reset();
|
2022-08-02 19:46:08 +00:00
|
|
|
if (success)
|
2021-10-18 20:22:31 +00:00
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
dialog_.close();
|
2021-10-18 20:22:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
progress_dialog_->show();
|
|
|
|
}
|
2009-09-17 01:28:45 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MakeDialog::Impl::updatePiecesLabel()
|
2009-09-17 01:28:45 +00:00
|
|
|
{
|
2022-09-07 22:25:04 +00:00
|
|
|
auto gstr = Glib::ustring();
|
2007-06-18 03:40:41 +00:00
|
|
|
|
2022-11-15 19:30:32 +00:00
|
|
|
if (!builder_.has_value() || std::empty(builder_->top()))
|
2009-09-17 01:28:45 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
gstr += _("No source selected");
|
2022-06-01 14:11:30 +00:00
|
|
|
piece_size_scale_->set_visible(false);
|
2008-03-19 19:23:54 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-09-17 01:28:45 +00:00
|
|
|
{
|
2022-04-07 13:23:04 +00:00
|
|
|
gstr += fmt::format(
|
2022-08-02 19:46:08 +00:00
|
|
|
ngettext("{total_size} in {file_count:L} file", "{total_size} in {file_count:L} files", builder_->fileCount()),
|
|
|
|
fmt::arg("total_size", tr_strlsize(builder_->totalSize())),
|
|
|
|
fmt::arg("file_count", builder_->fileCount()));
|
2022-04-07 13:23:04 +00:00
|
|
|
gstr += ' ';
|
|
|
|
gstr += fmt::format(
|
|
|
|
ngettext(
|
|
|
|
"({piece_count} BitTorrent piece @ {piece_size})",
|
|
|
|
"({piece_count} BitTorrent pieces @ {piece_size})",
|
2022-08-02 19:46:08 +00:00
|
|
|
builder_->pieceCount()),
|
|
|
|
fmt::arg("piece_count", builder_->pieceCount()),
|
|
|
|
fmt::arg("piece_size", tr_formatter_mem_B(builder_->pieceSize())));
|
2008-03-19 19:23:54 +00:00
|
|
|
}
|
2013-01-04 19:45:39 +00:00
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
pieces_lb_->set_text(gstr);
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 19:30:32 +00:00
|
|
|
void MakeDialog::Impl::configurePieceSizeScale(uint32_t piece_size)
|
2022-06-01 14:11:30 +00:00
|
|
|
{
|
2022-09-06 13:43:27 +00:00
|
|
|
// the below lower & upper bounds would allow piece size selection between approx 1KiB - 64MiB
|
2022-11-15 19:30:32 +00:00
|
|
|
auto adjustment = Gtk::Adjustment::create(log2(piece_size), 10, 26, 1.0, 1.0);
|
2022-06-01 14:11:30 +00:00
|
|
|
piece_size_scale_->set_adjustment(adjustment);
|
|
|
|
piece_size_scale_->set_visible(true);
|
|
|
|
}
|
|
|
|
|
2022-08-02 19:46:08 +00:00
|
|
|
void MakeDialog::Impl::setFilename(std::string_view filename)
|
2008-05-07 04:06:07 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
builder_.reset();
|
2009-09-17 01:28:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
if (!filename.empty())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
builder_.emplace(filename);
|
2022-11-15 19:30:32 +00:00
|
|
|
configurePieceSizeScale(builder_->pieceSize());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-09-17 01:28:45 +00:00
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
updatePiecesLabel();
|
2008-06-01 15:46:20 +00:00
|
|
|
}
|
2008-05-07 04:06:07 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
void MakeDialog::Impl::onChooserChosen(PathButton* chooser)
|
2008-06-01 15:46:20 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
setFilename(chooser->get_filename());
|
2008-05-07 04:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 20:46:23 +00:00
|
|
|
void MakeDialog::Impl::onSourceToggled(Gtk::CheckButton* tb, PathButton* chooser)
|
2007-06-18 04:52:03 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
if (tb->get_active())
|
2008-05-07 04:06:07 +00:00
|
|
|
{
|
2022-11-15 00:53:12 +00:00
|
|
|
onChooserChosen(chooser);
|
2008-05-07 04:06:07 +00:00
|
|
|
}
|
2007-06-18 04:52:03 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
bool MakeDialog::Impl::set_dropped_source_path(std::string const& filename)
|
|
|
|
{
|
|
|
|
if (Glib::file_test(filename, TR_GLIB_FILE_TEST(IS_DIR)))
|
|
|
|
{
|
|
|
|
/* a directory was dragged onto the dialog... */
|
|
|
|
folder_radio_->set_active(true);
|
|
|
|
folder_chooser_->set_filename(filename);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Glib::file_test(filename, TR_GLIB_FILE_TEST(IS_REGULAR)))
|
|
|
|
{
|
|
|
|
/* a file was dragged on to the dialog... */
|
|
|
|
file_radio_->set_active(true);
|
|
|
|
file_chooser_->set_filename(filename);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if GTKMM_CHECK_VERSION(4, 0, 0)
|
|
|
|
|
|
|
|
bool MakeDialog::Impl::on_drag_data_received(Glib::ValueBase const& value, double /*x*/, double /*y*/)
|
|
|
|
{
|
|
|
|
if (G_VALUE_HOLDS(value.gobj(), GDK_TYPE_FILE_LIST))
|
|
|
|
{
|
|
|
|
FileListValue files_value;
|
|
|
|
files_value.init(value.gobj());
|
|
|
|
if (auto const files = FileListHandler::slist_to_vector(files_value.get(), Glib::OwnershipType::OWNERSHIP_NONE);
|
|
|
|
!files.empty())
|
|
|
|
{
|
|
|
|
return set_dropped_source_path(files.front()->get_path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
void MakeDialog::Impl::on_drag_data_received(
|
|
|
|
Glib::RefPtr<Gdk::DragContext> const& drag_context,
|
|
|
|
int /*x*/,
|
|
|
|
int /*y*/,
|
|
|
|
Gtk::SelectionData const& selection_data,
|
|
|
|
guint /*info*/,
|
|
|
|
guint time_)
|
2010-08-02 20:55:11 +00:00
|
|
|
{
|
2021-10-18 20:22:31 +00:00
|
|
|
bool success = false;
|
2010-08-02 20:55:11 +00:00
|
|
|
|
2022-01-24 00:53:35 +00:00
|
|
|
if (auto const uris = selection_data.get_uris(); !uris.empty())
|
2010-08-02 20:55:11 +00:00
|
|
|
{
|
2022-10-08 22:50:03 +00:00
|
|
|
success = set_dropped_source_path(Glib::filename_from_uri(uris.front()));
|
2010-08-02 20:55:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 20:22:31 +00:00
|
|
|
drag_context->drag_finish(success, false, time_);
|
|
|
|
}
|
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
MakeDialog::MakeDialog(
|
|
|
|
BaseObjectType* cast_item,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& builder,
|
|
|
|
Gtk::Window& parent,
|
|
|
|
Glib::RefPtr<Session> const& core)
|
|
|
|
: Gtk::Dialog(cast_item)
|
|
|
|
, impl_(std::make_unique<Impl>(*this, builder, core))
|
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
|
|
|
MakeDialog::~MakeDialog() = default;
|
|
|
|
|
|
|
|
std::unique_ptr<MakeDialog> MakeDialog::create(Gtk::Window& parent, Glib::RefPtr<Session> const& core)
|
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("MakeDialog.ui"));
|
|
|
|
return std::unique_ptr<MakeDialog>(gtr_get_widget_derived<MakeDialog>(builder, "MakeDialog", parent, core));
|
2010-08-02 20:55:11 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
MakeDialog::Impl::Impl(MakeDialog& dialog, Glib::RefPtr<Gtk::Builder> const& builder, Glib::RefPtr<Session> const& core)
|
2021-10-18 20:22:31 +00:00
|
|
|
: dialog_(dialog)
|
|
|
|
, core_(core)
|
2022-10-08 22:50:03 +00:00
|
|
|
, file_radio_(gtr_get_widget<Gtk::CheckButton>(builder, "source_file_radio"))
|
|
|
|
, file_chooser_(gtr_get_widget_derived<PathButton>(builder, "source_file_button"))
|
|
|
|
, folder_radio_(gtr_get_widget<Gtk::CheckButton>(builder, "source_folder_radio"))
|
|
|
|
, folder_chooser_(gtr_get_widget_derived<PathButton>(builder, "source_folder_button"))
|
2022-09-07 22:25:04 +00:00
|
|
|
, pieces_lb_(gtr_get_widget<Gtk::Label>(builder, "source_size_label"))
|
|
|
|
, piece_size_scale_(gtr_get_widget<Gtk::Scale>(builder, "piece_size_scale"))
|
2022-10-08 22:50:03 +00:00
|
|
|
, destination_chooser_(gtr_get_widget_derived<PathButton>(builder, "destination_button"))
|
2022-09-07 22:25:04 +00:00
|
|
|
, comment_check_(gtr_get_widget<Gtk::CheckButton>(builder, "comment_check"))
|
|
|
|
, comment_entry_(gtr_get_widget<Gtk::Entry>(builder, "comment_entry"))
|
|
|
|
, private_check_(gtr_get_widget<Gtk::CheckButton>(builder, "private_check"))
|
|
|
|
, source_check_(gtr_get_widget<Gtk::CheckButton>(builder, "source_check"))
|
|
|
|
, source_entry_(gtr_get_widget<Gtk::Entry>(builder, "source_entry"))
|
|
|
|
, announce_text_buffer_(gtr_get_widget<Gtk::TextView>(builder, "trackers_view")->get_buffer())
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
dialog_.signal_response().connect(sigc::mem_fun(*this, &Impl::onResponse));
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
destination_chooser_->set_filename(Glib::get_user_special_dir(TR_GLIB_USER_DIRECTORY(DESKTOP)));
|
2021-10-18 20:22:31 +00:00
|
|
|
|
|
|
|
folder_radio_->signal_toggled().connect([this]() { onSourceToggled(folder_radio_, folder_chooser_); });
|
|
|
|
folder_chooser_->signal_selection_changed().connect([this]() { onChooserChosen(folder_chooser_); });
|
|
|
|
|
|
|
|
file_radio_->signal_toggled().connect([this]() { onSourceToggled(file_radio_, file_chooser_); });
|
|
|
|
file_chooser_->signal_selection_changed().connect([this]() { onChooserChosen(file_chooser_); });
|
|
|
|
|
2022-03-30 19:59:13 +00:00
|
|
|
pieces_lb_->set_markup(fmt::format(FMT_STRING("<i>{:s}</i>"), _("No source selected")));
|
2021-10-18 20:22:31 +00:00
|
|
|
|
2022-06-01 14:11:30 +00:00
|
|
|
piece_size_scale_->set_visible(false);
|
|
|
|
piece_size_scale_->signal_value_changed().connect([this]() { onPieceSizeUpdated(); });
|
2022-09-07 22:25:04 +00:00
|
|
|
|
2022-10-08 22:50:03 +00:00
|
|
|
#if GTKMM_CHECK_VERSION(4, 0, 0)
|
|
|
|
auto drop_controller = Gtk::DropTarget::create(GDK_TYPE_FILE_LIST, Gdk::DragAction::COPY);
|
|
|
|
drop_controller->signal_drop().connect(sigc::mem_fun(*this, &Impl::on_drag_data_received), false);
|
|
|
|
dialog_.add_controller(drop_controller);
|
|
|
|
#else
|
2021-10-18 20:22:31 +00:00
|
|
|
dialog_.drag_dest_set(Gtk::DEST_DEFAULT_ALL, Gdk::ACTION_COPY);
|
|
|
|
dialog_.drag_dest_add_uri_targets();
|
2021-11-12 09:12:50 +00:00
|
|
|
dialog_.signal_drag_data_received().connect(sigc::mem_fun(*this, &Impl::on_drag_data_received));
|
2022-10-08 22:50:03 +00:00
|
|
|
#endif
|
2007-06-18 03:40:41 +00:00
|
|
|
}
|
2022-06-01 14:11:30 +00:00
|
|
|
|
|
|
|
void MakeDialog::Impl::onPieceSizeUpdated()
|
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
if (builder_)
|
2022-06-01 14:11:30 +00:00
|
|
|
{
|
2022-08-02 19:46:08 +00:00
|
|
|
builder_->setPieceSize(static_cast<uint32_t>(std::pow(2, piece_size_scale_->get_value())));
|
2022-06-01 14:11:30 +00:00
|
|
|
updatePiecesLabel();
|
|
|
|
}
|
2022-07-26 02:45:54 +00:00
|
|
|
}
|