summaryrefslogtreecommitdiffstats
path: root/src/knemod/interfacestatistics.cpp
blob: a73d04b822924eaefe217a571f7edfd9ec90302b (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* This file is part of KNemo
   Copyright (C) 2005, 2006 Percy Leonhardt <percy@eris23.de>

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

   KNemo 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; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <qdom.h>
#include <qfile.h>
#include <qtimer.h>
#include <qstring.h>
#include <qdatetime.h>

#include <kdebug.h>

#include "interface.h"
#include "interfacestatistics.h"

InterfaceStatistics::InterfaceStatistics( Interface* interface )
    : QObject(),
      mInterface( interface )
{
    mDayStatistics.setAutoDelete( true );
    mMonthStatistics.setAutoDelete( true );
    mYearStatistics.setAutoDelete( true );
    initStatistics();

    mSaveTimer = new QTimer();
    connect( mSaveTimer, SIGNAL( timeout() ), this, SLOT( saveStatistics() ) );
    mSaveTimer->start( mInterface->getGeneralData().saveInterval * 1000 );
}

InterfaceStatistics::~InterfaceStatistics()
{
    mSaveTimer->stop();
    delete mSaveTimer;

    mDayStatistics.clear();
    mMonthStatistics.clear();
    mYearStatistics.clear();
}

void InterfaceStatistics::loadStatistics()
{
    QDomDocument doc( "statistics" );
    QString dir = mInterface->getGeneralData().statisticsDir;
    QFile file( dir + "/statistics_" + mInterface->getName() );

    if ( !file.open( IO_ReadOnly ) )
        return;
    if ( !doc.setContent( &file ) )
    {
        file.close();
        return;
    }
    file.close();

    mDayStatistics.clear();
    mMonthStatistics.clear();
    mYearStatistics.clear();

    QDomElement root = doc.documentElement();
    QDomNode n = root.namedItem( "days" );
    if ( !n.isNull() )
    {
        QDomNode dayNode = n.firstChild();
        while ( !dayNode.isNull() )
        {
            QDomElement day = dayNode.toElement();
            if ( !day.isNull() )
            {
                StatisticEntry* entry = new StatisticEntry();
                entry->day = day.attribute( "day" ).toInt();
                entry->month = day.attribute( "month" ).toInt();
                entry->year = day.attribute( "year" ).toInt();
                entry->rxBytes = (Q_UINT64) day.attribute( "rxBytes" ).toDouble();
                entry->txBytes = (Q_UINT64) day.attribute( "txBytes" ).toDouble();
                mDayStatistics.append( entry );
            }
            dayNode = dayNode.nextSibling();
        }
        mDayStatistics.sort();
    }

    n = root.namedItem( "months" );
    if ( !n.isNull() )
    {
        QDomNode monthNode = n.firstChild();
        while ( !monthNode.isNull() )
        {
            QDomElement month = monthNode.toElement();
            if ( !month.isNull() )
            {
                StatisticEntry* entry = new StatisticEntry();
                entry->day = 0;
                entry->month = month.attribute( "month" ).toInt();
                entry->year = month.attribute( "year" ).toInt();
                entry->rxBytes = (Q_UINT64) month.attribute( "rxBytes" ).toDouble();
                entry->txBytes = (Q_UINT64) month.attribute( "txBytes" ).toDouble();
                mMonthStatistics.append( entry );
            }
            monthNode = monthNode.nextSibling();
        }
        mMonthStatistics.sort();
    }

    n = root.namedItem( "years" );
    if ( !n.isNull() )
    {
        QDomNode yearNode = n.firstChild();
        while ( !yearNode.isNull() )
        {
            QDomElement year = yearNode.toElement();
            if ( !year.isNull() )
            {
                StatisticEntry* entry = new StatisticEntry();
                entry->day = 0;
                entry->month = 0;
                entry->year = year.attribute( "year" ).toInt();
                entry->rxBytes = (Q_UINT64) year.attribute( "rxBytes" ).toDouble();
                entry->txBytes = (Q_UINT64) year.attribute( "txBytes" ).toDouble();
                mYearStatistics.append( entry );
            }
            yearNode = yearNode.nextSibling();
        }
        mYearStatistics.sort();
    }
    initStatistics();
}

void InterfaceStatistics::saveStatistics()
{
    QDomDocument doc( "statistics" );
    QDomElement root = doc.createElement( "statistics" );
    doc.appendChild( root );

    QDomElement days = doc.createElement( "days" );
    StatisticEntry* iterator = mDayStatistics.first();
    while ( iterator )
    {
        QDomElement day = doc.createElement( "day" );
        day.setAttribute( "day", iterator->day );
        day.setAttribute( "month", iterator->month );
        day.setAttribute( "year", iterator->year );
        day.setAttribute( "rxBytes", (double) iterator->rxBytes );
        day.setAttribute( "txBytes", (double) iterator->txBytes );
        days.appendChild( day );
        iterator = mDayStatistics.next();
    }
    root.appendChild( days );

    QDomElement months = doc.createElement( "months" );
    iterator = mMonthStatistics.first();
    while ( iterator )
    {
        QDomElement month = doc.createElement( "month" );
        month.setAttribute( "month", iterator->month );
        month.setAttribute( "year", iterator->year );
        month.setAttribute( "rxBytes", (double) iterator->rxBytes );
        month.setAttribute( "txBytes", (double) iterator->txBytes );
        months.appendChild( month );
        iterator = mMonthStatistics.next();
    }
    root.appendChild( months );

    QDomElement years = doc.createElement( "years" );
    iterator = mYearStatistics.first();
    while ( iterator )
    {
        QDomElement year = doc.createElement( "year" );
        year.setAttribute( "year", iterator->year );
        year.setAttribute( "rxBytes", (double) iterator->rxBytes );
        year.setAttribute( "txBytes", (double) iterator->txBytes );
        years.appendChild( year );
        iterator = mYearStatistics.next();
    }
    root.appendChild( years );

    QString dir = mInterface->getGeneralData().statisticsDir;
    QFile file( dir + "/statistics_" + mInterface->getName() );
    if ( !file.open( IO_WriteOnly ) )
        return;

    QTextStream stream( &file );
    stream << doc.toString();
    file.close();
}

void InterfaceStatistics::configChanged()
{
    // restart the timer with the new value
    mSaveTimer->changeInterval( mInterface->getGeneralData().saveInterval * 1000 );
}

const StatisticEntry* InterfaceStatistics::getCurrentDay() const
{
    return mCurrentDay;
}

const StatisticEntry* InterfaceStatistics::getCurrentMonth() const
{
    return mCurrentMonth;
}

const StatisticEntry* InterfaceStatistics::getCurrentYear() const
{
    return mCurrentYear;
}

const StatisticsPtrList<StatisticEntry>& InterfaceStatistics::getDayStatistics() const
{
   return mDayStatistics;
}

const StatisticsPtrList<StatisticEntry>& InterfaceStatistics::getMonthStatistics() const
{
    return mMonthStatistics;
}

const StatisticsPtrList<StatisticEntry>& InterfaceStatistics::getYearStatistics() const
{
    return mYearStatistics;
}

void InterfaceStatistics::addIncomingData( unsigned long data )
{
    checkCurrentEntry();

    mCurrentDay->rxBytes += data;
    mCurrentMonth->rxBytes += data;
    mCurrentYear->rxBytes += data;

    emit currentEntryChanged();
}

void InterfaceStatistics::addOutgoingData( unsigned long data )
{
    checkCurrentEntry();

    mCurrentDay->txBytes += data;
    mCurrentMonth->txBytes += data;
    mCurrentYear->txBytes += data;

    emit currentEntryChanged();
}

void InterfaceStatistics::clearDayStatistics()
{
    mDayStatistics.clear();
    updateCurrentDay();
}

void InterfaceStatistics::clearMonthStatistics()
{
    mMonthStatistics.clear();
    updateCurrentMonth();
}

void InterfaceStatistics::clearYearStatistics()
{
    mYearStatistics.clear();
    updateCurrentYear();
}

void InterfaceStatistics::checkCurrentEntry()
{
    if ( mCurrentDay->day != QDate::currentDate().day() ||
         mCurrentDay->month != QDate::currentDate().month() ||
         mCurrentDay->year != QDate::currentDate().year() )
    {
        // current day has changed
        updateCurrentDay();

        if ( mCurrentMonth->month != QDate::currentDate().month() ||
             mCurrentMonth->year != QDate::currentDate().year() )
        {
            // current month has also changed
            updateCurrentMonth();
        }

        if ( mCurrentYear->year != QDate::currentDate().year() )
        {
            // current year has also changed
            updateCurrentYear();
        }
    }
}

void InterfaceStatistics::initStatistics()
{
    updateCurrentDay();
    updateCurrentMonth();
    updateCurrentYear();

    emit currentEntryChanged();
}

void InterfaceStatistics::updateCurrentDay()
{
    mCurrentDay = mDayStatistics.first();
    while ( mCurrentDay )
    {
        if ( mCurrentDay->day == QDate::currentDate().day() &&
             mCurrentDay->month == QDate::currentDate().month() &&
             mCurrentDay->year == QDate::currentDate().year() )
        {
            // found current day in list
            return;
        }
        mCurrentDay = mDayStatistics.next();
    }

    // the current day is not in the list
    mCurrentDay = new StatisticEntry();
    mCurrentDay->day = QDate::currentDate().day();
    mCurrentDay->month = QDate::currentDate().month();
    mCurrentDay->year = QDate::currentDate().year();
    mCurrentDay->rxBytes = 0;
    mCurrentDay->txBytes = 0;
    mDayStatistics.append( mCurrentDay ); // TODO: insert at correct position
    emit dayStatisticsChanged();
}

void InterfaceStatistics::updateCurrentMonth()
{
    mCurrentMonth = mMonthStatistics.first();
    while ( mCurrentMonth )
    {
        if ( mCurrentMonth->month == QDate::currentDate().month() &&
             mCurrentMonth->year == QDate::currentDate().year() )
        {
            // found current month in list
            return;
        }
        mCurrentMonth = mMonthStatistics.next();
    }

    // the current month is not in the list
    mCurrentMonth = new StatisticEntry();
    mCurrentMonth->day = 0;
    mCurrentMonth->month = QDate::currentDate().month();
    mCurrentMonth->year = QDate::currentDate().year();
    mCurrentMonth->rxBytes = 0;
    mCurrentMonth->txBytes = 0;
    mMonthStatistics.append( mCurrentMonth ); // TODO: insert at correct position
    emit monthStatisticsChanged();
}

void InterfaceStatistics::updateCurrentYear()
{
    mCurrentYear = mYearStatistics.first();
    while ( mCurrentYear )
    {
        if ( mCurrentYear->year == QDate::currentDate().year() )
        {
            // found current year in list
            return;
        }
        mCurrentYear = mYearStatistics.next();
    }

    // the current year is not in the list
    mCurrentYear = new StatisticEntry();
    mCurrentYear->day = 0;
    mCurrentYear->month = 0;
    mCurrentYear->year = QDate::currentDate().year();
    mCurrentYear->rxBytes = 0;
    mCurrentYear->txBytes = 0;
    mYearStatistics.append( mCurrentYear ); // TODO: insert at correct position
    emit yearStatisticsChanged();
}