~ubuntu-branches/ubuntu/natty/knemo/natty

« back to all changes in this revision

Viewing changes to src/knemod/storage/xmlstorage.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-02-22 16:36:22 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20110222163622-d8i62gy1stn7tydv
Tags: 0.7.0-0ubuntu1
* New upstream release.
* Switch to source format 3.0 (quilt).
* Make knemo depend on libqt4-sql-sqlite.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of KNemo
 
2
   Copyright (C) 2010 John Stamp <jstamp@users.sourceforge.net>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
18
*/
 
19
 
 
20
#include "global.h"
 
21
#include "statisticsmodel.h"
 
22
#include "xmlstorage.h"
 
23
#include "commonstorage.h"
 
24
 
 
25
#include <QDomNode>
 
26
#include <QFile>
 
27
#include <KCalendarSystem>
 
28
 
 
29
// xml storage
 
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";
 
35
 
 
36
 
 
37
XmlStorage::XmlStorage()
 
38
{
 
39
}
 
40
 
 
41
void XmlStorage::loadGroup( StorageData *sd, const QDomElement& parentItem,
 
42
    StatisticsModel* statistics )
 
43
{
 
44
    QDomNode n = parentItem.namedItem( periods.at( statistics->periodType() ) + "s" );
 
45
    if ( !n.isNull() )
 
46
    {
 
47
        QDomNode node = n.firstChild();
 
48
        while ( !node.isNull() )
 
49
        {
 
50
            QDomElement element = node.toElement();
 
51
            if ( !element.isNull() )
 
52
            {
 
53
                QDate date;
 
54
                QTime time;
 
55
 
 
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 );
 
60
 
 
61
                if ( date.isValid() )
 
62
                {
 
63
                    switch ( statistics->periodType() )
 
64
                    {
 
65
                        case KNemoStats::Hour:
 
66
                            time = QTime( element.attribute( periods.at( KNemoStats::Hour ) ).toInt(), 0 );
 
67
                            break;
 
68
                        default:
 
69
                            ;;
 
70
                    }
 
71
 
 
72
                    int entryIndex = statistics->createEntry( QDateTime( date, time ) );
 
73
                    statistics->setTraffic( entryIndex, element.attribute( attrib_rx ).toULongLong(), element.attribute( attrib_tx ).toULongLong() );
 
74
                }
 
75
            }
 
76
            node = node.nextSibling();
 
77
        }
 
78
        statistics->sort( 0 );
 
79
        // Make sure the ids are in order after a sort
 
80
        for ( int i = 0; i < statistics->rowCount(); ++i )
 
81
        {
 
82
            statistics->setId( i, i );
 
83
        }
 
84
    }
 
85
}
 
86
 
 
87
bool XmlStorage::loadStats( QString name, StorageData *sd, QHash<int, StatisticsModel*> *models )
 
88
{
 
89
    KUrl dir( generalSettings->statisticsDir );
 
90
    QDomDocument doc( doc_name );
 
91
    QFile file( dir.path() + statistics_prefix + name );
 
92
 
 
93
    if ( !file.open( QIODevice::ReadOnly ) )
 
94
        return false;
 
95
    if ( !doc.setContent( &file ) )
 
96
    {
 
97
        file.close();
 
98
        return false;
 
99
    }
 
100
    file.close();
 
101
 
 
102
    QDomElement root = doc.documentElement();
 
103
 
 
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 )
 
107
    {
 
108
        s->setCalendar( sd->calendar );
 
109
        loadGroup( sd, root, s );
 
110
    }
 
111
 
 
112
    sd->lastSaved = root.attribute( attrib_updated ).toUInt();
 
113
 
 
114
    return true;
 
115
}
 
116
 
 
117