2 This file is part of kdepim.
4 Copyright (c) 2005 Will Stephenson <lists@stevello.free-online.co.uk>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #include <kapplication.h>
24 #include <kmessagebox.h>
25 #include <kstaticdeleter.h>
27 #include "clientiface_stub.h"
28 #include "networkstatuscommon.h"
30 #include "connectionmanager.h"
32 // ConnectionManager's private parts
33 class ConnectionManagerPrivate
36 // this holds the currently active state
37 ConnectionManager::State m_state
;
38 ClientIface_stub
* m_stub
;
39 bool m_userInitiatedOnly
;
42 // Connection manager itself
43 ConnectionManager::ConnectionManager( QObject
* parent
, const char * name
) : DCOPObject( "ConnectionManager" ),QObject( parent
, name
)
45 d
= new ConnectionManagerPrivate
;
47 d
->m_stub
= new ClientIface_stub( kapp
->dcopClient(), "kded", "networkstatus" );
49 connectDCOPSignal( "kded", "networkstatus", "statusChange(QString,int)", "slotStatusChanged(QString,int)", false );
50 d
->m_userInitiatedOnly
= false;
54 ConnectionManager
*ConnectionManager::s_self
= 0L;
56 ConnectionManager
*ConnectionManager::self()
58 static KStaticDeleter
<ConnectionManager
> deleter
;
60 deleter
.setObject( s_self
, new ConnectionManager( 0, "connection_manager" ) );
64 void ConnectionManager::initialise()
66 // determine initial state and set the state object accordingly.
71 void ConnectionManager::updateStatus()
73 /*NetworkStatus::EnumStatus daemonStatus = (NetworkStatus::EnumStatus)d->m_stub->status( QString::null );
74 switch ( daemonStatus )
78 case OfflineDisconnected:
80 if ( d->m_state == Online )
91 d->m_state = Inactive;
96 ConnectionManager::~ConnectionManager()
101 NetworkStatus::EnumStatus
ConnectionManager::status( const QString
& host
)
103 if ( d
->m_state
== Inactive
)
104 return NetworkStatus::NoNetworks
;
106 return NetworkStatus::Offline
;
108 NetworkStatus::EnumRequestResult
ConnectionManager::requestConnection( QWidget
* mainWidget
, const QString
& host
, bool userInitiated
)
110 NetworkStatus::EnumRequestResult result
;
111 // if offline and the user has previously indicated they didn't want any new connections, suppress it
112 if ( d
->m_state
== Offline
&& !userInitiated
&& d
->m_userInitiatedOnly
)
113 result
= NetworkStatus::UserRefused
;
114 // if offline, ask the user whether this connection should be allowed
115 if ( d
->m_state
== Offline
)
117 if ( askToConnect( mainWidget
) )
118 result
= (NetworkStatus::EnumRequestResult
)d
->m_stub
->request( host
, userInitiated
);
120 result
= NetworkStatus::UserRefused
;
122 // otherwise, just ask for the connection
124 result
= (NetworkStatus::EnumRequestResult
)d
->m_stub
->request( host
, userInitiated
);
129 void ConnectionManager::relinquishConnection( const QString
& host
)
131 d
->m_stub
->relinquish( host
);
134 void ConnectionManager::slotStatusChanged( QString host
, int status
)
137 // reset user initiated only flag if we are now online
138 if ( d
->m_state
== Online
)
139 d
->m_userInitiatedOnly
= false;
141 emit
statusChanged( host
, (NetworkStatus::EnumStatus
)status
);
144 bool ConnectionManager::askToConnect( QWidget
* mainWidget
)
146 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?" );
147 i18n( "This application is currently in offline mode. Do you want to connect?" );
148 i18n( "Message shown when a network connection failed. The placeholder contains the concrete description of the operation eg 'while performing this operation", "A network connection failed %1. Do you want to place the application in offline mode?" );
149 return ( KMessageBox::questionYesNo( mainWidget
,
150 i18n("This application is currently in offline mode. Do you want to connect in order to carry out this operation?"),
151 i18n("Leave Offline Mode?"),
152 i18n("Connect"), i18n("Do Not Connect"),
153 QString::fromLatin1("OfflineModeAlwaysGoOnline") ) == KMessageBox::Yes
);
156 #include "connectionmanager.moc"