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-01-19 01:09:44 +00:00
|
|
|
* It may be used under the GNU Public License v2 or v3 licenses,
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
FreespaceLabel :: FreespaceLabel (Session & session,
|
|
|
|
const QString & path,
|
|
|
|
QWidget * parent):
|
|
|
|
QLabel (parent),
|
|
|
|
mySession (session),
|
|
|
|
myTag (-1),
|
|
|
|
myTimer (this)
|
|
|
|
{
|
|
|
|
myTimer.setSingleShot (false);
|
|
|
|
myTimer.setInterval (INTERVAL_MSEC);
|
|
|
|
myTimer.start ();
|
|
|
|
|
|
|
|
connect (&myTimer, SIGNAL(timeout()), this, SLOT(onTimer()));
|
|
|
|
|
2013-08-29 00:37:37 +00:00
|
|
|
connect (&mySession, SIGNAL(executed(int64_t, const QString&, struct tr_variant*)),
|
|
|
|
this, SLOT(onSessionExecuted(int64_t, const QString&, struct tr_variant*)));
|
2013-02-09 04:05:03 +00:00
|
|
|
|
2013-02-09 04:42:07 +00:00
|
|
|
setPath (path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FreespaceLabel :: setPath (const QString& path)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
FreespaceLabel :: onTimer ()
|
|
|
|
{
|
2013-08-29 00:37:37 +00:00
|
|
|
const int64_t tag = mySession.getUniqueTag ();
|
2013-02-09 04:05:03 +00:00
|
|
|
const QByteArray myPathUtf8 = myPath.toUtf8 ();
|
|
|
|
|
|
|
|
myTag = tag;
|
|
|
|
tr_variant top;
|
|
|
|
tr_variantInitDict (&top, 3);
|
|
|
|
tr_variantDictAddStr (&top, TR_KEY_method, "free-space");
|
|
|
|
tr_variantDictAddInt (&top, TR_KEY_tag, tag);
|
|
|
|
tr_variant * args = tr_variantDictAddDict (&top, TR_KEY_arguments, 1);
|
|
|
|
tr_variantDictAddStr (args, TR_KEY_path, myPathUtf8.constData());
|
|
|
|
mySession.exec (&top);
|
|
|
|
tr_variantFree (&top);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-29 00:37:37 +00:00
|
|
|
FreespaceLabel :: onSessionExecuted (int64_t tag, const QString& result, struct 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
|
|
|
|
setText ("");
|
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);
|
|
|
|
}
|