2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2013-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.
|
2013-02-09 04:05:03 +00:00
|
|
|
|
2015-04-15 00:08:24 +00:00
|
|
|
#include <QDir>
|
|
|
|
|
2013-02-09 04:05:03 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/variant.h>
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Formatter.h"
|
|
|
|
#include "FreeSpaceLabel.h"
|
2016-04-19 20:41:59 +00:00
|
|
|
#include "RpcQueue.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Session.h"
|
2020-07-27 04:30:58 +00:00
|
|
|
#include "VariantHelpers.h"
|
|
|
|
|
|
|
|
using ::trqt::variant_helpers::dictAdd;
|
|
|
|
using ::trqt::variant_helpers::dictFind;
|
2013-02-09 04:05:03 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
int const IntervalMSec = 15000;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
FreeSpaceLabel::FreeSpaceLabel(QWidget* parent)
|
|
|
|
: QLabel(parent)
|
|
|
|
, timer_(this)
|
2013-02-09 04:05:03 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
timer_.setSingleShot(true);
|
2020-06-05 19:02:11 +00:00
|
|
|
timer_.setInterval(IntervalMSec);
|
2013-02-09 04:05:03 +00:00
|
|
|
|
2020-10-31 18:56:12 +00:00
|
|
|
connect(&timer_, &QTimer::timeout, this, &FreeSpaceLabel::onTimer);
|
2014-12-26 18:41:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FreeSpaceLabel::setSession(Session& session)
|
2014-12-26 18:41:47 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ == &session)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-26 18:41:47 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
session_ = &session;
|
2017-04-19 12:04:45 +00:00
|
|
|
onTimer();
|
2013-02-09 04:42:07 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void FreeSpaceLabel::setPath(QString const& path)
|
2013-02-09 04:42:07 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (path_ != path)
|
2013-02-09 04:42:07 +00:00
|
|
|
{
|
2022-10-11 15:39:41 +00:00
|
|
|
setText(tr("<i>Calculating Free Space…</i>"));
|
2020-05-27 21:53:12 +00:00
|
|
|
path_ = path;
|
2017-04-19 12:04:45 +00:00
|
|
|
onTimer();
|
2013-02-09 04:42:07 +00:00
|
|
|
}
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void FreeSpaceLabel::onTimer()
|
2013-02-09 04:05:03 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
timer_.stop();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ == nullptr || path_.isEmpty())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-26 18:41:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 1);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, TR_KEY_path, path_);
|
2014-12-26 18:41:47 +00:00
|
|
|
|
2021-05-18 17:36:46 +00:00
|
|
|
auto* q = new RpcQueue(this);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add([this, &args]() { return session_->exec("free-space", &args); });
|
2013-07-27 21:58:14 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add(
|
|
|
|
[this](RpcResponse const& r)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-05-22 15:53:52 +00:00
|
|
|
// update the label
|
2022-02-03 13:02:11 +00:00
|
|
|
if (auto const bytes = dictFind<int64_t>(r.args.get(), TR_KEY_size_bytes); bytes && *bytes > 1)
|
2021-05-22 15:53:52 +00:00
|
|
|
{
|
|
|
|
setText(tr("%1 free").arg(Formatter::get().sizeToString(*bytes)));
|
|
|
|
}
|
|
|
|
else
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-05-22 15:53:52 +00:00
|
|
|
setText(QString());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 15:53:52 +00:00
|
|
|
// update the tooltip
|
|
|
|
auto const path = dictFind<QString>(r.args.get(), TR_KEY_path);
|
2022-11-27 20:56:34 +00:00
|
|
|
setToolTip(QDir::toNativeSeparators(path.value_or(QString{})));
|
2021-05-22 15:53:52 +00:00
|
|
|
|
|
|
|
timer_.start();
|
2017-04-19 12:04:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
q->run();
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|