1
/* This file is part of KNemo
2
Copyright (C) 2010 John Stamp <jstamp@users.sourceforge.net>
4
KNemo is free software; you can redistribute it and/or modify
5
it under the terms of the GNU Library General Public License as
6
published by the Free Software Foundation; either version 2 of
7
the License, or (at your option) any later version.
9
KNemo is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
21
#include "statisticsmodel.h"
22
#include "xmlstorage.h"
23
#include "commonstorage.h"
27
#include <KCalendarSystem>
30
static const char doc_name[] = "statistics";
31
static const char attrib_calendar[] = "calendar";
32
static const char attrib_updated[] = "lastUpdated";
33
static const char attrib_rx[] = "rxBytes";
34
static const char attrib_tx[] = "txBytes";
37
XmlStorage::XmlStorage()
41
void XmlStorage::loadGroup( StorageData *sd, const QDomElement& parentItem,
42
StatisticsModel* statistics )
44
QDomNode n = parentItem.namedItem( periods.at( statistics->periodType() ) + "s" );
47
QDomNode node = n.firstChild();
48
while ( !node.isNull() )
50
QDomElement element = node.toElement();
51
if ( !element.isNull() )
56
int year = element.attribute( periods.at( KNemoStats::Year ) ).toInt();
57
int month = element.attribute( periods.at( KNemoStats::Month ), "1" ).toInt();
58
int day = element.attribute( periods.at( KNemoStats::Day ), "1" ).toInt();
59
sd->calendar->setDate( date, year, month, day );
63
switch ( statistics->periodType() )
65
case KNemoStats::Hour:
66
time = QTime( element.attribute( periods.at( KNemoStats::Hour ) ).toInt(), 0 );
72
int entryIndex = statistics->createEntry( QDateTime( date, time ) );
73
statistics->setTraffic( entryIndex, element.attribute( attrib_rx ).toULongLong(), element.attribute( attrib_tx ).toULongLong() );
76
node = node.nextSibling();
78
statistics->sort( 0 );
79
// Make sure the ids are in order after a sort
80
for ( int i = 0; i < statistics->rowCount(); ++i )
82
statistics->setId( i, i );
87
bool XmlStorage::loadStats( QString name, StorageData *sd, QHash<int, StatisticsModel*> *models )
89
KUrl dir( generalSettings->statisticsDir );
90
QDomDocument doc( doc_name );
91
QFile file( dir.path() + statistics_prefix + name );
93
if ( !file.open( QIODevice::ReadOnly ) )
95
if ( !doc.setContent( &file ) )
102
QDomElement root = doc.documentElement();
104
// If unknown or empty calendar it will default to gregorian
105
sd->calendar = KCalendarSystem::create( root.attribute( attrib_calendar ) );
106
foreach( StatisticsModel * s, *models )
108
s->setCalendar( sd->calendar );
109
loadGroup( sd, root, s );
112
sd->lastSaved = root.attribute( attrib_updated ).toUInt();