2 connectionmanager.cpp - Provides the client side interface to the kde networkstatus daemon
4 Copyright (c) 2004 by Will Stephenson <wstephenson@kde.org>
6 Kopete (c) 2004-2007 by the Kopete developers <kopete-devel@kde.org>
8 *************************************************************************
10 * This library is free software; you can redistribute it and/or *
11 * modify it under the terms of the GNU Lesser General Public *
12 * License as published by the Free Software Foundation; either *
13 * version 2 of the License, or (at your option) any later version. *
15 *************************************************************************
21 #include <kmessagebox.h>
23 #include "clientiface_stub.h"
24 #include "networkstatuscommon.h"
26 #include "connectionmanager.h"
28 // ConnectionManager's private parts
29 class ConnectionManagerPrivate
32 // this holds the currently active state
33 ConnectionManager::State m_state
;
34 ClientIface_stub
* m_stub
;
35 bool m_userInitiatedOnly
;
38 // Connection manager itself
39 ConnectionManager::ConnectionManager( QObject
* parent
, const char * name
) : DCOPObject( "ConnectionManager" ),QObject( parent
)
42 d
= new ConnectionManagerPrivate
;
44 d
->m_stub
= new ClientIface_stub( kapp
->dcopClient(), "kded", "networkstatus" );
46 connectDCOPSignal( "kded", "networkstatus", "statusChange(QString,int)", "slotStatusChanged(QString,int)", false );
47 d
->m_userInitiatedOnly
= false;
51 ConnectionManager
*ConnectionManager::self()
53 static ConnectionManager s
;
57 void ConnectionManager::initialise()
59 // determine initial state and set the state object accordingly.
60 d
->m_state
= Inactive
;
64 void ConnectionManager::updateStatus()
66 NetworkStatus::EnumStatus daemonStatus
= (NetworkStatus::EnumStatus
)d
->m_stub
->status( QString() );
68 switch ( daemonStatus
)
70 case NetworkStatus::Offline
:
71 case NetworkStatus::OfflineFailed
:
72 case NetworkStatus::OfflineDisconnected
:
73 case NetworkStatus::ShuttingDown
:
74 if ( d
->m_state
== Online
)
76 kDebug() << "STATE IS PENDING";
81 kDebug() << "STATE IS OFFLINE";
85 case NetworkStatus::Establishing
:
86 case NetworkStatus::Online
:
87 kDebug() << "STATE IS ONLINE";
90 case NetworkStatus::NoNetworks
:
91 case NetworkStatus::Unreachable
:
92 kDebug() << "STATE IS INACTIVE";
93 d
->m_state
= Inactive
;
98 ConnectionManager::~ConnectionManager()
103 NetworkStatus::EnumStatus
ConnectionManager::status( const QString
& host
)
105 // need also to check that the daemon hasn't died
107 if ( d
->m_state
== Pending
)
108 return NetworkStatus::Offline
;
109 if ( d
->m_state
== Online
)
110 return NetworkStatus::Online
;
111 if ( d
->m_state
== Offline
)
112 return NetworkStatus::Offline
;
113 return NetworkStatus::NoNetworks
;
116 NetworkStatus::EnumRequestResult
ConnectionManager::requestConnection( QWidget
* mainWidget
, const QString
& host
, bool userInitiated
)
119 NetworkStatus::EnumRequestResult result
;
120 // if offline and the user has previously indicated they didn't want any new connections, suppress it
121 if ( d
->m_state
== Offline
&& !userInitiated
&& d
->m_userInitiatedOnly
)
122 result
= NetworkStatus::UserRefused
;
123 // if offline, ask the user whether this connection should be allowed
124 if ( d
->m_state
== Offline
)
126 if ( askToConnect( mainWidget
) )
127 //result = NetworkStatus::Connected;
128 result
= (NetworkStatus::EnumRequestResult
)d
->m_stub
->request( host
, userInitiated
);
130 result
= NetworkStatus::UserRefused
;
132 // otherwise, just ask for the connection
134 result
= (NetworkStatus::EnumRequestResult
)d
->m_stub
->request( host
, userInitiated
);
139 void ConnectionManager::relinquishConnection( const QString
& host
)
141 d
->m_stub
->relinquish( host
);
144 void ConnectionManager::slotStatusChanged( QString host
, int status
)
148 // reset user initiated only flag if we are now online
149 if ( d
->m_state
== Online
)
150 d
->m_userInitiatedOnly
= false;
152 emit
statusChanged( host
, (NetworkStatus::EnumStatus
)status
);
155 bool ConnectionManager::askToConnect( QWidget
* mainWidget
)
157 i18n( "A network connection was disconnected. The application is now in offline mode. Do you want the application to resume network operations when the network is available again?" );
158 i18n( "This application is currently in offline mode. Do you want to connect?" );
159 return ( KMessageBox::questionYesNo( mainWidget
,
160 i18n("This application is currently in offline mode. Do you want to connect in order to carry out this operation?"),
161 i18n("Leave Offline Mode?"),
162 i18n("Connect"), i18n("Stay Offline"),
163 QLatin1String("OfflineModeAlwaysGoOnline") ) == KMessageBox::Yes
);
166 #include "connectionmanager.moc"