~ubuntu-branches/ubuntu/feisty/kdetv/feisty

« back to all changes in this revision

Viewing changes to kdetv/plugins/channel/csv/channeliocsv.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-09-17 23:25:16 UTC
  • Revision ID: james.westby@ubuntu.com-20050917232516-9wdsn3ckagbqieh8
Tags: upstream-0.8.8
ImportĀ upstreamĀ versionĀ 0.8.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- c++ -*-
 
2
 
 
3
/*
 
4
 *
 
5
 * Copyright (C) 2002 Richard Moore <rich@kde.org>
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public License
 
18
 * along with this library; see the file COPYING.LIB.  If not, write to
 
19
 * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
 
20
 * Boston, MA 02110-1301, USA.
 
21
 */
 
22
 
 
23
#include <klocale.h>
 
24
#include <qfile.h>
 
25
#include <qstringlist.h>
 
26
#include <qtextstream.h>
 
27
 
 
28
#include <kdebug.h>
 
29
 
 
30
#include "channel.h"
 
31
#include "channelstore.h"
 
32
 
 
33
#include "channeliocsv.h"
 
34
 
 
35
//
 
36
// CSV Format Handler
 
37
//
 
38
 
 
39
ChannelIOFormatCSV::ChannelIOFormatCSV(Kdetv *ktv, QObject *parent, const char *name)
 
40
    : KdetvChannelPlugin(ktv, "CSV Channels", parent, name)
 
41
{
 
42
    _fmtName  = "csv";
 
43
    _menuName = i18n("CSV");
 
44
    _flags    = FormatRead|FormatWrite;
 
45
}
 
46
 
 
47
bool ChannelIOFormatCSV::save( ChannelStore *store, ChannelFileMetaInfo *,
 
48
                               QIODevice *file, const QString&/*fmt*/ )
 
49
{
 
50
    kdDebug() << "IOFormatCSV::save(...)" << endl;
 
51
 
 
52
    kdDebug() << "IOFormatCSV::save(...) channel count=" << store->count() << endl;
 
53
    
 
54
    this->store = store;
 
55
    QTextStream ts( file );
 
56
 
 
57
    for ( uint i = 0; i < store->count(); i++ ) {
 
58
        Channel *j = store->channelAt(i);
 
59
        kdDebug() << "IOFormatCSV::save(...), line='" << j->number() << "(" << j->name() << ")" << endl;
 
60
 
 
61
        ts << j->number() << ","
 
62
           << (long)j->getChannelProperty("frequency").toULongLong() << ","
 
63
           << j->name() << ","
 
64
           << j->getChannelProperty("encoding").toString() << ","
 
65
           << j->getChannelProperty("source").toString() << endl;
 
66
    }
 
67
 
 
68
    kdDebug() << "IOFormatCSV::save(...) done" << endl;
 
69
    return true;
 
70
}
 
71
 
 
72
bool ChannelIOFormatCSV::load( ChannelStore *store, ChannelFileMetaInfo *,
 
73
                               QIODevice *file, const QString&/*fmt*/ )
 
74
{
 
75
    kdDebug() << "IOFormatCSV::load(...)" << endl;
 
76
 
 
77
    this->store = store;
 
78
    QString rawch( file->readAll() );
 
79
    file->close();
 
80
 
 
81
    QStringList lines = QStringList::split("\n", rawch);
 
82
    if ( !lines.count() )
 
83
        return false;
 
84
 
 
85
    bool foundOne = false;
 
86
    for ( QStringList::ConstIterator i = lines.constBegin(); i != lines.constEnd(); ++i ) {
 
87
        QStringList ch = QStringList::split( ",", *i );
 
88
 
 
89
        bool numok, freqok;
 
90
        int num = ch[0].toInt( &numok );
 
91
        unsigned long freq = ch[1].toULong( &freqok );
 
92
        QString name     = (ch.count() >= 3 ) ? ch[2] : QString::null;
 
93
        QString encoding = (ch.count() >= 4 ) ? ch[3] : QString::null;
 
94
        QString source   = (ch.count() >= 5 ) ? ch[4] : QString::null;
 
95
 
 
96
        if ( numok && freqok ) {
 
97
            Channel *x = new Channel( store );
 
98
            x->setName( name );
 
99
            x->setNumber( num );
 
100
            x->setChannelProperty("frequency", (unsigned int)freq);
 
101
            x->setChannelProperty("source", source);
 
102
            x->setChannelProperty("encoding", encoding );
 
103
 
 
104
            kdDebug() << "IOFormatCSV::load(...), adding '" << x->name() << "'" << endl;
 
105
            store->addChannel(x);
 
106
            foundOne = true;
 
107
        }
 
108
    }
 
109
 
 
110
    return foundOne;
 
111
}
 
112
    
 
113
 
 
114
extern "C" {
 
115
        ChannelIOFormatCSV* create_csvchannels(Kdetv *ktv)
 
116
    {
 
117
                return new ChannelIOFormatCSV(ktv, 0, "CSV Channels Plugin");
 
118
        }
 
119
}
 
120