5
* Copyright (C) 2002 Richard Moore <rich@kde.org>
6
* Copyright (C) 2002 George Staikos <staikos@kde.org>
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Library General Public
10
* License as published by the Free Software Foundation; either
11
* version 2 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Library General Public License for more details.
18
* You should have received a copy of the GNU Library General Public License
19
* along with this library; see the file COPYING.LIB. If not, write to
20
* the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
21
* Boston, MA 02110-1301, USA.
26
#include <qstringlist.h>
27
#include <qtextstream.h>
29
#include <ktempfile.h>
31
#include <ksimpleconfig.h>
34
#include "channelstore.h"
36
#include "channeliokwintv2.h"
39
// KWinTV2 Format Handler
42
ChannelIOFormatKWinTV2::ChannelIOFormatKWinTV2(Kdetv *ktv, QObject *parent, const char* name)
43
: KdetvChannelPlugin(ktv, "KWinTV Channels", parent, name)
46
_menuName = i18n("KWinTV2");
47
_flags = FormatRead|FormatWrite;
51
bool ChannelIOFormatKWinTV2::readKConfigFormat(ChannelStore *store, QIODevice *file)
58
tf.setAutoDelete(false);
60
// Copy into a temp file
61
QString tmpnam = tf.name();
62
ts = new QTextStream( file );
63
QTextStream *ots = tf.textStream();
65
while (!ts->atEnd()) {
66
QString l = ts->readLine();
72
// Try to read it as a KConfig file
73
KSimpleConfig *cfg = new KSimpleConfig(tmpnam, true);
74
QStringList groups = cfg->groupList();
75
for (QStringList::ConstIterator i = groups.constBegin();
76
i != groups.constEnd();
79
Channel *chan = new Channel( store );
80
chan->setChannelProperty("frequency", (Q_ULLONG)cfg->readNumEntry("Frequency")*1000/16);
81
chan->setNumber(cfg->readNumEntry("ChannelId"));
82
chan->setName(cfg->readEntry("ChannelName"));
83
chan->setEnabled(cfg->readBoolEntry("Enabled", true));
86
* Dirk Ziegelmeier: this is a hack. To do it correctly,
87
* I would need the _encodings enumeration for the currently
88
* selected source. But it would be ugly to give the channel
89
* load/save function a pointer to kdetv from where we
90
* could get to the current video plugin...
91
* No. That's not worth it. I hacked TV norm to work,
92
* source has to stand back.
94
int norm = cfg->readNumEntry("Enabled", 0);
96
chan->setChannelProperty("encoding", QString("pal"));
97
} else if (norm == 1) {
98
chan->setChannelProperty("encoding", QString("ntsc"));
99
} else if (norm == 2) {
100
chan->setChannelProperty("encoding", QString("secam"));
101
} else if (norm == 3) {
102
chan->setChannelProperty("encoding", QString("auto"));
105
store->addChannel(chan);
106
kdDebug() << "IOFormatKWinTV2: found channel "
107
<< chan->name() << " " << chan->number()
108
<< " " << (unsigned int)chan->getChannelProperty("frequency").toULongLong() << endl;
113
// FIXME - delete the tmp file
114
QFile::remove(tmpnam);
116
return groups.count() > 0;
120
bool ChannelIOFormatKWinTV2::load( ChannelStore *store, ChannelFileMetaInfo *,
121
QIODevice *file, const QString& /*fmt*/ )
123
kdDebug() << "IOFormatKWinTV2::load(...)" << endl;
126
ts = new QTextStream( file );
128
if ( !readHeader() ) {
129
kdDebug() << "IOFormatKWinTV2::load() trying KConfig format instead" << endl;
131
return readKConfigFormat(store, file);
135
QString line = ts->readLine();
136
while( !line.isNull() ) {
138
chan = readChannel();
140
store->addChannel( chan );
144
line = ts->readLine();
150
return chan ? true : false;
153
bool ChannelIOFormatKWinTV2::readHeader()
155
QString line = ts->readLine();
156
if ( line != "### 4" ) {
157
kdWarning() << "IOFormatKWinTV2::readHeader() Expected '### 4' but found '"
158
<< line << "'" << endl;
162
for ( int i = 0; i < 2; i++ ) {
163
line = ts->readLine();
164
if ( !line.startsWith("#") ) {
165
kdWarning() << "IOFormatKWinTV2::readHeader() Parse error, line was '"
166
<< line << "'" << endl;
174
Channel *ChannelIOFormatKWinTV2::readChannel()
178
for ( int i = 0; i < 10; i++ ) {
179
QString line = ts->readLine();
183
fields.append( line );
186
QString s = readField( fields[1], "Frq" );
188
kdWarning() << "IOFormatKWinTV2::readChannel() Could not find 'Frq' field" << endl;
193
unsigned long freq = s.toULong( &ok );
197
QString n = readField( fields[7], "Norm" );
199
kdWarning() << "IOFormatKWinTV2::readChannel() Could not find 'Norm' field" << endl;
203
unsigned long norm = n.toULong( &ok );
205
Channel *ch = new Channel( store );
206
ch->setChannelProperty("frequency", (Q_ULLONG)(freq*1000/16));
208
QString c = readField( fields[0], "Chnl" );
213
/* read my remarks at readKConfigFormat() */
215
ch->setChannelProperty("encoding", QString("pal"));
216
} else if (norm == 1) {
217
ch->setChannelProperty("encoding", QString("ntsc"));
218
} else if (norm == 2) {
219
ch->setChannelProperty("encoding", QString("secam"));
220
} else if (norm == 3) {
221
ch->setChannelProperty("encoding", QString("auto"));
227
QString ChannelIOFormatKWinTV2::readField( const QString &line, const QString &field )
229
QStringList sl = QStringList::split( ": ", line );
230
if ( sl[0] != field )
231
return QString::null;
236
bool ChannelIOFormatKWinTV2::save( ChannelStore *store, ChannelFileMetaInfo *,
237
QIODevice *file, const QString&)
239
kdDebug() << "IOFormatKWinTV2::save() called" << endl;
242
QString tn = tf.name();
246
KSimpleConfig *cfg = new KSimpleConfig(tn, false);
248
for (uint i = 0; i < store->count(); i++) {
249
Channel *c = store->channelAt(i);
250
cfg->setGroup(QString("channel %1").arg(c->number()));
251
cfg->writeEntry("ChannelId", c->number());
252
cfg->writeEntry("ChannelName", c->name());
253
cfg->writeEntry("Frequency", c->getChannelProperty("frequency").toULongLong()*16/1000);
254
cfg->writeEntry("Enabled", c->enabled());
256
/* read my remarks at readKConfigFormat() */
257
if (c->getChannelProperty("encoding").toString() == "pal") {
258
cfg->writeEntry("Norm", 0);
259
} else if (c->getChannelProperty("encoding").toString() == "ntsc") {
260
cfg->writeEntry("Norm", 1);
261
} else if (c->getChannelProperty("encoding").toString() == "secam") {
262
cfg->writeEntry("Norm", 2);
263
} else if (c->getChannelProperty("encoding").toString() == "auto") {
264
cfg->writeEntry("Norm", 3);
270
ts = new QTextStream( file );
273
if (!qf.open(IO_ReadOnly)) {
278
QTextStream *itf = new QTextStream(&qf);
279
while (!itf->atEnd()) {
280
QString l = itf->readLine();
294
ChannelIOFormatKWinTV2* create_kwintvchannels(Kdetv *ktv)
296
return new ChannelIOFormatKWinTV2(ktv, 0L, "KWinTV Channel Plugin");