summaryrefslogtreecommitdiffstats
path: root/kopete/plugins/smpppdcs/smpppdcsplugin.cpp
blob: f17f2ca97c32a340d1b6aa839cfcfb9a5f52d864 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
    smpppdcsplugin.cpp
 
    Copyright (c) 2002-2003 by Chris Howells         <howells@kde.org>
    Copyright (c) 2003      by Martijn Klingens      <klingens@kde.org>
    Copyright (c) 2004-2006 by Heiko Schaefer        <heiko@rangun.de>
 
    Kopete    (c) 2002-2006 by the Kopete developers <kopete-devel@kde.org>
 
    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; version 2 of the License.               *
    *                                                                       *
    *************************************************************************
*/

#include "onlineinquiry.h"
#include "smpppdcsplugin.h"

#include <tqtimer.h>

#include <kdebug.h>
#include <kgenericfactory.h>

#include "kopeteprotocol.h"
#include "networkstatuscommon.h"
#include "kopetepluginmanager.h"
#include "kopeteaccountmanager.h"

#include "detectornetworkstatus.h"
#include "detectornetstat.h"
#include "detectorsmpppd.h"
#include "smpppdcsconfig.h"

typedef KGenericFactory<SMPPPDCSPlugin> SMPPPDCSPluginFactory;
K_EXPORT_COMPONENT_FACTORY(kopete_smpppdcs, SMPPPDCSPluginFactory("kopete_smpppdcs"))

SMPPPDCSPlugin::SMPPPDCSPlugin(TQObject *parent, const char * name, const TQStringList& /* args */)
        : DCOPObject("SMPPPDCSIface"), Kopete::Plugin(SMPPPDCSPluginFactory::instance(), parent, name),
        m_detectorSMPPPD(NULL), m_detectorNetstat(NULL), m_detectorNetworkStatus(NULL), m_timer(NULL),
m_onlineInquiry(NULL) {

    kdDebug(14312) << k_funcinfo << endl;

    m_pluginConnected = false;

    m_onlineInquiry   = new OnlineInquiry();
    m_detectorSMPPPD  = new DetectorSMPPPD(this);
    m_detectorNetstat = new DetectorNetstat(this);

    // experimental, not used yet
    m_detectorNetworkStatus = new DetectorNetworkStatus(this);

    // we wait for the allPluginsLoaded signal, to connect
    // as early as possible after startup, but not before
    // all accounts are ready
    connect(Kopete::PluginManager::self(), TQT_SIGNAL(allPluginsLoaded()),
            this, TQT_SLOT(allPluginsLoaded()));

    // if kopete was already running and the plugin
    // was loaded later, we check once after 15 secs
    // if all other plugins have been loaded
    TQTimer::singleShot(15000, this, TQT_SLOT(allPluginsLoaded()));
}

SMPPPDCSPlugin::~SMPPPDCSPlugin() {

    kdDebug(14312) << k_funcinfo << endl;

    delete m_timer;
    delete m_detectorSMPPPD;
    delete m_detectorNetstat;
    delete m_detectorNetworkStatus;
    delete m_onlineInquiry;
}

void SMPPPDCSPlugin::allPluginsLoaded() {

    if(Kopete::PluginManager::self()->isAllPluginsLoaded()) {
        m_timer = new TQTimer();
        connect(m_timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotCheckStatus()));

		if(SMPPPDCSConfig::self()->useSmpppd()) {
            m_timer->start(30000);
        } else {
            // we use 1 min interval, because it reflects
            // the old connectionstatus plugin behaviour
            m_timer->start(60000);
        }

        slotCheckStatus();
    }
}

bool SMPPPDCSPlugin::isOnline() const {
	return m_onlineInquiry->isOnline(SMPPPDCSConfig::self()->useSmpppd());
}

void SMPPPDCSPlugin::slotCheckStatus() {
	
	// reread config to get changes
	SMPPPDCSConfig::self()->readConfig();
	
	if(SMPPPDCSConfig::self()->useSmpppd()) {
        m_detectorSMPPPD->checkStatus();
    } else {
        m_detectorNetstat->checkStatus();
    }
}

void SMPPPDCSPlugin::setConnectedStatus( bool connected ) {
    kdDebug(14312) << k_funcinfo << connected << endl;

    // We have to handle a few cases here. First is the machine is connected, and the plugin thinks
    // we're connected. Then we don't do anything. Next, we can have machine connected, but plugin thinks
    // we're disconnected. Also, machine disconnected, plugin disconnected -- we
    // don't do anything. Finally, we can have the machine disconnected, and the plugin thinks we're
    // connected. This mechanism is required so that we don't keep calling the connect/disconnect functions
    // constantly.

    if ( connected && !m_pluginConnected ) {
        // The machine is connected and plugin thinks we're disconnected
        kdDebug(14312) << k_funcinfo << "Setting m_pluginConnected to true" << endl;
        m_pluginConnected = true;
        connectAllowed();
        kdDebug(14312) << k_funcinfo << "We're connected" << endl;
    } else if ( !connected && m_pluginConnected ) {
        // The machine isn't connected and plugin thinks we're connected
        kdDebug(14312) << k_funcinfo << "Setting m_pluginConnected to false" << endl;
        m_pluginConnected = false;
        disconnectAllowed();
        kdDebug(14312) << k_funcinfo << "We're offline" << endl;
    }
}

void SMPPPDCSPlugin::connectAllowed() {

	TQStringList list = SMPPPDCSConfig::self()->ignoredAccounts();

    Kopete::AccountManager * m = Kopete::AccountManager::self();
    for(TQPtrListIterator<Kopete::Account> it(m->accounts())
            ;
            it.current();
            ++it) {

#ifndef NDEBUG
        if(it.current()->inherits("Kopete::ManagedConnectionAccount")) {
            kdDebug(14312) << k_funcinfo << "Account " << it.current()->protocol()->pluginId() + "_" + it.current()->accountId() << " is an managed account!" << endl;
        } else {
            kdDebug(14312) << k_funcinfo << "Account " << it.current()->protocol()->pluginId() + "_" + it.current()->accountId() << " is an unmanaged account!" << endl;
        }
#endif

        if(!list.contains(it.current()->protocol()->pluginId() + "_" + it.current()->
                          accountId())) {
            it.current()->connect();
        }
    }
}

void SMPPPDCSPlugin::disconnectAllowed() {

	TQStringList list = SMPPPDCSConfig::self()->ignoredAccounts();

    Kopete::AccountManager * m = Kopete::AccountManager::self();
    for(TQPtrListIterator<Kopete::Account> it(m->accounts())
            ;
            it.current();
            ++it) {

#ifndef NDEBUG
        if(it.current()->inherits("Kopete::ManagedConnectionAccount")) {
            kdDebug(14312) << k_funcinfo << "Account " << it.current()->protocol()->pluginId() + "_" + it.current()->accountId() << " is an managed account!" << endl;
        } else {
            kdDebug(14312) << k_funcinfo << "Account " << it.current()->protocol()->pluginId() + "_" + it.current()->accountId() << " is an unmanaged account!" << endl;
        }
#endif

        if(!list.contains(it.current()->protocol()->pluginId() + "_" + it.current()->accountId())) {
            it.current()->disconnect();
        }
    }
}

TQString SMPPPDCSPlugin::detectionMethod() const {
	if(SMPPPDCSConfig::self()->useSmpppd()) {
        return "smpppd";
    } else {
        return "netstat";
    }
}

/*!
    \fn SMPPPDCSPlugin::smpppdServerChanged(const TQString& server)
 */
void SMPPPDCSPlugin::smpppdServerChanged(const TQString& server) {

	TQString oldServer = SMPPPDCSConfig::self()->server().utf8();

    if(oldServer != server) {
        kdDebug(14312) << k_funcinfo << "Detected a server change" << endl;
        m_detectorSMPPPD->smpppdServerChange();
    }
}

void SMPPPDCSPlugin::aboutToUnload() {

    kdDebug(14312) << k_funcinfo << endl;

    if(m_timer) {
        m_timer->stop();
    }

    emit readyForUnload();
}

#include "smpppdcsplugin.moc"

// vim: set noet ts=4 sts=4 sw=4: