[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdemultimedia/noatun/library
From:       Stefan Gehn <mETz81 () web ! de>
Date:       2007-02-14 17:05:02
Message-ID: 1171472702.748989.16927.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 633637 by metz:

- Use KSaveFile for saving local playlists, thanks to dfaure for the hint
- Add const where applicable, remove dead code
- Add license/copyright headers


 M  +33 -1     noatun/playlistsaver.h  
 M  +120 -73   playlistsaver.cpp  


--- trunk/KDE/kdemultimedia/noatun/library/noatun/playlistsaver.h #633636:633637
@@ -1,3 +1,29 @@
+/* This file is part of Noatun
+
+  Copyright 2000-2006 by Charles Samuels <charles@kde.org>
+  Copyright 2003-2007 by Stefan Gehn <mETz81@web.de>
+
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions
+  are met:
+
+  1. Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+  2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
 #ifndef NOATUNPLAYLISTSAVER_H
 #define NOATUNPLAYLISTSAVER_H
 
@@ -8,6 +34,8 @@
 #include <noatun/playlist.h>
 #include <noatun_export.h>
 
+class QFile;
+
 class NoatunXMLStructure;
 class MSASXStructure;
 
@@ -26,7 +54,9 @@
  * </ul>
  *
  * @see PlaylistType
- *
+ * @todo Support XSPF playlists (http://www.xspf.org/)
+ * @todo Support any useful/sane format found on \
http://gonze.com/playlists/playlist-format-survey.html + * @todo Clean up and use \
                mimetypes for determining what format to load/save
  **/
 class NOATUN_EXPORT PlaylistSaver
 {
@@ -93,6 +123,8 @@
 	virtual void reset() {}
 
 private:
+	bool saveLocal(QFile &file, const PlaylistType opt);
+
 	/**
 	 * guess the list's content between M3U or
 	 * PLS, and give it to you
--- trunk/KDE/kdemultimedia/noatun/library/playlistsaver.cpp #633636:633637
@@ -1,6 +1,29 @@
-/**
- *
- **/
+/* This file is part of Noatun
+
+  Copyright 2000-2006 by Charles Samuels <charles@kde.org>
+  Copyright 2003-2007 by Stefan Gehn <mETz81@web.de>
+
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions
+  are met:
+
+  1. Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+  2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
 #include "noatun/playlistsaver.h"
 
 #include <qdom.h>
@@ -13,6 +36,7 @@
 #include <ksimpleconfig.h>
 #include <kmimetype.h>
 #include <klocale.h>
+#include <ksavefile.h>
 #include <ktemporaryfile.h>
 
 #include <kdebug.h>
@@ -40,44 +64,82 @@
 
 bool PlaylistSaver::save(const KUrl &url, const PlaylistType opt)
 {
-	//kDebug(66666) << k_funcinfo << "url:" << url << "; opt:" << opt << endl;
-	if(url.isEmpty() || !url.isValid() || (opt == PLS) || (opt == ASX) )
+	bool ret = false;
+
+	kDebug(66666) << k_funcinfo << "url:" << url << "; opt:" << opt << endl;
+
+	if (url.isEmpty() || !url.isValid())
 		return false;
 
-	bool ret = false;
-	KTemporaryFile tmpFile;
-	tmpFile.setSuffix(".noatunplaylist.xml");
-
-	if (tmpFile.open())
+	if ((opt == PLS) || (opt == ASX))
 	{
-		QTextStream *stream = new QTextStream(&tmpFile);
-		if (opt == M3U || opt == EXTM3U)
-			ret = saveM3U(*stream, opt);
-		else  // It's a XMLPlaylist
-			ret = saveXML(*stream);
-		delete stream;
+		kWarning(66666) << "Saving PLS and ASX playlists is not supported" << endl;
+		return false;
+	}
 
-		if (ret) // saving worked
+	if (url.isLocalFile())
+	{ // local files
+		KSaveFile saveFile;
+		saveFile.setFileName(url.path());
+
+		if (saveFile.open())
 		{
-			ret = KIO::NetAccess::upload(tmpFile.fileName(), url, 0L);
+			if (saveLocal(saveFile, opt)) // saving worked
+			{
+				ret = saveFile.finalize();
+				//TODO: check if finalize worked and use saveFile.errorString();
+			}
+			else
+			{
+				saveFile.abort();
+			}
 		}
 		else
 		{
-			kWarning(66666) << "Couldn't save playlist to temporary file" << \
tmpFile.fileName() << endl; +			kWarning(66666) << "Couldn't open playlist file" << \
endl; +			//TODO: make use of saveFile.errorString();
 		}
 	}
 	else
-	{
-		kWarning(66666) << "Couldn't open temporary file " << tmpFile.fileName() << endl;
+	{ // remote files
+		KTemporaryFile tmpFile;
+		tmpFile.setSuffix(".noatunplaylist.xml");
+
+		if (tmpFile.open())
+		{
+			if (saveLocal(tmpFile, opt)) // saving worked
+			{
+				ret = KIO::NetAccess::upload(tmpFile.fileName(), url, 0L);
+			}
+		}
+		else
+		{
+			kWarning(66666) << "Couldn't open tempfile " <<
+				tmpFile.fileName() << endl;
+		}
 	}
 	return ret;
 }
 
 
+bool PlaylistSaver::saveLocal(QFile &file, const PlaylistType opt)
+{
+	QTextStream stream(&file);
+	if (opt == M3U || opt == EXTM3U)
+		return saveM3U(stream, opt);
+	else  // It's a XMLPlaylist
+		return saveXML(stream);
+}
+
+
+
 bool PlaylistSaver::load(const KUrl &url, const PlaylistType opt)
 {
 	bool res = false;
 	QString localTempFile;
+
+	kDebug(66666) << k_funcinfo << "url:" << url << "; opt:" << opt << endl;
+
 	if(!KIO::NetAccess::download(url, localTempFile, 0L))
 		return false;
 
@@ -145,32 +207,32 @@
 
 bool PlaylistSaver::saveXML(QTextStream &stream)
 {
+	const PlaylistItem &i = PlaylistItem();
 	QDomDocument doc("playlist");
-	doc.setContent(QString("<!DOCTYPE XMLPlaylist><playlist version=\"1.0\" \
                client=\"noatun\"/>"));
-	QDomElement docElem=doc.documentElement();
+	doc.setContent(QString("<!DOCTYPE XMLPlaylist><playlist version=\"2.0\" \
client=\"noatun\"/>")); +	QDomElement docElem = doc.documentElement();
 
 	reset();
-	PlaylistItem i;
-	QStringList props;
 
 	while ((i = writeItem()))
 	{
 		// write all properties
-		props = i.properties();
-		QDomElement elem=doc.createElement("item");
-		for (QStringList::Iterator pi(props.begin()); pi!=props.end(); ++pi)
+		const QStringList &props = i.properties();
+		QDomElement elem = doc.createElement("item");
+
+		foreach(const QString &propertyName, props)
 		{
-			QString val = i.property(*pi);
-			elem.setAttribute(*pi, val);
-			if ((*pi) == "url")
+			QString propertyValue = i.property(propertyName);
+			elem.setAttribute(propertyName, propertyValue);
+
+			/*if (propertyName == "url")
 			{
 				KUrl u(val);
 				if (u.isLocalFile())
 					elem.setAttribute("local", u.path());
-			}
+			}*/
 		}
 		docElem.appendChild(elem);
-		props.clear();
 	}
 
 	stream.setCodec(QTextCodec::codecForName("UTF-8"));
@@ -190,34 +252,27 @@
 	{
 	}
 
-	bool startElement(
-			const QString&, const QString &,
-			const QString &name, const QXmlAttributes &a
-					 )
+	bool startElement(const QString&, const QString &, const QString &name,
+		const QXmlAttributes &attr)
 	{
 		if (fresh)
 		{
-			if (name=="playlist")
-			{
-				fresh=false;
-				return true;
-			}
-			else
-			{
-				return false;
-			}
+			fresh = (name != "playlist");
+			// if we are still marked as "fresh" then the xml file did not start
+			// with a playlist-tag and is thus INVALID.
+			return (!fresh);
 		}
 
-		if (name != "item")
-			return true;
-
-		Noatun::PropertyMap propMap;
-
-		for (int i=0; i<a.count(); i++)
+		if (name == "item")
 		{
-			propMap[a.qName(i)] = a.value(i);
+			Noatun::PropertyMap propMap;
+			int attrCount = attr.count();
+			for (int i = 0; i < attrCount; i++)
+			{
+				propMap[attr.qName(i)] = attr.value(i);
+			}
+			saver->readItem(propMap);
 		}
-		saver->readItem(propMap);
 
 		return true;
 	}
@@ -245,17 +300,10 @@
 	{
 		if (fresh)
 		{
-			if (name.toLower() == "asx")
-			{
- 				//kDebug(66666) << "found ASX format" << endl;
-				fresh=false;
-				return true;
-			}
-			else
-			{
-				kDebug(66666) << "This is NOT an ASX style playlist!" << endl;
-				return false;
-			}
+			fresh = (name.toLower() != "asx");
+			// if we are still marked as "fresh" then the xml file did not start
+			// with an asx-tag and is thus INVALID.
+			return (!fresh);
 		}
 
 		if (name.toLower() == "entry")
@@ -265,7 +313,6 @@
 				kDebug(66666) << "STOP, ENTRY INSIDE ENTRY!" << endl;
 				return false;
 			}
-//			kDebug(66666) << "<ENTRY> =====================" << endl;
 			inEntry=true;
 		}
 		else
@@ -301,8 +348,6 @@
 										url.setPath("/");
 									propMap["url"] = url.url();
 									propMap["stream_"]=propMap["url"];
-//									readItem(propMap);
-//									continue;
 								}
 							}
 							else
@@ -320,7 +365,6 @@
 								}
 								propMap["url"]=u1.url();
 							}
-//							kDebug(66666) << "adding property url, value='" << propMap["url"] << "'" << \
endl;  }
 					}
 				}
@@ -336,14 +380,12 @@
 							keyName="author";
 						else if(!keyName.isEmpty()) // successfully found a key, the next key=value \
pair has to be the value  {
-//							kDebug(66666) << "keyName=" << keyName << ", next value is '" << a.value(i) \
<< "'" << endl;  keyValue=a.value(i);
 						}
 					}
 
 					if (!keyName.isEmpty() && !keyValue.isEmpty())
 					{
-//						kDebug(66666) << "adding property; key='" << keyName << "', value='" << \
keyValue << "'" << endl;  propMap[keyName]=keyValue;
 					}
 				}
@@ -409,9 +451,12 @@
 	}
 };
 
+
+
 bool PlaylistSaver::loadASX(const QString &file, const KUrl &originalUrl, const \
PlaylistType opt)  {
 	Q_UNUSED(opt);
+	kDebug(66666) << k_funcinfo << "url:" << originalUrl << "; opt:" << opt << endl;
 	QFile f(file);
 	if (!f.open(QIODevice::ReadOnly))
 		return false;
@@ -430,6 +475,8 @@
 bool PlaylistSaver::loadXML(const QString &file, const PlaylistType opt)
 {
 	Q_UNUSED(opt);
+	kDebug(66666) << k_funcinfo << "file:" << file << "; opt:" << opt << endl;
+
 	QFile f(file);
 	if (!f.open(QIODevice::ReadOnly))
 		return false;
@@ -627,13 +674,13 @@
 {
 	Noatun::PropertyMap::ConstIterator it(map.begin());
 	Noatun::PropertyMap::ConstIterator end(map.end());
-	QString lowerKey = key.toLower();
+	const QString lowerKey = key.toLower();
 	for ( ; it != end; ++it)
 	{
 		if (it.key().toLower() == lowerKey)
 			return it.value();
 	}
-	return QString::null;
+	return QString();
 }
 
 bool PlaylistSaver::loadPLS(const QString &file, const PlaylistType /*opt*/)


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic