Improved hw zoom keys handling
[someplayer] / src / mainwindow.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 "mainwindow.h"
21 #include "ui_mainwindow.h"
22 #include <QFileDialog>
23 #include <QMessageBox>
24 #include <QInputDialog>
25 #include <QFile>
26 #include <QDesktopWidget>
27 #include <QTranslator>
28 #include <QKeyEvent>
29
30 #include "player/player.h"
31
32 #include "library.h"
33 #include "timerdialog.h"
34 #include "equalizerdialog.h"
35 #include "saveplaylistdialog.h"
36 #include "settingsform.h"
37
38 using namespace SomePlayer::DataObjects;
39 using namespace SomePlayer::Storage;
40 using namespace SomePlayer::Playback;
41
42 MainWindow::MainWindow(QWidget *parent) :
43         QMainWindow(parent),
44         ui(new Ui::MainWindow)
45 {
46         _display_unlocked = true; // in most cases
47         Config config;
48         _library = new Library(config.applicationDir(), config.applicationDir());
49         _translator = new QTranslator(this);
50         ui->setupUi(this);
51         connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
52         connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(settings()));
53         setAnimated(true);
54         setAttribute(Qt::WA_Maemo5StackedWindow);
55         _player_form = new PlayerForm(_library, this);
56         ui->centralWidget->layout()->addWidget(_player_form);
57         _library_form = new LibraryForm(_library, this);
58         _directory_form = new DirectoryView(this);
59         _directory_form->hide();
60         _timer = new QTimer(this);
61         _equalizer_dialog = new EqualizerDialog(this);
62         _manage_library_form = new ManageLibraryForm(_library, this);
63         _settings_form = new SettingsForm(this);
64         _settings_form->hide();
65         _about_form = new AboutForm(this);
66         _about_form->hide();
67         connect(_player_form, SIGNAL(library()), this, SLOT(library()));
68         connect(_library_form, SIGNAL(refreshPlayer()), this, SLOT(player()));
69         connect(ui->actionManageLibrary, SIGNAL(triggered()), this, SLOT(_manage_library()));
70         connect(ui->actionSavePlaylist, SIGNAL(triggered()), this, SLOT(_save_playlist()));
71         connect(_player_form, SIGNAL(clearPlaylist()), this, SLOT(_clear_current_playlist()));
72         connect(ui->actionSetTimer, SIGNAL(triggered()), this, SLOT(_set_timer()));
73         connect(ui->actionEqualizer, SIGNAL(triggered()), this, SLOT(_equalizer()));
74         connect(_library, SIGNAL(done()), _library_form, SLOT(refresh()));
75         connect(_player_form, SIGNAL(refreshLibrary()), _library_form, SLOT(refresh()));
76         connect(_manage_library_form, SIGNAL(refreshLibrary()), _library_form, SLOT(refresh()));
77         connect(_timer, SIGNAL(timeout()), this, SLOT(_timeout()));
78         connect(_equalizer_dialog, SIGNAL(valueChanged(int,int)), this, SLOT(_equalizer_value_changed(int, int)));
79         connect(_equalizer_dialog, SIGNAL(equalizerEnabled()), _player_form, SLOT(enableEqualizer()));
80         connect(_equalizer_dialog, SIGNAL(equalizerDisabled()), _player_form, SLOT(disableEqualizer()));
81         connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(_orientation_changed()));
82         connect(_player_form, SIGNAL(fullscreen(bool)), this, SLOT(_fullscreen(bool)));
83         connect(_library_form, SIGNAL(addAndPlay(Track)), _player_form, SLOT(play(Track)));
84         connect(_directory_form, SIGNAL(addAndPlay(Track)), _player_form, SLOT(play(Track)));
85         connect(_player_form, SIGNAL(dirView()), _directory_form, SLOT(show()));
86         connect(_directory_form, SIGNAL(addTracks(QList<Track>)), this, SLOT(_add_tracks(QList<Track>)));
87         connect(_settings_form, SIGNAL(iconsChanged()), _player_form, SLOT(updateIcons()));
88         connect(_settings_form, SIGNAL(iconsChanged()), _library_form, SLOT(updateIcons()));
89         connect(_settings_form, SIGNAL(iconsChanged()), _manage_library_form, SLOT(updateIcons()));
90         connect(_settings_form, SIGNAL(iconsChanged()), _directory_form, SLOT(updateIcons()));
91         connect(_settings_form, SIGNAL(iconsChanged()), _about_form, SLOT(updateIcons()));
92         connect(_settings_form, SIGNAL(gradientChanged()), _player_form, SLOT(checkGradient()));
93         connect(_settings_form, SIGNAL(gradientChanged()), _library_form, SLOT(checkGradient()));
94         connect(_settings_form, SIGNAL(gradientChanged()), _directory_form, SLOT(checkGradient()));
95         connect(_settings_form, SIGNAL(libraryOptionsChanged()), _library_form, SLOT(refresh()));
96         connect(_settings_form, SIGNAL(orientationChanged()), this, SLOT(_change_orientation()));
97         connect(_settings_form, SIGNAL(translationChanged()), this, SLOT(updateTranslations()));
98         connect(_settings_form, SIGNAL(trackColorChanged()), _player_form, SLOT(updateTrackColor()));
99         connect(_settings_form, SIGNAL(hwZoomPolicyChanged()), this, SLOT(_hw_zoom_policy_changed()));
100         connect(&_dbus_client, SIGNAL(displayStateChanged(bool)), this, SLOT(_set_display_state(bool)));
101         connect(_settings_form, SIGNAL(fmtxSettingsChanged()), this, SLOT(_fmtx_settings_changed()));
102         _player_form->reload(true);
103         QString mode = config.getValue("ui/orientation").toString();
104         if (mode == "landscape") {
105                 setAttribute(Qt::WA_Maemo5LandscapeOrientation);
106                 _player_form->landscapeMode();
107                 _library_form->landscapeMode();
108                 _equalizer_dialog->landscapeMode();
109                 _directory_form->lanscapeMode();
110                 _settings_form->landscapeMode();
111         } else if (mode == "portrait") {
112                 setAttribute(Qt::WA_Maemo5PortraitOrientation);
113                 _player_form->portraitMode();
114                 _library_form->portraitMode();
115                 _equalizer_dialog->portraitMode();
116                 _directory_form->portraitMode();
117                 _settings_form->portraitMode();
118         } else if (mode == "auto") { // initialization in landscape
119                 _player_form->landscapeMode();
120                 _library_form->landscapeMode();
121                 _equalizer_dialog->landscapeMode();
122                 _directory_form->lanscapeMode();
123                 _settings_form->landscapeMode();
124                 setAttribute(Qt::WA_Maemo5AutoOrientation);
125         }
126         _library_form->updateIcons();
127         _player_form->updateIcons();
128         _manage_library_form->updateIcons();
129         _directory_form->updateIcons();
130         _player_form->checkGradient();
131         _library_form->checkGradient();
132         _directory_form->checkGradient();
133         _hw_zoom_policy_changed();
134         config.setValue("fmtx/enabled", "no");
135         setWindowTitle("SomePlayer");
136 }
137
138 MainWindow::~MainWindow()
139 {
140         delete _player_form;
141         delete _library_form;
142         delete ui;
143 }
144
145 void MainWindow::about() {
146         _about_form->show();
147 }
148
149 void MainWindow::player() {
150         _player_form->reload(true);
151         _orientation_changed(); // workaround
152 }
153
154 void MainWindow::library() {
155         ui->menuBar->setEnabled(true);
156         _library_form->show();
157         _orientation_changed(); // workaround
158         _manage_library_form->hide();
159 }
160
161 void MainWindow::_manage_library() {
162         _manage_library_form->refresh();
163         _manage_library_form->show();
164 }
165
166 void MainWindow::_save_playlist() {
167         QList<QString> playlists = _library->getPlaylistsNames();
168         playlists.removeOne(_CURRENT_PLAYLIST_SUBST_);
169         Playlist cur = _library->getCurrentPlaylist();
170         // construct playlist name if possible
171         QString suggest_name;
172         QList<Track> tracks = cur.tracks();
173         QString artist = tracks.at(0).metadata().artist(), album = tracks.at(0).metadata().album();
174         foreach (Track t, tracks) {
175                 if (t.metadata().album() != album)
176                         album = "";
177                 if (t.metadata().artist() != artist)
178                         artist = "";
179         }
180         if (album.isEmpty() && artist.isEmpty()) {
181                 suggest_name = "New playlist";
182         } else if (album.isEmpty()) {
183                 suggest_name = artist;
184         } else {
185                 suggest_name = QString("%1 - %2").arg(artist).arg(album);
186         }
187
188         // constructed
189         SavePlaylistDialog dialog(suggest_name, this);
190         dialog.setPlaylistNames(playlists);
191         if (dialog.exec() == QDialog::Accepted) {
192                 QString name = dialog.selectedName();
193                 bool append = false;
194                 if (playlists.contains(name)) {
195                         if (QMessageBox::question(this, "Overwrite playlist?", "Overwrite playlist \""+name+"\"?", QMessageBox::Ok, QMessageBox::Cancel)
196                                 != QMessageBox::Ok) {
197                                 return;
198                         }
199                         append = (QMessageBox::question(this, "Append to playlist?", "Playlist with name \""+name+"\" already exists.\n"
200                                                   "Dow you want to append current playlist to it?",
201                                                   QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok);
202                 }
203                 if (append) {
204                         Playlist target = _library->getPlaylist(name);
205                         QList<Track> tracks = cur.tracks();
206                         foreach (Track track, tracks) {
207                                 target.addTrack(track);
208                         }
209                         _library->savePlaylist(target);
210                 } else {
211                         cur.setName(name);
212                         _library->savePlaylist(cur);
213                 }
214         }
215 }
216
217 void MainWindow::_clear_current_playlist() {
218         Playlist playlist = _library->getCurrentPlaylist();
219         playlist.clear();
220         _library->saveCurrentPlaylist(playlist);
221         _player_form->reload(true);
222 }
223
224 void MainWindow::_set_timer() {
225         TimerDialog dialog(this);
226         dialog.init();
227         if (_timer->isActive()) {
228                 dialog.showDisable();
229                 int msec = _timeout_interval;
230                 int h = msec/3600000;
231                 int m = msec/60000 - h * 60;
232                 int s = msec/1000 - h * 3600 - m * 60;
233                 dialog.setTime(h, m, s);
234         }
235         if (QDialog::Accepted == dialog.exec()) {
236                 if (!dialog.timerDisabled()) {
237                         int h, m, s;
238                         dialog.getTime(&h, &m, &s);
239                         _timeout_interval = h*3600000+m*60000+s*1000;
240                         _timer->setInterval(1000);
241                         _timer->setSingleShot(false);
242                         _timer->start();
243                 } else if (_timer->isActive()) {
244                         _timer->stop();
245                         _player_form->hideCountdown();
246                 }
247         }
248 }
249
250 void MainWindow::_timeout() {
251         _timeout_interval -= 1000;
252         if (_timeout_interval <= 0) {
253                 _player_form->stop();
254                 _player_form->hideCountdown();
255                 _timer->stop();
256         } else {
257                 int h = _timeout_interval / 3600000;
258                 int m = (_timeout_interval / 60000) - 60*h;
259                 int s = (_timeout_interval / 1000) - 3600*h - 60*m;
260                 QString hp = h < 10 ? QString("0%1").arg(h) : QString("%1").arg(h);
261                 QString mp = m < 10 ? QString("0%1").arg(m) : QString("%1").arg(m);
262                 QString sp = s < 10 ? QString("0%1").arg(s) : QString("%1").arg(s);
263                 _player_form->showCountdown(tr("Music off: ")+hp+":"+mp+":"+sp);
264         }
265 }
266
267 void MainWindow::_equalizer() {
268         if (_player_form->isEqualizerAvailable()) {
269                 double val = 0;
270                 for (int i = 0; i < 10; i++) {
271                         _player_form->equalizerValue(i, &val);
272                         _equalizer_dialog->setValue(i, (int)(val * 10 + 0.5));
273                 }
274                 _equalizer_dialog->setEqualizerEnabled(_player_form->isEqualizerEnabled());
275                 _equalizer_dialog->reloadPresets();
276                 _equalizer_dialog->exec();
277         } else {
278                 QMessageBox::information(this, "Error", "No equalizer support. Please install gstreamer0.10-plugins-good-extra");
279         }
280 }
281
282 void MainWindow::_equalizer_value_changed(int band, int val) {
283         _player_form->setEqualizerValue(band, (val / 10.0));
284 }
285
286 void MainWindow::settings() {
287         _settings_form->show();
288 }
289
290 void MainWindow::_change_orientation() {
291         Config config;
292         QString mode = config.getValue("ui/orientation").toString();
293         if (mode == "landscape") {
294                 setAttribute(Qt::WA_Maemo5LandscapeOrientation);
295         } else if (mode == "portrait") {
296                 setAttribute(Qt::WA_Maemo5PortraitOrientation);
297         } else if (mode == "auto") {
298                 setAttribute(Qt::WA_Maemo5AutoOrientation);
299         }
300 }
301
302 void MainWindow::_orientation_changed() {
303         QRect screenGeometry = QApplication::desktop()->screenGeometry();
304         if (screenGeometry.width() > screenGeometry.height()) {
305                 _orientation = ORIENTATION_LANDSCAPE;
306                 _player_form->landscapeMode();
307                 _library_form->landscapeMode();
308                 _equalizer_dialog->landscapeMode();
309                 _directory_form->lanscapeMode();
310                 _settings_form->landscapeMode();
311         } else {
312                 _orientation = ORIENTATION_PORTRAIT;
313                 _player_form->portraitMode();
314                 _library_form->portraitMode();
315                 _equalizer_dialog->portraitMode();
316                 _directory_form->portraitMode();
317                 _settings_form->portraitMode();
318         }
319 }
320
321 void MainWindow::_fullscreen(bool f) {
322         if (f) showFullScreen();
323         else showNormal();
324 }
325
326 void MainWindow::_add_tracks(QList<Track> tracks) {
327         Playlist cur = _library->getCurrentPlaylist();
328         foreach (Track track, tracks) {
329                 cur.addTrack(track);
330         }
331         _library->saveCurrentPlaylist(cur);
332         _player_form->reload(true);
333 }
334
335 void MainWindow::updateTranslations() {
336         Config config;
337         if (config.getValue("ui/language").toString() != "en") {
338                 _translator->load(QString("/opt/someplayer/someplayer_%1").arg(config.getValue("ui/language").toString()));
339                 QApplication::installTranslator(_translator);
340         } else {
341                 QApplication::removeTranslator(_translator);
342         }
343         ui->retranslateUi(this);
344         _player_form->updateTranslations();
345         _library_form->updateTranslations();
346         _equalizer_dialog->updateTranslations();
347         _manage_library_form->updateTranslations();
348         _directory_form->updateTranslations();
349         _settings_form->updateTranslations();
350         _about_form->updateTranslations();
351 }
352
353 void MainWindow::_hw_zoom_policy_changed() {
354         Config config;
355         QString state = config.getValue("hw/zoomkeys").toString();
356         if (state == "enabled") {
357                 _dbus_client.enableKeys();
358                 connect(&_dbus_client, SIGNAL(zoomKeyPressed(quint32)), this, SLOT(_zoom_key_pressed(quint32)));
359         } else {
360                 _dbus_client.disableKeys();
361                 disconnect(&_dbus_client, SIGNAL(zoomKeyPressed(quint32)), this, SLOT(_zoom_key_pressed(quint32)));
362         }
363 }
364
365 void MainWindow::_set_display_state(bool state) {
366         _display_unlocked = state;
367         if (!_display_unlocked) { // remember volume level when blocking screen
368                 _system_volume = _dbus_client.getVolume();
369         }
370 }
371
372 void MainWindow::_zoom_key_pressed(quint32 code) {
373         if (_display_unlocked) {
374                 return;
375         }
376         Config config;
377         QString behavior = config.getValue("hw/zoom_action").toString();
378         bool inverted = config.getValue("hw/zoom_inverted").toBool();
379         if (inverted) {
380                 if (code == MM_KEY_DOWN) {
381                         code = MM_KEY_UP;
382                 } else {
383                         code = MM_KEY_DOWN;
384                 }
385         }
386         if (code == MM_KEY_DOWN) {
387                 if (behavior == "track") {
388                         if (_orientation == ORIENTATION_LANDSCAPE) {
389                                 _player_form->next();
390                         } else {
391                                 _player_form->prev();
392                         }
393                         _dbus_client.setVolume(_system_volume);
394                 }
395         } else if (code == MM_KEY_UP) {
396                 if (behavior == "track") {
397                         if (_orientation == ORIENTATION_LANDSCAPE) {
398                                 _player_form->prev();
399                         } else {
400                                 _player_form->next();
401                         }
402                         _dbus_client.setVolume(_system_volume);
403                 }
404         }
405 }
406
407 void MainWindow::_fmtx_settings_changed() {
408         Config config;
409         if (config.getValue("fmtx/enabled").toString() == "yes") {
410                 QString station_name = config.getValue("fmtx/station_name").toString();
411                 int frequency = config.getValue("fmtx/frequency").toInt();
412                 system(QString("fmtx_client -p1 -f%1 -s\"%2\" -t\"%3\" 2>&1 >/dev/null")
413                        .arg(frequency)
414                        .arg(station_name)
415                        .arg(_player_form->playerCaption()).toAscii());
416         } else {
417                 system("fmtx_client -p 0 2>&1 >/dev/null");
418         }
419 }