File indexing completed on 2024-05-19 04:59:18
0001 /* ============================================================ 0002 * StatusBarIcons - Extra icons in statusbar for Falkon 0003 * Copyright (C) 2013-2017 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "sbi_networkicon.h" 0019 #include "sbi_networkicondialog.h" 0020 #include "sbi_networkproxy.h" 0021 #include "sbi_networkmanager.h" 0022 #include "mainapplication.h" 0023 #include "browserwindow.h" 0024 0025 #include <QMenu> 0026 0027 SBI_NetworkIcon::SBI_NetworkIcon(BrowserWindow* window) 0028 : SBI_Icon(window) 0029 { 0030 setObjectName(QSL("sbi_networkicon")); 0031 setCursor(Qt::PointingHandCursor); 0032 0033 /* TODO rework connection detection */ 0034 onlineStateChanged(true); 0035 connect(this, &ClickableLabel::clicked, this, &SBI_NetworkIcon::showMenu); 0036 } 0037 0038 void SBI_NetworkIcon::onlineStateChanged(bool online) 0039 { 0040 if (online) { 0041 setPixmap(QIcon(QSL(":sbi/data/network-online.png")).pixmap(16)); 0042 } 0043 else { 0044 setPixmap(QIcon(QSL(":sbi/data/network-offline.png")).pixmap(16)); 0045 } 0046 0047 updateToolTip(); 0048 } 0049 0050 void SBI_NetworkIcon::showDialog() 0051 { 0052 auto* dialog = new SBI_NetworkIconDialog(m_window); 0053 dialog->open(); 0054 } 0055 0056 void SBI_NetworkIcon::showMenu(const QPoint &pos) 0057 { 0058 QFont boldFont = font(); 0059 boldFont.setBold(true); 0060 0061 QMenu menu; 0062 menu.addAction(QIcon::fromTheme(QSL("preferences-system-network"), QIcon(QSL(":sbi/data/preferences-network.png"))), tr("Proxy Configuration"))->setFont(boldFont); 0063 0064 QMenu* proxyMenu = menu.addMenu(tr("Select proxy")); 0065 0066 const QHash<QString, SBI_NetworkProxy*> &proxies = SBINetManager->proxies(); 0067 0068 QHashIterator<QString, SBI_NetworkProxy*> it(proxies); 0069 while (it.hasNext()) { 0070 it.next(); 0071 QAction* act = proxyMenu->addAction(it.key(), this, &SBI_NetworkIcon::useProxy); 0072 act->setData(it.key()); 0073 act->setCheckable(true); 0074 act->setChecked(it.value() == SBINetManager->currentProxy()); 0075 } 0076 0077 if (proxyMenu->actions().isEmpty()) { 0078 proxyMenu->addAction(tr("Empty"))->setEnabled(false); 0079 } 0080 0081 menu.addSeparator(); 0082 menu.addAction(tr("Manage proxies"), this, &SBI_NetworkIcon::showDialog); 0083 menu.exec(pos); 0084 } 0085 0086 void SBI_NetworkIcon::useProxy() 0087 { 0088 if (auto* act = qobject_cast<QAction*>(sender())) { 0089 SBINetManager->setCurrentProxy(act->data().toString()); 0090 } 0091 } 0092 0093 void SBI_NetworkIcon::updateToolTip() 0094 { 0095 QString tooltip = tr("Shows network status and manages proxy<br/><br/><b>Network:</b><br/>%1<br/><br/><b>Proxy:</b><br/>%2"); 0096 0097 // TODO QT6 - in Qt6 we're always reporting as online, should we just remove this functionality instead? 0098 // Or is there a way to detect network status in Qt6? 0099 tooltip = tooltip.arg(tr("Connected")); 0100 0101 switch (QNetworkProxy::applicationProxy().type()) { 0102 case QNetworkProxy::DefaultProxy: 0103 tooltip = tooltip.arg(tr("System proxy")); 0104 break; 0105 0106 case QNetworkProxy::NoProxy: 0107 tooltip = tooltip.arg(tr("No proxy")); 0108 break; 0109 0110 default: 0111 tooltip = tooltip.arg(tr("User defined")); 0112 break; 0113 } 0114 0115 if (SBINetManager->currentProxy()) { 0116 tooltip.append(QSL(" (%1)").arg(SBINetManager->currentProxyName())); 0117 } 0118 0119 setToolTip(tooltip); 0120 } 0121 0122 void SBI_NetworkIcon::enterEvent(QEnterEvent* event) 0123 { 0124 updateToolTip(); 0125 0126 SBI_Icon::enterEvent(event); 0127 }