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

« back to all changes in this revision

Viewing changes to kdetv/plugins/channel/kwintv/channeliokwintv2.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
 * Copyright (C) 2002 George Staikos <staikos@kde.org>
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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.
 
22
 */
 
23
 
 
24
#include <klocale.h>
 
25
#include <qfile.h>
 
26
#include <qstringlist.h>
 
27
#include <qtextstream.h>
 
28
 
 
29
#include <ktempfile.h>
 
30
#include <kdebug.h>
 
31
#include <ksimpleconfig.h>
 
32
 
 
33
#include "channel.h"
 
34
#include "channelstore.h"
 
35
 
 
36
#include "channeliokwintv2.h"
 
37
 
 
38
//
 
39
// KWinTV2 Format Handler
 
40
//
 
41
 
 
42
ChannelIOFormatKWinTV2::ChannelIOFormatKWinTV2(Kdetv *ktv, QObject *parent, const char* name)
 
43
    : KdetvChannelPlugin(ktv, "KWinTV Channels", parent, name)
 
44
{
 
45
    _fmtName  = "ch";
 
46
    _menuName = i18n("KWinTV2");
 
47
    _flags    = FormatRead|FormatWrite;
 
48
}
 
49
 
 
50
 
 
51
bool ChannelIOFormatKWinTV2::readKConfigFormat(ChannelStore *store, QIODevice *file)
 
52
{
 
53
        if (!file->reset())
 
54
                return false;
 
55
 
 
56
        KTempFile tf;
 
57
 
 
58
        tf.setAutoDelete(false);
 
59
 
 
60
        // Copy into a temp file
 
61
        QString tmpnam = tf.name();
 
62
        ts = new QTextStream( file );
 
63
        QTextStream *ots = tf.textStream();
 
64
        
 
65
        while (!ts->atEnd()) {
 
66
                QString l = ts->readLine();
 
67
                (*ots) << l << endl;
 
68
        }
 
69
        
 
70
        tf.close();
 
71
 
 
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();
 
77
         ++i) {
 
78
                cfg->setGroup(*i);
 
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));
 
84
 
 
85
                /*
 
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.
 
93
                 */
 
94
                int norm = cfg->readNumEntry("Enabled", 0);
 
95
                if (norm == 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"));
 
103
                }      
 
104
 
 
105
                store->addChannel(chan);
 
106
                kdDebug() << "IOFormatKWinTV2: found channel "
 
107
                  << chan->name() << " " << chan->number() 
 
108
                          << " " << (unsigned int)chan->getChannelProperty("frequency").toULongLong() << endl;
 
109
        }
 
110
 
 
111
        delete cfg;
 
112
 
 
113
        // FIXME - delete the tmp file
 
114
        QFile::remove(tmpnam);
 
115
 
 
116
    return groups.count() > 0;
 
117
}
 
118
 
 
119
 
 
120
bool ChannelIOFormatKWinTV2::load( ChannelStore *store, ChannelFileMetaInfo *,
 
121
                                   QIODevice *file, const QString& /*fmt*/ )
 
122
{
 
123
    kdDebug() << "IOFormatKWinTV2::load(...)" << endl;
 
124
 
 
125
    chan = 0;
 
126
    ts = new QTextStream( file );
 
127
 
 
128
    if ( !readHeader() ) {
 
129
        kdDebug() << "IOFormatKWinTV2::load() trying KConfig format instead" << endl;
 
130
        delete ts;
 
131
        return readKConfigFormat(store, file);
 
132
    }
 
133
 
 
134
    this->store = store;
 
135
    QString line = ts->readLine();
 
136
    while( !line.isNull() ) {
 
137
        if ( line == "*" ) {
 
138
            chan = readChannel();
 
139
            if ( chan )
 
140
                store->addChannel( chan );
 
141
            else
 
142
                break;
 
143
        }
 
144
        line = ts->readLine();
 
145
    }
 
146
 
 
147
    delete ts;
 
148
    file->close();
 
149
 
 
150
    return chan ? true : false;
 
151
}
 
152
 
 
153
bool ChannelIOFormatKWinTV2::readHeader()
 
154
{
 
155
    QString line = ts->readLine();
 
156
    if ( line != "### 4" ) {
 
157
        kdWarning() << "IOFormatKWinTV2::readHeader() Expected '### 4' but found '" 
 
158
                    << line << "'" << endl;
 
159
        return false;
 
160
    }
 
161
 
 
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;
 
167
            return false;
 
168
        }
 
169
    }
 
170
 
 
171
    return true;
 
172
}
 
173
 
 
174
Channel *ChannelIOFormatKWinTV2::readChannel()
 
175
{
 
176
    QStringList fields;
 
177
 
 
178
    for ( int i = 0; i < 10; i++ ) {
 
179
        QString line = ts->readLine();
 
180
        if ( line.isNull() )
 
181
            return 0;
 
182
 
 
183
        fields.append( line );
 
184
    }
 
185
 
 
186
    QString s = readField( fields[1], "Frq" );
 
187
    if ( s.isNull() ) {
 
188
        kdWarning() << "IOFormatKWinTV2::readChannel() Could not find 'Frq' field" << endl;
 
189
        return 0;
 
190
    }
 
191
 
 
192
    bool ok;
 
193
    unsigned long freq = s.toULong( &ok );
 
194
    if ( !ok )
 
195
        return 0;
 
196
 
 
197
    QString n = readField( fields[7], "Norm" );
 
198
    if ( s.isNull() ) {
 
199
        kdWarning() << "IOFormatKWinTV2::readChannel() Could not find 'Norm' field" << endl;
 
200
        return 0;
 
201
    }
 
202
 
 
203
    unsigned long norm = n.toULong( &ok );
 
204
 
 
205
    Channel *ch = new Channel( store );
 
206
    ch->setChannelProperty("frequency", (Q_ULLONG)(freq*1000/16));
 
207
 
 
208
    QString c = readField( fields[0], "Chnl" );
 
209
    if ( !c.isNull() ) {
 
210
        ch->setName(c);
 
211
    }
 
212
 
 
213
    /* read my remarks at readKConfigFormat() */
 
214
    if (norm == 0) {
 
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"));
 
222
    }      
 
223
 
 
224
    return ch;
 
225
}
 
226
 
 
227
QString ChannelIOFormatKWinTV2::readField( const QString &line, const QString &field )
 
228
{
 
229
    QStringList sl = QStringList::split( ": ", line );
 
230
    if ( sl[0] != field )
 
231
        return QString::null;
 
232
    else
 
233
        return sl[1];
 
234
}
 
235
 
 
236
bool ChannelIOFormatKWinTV2::save( ChannelStore *store, ChannelFileMetaInfo *,
 
237
                                   QIODevice *file, const QString&)
 
238
{
 
239
    kdDebug() << "IOFormatKWinTV2::save() called" << endl;
 
240
        KTempFile tf;
 
241
 
 
242
        QString tn = tf.name();
 
243
        tf.close();
 
244
        this->store = store;
 
245
 
 
246
        KSimpleConfig *cfg = new KSimpleConfig(tn, false);
 
247
 
 
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());
 
255
 
 
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);
 
265
                }
 
266
        }
 
267
 
 
268
        delete cfg;
 
269
 
 
270
        ts = new QTextStream( file );
 
271
 
 
272
        QFile qf(tn);
 
273
        if (!qf.open(IO_ReadOnly)) {
 
274
                QFile::remove(tn);
 
275
                return false;
 
276
        }
 
277
 
 
278
        QTextStream *itf = new QTextStream(&qf);
 
279
        while (!itf->atEnd()) {
 
280
                QString l = itf->readLine();
 
281
                (*ts) << l << endl;
 
282
        }
 
283
 
 
284
        delete itf;
 
285
        qf.close();
 
286
 
 
287
        QFile::remove(tn);
 
288
 
 
289
    return true;
 
290
}
 
291
 
 
292
 
 
293
extern "C" {
 
294
        ChannelIOFormatKWinTV2* create_kwintvchannels(Kdetv *ktv)
 
295
    {
 
296
                return new ChannelIOFormatKWinTV2(ktv, 0L, "KWinTV Channel Plugin");
 
297
        }
 
298
}
 
299