summaryrefslogtreecommitdiffstats
path: root/superkaramba/src/rsssensor.cpp
blob: 5a999ec3badac1d92532a1bd923cf18e2570f3b8 (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
/***************************************************************************
 *   Copyright (C) 2003 by Ralph M. Churchill                              *
 *   mrchucho@yahoo.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "karambaapp.h"
#include "rsssensor.h"
#include <tqdom.h>
#include <tqregexp.h>
#include <kurl.h>
#include <kio/netaccess.h>

RssSensor::RssSensor( const TQString &src, int interval, const TQString &form, const TQString &enc)
    : Sensor(interval),
    source(src),
    format(form),
    encoding(enc)

{
    // Format:
    //  %t = title (DEFAULT)
    //  %d = desc

    if( !encoding.isEmpty() )
    {
        codec = TQTextCodec::codecForName( encoding.ascii() );
        if ( codec == 0)
            codec = TQTextCodec::codecForLocale();
    }
    else
        codec = TQTextCodec::codecForLocale();
}

RssSensor::~RssSensor()
{
}

void RssSensor::update()
{
    TQDomDocument doc;
    TQFile file;
    TQString tmpFile;
    bool OK = false;

#if defined(KDE_3_3)
    if(KIO::NetAccess::download(KURL(source), tmpFile, karambaApp->parentWindow()))
#else
    if(KIO::NetAccess::download(KURL(source), tmpFile))
#endif
    {
        file.setName(tmpFile);
        if ( file.open(IO_ReadOnly | IO_Translate) )
        {
            if ( doc.setContent( &file ) )
            {
                OK = true;
            }
            else
            {
                qDebug("Error on building DOM");
            }
        }
        else
        {
            qDebug("Error opening file");
        }
    }
    else {
        qDebug( "Error Downloading: %s", source.ascii());
    }

    if ( OK )
    {
        SensorParams *sp;
        Meter *meter;

        TQObjectListIt it( *objList );
        while (it != 0)
        {
            sp = (SensorParams*)(*it);
            meter = sp->getMeter();

            // this is a hack to force the
            // clickmap to reset its data lists
            meter->setValue(0);

            TQDomElement docElem = doc.documentElement();
            TQDomNode n = docElem.firstChild();
            if (!n.isNull())
            {
                TQDomNodeList links = docElem.elementsByTagName( "link" );
                TQDomNodeList displays;
                if ( format.contains( "%d", false ) > 0 )
                {
                    displays = docElem.elementsByTagName( "description" );
                }
                else
                {
                    displays = docElem.elementsByTagName( "title" );
                }

                TQRegExp rx("^http://", false );
                for (uint i=1; i < displays.count(); ++i )
                {
                    TQString dispTxt = displays.item( i ).toElement().text();
                    TQString linkTxt = links.item( i ).toElement().text();
                    if( (rx.search(dispTxt) == -1) && (rx.search(linkTxt) != -1) )
                    {
                        meter->setValue( dispTxt );
                        meter->setValue( linkTxt );
                    }
                    else
                    {
                        qDebug("Skipping");
                    }
                }
            }
            else
            {
                qDebug ("Document Node was null!!");
            }

            ++it;
        }
    }
    // Cleanup
    file.close();
    KIO::NetAccess::removeTempFile( tmpFile );
}

#include "rsssensor.moc"