File indexing completed on 2022-11-23 10:29:26

0001 /*  This file is part of kdepim.
0002     Copyright (C) 2005,2007 Will Stephenson <wstephenson@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify
0005     it under the terms of the GNU General Public License as published
0006     by the Free Software Foundation; either version 2 of the License,
0007     or (at your option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 
0019     As a special exception, permission is given to link this program
0020     with any edition of Qt, and distribute the resulting executable,
0021     without including the source code for Qt in the source distribution.
0022 */
0023 
0024 #include <QComboBox>
0025 #include <QLabel>
0026 #include <QPushButton>
0027 #include <QTimer>
0028 
0029 #include <k4aboutdata.h>
0030 #include <kapplication.h>
0031 #include <kcmdlineargs.h>
0032 #include <kdebug.h>
0033 #include <klocale.h>
0034 
0035 #include "networkingservice.h"
0036 #include "serviceinterface.h"
0037 
0038 QString toString( Solid::Networking::Status st )
0039 {
0040   QString str;
0041   switch ( st ) {
0042     case Solid::Networking::Unknown:
0043       str = "Unknown";
0044       break;
0045     case Solid::Networking::Disconnecting:
0046       str = "Disconnecting";
0047       break;
0048     case Solid::Networking::Unconnected:
0049       str = "Unconnected";
0050       break;
0051     case Solid::Networking::Connecting:
0052       str = "Connecting";
0053       break;
0054     case Solid::Networking::Connected:
0055       str = "Connected";
0056       break;
0057   }
0058   return str;
0059 }
0060 
0061 TestService::TestService() : KMainWindow( nullptr ),
0062     m_service( new OrgKdeSolidNetworkingServiceInterface( "org.kde.kded5", "/modules/networkstatus", QDBusConnection::sessionBus(), this ) ),
0063     m_status ( Solid::Networking::Unconnected ),
0064     m_nextStatus( Solid::Networking::Unconnected ),
0065     m_view( new QWidget( this ) )
0066 {
0067     QDBusConnection::sessionBus().registerService( "org.kde.Solid.Networking.TestService" );
0068     if (!m_service->isValid())
0069     {
0070         qCritical() << "could not register module" << m_service->path() << "at" << m_service->service();
0071     }
0072 
0073     ui.setupUi( m_view );
0074     setCentralWidget( m_view );
0075     connect( ui.changeCombo, SIGNAL(activated(int)), SLOT(changeComboActivated(int)) );
0076     connect( ui.changeButton, SIGNAL(clicked()), SLOT(changeButtonClicked()) );
0077 
0078     connect( QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(QString,QString,QString)), SLOT(serviceOwnerChanged(QString,QString,QString)) );
0079 
0080     ui.statusLabel->setText( toString( m_status ) );
0081     QPalette palette;
0082     palette.setColor( ui.statusLabel->backgroundRole(), toQColor( m_status ) );
0083     ui.statusLabel->setPalette( palette );
0084     setCaption( toString( m_status ) );
0085 
0086     registerService();
0087 }
0088 
0089 TestService::~TestService()
0090 {
0091     delete m_service;
0092 }
0093 
0094 void TestService::registerService()
0095 {
0096     m_service->registerNetwork( "test_net", m_status, "org.kde.Solid.Networking.TestService" );
0097 }
0098 
0099 void TestService::serviceOwnerChanged( const QString& service,const QString& oldOwner, const QString& newOwner )
0100 {
0101     Q_UNUSED( oldOwner );
0102     if ( !newOwner.isEmpty() && service == "org.kde.kded5" ) {
0103         kDebug() << "KDED restarted, trying to re-register service with it";
0104         registerService();
0105     }
0106 }
0107 
0108 int TestService::status( const QString & network )
0109 {
0110     Q_UNUSED( network );
0111     return (int)m_status;
0112 }
0113 
0114 bool TestService::isValid()
0115 {
0116      return m_service->isValid();
0117 }
0118 
0119 void TestService::changeComboActivated( int index )
0120 {
0121   switch ( index ) {
0122     case 0 /*Solid::Networking::Unknown*/:
0123       m_nextStatus = Solid::Networking::Unknown;
0124       break;
0125     case 1 /*Solid::Networking::Unconnected*/:
0126       m_nextStatus = Solid::Networking::Unconnected;
0127       break;
0128     case 2 /*Solid::Networking::Disconnecting*/:
0129       m_nextStatus = Solid::Networking::Disconnecting;
0130       break;
0131     case 3 /*Solid::Networking::Connecting*/:
0132       m_nextStatus = Solid::Networking::Connecting;
0133       break;
0134     case 4 /*Solid::Networking::Connected*/:
0135       m_nextStatus = Solid::Networking::Connected;
0136       break;
0137     default:
0138       kDebug() << "Unrecognised status!";
0139       Q_ASSERT( false );
0140   }
0141   ui.changeButton->setEnabled( true );
0142 }
0143 
0144 void TestService::changeButtonClicked()
0145 {
0146   ui.changeButton->setEnabled( false );
0147   m_status = m_nextStatus;
0148   m_service->setNetworkStatus( "test_net", ( int )m_status );
0149   ui.statusLabel->setText( toString( m_status ) );
0150   QPalette palette;
0151   palette.setColor( ui.statusLabel->backgroundRole(), toQColor( m_status ) );
0152   ui.statusLabel->setPalette( palette );
0153   setCaption( toString( m_status ) );
0154 }
0155 #if 0
0156 int TestService::establish( const QString & network )
0157 {
0158     Q_UNUSED( network );
0159     m_status = Solid::Networking::Connecting;
0160     m_service->setNetworkStatus( "test_net", (int)m_status );
0161     m_nextStatus = Solid::Networking::Connected;
0162     QTimer::singleShot( 5000, this, SLOT(slotStatusChange()) );
0163     return (int)Solid::Networking::RequestAccepted;
0164 }
0165 
0166 int TestService::shutdown( const QString & network )
0167 {
0168     Q_UNUSED( network );
0169     m_status = Solid::Networking::Disconnecting;
0170     m_service->setNetworkStatus( "test_net", (int)m_status );
0171     m_nextStatus = Solid::Networking::Unconnected;
0172     QTimer::singleShot( 5000, this, SLOT(slotStatusChange()) );
0173     return (int)Solid::Networking::RequestAccepted;
0174 }
0175 
0176 void TestService::simulateFailure()
0177 {
0178     m_status = Solid::Networking::UnconnectedFailed;
0179     m_service->setNetworkStatus( "test_net", (int)m_status );
0180 }
0181 
0182 void TestService::simulateDisconnect()
0183 {
0184     m_status = Solid::Networking::UnconnectedDisconnected;
0185     m_service->setNetworkStatus( "test_net", (int)m_status );
0186 }
0187 #endif
0188 void TestService::slotStatusChange()
0189 {
0190     m_status = m_nextStatus;
0191     m_service->setNetworkStatus( "test_net", (int)m_status );
0192 }
0193 
0194 QColor TestService::toQColor( Solid::Networking::Status st )
0195 {
0196     QColor col;
0197     switch ( st ) {
0198       case Solid::Networking::Unknown:
0199         col = Qt::darkGray;
0200         break;
0201       case Solid::Networking::Disconnecting:
0202         col = Qt::darkYellow;
0203         break;
0204       case Solid::Networking::Unconnected:
0205         col = Qt::blue;
0206         break;
0207       case Solid::Networking::Connecting:
0208         col = Qt::yellow;
0209         break;
0210       case Solid::Networking::Connected:
0211         col = Qt::green;
0212         break;
0213     }
0214     return col;
0215 }
0216 
0217 static const char description[] =
0218     I18N_NOOP("Test Service for Network Status kded module");
0219 
0220 static const char version[] = "v0.1";
0221 
0222 int main( int argc, char** argv )
0223 {
0224     K4AboutData about("KNetworkStatusTestService", nullptr, ki18n("knetworkstatustestservice"), version, ki18n(description), K4AboutData::License_GPL, ki18n("(C) 2007 Will Stephenson"), KLocalizedString(), nullptr, "wstephenson@kde.org");
0225     about.addAuthor( ki18n("Will Stephenson"), KLocalizedString(), "wstephenson@kde.org" );
0226     KCmdLineArgs::init(argc, argv, &about);
0227 
0228     KCmdLineOptions options;
0229     KCmdLineArgs::addCmdLineOptions(options);
0230     KApplication app;
0231 
0232     TestService * test = new TestService;
0233     if (!test->isValid())
0234     {
0235         return 1;
0236     }
0237     test->show();
0238     return app.exec();
0239 }
0240