Turn songdb into a plugin, lots of tweaks, drag'n'drop of songs temporarily broken
[kworship.git] / kworship / playlist / KwPlaylistModel.cpp
blobc81fa1840bba9bc7767a8849e2364d02137742f4
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 "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"
34 #include "KwSongdb.h"
36 #include "kmimetype.h"
38 #include <QMimeData>
39 #include <QUrl>
40 #include <QStringList>
43 * Constructors + destructor.
46 /// Default constructor.
47 KwPlaylistModel::KwPlaylistModel(QObject* parent)
48 : NodeBasedModel<KwPlaylistNode>(parent)
52 /// Destructor.
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));
65 Q_ASSERT(0 != list);
66 if (position == -1)
68 position = list->getChildCount();
70 beginInsertRows(parent, position, position);
71 list->childrenAdded(position, position);
72 list->getItem()->addItem(item, position);
73 endInsertRows();
77 * Drag and drop
80 QStringList KwPlaylistModel::mimeTypes() const
82 QStringList mimes;
83 //mimes << "application/x.kworship.song.list";
84 mimes << "text/uri-list";
85 return mimes;
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);
98 if (item != 0)
100 defaultFlags = item->getFlags(defaultFlags);
103 return defaultFlags;
106 bool KwPlaylistModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent)
108 Q_UNUSED(column)
110 if (action == Qt::IgnoreAction)
112 return true;
115 KwPlaylistListNode* list = dynamic_cast<KwPlaylistListNode*>(itemFromIndex(parent));
116 if (0 == list)
118 return true;
120 // Always just append if not given explicit row
121 if (row == -1)
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())
134 QString text;
135 stream >> text;
137 QStringList words = text.split(" ");
138 if (words[0] == "songdb")
140 if (words.size() > 1)
142 bool ok;
143 int versionId = words[1].toInt(&ok, 0);
144 if (ok)
146 KwPlaylistSong* newSong = new KwPlaylistSong(KwSongdb::self()->songVersionById(versionId));
147 addItem(parent, newSong, row);
148 ++row;
154 return true;
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);
181 if (0 == newItem)
183 newItem = new KwPlaylistFile(file);
185 addItem(parent, newItem, row);
186 ++row;
189 return true;
192 return false;