summaryrefslogtreecommitdiffstats
path: root/src/knemod/interfacestatisticsdialog.cpp
blob: 7a317ace836f567f2bdbe0158339d21f85a3de2a (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
/* This file is part of KNemo
   Copyright (C) 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 <tqtable.h>
#include <tqdatetime.h>
#include <tqpushbutton.h>

#include <klocale.h>
#include <kio/global.h>
#include <kiconloader.h>
#include <kcalendarsystem.h>

#include "data.h"
#include "interface.h"
#include "interfacestatistics.h"
#include "interfacestatisticsdialog.h"

/*
 * I assume that the last entry in each table is also the current. This will fail
 * if we walk back in time, so better not play with the system date...
 */

InterfaceStatisticsDialog::InterfaceStatisticsDialog( Interface* interface, TQWidget* parent, const char* name )
    : InterfaceStatisticsDlg( parent, name ),
      mInterface( interface )
{
    setIcon( SmallIcon( "knemo" ) );
    setCaption( interface->getName() + " " + i18n( "Statistics" ) );

    connect( buttonClearDaily, TQT_SIGNAL( clicked() ), TQT_SIGNAL( clearDailyStatisticsClicked() ) );
    connect( buttonClearMonthly, TQT_SIGNAL( clicked() ), TQT_SIGNAL( clearMonthlyStatisticsClicked() ) );
    connect( buttonClearYearly, TQT_SIGNAL( clicked() ), TQT_SIGNAL( clearYearlyStatisticsClicked() ) );
}

InterfaceStatisticsDialog::~InterfaceStatisticsDialog()
{
}

void InterfaceStatisticsDialog::updateDays()
{
    TQPtrList<StatisticEntry> dayStatistics = mInterface->getStatistics()->getDayStatistics();
    StatisticEntry* iterator = dayStatistics.first();
    tableDaily->setNumRows( dayStatistics.count() );
    int row = 0;
    while ( iterator )
    {
        TQDate date( iterator->year, iterator->month,  iterator->day );
        tableDaily->verticalHeader()->setLabel( row, KGlobal::locale()->formatDate( date, true ) );
        tableDaily->setText( row, 0, KIO::convertSize( iterator->txBytes ) );
        tableDaily->setText( row, 1, KIO::convertSize( iterator->rxBytes ) );
        tableDaily->setText( row, 2, KIO::convertSize( iterator->rxBytes + iterator->txBytes ) );
        row++;
        iterator = dayStatistics.next();
    }

    tableDaily->setCurrentCell( row - 1, 2 );
    tableDaily->ensureCellVisible( row - 1, 2 );
}

void InterfaceStatisticsDialog::updateMonths()
{
    TQPtrList<StatisticEntry> monthStatistics = mInterface->getStatistics()->getMonthStatistics();
    StatisticEntry* iterator = monthStatistics.first();
    tableMonthly->setNumRows( monthStatistics.count() );
    int row = 0;
    while ( iterator )
    {
        const KCalendarSystem* calendar = KGlobal::locale()->calendar();
        TQString monthName = calendar->monthName( iterator->month, iterator->year ) + " " + TQString::number( iterator->year );
        tableMonthly->verticalHeader()->setLabel( row, monthName );
        tableMonthly->setText( row, 0, KIO::convertSize( iterator->txBytes ) );
        tableMonthly->setText( row, 1, KIO::convertSize( iterator->rxBytes ) );
        tableMonthly->setText( row, 2, KIO::convertSize( iterator->rxBytes + iterator->txBytes ) );
        row++;
        iterator = monthStatistics.next();
    }

    tableMonthly->setCurrentCell( row - 1, 2 );
    tableMonthly->ensureCellVisible( row - 1, 2 );
}

void InterfaceStatisticsDialog::updateYears()
{
    TQPtrList<StatisticEntry> yearStatistics = mInterface->getStatistics()->getYearStatistics();
    StatisticEntry* iterator = yearStatistics.first();
    tableYearly->setNumRows( yearStatistics.count() );
    int row = 0;
    while ( iterator )
    {
        tableYearly->verticalHeader()->setLabel( row, TQString::number( iterator->year ) );
        tableYearly->setText( row, 0, KIO::convertSize( iterator->txBytes ) );
        tableYearly->setText( row, 1, KIO::convertSize( iterator->rxBytes ) );
        tableYearly->setText( row, 2, KIO::convertSize( iterator->rxBytes + iterator->txBytes ) );
        row++;
        iterator = yearStatistics.next();
    }

    tableYearly->setCurrentCell( row - 1, 2 );
    tableYearly->ensureCellVisible( row - 1, 2 );
}

void InterfaceStatisticsDialog::updateCurrentEntry()
{
    int lastRow = tableDaily->numRows() - 1;
    const StatisticEntry* currentEntry = mInterface->getStatistics()->getCurrentDay();
    tableDaily->setText( lastRow, 0, KIO::convertSize( currentEntry->txBytes ) );
    tableDaily->setText( lastRow, 1, KIO::convertSize( currentEntry->rxBytes ) );
    tableDaily->setText( lastRow, 2, KIO::convertSize( currentEntry->rxBytes + currentEntry->txBytes ) );

    lastRow = tableMonthly->numRows() - 1;
    currentEntry = mInterface->getStatistics()->getCurrentMonth();
    tableMonthly->setText( lastRow, 0, KIO::convertSize( currentEntry->txBytes ) );
    tableMonthly->setText( lastRow, 1, KIO::convertSize( currentEntry->rxBytes ) );
    tableMonthly->setText( lastRow, 2, KIO::convertSize( currentEntry->rxBytes + currentEntry->txBytes ) );

    lastRow = tableYearly->numRows() - 1;
    currentEntry = mInterface->getStatistics()->getCurrentYear();
    tableYearly->setText( lastRow, 0, KIO::convertSize( currentEntry->txBytes ) );
    tableYearly->setText( lastRow, 1, KIO::convertSize( currentEntry->rxBytes ) );
    tableYearly->setText( lastRow, 2, KIO::convertSize( currentEntry->rxBytes + currentEntry->txBytes ) );
}

#include "interfacestatisticsdialog.moc"