summaryrefslogtreecommitdiffstats
path: root/networkstatus/networkstatus.cpp
blob: f6966639df68a6085d8097705100fc8e231e8cdd (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
/*  This file is part of kdepim
    Copyright (C) 2005,2007 Will Stephenson <wstephenson@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library.  If not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    As a special exception, permission is given to link this library
    with any edition of TQt, and distribute the resulting executable,
    without including the source code for TQt in the source distribution.
*/

#include "networkstatus.h"

#include <tqmap.h>

#include <dcopclient.h>
#include <kapplication.h>
#include <kdebug.h>

#include "network.h"
#include <kdemacros.h>

extern "C" {
    KDE_EXPORT KDEDModule* create_networkstatus( const TQCString& obj )
    {
        return new NetworkStatusModule( obj );
    }
}

// INTERNALLY USED STRUCTS AND TYPEDEFS

typedef TQMap< TQString, Network * > NetworkMap;

class NetworkStatusModule::Private
{
public:
    NetworkMap networks;
    NetworkStatus::Status status;
};

// CTORS/DTORS

NetworkStatusModule::NetworkStatusModule( const TQCString & obj ) : KDEDModule( obj ), d( new Private )
{
    d->status = NetworkStatus::NoNetworks;
    connect( kapp->dcopClient(), TQT_SIGNAL( applicationRemoved( const TQCString& ) ) , this, TQT_SLOT( unregisteredFromDCOP( const TQCString& ) ) );
    //	connect( kapp->dcopClient(), TQT_SIGNAL( applicationRegistered( const TQCString& ) ) , this, TQT_SLOT( registeredToDCOP( const TQCString& ) ) );
}

NetworkStatusModule::~NetworkStatusModule()
{
    NetworkMap::ConstIterator it;
    const NetworkMap::ConstIterator end = d->networks.end();

    for ( it = d->networks.begin(); it != end; ++it ) {
        delete ( *it );
    }

    delete d;
}

// CLIENT INTERFACE

TQStringList NetworkStatusModule::networks()
{
    kdDebug() << k_funcinfo << " contains " << d->networks.count() << " networks" << endl;
    return d->networks.keys();
}

int NetworkStatusModule::status()
{
    kdDebug() << k_funcinfo << " status: " << (int)d->status << endl;
    return (int)d->status;
}

//protected:

void NetworkStatusModule::updateStatus()
{
    NetworkStatus::Status bestStatus = NetworkStatus::NoNetworks;
    const NetworkStatus::Status oldStatus = d->status;

    NetworkMap::ConstIterator it;
    const NetworkMap::ConstIterator end = d->networks.end();
    for ( it = d->networks.begin(); it != end; ++it ) {
        if ( ( *it )->status() > bestStatus )
            bestStatus = ( *it )->status();
    }
    d->status = bestStatus;

    if ( oldStatus != d->status ) {
        statusChange( (int)d->status );
    }
}

void NetworkStatusModule::unregisteredFromDCOP( const TQCString & appId )
{
    // unregister and delete any networks owned by a service that has just unregistered
    NetworkMap::Iterator it = d->networks.begin();
    const NetworkMap::Iterator end = d->networks.end();
	while (it != d->networks.end())
    {
        if ( ( *it )->service() == TQString( appId ) )
        {
            NetworkMap::Iterator toRemove = it++;
			delete *toRemove;
            d->networks.remove( toRemove );
            updateStatus();
			continue;
        }
		++it;
    }
}

// SERVICE INTERFACE //
void NetworkStatusModule::setNetworkStatus( const TQString & networkName, int st )
{
    kdDebug() << k_funcinfo << networkName << ", " << st << endl;
    NetworkStatus::Status changedStatus = (NetworkStatus::Status)st;
    Network * net = 0;
    NetworkMap::Iterator it = d->networks.find( networkName );
    if ( it != d->networks.end() ) {
        net = (*it);
        net->setStatus( changedStatus );
        updateStatus();
    }
    else
        kdDebug() << "  No network named '" << networkName << "' found." << endl;
}

void NetworkStatusModule::registerNetwork( const NetworkStatus::Properties properties )
{
    kdDebug() << k_funcinfo << properties.name << ", with status " << properties.status << endl;

    d->networks.insert( properties.name, new Network( properties ) );
    updateStatus();
}

void NetworkStatusModule::unregisterNetwork( const TQString & networkName )
{
    kdDebug() << k_funcinfo << networkName << endl;

    NetworkMap::Iterator it = d->networks.find( networkName );
    if ( it != d->networks.end() ) {
        delete *it;
        d->networks.remove( it );
    }
    updateStatus();
}

#include "networkstatus.moc"
// vim: set noet sw=4 ts=4: