~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to libkopete/connectionmanager.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
     connectionmanager.cpp - Provides the client side interface to the kde networkstatus daemon
 
3
 
 
4
     Copyright (c) 2004      by Will Stephenson <wstephenson@kde.org>
 
5
 
 
6
     Kopete    (c) 2004-2007 by the Kopete developers  <kopete-devel@kde.org>
 
7
 
 
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
     *************************************************************************
 
16
 */
 
17
 
 
18
 
 
19
#include <kdebug.h>
 
20
#include <klocale.h>
 
21
#include <kmessagebox.h>
 
22
 
 
23
#include "clientiface_stub.h"
 
24
#include "networkstatuscommon.h"
 
25
 
 
26
#include "connectionmanager.h"
 
27
 
 
28
// ConnectionManager's private parts
 
29
class ConnectionManagerPrivate
 
30
{
 
31
        public:
 
32
                // this holds the currently active state
 
33
                ConnectionManager::State m_state;
 
34
                ClientIface_stub * m_stub;
 
35
                bool m_userInitiatedOnly;
 
36
};
 
37
 
 
38
// Connection manager itself
 
39
ConnectionManager::ConnectionManager( QObject * parent, const char * name ) : DCOPObject( "ConnectionManager" ),QObject( parent )
 
40
{
 
41
        setObjectName(name);
 
42
        d = new ConnectionManagerPrivate;
 
43
        
 
44
        d->m_stub = new ClientIface_stub( kapp->dcopClient(), "kded", "networkstatus" );
 
45
        
 
46
        connectDCOPSignal( "kded", "networkstatus", "statusChange(QString,int)", "slotStatusChanged(QString,int)", false );
 
47
        d->m_userInitiatedOnly = false;
 
48
        initialise();
 
49
}
 
50
 
 
51
ConnectionManager *ConnectionManager::self()
 
52
{
 
53
        static ConnectionManager s;
 
54
        return &s;
 
55
}
 
56
 
 
57
void ConnectionManager::initialise()
 
58
{
 
59
        // determine initial state and set the state object accordingly.
 
60
        d->m_state = Inactive;
 
61
        updateStatus();
 
62
}
 
63
 
 
64
void ConnectionManager::updateStatus()
 
65
{
 
66
        NetworkStatus::EnumStatus daemonStatus = (NetworkStatus::EnumStatus)d->m_stub->status( QString() );
 
67
        kDebug() ;
 
68
        switch ( daemonStatus )
 
69
        {
 
70
                case NetworkStatus::Offline:
 
71
                case NetworkStatus::OfflineFailed:
 
72
                case NetworkStatus::OfflineDisconnected:
 
73
                case NetworkStatus::ShuttingDown:
 
74
                        if ( d->m_state == Online )
 
75
                        {
 
76
                                kDebug() << "STATE IS PENDING";
 
77
                                d->m_state = Pending;
 
78
                        }
 
79
                        else
 
80
                        {
 
81
                                kDebug() << "STATE IS OFFLINE";
 
82
                                d->m_state = Offline;
 
83
                        }
 
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;
 
95
        }
 
96
}
 
97
 
 
98
ConnectionManager::~ConnectionManager()
 
99
{
 
100
        delete d;
 
101
}
 
102
 
 
103
NetworkStatus::EnumStatus ConnectionManager::status( const QString & host )
 
104
{
 
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;
 
114
}
 
115
 
 
116
NetworkStatus::EnumRequestResult ConnectionManager::requestConnection( QWidget * mainWidget, const QString & host, bool userInitiated )
 
117
{
 
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 )
 
125
        {
 
126
                if ( askToConnect( mainWidget ) )
 
127
                        //result = NetworkStatus::Connected;
 
128
                        result = (NetworkStatus::EnumRequestResult)d->m_stub->request( host, userInitiated );
 
129
                else
 
130
                        result = NetworkStatus::UserRefused;
 
131
        }
 
132
        // otherwise, just ask for the connection
 
133
        else
 
134
                result = (NetworkStatus::EnumRequestResult)d->m_stub->request( host, userInitiated );
 
135
        
 
136
        return result;
 
137
}
 
138
 
 
139
void ConnectionManager::relinquishConnection( const QString & host )
 
140
{
 
141
        d->m_stub->relinquish( host );
 
142
}
 
143
 
 
144
void ConnectionManager::slotStatusChanged( QString host, int status )
 
145
{
 
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;
 
151
 
 
152
        emit statusChanged( host, (NetworkStatus::EnumStatus)status );
 
153
}
 
154
 
 
155
bool ConnectionManager::askToConnect( QWidget * mainWidget )
 
156
{
 
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 );
 
164
}
 
165
 
 
166
#include "connectionmanager.moc"