summaryrefslogtreecommitdiffstats
path: root/kaffeine/src/player-parts/kaffeine-part/playlistimport.cpp
blob: f8a85b85e14e85b59482c9bfc302ab3669bca006 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
 * playlistimport.cpp
 *
 * Copyright (C) 2004-2005 Jürgen Kofler <kaffeine@gmx.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <tqdom.h>
#include <tqregexp.h>
#include <tqxml.h>

#include <kdebug.h>
#include <tdeio/netaccess.h>

#include "playlistimport.h"

class MyXMLParser : public TQXmlDefaultHandler
{

public:
	TQValueList<MRL> mrls;
	bool isKaffeinePlaylist;


	MyXMLParser() : isKaffeinePlaylist(false)
	{}

	bool startElement(const TQString&, const TQString&,
	                  const TQString &qname, const TQXmlAttributes &att)
	{

		if (qname == "playlist")
			if (att.value("client") == "kaffeine")
			{
				isKaffeinePlaylist = true;
				return true;
			}
			else
			{
				return false;
			}

		if (qname != "entry") return true;

		TQStringList subs = TQStringList();
		int currentSub = -1;

		if ((!att.value("subs").isNull()) && (!att.value("subs").isEmpty()))
			subs =  TQStringList::split("&",att.value("subs"),false);
		if ((!att.value("subs").isNull()) && (!att.value("subs").isEmpty()))
		{
			bool ok;
			currentSub = att.value("currentSub").toInt(&ok);
			if (!ok)
				currentSub = -1;
		}

		// kdDebug() << "PlaylistImport: kaffeine import url: " << att.value("url") << endl;
		mrls.append(MRL(att.value("url"), att.value("title"), PlaylistImport::stringToTime(att.value("length")),
		                att.value("mime"), att.value("artist"), att.value("album"), att.value("track"),
		                att.value("year"), att.value("genre"), TQString(), subs, currentSub));
		return true;
	}

};

bool PlaylistImport::kaffeine(const TQString& playlist, TQValueList<MRL>& mrls)
{
	kdDebug() << "PlaylistImport: kaffeine: " << playlist << endl;
	TQFile file(playlist);
	if (!file.open(IO_ReadOnly)) return false;

	TQXmlInputSource source(TQT_TQIODEVICE(&file));
	TQXmlSimpleReader reader;

	MyXMLParser parser;
	reader.setContentHandler(&parser);
	reader.parse(source);
	file.close();

	if (!parser.isKaffeinePlaylist)
	{
		return false;
	}
	else
	{
		TQValueList<MRL>::ConstIterator end(parser.mrls.end());
		for (TQValueList<MRL>::ConstIterator it = parser.mrls.begin(); it != end; ++it)
			mrls.append(*it);
		return true;
	}
}

class NoatunXMLParser : public TQXmlDefaultHandler
{

public:
	TQValueList<MRL> mrls;
	bool isNoatunPlaylist;

	NoatunXMLParser(): isNoatunPlaylist(false)
	{}

	bool startElement(const TQString&, const TQString &,
	                  const TQString &qname, const TQXmlAttributes &att)
	{

		if (qname == "playlist")
			if (att.value("client") == "noatun")
			{
				isNoatunPlaylist = true;
				return true;
			}
			else
			{
				return false;
			}

		if (qname != "item") return true;

		TQString title = att.value("title");

		if (title.isNull())
			title = att.value("url");

		bool ok;
		TQTime length;
		int time = att.value("length").toInt(&ok);
		if ((ok) && (time > 0))
		{
			length = TQTime().addMSecs(time);
		}

		kdDebug() << "PlaylistImport: noatun import url: " << att.value("url") << endl;
		mrls.append(MRL(att.value("url"), title, length, TQString(), att.value("author"),
		                att.value("album"), att.value("track")));

		return true;
	}

};

bool PlaylistImport::noatun(const TQString& playlist, TQValueList<MRL>& mrls)
{
	kdDebug() << "PlaylistImport: noatun: " << playlist << endl;
	TQFile file(playlist);
	if (!file.open(IO_ReadOnly)) return false;

	TQXmlInputSource source(TQT_TQIODEVICE(&file));
	TQXmlSimpleReader reader;

	NoatunXMLParser parser;
	reader.setContentHandler(&parser);
	reader.parse(source);
	file.close();

	if (!parser.isNoatunPlaylist)
	{
		return false;
	}
	else
	{
		TQValueList<MRL>::ConstIterator end(parser.mrls.end());
		for (TQValueList<MRL>::ConstIterator it = parser.mrls.begin(); it != end; ++it)
			mrls.append(*it);
		return true;
	}
}

bool PlaylistImport::m3u(const TQString& playlist , TQValueList<MRL>& mrls)
{
	kdDebug() << "PlaylistImport: m3u: " << playlist << endl;
	TQFile file(playlist);
	if (!file.open(IO_ReadOnly)) return false;
	TQTextStream stream(&file);

	//  if (stream.readLine().upper() != "#EXTM3U") return false;
	TQString url;
	int time;
	bool ok;
	TQTime length;
	TQString title;
	KURL kUrl;
	bool foundAnyUrl = false;
	KURL plurl(playlist);
	plurl.setFileName ("");

	while (!stream.eof())
	{
		url        = stream.readLine();
		time       = 0;
		title      = TQString();
		length     = TQTime();
		if (url.left(1) == "#")
		{
			if (url.left(7).upper() == "#EXTINF")
			{
				url  = url.remove(0,8);
				time = url.section(",", 0,0).toInt(&ok);
				if ((ok) && (time > 0))
				{
					length = TQTime().addSecs(time);
				}

				title = url.section(",",1,1);
				url = stream.readLine();
			}
			else
			{
				continue;
			}
		}
		url.replace ('\\', '/'); /* for windows styled urls */
		kUrl = KURL (plurl, url); /* maybe a relative url */
		if (kUrl.isValid())
		{
			kdDebug() << "PlaylistImport: m3u import url: " << kUrl.prettyURL() << endl;

			MRL mrl;
			if (kUrl.isLocalFile())
				mrl.setURL(kUrl.path());
			else
				mrl.setURL(kUrl.prettyURL());
			if (title.isNull())
				title = kUrl.fileName();
			mrl.setTitle(title);
			mrl.setLength(length);

			mrls.append(mrl);

			foundAnyUrl = true;
		}
		else
			kdDebug() << "PlaylistImport: M3U: Not valid: " << kUrl.prettyURL() << endl;
	}
	file.close();
	if (foundAnyUrl)
		return true;
	else
		return false;
}

bool PlaylistImport::pls(const TQString& playlist, TQValueList<MRL>& mrls)
{
	kdDebug() << "PlaylistImport: pls: " << playlist << endl;
	TQFile file(playlist);
	if (!file.open(IO_ReadOnly)) return false;

	TQTextStream stream(&file);

	//if (stream.readLine().upper() != "[PLAYLIST]") return false;

	// Better Handling of pls playlists - Taken from amaroK - amarok.kde.org
	// Counted number of "File#=" lines.
	uint entryCnt = 0;
	// Value of the "NumberOfEntries=#" line.
	uint numberOfEntries = 0;
	bool havePlaylistSection = false;
	TQString tmp;
	TQStringList lines;

	// set Regexp keywords, Be case insensitive
	const TQRegExp regExp_NumberOfEntries("^NumberOfEntries\\s*=\\s*\\d+$", false);
	const TQRegExp regExp_File("^File\\d+\\s*=", false);
	const TQRegExp regExp_Title("^Title\\d+\\s*=", false);
	const TQRegExp regExp_Length("^Length\\d+\\s*=\\s*-?\\d+$", false);
	const TQRegExp regExp_Version("^Version\\s*=\\s*\\d+$", false);
	const TQString section_playlist("[playlist]");

	/* Preprocess the input data.
	* Read the lines into a buffer; Cleanup the line strings;
	* Count the entries manually and read "NumberOfEntries".
	*/
	while (!stream.atEnd()) {
		tmp = stream.readLine();
		tmp = tmp.stripWhiteSpace();
		if (tmp.isEmpty())
			continue;
		lines.append(tmp);

		if (tmp == section_playlist) {
			havePlaylistSection = true;
			continue;
		}

		if (tmp.contains(regExp_File)) {
			entryCnt++;
			continue;
		}

		if (tmp.contains(regExp_NumberOfEntries)) {
			numberOfEntries = TQString(tmp.section('=', -1)).stripWhiteSpace().toUInt();
			continue;
		}
	}

	file.close();

	if (numberOfEntries != entryCnt) {
		kdError() << "PlaylistImport: Invalid \"NumberOfEntries\" value in .pls playlist.  "
			<< "NumberOfEntries=" << numberOfEntries << "  counted="
			<< entryCnt << endl;
		/* Corrupt file. The "NumberOfEntries" value is
		 * not correct. Fix it by setting it to the manually
		 * counted number and go on parsing.
		 */
		numberOfEntries = entryCnt;
	}
	// If we have no Entries, return
	if (!numberOfEntries)
		return true;

	int time;
	bool ok;
	uint index;
	bool inPlaylistSection = false;
	TQString* titles = new TQString[entryCnt];
	TQString* files = new TQString[entryCnt];
	TQTime* length = new TQTime[entryCnt];

	TQStringList::const_iterator i = lines.begin(), end = lines.end();
	for ( ; i != end; ++i) {
		if (!inPlaylistSection && havePlaylistSection) {
			/* The playlist begins with the "[playlist]" tag.
			 * Skip everything before this.
			 */
			if ((*i) == section_playlist)
				inPlaylistSection = true;
			continue;
		}
		if ((*i).contains(regExp_File)) {
			// Have a "File#=XYZ" line.
			index = extractIndex(*i);
			if (index > numberOfEntries || index == 0)
				continue;
			files[index-1] = TQString((*i).section('=', 1)).stripWhiteSpace();
			continue;
		}
		if ((*i).contains(regExp_Title)) {
			// Have a "Title#=XYZ" line.
			index = extractIndex(*i);
			if (index > numberOfEntries || index == 0)
				continue;
			titles[index-1] = TQString((*i).section('=', 1)).stripWhiteSpace();
			continue;
		}
		if ((*i).contains(regExp_Length)) {
			// Have a "Length#=XYZ" line.
			index = extractIndex(*i);
			if (index > numberOfEntries || index == 0)
				continue;
			tmp = TQString((*i).section('=', 1)).stripWhiteSpace();
			time = tmp.toInt(&ok);
			if ( !(ok) || !(time > 0) ) continue;
			length[index-1] = TQTime().addSecs(time);
				continue;
		}
		if ((*i).contains(regExp_NumberOfEntries)) {
			// Have the "NumberOfEntries=#" line.
			continue;
		}
		if ((*i).contains(regExp_Version)) {
			// Have the "Version=#" line.
			tmp = TQString((*i).section('=', 1)).stripWhiteSpace();
			// We only support Version=2
			if (tmp.toUInt(&ok) != 2)
			kdWarning() << "PlaylistImport: pls: Unsupported version." << endl;
			continue;
		}
		kdWarning() << "PlaylistImport: pls: Unrecognized line: \"" << *i << "\"" << endl;
	}

	for (uint i=0; i<entryCnt; i++)
	{
		if (files[i].isNull()) continue;
		kdDebug() << "PlaylistImport: pls import url: " << files[i] << endl;
		if (titles[i].isNull())
			titles[i] = files[i];
		mrls.append(MRL(files[i], titles[i], length[i]));
	}

	delete [] titles;
	delete [] files;
	delete [] length;

	return true;
}

// Helper for pls import
uint PlaylistImport::extractIndex( const TQString &str )
{
	/* Extract the index number out of a .pls line.
	 * Example:
	 *   extractIndex("File2=foobar") == 2
	 */
	bool ok = false;
	unsigned int ret;
	TQString tmp(str.section('=', 0, 0));
	tmp.remove(TQRegExp("^\\D*"));
	ret = tmp.stripWhiteSpace().toUInt(&ok);
	if (!ok)
		kdError() << "PlaylistImport: pls: Corrupt pls file, Error extracting index." << endl;
	return ret;
}

bool PlaylistImport::ram(const MRL& playlist, TQValueList<MRL>& mrls, TQWidget* parent)
{
	TQ_ULONG result;
	char buf[10];

	kdDebug() << "PlaylistImport: ram: " << playlist.url() << endl;
	memset( buf, 0, 10 );

	if (playlist.kurl().isLocalFile()) {
		TQFile file(playlist.kurl().path());
		if (!file.open(IO_ReadOnly)) {
			kdError() << "PlaylistImport: Can't open " << playlist.url() << endl;
			return false;
		}
		result = file.readBlock(buf, 4);
		file.close();
		if (result != 4) {
			kdError() << "PlaylistImport: Can't read " << playlist.url() << endl;
			return false;
		}
		if (buf[0]=='.' && buf[1]=='R' && buf[2]=='M' && buf[3]=='F') {
			kdDebug() << "PlaylistImport: Seems to be a real media file" << endl;
			return false;
		}
	}
	else if (!playlist.kurl().protocol().startsWith("http")) {
		kdError() << "PlaylistImport: ram: Download via " << playlist.kurl().protocol() << " protocol not supported." << endl;
		return false;
	}

	kdDebug() << "PlaylistImport: Seems to be a ram playlist!" << endl;

	TQString localFile, url;
	if (TDEIO::NetAccess::mimetype(playlist.kurl(), parent) == "application/vnd.rn-realmedia") {
		kdDebug() << "PlaylistImport: Seems to be a real media file" << endl;
		return false;
	}

	if (TDEIO::NetAccess::download(playlist.kurl(), localFile, parent))
	{
		TQFile plFile(localFile);
		if (!plFile.open(IO_ReadOnly)) return false;
		TQTextStream stream( &plFile );

		while (!stream.eof())
		{
			url = stream.readLine();

			if (url[0] == '#') continue; /* ignore comments */
			if (url == "--stop--") break; /* stop line */

			if ((url.left(7) == "rtsp://") || (url.left(6) == "pnm://") || (url.left(7) == "http://"))
			{
				kdDebug() << "PlaylistImport: ram import url: " << url << endl;
				mrls.append(MRL(url, url));
			}
		}
	}
	else {
		kdError() << "PlaylistImport: " << TDEIO::NetAccess::lastErrorString() << endl;
                return false;
        }

	return true;
}

/**********************************************************************************
 *                             load asx playlist                                  *
 *   spec: http://msdn.microsoft.com/library/en-us/wmplay/mmp_sdk/asxelement.asp  *
 **********************************************************************************/

bool PlaylistImport::asx(const TQString& playlist, TQValueList<MRL>& mrls)
{
	kdDebug() << "PlaylistImport: asx: " << playlist << endl;
	TQFile file(playlist);
	if (!file.open(IO_ReadOnly)) return false;

	TQDomDocument doc;
	TQString errorMsg;
	int errorLine, errorColumn;
	if (!doc.setContent(&file, &errorMsg, &errorLine, &errorColumn))
	{
		kdError() << "PlaylistImport: XML parse error: " << errorMsg
		          << " (line: " << errorLine << ", column: " << errorColumn << ")" << endl;
		return false;
	}

	TQDomElement root = doc.documentElement();

	TQString url;
	TQString title;
	TQString author;
	TQTime length;
	TQString duration;

	if (root.nodeName().lower() != "asx") return false;

	TQDomNode node = root.firstChild();
	TQDomNode subNode;
	TQDomElement element;

	while (!node.isNull())
	{
		url = TQString();
		title = TQString();
		author = TQString();
		length = TQTime();
		if (node.nodeName().lower() == "entry")
		{
			subNode = node.firstChild();
			while (!subNode.isNull())
			{
				if ((subNode.nodeName().lower() == "ref") && (subNode.isElement()) && (url.isNull()))
				{
					element = subNode.toElement();
					if (element.hasAttribute("href"))
						url = element.attribute("href");
					if (element.hasAttribute("HREF"))
						url = element.attribute("HREF");
					if (element.hasAttribute("Href"))
						url = element.attribute("Href");
					if (element.hasAttribute("HRef"))
						url = element.attribute("HRef");

				}

				if ((subNode.nodeName().lower() == "duration") && (subNode.isElement()))
				{
					duration = TQString();
					element = subNode.toElement();
					if (element.hasAttribute("value"))
						duration = element.attribute("value");
					if (element.hasAttribute("Value"))
						duration = element.attribute("Value");
					if (element.hasAttribute("VALUE"))
						duration = element.attribute("VALUE");

					if (!duration.isNull())
						length = PlaylistImport::stringToTime(duration);
				}

				if ((subNode.nodeName().lower() == "title") && (subNode.isElement()))
				{
					title = subNode.toElement().text();
				}
				if ((subNode.nodeName().lower() == "author") && (subNode.isElement()))
				{
					author = subNode.toElement().text();
				}

				/* possible nodes we ignore: ABSTRACT, BANNER, BASE, COPYRIGHT, ENDMARKER, MOREINFO, PARAM,
				 * PREVIEWDURATION, STARTMARKER, STARTTIME
				 */
				subNode = subNode.nextSibling();
			}

			if (!url.isNull())
			{
				if (title.isNull())
					title = url;
				kdDebug() << "PlaylistImport: asx import url: " << url << endl;
				mrls.append(MRL(url, title, length, TQString(), author));
			}
		}
		node = node.nextSibling();
	}

	file.close();

	return true;
}

/****************************************************************
 * Full SMIL support seems to be impossible at the moment...    *
 * spec: http://www.w3.org/TR/REC-smil/                         *
 ****************************************************************/

bool PlaylistImport::smil(const TQString& playlist, const MRL& baseMRL, TQValueList<MRL>& mrls)
{
	kdDebug() << "PlaylistImport: smil: " << playlist << endl;
	TQFile file(playlist);
	if (!file.open(IO_ReadOnly)) return false;

	TQDomDocument doc;
	doc.setContent(&file);
	TQDomElement root = doc.documentElement();

	if (root.nodeName().lower() != "smil") return false;

	bool anyURL = false;
	KURL kurl;
	TQString url;
	TQDomNodeList nodeList;
	TQDomNode node;
	TQDomElement element;

	//video sources...
	nodeList = doc.elementsByTagName("video");
	kdDebug() << "PlaylistImport: smil: " << nodeList.count() << " 'video' tags found" << endl;
	for (uint i = 0; i < nodeList.count(); i++)
	{
		node = nodeList.item(i);
		url = TQString();
		if ((node.nodeName().lower() == "video") && (node.isElement()))
		{
			element = node.toElement();
			if (element.hasAttribute("src"))
				url = element.attribute("src");
			if (element.hasAttribute("Src"))
				url = element.attribute("Src");
			if (element.hasAttribute("SRC"))
				url = element.attribute("SRC");
		}
		if (!url.isNull())
		{
			kurl = KURL(baseMRL.kurl(), url);
			kdDebug() << "PlaylistImport: smil: found video source: " << kurl.url() << endl;
			mrls.append(kurl);
			anyURL = true;
		}
	}

	//audio sources...
	nodeList = doc.elementsByTagName("audio");
	kdDebug() << "PlaylistImport: smil: " << nodeList.count() << " 'audio' tags found" << endl;
	for (uint i = 0; i < nodeList.count(); i++)
	{
		node = nodeList.item(i);
		url = TQString();
		if ((node.nodeName().lower() == "audio") && (node.isElement()))
		{
			element = node.toElement();
			if (element.hasAttribute("src"))
				url = element.attribute("src");
			if (element.hasAttribute("Src"))
				url = element.attribute("Src");
			if (element.hasAttribute("SRC"))
				url = element.attribute("SRC");
		}
		if (!url.isNull())
		{
			kurl = KURL(baseMRL.kurl(), url);
			kdDebug() << "PlaylistImport: smil: found audio source: " << kurl.url() << endl;
			mrls.append(kurl);
			anyURL = true;
		}
	}

	file.close();
	return anyURL;
}

TQTime PlaylistImport::stringToTime(const TQString& timeString)
{
	int sec = 0;
	bool ok = false;
	TQStringList tokens = TQStringList::split(':',timeString);

	sec += tokens[0].toInt(&ok)*3600; //hours
	sec += tokens[1].toInt(&ok)*60; //minutes
	sec += tokens[2].toInt(&ok); //secs

	if (ok)
		return TQTime().addSecs(sec);
	else
		return TQTime();
}