1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-27 10:07:40 +00:00
transmission/qt/FreeSpaceLabel.cc
Charles Kerr 51573a3c1e
chore: clang-tidy cleanups (#1287)
* chore: fix syntax error in clang-tidy config file
2020-06-05 14:02:11 -05:00

104 lines
2.1 KiB
C++

/*
* This file Copyright (C) 2013-2016 Mnemosyne LLC
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*
*/
#include <QDir>
#include <libtransmission/transmission.h>
#include <libtransmission/variant.h>
#include "Formatter.h"
#include "FreeSpaceLabel.h"
#include "RpcQueue.h"
#include "Session.h"
namespace
{
int const IntervalMSec = 15000;
} // namespace
FreeSpaceLabel::FreeSpaceLabel(QWidget* parent) :
QLabel(parent),
timer_(this)
{
timer_.setSingleShot(true);
timer_.setInterval(IntervalMSec);
connect(&timer_, SIGNAL(timeout()), this, SLOT(onTimer()));
}
void FreeSpaceLabel::setSession(Session& session)
{
if (session_ == &session)
{
return;
}
session_ = &session;
onTimer();
}
void FreeSpaceLabel::setPath(QString const& path)
{
if (path_ != path)
{
setText(tr("<i>Calculating Free Space...</i>"));
path_ = path;
onTimer();
}
}
void FreeSpaceLabel::onTimer()
{
timer_.stop();
if (session_ == nullptr || path_.isEmpty())
{
return;
}
tr_variant args;
tr_variantInitDict(&args, 1);
tr_variantDictAddStr(&args, TR_KEY_path, path_.toUtf8().constData());
auto* q = new RpcQueue();
q->add([this, &args]()
{
return session_->exec("free-space", &args);
});
q->add([this](RpcResponse const& r)
{
QString str;
// update the label
int64_t bytes = -1;
if (tr_variantDictFindInt(r.args.get(), TR_KEY_size_bytes, &bytes) && bytes >= 0)
{
setText(tr("%1 free").arg(Formatter::sizeToString(bytes)));
}
else
{
setText(QString());
}
// update the tooltip
size_t len = 0;
char const* path = nullptr;
tr_variantDictFindStr(r.args.get(), TR_KEY_path, &path, &len);
str = QString::fromUtf8(path, len);
setToolTip(QDir::toNativeSeparators(str));
timer_.start();
});
q->run();
}