2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-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.
|
2021-11-01 00:11:23 +00:00
|
|
|
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <string_view>
|
2022-01-13 02:13:58 +00:00
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
#include <glibmm/i18n.h>
|
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
#include "FreeSpaceLabel.h"
|
|
|
|
#include "Session.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
class FreeSpaceLabel::Impl
|
|
|
|
{
|
|
|
|
public:
|
2022-04-01 19:16:33 +00:00
|
|
|
Impl(FreeSpaceLabel& label, Glib::RefPtr<Session> const& core, std::string_view dir);
|
2021-11-01 00:11:23 +00:00
|
|
|
~Impl();
|
|
|
|
|
2021-12-14 08:43:27 +00:00
|
|
|
TR_DISABLE_COPY_MOVE(Impl)
|
|
|
|
|
2022-04-01 19:16:33 +00:00
|
|
|
void set_dir(std::string_view dir);
|
2021-11-01 00:11:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool on_freespace_timer();
|
|
|
|
|
|
|
|
FreeSpaceLabel& label_;
|
|
|
|
Glib::RefPtr<Session> const core_;
|
|
|
|
std::string dir_;
|
|
|
|
sigc::connection timer_id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
FreeSpaceLabel::Impl::~Impl()
|
|
|
|
{
|
|
|
|
timer_id_.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FreeSpaceLabel::Impl::on_freespace_timer()
|
|
|
|
{
|
2021-12-01 23:11:57 +00:00
|
|
|
if (core_->get_session() == nullptr)
|
2021-11-01 00:11:23 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-13 18:09:14 +00:00
|
|
|
auto const bytes = tr_dirSpace(dir_).free;
|
2022-03-21 14:15:48 +00:00
|
|
|
auto const text = bytes < 0 ? _("Error") : fmt::format(_("{disk_space} free"), fmt::arg("disk_space", tr_strlsize(bytes)));
|
2022-03-30 19:59:13 +00:00
|
|
|
label_.set_markup(fmt::format(FMT_STRING("<i>{:s}</i>"), text));
|
2021-11-01 00:11:23 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-01 19:16:33 +00:00
|
|
|
FreeSpaceLabel::FreeSpaceLabel(Glib::RefPtr<Session> const& core, std::string_view dir)
|
2021-11-01 00:11:23 +00:00
|
|
|
: Gtk::Label()
|
|
|
|
, impl_(std::make_unique<Impl>(*this, core, dir))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-07 22:25:04 +00:00
|
|
|
FreeSpaceLabel::FreeSpaceLabel(
|
|
|
|
BaseObjectType* cast_item,
|
|
|
|
Glib::RefPtr<Gtk::Builder> const& /*builder*/,
|
|
|
|
Glib::RefPtr<Session> const& core,
|
|
|
|
std::string_view dir)
|
|
|
|
: Gtk::Label(cast_item)
|
|
|
|
, impl_(std::make_unique<Impl>(*this, core, dir))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-01 00:11:23 +00:00
|
|
|
FreeSpaceLabel::~FreeSpaceLabel() = default;
|
|
|
|
|
2022-04-01 19:16:33 +00:00
|
|
|
FreeSpaceLabel::Impl::Impl(FreeSpaceLabel& label, Glib::RefPtr<Session> const& core, std::string_view dir)
|
2021-11-01 00:11:23 +00:00
|
|
|
: label_(label)
|
|
|
|
, core_(core)
|
|
|
|
, dir_(dir)
|
|
|
|
{
|
2021-11-12 09:12:50 +00:00
|
|
|
timer_id_ = Glib::signal_timeout().connect_seconds(sigc::mem_fun(*this, &Impl::on_freespace_timer), 3);
|
2021-11-01 00:11:23 +00:00
|
|
|
on_freespace_timer();
|
|
|
|
}
|
|
|
|
|
2022-04-01 19:16:33 +00:00
|
|
|
void FreeSpaceLabel::set_dir(std::string_view dir)
|
2021-11-01 00:11:23 +00:00
|
|
|
{
|
|
|
|
impl_->set_dir(dir);
|
|
|
|
}
|
|
|
|
|
2022-04-01 19:16:33 +00:00
|
|
|
void FreeSpaceLabel::Impl::set_dir(std::string_view dir)
|
2021-11-01 00:11:23 +00:00
|
|
|
{
|
|
|
|
dir_ = dir;
|
|
|
|
on_freespace_timer();
|
|
|
|
}
|