Adding files from filesystem to current playlist
[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                         _delete_track(_current_tracks.at(id.row()));
281                         to_delete.append(id.row());
282                 }
283                 qSort(to_delete);
284                 int count = to_delete.count();
285                 for (int i = count-1; i >= 0; i--) {
286                         _current_tracks.removeAt(to_delete.at(i));
287                 }
288                 __fill_model_tracks(_model, _current_tracks);
289         } else if (_state == STATE_PLAYLIST) {
290                 QModelIndexList selected = ui->listView->selectionModel()->selectedIndexes();
291                 QQueue<int> to_delete;
292                 foreach (QModelIndex id, selected) {
293                         to_delete.append(id.row());
294                 }
295                 qSort(to_delete);
296                 int count = to_delete.count();
297                 for (int i = count-1; i >= 0; i--) {
298                         QString name = _model->item(to_delete.at(i))->text();
299                         if (name != _CURRENT_PLAYLIST_SUBST_) {
300                                 qDebug() << "deleting " << name;
301                                 _lib->removePlaylist(name);
302                                 _model->removeRow(to_delete.at(i));
303                         }
304                 }
305         }
306 }
307
308 void LibraryForm::_delete_track(Track track) {
309         Playlist current = _lib->getCurrentPlaylist();
310         current.removeTrack(track);
311         _lib->saveCurrentPlaylist(current);
312 }
313
314 void LibraryForm::_use_button() {
315         _lib->saveCurrentPlaylist(_current_playlist);
316 }
317
318 void LibraryForm::search(QString &pattern) {
319         _search_pattern = pattern;
320         _search_current_id = -1;
321         nextItem();
322 }
323
324 void LibraryForm::nextItem() {
325         QString data = _model->index(_search_current_id, 0).data().toString();
326         for (int i = _search_current_id+1; i < _model->rowCount(); i++) {
327                 data = _model->index(i, 0).data().toString();
328                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
329                         _search_current_id = i;
330                         break;
331                 }
332         }
333         QModelIndex id = _model->index(_search_current_id, 0);
334         ui->listView->selectionModel()->clearSelection();
335         ui->listView->selectionModel()->select(id, QItemSelectionModel::Select);
336         ui->listView->scrollTo(id);
337 }
338
339 void LibraryForm::prevItem() {
340         QString data = _model->index(_search_current_id, 0).data().toString();
341         for (int i = _search_current_id-1; i >= 0; i--) {
342                 data = _model->index(i, 0).data().toString();
343                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
344                         _search_current_id = i;
345                         break;
346                 }
347         }
348         QModelIndex id = _model->index(_search_current_id, 0);
349         ui->listView->selectionModel()->clearSelection();
350         ui->listView->selectionModel()->select(id, QItemSelectionModel::Select);
351         ui->listView->scrollTo(id);
352 }
353
354 void LibraryForm::cancelSearch() {
355         _search_pattern = "";
356         ui->listView->selectionModel()->clearSelection();
357 }