Added include path for libv4l2 includes, rather than assuming they are
[kdenetwork.git] / kopete / libkopete / connectionmanager.cpp
blob57d35eac9dd567cbcc30f882fb7c28d9f6ecda13
1 /*
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 *************************************************************************
9 * *
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. *
14 * *
15 *************************************************************************
19 #include <kdebug.h>
20 #include <klocale.h>
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
31 public:
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 )
41 setObjectName(name);
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;
48 initialise();
51 ConnectionManager *ConnectionManager::self()
53 static ConnectionManager s;
54 return &s;
57 void ConnectionManager::initialise()
59 // determine initial state and set the state object accordingly.
60 d->m_state = Inactive;
61 updateStatus();
64 void ConnectionManager::updateStatus()
66 NetworkStatus::EnumStatus daemonStatus = (NetworkStatus::EnumStatus)d->m_stub->status( QString() );
67 kDebug() ;
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";
77 d->m_state = Pending;
79 else
81 kDebug() << "STATE IS OFFLINE";
82 d->m_state = Offline;
84 break;
85 case NetworkStatus::Establishing:
86 case NetworkStatus::Online:
87 kDebug() << "STATE IS ONLINE";
88 d->m_state = Online;
89 break;
90 case NetworkStatus::NoNetworks:
91 case NetworkStatus::Unreachable:
92 kDebug() << "STATE IS INACTIVE";
93 d->m_state = Inactive;
94 break;
98 ConnectionManager::~ConnectionManager()
100 delete d;
103 NetworkStatus::EnumStatus ConnectionManager::status( const QString & host )
105 // need also to check that the daemon hasn't died
106 updateStatus();
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 )
118 kDebug() ;
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 );
129 else
130 result = NetworkStatus::UserRefused;
132 // otherwise, just ask for the connection
133 else
134 result = (NetworkStatus::EnumRequestResult)d->m_stub->request( host, userInitiated );
136 return result;
139 void ConnectionManager::relinquishConnection( const QString & host )
141 d->m_stub->relinquish( host );
144 void ConnectionManager::slotStatusChanged( QString host, int status )
146 kDebug() ;
147 updateStatus();
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"