Fixed settings save bug
[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         ui->viewButton->setIcon(QIcon(":/icons/white/playback.png"));
122
123         // dbus
124         _dbusadaptor = new DBusAdaptop(_player);
125         QDBusConnection connection = QDBusConnection::sessionBus();
126         bool ret = connection.registerService(_SERVICE_NAME_);
127         ret = connection.registerObject("/", _player);
128 }
129
130 PlayerForm::~PlayerForm()
131 {
132     delete ui;
133 }
134
135 void PlayerForm::_library() {
136         emit library();
137 }
138
139 void PlayerForm::reload(bool reread) {
140         if (reread) {
141                 _current_playlist = _lib->getCurrentPlaylist();
142                 _player->setPlaylist(_current_playlist);
143                 __fill_list(_model, _current_playlist);
144         }
145 }
146
147 void PlayerForm::_toggle_view() {
148         int index = ui->stackedWidget->currentIndex();
149         index = (!index % 2);
150         if (index) {
151                 ui->viewButton->setIcon(QIcon(":/icons/white/playlist.png"));
152                 ui->moreButton->setEnabled(false);
153         } else {
154                 ui->viewButton->setIcon(QIcon(":/icons/white/playback.png"));
155                 ui->moreButton->setEnabled(true);
156         }
157         ui->stackedWidget->setCurrentIndex(index);
158 }
159
160 void PlayerForm::_process_click(QModelIndex index) {
161         int id = index.row();
162         _player->stop();
163         _player->setTrackId(id);
164         _player->play();
165         _track_renderer->setActiveRow(id);
166         ui->playlistView->hide();
167         ui->playlistView->show();
168 }
169
170 void PlayerForm::_track_changed(Track track) {
171         int id = _current_playlist.tracks().indexOf(track);
172         QModelIndex index = _model->index(id, 0);
173         ui->playlistView->setCurrentIndex(index);
174         ui->playlistView->scrollTo(index);
175         _track_renderer->setActiveRow(id);
176         ui->playlistView->hide();
177         ui->playlistView->show();
178         _display_track(track);
179 }
180
181 void PlayerForm::_display_track(Track track) {
182         ui->countLabel->setText(QString("%1/%2").
183                                                         arg(_current_playlist.tracks().indexOf(track)+1).
184                                                         arg(_current_playlist.tracks().count()));
185         ui->titleLabel->setText(QString("<h3>%1</h3>").arg(track.metadata().title()));
186         ui->artistAlbumLabel->setText(QString("<b>%1</b><br/>%2").
187                                                                   arg(track.metadata().artist()).
188                                                                   arg(track.metadata().album()));
189         ui->seekSlider->setMinimum(0);
190         ui->seekSlider->setMaximum(track.metadata().length());
191         _tick(0, track.metadata().length());
192 }
193
194 void PlayerForm::_tick(int done, int all) {
195         _time->setHMS(0, all/60, all%60);
196         ui->allTimeLabel->setText(_time->toString("mm:ss"));
197         _time->setHMS(0, done/60, done%60);
198         ui->doneTimeLabel->setText(_time->toString("mm:ss"));
199         ui->seekSlider->setValue(done);
200 }
201
202 void PlayerForm::_slider_released() {
203         _player->seek(ui->seekSlider->value());
204 }
205
206 void PlayerForm::_custom_context_menu_requested(const QPoint &pos) {
207         _context_menu->exec(pos);
208 }
209
210 void PlayerForm::_delete_track() {
211         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
212         int id = idx.first().row();
213         int aid = _track_renderer->activeRow();
214         if (aid > id) {
215                 _track_renderer->setActiveRow(aid-1);
216         } else if (aid == id) {
217                 _track_renderer->setActiveRow(-1);
218         }
219         _current_playlist.removeTrackAt(id);
220         _lib->saveCurrentPlaylist(_current_playlist);
221         reload(true);
222 }
223
224 void PlayerForm::_enqueue_track() {
225         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
226         int id = idx.first().row();
227         _player->enqueue(id);
228 }
229
230 void PlayerForm::_add_to_favorites() {
231         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
232         int id = idx.first().row();
233         _lib->addToFavorites(_current_playlist.tracks().at(id));
234 }
235
236 void PlayerForm::_state_changed(PlayerState state) {
237         if (state == PLAYER_PLAYING) {
238                 ui->playpauseButton->setIcon(QIcon(":/icons/white/pause.png"));
239                 ui->seekSlider->setEnabled(true);
240         } else {
241                 if (state == PLAYER_STOPPED) {
242                         ui->seekSlider->setValue(0);
243                         ui->doneTimeLabel->setText("00:00");
244                         ui->seekSlider->setEnabled(false);
245                 }
246                 ui->playpauseButton->setIcon(QIcon(":/icons/white/play.png"));
247         }
248 }
249
250 void PlayerForm::_toggle_random() {
251         _player->toggleRandom();
252         if (_player->random()) {
253                 ui->randomButton->setIcon(QIcon(":/icons/white/random_active.png"));
254         } else {
255                 ui->randomButton->setIcon(QIcon(":/icons/white/random_inactive.png"));
256         }
257 }
258
259 void PlayerForm::_toggle_repeat() {
260         _player->toggleRepeat();
261         if (_player->repeat()) {
262                 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_active.png"));
263         } else {
264                 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_inactive.png"));
265         }
266 }
267
268 void PlayerForm::search(QString pattern) {
269         _search_pattern = pattern;
270         _search_current_id = -1;
271         nextItem();
272 }
273
274 void PlayerForm::nextItem() {
275         QString data = _model->index(_search_current_id, 0).data().toString();
276         for (int i = _search_current_id+1; i < _model->rowCount(); i++) {
277                 data = _model->index(i, 0).data().toString();
278                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
279                         _search_current_id = i;
280                         break;
281                 }
282         }
283         QModelIndex id = _model->index(_search_current_id, 0);
284         _track_renderer->setSearchRow(_search_current_id);
285         ui->playlistView->scrollTo(id);
286         ui->playlistView->hide();
287         ui->playlistView->show();
288 }
289
290 void PlayerForm::prevItem() {
291         QString data = _model->index(_search_current_id, 0).data().toString();
292         for (int i = _search_current_id-1; i >= 0; i--) {
293                 data = _model->index(i, 0).data().toString();
294                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
295                         _search_current_id = i;
296                         break;
297                 }
298         }
299         QModelIndex id = _model->index(_search_current_id, 0);
300         _track_renderer->setSearchRow(_search_current_id);
301         ui->playlistView->scrollTo(id);
302         ui->playlistView->hide();
303         ui->playlistView->show();
304 }
305
306 void PlayerForm::cancelSearch() {
307         _search_pattern = "";
308         _track_renderer->setSearchRow(-1);
309         ui->playlistView->scrollTo(_model->index(_track_renderer->activeRow(), 0));
310         ui->playlistView->hide();
311         ui->playlistView->show();
312 }
313
314 void PlayerForm::addFiles(QList<QString> files) {
315         _tag_resolver->decode(files);
316 }
317
318 void PlayerForm::_track_decoded(Track track) {
319         _current_playlist.addTrack(track);
320         __fill_list(_model, _current_playlist);
321         _lib->saveCurrentPlaylist(_current_playlist);
322         _player->setPlaylist(_current_playlist);
323 }
324
325 void PlayerForm::_add_to_playlists() {
326         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
327         int id = idx.first().row();
328
329         QList<QString> names = _lib->getPlaylistsNames();
330         names.removeOne(_CURRENT_PLAYLIST_SUBST_);
331         PlaylistDialog dialog(names, this);
332         if (dialog.exec() == QDialog::Accepted) {
333                 QList<QString> selected = dialog.selected();
334                 foreach (QString name, selected) {
335                         Playlist pl = _lib->getPlaylist(name);
336                         pl.addTrack(_current_playlist.tracks().at(id));
337                         _lib->savePlaylist(pl);
338                 }
339         }
340 }
341
342 void PlayerForm::_edit_tags() {
343         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
344         Track track = _current_playlist.tracks().at(idx.first().row());
345
346         EditTagsDialog dialog(this);
347         dialog.setTrackMetadata(track.metadata());
348         if (dialog.exec() == QDialog::Accepted) {
349                 track.setMetadata(dialog.meta());
350                 _lib->updateTrackMetadata(track);
351                 reload(true);
352         }
353 }
354
355 void PlayerForm::stop() {
356         _player->stop();
357 }
358
359 void PlayerForm::_toggle_volume() {
360         if (ui->volumeSlider->isVisible()) {
361                 ui->volumeSlider->hide();
362         } else {
363                 ui->volumeSlider->show();
364                 ui->volumeSlider->setValue(_player->volume());
365         }
366 }
367
368 void PlayerForm::_volume_changed() {
369         int value = ui->volumeSlider->value();
370         _player->setVolume(value);
371 }
372
373 void PlayerForm::landscapeMode() {
374         ui->progressLayout->removeItem(ui->seekSpacer);
375         ui->progressLayout->insertWidget(1, ui->seekSlider);
376         ui->progressWidget->setVisible(false);
377 }
378
379 void PlayerForm::portraitMode() {
380         ui->progressLayout->insertSpacerItem(1, ui->seekSpacer);
381         ui->progressWidget->layout()->addWidget(ui->seekSlider);
382         ui->progressWidget->setVisible(true);
383 }
384
385 void PlayerForm::_tools_widget_toggle() {
386         if (_tools_widget->isVisible()) {
387                 ui->moreButton->setIcon(QIcon(":/icons/white/more.png"));
388                 _tools_widget->hide();
389                 _tools_widget->reset();
390                 cancelSearch();
391         } else {
392                 ui->moreButton->setIcon(QIcon(":/icons/white/unmore.png"));
393                 _tools_widget->show();
394                 _tools_widget->setFocus();
395         }
396 }