1 | /* |
---|
2 | * This file Copyright (C) Mnemosyne LLC |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License version 2 |
---|
6 | * as published by the Free Software Foundation. |
---|
7 | * |
---|
8 | * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
9 | * |
---|
10 | * $Id:$ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <libtransmission/transmission.h> |
---|
14 | #include <libtransmission/variant.h> |
---|
15 | |
---|
16 | #include "formatter.h" |
---|
17 | #include "freespace-label.h" |
---|
18 | #include "session.h" |
---|
19 | |
---|
20 | namespace |
---|
21 | { |
---|
22 | static const int INTERVAL_MSEC = 15000; |
---|
23 | } |
---|
24 | |
---|
25 | FreespaceLabel :: FreespaceLabel (Session & session, |
---|
26 | const QString & path, |
---|
27 | QWidget * parent): |
---|
28 | QLabel (parent), |
---|
29 | mySession (session), |
---|
30 | myTag (-1), |
---|
31 | myTimer (this) |
---|
32 | { |
---|
33 | myTimer.setSingleShot (false); |
---|
34 | myTimer.setInterval (INTERVAL_MSEC); |
---|
35 | myTimer.start (); |
---|
36 | |
---|
37 | connect (&myTimer, SIGNAL(timeout()), this, SLOT(onTimer())); |
---|
38 | |
---|
39 | connect (&mySession, SIGNAL(executed(int64_t, const QString&, struct tr_variant*)), |
---|
40 | this, SLOT(onSessionExecuted(int64_t, const QString&, struct tr_variant*))); |
---|
41 | |
---|
42 | setPath (path); |
---|
43 | } |
---|
44 | |
---|
45 | void |
---|
46 | FreespaceLabel :: setPath (const QString& path) |
---|
47 | { |
---|
48 | if (myPath != path) |
---|
49 | { |
---|
50 | setText (tr("<i>Calculating Free Space...</i>")); |
---|
51 | myPath = path; |
---|
52 | onTimer (); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | void |
---|
57 | FreespaceLabel :: onTimer () |
---|
58 | { |
---|
59 | const int64_t tag = mySession.getUniqueTag (); |
---|
60 | const QByteArray myPathUtf8 = myPath.toUtf8 (); |
---|
61 | |
---|
62 | myTag = tag; |
---|
63 | tr_variant top; |
---|
64 | tr_variantInitDict (&top, 3); |
---|
65 | tr_variantDictAddStr (&top, TR_KEY_method, "free-space"); |
---|
66 | tr_variantDictAddInt (&top, TR_KEY_tag, tag); |
---|
67 | tr_variant * args = tr_variantDictAddDict (&top, TR_KEY_arguments, 1); |
---|
68 | tr_variantDictAddStr (args, TR_KEY_path, myPathUtf8.constData()); |
---|
69 | mySession.exec (&top); |
---|
70 | tr_variantFree (&top); |
---|
71 | } |
---|
72 | |
---|
73 | void |
---|
74 | FreespaceLabel :: onSessionExecuted (int64_t tag, const QString& result, struct tr_variant * arguments) |
---|
75 | { |
---|
76 | Q_UNUSED (result); |
---|
77 | |
---|
78 | if (tag != myTag) |
---|
79 | return; |
---|
80 | |
---|
81 | QString str; |
---|
82 | |
---|
83 | // update the label |
---|
84 | int64_t bytes = -1; |
---|
85 | tr_variantDictFindInt (arguments, TR_KEY_size_bytes, &bytes); |
---|
86 | if (bytes >= 0) |
---|
87 | setText (tr("%1 free").arg(Formatter::sizeToString (bytes))); |
---|
88 | else |
---|
89 | setText (""); |
---|
90 | |
---|
91 | // update the tooltip |
---|
92 | size_t len = 0; |
---|
93 | const char * path = 0; |
---|
94 | tr_variantDictFindStr (arguments, TR_KEY_path, &path, &len); |
---|
95 | str = QString::fromUtf8 (path, len); |
---|
96 | setToolTip (str); |
---|
97 | } |
---|