Commit 0f52cbd1 authored by Michael Pyne's avatar Michael Pyne

Forward port fix allowing JuK to load JuK data from KDE 4.0.0 to trunk.

svn path=/trunk/KDE/kdemultimedia/juk/; revision=758772
parent 7680d038
......@@ -17,6 +17,7 @@
***************************************************************************/
#include "cache.h"
#include "juk-exception.h"
#include <kstandarddirs.h>
#include <kmessagebox.h>
......@@ -118,6 +119,7 @@ void Cache::loadPlaylists(PlaylistCollection *collection) // static
qint32 version;
fs >> version;
kDebug(65432) << "Playlists file is version" << version;
switch(version) {
case 3:
......@@ -137,66 +139,112 @@ void Cache::loadPlaylists(PlaylistCollection *collection) // static
if(checksum != qChecksum(data.data(), data.size()))
return;
// Create a new stream just based on the data.
QDataStream s(&data, QIODevice::ReadOnly);
s.setVersion(dataStreamVersion);
while(!s.atEnd()) {
qint32 playlistType;
s >> playlistType;
Playlist *playlist = 0;
switch(playlistType) {
case Search:
{
SearchPlaylist *p = new SearchPlaylist(collection);
s >> *p;
playlist = p;
break;
}
case History:
{
action<KToggleAction>("showHistory")->setChecked(true);
collection->setHistoryPlaylistEnabled(true);
s >> *collection->historyPlaylist();
playlist = collection->historyPlaylist();
break;
}
case Upcoming:
{
/*
collection->setUpcomingPlaylistEnabled(true);
Playlist *p = collection->upcomingPlaylist();
action<KToggleAction>("saveUpcomingTracks")->setChecked(true);
s >> *p;
playlist = p;
*/
break;
}
case Folder:
{
FolderPlaylist *p = new FolderPlaylist(collection);
s >> *p;
playlist = p;
break;
}
default:
Playlist *p = new Playlist(collection, true);
s >> *p;
playlist = p;
break;
}
if(version >= 2) {
qint32 sortColumn;
s >> sortColumn;
if(playlist)
playlist->setSorting(sortColumn);
}
}
// If we chose the wrong QDataStream version we may have to back out
// and try again, so get ready by noting the playlists we have made.
QList<Playlist *> createdPlaylists;
bool errorOccurred = true;
while(errorOccurred) {
// Create a new stream just based on the data.
QDataStream s(&data, QIODevice::ReadOnly);
s.setVersion(dataStreamVersion);
try { // Failure due to wrong version can be indicated by an exception
while(!s.atEnd()) {
qint32 playlistType;
s >> playlistType;
Playlist *playlist = 0;
switch(playlistType) {
case Search:
{
SearchPlaylist *p = new SearchPlaylist(collection);
createdPlaylists.append(p);
s >> *p;
playlist = p;
break;
}
case History:
{
action<KToggleAction>("showHistory")->setChecked(true);
collection->setHistoryPlaylistEnabled(true);
s >> *collection->historyPlaylist();
playlist = collection->historyPlaylist();
break;
}
case Upcoming:
{
/*
collection->setUpcomingPlaylistEnabled(true);
Playlist *p = collection->upcomingPlaylist();
action<KToggleAction>("saveUpcomingTracks")->setChecked(true);
s >> *p;
playlist = p;
*/
break;
}
case Folder:
{
FolderPlaylist *p = new FolderPlaylist(collection);
createdPlaylists.append(p);
s >> *p;
playlist = p;
break;
}
default:
Playlist *p = new Playlist(collection, true);
createdPlaylists.append(p);
s >> *p;
playlist = p;
break;
} // switch
if(version >= 2) {
qint32 sortColumn;
s >> sortColumn;
if(playlist)
playlist->setSorting(sortColumn);
}
} // while !s.atEnd()
// Must be ok if we got this far, break out of loop.
errorOccurred = false;
} // try
catch(BICStreamException &) {
kError(65432) << "Exception loading playlists - binary incompatible stream.";
// Delete created playlists which probably have junk now.
foreach(Playlist *p, createdPlaylists)
delete p;
createdPlaylists.clear();
if(dataStreamVersion == QDataStream::Qt_3_3) {
kError(65432) << "Attempting other binary protocol - Qt 4.3";
dataStreamVersion = QDataStream::Qt_4_3;
break; // escape from while(!s.atEnd()) to try again
}
#if QT_VERSION >= 0x040400
// Unlikely, but maybe user had Qt 4.4 with KDE 4.0.0?
else if(dataStreamVersion == QDataStream::Qt_4_3) {
kError(65432) << "Attempting other binary protocol - Qt 4.4";
dataStreamVersion = QDataStream::Qt_4_4;
break;
}
#endif
// We tried 3.3 first, if 4.3/4.4 doesn't work who knows...
kError(65432) << "Unable to recover, no playlists will be loaded.";
return;
} // catch
} // while dataStreamVersion != -1
break;
}
default:
......
......@@ -14,6 +14,7 @@
#include "folderplaylist.h"
#include "playlistcollection.h"
#include "juk-exception.h"
#include <QTimer>
......@@ -73,6 +74,9 @@ QDataStream &operator>>(QDataStream &s, FolderPlaylist &p)
s >> name
>> folder;
if(folder.isEmpty() || name.isEmpty())
throw BICStreamException();
p.setFolder(folder);
p.setName(name);
return s;
......
......@@ -16,6 +16,7 @@
#include "historyplaylist.h"
#include "collectionlist.h"
#include "playermanager.h"
#include "juk-exception.h"
#include <QTimer>
......@@ -151,6 +152,9 @@ QDataStream &operator>>(QDataStream &s, HistoryPlaylist &p)
s >> fileName;
s >> dateTime;
if(fileName.isEmpty() || !dateTime.isValid())
throw BICStreamException();
after = p.createItem(FileHandle(fileName), after, false);
after->setDateTime(dateTime);
}
......
/***************************************************************************
begin : Tue Jan 08 2008
copyright : (C) 2008 by Michael Pyne
email : michael.pyne@kdemail.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. *
* *
***************************************************************************/
#ifndef JUK_EXCEPTION_H
#define JUK_EXCEPTION_H
#include <exception>
/**
* This exception is thrown when playlist reading code believes that it has
* encountered a binary incompatible version of QDataStream. Added due to a
* coding error which resulted in not setting a specific encoding for
* QDataStreams.
*/
class BICStreamException : public std::exception
{
virtual const char *what() const throw()
{
return "Read jibberish from a QDataStream, probably using an older protocol.";
}
};
#endif /* JUK_EXCEPTION_H */
// vim: set et sw=4 tw=0 sta:
......@@ -2,6 +2,9 @@
begin : Sat Feb 16 2002
copyright : (C) 2002 - 2004 by Scott Wheeler
email : wheeler@kde.org
copyright : (C) 2008 by Michael Pyne
email : michael.pyne@kdemail.net
***************************************************************************/
/***************************************************************************
......@@ -14,6 +17,7 @@
***************************************************************************/
#include "playlist.h"
#include "juk-exception.h"
#include <kconfig.h>
#include <kapplication.h>
......@@ -1262,6 +1266,10 @@ void Playlist::read(QDataStream &s)
s >> m_playlistName
>> m_fileName;
// m_fileName is probably empty.
if(m_playlistName.isEmpty())
throw BICStreamException();
QStringList files;
s >> files;
......@@ -1269,8 +1277,12 @@ void Playlist::read(QDataStream &s)
m_blockDataChanged = true;
for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it)
after = createItem(FileHandle(*it), after, false);
foreach(QString file, files) {
if(file.isEmpty())
throw BICStreamException();
after = createItem(FileHandle(file), after, false);
}
m_blockDataChanged = false;
......
......@@ -265,7 +265,10 @@ void PlaylistBox::setupPlaylist(Playlist *playlist, const QString &iconName, Ite
void PlaylistBox::removePlaylist(Playlist *playlist)
{
removeNameFromDict(m_playlistDict[playlist]->text(0));
// Could be false if setup() wasn't run yet.
if(m_playlistDict.contains(playlist))
removeNameFromDict(m_playlistDict[playlist]->text(0));
removeFileFromDict(playlist->fileName());
m_playlistDict.remove(playlist);
}
......
......@@ -17,6 +17,7 @@
#include "playlist.h"
#include "playlistitem.h"
#include "collectionlist.h"
#include "juk-exception.h"
#include <kdebug.h>
......@@ -317,6 +318,9 @@ QDataStream &operator>>(QDataStream &s, PlaylistSearch::Component &c)
>> columns
>> mode;
if(pattern.isEmpty())
throw BICStreamException();
if(patternSearch)
c = PlaylistSearch::Component(QRegExp(pattern), columns);
else
......
......@@ -14,6 +14,7 @@
***************************************************************************/
#include "searchplaylist.h"
#include "juk-exception.h"
#include <kdebug.h>
......@@ -108,6 +109,9 @@ QDataStream &operator>>(QDataStream &s, SearchPlaylist &p)
s >> name
>> search;
if(name.isEmpty())
throw BICStreamException();
p.setName(name);
p.setPlaylistSearch(search, false);
......
/***************************************************************************
begin : Thu Aug 19 2004
copyright : (C) 2002 - 2004 by Michael Pyne
copyright : (C) 2002 - 2004, 2008 by Michael Pyne
email : michael.pyne@kde.org
***************************************************************************/
......@@ -14,6 +14,7 @@
***************************************************************************/
#include "upcomingplaylist.h"
#include "juk-exception.h"
#include <kdebug.h>
#include <kapplication.h>
......@@ -269,6 +270,9 @@ QDataStream &operator>>(QDataStream &s, UpcomingPlaylist &p)
for(qint32 i = 0; i < count; ++i) {
s >> fileName;
if(fileName.isEmpty())
throw BICStreamException();
newItem = p.createItem(FileHandle(fileName), newItem, false);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment