1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
21 * @file KwPlaylistModel.cpp
22 * @brief A Qt model for playlist items.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwPlaylistModel.h"
27 #include "KwPlaylistNode.h"
28 #include "KwPlaylistList.h"
29 #include "KwPlaylistListNode.h"
30 #include "KwPlaylistSong.h"
31 #include "KwPlaylistVideo.h"
32 #include "KwPlaylistImage.h"
33 #include "KwPlaylistPresentation.h"
36 #include "kmimetype.h"
40 #include <QStringList>
43 * Constructors + destructor.
46 /// Default constructor.
47 KwPlaylistModel::KwPlaylistModel(QObject
* parent
)
48 : NodeBasedModel
<KwPlaylistNode
>(parent
)
53 KwPlaylistModel::~KwPlaylistModel()
58 * Modification interface
61 /// Add an item to the list.
62 void KwPlaylistModel::addItem(const QModelIndex
& parent
, KwPlaylistItem
* item
, int position
)
64 KwPlaylistListNode
* list
= dynamic_cast<KwPlaylistListNode
*>(itemFromIndex(parent
));
68 position
= list
->getChildCount();
70 beginInsertRows(parent
, position
, position
);
71 list
->childrenAdded(position
, position
);
72 list
->getItem()->addItem(item
, position
);
80 QStringList
KwPlaylistModel::mimeTypes() const
83 //mimes << "application/x.kworship.song.list";
84 mimes
<< "text/uri-list";
88 Qt::DropActions
KwPlaylistModel::supportedDropActions() const
90 return Qt::CopyAction
| Qt::MoveAction
;
93 Qt::ItemFlags
KwPlaylistModel::flags(const QModelIndex
& index
) const
95 Qt::ItemFlags defaultFlags
= QAbstractItemModel::flags(index
);
97 KwPlaylistNode
* item
= itemFromIndex(index
);
100 defaultFlags
= item
->getFlags(defaultFlags
);
106 bool KwPlaylistModel::dropMimeData(const QMimeData
* data
, Qt::DropAction action
, int row
, int column
, const QModelIndex
& parent
)
110 if (action
== Qt::IgnoreAction
)
115 KwPlaylistListNode
* list
= dynamic_cast<KwPlaylistListNode
*>(itemFromIndex(parent
));
120 // Always just append if not given explicit row
123 row
= list
->getChildCount();
126 if (0 && data
->hasFormat("application/x.kworship.song.list"))
128 QByteArray encodedData
= data
->data("application/x.kworship.song.list");
129 QDataStream
stream(&encodedData
, QIODevice::ReadOnly
);
130 QStringList newItems
;
132 while (!stream
.atEnd())
137 QStringList words
= text
.split(" ");
138 if (words
[0] == "songdb")
140 if (words
.size() > 1)
143 int versionId
= words
[1].toInt(&ok
, 0);
146 KwPlaylistSong
* newSong
= new KwPlaylistSong(KwSongdb::self()->songVersionById(versionId
));
147 addItem(parent
, newSong
, row
);
156 else if (data
->hasUrls())
158 QList
<QUrl
> files
= data
->urls();
159 foreach (QUrl file
, files
)
161 // Get the file's mime type
162 KMimeType::Ptr result
= KMimeType::findByUrl(KUrl(file
));
163 KwPlaylistItem
* newItem
= 0;
164 if (!result
->isDefault())
166 if (result
->name().startsWith("image/"))
168 newItem
= new KwPlaylistImage(file
);
170 else if (result
->name().startsWith("video/"))
172 newItem
= new KwPlaylistVideo(file
);
174 // perhaps its a presentation
175 /// @todo match against all known presentation mime types
176 else if (result
->name() == "application/vnd.oasis.opendocument.presentation")
178 newItem
= new KwPlaylistPresentation(file
);
183 newItem
= new KwPlaylistFile(file
);
185 addItem(parent
, newItem
, row
);