Falkon Develop
Cross-platform Qt-based web browser
sbi_networkicon.cpp
Go to the documentation of this file.
1/* ============================================================
2* StatusBarIcons - Extra icons in statusbar for Falkon
3* Copyright (C) 2013-2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "sbi_networkicon.h"
20#include "sbi_networkproxy.h"
21#include "sbi_networkmanager.h"
22#include "mainapplication.h"
23#include "browserwindow.h"
24
25#include <QMenu>
26
28 : SBI_Icon(window)
29{
30 setObjectName(QSL("sbi_networkicon"));
31 setCursor(Qt::PointingHandCursor);
32
33 /* TODO rework connection detection */
34 onlineStateChanged(true);
35 connect(this, &ClickableLabel::clicked, this, &SBI_NetworkIcon::showMenu);
36}
37
38void SBI_NetworkIcon::onlineStateChanged(bool online)
39{
40 if (online) {
41 setPixmap(QIcon(QSL(":sbi/data/network-online.png")).pixmap(16));
42 }
43 else {
44 setPixmap(QIcon(QSL(":sbi/data/network-offline.png")).pixmap(16));
45 }
46
47 updateToolTip();
48}
49
50void SBI_NetworkIcon::showDialog()
51{
52 auto* dialog = new SBI_NetworkIconDialog(m_window);
53 dialog->open();
54}
55
56void SBI_NetworkIcon::showMenu(const QPoint &pos)
57{
58 QFont boldFont = font();
59 boldFont.setBold(true);
60
61 QMenu menu;
62 menu.addAction(QIcon::fromTheme(QSL("preferences-system-network"), QIcon(QSL(":sbi/data/preferences-network.png"))), tr("Proxy Configuration"))->setFont(boldFont);
63
64 QMenu* proxyMenu = menu.addMenu(tr("Select proxy"));
65
66 const QHash<QString, SBI_NetworkProxy*> &proxies = SBINetManager->proxies();
67
68 QHashIterator<QString, SBI_NetworkProxy*> it(proxies);
69 while (it.hasNext()) {
70 it.next();
71 QAction* act = proxyMenu->addAction(it.key(), this, &SBI_NetworkIcon::useProxy);
72 act->setData(it.key());
73 act->setCheckable(true);
74 act->setChecked(it.value() == SBINetManager->currentProxy());
75 }
76
77 if (proxyMenu->actions().isEmpty()) {
78 proxyMenu->addAction(tr("Empty"))->setEnabled(false);
79 }
80
81 menu.addSeparator();
82 menu.addAction(tr("Manage proxies"), this, &SBI_NetworkIcon::showDialog);
83 menu.exec(pos);
84}
85
86void SBI_NetworkIcon::useProxy()
87{
88 if (auto* act = qobject_cast<QAction*>(sender())) {
89 SBINetManager->setCurrentProxy(act->data().toString());
90 }
91}
92
93void SBI_NetworkIcon::updateToolTip()
94{
95 QString tooltip = tr("Shows network status and manages proxy<br/><br/><b>Network:</b><br/>%1<br/><br/><b>Proxy:</b><br/>%2");
96
97 // TODO QT6 - in Qt6 we're always reporting as online, should we just remove this functionality instead?
98 // Or is there a way to detect network status in Qt6?
99 tooltip = tooltip.arg(tr("Connected"));
100
101 switch (QNetworkProxy::applicationProxy().type()) {
102 case QNetworkProxy::DefaultProxy:
103 tooltip = tooltip.arg(tr("System proxy"));
104 break;
105
106 case QNetworkProxy::NoProxy:
107 tooltip = tooltip.arg(tr("No proxy"));
108 break;
109
110 default:
111 tooltip = tooltip.arg(tr("User defined"));
112 break;
113 }
114
115 if (SBINetManager->currentProxy()) {
116 tooltip.append(QSL(" (%1)").arg(SBINetManager->currentProxyName()));
117 }
118
119 setToolTip(tooltip);
120}
121
122void SBI_NetworkIcon::enterEvent(QEnterEvent* event)
123{
124 updateToolTip();
125
126 SBI_Icon::enterEvent(event);
127}
void clicked(QPoint)
BrowserWindow * m_window
Definition: sbi_icon.h:42
SBI_NetworkIcon(BrowserWindow *window)
#define QSL(x)
Definition: qzcommon.h:40
#define SBINetManager