Added playlist actions (insert, delete, move up, move down), with only insert working...
[kworship.git] / kworship / playlist / KwPlaylistModel.cpp
blobc1a629cba51457e96875e46688440375f8efeef7
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 /**
21 * @file KwPlaylistModel.cpp
22 * @brief A Qt model for playlist items.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwDataFile.h"
27 #include "KwPlaylistModel.h"
28 #include "KwPlaylistNode.h"
29 #include "KwPlaylistList.h"
30 #include "KwPlaylistListNode.h"
31 #include "KwPlaylistSong.h"
32 #include "KwPlaylistVideo.h"
33 #include "KwPlaylistImage.h"
34 #include "KwPlaylistPresentation.h"
35 #include "KwSongdb.h"
37 #include "kmimetype.h"
39 #include <QMimeData>
40 #include <QUrl>
41 #include <QStringList>
42 #include <QTextStream>
45 * Constructors + destructor.
48 /// Default constructor.
49 KwPlaylistModel::KwPlaylistModel(QObject* parent)
50 : NodeBasedModel<KwPlaylistNode>(parent)
54 /// Destructor.
55 KwPlaylistModel::~KwPlaylistModel()
60 * Modification interface
63 /// Add a file to the list.
64 void KwPlaylistModel::addFile(const QModelIndex& parent, const QUrl& file, int position)
66 // Get the file's mime type
67 KMimeType::Ptr result = KMimeType::findByUrl(KUrl(file));
68 KwPlaylistItem* newItem = 0;
69 if (!result->isDefault())
71 if (result->name().startsWith("image/"))
73 newItem = new KwPlaylistImage(file);
75 else if (result->name().startsWith("video/"))
77 newItem = new KwPlaylistVideo(file);
79 // perhaps its a presentation
80 /// @todo match against all known presentation mime types
81 else if (result->name() == "application/vnd.oasis.opendocument.presentation")
83 newItem = new KwPlaylistPresentation(file);
86 if (0 == newItem)
88 newItem = new KwPlaylistFile(file);
90 addItem(parent, newItem, position);
93 /// Add an item to the list.
94 void KwPlaylistModel::addItem(const QModelIndex& parent, KwPlaylistItem* item, int position)
96 KwPlaylistListNode* list = dynamic_cast<KwPlaylistListNode*>(itemFromIndex(parent));
97 Q_ASSERT(0 != list);
98 if (position == -1)
100 position = list->getChildCount();
102 beginInsertRows(parent, position, position);
103 list->childrenAdded(position, position);
104 list->getItem()->addItem(item, position);
105 endInsertRows();
109 * Drag and drop
112 QStringList KwPlaylistModel::mimeTypes() const
114 QStringList mimes;
115 mimes << "application/x-kworship+xml";
116 //mimes << "application/x.kworship.song.list";
117 mimes << "text/uri-list";
118 return mimes;
121 Qt::DropActions KwPlaylistModel::supportedDropActions() const
123 return Qt::CopyAction | Qt::MoveAction;
126 Qt::ItemFlags KwPlaylistModel::flags(const QModelIndex& index) const
128 Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
130 KwPlaylistNode* item = itemFromIndex(index);
131 if (item != 0)
133 defaultFlags = item->getFlags(defaultFlags);
136 return defaultFlags;
139 QMimeData *KwPlaylistModel::mimeData(const QModelIndexList &indexes) const
141 QMimeData *mimeData = new QMimeData();
142 QByteArray encodedData;
144 QTextStream stream(&encodedData, QIODevice::WriteOnly);
145 KwDataFile data;
147 QList<KwPlaylistItem*> items;
148 foreach(QModelIndex index, indexes)
150 if (index.isValid())
152 KwPlaylistNode *node = itemFromIndex(index);
153 KwPlaylistItem *item = node->playlistItem();
154 if (0 != item)
156 items << item;
158 else
160 Q_ASSERT(false);
161 delete mimeData;
162 return 0;
166 // Use data file as resource manager for now
167 data.insertPlaylistItems(items, &data);
168 data.writeTo(stream);
170 mimeData->setData("application/x-kworship+xml", encodedData);
171 return mimeData;
174 bool KwPlaylistModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent)
176 Q_UNUSED(column)
178 if (action == Qt::IgnoreAction)
180 return true;
183 KwPlaylistListNode* list = dynamic_cast<KwPlaylistListNode*>(itemFromIndex(parent));
184 if (0 == list)
186 return true;
188 // Always just append if not given explicit row
189 if (row == -1)
191 row = list->getChildCount();
194 if (data->hasFormat("application/x-kworship+xml"))
196 QByteArray encodedData = data->data("application/x-kworship+xml");
197 QTextStream stream(&encodedData, QIODevice::ReadOnly);
199 KwDataFile data;
200 data.readFrom(stream.device());
202 // Use data file as resource manager for now
203 QList<KwPlaylistItem*> items = data.extractPlaylistItems(&data);
205 foreach (KwPlaylistItem* item, items)
207 addItem(parent, item, row);
208 ++row;
211 return true;
213 else if (0 && data->hasFormat("application/x.kworship.song.list"))
215 QByteArray encodedData = data->data("application/x.kworship.song.list");
216 QDataStream stream(&encodedData, QIODevice::ReadOnly);
217 QStringList newItems;
219 while (!stream.atEnd())
221 QString text;
222 stream >> text;
224 QStringList words = text.split(" ");
225 if (words[0] == "songdb")
227 if (words.size() > 1)
229 bool ok;
230 int versionId = words[1].toInt(&ok, 0);
231 if (ok)
233 KwPlaylistSong* newSong = new KwPlaylistSong(KwSongdb::self()->songVersionById(versionId));
234 addItem(parent, newSong, row);
235 ++row;
241 return true;
243 else if (data->hasUrls())
245 QList<QUrl> files = data->urls();
246 foreach (QUrl file, files)
248 addFile(parent, file, row);
249 ++row;
252 return true;
255 return false;