5
* Copyright (C) 2002 Richard Moore <rich@kde.org>
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.
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.
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.
25
#include <qstringlist.h>
26
#include <qtextstream.h>
31
#include "channelstore.h"
33
#include "channeliocsv.h"
39
ChannelIOFormatCSV::ChannelIOFormatCSV(Kdetv *ktv, QObject *parent, const char *name)
40
: KdetvChannelPlugin(ktv, "CSV Channels", parent, name)
43
_menuName = i18n("CSV");
44
_flags = FormatRead|FormatWrite;
47
bool ChannelIOFormatCSV::save( ChannelStore *store, ChannelFileMetaInfo *,
48
QIODevice *file, const QString&/*fmt*/ )
50
kdDebug() << "IOFormatCSV::save(...)" << endl;
52
kdDebug() << "IOFormatCSV::save(...) channel count=" << store->count() << endl;
55
QTextStream ts( file );
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;
61
ts << j->number() << ","
62
<< (long)j->getChannelProperty("frequency").toULongLong() << ","
64
<< j->getChannelProperty("encoding").toString() << ","
65
<< j->getChannelProperty("source").toString() << endl;
68
kdDebug() << "IOFormatCSV::save(...) done" << endl;
72
bool ChannelIOFormatCSV::load( ChannelStore *store, ChannelFileMetaInfo *,
73
QIODevice *file, const QString&/*fmt*/ )
75
kdDebug() << "IOFormatCSV::load(...)" << endl;
78
QString rawch( file->readAll() );
81
QStringList lines = QStringList::split("\n", rawch);
85
bool foundOne = false;
86
for ( QStringList::ConstIterator i = lines.constBegin(); i != lines.constEnd(); ++i ) {
87
QStringList ch = QStringList::split( ",", *i );
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;
96
if ( numok && freqok ) {
97
Channel *x = new Channel( store );
100
x->setChannelProperty("frequency", (unsigned int)freq);
101
x->setChannelProperty("source", source);
102
x->setChannelProperty("encoding", encoding );
104
kdDebug() << "IOFormatCSV::load(...), adding '" << x->name() << "'" << endl;
105
store->addChannel(x);
115
ChannelIOFormatCSV* create_csvchannels(Kdetv *ktv)
117
return new ChannelIOFormatCSV(ktv, 0, "CSV Channels Plugin");