5fd2177f9bdabd96ca1488802d2ff1eaa8a8b47c
[someplayer] / src / libraryform.cpp
1 /*
2  * SomePlayer - An alternate music player for Maemo 5
3  * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program 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 this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include "libraryform.h"
21 #include "ui_libraryform.h"
22 #include "library.h"
23 #include <QStandardItemModel>
24 #include <QStandardItem>
25 #include <QModelIndex>
26 #include <QModelIndexList>
27 #include "track.h"
28 #include "playlist.h"
29 #include <QDebug>
30 #include <QTime>
31 #include <QQueue>
32
33 using namespace SomePlayer::DataObjects;
34
35 inline QString __format_track_string(TrackMetadata meta) {
36         int minutes = meta.length() / 60;
37         int seconds = meta.length() % 60;
38         QTime time(0, minutes, seconds);
39         return QString("[%1] %2").arg(time.toString("mm:ss")).arg(meta.title());
40
41 }
42
43 inline void __fill_model(QStandardItemModel *model, QList<QString> data) {
44         model->clear();
45         int count = data.count();
46         model->setRowCount(count);
47         for (int i = 0; i < count; i++) {
48                 model->setItem(i, 0, new QStandardItem(data.at(i)));
49         }
50 }
51
52 inline void __fill_model_tracks (QStandardItemModel *model, QList<Track> tracks) {
53         int count = tracks.count();
54         model->setRowCount(count);
55         for (int i = 0; i < count; i++) {
56                 TrackMetadata meta = tracks.at(i).metadata();
57                 model->setItem(i, 0, new QStandardItem(__format_track_string(meta)));
58         }
59 }
60
61 LibraryForm::LibraryForm(Library *lib, QWidget *parent) :
62     QWidget(parent),
63     ui(new Ui::LibraryForm)
64 {
65         _lib = lib;
66         _model = new QStandardItemModel(this);
67         _state = STATE_NONE;
68         ui->setupUi(this);
69         connect(ui->playerButton, SIGNAL(clicked()), this, SLOT(_player()));
70         connect(ui->viewButton, SIGNAL(clicked()), this, SLOT(_view_button()));
71         connect(ui->playlistsButton, SIGNAL(clicked()), this, SLOT(_playlists_button()));
72         connect(ui->dynamicButton, SIGNAL(clicked()), this, SLOT(_dynamic_button()));
73         connect(ui->listView, SIGNAL(clicked(QModelIndex)), this, SLOT(_process_list_click(QModelIndex)));
74         connect(ui->addButton, SIGNAL(clicked()), this, SLOT(_add_button()));
75         connect(ui->backButton, SIGNAL(clicked()), this, SLOT(_back_button()));
76         connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(_delete_button()));
77         connect(ui->useButton, SIGNAL(clicked()), this, SLOT(_use_button()));
78         _view_button();
79 }
80
81 LibraryForm::~LibraryForm()
82 {
83         _lib->saveCurrentPlaylist(_lib->getCurrentPlaylist());
84     delete ui;
85 }
86
87 void LibraryForm::_player() {
88         emit player();
89 }
90
91 void LibraryForm::_view_button() {
92         QList<QString> artitst = _lib->getArtists();
93         __fill_model(_model, artitst);
94         ui->listView->setModel(_model);
95         _state = STATE_ARTIST;
96         ui->backButton->hide();
97         ui->listLabel->setText("Artists");
98         ui->addButton->show();
99         ui->deleteButton->hide();
100         ui->useButton->hide();
101 }
102
103 void LibraryForm::_dynamic_button() {
104         ui->useButton->hide();
105         ui->backButton->hide();
106         ui->addButton->show();
107         ui->deleteButton->hide();
108         _model->clear();
109         _model->setRowCount(4);
110         _model->setItem(0, new QStandardItem("Favorites"));
111         _model->setItem(1, new QStandardItem("Most played"));
112         _model->setItem(2, new QStandardItem("Never played"));
113         _model->setItem(3, new QStandardItem("Recently added"));
114         _state = STATE_DYNAMIC;
115 }
116
117 void LibraryForm::_process_list_click(QModelIndex index) {
118         if (_state == STATE_NONE) return;
119         QString data = index.data().toString();
120         switch (_state) {
121         case STATE_ARTIST:
122                 __fill_model(_model, _lib->getAlbumsForArtist(data));
123                 _current_artist = data;
124                 _state = STATE_ALBUM;
125                 ui->backButton->show();
126                 ui->listLabel->setText(QString("Albums by \"%1\"").arg(_current_artist));
127                 break;
128         case STATE_ALBUM:
129                 _current_album = data;
130                 _current_tracks = _lib->getTracksForAlbum(data, _current_artist);
131                 __fill_model_tracks(_model, _current_tracks);
132                 _state = STATE_TRACK;
133                 ui->backButton->show();
134                 ui->listLabel->setText(QString("Tracks from \"%1\" by \"%2\"").arg(_current_album).arg(_current_artist));
135                 break;
136         case STATE_PLAYLIST:
137                 {
138                         _current_playlist = _lib->getPlaylist(data);
139                         _current_tracks = _current_playlist.tracks();
140                         __fill_model_tracks(_model, _current_tracks);
141                         _state = STATE_PLAYLIST_TRACK;
142                         ui->backButton->show();
143                         ui->deleteButton->show();
144                         ui->useButton->show();
145                         ui->listLabel->setText(QString("Tracks in playlist \"%1\"").arg(data));
146                 }
147                 break;
148         case STATE_DYNAMIC:
149                 {
150                         switch(index.row()) {
151                         case 0: //favorites
152                                 _current_playlist = _lib->getFavorites();
153                                 break;
154                         case 1: //most played
155                                 _current_playlist = _lib->getMostPlayed();
156                                 break;
157                         case 2: //never played
158                                 _current_playlist = _lib->getNeverPlayed();
159                         case 3: //recently added
160                                 _current_playlist = _lib->getRecentlyAdded();
161                                 break;
162                         default:
163                                 return;
164                         }
165                         _current_tracks = _current_playlist.tracks();
166                         __fill_model_tracks(_model, _current_tracks);
167                         _state = STATE_PLAYLIST_TRACK;
168                         ui->backButton->show();
169                         ui->useButton->show();
170                         ui->addButton->show();
171                         ui->listLabel->setText(_current_playlist.name());
172                 }
173         default:
174                 return;
175         }
176 }
177
178 void LibraryForm::_add_button() {
179         if (_state == STATE_NONE) return;
180         QModelIndexList selected = ui->listView->selectionModel()->selectedIndexes();
181         ui->listView->selectionModel()->clearSelection();
182         emit busy(QString("<H1>Adding... Please wait</H1>"));
183         switch (_state) {
184         case STATE_ARTIST:
185                 foreach (QModelIndex id, selected) {
186                         _add_artist(id.data().toString());
187                 }
188                 break;
189         case STATE_ALBUM:
190                 foreach (QModelIndex id, selected) {
191                         _add_album(_current_artist, id.data().toString());
192                 }
193                 break;
194         case STATE_TRACK:
195                 foreach (QModelIndex id, selected) {
196                         _add_track(_current_tracks.at(id.row()));
197                 }
198                 break;
199         case STATE_PLAYLIST:
200                 foreach (QModelIndex id, selected) {
201                         _add_playlist(id.data().toString());
202                 }
203                 break;
204         case STATE_PLAYLIST_TRACK:
205                 foreach (QModelIndex id, selected) {
206                         _add_track(_current_tracks.at(id.row()));
207                 }
208                 break;
209         default:
210                 emit done();
211                 return;
212         }
213         emit done();
214 }
215
216
217 void LibraryForm::_add_artist(QString artist) {
218         QList<QString> albums = _lib->getAlbumsForArtist(artist);
219         foreach(QString album, albums) {
220                 _add_album(artist, album);
221         }
222 }
223
224 void LibraryForm::_add_album(QString artist, QString album) {
225         QList<Track> tracks = _lib->getTracksForAlbum(album, artist);
226         foreach(Track track, tracks) {
227                 _add_track(track);
228         }
229 }
230
231 void LibraryForm::_add_track(Track track) {
232         Playlist current = _lib->getCurrentPlaylist();
233         current.addTrack(track);
234         _lib->saveCurrentPlaylist(current);
235 }
236
237 void LibraryForm::_add_playlist(QString name) {
238         Playlist playlist = _lib->getPlaylist(name);
239         QList<Track> tracks = playlist.tracks();
240         foreach (Track track, tracks) {
241                 _add_track(track);
242         }
243 }
244
245 void LibraryForm::_back_button() {
246         switch (_state) {
247         case STATE_ALBUM:
248                 _view_button();
249                 break;
250         case STATE_TRACK:
251                 __fill_model(_model, _lib->getAlbumsForArtist(_current_artist));
252                 _state = STATE_ALBUM;
253                 ui->listLabel->setText(QString("Albums by \"%1\"").arg(_current_artist));
254                 break;
255         case STATE_PLAYLIST_TRACK:
256                 _playlists_button();
257         default:
258                 return;
259         }
260 }
261
262 void LibraryForm::_playlists_button() {
263         QList<QString> playlists = _lib->getPlaylistsNames();
264         __fill_model(_model, playlists);
265         ui->listView->setModel(_model);
266         _state = STATE_PLAYLIST;
267         ui->backButton->hide();
268         ui->listLabel->setText("Playlists");
269         ui->addButton->hide();
270         ui->deleteButton->show();
271         ui->useButton->hide();
272 }
273
274 void LibraryForm::_delete_button() {
275         if (_state == STATE_PLAYLIST_TRACK) {
276                 QModelIndexList selected = ui->listView->selectionModel()->selectedIndexes();
277                 ui->listView->selectionModel()->clearSelection();
278                 QQueue<int> to_delete;
279                 foreach (QModelIndex id, selected) {
280                         to_delete.append(id.row());
281                 }
282                 qSort(to_delete);
283                 int count = to_delete.count();
284                 for (int i = count-1; i >= 0; i--) {
285                         _current_playlist.removeTrackAt(to_delete.at(i));
286                         qDebug() << "Removing from" << _current_playlist.name() << to_delete.at(i);
287                 }
288                 _current_tracks = _current_playlist.tracks();
289                 _lib->savePlaylist(_current_playlist);
290                 __fill_model_tracks(_model, _current_tracks);
291         } else if (_state == STATE_PLAYLIST) {
292                 QModelIndexList selected = ui->listView->selectionModel()->selectedIndexes();
293                 QQueue<int> to_delete;
294                 foreach (QModelIndex id, selected) {
295                         to_delete.append(id.row());
296                 }
297                 qSort(to_delete);
298                 int count = to_delete.count();
299                 for (int i = count-1; i >= 0; i--) {
300                         QString name = _model->item(to_delete.at(i))->text();
301                         if (name != _CURRENT_PLAYLIST_SUBST_) {
302                                 qDebug() << "deleting " << name;
303                                 _lib->removePlaylist(name);
304                                 _model->removeRow(to_delete.at(i));
305                         }
306                 }
307         }
308 }
309
310 void LibraryForm::_delete_track(Track track) {
311         Playlist current = _lib->getCurrentPlaylist();
312         current.removeTrack(track);
313         _lib->saveCurrentPlaylist(current);
314 }
315
316 void LibraryForm::_use_button() {
317         _lib->saveCurrentPlaylist(_current_playlist);
318         _current_playlist = _lib->getCurrentPlaylist();
319 }
320
321 void LibraryForm::search(QString &pattern) {
322         _search_pattern = pattern;
323         _search_current_id = -1;
324         nextItem();
325 }
326
327 void LibraryForm::nextItem() {
328         QString data = _model->index(_search_current_id, 0).data().toString();
329         for (int i = _search_current_id+1; i < _model->rowCount(); i++) {
330                 data = _model->index(i, 0).data().toString();
331                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
332                         _search_current_id = i;
333                         break;
334                 }
335         }
336         QModelIndex id = _model->index(_search_current_id, 0);
337         ui->listView->selectionModel()->clearSelection();
338         ui->listView->selectionModel()->select(id, QItemSelectionModel::Select);
339         ui->listView->scrollTo(id);
340 }
341
342 void LibraryForm::prevItem() {
343         QString data = _model->index(_search_current_id, 0).data().toString();
344         for (int i = _search_current_id-1; i >= 0; i--) {
345                 data = _model->index(i, 0).data().toString();
346                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
347                         _search_current_id = i;
348                         break;
349                 }
350         }
351         QModelIndex id = _model->index(_search_current_id, 0);
352         ui->listView->selectionModel()->clearSelection();
353         ui->listView->selectionModel()->select(id, QItemSelectionModel::Select);
354         ui->listView->scrollTo(id);
355 }
356
357 void LibraryForm::cancelSearch() {
358         _search_pattern = "";
359         ui->listView->selectionModel()->clearSelection();
360 }
361
362 void LibraryForm::refresh() {
363         switch (_state) {
364         case STATE_ARTIST:
365                 _view_button();
366                 break;
367         case STATE_ALBUM:
368                 __fill_model(_model, _lib->getAlbumsForArtist(_current_artist));
369                 break;
370         case STATE_PLAYLIST:
371                 _playlists_button();
372                 break;
373         case STATE_DYNAMIC:
374                 _dynamic_button();
375                 break;
376         case STATE_PLAYLIST_TRACK:
377                 _current_playlist = _lib->getPlaylist(_current_playlist.name());
378                 _current_tracks = _current_playlist.tracks();
379                 __fill_model_tracks(_model, _current_tracks);
380                 break;
381         case STATE_TRACK:
382                 _current_tracks = _lib->getTracksForAlbum(_current_album, _current_artist);
383                 __fill_model_tracks(_model, _current_tracks);
384                 break;
385         }
386 }