Repository navigation
Expand file tree
/
Copy pathStatusBar.cpp
More file actions
213 lines (178 loc) · 6.91 KB
/
Copy pathStatusBar.cpp
File metadata and controls
213 lines (178 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* D-LAN - A decentralized LAN file sharing software.
* Copyright (C) 2010-2012 Greg Burri <greg.burri@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <StatusBar.h>
#include <ui_StatusBar.h>
using namespace GUI;
#include <Common/Global.h>
#include <Common/ProtoHelper.h>
#include <Common/Settings.h>
#include <DialogAbout.h>
#include <Utils.h>
StatusBar::StatusBar(QSharedPointer<RCC::ICoreConnection> coreConnection, QWidget* parent) :
QWidget(parent), ui(new Ui::StatusBar), coreConnection(coreConnection)
{
this->ui->setupUi(this);
this->coreDisconnected();
connect(this->coreConnection.data(), &RCC::ICoreConnection::newState, this, &StatusBar::newState);
connect(this->coreConnection.data(), &RCC::ICoreConnection::connecting, this, &StatusBar::coreConnecting);
connect(this->coreConnection.data(), &RCC::ICoreConnection::connectingError, this, &StatusBar::coreConnectingError);
connect(this->coreConnection.data(), &RCC::ICoreConnection::connected, this, &StatusBar::coreConnected);
connect(this->coreConnection.data(), &RCC::ICoreConnection::disconnected, this, &StatusBar::coreDisconnected);
this->ui->lblDownloadRate->installEventFilter(this);
this->ui->icoDownloadRate->installEventFilter(this);
this->ui->lblUploadRate->installEventFilter(this);
this->ui->icoUploadRate->installEventFilter(this);
connect(this->ui->butLog, &QPushButton::toggled, this, &StatusBar::showDockLog);
connect(this->ui->butHelp, &QPushButton::clicked, this, &StatusBar::showAbout);
connect(this->ui->butGraphs, &QPushButton::toggled, this, &StatusBar::showDockGraphs);
connect(this->ui->butConnectToLocal, &QPushButton::clicked, this, &StatusBar::connectToLocalClicked);
}
StatusBar::~StatusBar()
{
this->coreConnection->disconnect(this); // Disconnect all signals from coreConnection.
delete this->ui;
}
bool StatusBar::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::MouseButtonPress)
{
if (obj == this->ui->lblDownloadRate || obj == this->ui->icoDownloadRate)
emit downloadClicked();
else if (obj == this->ui->lblUploadRate || obj == this->ui->icoUploadRate)
emit uploadClicked();
}
return false;
}
void StatusBar::changeEvent(QEvent* event)
{
if (event->type() == QEvent::LanguageChange)
{
// The texts which depend on the state of the core aren't defined by the form, which clears them.
this->ui->retranslateUi(this);
this->setDownloadRate(this->downloadRate);
this->setUploadRate(this->uploadRate);
this->setTotalSharing(this->nbPeer, this->sharingAmount);
this->updateCoreStatus(this->cacheStatus, this->progress);
}
QWidget::changeEvent(event);
}
void StatusBar::dockLogVisibilityChanged(bool visibility)
{
this->ui->butLog->setChecked(visibility);
}
void StatusBar::dockGraphsVisibilityChanged(bool visibility)
{
this->ui->butGraphs->setChecked(visibility);
}
void StatusBar::coreConnecting()
{
this->ui->butConnectToLocal->setDisabled(true);
}
void StatusBar::coreConnectingError()
{
this->ui->butConnectToLocal->setDisabled(false);
}
void StatusBar::coreConnected()
{
this->ui->butConnectToLocal->setDisabled(false);
this->updateCoreStatus();
}
void StatusBar::coreDisconnected()
{
this->setDownloadRate(0);
this->setUploadRate(0);
this->setTotalSharing(0, 0);
this->updateCoreStatus();
}
void StatusBar::newState(const Protos::GUI::State& state)
{
this->setDownloadRate(Common::ProtoHelper::getCurrentDownloadRate(state));
this->setUploadRate(Common::ProtoHelper::getCurrentUploadRate(state));
qint64 totalSharing = 0;
for (int i = 0; i < state.peers_size(); i++)
totalSharing += state.peers(i).sharing_amount();
this->setTotalSharing(state.peers_size(), totalSharing);
this->updateCoreStatus(state.stats().cache_status(), state.stats().progress());
}
void StatusBar::showAbout()
{
Utils::showModal(new DialogAbout(this));
}
void StatusBar::setDownloadRate(qint64 rate)
{
this->downloadRate = rate;
this->ui->lblDownloadRate->setText(Common::Global::formatByteSize(rate).append("/s"));
}
void StatusBar::setUploadRate(qint64 rate)
{
this->uploadRate = rate;
this->ui->lblUploadRate->setText(Common::Global::formatByteSize(rate).append("/s"));
}
void StatusBar::setTotalSharing(int nbPeer, qint64 amount)
{
this->nbPeer = nbPeer;
this->sharingAmount = amount;
this->ui->lblTotalSharing->setText(
QString::number(nbPeer)
.append(" ")
.append(nbPeer > 1 ? tr("peers") : tr("peer"))
.append(": ")
.append(Common::Global::formatByteSize(amount))
);
}
void StatusBar::updateCoreStatus(Protos::GUI::State_Stats_CacheStatus status, int progress)
{
this->cacheStatus = status;
this->progress = progress;
QString statusMess;
this->ui->butConnectToLocal->setVisible(this->coreConnection->isConnected() && !this->coreConnection->isLocal());
if (this->coreConnection->isConnected())
{
if (!this->coreConnection->isLocal())
statusMess.append(QString(tr("connected to %1").arg(SETTINGS.get<QString>("core_address"))));
else
statusMess.append(tr("connected"));
switch (status)
{
case Protos::GUI::State_Stats_CacheStatus_INITIAL_SCANNING_IN_PROGRESS:
case Protos::GUI::State_Stats_CacheStatus_SCANNING_IN_PROGRESS:
statusMess.append(" - ").append(tr("scanning in progress . . ."));
this->ui->prgCurrentAction->setVisible(false);
break;
case Protos::GUI::State_Stats_CacheStatus_HASHING_IN_PROGRESS:
statusMess.append(" - ").append(tr("indexing in progress . . ."));
this->ui->prgCurrentAction->setVisible(true);
this->ui->prgCurrentAction->setValue(progress);
break;
case Protos::GUI::State_Stats_CacheStatus_UP_TO_DATE:
statusMess.append(" - ").append(tr("cache is up to date"));
this->ui->prgCurrentAction->setVisible(false);
this->ui->prgCurrentAction->setValue(progress);
break;
case Protos::GUI::State_Stats_CacheStatus_UNKNOWN: // Nothing to display.
default:;
this->ui->prgCurrentAction->setVisible(false);
}
}
else
{
statusMess.append(tr("disconnected"));
this->ui->prgCurrentAction->setVisible(false);
}
this->ui->lblCoreStatus->setText(tr("Core: %1").arg(statusMess));
}