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 "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"
37 #include "kmimetype.h"
41 #include <QStringList>
42 #include <QTextStream>
45 * Constructors + destructor.
48 /// Default constructor.
49 KwPlaylistModel::KwPlaylistModel(QObject
* parent
)
50 : NodeBasedModel
<KwPlaylistNode
>(parent
)
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
);
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
));
100 position
= list
->getChildCount();
102 beginInsertRows(parent
, position
, position
);
103 list
->childrenAdded(position
, position
);
104 list
->getItem()->addItem(item
, position
);
112 QStringList
KwPlaylistModel::mimeTypes() const
115 mimes
<< "application/x-kworship+xml";
116 //mimes << "application/x.kworship.song.list";
117 mimes
<< "text/uri-list";
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
);
133 defaultFlags
= item
->getFlags(defaultFlags
);
139 QMimeData
*KwPlaylistModel::mimeData(const QModelIndexList
&indexes
) const
141 QMimeData
*mimeData
= new QMimeData();
142 QByteArray encodedData
;
144 QTextStream
stream(&encodedData
, QIODevice::WriteOnly
);
147 QList
<KwPlaylistItem
*> items
;
148 foreach(QModelIndex index
, indexes
)
152 KwPlaylistNode
*node
= itemFromIndex(index
);
153 KwPlaylistItem
*item
= node
->playlistItem();
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
);
174 bool KwPlaylistModel::dropMimeData(const QMimeData
* data
, Qt::DropAction action
, int row
, int column
, const QModelIndex
& parent
)
178 if (action
== Qt::IgnoreAction
)
183 KwPlaylistListNode
* list
= dynamic_cast<KwPlaylistListNode
*>(itemFromIndex(parent
));
188 // Always just append if not given explicit row
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
);
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
);
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())
224 QStringList words
= text
.split(" ");
225 if (words
[0] == "songdb")
227 if (words
.size() > 1)
230 int versionId
= words
[1].toInt(&ok
, 0);
233 KwPlaylistSong
* newSong
= new KwPlaylistSong(KwSongdb::self()->songVersionById(versionId
));
234 addItem(parent
, newSong
, row
);
243 else if (data
->hasUrls())
245 QList
<QUrl
> files
= data
->urls();
246 foreach (QUrl file
, files
)
248 addFile(parent
, file
, row
);