2013-02-09 04:05:03 +00:00
|
|
|
/*
|
2016-04-19 20:41:59 +00:00
|
|
|
* This file Copyright (C) 2013-2016 Mnemosyne LLC
|
2013-02-09 04:05:03 +00:00
|
|
|
*
|
2014-12-21 23:49:39 +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.
|
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
|
|
|
|
|
|
|
|
FreeSpaceLabel::FreeSpaceLabel(QWidget* parent) :
|
|
|
|
QLabel(parent),
|
2020-05-27 21:53:12 +00:00
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* q = new RpcQueue();
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2021-01-25 04:20:21 +00:00
|
|
|
auto* alive = new bool(true);
|
|
|
|
connect(this, &QObject::destroyed, [alive] { *alive = false; });
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
q->add([this, &args]()
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return session_->exec("free-space", &args);
|
2017-04-19 12:04:45 +00:00
|
|
|
});
|
2013-07-27 21:58:14 +00:00
|
|
|
|
2021-01-25 04:20:21 +00:00
|
|
|
q->add([this, alive](RpcResponse const& r)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-01-25 04:20:21 +00:00
|
|
|
if (*alive)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-01-25 04:20:21 +00:00
|
|
|
// update the label
|
|
|
|
auto const bytes = dictFind<int64_t>(r.args.get(), TR_KEY_size_bytes);
|
|
|
|
if (bytes && *bytes > 1)
|
|
|
|
{
|
|
|
|
setText(tr("%1 free").arg(Formatter::get().sizeToString(*bytes)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the tooltip
|
|
|
|
auto const path = dictFind<QString>(r.args.get(), TR_KEY_path);
|
|
|
|
setToolTip(QDir::toNativeSeparators(path ? *path : QString()));
|
|
|
|
|
|
|
|
timer_.start();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 04:20:21 +00:00
|
|
|
delete alive;
|
2017-04-19 12:04:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
q->run();
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|