2013-02-09 04:05:03 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2013-2014 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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/variant.h>
|
|
|
|
|
|
|
|
#include "formatter.h"
|
|
|
|
#include "freespace-label.h"
|
|
|
|
#include "session.h"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2013-04-10 03:17:15 +00:00
|
|
|
static const int INTERVAL_MSEC = 15000;
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|
|
|
|
|
2014-12-26 18:41:47 +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
|
|
|
myTag (-1),
|
|
|
|
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
|
|
|
|
FreespaceLabel::setSession (Session& session)
|
|
|
|
{
|
|
|
|
if (mySession == &session)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mySession != nullptr)
|
|
|
|
disconnect (mySession, nullptr, this, nullptr);
|
2013-02-09 04:05:03 +00:00
|
|
|
|
2014-12-26 18:41:47 +00:00
|
|
|
mySession = &session;
|
2013-02-09 04:05:03 +00:00
|
|
|
|
2014-12-26 18:41:47 +00:00
|
|
|
connect (mySession, SIGNAL (executed (int64_t, QString, tr_variant *)),
|
|
|
|
this, SLOT (onSessionExecuted (int64_t, QString, tr_variant *)));
|
|
|
|
|
|
|
|
onTimer ();
|
2013-02-09 04:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:05:10 +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
|
2014-12-12 23:05:10 +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());
|
|
|
|
|
|
|
|
myTag = mySession->getUniqueTag ();
|
|
|
|
mySession->exec ("free-space", &args, myTag);
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 23:21:04 +00:00
|
|
|
FreespaceLabel::onSessionExecuted (int64_t tag, const QString& result, tr_variant * arguments)
|
2013-02-09 04:05:03 +00:00
|
|
|
{
|
2013-07-27 21:58:14 +00:00
|
|
|
Q_UNUSED (result);
|
|
|
|
|
2013-02-09 04:05:03 +00:00
|
|
|
if (tag != myTag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString str;
|
|
|
|
|
|
|
|
// update the label
|
2013-08-29 00:37:37 +00:00
|
|
|
int64_t bytes = -1;
|
2013-02-09 04:05:03 +00:00
|
|
|
tr_variantDictFindInt (arguments, TR_KEY_size_bytes, &bytes);
|
2013-04-10 03:05:31 +00:00
|
|
|
if (bytes >= 0)
|
|
|
|
setText (tr("%1 free").arg(Formatter::sizeToString (bytes)));
|
2013-04-13 12:22:07 +00:00
|
|
|
else
|
2015-01-29 21:53:05 +00:00
|
|
|
setText (QString ());
|
2013-02-09 04:05:03 +00:00
|
|
|
|
|
|
|
// update the tooltip
|
|
|
|
size_t len = 0;
|
|
|
|
const char * path = 0;
|
|
|
|
tr_variantDictFindStr (arguments, TR_KEY_path, &path, &len);
|
|
|
|
str = QString::fromUtf8 (path, len);
|
|
|
|
setToolTip (str);
|
2014-12-26 18:41:47 +00:00
|
|
|
|
|
|
|
myTimer.start ();
|
2013-02-09 04:05:03 +00:00
|
|
|
}
|