File: connectionmanager.cpp

package info (click to toggle)
kopete 4%3A16.08.1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 38,656 kB
  • ctags: 50,148
  • sloc: cpp: 347,618; ansic: 13,983; xml: 1,775; lex: 1,735; python: 421; sh: 271; perl: 126; ruby: 28; makefile: 18
file content (166 lines) | stat: -rw-r--r-- 5,177 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
     connectionmanager.cpp - Provides the client side interface to the kde networkstatus daemon

     Copyright (c) 2004      by Will Stephenson <wstephenson@kde.org>

     Kopete    (c) 2004-2007 by the Kopete developers  <kopete-devel@kde.org>

     *************************************************************************
     *                                                                       *
     * This library is free software; you can redistribute it and/or         *
     * modify it under the terms of the GNU Lesser General Public            *
     * License as published by the Free Software Foundation; either          *
     * version 2 of the License, or (at your option) any later version.      *
     *                                                                       *
     *************************************************************************
 */


#include "connectionmanager.h"

#include <kdebug.h>
#include <klocale.h>
#include <kmessagebox.h>

#include "clientiface_stub.h"
#include "networkstatuscommon.h"

// ConnectionManager's private parts
class ConnectionManagerPrivate
{
	public:
		// this holds the currently active state
		ConnectionManager::State m_state;
		ClientIface_stub * m_stub;
		bool m_userInitiatedOnly;
};

// Connection manager itself
ConnectionManager::ConnectionManager( QObject * parent, const char * name ) : DCOPObject( "ConnectionManager" ),QObject( parent )
{
	setObjectName(name);
	d = new ConnectionManagerPrivate;
	
	d->m_stub = new ClientIface_stub( kapp->dcopClient(), "kded", "networkstatus" );
	
	connectDCOPSignal( "kded", "networkstatus", "statusChange(QString,int)", "slotStatusChanged(QString,int)", false );
	d->m_userInitiatedOnly = false;
	initialise();
}

ConnectionManager *ConnectionManager::self()
{
	static ConnectionManager s;
	return &s;
}

void ConnectionManager::initialise()
{
	// determine initial state and set the state object accordingly.
	d->m_state = Inactive;
	updateStatus();
}

void ConnectionManager::updateStatus()
{
	NetworkStatus::EnumStatus daemonStatus = (NetworkStatus::EnumStatus)d->m_stub->status( QString() );
	kDebug() ;
	switch ( daemonStatus )
	{
		case NetworkStatus::Offline:
		case NetworkStatus::OfflineFailed:
		case NetworkStatus::OfflineDisconnected:
		case NetworkStatus::ShuttingDown:
			if ( d->m_state == Online )
			{
				kDebug() << "STATE IS PENDING";
				d->m_state = Pending;
			}
			else
			{
				kDebug() << "STATE IS OFFLINE";
				d->m_state = Offline;
			}
			break;
		case NetworkStatus::Establishing:
		case NetworkStatus::Online:
			kDebug() << "STATE IS ONLINE";
			d->m_state = Online;
			break;
		case NetworkStatus::NoNetworks:
		case NetworkStatus::Unreachable:
			kDebug() << "STATE IS INACTIVE";
			d->m_state = Inactive;
			break;
	}
}

ConnectionManager::~ConnectionManager()
{
	delete d;
}

NetworkStatus::EnumStatus ConnectionManager::status( const QString & host )
{
	// need also to check that the daemon hasn't died
	updateStatus();
	if ( d->m_state == Pending )
		return NetworkStatus::Offline;
	if ( d->m_state == Online )
		return NetworkStatus::Online;
	if ( d->m_state == Offline )
		return NetworkStatus::Offline;
	return NetworkStatus::NoNetworks;
}

NetworkStatus::EnumRequestResult ConnectionManager::requestConnection( QWidget * mainWidget, const QString & host, bool userInitiated )
{
	kDebug() ;
	NetworkStatus::EnumRequestResult result;
	// if offline and the user has previously indicated they didn't want any new connections, suppress it
	if ( d->m_state == Offline && !userInitiated && d->m_userInitiatedOnly )
		result = NetworkStatus::UserRefused;
	// if offline, ask the user whether this connection should be allowed
	if ( d->m_state == Offline )
	{
		if ( askToConnect( mainWidget ) )
			//result = NetworkStatus::Connected;
			result = (NetworkStatus::EnumRequestResult)d->m_stub->request( host, userInitiated );
		else
			result = NetworkStatus::UserRefused;
	}
	// otherwise, just ask for the connection
	else
		result = (NetworkStatus::EnumRequestResult)d->m_stub->request( host, userInitiated );
	
	return result;
}

void ConnectionManager::relinquishConnection( const QString & host )
{
	d->m_stub->relinquish( host );
}

void ConnectionManager::slotStatusChanged( QString host, int status )
{
	kDebug() ;
	updateStatus();
	// reset user initiated only flag if we are now online
	if ( d->m_state == Online )
		d->m_userInitiatedOnly = false;

	emit statusChanged( host, (NetworkStatus::EnumStatus)status );
}

bool ConnectionManager::askToConnect( QWidget * mainWidget )
{
	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?" );
	i18n( "This application is currently in offline mode.  Do you want to connect?" );
	return ( KMessageBox::questionYesNo( mainWidget,
			i18n("This application is currently in offline mode.  Do you want to connect in order to carry out this operation?"),
			i18n("Leave Offline Mode?"),
			i18n("Connect"), i18n("Stay Offline"),
			QLatin1String("OfflineModeAlwaysGoOnline") ) == KMessageBox::Yes );
}

#include "connectionmanager.moc"