File indexing completed on 2024-04-21 14:56:13

0001 /*
0002     Copyright 2007 Will Stephenson <wstephenson@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) version 3, or any
0008     later version accepted by the membership of KDE e.V. (or its
0009     successor approved by the membership of KDE e.V.), which shall
0010     act as a proxy defined in Section 6 of version 3 of the license.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Lesser General Public License for more details.
0016 
0017     You should have received a copy of the GNU Lesser General Public
0018     License along with this library. If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <QLabel>
0022 #include <QPushButton>
0023 
0024 #include <qdebug.h>
0025 
0026 #include "networkingclient.h"
0027 
0028 QString toString(Solid::Networking::Status st)
0029 {
0030     QString str;
0031     switch (st) {
0032     case Solid::Networking::Unknown:
0033         str = "Unknown";
0034         break;
0035     case Solid::Networking::Unconnected:
0036         str = "Unconnected";
0037         break;
0038     case Solid::Networking::Disconnecting:
0039         str = "Disconnecting";
0040         break;
0041     case Solid::Networking::Connecting:
0042         str = "Connecting";
0043         break;
0044     case Solid::Networking::Connected:
0045         str = "Connected";
0046         break;
0047     }
0048     return str;
0049 }
0050 
0051 TestClient::TestClient()
0052     : QMainWindow(nullptr),
0053       m_status(AppDisconnected), m_view(new QWidget(this))
0054 {
0055     ui.setupUi(m_view);
0056 
0057     setCentralWidget(m_view);
0058 
0059     networkStatusChanged(Solid::Networking::status());
0060     appDisconnected();
0061 
0062     qDebug() << "About to connect";
0063     connect(Solid::Networking::notifier(), SIGNAL(statusChanged(Solid::Networking::Status)), SLOT(networkStatusChanged(Solid::Networking::Status)));
0064     qDebug() << "Connected.";
0065     connect(Solid::Networking::notifier(), SIGNAL(shouldConnect()), this, SLOT(doConnect()));
0066     connect(Solid::Networking::notifier(), SIGNAL(shouldDisconnect()), this, SLOT(doDisconnect()));
0067 
0068     connect(ui.connectButton, SIGNAL(clicked()), SLOT(connectButtonClicked()));
0069 }
0070 
0071 TestClient::~TestClient()
0072 {
0073 }
0074 
0075 void TestClient::networkStatusChanged(Solid::Networking::Status status)
0076 {
0077     qDebug() << Q_FUNC_INFO;
0078     qDebug() << "Networking is now: " << toString(status) << " (" << status << ")";
0079     ui.netStatusLabel->setText(toString(status));
0080     QPalette palette;
0081     palette.setColor(ui.netStatusLabel->backgroundRole(), toQColor(m_status));
0082     ui.netStatusLabel->setPalette(palette);
0083 }
0084 
0085 void TestClient::doConnect()
0086 {
0087     Q_ASSERT(Solid::Networking::status() == Solid::Networking::Connected);
0088     if (m_status != AppConnected) {
0089         appIsConnected();
0090     }
0091 }
0092 
0093 void TestClient::doDisconnect()
0094 {
0095     Q_ASSERT(Solid::Networking::status() != Solid::Networking::Connected);
0096     if (m_status == AppConnected) {
0097         appDisconnected();
0098     }
0099 }
0100 
0101 void TestClient::connectButtonClicked()
0102 {
0103     qDebug() << Q_FUNC_INFO;
0104     if (m_status == AppDisconnected) {
0105         switch (Solid::Networking::status()) {
0106         case Solid::Networking::Unknown:
0107         case Solid::Networking::Connected:
0108             appIsConnected();
0109             break;
0110         default:
0111             appWaiting();
0112             break;
0113         }
0114     } else if (m_status == AppConnected || m_status == AppWaitingForConnect) {
0115         appDisconnected();
0116     }
0117 }
0118 
0119 void TestClient::appWaiting()
0120 {
0121     qDebug() << Q_FUNC_INFO;
0122     //m_status = AppWaitingForConnect;
0123     ui.appStatusLabel->setText("Waiting");
0124 }
0125 
0126 void TestClient::appIsConnected()
0127 {
0128     qDebug() << Q_FUNC_INFO;
0129     ui.connectButton->setEnabled(true);
0130     ui.connectButton->setText("Disconnect");
0131     ui.appStatusLabel->setText("Connected");
0132     m_status = AppConnected;
0133 }
0134 
0135 void TestClient::appEstablishing()
0136 {
0137     qDebug() << Q_FUNC_INFO;
0138     ui.netStatusLabel->setText("Establishing");
0139     ui.connectButton->setEnabled(false);
0140 }
0141 
0142 void TestClient::appDisestablishing()
0143 {
0144     qDebug() << Q_FUNC_INFO;
0145     ui.connectButton->setEnabled(false);
0146     ui.appStatusLabel->setText("Disconnected");
0147 }
0148 
0149 void TestClient::appDisconnected()
0150 {
0151     qDebug() << Q_FUNC_INFO;
0152     ui.connectButton->setEnabled(true);
0153     ui.connectButton->setText("Start Connect");
0154     ui.appStatusLabel->setText("Disconnected");
0155     m_status = AppDisconnected;
0156 }
0157 
0158 QColor TestClient::toQColor(TestClient::AppStatus st)
0159 {
0160     QColor col;
0161     switch (st) {
0162     case TestClient::AppDisconnected:
0163         col = Qt::red;
0164         break;
0165     case TestClient::AppWaitingForConnect:
0166         col = Qt::yellow;
0167         break;
0168     case TestClient::AppConnected:
0169         col = Qt::green;
0170         break;
0171     }
0172     return col;
0173 }
0174 
0175 //main
0176 int main(int argc, char **argv)
0177 {
0178     QApplication app(argc, argv);
0179 
0180     if (app.arguments().count() == 0) {
0181         TestClient *widget = new TestClient;
0182         widget->show();
0183     } else {
0184         int i = 0;
0185         for (; i < app.arguments().count(); i++) {
0186             TestClient *widget = new TestClient;
0187             widget->show();
0188         }
0189     }
0190     app.arguments().clear();
0191 
0192     return app.exec();
0193 }
0194 
0195 #include "moc_networkingclient.cpp"
0196