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
|
|
|
*
|
2014-01-19 01:29:38 +00:00
|
|
|
* $Id$
|
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"
|
2013-02-09 04:05:03 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2013-04-10 03:17:15 +00:00
|
|
|
static const int INTERVAL_MSEC = 15000;
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
FreeSpaceLabel::FreeSpaceLabel (QWidget * parent):
|
2013-02-09 04:05:03 +00:00
|
|
|
QLabel (parent),
|
2014-12-26 18:41:47 +00:00
|
|
|
mySession (nullptr),
|
2013-02-09 04:05:03 +00:00
|
|
|
myTimer (this)
|
|
|
|
{
|
2014-12-26 18:41:47 +00:00
|
|
|
myTimer.setSingleShot (true);
|
2013-02-09 04:05:03 +00:00
|
|
|
myTimer.setInterval (INTERVAL_MSEC);
|
|
|
|
|
2014-12-26 18:41:47 +00:00
|
|
|
connect (&myTimer, SIGNAL (timeout ()), this, SLOT (onTimer ()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-06-10 21:27:11 +00:00
|
|
|
FreeSpaceLabel::setSession (Session& session)
|
2014-12-26 18:41:47 +00:00
|
|
|
{
|
|
|
|
if (mySession == &session)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mySession = &session;
|
|
|
|
onTimer ();
|
2013-02-09 04:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-06-10 21:27:11 +00:00
|
|
|
FreeSpaceLabel::setPath (const QString& path)
|
2013-02-09 04:42:07 +00:00
|
|
|
{
|
|
|
|
if (myPath != path)
|
|
|
|
{
|
2013-02-18 14:45:44 +00:00
|
|
|
setText (tr("<i>Calculating Free Space...</i>"));
|
2013-02-09 04:42:07 +00:00
|
|
|
myPath = path;
|
|
|
|
onTimer ();
|
|
|
|
}
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-06-10 21:27:11 +00:00
|
|
|
FreeSpaceLabel::onTimer ()
|
2013-02-09 04:05:03 +00:00
|
|
|
{
|
2014-12-26 18:41:47 +00:00
|
|
|
myTimer.stop ();
|
|
|
|
|
|
|
|
if (mySession == nullptr || myPath.isEmpty ())
|
|
|
|
return;
|
|
|
|
|
2014-12-27 20:03:10 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict (&args, 1);
|
|
|
|
tr_variantDictAddStr (&args, TR_KEY_path, myPath.toUtf8 ().constData());
|
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
RpcQueue * q = new RpcQueue ();
|
2013-07-27 21:58:14 +00:00
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
q->add (
|
|
|
|
[this, &args] ()
|
|
|
|
{
|
|
|
|
return mySession->exec ("free-space", &args);
|
|
|
|
});
|
2014-12-26 18:41:47 +00:00
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
q->add (
|
|
|
|
[this] (const RpcResponse& 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;
|
|
|
|
const char * path = 0;
|
|
|
|
tr_variantDictFindStr (r.args.get (), TR_KEY_path, &path, &len);
|
|
|
|
str = QString::fromUtf8 (path, len);
|
|
|
|
setToolTip (QDir::toNativeSeparators (str));
|
|
|
|
|
|
|
|
myTimer.start ();
|
|
|
|
});
|
|
|
|
|
|
|
|
q->run ();
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|