~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to noatun/noatun/modules/tron/mcp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// mcp.cpp
 
2
//
 
3
// Copyright (C) 2000, 2001 Neil Stevens <multivac@fcmail.com>
 
4
//
 
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
6
// of this software and associated documentation files (the "Software"), to deal
 
7
// in the Software without restriction, including without limitation the rights
 
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the Software is
 
10
// furnished to do so, subject to the following conditions:
 
11
// 
 
12
// The above copyright notice and this permission notice shall be included in
 
13
// all copies or substantial portions of the Software.
 
14
// 
 
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 
18
// THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
19
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
20
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
21
// 
 
22
// Except as contained in this notice, the name(s) of the author(s) shall not be
 
23
// used in advertising or otherwise to promote the sale, use or other dealings
 
24
// in this Software without prior written authorization from the author(s).
 
25
 
 
26
#ifdef HAVE_CONFIG_H
 
27
#include "config.h"
 
28
#endif
 
29
 
 
30
extern "C"
 
31
{
 
32
#include <sys/stat.h>
 
33
#include <unistd.h>
 
34
}
 
35
 
 
36
#include <kdebug.h>
 
37
#include <kio/global.h>
 
38
#include <kio/job.h>
 
39
#include <kio/netaccess.h>
 
40
#include <klocale.h>
 
41
#include <kmessagebox.h>
 
42
#include <krandomsequence.h>
 
43
#include <ktempfile.h>
 
44
#include <qfile.h>
 
45
#include <qtextstream.h>
 
46
 
 
47
#include "ksaver.h"
 
48
#include "mcp.h"
 
49
#include "noatunapp.h"
 
50
#include "player.h"
 
51
 
 
52
/**
 
53
 * class MCPItem is closely coupled with class MCP.  It makes sure that,
 
54
 * at all times, every PlaylistItem maintains a one-to-one relationship
 
55
 * with a QDomElement from MCP.
 
56
 *
 
57
 * Additionally, it makes sure that the QDomElement is kept updated with
 
58
 * the PlaylistItem information.
 
59
 */
 
60
class MCPItem : public PlaylistItem
 
61
{
 
62
public:
 
63
        /**
 
64
         * Create a new item, and create an element for it
 
65
         */
 
66
        MCPItem(QDomDocument, const KURL & = 0, bool = true);
 
67
 
 
68
        /**
 
69
         * Create an item for an existing element, as from a playlist file
 
70
         */
 
71
        MCPItem(QDomElement);
 
72
 
 
73
        virtual ~MCPItem();
 
74
 
 
75
        inline QDomElement element(void) const {return node;};
 
76
        static inline KURL getURL(const QString &path);
 
77
 
 
78
protected:
 
79
        virtual void modified(void);
 
80
 
 
81
private:
 
82
        void setPointer(void);
 
83
 
 
84
        void update(void);
 
85
 
 
86
        QDomElement node;
 
87
};
 
88
 
 
89
MCPItem::MCPItem(QDomElement n)
 
90
        : PlaylistItem(getURL(n.attribute("path")), n.attribute("download") == "true")
 
91
{
 
92
        node = n;
 
93
        setProperty("title", node.attribute("title"), false);
 
94
        setProperty("artist", node.attribute("artist"), false);
 
95
        setProperty("album", node.attribute("album"), false);
 
96
        setProperty("date", node.attribute("date"), false);
 
97
        setProperty("comment", node.attribute("comment"), false);
 
98
        mLength = node.attribute("length").toInt();
 
99
        // Keep compatible, despite new ms system
 
100
        if(mLength != -1) mLength *= 1000;
 
101
        setPointer();
 
102
}
 
103
 
 
104
MCPItem::MCPItem(QDomDocument d, const KURL &url, bool download)
 
105
        : PlaylistItem(url, download)
 
106
{
 
107
        node = d.createElement("item");
 
108
        setPointer();
 
109
        update();
 
110
}
 
111
 
 
112
MCPItem::~MCPItem()
 
113
{
 
114
        node.removeAttribute("MCPItem");
 
115
}
 
116
 
 
117
void MCPItem::modified(void)
 
118
{
 
119
        update();
 
120
        static_cast<MCP *>(napp->playlist())->itemModified(this);
 
121
}
 
122
 
 
123
void MCPItem::setPointer(void)
 
124
{
 
125
        node.setAttribute("MCPItem", QString::number((long)this));
 
126
}
 
127
 
 
128
void MCPItem::update(void)
 
129
{
 
130
        node.setAttribute("title", property("title"));
 
131
        node.setAttribute("artist", property("artist"));
 
132
        node.setAttribute("album", property("album"));
 
133
        node.setAttribute("date", property("date"));
 
134
        node.setAttribute("comment", property("comment"));
 
135
        node.setAttribute("path", mUrl.path());
 
136
        node.setAttribute("download", mDownloaded ? "true" : "false");
 
137
        // Keep compatible, despite new ms system
 
138
        int length = mLength;
 
139
        if(length != -1) length /= 1000;
 
140
        node.setAttribute("length", QString::number(length));
 
141
}
 
142
 
 
143
KURL MCPItem::getURL(const QString &path)
 
144
{
 
145
        KURL result(path);
 
146
        if (result.isMalformed() || result.protocol() == "file")
 
147
        {
 
148
                result.setProtocol("file");
 
149
                result.setPath(path);
 
150
        }
 
151
        return result;
 
152
}
 
153
 
 
154
inline MCPItem *getItemForElement(QDomElement element)
 
155
{
 
156
        return (MCPItem *)element.attribute("MCPItem").toLong();
 
157
}
 
158
 
 
159
/////////
 
160
// MCP //
 
161
/////////
 
162
 
 
163
MCP::MCP(QObject *parent, const char *name)
 
164
        : Playlist(parent, name)
 
165
        , shuffle(false)
 
166
        , applyVolume(false)
 
167
{
 
168
        clear();
 
169
        shuffleUnused.setAutoDelete(false);
 
170
        connect(this, SIGNAL(current(PlaylistItem *)), this, SLOT(setVolume(PlaylistItem *)));
 
171
 
 
172
        // I just checked.  napp->player() should exist at this point.
 
173
        connect(napp->player(), SIGNAL(volumeChanged(int)), this, SLOT(getVolume(int)));
 
174
}
 
175
 
 
176
MCP::~MCP()
 
177
{
 
178
        // Only You can help prevent memory leaks
 
179
        clearInternal();
 
180
}
 
181
 
 
182
void MCP::reset(void)
 
183
{
 
184
        if(shuffle)
 
185
        {
 
186
                setShuffle(true);
 
187
        }
 
188
        else
 
189
        {
 
190
                cur = doc.documentElement().namedItem("item").toElement();
 
191
                emit current(getItemForElement(cur));
 
192
        }
 
193
}
 
194
 
 
195
void MCP::clear(void)
 
196
{
 
197
        clearInternal();
 
198
        emit cleared();
 
199
}
 
200
 
 
201
void MCP::clearInternal(void)
 
202
{
 
203
        QDomElement element = doc.documentElement().namedItem("item").toElement();
 
204
 
 
205
        while(!element.isNull())
 
206
        {
 
207
                MCPItem *item = getItemForElement(element);
 
208
                delete item;
 
209
                element = element.nextSibling().toElement();
 
210
        }
 
211
 
 
212
        doc.clear();
 
213
        doc.setContent(QString("<!DOCTYPE NoatunPlaylist><playlist/>"));
 
214
 
 
215
        if(napp->player())
 
216
                napp->player()->stop();
 
217
 
 
218
        if(shuffle)
 
219
                shuffleUnused.clear();
 
220
}
 
221
 
 
222
const MCPItem *MCP::AtBottom = (MCPItem *)-1;
 
223
 
 
224
MCPItem *MCP::addFileG(const KURL &_url, bool _play, MCPItem *_afterThis)
 
225
{
 
226
        MCPItem *item = new MCPItem(doc, _url);
 
227
 
 
228
        if(_afterThis == AtBottom)
 
229
                doc.documentElement().appendChild(item->element());
 
230
        else
 
231
                moveAfter(item, _afterThis);
 
232
 
 
233
        if(_play) play(item);
 
234
 
 
235
        if(shuffle)
 
236
        {
 
237
                shuffleUnused.append(item);
 
238
                KRandomSequence rand;
 
239
                rand.randomize(&shuffleUnused);
 
240
        }
 
241
 
 
242
        return item;
 
243
}
 
244
 
 
245
void MCP::addFile(const KURL &_url, bool _play)
 
246
{
 
247
        (void)addFileG(_url, _play);
 
248
}
 
249
 
 
250
PlaylistItem *MCP::addFile(const KURL &_url, PlaylistItem *_afterThis)
 
251
{
 
252
        return addFileG(_url, false, static_cast<MCPItem *>(_afterThis));
 
253
}
 
254
 
 
255
PlaylistItem *MCP::next(void)
 
256
{
 
257
        if(shuffle)
 
258
        {
 
259
                PlaylistItem *item = shuffleUnused.take();
 
260
                if(!item)
 
261
                {
 
262
                        setShuffle(true);
 
263
                        item = shuffleUnused.take();
 
264
                }
 
265
 
 
266
                if(item)
 
267
                        cur = static_cast<MCPItem *>(item)->element();
 
268
                else
 
269
                        cur = QDomElement();
 
270
 
 
271
                return item;
 
272
        }
 
273
        else
 
274
        {
 
275
                QDomElement element = cur.nextSibling().toElement();
 
276
                if(element.isNull())
 
277
                {
 
278
                        return 0;
 
279
                }
 
280
                else
 
281
                {
 
282
                        cur = element;
 
283
                        emit current(getItemForElement(cur));
 
284
                        return getItemForElement(cur);
 
285
                }
 
286
        }
 
287
}
 
288
 
 
289
PlaylistItem *MCP::current(void)
 
290
{
 
291
        return getItemForElement(cur);
 
292
}
 
293
 
 
294
PlaylistItem *MCP::previous(void)
 
295
{
 
296
        if(shuffle)
 
297
                return next();
 
298
 
 
299
        QDomElement element = cur.previousSibling().toElement();
 
300
        if(element.isNull())
 
301
        {
 
302
                return 0;
 
303
        }
 
304
        else
 
305
        {
 
306
                cur = element;
 
307
                emit current(getItemForElement(cur));
 
308
                return getItemForElement(cur);
 
309
        }
 
310
}
 
311
 
 
312
PlaylistItem *MCP::getFirst(void) const
 
313
{
 
314
        return getItemForElement(doc.documentElement().namedItem("item").toElement());
 
315
}
 
316
 
 
317
PlaylistItem *MCP::getAfter(const PlaylistItem *_item) const
 
318
{
 
319
        const MCPItem *item = static_cast<const MCPItem *>(_item);
 
320
        QDomElement element = item->element();
 
321
        element = element.nextSibling().toElement();
 
322
        return getItemForElement(element);
 
323
}
 
324
 
 
325
PlaylistItem *MCP::getBefore(const PlaylistItem *_item) const
 
326
{
 
327
        const MCPItem *item = static_cast<const MCPItem *>(_item);
 
328
        QDomElement element = item->element();
 
329
        element = element.previousSibling().toElement();
 
330
        return getItemForElement(element);
 
331
}
 
332
 
 
333
bool MCP::listVisible(void) const
 
334
{
 
335
        return !static_cast<QWidget *>(parent())->isHidden();
 
336
}
 
337
 
 
338
void MCP::setShuffle(bool b)
 
339
{
 
340
        shuffle = b;
 
341
        if(shuffle)
 
342
        {
 
343
                shuffleUnused.clear();
 
344
 
 
345
                QDomElement element = doc.documentElement().namedItem("item").toElement();
 
346
                while(!element.isNull())
 
347
                {
 
348
                        shuffleUnused.append(getItemForElement(element));
 
349
                        element = element.nextSibling().toElement();
 
350
                }
 
351
                KRandomSequence rand;
 
352
                rand.randomize(&shuffleUnused);
 
353
        }
 
354
}
 
355
 
 
356
void MCP::setApplyVolume(bool b)
 
357
{
 
358
        applyVolume = b;
 
359
        if(!cur.isNull()) setVolume(getItemForElement(cur));
 
360
}
 
361
 
 
362
void MCP::moveAfter(PlaylistItem *_item, PlaylistItem *_moveAfter)
 
363
{
 
364
        MCPItem *item = static_cast<MCPItem *>(_item);
 
365
        QDomElement element = item->element();
 
366
 
 
367
        MCPItem *moveAfter = static_cast<MCPItem *>(_moveAfter);
 
368
        if(moveAfter)
 
369
        {
 
370
                QDomElement after = moveAfter->element();
 
371
                after.parentNode().insertAfter(element, after);
 
372
                emit moved(_item, _moveAfter);
 
373
        }
 
374
        else
 
375
        {
 
376
                QDomNode parent = doc.documentElement();
 
377
                parent.insertBefore(element, parent.firstChild());
 
378
                emit moved(_item, 0);
 
379
        }
 
380
}
 
381
 
 
382
void MCP::play(PlaylistItem *_item)
 
383
{
 
384
        MCPItem *item = static_cast<MCPItem *>(_item);
 
385
        QDomElement element = item->element();
 
386
 
 
387
        cur = element;
 
388
        emit current(item);
 
389
        emit playCurrent();
 
390
}
 
391
 
 
392
void MCP::itemModified(PlaylistItem *item)
 
393
{
 
394
        emit modified(item);
 
395
}
 
396
 
 
397
void MCP::showList(void)
 
398
{
 
399
        if(!listVisible()) static_cast<QWidget *>(parent())->show();
 
400
        emit listShown();
 
401
}
 
402
 
 
403
void MCP::hideList(void)
 
404
{
 
405
        if(listVisible()) static_cast<QWidget *>(parent())->hide();
 
406
        emit listHidden();
 
407
}
 
408
 
 
409
void MCP::toggleList(void)
 
410
{
 
411
        QWidget *widget = static_cast<QWidget *>(parent());
 
412
        if(widget->isHidden())
 
413
        {
 
414
                widget->show();
 
415
                emit listShown();
 
416
        }
 
417
        else
 
418
        {
 
419
                widget->hide();
 
420
                emit listHidden();
 
421
        }
 
422
}
 
423
 
 
424
void MCP::remove(PlaylistItem *_item)
 
425
{
 
426
        MCPItem *item = static_cast<MCPItem *>(_item);
 
427
        QDomElement element = item->element();
 
428
 
 
429
        if(shuffle)
 
430
        {
 
431
                shuffleUnused.removeRef(item);
 
432
        }
 
433
 
 
434
        if(element == cur)
 
435
        {
 
436
                if(cur.nextSibling().isNull())
 
437
                        napp->player()->stop();
 
438
                else
 
439
                        previous();
 
440
        }
 
441
 
 
442
        delete item;
 
443
        element.parentNode().removeChild(element);
 
444
        element.clear();
 
445
 
 
446
        emit removed(item);
 
447
}
 
448
 
 
449
static inline void MCPError(QObject *obj, QString err)
 
450
{
 
451
        KMessageBox::error(static_cast<QWidget *>(obj), err);
 
452
}
 
453
 
 
454
void MCP::load(const KURL &url, bool errorFree)
 
455
{
 
456
        QString dest;
 
457
        if(!KIO::NetAccess::download(url, dest))
 
458
        {
 
459
                if(!errorFree)
 
460
                        MCPError(this, i18n("Failed to load %1.").arg(url.prettyURL()));
 
461
        }
 
462
        else
 
463
        {
 
464
                QFile file(dest);
 
465
                file.open(IO_ReadOnly);
 
466
                QTextStream stream(&file);
 
467
                QString data = stream.read();
 
468
 
 
469
                QDomDocument newDoc;
 
470
                if(!newDoc.setContent(data))
 
471
                {
 
472
                        MCPError(this, i18n("%1 is not a valid Noatun playlist.").arg(url.prettyURL()));
 
473
                }
 
474
                else
 
475
                {
 
476
                        clearInternal();
 
477
                        doc.setContent(data);
 
478
                        QDomElement element = doc.documentElement().namedItem("item").toElement();
 
479
                        while(!element.isNull())
 
480
                        {
 
481
                                (void)new MCPItem(element);
 
482
                                element = element.nextSibling().toElement();
 
483
                        }
 
484
                        emit loaded();
 
485
                        setShuffle(shuffle);
 
486
                }
 
487
                KIO::NetAccess::removeTempFile(dest);
 
488
        }
 
489
}
 
490
 
 
491
void MCP::save(const KURL &url)
 
492
{
 
493
        KSaver saver(url);
 
494
        if(!saver.open())
 
495
        {
 
496
                MCPError(this, i18n("Failed to save to %1.").arg(url.prettyURL()));
 
497
                return;
 
498
        }
 
499
 
 
500
        saver.textStream() << doc.toString();
 
501
 
 
502
        if(!saver.close())
 
503
        {
 
504
                MCPError(this, i18n("Failed to save to %1.").arg(url.prettyURL()));
 
505
                return;
 
506
        }
 
507
 
 
508
        emit saved();
 
509
}
 
510
 
 
511
PlaylistItem *MCP::importWindowsPlaylist(const KURL &url, PlaylistItem *_item)
 
512
{
 
513
        MCPItem *item = static_cast<MCPItem *>(_item);
 
514
 
 
515
        QString dest;
 
516
        if(!KIO::NetAccess::download(url, dest))
 
517
        {
 
518
                MCPError(this, i18n("Failed to load %1.").arg(url.prettyURL()));
 
519
        }
 
520
        else
 
521
        {
 
522
                QFile file(dest);
 
523
                file.open(IO_ReadOnly);
 
524
                QTextStream stream(&file);
 
525
                QString data = stream.readLine();
 
526
                while(!data.isNull())
 
527
                {
 
528
                        // happy Moose feature
 
529
                        if(KURL::isRelativeURL(data)) data = url.path(1) + data;
 
530
                        item = addFileG( MCPItem::getURL(data), false, item);
 
531
                        data = stream.readLine();
 
532
                }
 
533
                KIO::NetAccess::removeTempFile(dest);
 
534
        }
 
535
 
 
536
        return item;
 
537
}
 
538
 
 
539
PlaylistItem *MCP::importPlaylist(const KURL &url, PlaylistItem *_item)
 
540
{
 
541
        MCPItem *item = static_cast<MCPItem *>(_item);
 
542
 
 
543
        QString dest;
 
544
        if(!KIO::NetAccess::download(url, dest))
 
545
        {
 
546
                MCPError(this, i18n("Failed to load %1.").arg(url.prettyURL()));
 
547
        }
 
548
        else
 
549
        {
 
550
                QFile file(dest);
 
551
                file.open(IO_ReadOnly);
 
552
                QTextStream stream(&file);
 
553
                QString data = stream.read();
 
554
 
 
555
                QDomDocument list;
 
556
                list.setContent(data);
 
557
 
 
558
                QDomElement cur = list.documentElement().namedItem("item").toElement();
 
559
 
 
560
                while(!cur.isNull())
 
561
                {
 
562
                        QDomElement element = doc.createElement("item");
 
563
                        QDomNamedNodeMap attrs = cur.attributes();
 
564
 
 
565
                        for(unsigned i = 0; i < attrs.length(); i++)
 
566
                        {
 
567
                                QDomAttr attr = attrs.item(i).toAttr();
 
568
                                element.setAttribute( attr.name(), attr.value() );
 
569
                        }
 
570
                                
 
571
                        MCPItem *item = new MCPItem(element);
 
572
                        moveAfter(item, _item);
 
573
                        emit modified(item);
 
574
                        _item = item;
 
575
 
 
576
                        cur = cur.nextSibling().toElement();
 
577
                }
 
578
                        
 
579
                KIO::NetAccess::removeTempFile(dest);
 
580
        }
 
581
 
 
582
        return item;
 
583
}
 
584
 
 
585
PlaylistItem *MCP::addDirectory(const KURL &_url, PlaylistItem *_item)
 
586
{
 
587
        listLastAdded = _item;
 
588
        listDone = false;
 
589
 
 
590
        KIO::ListJob *job = KIO::listRecursive(_url, false);
 
591
        connect(job, SIGNAL(result(KIO::Job *)), this, SLOT(listFinished(KIO::Job *)));
 
592
        connect(job, SIGNAL(entries(KIO::Job *, const KIO::UDSEntryList &)), this, SLOT(listEntries(KIO::Job *, const KIO::UDSEntryList &)));
 
593
 
 
594
        while(!listDone) napp->processEvents();
 
595
 
 
596
        return listLastAdded;
 
597
}
 
598
 
 
599
void MCP::listFinished(KIO::Job *)
 
600
{
 
601
        listDone = true;
 
602
}
 
603
 
 
604
void MCP::listEntries(KIO::Job *job, const KIO::UDSEntryList &list)
 
605
{
 
606
        for(KIO::UDSEntryListConstIterator i = list.begin(); i != list.end(); ++i)
 
607
        {
 
608
                QValueList<KIO::UDSAtom>::ConstIterator j;
 
609
                for(j = (*i).begin(); (*j).m_uds != KIO::UDS_FILE_TYPE && j != (*i).end(); ++j);
 
610
 
 
611
                if( j != (*i).end() && S_ISREG((*j).m_long) )
 
612
                {
 
613
                        for(j = (*i).begin(); (*j).m_uds != KIO::UDS_NAME && j != (*i).end(); ++j);
 
614
                        if(j != (*i).end())
 
615
                        {
 
616
                                KURL url = static_cast<KIO::ListJob *>(job)->url();
 
617
                                url.addPath((*j).m_str);
 
618
                                listLastAdded = addFile(url, listLastAdded);
 
619
                        }
 
620
                }
 
621
        }
 
622
}
 
623
 
 
624
void MCP::setVolume(PlaylistItem *_item)
 
625
{
 
626
        if(!applyVolume) return;
 
627
 
 
628
        MCPItem *item = static_cast<MCPItem *>(_item);
 
629
        QDomElement node = item->element();
 
630
        if(node.hasAttribute("volume"))
 
631
                napp->player()->setVolume(node.attribute("volume").toInt());
 
632
        else
 
633
                getVolume(napp->player()->volume());
 
634
}
 
635
 
 
636
void MCP::getVolume(int _volume)
 
637
{
 
638
        cur.setAttribute("volume", QString::number(_volume));
 
639
}
 
640
 
 
641
#include "mcp.moc"