Now track without tags has title from file basename
[someplayer] / src / playerform.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 "playerform.h"
21 #include "ui_playerform.h"
22 #include "library.h"
23 #include "player/player.h"
24 #include <QDebug>
25 #include <QTime>
26 #include <QSlider>
27 #include "trackrenderer.h"
28 #include <QResource>
29 #include "playlistdialog.h"
30
31 using namespace SomePlayer::DataObjects;
32 using namespace SomePlayer::Playback;
33
34 inline void __fill_list(QStandardItemModel *_model, Playlist playlist) {
35         _model->clear();
36         QList<Track> tracks = playlist.tracks();
37         int count = tracks.count();
38         _model->setRowCount(count);
39         for (int i = 0; i < count; i++) {
40                 TrackMetadata meta = tracks.at(i).metadata();
41                 QString t = meta.title()+"#_#"+meta.artist()+"#_#"+meta.album();
42                 _model->setItem(i, 0, new QStandardItem(t));
43         }
44 }
45
46 PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
47     QWidget(parent),
48     ui(new Ui::PlayerForm)
49 {
50         _lib = lib;
51         _player = new Player(this);
52         _time = new QTime();
53         ui->setupUi(this);
54         if (_player->random()) {
55                 ui->randomButton->setIcon(QIcon(":/icons/random_active.png"));
56         } else {
57                 ui->randomButton->setIcon(QIcon(":/icons/random_inactive.png"));
58         }
59         if (_player->repeat()) {
60                 ui->repeatButton->setIcon(QIcon(":/icons/repeat_active.png"));
61         } else {
62                 ui->repeatButton->setIcon(QIcon(":/icons/repeat_inactive.png"));
63         }
64         _seek_slider = new QSlider(Qt::Horizontal);
65         _seek_slider->setEnabled(false);
66         ui->progressLayout->insertWidget(1, _seek_slider);
67         _seek_slider->setTracking(false);
68         _model = new QStandardItemModel(0, 2, this);
69         ui->playlistView->setModel(_model);
70         _context_menu = new QMenu(ui->playlistView);
71         QAction *delete_action = _context_menu->addAction("Delete");
72         QAction *enqueue_action = _context_menu->addAction("Enqueue");
73         QAction *add_to_favorites = _context_menu->addAction("Add to favorites");
74         QAction *add_to_playlists = _context_menu->addAction("Add to playlists");
75
76         _track_renderer = new TrackRenderer(this);
77         ui->playlistView->setItemDelegateForColumn(0, _track_renderer);
78
79         _tag_resolver = new TagResolver(this);
80
81         connect(ui->libraryButton, SIGNAL(clicked()), this, SLOT(_library()));
82         connect(ui->viewButton, SIGNAL(clicked()), this, SLOT(_toggle_view()));
83         connect(ui->playlistView, SIGNAL(clicked(QModelIndex)), this, SLOT(_process_click(QModelIndex)));
84         connect(ui->playpauseButton, SIGNAL(clicked()), _player, SLOT(toggle()));
85         connect(ui->stopButton, SIGNAL(clicked()), _player, SLOT(stop()));
86         connect(ui->nextButton, SIGNAL(clicked()), _player, SLOT(next()));
87         connect(ui->prevButton, SIGNAL(clicked()), _player, SLOT(prev()));
88         connect(_player, SIGNAL(trackChanged(Track)), this, SLOT(_track_changed(Track)));
89         connect(_player, SIGNAL(tick(int,int)), this, SLOT(_tick(int,int)));
90         connect(ui->randomButton, SIGNAL(clicked()), this, SLOT(_toggle_random()));
91         connect(ui->repeatButton, SIGNAL(clicked()), this, SLOT(_toggle_repeat()));
92         connect(_seek_slider, SIGNAL(sliderReleased()), this, SLOT(_slider_released()));
93         connect(ui->playlistView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(_custom_context_venu_requested(QPoint)));
94         connect(delete_action, SIGNAL(triggered()), this, SLOT(_delete_track()));
95         connect(enqueue_action, SIGNAL(triggered()), this, SLOT(_enqueue_track()));
96         connect(add_to_favorites, SIGNAL(triggered()), this, SLOT(_add_to_favorites()));
97         connect(add_to_playlists, SIGNAL(triggered()), this, SLOT(_add_to_playlists()));
98         connect(_player, SIGNAL(stateChanged(PlayerState)), this, SLOT(_state_changed(PlayerState)));
99         connect(_player, SIGNAL(trackDone(Track)), _lib, SLOT(updateTrackCount(Track)));
100         connect(_tag_resolver, SIGNAL(decoded(Track)), this, SLOT(_track_decoded(Track)));
101 }
102
103 PlayerForm::~PlayerForm()
104 {
105     delete ui;
106 }
107
108 void PlayerForm::_library() {
109         emit library();
110 }
111
112 void PlayerForm::reload() {
113         if (ui->stackedWidget->currentIndex() == 1) {
114                 emit hideSearchPanel();
115         }
116         _current_playlist = _lib->getCurrentPlaylist();
117         _player->setPlaylist(_current_playlist);
118         __fill_list(_model, _current_playlist);
119 }
120
121 void PlayerForm::_toggle_view() {
122         int index = ui->stackedWidget->currentIndex();
123         index = (!index % 2);
124         if (index) {
125                 ui->viewButton->setIcon(QIcon(":/icons/playlist.png"));
126                 emit hideSearchPanel();
127         } else {
128                 ui->viewButton->setIcon(QIcon(":/icons/playback.png"));
129                 emit showSearchPanel();
130         }
131         ui->stackedWidget->setCurrentIndex(index);
132 }
133
134 void PlayerForm::_process_click(QModelIndex index) {
135         int id = index.row();
136         _player->stop();
137         _player->setTrackId(id);
138         _player->play();
139         _track_renderer->setActiveRow(id);
140         ui->playlistView->hide();
141         ui->playlistView->show();
142 }
143
144 void PlayerForm::_track_changed(Track track) {
145         int id = _current_playlist.tracks().indexOf(track);
146         QModelIndex index = _model->index(id, 0);
147         ui->playlistView->setCurrentIndex(index);
148         ui->playlistView->scrollTo(index);
149         _track_renderer->setActiveRow(id);
150         ui->playlistView->hide();
151         ui->playlistView->show();
152         _display_track(track);
153 }
154
155 void PlayerForm::_display_track(Track track) {
156         ui->countLabel->setText(QString("%1/%2").
157                                                         arg(_current_playlist.tracks().indexOf(track)+1).
158                                                         arg(_current_playlist.tracks().count()));
159         ui->titleLabel->setText(QString("<h3>%1</h3>").arg(track.metadata().title()));
160         ui->artistAlbumLabel->setText(QString("<b>%1</b><br/>%2").
161                                                                   arg(track.metadata().artist()).
162                                                                   arg(track.metadata().album()));
163         _seek_slider->setMinimum(0);
164         _seek_slider->setMaximum(track.metadata().length());
165         _tick(0, track.metadata().length());
166 }
167
168 void PlayerForm::_tick(int done, int all) {
169         _time->setHMS(0, all/60, all%60);
170         ui->allTimeLabel->setText(_time->toString("mm:ss"));
171         _time->setHMS(0, done/60, done%60);
172         ui->doneTimeLabel->setText(_time->toString("mm:ss"));
173         _seek_slider->setValue(done);
174 }
175
176 void PlayerForm::_slider_released() {
177         _player->seek(_seek_slider->value());
178 }
179
180 void PlayerForm::_custom_context_venu_requested(const QPoint &pos) {
181         _context_menu->exec(pos);
182 }
183
184 void PlayerForm::_delete_track() {
185         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
186         int id = idx.first().row();
187         _current_playlist.removeTrackAt(id);
188         _lib->saveCurrentPlaylist(_current_playlist);
189         reload();
190 }
191
192 void PlayerForm::_enqueue_track() {
193         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
194         int id = idx.first().row();
195         _player->enqueue(id);
196 }
197
198 void PlayerForm::_add_to_favorites() {
199         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
200         int id = idx.first().row();
201         _lib->addToFavorites(_current_playlist.tracks().at(id));
202 }
203
204 void PlayerForm::_state_changed(PlayerState state) {
205         if (state == PLAYER_PLAYING) {
206                 ui->playpauseButton->setIcon(QIcon(":/icons/pause.png"));
207                 _seek_slider->setEnabled(true);
208         } else {
209                 if (state == PLAYER_STOPPED) {
210                         _seek_slider->setValue(0);
211                         ui->doneTimeLabel->setText("00:00");
212                         _seek_slider->setEnabled(false);
213                 }
214                 ui->playpauseButton->setIcon(QIcon(":/icons/play.png"));
215         }
216 }
217
218 void PlayerForm::_toggle_random() {
219         _player->toggleRandom();
220         if (_player->random()) {
221                 ui->randomButton->setIcon(QIcon(":/icons/random_active.png"));
222         } else {
223                 ui->randomButton->setIcon(QIcon(":/icons/random_inactive.png"));
224         }
225 }
226
227 void PlayerForm::_toggle_repeat() {
228         _player->toggleRepeat();
229         if (_player->repeat()) {
230                 ui->repeatButton->setIcon(QIcon(":/icons/repeat_active.png"));
231         } else {
232                 ui->repeatButton->setIcon(QIcon(":/icons/repeat_inactive.png"));
233         }
234 }
235
236 void PlayerForm::search(QString &pattern) {
237         _search_pattern = pattern;
238         _search_current_id = -1;
239         nextItem();
240 }
241
242 void PlayerForm::nextItem() {
243         QString data = _model->index(_search_current_id, 0).data().toString();
244         for (int i = _search_current_id+1; i < _model->rowCount(); i++) {
245                 data = _model->index(i, 0).data().toString();
246                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
247                         _search_current_id = i;
248                         break;
249                 }
250         }
251         QModelIndex id = _model->index(_search_current_id, 0);
252         _track_renderer->setSearchRow(_search_current_id);
253         ui->playlistView->scrollTo(id);
254         ui->playlistView->hide();
255         ui->playlistView->show();
256 }
257
258 void PlayerForm::prevItem() {
259         QString data = _model->index(_search_current_id, 0).data().toString();
260         for (int i = _search_current_id-1; i >= 0; i--) {
261                 data = _model->index(i, 0).data().toString();
262                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
263                         _search_current_id = i;
264                         break;
265                 }
266         }
267         QModelIndex id = _model->index(_search_current_id, 0);
268         _track_renderer->setSearchRow(_search_current_id);
269         ui->playlistView->scrollTo(id);
270         ui->playlistView->hide();
271         ui->playlistView->show();
272 }
273
274 void PlayerForm::cancelSearch() {
275         _search_pattern = "";
276         _track_renderer->setSearchRow(-1);
277         ui->playlistView->scrollTo(_model->index(_track_renderer->activeRow(), 0));
278         ui->playlistView->hide();
279         ui->playlistView->show();
280 }
281
282 void PlayerForm::addFiles(QList<QString> files) {
283         _tag_resolver->decode(files);
284 }
285
286 void PlayerForm::_track_decoded(Track track) {
287         _current_playlist.addTrack(track);
288         __fill_list(_model, _current_playlist);
289         _lib->saveCurrentPlaylist(_current_playlist);
290         _player->setPlaylist(_current_playlist);
291 }
292
293 void PlayerForm::_add_to_playlists() {
294         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
295         int id = idx.first().row();
296
297         QList<QString> names = _lib->getPlaylistsNames();
298         names.removeOne(_CURRENT_PLAYLIST_SUBST_);
299         PlaylistDialog dialog(names, this);
300         dialog.exec();
301         QList<QString> selected = dialog.selected();
302         foreach (QString name, selected) {
303                 Playlist pl = _lib->getPlaylist(name);
304                 pl.addTrack(_current_playlist.tracks().at(id));
305                 _lib->savePlaylist(pl);
306         }
307 }