1 | /* |
---|
2 | * This file Copyright (C) 2013-2014 Mnemosyne LLC |
---|
3 | * |
---|
4 | * It may be used under the GNU GPL versions 2 or 3 |
---|
5 | * or any future license endorsed by Mnemosyne LLC. |
---|
6 | * |
---|
7 | * $Id: freespace-label.cc 14525 2015-05-09 08:37:55Z mikedld $ |
---|
8 | */ |
---|
9 | |
---|
10 | #include <QDir> |
---|
11 | |
---|
12 | #include <libtransmission/transmission.h> |
---|
13 | #include <libtransmission/variant.h> |
---|
14 | |
---|
15 | #include "formatter.h" |
---|
16 | #include "freespace-label.h" |
---|
17 | #include "session.h" |
---|
18 | |
---|
19 | namespace |
---|
20 | { |
---|
21 | static const int INTERVAL_MSEC = 15000; |
---|
22 | } |
---|
23 | |
---|
24 | FreespaceLabel::FreespaceLabel (QWidget * parent): |
---|
25 | QLabel (parent), |
---|
26 | mySession (nullptr), |
---|
27 | myTag (-1), |
---|
28 | myTimer (this) |
---|
29 | { |
---|
30 | myTimer.setSingleShot (true); |
---|
31 | myTimer.setInterval (INTERVAL_MSEC); |
---|
32 | |
---|
33 | connect (&myTimer, SIGNAL (timeout ()), this, SLOT (onTimer ())); |
---|
34 | } |
---|
35 | |
---|
36 | void |
---|
37 | FreespaceLabel::setSession (Session& session) |
---|
38 | { |
---|
39 | if (mySession == &session) |
---|
40 | return; |
---|
41 | |
---|
42 | if (mySession != nullptr) |
---|
43 | disconnect (mySession, nullptr, this, nullptr); |
---|
44 | |
---|
45 | mySession = &session; |
---|
46 | |
---|
47 | connect (mySession, SIGNAL (executed (int64_t, QString, tr_variant *)), |
---|
48 | this, SLOT (onSessionExecuted (int64_t, QString, tr_variant *))); |
---|
49 | |
---|
50 | onTimer (); |
---|
51 | } |
---|
52 | |
---|
53 | void |
---|
54 | FreespaceLabel::setPath (const QString& path) |
---|
55 | { |
---|
56 | if (myPath != path) |
---|
57 | { |
---|
58 | setText (tr("<i>Calculating Free Space...</i>")); |
---|
59 | myPath = path; |
---|
60 | onTimer (); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | void |
---|
65 | FreespaceLabel::onTimer () |
---|
66 | { |
---|
67 | myTimer.stop (); |
---|
68 | |
---|
69 | if (mySession == nullptr || myPath.isEmpty ()) |
---|
70 | return; |
---|
71 | |
---|
72 | tr_variant args; |
---|
73 | tr_variantInitDict (&args, 1); |
---|
74 | tr_variantDictAddStr (&args, TR_KEY_path, myPath.toUtf8 ().constData()); |
---|
75 | |
---|
76 | myTag = mySession->getUniqueTag (); |
---|
77 | mySession->exec ("free-space", &args, myTag); |
---|
78 | } |
---|
79 | |
---|
80 | void |
---|
81 | FreespaceLabel::onSessionExecuted (int64_t tag, const QString& result, tr_variant * arguments) |
---|
82 | { |
---|
83 | Q_UNUSED (result); |
---|
84 | |
---|
85 | if (tag != myTag) |
---|
86 | return; |
---|
87 | |
---|
88 | QString str; |
---|
89 | |
---|
90 | // update the label |
---|
91 | int64_t bytes = -1; |
---|
92 | if (tr_variantDictFindInt (arguments, TR_KEY_size_bytes, &bytes) && bytes >= 0) |
---|
93 | setText (tr("%1 free").arg(Formatter::sizeToString (bytes))); |
---|
94 | else |
---|
95 | setText (QString ()); |
---|
96 | |
---|
97 | // update the tooltip |
---|
98 | size_t len = 0; |
---|
99 | const char * path = 0; |
---|
100 | tr_variantDictFindStr (arguments, TR_KEY_path, &path, &len); |
---|
101 | str = QString::fromUtf8 (path, len); |
---|
102 | setToolTip (QDir::toNativeSeparators (str)); |
---|
103 | |
---|
104 | myTimer.start (); |
---|
105 | } |
---|