]> git.sur5r.net Git - minitube/blob - src/ListModel.cpp
Imported Upstream version 1.2
[minitube] / src / ListModel.cpp
1 #include "ListModel.h"
2 #include "videomimedata.h"
3
4 #define MAX_ITEMS 10
5 static const QString recentKeywordsKey = "recentKeywords";
6
7 ListModel::ListModel(QWidget *parent) : QAbstractListModel(parent) {
8     youtubeSearch = 0;
9     searching = false;
10     canSearchMore = true;
11     m_activeVideo = 0;
12     m_activeRow = -1;
13     skip = 1;
14 }
15
16 ListModel::~ListModel() {
17     delete youtubeSearch;
18 }
19
20 int ListModel::rowCount(const QModelIndex &/*parent*/) const {
21     int count = videos.size();
22     
23     // add the message item
24     if (videos.isEmpty() || !searching)
25         count++;
26     
27     return count;
28 }
29
30 QVariant ListModel::data(const QModelIndex &index, int role) const {
31     
32     int row = index.row();
33     
34     if (row == videos.size()) {
35         
36         QPalette palette;
37         QFont boldFont;
38         boldFont.setBold(true);
39         
40         switch (role) {
41         case ItemTypeRole:
42             return ItemTypeShowMore;
43         case Qt::DisplayRole:
44         case Qt::StatusTipRole:
45             if (!errorMessage.isEmpty()) return errorMessage;
46             if (searching) return tr("Searching...");
47             if (canSearchMore) return tr("Show %1 More").arg(MAX_ITEMS);
48             if (videos.isEmpty()) return tr("No videos");
49             else return tr("No more videos");
50         case Qt::TextAlignmentRole:
51             return QVariant(int(Qt::AlignHCenter | Qt::AlignVCenter));
52         case Qt::ForegroundRole:
53             if (!errorMessage.isEmpty())
54                 return palette.color(QPalette::ToolTipText);
55             else
56                 return palette.color(QPalette::Dark);
57         case Qt::BackgroundColorRole:
58             if (!errorMessage.isEmpty())
59                 return palette.color(QPalette::ToolTipBase);
60             else
61                 return QVariant();
62         case Qt::FontRole:
63             return boldFont;
64         default:
65             return QVariant();
66         }
67         
68     } else if (row < 0 || row >= videos.size())
69         return QVariant();
70     
71     Video *video = videos.at(row);
72     
73     switch (role) {
74     case ItemTypeRole:
75         return ItemTypeVideo;
76     case VideoRole:
77         return QVariant::fromValue(QPointer<Video>(video));
78     case ActiveTrackRole:
79         return video == m_activeVideo;
80     case Qt::DisplayRole:
81         return video->title();
82     }
83     
84     return QVariant();
85 }
86
87 void ListModel::setActiveRow( int row) {
88     if ( rowExists( row ) ) {
89         
90         m_activeRow = row;
91         m_activeVideo = videoAt(row);
92         
93         int oldactiverow = m_activeRow;
94         
95         if ( rowExists( oldactiverow ) )
96             emit dataChanged( createIndex( oldactiverow, 0 ), createIndex( oldactiverow, columnCount() - 1 ) );
97         
98         emit dataChanged( createIndex( m_activeRow, 0 ), createIndex( m_activeRow, columnCount() - 1 ) );
99         emit activeRowChanged(row);
100         
101     } else {
102         m_activeRow = -1;
103         m_activeVideo = 0;
104     }
105
106 }
107
108 int ListModel::nextRow() const {
109     int nextRow = m_activeRow + 1;
110     if (rowExists(nextRow))
111         return nextRow;
112     return -1;
113 }
114
115 Video* ListModel::videoAt( int row ) const {
116     if ( rowExists( row ) )
117         return videos.at( row );
118     return 0;
119 }
120
121 Video* ListModel::activeVideo() const {
122     return m_activeVideo;
123 }
124
125 void ListModel::search(SearchParams *searchParams) {
126
127     // delete current videos
128     while (!videos.isEmpty())
129         delete videos.takeFirst();
130     m_activeVideo = 0;
131     m_activeRow = -1;
132     skip = 1;
133     errorMessage.clear();
134     reset();
135
136     // (re)initialize the YouTubeSearch
137     if (youtubeSearch) delete youtubeSearch;
138     youtubeSearch = new YouTubeSearch();
139     connect(youtubeSearch, SIGNAL(gotVideo(Video*)), this, SLOT(addVideo(Video*)));
140     connect(youtubeSearch, SIGNAL(finished(int)), this, SLOT(searchFinished(int)));
141     connect(youtubeSearch, SIGNAL(error(QString)), this, SLOT(searchError(QString)));
142
143     this->searchParams = searchParams;
144     searching = true;
145     youtubeSearch->search(searchParams, MAX_ITEMS, skip);
146     skip += MAX_ITEMS;
147 }
148
149 void ListModel::searchMore(int max) {
150     if (searching) return;
151     searching = true;
152     errorMessage.clear();
153     youtubeSearch->search(searchParams, max, skip);
154     skip += max;
155 }
156
157 void ListModel::searchMore() {
158     searchMore(MAX_ITEMS);
159 }
160
161 void ListModel::searchNeeded() {
162     int remainingRows = videos.size() - m_activeRow;
163     int rowsNeeded = MAX_ITEMS - remainingRows;
164     if (rowsNeeded > 0)
165         searchMore(rowsNeeded);
166 }
167
168 void ListModel::abortSearch() {
169     while (!videos.isEmpty())
170         delete videos.takeFirst();
171     reset();
172     youtubeSearch->abort();
173     searching = false;
174 }
175
176 void ListModel::searchFinished(int total) {
177     searching = false;
178     canSearchMore = total > 0;
179
180     // update the message item
181     emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
182 }
183
184 void ListModel::searchError(QString message) {
185     errorMessage = message;
186     // update the message item
187     emit dataChanged( createIndex( MAX_ITEMS, 0 ), createIndex( MAX_ITEMS, columnCount() - 1 ) );
188 }
189
190 void ListModel::addVideo(Video* video) {
191     
192     connect(video, SIGNAL(gotThumbnail()), this, SLOT(updateThumbnail()));
193
194     beginInsertRows(QModelIndex(), videos.size(), videos.size());
195     videos << video;
196     endInsertRows();
197     
198     // first result!
199     if (videos.size() == 1) {
200         // autoplay
201         setActiveRow(0);
202
203         // save keyword
204         QString query = searchParams->keywords();
205         QSettings settings;
206         QStringList keywords = settings.value(recentKeywordsKey).toStringList();
207         keywords.removeAll(query);
208         keywords.prepend(query);
209         while (keywords.size() > 10)
210             keywords.removeLast();
211         settings.setValue(recentKeywordsKey, keywords);
212     }
213
214 }
215
216 void ListModel::updateThumbnail() {
217
218     Video *video = static_cast<Video *>(sender());
219     if (!video) {
220         qDebug() << "Cannot get sender";
221         return;
222     }
223
224     int row = rowForVideo(video);
225     emit dataChanged( createIndex( row, 0 ), createIndex( row, columnCount() - 1 ) );
226
227 }
228
229 // --- item removal
230
231 /**
232   * This function does not free memory
233   */
234 bool ListModel::removeRows(int position, int rows, const QModelIndex & /*parent*/) {
235     beginRemoveRows(QModelIndex(), position, position+rows-1);
236     for (int row = 0; row < rows; ++row) {
237         videos.removeAt(position);
238     }
239     endRemoveRows();
240     return true;
241 }
242
243 void ListModel::removeIndexes(QModelIndexList &indexes) {
244     QList<Video*> originalList(videos);
245     QList<Video*> delitems;
246     foreach (QModelIndex index, indexes) {
247         if (index.row() >= originalList.size()) continue;
248         Video* video = originalList.at(index.row());
249         int idx = videos.indexOf(video);
250         if (idx != -1) {
251             beginRemoveRows(QModelIndex(), idx, idx);
252             delitems.append(video);
253             videos.removeAll(video);
254             endRemoveRows();
255         }
256     }
257
258     qDeleteAll(delitems);
259
260 }
261
262 // --- Sturm und drang ---
263
264
265
266 Qt::DropActions ListModel::supportedDropActions() const {
267     return Qt::MoveAction;
268 }
269
270 Qt::ItemFlags ListModel::flags(const QModelIndex &index) const {
271     Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
272
273     if (index.isValid()) {
274         if (index.row() == videos.size()) {
275             // don't drag the "show 10 more" item
276             return defaultFlags;
277         } else
278             return ( defaultFlags | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled );
279     } else
280         return Qt::ItemIsDropEnabled | defaultFlags;
281 }
282
283 QStringList ListModel::mimeTypes() const {
284     QStringList types;
285     types << "application/x-minitube-video";
286     return types;
287 }
288
289 QMimeData* ListModel::mimeData( const QModelIndexList &indexes ) const {
290     VideoMimeData* mime = new VideoMimeData();
291
292     foreach( const QModelIndex &it, indexes ) {
293         int row = it.row();
294         if (row >= 0 && row < videos.size())
295             mime->addVideo( videos.at( it.row() ) );
296     }
297
298     return mime;
299 }
300
301 bool ListModel::dropMimeData(const QMimeData *data,
302                              Qt::DropAction action, int row, int column,
303                              const QModelIndex &parent) {
304     if (action == Qt::IgnoreAction)
305         return true;
306
307     if (!data->hasFormat("application/x-minitube-video"))
308         return false;
309
310     if (column > 0)
311         return false;
312
313     int beginRow;
314     if (row != -1)
315         beginRow = row;
316     else if (parent.isValid())
317         beginRow = parent.row();
318     else
319         beginRow = rowCount(QModelIndex());
320
321     const VideoMimeData* videoMimeData = dynamic_cast<const VideoMimeData*>( data );
322     if(!videoMimeData ) return false;
323
324     QList<Video*> droppedVideos = videoMimeData->videos();
325     foreach( Video *video, droppedVideos) {
326         
327         // remove videos
328         int videoRow = videos.indexOf(video);
329         removeRows(videoRow, 1, QModelIndex());
330         
331         // and then add them again at the new position
332         beginInsertRows(QModelIndex(), beginRow, beginRow);
333         videos.insert(beginRow, video);
334         endInsertRows();
335
336     }
337
338     // fix m_activeRow after all this
339     m_activeRow = videos.indexOf(m_activeVideo);
340
341     // let the MediaView restore the selection
342     emit needSelectionFor(droppedVideos);
343
344     return true;
345
346 }
347
348 int ListModel::rowForVideo(Video* video) {
349     return videos.indexOf(video);
350 }
351
352 QModelIndex ListModel::indexForVideo(Video* video) {
353     return createIndex(videos.indexOf(video), 0);
354 }
355
356 void ListModel::move(QModelIndexList &indexes, bool up) {
357     QList<Video*> movedVideos;
358
359     foreach (QModelIndex index, indexes) {
360         int row = index.row();
361         if (row >= videos.size()) continue;
362         // qDebug() << "index row" << row;
363         Video *video = videoAt(row);
364         movedVideos << video;
365     }
366
367     int end=up ? -1 : rowCount()-1, mod=up ? -1 : 1;
368     foreach (Video *video, movedVideos) {
369
370         int row = rowForVideo(video);
371         if (row+mod==end) { end=row; continue; }
372         // qDebug() << "video row" << row;
373         removeRows(row, 1, QModelIndex());
374
375         if (up) row--;
376         else row++;
377
378         beginInsertRows(QModelIndex(), row, row);
379         videos.insert(row, video);
380         endInsertRows();
381
382     }
383
384     emit needSelectionFor(movedVideos);
385
386 }