summaryrefslogtreecommitdiffstats
path: root/src/knemod/interfacestatistics.h
blob: a4e3246e34da17815780a3382b8634677420b6b9 (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
/* 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.
*/

#ifndef INTERFACESTATISTICS_H
#define INTERFACESTATISTICS_H

#include <qobject.h>
#include <qptrlist.h>

#include "global.h"

class QTimer;
class Interface;

template<class type>
class StatisticsPtrList : public QPtrList<type>
{
protected:
    virtual int compareItems ( QPtrCollection::Item item1, QPtrCollection::Item item2 )
    {
        StatisticEntry* entry1 = static_cast<StatisticEntry*>( item1 );
        StatisticEntry* entry2 = static_cast<StatisticEntry*>( item2 );

        if ( entry1->year > entry2->year )
        {
            return 1;
        }
        else if ( entry2->year > entry1->year )
        {
            return -1;
        } // ...here we know that years are the same...
        else if ( entry1->month > entry2->month )
        {
            return 1;
        }
        else if ( entry2->month > entry1->month )
        {
            return -1;
        } // ...here we know that months are the same...
        else if ( entry1->day > entry2->day )
        {
            return 1;
        }
        else if ( entry2->day > entry1->day )
        {
            return -1;
        } // ...here we know that dates are equal.

        return 0;
    };
};

/**
 * This class is able to collect transfered data for an interface,
 * store it in a file and deliver it on request.
 *
 * @short Statistics of transfered data for an interface
 * @author Percy Leonhardt <percy@eris23.de>
 */

class InterfaceStatistics : public QObject
{
    Q_OBJECT
public:
    /**
     * Default Constructor
     */
    InterfaceStatistics( Interface* interface );

    /**
     * Default Destructor
     */
    virtual ~InterfaceStatistics();

    /**
     * Load the statistics from a xml file
     */
    void loadStatistics();

    /**
     * Called from Interface::configChanged() when the user
     * changed the settings.
     */
    void configChanged();

    const StatisticEntry* getCurrentDay() const;
    const StatisticEntry* getCurrentMonth() const;
    const StatisticEntry* getCurrentYear() const;
    const StatisticsPtrList<StatisticEntry>& getDayStatistics() const;
    const StatisticsPtrList<StatisticEntry>& getMonthStatistics() const;
    const StatisticsPtrList<StatisticEntry>& getYearStatistics() const;

signals:
    /**
     * The current entry has changed. There is only one signal
     * for day, month and year because if the day changes,
     * month and year also change.
     */
    void currentEntryChanged();
    /**
     * The list has changed i.e. there is a new day entry or the list was cleared
     */
    void dayStatisticsChanged();
    /**
     * The list has changed i.e. there is a new month entry or the list was cleared
     */
    void monthStatisticsChanged();
    /**
     * The list has changed i.e. there is a new year entry or the list was cleared
     */
    void yearStatisticsChanged();

public slots:
    /**
     * Save the statistics to a xml file
     * (slot so it can be triggered by a timer signal)
     */
    void saveStatistics();
    /**
     * Add incoming data to the current day, month and year
     */
    void addIncomingData( unsigned long data );
    /**
     * Add outgoing data to the current day, month and year
     */
    void addOutgoingData( unsigned long data );
    /**
     * Clear all entries of the day statistics
     */
    void clearDayStatistics();
    /**
     * Clear all entries of the month statistics
     */
    void clearMonthStatistics();
    /**
     * Clear all entries of the year statistics
     */
    void clearYearStatistics();

private:
    /**
     * Make sure the current entry corresponds with the current date
     */
    void checkCurrentEntry();
    /**
     * Fill the statistics with a current entry
     */
    void initStatistics();
    /**
     * Check if the current day is in the day statistics. If found set
     * mCurrentDay to the found entry else create a new one.
     */
    void updateCurrentDay();
    /**
     * Check if the current month is in the month statistics. If found set
     * mCurrentMonth to the found entry else create a new one.
     */
    void updateCurrentMonth();
    /**
     * Check if the current year is in the year statistics. If found set
     * mCurrentYear to the found entry else create a new one.
     */
    void updateCurrentYear();

    QTimer* mSaveTimer;
    Interface* mInterface;
    StatisticEntry* mCurrentDay;
    StatisticEntry* mCurrentMonth;
    StatisticEntry* mCurrentYear;
    StatisticsPtrList<StatisticEntry> mDayStatistics;
    StatisticsPtrList<StatisticEntry> mMonthStatistics;
    StatisticsPtrList<StatisticEntry> mYearStatistics;
};

#endif // INTERFACESTATISTICS_H