source: trunk/qt/freespace-label.cc @ 14378

Last change on this file since 14378 was 14378, checked in by mikedld, 8 years ago

Remove redundant "struct" keyword use in C++ code (Qt client)

  • Property svn:keywords set to Date Rev Author Id
File size: 2.2 KB
Line 
1/*
2 * This file Copyright (C) 2013-2014 Mnemosyne LLC
3 *
4 * It may be used under the GNU Public License v2 or v3 licenses,
5 * or any future license endorsed by Mnemosyne LLC.
6 *
7 * $Id: freespace-label.cc 14378 2014-12-12 23:21:04Z mikedld $
8 */
9
10#include <libtransmission/transmission.h>
11#include <libtransmission/variant.h>
12
13#include "formatter.h"
14#include "freespace-label.h"
15#include "session.h"
16
17namespace
18{
19  static const int INTERVAL_MSEC = 15000;
20}
21
22FreespaceLabel::FreespaceLabel (Session        & session,
23                                const QString  & path,
24                                QWidget        * parent):
25  QLabel (parent),
26  mySession (session),
27  myTag (-1),
28  myTimer (this)
29{
30  myTimer.setSingleShot (false);
31  myTimer.setInterval (INTERVAL_MSEC);
32  myTimer.start ();
33
34  connect (&myTimer, SIGNAL(timeout()), this, SLOT(onTimer()));
35
36  connect (&mySession, SIGNAL(executed(int64_t, const QString&, tr_variant *)),
37           this,       SLOT(onSessionExecuted(int64_t, const QString&, tr_variant *)));
38
39  setPath (path);
40}
41
42void
43FreespaceLabel::setPath (const QString& path)
44{
45  if (myPath != path)
46    {
47      setText (tr("<i>Calculating Free Space...</i>"));
48      myPath = path;
49      onTimer ();
50    }
51}
52
53void
54FreespaceLabel::onTimer ()
55{
56  const int64_t tag = mySession.getUniqueTag ();
57  const QByteArray myPathUtf8 = myPath.toUtf8 ();
58
59  myTag = tag;
60  tr_variant top;
61  tr_variantInitDict (&top, 3);
62  tr_variantDictAddStr (&top, TR_KEY_method, "free-space");
63  tr_variantDictAddInt (&top, TR_KEY_tag, tag);
64  tr_variant * args = tr_variantDictAddDict (&top, TR_KEY_arguments, 1);
65  tr_variantDictAddStr (args, TR_KEY_path, myPathUtf8.constData());
66  mySession.exec (&top);
67  tr_variantFree (&top);
68}
69
70void
71FreespaceLabel::onSessionExecuted (int64_t tag, const QString& result, tr_variant * arguments)
72{
73  Q_UNUSED (result);
74
75  if (tag != myTag)
76    return;
77
78  QString str;
79
80  // update the label
81  int64_t bytes = -1;
82  tr_variantDictFindInt (arguments, TR_KEY_size_bytes, &bytes);
83  if (bytes >= 0)
84    setText (tr("%1 free").arg(Formatter::sizeToString (bytes)));
85  else
86    setText ("");
87
88  // update the tooltip
89  size_t len = 0;
90  const char * path = 0;
91  tr_variantDictFindStr (arguments, TR_KEY_path, &path, &len);
92  str = QString::fromUtf8 (path, len);
93  setToolTip (str);
94}
Note: See TracBrowser for help on using the repository browser.