-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathStatusIcon.cpp
More file actions
140 lines (132 loc) · 4.36 KB
/
Copy pathStatusIcon.cpp
File metadata and controls
140 lines (132 loc) · 4.36 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
/*
* Copyright (C) 2017 Nessla AB
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "StatusIcon.h"
#include "LogWindow.h"
#include "platform-constants.h"
#include <QApplication>
#include <QMenu>
#include <QAction>
#include <QDebug>
void StatusIcon::setStatus(StatusIcon::Status status)
{
QString icon, text;
m_status = status;
switch (status) {
case Disconnected:
icon = ":icons/" PLATFORM_ICONS "/disconnected.svg";
text = tr("Disconnected");
m_disconnectMenu->setText(tr("&Connect"));
m_transferMenu->setVisible(false);
break;
case Connecting:
icon = ":icons/" PLATFORM_ICONS "/connecting.svg";
text = tr("Connecting...");
m_disconnectMenu->setText(tr("&Disconnect"));
m_transferMenu->setVisible(false);
break;
case Connected:
icon = ":icons/" PLATFORM_ICONS "/connected.svg";
text = tr("Connected");
m_disconnectMenu->setText(tr("&Disconnect"));
showMessage(qApp->applicationName(), tr("%1 is now connected.").arg(qApp->applicationName()), QSystemTrayIcon::Information, 5000);
break;
}
setIcon(QIcon(icon).pixmap(128, 128));
setToolTip(tr("%1\nStatus: %2").arg(qApp->applicationName(), text));
m_statusMenu->setText(tr("%1 Status: %2").arg(qApp->applicationName(), text));
}
QString StatusIcon::unit(double bytes) const
{
if (bytes < 1024.0)
return QString("%1 B").arg(bytes, 0, 'f', 2);
if (bytes < 1024.0 * 1024.0)
return QString("%1 KiB").arg(bytes / 1024.0, 0, 'f', 2);
if (bytes < 1024.0 * 1024.0 * 1024.0)
return QString("%1 MiB").arg(bytes / (1024.0 * 1024.0), 0, 'f', 2);
if (bytes < 1024.0 * 1024.0 * 1024.0 * 1024.0)
return QString("%1 GiB").arg(bytes / (1024.0 * 1024.0 * 1024.0), 0, 'f', 2);
return QString("%1 TiB").arg(bytes / (1024.0 * 1024.0 * 1024.0 * 1024.0), 0, 'f', 2);
}
void StatusIcon::setTransfer(quint64 down, quint64 up)
{
if (m_status != Connected)
return;
const QString text = tr("%1 down / %2 up").arg(unit(down), unit(up));
setToolTip(QString("%1\nStatus: Connected\n%2").arg(qApp->applicationName(), text));
m_transferMenu->setText(text);
m_transferMenu->setVisible(true);
}
bool StatusIcon::maybeShowParent() const
{
if (m_status != Disconnected)
return false;
QWidget *parentWidget = qobject_cast<QWidget *>(parent());
if (parentWidget) {
parentWidget->show();
qApp->setActiveWindow(parentWidget);
}
return true;
}
StatusIcon::StatusIcon(QObject *owner) :
QSystemTrayIcon(owner)
{
QMenu *menu = new QMenu;
m_statusMenu = new QAction(menu);
m_statusMenu->setEnabled(false);
m_transferMenu = new QAction(menu);
m_transferMenu->setEnabled(false);
m_transferMenu->setVisible(false);
QFont font;
font.setBold(true);
m_statusMenu->setFont(font);
m_disconnectMenu = new QAction(menu);
connect(this, &QSystemTrayIcon::activated, this, [=](ActivationReason reason) {
#if defined(Q_OS_LINUX)
if (reason == Trigger)
#else
if (reason == DoubleClick)
#endif
maybeShowParent();
});
connect(m_disconnectMenu, &QAction::triggered, this, [=]() {
if (maybeShowParent())
return;
setStatus(Disconnected);
emit disconnect();
});
menu->addAction(m_statusMenu);
menu->addAction(m_transferMenu);
menu->addSeparator();
menu->addAction(m_disconnectMenu);
#if QT_VERSION >= 0x050600
menu->addAction(tr("Show Log"), &LogWindow::instance(), &LogWindow::show);
menu->addAction(tr("&Exit %1").arg(qApp->applicationName()), qApp, &QApplication::quit);
#else
/* TODO: once nobody cares about Ubuntu 16.04, get rid of this ifdef,
* since Qt 5.6 is a LTS release anyway. */
menu->addAction(tr("Show Log"), &LogWindow::instance(), SLOT(show()));
menu->addAction(tr("&Exit %1").arg(qApp->applicationName()), qApp, SLOT(quit()));
#endif
setContextMenu(menu);
setStatus(Disconnected);
show();
}
StatusIcon::~StatusIcon()
{
contextMenu()->deleteLater();
}