Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 40c114dd authored by Michael Pyne's avatar Michael Pyne

Move much of the createItems<> templated method implementation to a helper function in playlist.cpp

While doing so I managed to remove the need to have extraneous template types to avoid
recursive includes so the code is a bit neater now as well.

One functionality change is that items belonging to files that don't exist anymore are not
created anymore.  Hopefully this will eliminate crashes on startup due to files in the collection
being deleted between JuK startups.

svn path=/trunk/KDE/kdemultimedia/juk/; revision=852974
parent c8f53250
......@@ -48,13 +48,12 @@ HistoryPlaylistItem *HistoryPlaylist::createItem(const FileHandle &file,
{
if(!after)
after = lastItem();
return Playlist::createItem<HistoryPlaylistItem, CollectionListItem,
CollectionList>(file, after, emitChanged);
return Playlist::createItem<HistoryPlaylistItem>(file, after, emitChanged);
}
void HistoryPlaylist::createItems(const PlaylistItemList &siblings)
{
Playlist::createItems<CollectionListItem, HistoryPlaylistItem, PlaylistItem>(siblings);
Playlist::createItems<HistoryPlaylistItem, PlaylistItem>(siblings);
}
////////////////////////////////////////////////////////////////////////////////
......
......@@ -1333,12 +1333,12 @@ int Playlist::addColumn(const QString &label, int)
PlaylistItem *Playlist::createItem(const FileHandle &file,
Q3ListViewItem *after, bool emitChanged)
{
return createItem<PlaylistItem, CollectionListItem, CollectionList>(file, after, emitChanged);
return createItem<PlaylistItem>(file, after, emitChanged);
}
void Playlist::createItems(const PlaylistItemList &siblings, PlaylistItem *after)
{
createItems<CollectionListItem, PlaylistItem, PlaylistItem>(siblings, after);
createItems<PlaylistItem, PlaylistItem>(siblings, after);
}
void Playlist::addFiles(const QStringList &files, PlaylistItem *after)
......@@ -1618,6 +1618,32 @@ void Playlist::setDynamicListsFrozen(bool frozen)
m_collection->setDynamicListsFrozen(frozen);
}
CollectionListItem *Playlist::collectionListItem(const FileHandle &file)
{
if(!QFile::exists(file.absFilePath())) {
kError() << "File" << file.absFilePath() << "does not exist.";
return 0;
}
CollectionListItem *item = CollectionList::instance()->lookup(file.absFilePath());
if(!item) {
item = new CollectionListItem(file);
setupItem(item);
// If a valid tag was not created, destroy the CollectionListItem.
if(!item->isValid()) {
kError(65432) << "Playlist::createItem() -- A valid tag was not created for \""
<< file.absFilePath() << "\"" << endl;
delete item;
return 0;
}
}
return item;
}
////////////////////////////////////////////////////////////////////////////////
// protected slots
////////////////////////////////////////////////////////////////////////////////
......
......@@ -44,6 +44,7 @@ class WebImageFetcher;
class PlaylistItem;
class PlaylistCollection;
class PlaylistToolTip;
class CollectionListItem;
typedef QList<PlaylistItem *> PlaylistItemList;
......@@ -171,12 +172,9 @@ public:
/**
* This is implemented as a template method to allow subclasses to
* instantiate their PlaylistItem subclasses using the same method. Some
* of the types here are artificially templatized (i.e. CollectionListType and
* CollectionItemType) to avoid recursive includes, but in fact will always
* be the same.
* instantiate their PlaylistItem subclasses using the same method.
*/
template <class ItemType, class CollectionItemType, class CollectionListType>
template <class ItemType>
ItemType *createItem(const FileHandle &file,
Q3ListViewItem *after = 0,
bool emitChanged = true);
......@@ -461,10 +459,9 @@ protected:
/**
* As a template this allows us to use the same code to initialize the items
* in subclasses. CollectionItemType should always be CollectionListItem and
* ItemType should be a PlaylistItem subclass.
* in subclasses. ItemType should be a PlaylistItem subclass.
*/
template <class CollectionItemType, class ItemType, class SiblingType>
template <class ItemType, class SiblingType>
void createItems(const QList<SiblingType *> &siblings, ItemType *after = 0);
protected slots:
......@@ -554,6 +551,14 @@ private:
void updatePlaying() const;
/**
* Used as a helper to implement template<> createItem(). This grabs the
* CollectionListItem for file if it exists, otherwise it creates a new one and
* returns that. If 0 is returned then some kind of error occurred, such as file not
* found and probably nothing should be done with the FileHandle you have.
*/
CollectionListItem *collectionListItem(const FileHandle &file);
/**
* This class is used internally to store settings that are shared by all
* of the playlists, such as column order. It is implemented as a singleton.
......@@ -725,26 +730,11 @@ QDataStream &operator>>(QDataStream &s, Playlist &p);
// template method implementations
template <class ItemType, class CollectionItemType, class CollectionListType>
template <class ItemType>
ItemType *Playlist::createItem(const FileHandle &file, Q3ListViewItem *after,
bool emitChanged)
{
CollectionItemType *item = CollectionListType::instance()->lookup(file.absFilePath());
if(!item) {
item = new CollectionItemType(file);
setupItem(item);
// If a valid tag was not created, destroy the CollectionListItem.
if(!item->isValid()) {
kError(65432) << "Playlist::createItem() -- A valid tag was not created for \""
<< file.absFilePath() << "\"" << endl;
delete item;
return 0;
}
}
CollectionListItem *item = collectionListItem(file);
if(item && (!m_members.insert(file.absFilePath()) || m_allowDuplicates)) {
ItemType *i = after ? new ItemType(item, this, after) : new ItemType(item, this);
......@@ -774,7 +764,7 @@ ItemType *Playlist::createItem(SiblingType *sibling, ItemType *after)
return after;
}
template <class CollectionItemType, class ItemType, class SiblingType>
template <class ItemType, class SiblingType>
void Playlist::createItems(const QList<SiblingType *> &siblings, ItemType *after)
{
if(siblings.isEmpty())
......
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