Version 1.3.5
[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 <QTime>
25 #include <QSlider>
26 #include "trackrenderer.h"
27 #include <QResource>
28 #include "playlistdialog.h"
29 #include "edittagsdialog.h"
30 #include <QSpacerItem>
31 #include "config.h"
32
33 using namespace SomePlayer::DataObjects;
34 using namespace SomePlayer::Playback;
35 using namespace SomePlayer::Storage;
36
37 inline void __fill_list(QStandardItemModel *_model, Playlist playlist) {
38         _model->clear();
39         QList<Track> tracks = playlist.tracks();
40         int count = tracks.count();
41         _model->setRowCount(count);
42         QTime time;
43         for (int i = 0; i < count; i++) {
44                 TrackMetadata meta = tracks.at(i).metadata();
45                 time.setHMS(0, meta.length()/60, meta.length() % 60);
46                 QString t = meta.title()+"#_#"+meta.artist()+"#_#"+meta.album()+"#_#"+time.toString("mm:ss");
47                 _model->setItem(i, 0, new QStandardItem(t));
48         }
49 }
50
51 PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
52     QWidget(parent),
53     ui(new Ui::PlayerForm)
54 {
55         _lib = lib;
56         _player = new Player(this);
57         _time = new QTime();
58         ui->setupUi(this);
59         if (_player->random()) {
60                 ui->randomButton->setIcon(QIcon(":/icons/white/random_active.png"));
61         } else {
62                 ui->randomButton->setIcon(QIcon(":/icons/white/random_inactive.png"));
63         }
64         if (_player->repeat()) {
65                 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_active.png"));
66         } else {
67                 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_inactive.png"));
68         }
69         ui->volumeSlider->setMinimum(0);
70         ui->volumeSlider->setMaximum(100);
71         ui->volumeSlider->hide();
72         ui->seekSlider->setEnabled(false);
73         ui->progressLayout->removeItem(ui->seekSpacer);
74         _tools_widget = new ToolsWidget(this);
75         ui->toolsLayout->insertWidget(0, _tools_widget);
76         _tools_widget->hide();
77         _model = new QStandardItemModel(0, 2, this);
78         ui->playlistView->setModel(_model);
79         _context_menu = new QMenu(ui->playlistView);
80         QAction *delete_action = _context_menu->addAction("Delete");
81         QAction *add_to_favorites = _context_menu->addAction("Add to favorites");
82         QAction *enqueue_action = _context_menu->addAction("Enqueue");
83         QAction *add_to_playlists = _context_menu->addAction("Add to playlists");
84         QAction *edit_tags = _context_menu->addAction("Edit tags");
85
86         _track_renderer = new TrackRenderer(this);
87         _track_renderer->setActiveRow(-1);
88         _track_renderer->setSearchRow(-1);
89         ui->playlistView->setItemDelegateForColumn(0, _track_renderer);
90
91         _tag_resolver = new TagResolver(this);
92
93         connect(ui->libraryButton, SIGNAL(clicked()), this, SLOT(_library()));
94         connect(ui->viewButton, SIGNAL(clicked()), this, SLOT(_toggle_view()));
95         connect(ui->playlistView, SIGNAL(clicked(QModelIndex)), this, SLOT(_process_click(QModelIndex)));
96         connect(ui->playpauseButton, SIGNAL(clicked()), _player, SLOT(toggle()));
97         connect(ui->nextButton, SIGNAL(clicked()), _player, SLOT(next()));
98         connect(ui->stopButton, SIGNAL(clicked()), _player, SLOT(stop()));
99         connect(ui->prevButton, SIGNAL(clicked()), _player, SLOT(prev()));
100         connect(_player, SIGNAL(trackChanged(Track)), this, SLOT(_track_changed(Track)));
101         connect(_player, SIGNAL(tick(int,int)), this, SLOT(_tick(int,int)));
102         connect(ui->randomButton, SIGNAL(clicked()), this, SLOT(_toggle_random()));
103         connect(ui->repeatButton, SIGNAL(clicked()), this, SLOT(_toggle_repeat()));
104         connect(ui->seekSlider, SIGNAL(sliderMoved(int)), _player, SLOT(seek(int)));
105         connect(ui->volumeSlider, SIGNAL(sliderMoved(int)), _player, SLOT(setVolume(int)));
106         connect(ui->playlistView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(_custom_context_menu_requested(QPoint)));
107         connect(delete_action, SIGNAL(triggered()), this, SLOT(_delete_track()));
108         connect(enqueue_action, SIGNAL(triggered()), this, SLOT(_enqueue_track()));
109         connect(add_to_favorites, SIGNAL(triggered()), this, SLOT(_add_to_favorites()));
110         connect(add_to_playlists, SIGNAL(triggered()), this, SLOT(_add_to_playlists()));
111         connect(edit_tags, SIGNAL(triggered()), this, SLOT(_edit_tags()));
112         connect(_player, SIGNAL(stateChanged(PlayerState)), this, SLOT(_state_changed(PlayerState)));
113         connect(_player, SIGNAL(trackDone(Track)), _lib, SLOT(updateTrackCount(Track)));
114         connect(_tag_resolver, SIGNAL(decoded(Track)), this, SLOT(_track_decoded(Track)));
115         connect(ui->volumeButton, SIGNAL(clicked()), this, SLOT(_toggle_volume()));
116         connect(ui->moreButton, SIGNAL(clicked()), this, SLOT(_tools_widget_toggle()));
117         connect(_tools_widget, SIGNAL(search(QString)), this, SLOT(search(QString)));
118         connect(_tools_widget, SIGNAL(nextSearch()), this, SLOT(nextItem()));
119         connect(_tools_widget, SIGNAL(prevSearch()), this, SLOT(prevItem()));
120         connect(_tools_widget, SIGNAL(toggleFullscreen(bool)), this, SIGNAL(fullscreen(bool)));
121
122         // dbus
123         _dbusadaptor = new DBusAdaptop(_player);
124         QDBusConnection connection = QDBusConnection::sessionBus();
125         bool ret = connection.registerService(_SERVICE_NAME_);
126         ret = connection.registerObject("/", _player);
127 }
128
129 PlayerForm::~PlayerForm()
130 {
131     delete ui;
132 }
133
134 void PlayerForm::_library() {
135         emit library();
136 }
137
138 void PlayerForm::reload(bool reread) {
139         if (reread) {
140                 _current_playlist = _lib->getCurrentPlaylist();
141                 _player->setPlaylist(_current_playlist);
142                 __fill_list(_model, _current_playlist);
143         }
144 }
145
146 void PlayerForm::_toggle_view() {
147         int index = ui->stackedWidget->currentIndex();
148         index = (!index % 2);
149         if (index) {
150                 ui->viewButton->setIcon(QIcon(":/icons/white/playlist.png"));
151                 ui->moreButton->setEnabled(false);
152         } else {
153                 ui->viewButton->setIcon(QIcon(":/icons/white/playback.png"));
154                 ui->moreButton->setEnabled(true);
155         }
156         ui->stackedWidget->setCurrentIndex(index);
157 }
158
159 void PlayerForm::_process_click(QModelIndex index) {
160         int id = index.row();
161         _player->stop();
162         _player->setTrackId(id);
163         _player->play();
164         _track_renderer->setActiveRow(id);
165         ui->playlistView->hide();
166         ui->playlistView->show();
167 }
168
169 void PlayerForm::_track_changed(Track track) {
170         int id = _current_playlist.tracks().indexOf(track);
171         QModelIndex index = _model->index(id, 0);
172         ui->playlistView->setCurrentIndex(index);
173         ui->playlistView->scrollTo(index);
174         _track_renderer->setActiveRow(id);
175         ui->playlistView->hide();
176         ui->playlistView->show();
177         _display_track(track);
178 }
179
180 void PlayerForm::_display_track(Track track) {
181         ui->countLabel->setText(QString("%1/%2").
182                                                         arg(_current_playlist.tracks().indexOf(track)+1).
183                                                         arg(_current_playlist.tracks().count()));
184         ui->titleLabel->setText(QString("<h3>%1</h3>").arg(track.metadata().title()));
185         ui->artistAlbumLabel->setText(QString("<b>%1</b><br/>%2").
186                                                                   arg(track.metadata().artist()).
187                                                                   arg(track.metadata().album()));
188         ui->seekSlider->setMinimum(0);
189         ui->seekSlider->setMaximum(track.metadata().length());
190         _tick(0, track.metadata().length());
191 }
192
193 void PlayerForm::_tick(int done, int all) {
194         _time->setHMS(0, all/60, all%60);
195         ui->allTimeLabel->setText(_time->toString("mm:ss"));
196         _time->setHMS(0, done/60, done%60);
197         ui->doneTimeLabel->setText(_time->toString("mm:ss"));
198         ui->seekSlider->setValue(done);
199 }
200
201 void PlayerForm::_slider_released() {
202         _player->seek(ui->seekSlider->value());
203 }
204
205 void PlayerForm::_custom_context_menu_requested(const QPoint &pos) {
206         _context_menu->exec(pos);
207 }
208
209 void PlayerForm::_delete_track() {
210         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
211         int id = idx.first().row();
212         int aid = _track_renderer->activeRow();
213         if (aid > id) {
214                 _track_renderer->setActiveRow(aid-1);
215         } else if (aid == id) {
216                 _track_renderer->setActiveRow(-1);
217         }
218         _current_playlist.removeTrackAt(id);
219         _lib->saveCurrentPlaylist(_current_playlist);
220         reload(true);
221 }
222
223 void PlayerForm::_enqueue_track() {
224         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
225         int id = idx.first().row();
226         _player->enqueue(id);
227 }
228
229 void PlayerForm::_add_to_favorites() {
230         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
231         int id = idx.first().row();
232         _lib->addToFavorites(_current_playlist.tracks().at(id));
233 }
234
235 void PlayerForm::_state_changed(PlayerState state) {
236         if (state == PLAYER_PLAYING) {
237                 ui->playpauseButton->setIcon(QIcon(":/icons/white/pause.png"));
238                 ui->seekSlider->setEnabled(true);
239         } else {
240                 if (state == PLAYER_STOPPED) {
241                         ui->seekSlider->setValue(0);
242                         ui->doneTimeLabel->setText("00:00");
243                         ui->seekSlider->setEnabled(false);
244                 }
245                 ui->playpauseButton->setIcon(QIcon(":/icons/white/play.png"));
246         }
247 }
248
249 void PlayerForm::_toggle_random() {
250         _player->toggleRandom();
251         if (_player->random()) {
252                 ui->randomButton->setIcon(QIcon(":/icons/white/random_active.png"));
253         } else {
254                 ui->randomButton->setIcon(QIcon(":/icons/white/random_inactive.png"));
255         }
256 }
257
258 void PlayerForm::_toggle_repeat() {
259         _player->toggleRepeat();
260         if (_player->repeat()) {
261                 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_active.png"));
262         } else {
263                 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_inactive.png"));
264         }
265 }
266
267 void PlayerForm::search(QString pattern) {
268         _search_pattern = pattern;
269         _search_current_id = -1;
270         nextItem();
271 }
272
273 void PlayerForm::nextItem() {
274         QString data = _model->index(_search_current_id, 0).data().toString();
275         for (int i = _search_current_id+1; i < _model->rowCount(); i++) {
276                 data = _model->index(i, 0).data().toString();
277                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
278                         _search_current_id = i;
279                         break;
280                 }
281         }
282         QModelIndex id = _model->index(_search_current_id, 0);
283         _track_renderer->setSearchRow(_search_current_id);
284         ui->playlistView->scrollTo(id);
285         ui->playlistView->hide();
286         ui->playlistView->show();
287 }
288
289 void PlayerForm::prevItem() {
290         QString data = _model->index(_search_current_id, 0).data().toString();
291         for (int i = _search_current_id-1; i >= 0; i--) {
292                 data = _model->index(i, 0).data().toString();
293                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
294                         _search_current_id = i;
295                         break;
296                 }
297         }
298         QModelIndex id = _model->index(_search_current_id, 0);
299         _track_renderer->setSearchRow(_search_current_id);
300         ui->playlistView->scrollTo(id);
301         ui->playlistView->hide();
302         ui->playlistView->show();
303 }
304
305 void PlayerForm::cancelSearch() {
306         _search_pattern = "";
307         _track_renderer->setSearchRow(-1);
308         ui->playlistView->scrollTo(_model->index(_track_renderer->activeRow(), 0));
309         ui->playlistView->hide();
310         ui->playlistView->show();
311 }
312
313 void PlayerForm::addFiles(QList<QString> files) {
314         _tag_resolver->decode(files);
315 }
316
317 void PlayerForm::_track_decoded(Track track) {
318         _current_playlist.addTrack(track);
319         __fill_list(_model, _current_playlist);
320         _lib->saveCurrentPlaylist(_current_playlist);
321         _player->setPlaylist(_current_playlist);
322 }
323
324 void PlayerForm::_add_to_playlists() {
325         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
326         int id = idx.first().row();
327
328         QList<QString> names = _lib->getPlaylistsNames();
329         names.removeOne(_CURRENT_PLAYLIST_SUBST_);
330         PlaylistDialog dialog(names, this);
331         if (dialog.exec() == QDialog::Accepted) {
332                 QList<QString> selected = dialog.selected();
333                 foreach (QString name, selected) {
334                         Playlist pl = _lib->getPlaylist(name);
335                         pl.addTrack(_current_playlist.tracks().at(id));
336                         _lib->savePlaylist(pl);
337                 }
338         }
339 }
340
341 void PlayerForm::_edit_tags() {
342         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
343         Track track = _current_playlist.tracks().at(idx.first().row());
344
345         EditTagsDialog dialog(this);
346         dialog.setTrackMetadata(track.metadata());
347         if (dialog.exec() == QDialog::Accepted) {
348                 track.setMetadata(dialog.meta());
349                 _lib->updateTrackMetadata(track);
350                 reload(true);
351         }
352 }
353
354 void PlayerForm::stop() {
355         _player->stop();
356 }
357
358 void PlayerForm::_toggle_volume() {
359         if (ui->volumeSlider->isVisible()) {
360                 ui->volumeSlider->hide();
361         } else {
362                 ui->volumeSlider->show();
363                 ui->volumeSlider->setValue(_player->volume());
364         }
365 }
366
367 void PlayerForm::_volume_changed() {
368         int value = ui->volumeSlider->value();
369         _player->setVolume(value);
370 }
371
372 void PlayerForm::landscapeMode() {
373         ui->progressLayout->removeItem(ui->seekSpacer);
374         ui->progressLayout->insertWidget(1, ui->seekSlider);
375         ui->progressWidget->setVisible(false);
376 }
377
378 void PlayerForm::portraitMode() {
379         ui->progressLayout->insertSpacerItem(1, ui->seekSpacer);
380         ui->progressWidget->layout()->addWidget(ui->seekSlider);
381         ui->progressWidget->setVisible(true);
382 }
383
384 void PlayerForm::_tools_widget_toggle() {
385         if (_tools_widget->isVisible()) {
386                 ui->moreButton->setIcon(QIcon(":/icons/white/more.png"));
387                 _tools_widget->hide();
388                 cancelSearch();
389         } else {
390                 ui->moreButton->setIcon(QIcon(":/icons/white/unmore.png"));
391                 _tools_widget->show();
392         }
393 }