add more spacing
[personal-kdebase.git] / workspace / solid / networking / tests / networkingservice.cpp
blob042c09a794a349590115ae6ef83132cebc477432
1 /* This file is part of kdepim.
2 Copyright (C) 2005,2007 Will Stephenson <wstephenson@kde.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; either version 2 of the License,
7 or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
19 As a special exception, permission is given to link this program
20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution.
24 #include <QComboBox>
25 #include <QLabel>
26 #include <QPushButton>
27 #include <QTimer>
29 #include <KAboutData>
30 #include <KApplication>
31 #include <KCmdLineArgs>
32 #include <KDebug>
33 #include <KLocale>
35 #include "networkingservice.h"
36 #include "serviceinterface.h"
38 QString toString( Solid::Networking::Status st )
40 QString str;
41 switch ( st ) {
42 case Solid::Networking::Unknown:
43 str = "Unknown";
44 break;
45 case Solid::Networking::Disconnecting:
46 str = "Disconnecting";
47 break;
48 case Solid::Networking::Unconnected:
49 str = "Unconnected";
50 break;
51 case Solid::Networking::Connecting:
52 str = "Connecting";
53 break;
54 case Solid::Networking::Connected:
55 str = "Connected";
56 break;
58 return str;
61 TestService::TestService() : KMainWindow( 0 ),
62 m_service( new OrgKdeSolidNetworkingServiceInterface( "org.kde.kded", "/modules/networkstatus", QDBusConnection::sessionBus(), this ) ),
63 m_status ( Solid::Networking::Unconnected ),
64 m_nextStatus( Solid::Networking::Unconnected ),
65 m_view( new QWidget( this ) )
67 QDBusConnection::sessionBus().registerService( "org.kde.Solid.Networking.TestService" );
69 ui.setupUi( m_view );
70 setCentralWidget( m_view );
71 connect( ui.changeCombo, SIGNAL( activated( int ) ), SLOT( changeComboActivated( int ) ) );
72 connect( ui.changeButton, SIGNAL( clicked() ), SLOT( changeButtonClicked() ) );
74 connect( QDBusConnection::sessionBus().interface(), SIGNAL(serviceOwnerChanged(const QString&, const QString&, const QString & ) ), SLOT(serviceOwnerChanged(const QString&, const QString&, const QString & ) ) );
76 ui.statusLabel->setText( toString( m_status ) );
77 QPalette palette;
78 palette.setColor( ui.statusLabel->backgroundRole(), toQColor( m_status ) );
79 ui.statusLabel->setPalette( palette );
80 setCaption( toString( m_status ) );
82 registerService();
85 TestService::~TestService()
87 delete m_service;
90 void TestService::registerService()
92 m_service->registerNetwork( "test_net", m_status, "org.kde.Solid.Networking.TestService" );
95 void TestService::serviceOwnerChanged( const QString& service,const QString& oldOwner, const QString& newOwner )
97 Q_UNUSED( oldOwner );
98 if ( !newOwner.isEmpty() && service == "org.kde.kded" ) {
99 kDebug() << "KDED restarted, trying to re-register service with it";
100 registerService();
104 int TestService::status( const QString & network )
106 Q_UNUSED( network );
107 return (int)m_status;
110 void TestService::changeComboActivated( int index )
112 switch ( index ) {
113 case 0 /*Solid::Networking::Unknown*/:
114 m_nextStatus = Solid::Networking::Unknown;
115 break;
116 case 1 /*Solid::Networking::Unconnected*/:
117 m_nextStatus = Solid::Networking::Unconnected;
118 break;
119 case 2 /*Solid::Networking::Disconnecting*/:
120 m_nextStatus = Solid::Networking::Disconnecting;
121 break;
122 case 3 /*Solid::Networking::Connecting*/:
123 m_nextStatus = Solid::Networking::Connecting;
124 break;
125 case 4 /*Solid::Networking::Connected*/:
126 m_nextStatus = Solid::Networking::Connected;
127 break;
128 default:
129 kDebug() << "Unrecognised status!";
130 Q_ASSERT( false );
132 ui.changeButton->setEnabled( true );
135 void TestService::changeButtonClicked()
137 ui.changeButton->setEnabled( false );
138 m_status = m_nextStatus;
139 m_service->setNetworkStatus( "test_net", ( int )m_status );
140 ui.statusLabel->setText( toString( m_status ) );
141 QPalette palette;
142 palette.setColor( ui.statusLabel->backgroundRole(), toQColor( m_status ) );
143 ui.statusLabel->setPalette( palette );
144 setCaption( toString( m_status ) );
146 #if 0
147 int TestService::establish( const QString & network )
149 Q_UNUSED( network );
150 m_status = Solid::Networking::Connecting;
151 m_service->setNetworkStatus( "test_net", (int)m_status );
152 m_nextStatus = Solid::Networking::Connected;
153 QTimer::singleShot( 5000, this, SLOT( slotStatusChange() ) );
154 return (int)Solid::Networking::RequestAccepted;
157 int TestService::shutdown( const QString & network )
159 Q_UNUSED( network );
160 m_status = Solid::Networking::Disconnecting;
161 m_service->setNetworkStatus( "test_net", (int)m_status );
162 m_nextStatus = Solid::Networking::Unconnected;
163 QTimer::singleShot( 5000, this, SLOT( slotStatusChange() ) );
164 return (int)Solid::Networking::RequestAccepted;
167 void TestService::simulateFailure()
169 m_status = Solid::Networking::UnconnectedFailed;
170 m_service->setNetworkStatus( "test_net", (int)m_status );
173 void TestService::simulateDisconnect()
175 m_status = Solid::Networking::UnconnectedDisconnected;
176 m_service->setNetworkStatus( "test_net", (int)m_status );
178 #endif
179 void TestService::slotStatusChange()
181 m_status = m_nextStatus;
182 m_service->setNetworkStatus( "test_net", (int)m_status );
185 QColor TestService::toQColor( Solid::Networking::Status st )
187 QColor col;
188 switch ( st ) {
189 case Solid::Networking::Unknown:
190 col = Qt::darkGray;
191 break;
192 case Solid::Networking::Disconnecting:
193 col = Qt::darkYellow;
194 break;
195 case Solid::Networking::Unconnected:
196 col = Qt::blue;
197 break;
198 case Solid::Networking::Connecting:
199 col = Qt::yellow;
200 break;
201 case Solid::Networking::Connected:
202 col = Qt::green;
203 break;
205 return col;
208 static const char description[] =
209 I18N_NOOP("Test Service for Network Status kded module");
211 static const char version[] = "v0.1";
213 int main( int argc, char** argv )
215 KAboutData about("KNetworkStatusTestService", 0, ki18n("knetworkstatustestservice"), version, ki18n(description), KAboutData::License_GPL, ki18n("(C) 2007 Will Stephenson"), KLocalizedString(), 0, "wstephenson@kde.org");
216 about.addAuthor( ki18n("Will Stephenson"), KLocalizedString(), "wstephenson@kde.org" );
217 KCmdLineArgs::init(argc, argv, &about);
219 KCmdLineOptions options;
220 KCmdLineArgs::addCmdLineOptions(options);
221 KApplication app;
223 TestService * test = new TestService;
224 test->show();
225 return app.exec();
228 #include "networkingservice.moc"