5c5d7878af7fa7d779e9180b4248b0ab44a346de
[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 "library.h"
31 #include "timerdialog.h"
32 #include "equalizerdialog.h"
33 #include "saveplaylistdialog.h"
34 #include "settingsform.h"
35
36 using namespace SomePlayer::DataObjects;
37 using namespace SomePlayer::Storage;
38 using namespace SomePlayer::Playback;
39
40 MainWindow::MainWindow(QWidget *parent) :
41         QMainWindow(parent),
42         ui(new Ui::MainWindow)
43 {
44         _display_unlocked = true; // in most cases
45         Config config;
46         _library = new Library(config.applicationDir(), config.applicationDir());
47         _translator = new QTranslator(this);
48         ui->setupUi(this);
49         connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
50         connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(settings()));
51         setAnimated(true);
52         setAttribute(Qt::WA_Maemo5StackedWindow);
53         _player_form = new PlayerForm(_library, this);
54         ui->centralWidget->layout()->addWidget(_player_form);
55         _library_form = new LibraryForm(_library, this);
56         _directory_form = new DirectoryView(this);
57         _directory_form->hide();
58         _timer = new QTimer(this);
59         _equalizer_dialog = new EqualizerDialog(this);
60         _manage_library_form = new ManageLibraryForm(_library, this);
61         _settings_form = new SettingsForm(this);
62         _settings_form->hide();
63         _about_form = new AboutForm(this);
64         _about_form->hide();
65         connect(_player_form, SIGNAL(library()), this, SLOT(library()));
66         connect(_library_form, SIGNAL(refreshPlayer()), this, SLOT(player()));
67         connect(ui->actionManageLibrary, SIGNAL(triggered()), this, SLOT(_manage_library()));
68         connect(ui->actionSavePlaylist, SIGNAL(triggered()), this, SLOT(_save_playlist()));
69         connect(ui->actionImport, SIGNAL(triggered()), this, SLOT(_import_playlits()));
70         connect(_player_form, SIGNAL(clearPlaylist()), this, SLOT(_clear_current_playlist()));
71         connect(ui->actionSetTimer, SIGNAL(triggered()), this, SLOT(_set_timer()));
72         connect(ui->actionEqualizer, SIGNAL(triggered()), this, SLOT(_equalizer()));
73         connect(ui->actionOnlineHelp, SIGNAL(triggered()), _about_form, SLOT(onlineHelp()));
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         QList<QWidget *> widgets = ui->centralWidget->findChildren<QWidget *>();
138         foreach (QWidget *widget, widgets) {
139                 if (widget->objectName() != "searchLine") {
140                         widget->installEventFilter(this);
141                 }
142         }
143 }
144
145 MainWindow::~MainWindow()
146 {
147         delete _player_form;
148         delete _library_form;
149         delete ui;
150 }
151
152 void MainWindow::about() {
153         _about_form->show();
154 }
155
156 void MainWindow::player() {
157         _player_form->reload(true);
158         _orientation_changed(); // workaround
159 }
160
161 void MainWindow::library() {
162         ui->menuBar->setEnabled(true);
163         _library_form->show();
164         _orientation_changed(); // workaround
165         _manage_library_form->hide();
166 }
167
168 void MainWindow::_manage_library() {
169         _manage_library_form->refresh();
170         _manage_library_form->show();
171 }
172
173 void MainWindow::_save_playlist() {
174         QList<QString> playlists = _library->getPlaylistsNames();
175         playlists.removeOne(_CURRENT_PLAYLIST_SUBST_);
176         Playlist cur = _library->getCurrentPlaylist();
177         // construct playlist name if possible
178         QString suggest_name;
179         QList<Track> tracks = cur.tracks();
180         QString artist = tracks.at(0).metadata().artist(), album = tracks.at(0).metadata().album();
181         foreach (Track t, tracks) {
182                 if (t.metadata().album() != album)
183                         album = "";
184                 if (t.metadata().artist() != artist)
185                         artist = "";
186         }
187         if (album.isEmpty() && artist.isEmpty()) {
188                 suggest_name = "New playlist";
189         } else if (album.isEmpty()) {
190                 suggest_name = artist;
191         } else {
192                 suggest_name = QString("%1 - %2").arg(artist).arg(album);
193         }
194
195         // constructed
196         SavePlaylistDialog dialog(suggest_name, this);
197         dialog.setPlaylistNames(playlists);
198         if (dialog.exec() == QDialog::Accepted) {
199                 QString name = dialog.selectedName();
200                 bool append = false;
201                 if (playlists.contains(name)) {
202                         if (QMessageBox::question(this, "Overwrite playlist?", "Overwrite playlist \""+name+"\"?", QMessageBox::Ok, QMessageBox::Cancel)
203                                 != QMessageBox::Ok) {
204                                 return;
205                         }
206                         append = (QMessageBox::question(this, "Append to playlist?", "Playlist with name \""+name+"\" already exists.\n"
207                                                   "Dow you want to append current playlist to it?",
208                                                   QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok);
209                 }
210                 if (append) {
211                         Playlist target = _library->getPlaylist(name);
212                         QList<Track> tracks = cur.tracks();
213                         foreach (Track track, tracks) {
214                                 target.addTrack(track);
215                         }
216                         _library->savePlaylist(target);
217                 } else {
218                         cur.setName(name);
219                         _library->savePlaylist(cur);
220                 }
221         }
222 }
223
224 void MainWindow::_clear_current_playlist() {
225         CONFIRM_ACTION(this, tr("Clear playlist?"))
226         Playlist playlist = _library->getCurrentPlaylist();
227         playlist.clear();
228         _library->saveCurrentPlaylist(playlist);
229         _player_form->reload(true);
230 }
231
232 void MainWindow::_set_timer() {
233         TimerDialog dialog(this);
234         dialog.init();
235         if (_timer->isActive()) {
236                 dialog.showDisable();
237                 int msec = _timeout_interval;
238                 int h = msec/3600000;
239                 int m = msec/60000 - h * 60;
240                 int s = msec/1000 - h * 3600 - m * 60;
241                 dialog.setTime(h, m, s);
242         }
243         if (QDialog::Accepted == dialog.exec()) {
244                 if (!dialog.timerDisabled()) {
245                         int h, m, s;
246                         dialog.getTime(&h, &m, &s);
247                         _timeout_interval = h*3600000+m*60000+s*1000;
248                         _timer->setInterval(1000);
249                         _timer->setSingleShot(false);
250                         _timer->start();
251                 } else if (_timer->isActive()) {
252                         _timer->stop();
253                         _player_form->hideCountdown();
254                 }
255         }
256 }
257
258 void MainWindow::_timeout() {
259         _timeout_interval -= 1000;
260         if (_timeout_interval <= 0) {
261                 _player_form->stop();
262                 _player_form->hideCountdown();
263                 _timer->stop();
264         } else {
265                 int h = _timeout_interval / 3600000;
266                 int m = (_timeout_interval / 60000) - 60*h;
267                 int s = (_timeout_interval / 1000) - 3600*h - 60*m;
268                 QString hp = h < 10 ? QString("0%1").arg(h) : QString("%1").arg(h);
269                 QString mp = m < 10 ? QString("0%1").arg(m) : QString("%1").arg(m);
270                 QString sp = s < 10 ? QString("0%1").arg(s) : QString("%1").arg(s);
271                 _player_form->showCountdown(tr("Music off: ")+hp+":"+mp+":"+sp);
272         }
273 }
274
275 void MainWindow::_equalizer() {
276         if (_player_form->isEqualizerAvailable()) {
277                 double val = 0;
278                 for (int i = 0; i < 10; i++) {
279                         _player_form->equalizerValue(i, &val);
280                         _equalizer_dialog->setValue(i, (int)(val * 10 + 0.5));
281                 }
282                 _equalizer_dialog->setEqualizerEnabled(_player_form->isEqualizerEnabled());
283                 _equalizer_dialog->reloadPresets();
284                 _equalizer_dialog->exec();
285         } else {
286                 QMessageBox::information(this, "Error", "No equalizer support. Please install gstreamer0.10-plugins-good-extra");
287         }
288 }
289
290 void MainWindow::_equalizer_value_changed(int band, int val) {
291         _player_form->setEqualizerValue(band, (val / 10.0));
292 }
293
294 void MainWindow::settings() {
295         _settings_form->show();
296 }
297
298 void MainWindow::_change_orientation() {
299         Config config;
300         QString mode = config.getValue("ui/orientation").toString();
301         if (mode == "landscape") {
302                 setAttribute(Qt::WA_Maemo5LandscapeOrientation);
303         } else if (mode == "portrait") {
304                 setAttribute(Qt::WA_Maemo5PortraitOrientation);
305         } else if (mode == "auto") {
306                 setAttribute(Qt::WA_Maemo5AutoOrientation);
307         }
308 }
309
310 void MainWindow::_orientation_changed() {
311         QRect screenGeometry = QApplication::desktop()->screenGeometry();
312         if (screenGeometry.width() > screenGeometry.height()) {
313                 _orientation = ORIENTATION_LANDSCAPE;
314                 _player_form->landscapeMode();
315                 _library_form->landscapeMode();
316                 _equalizer_dialog->landscapeMode();
317                 _directory_form->lanscapeMode();
318                 _settings_form->landscapeMode();
319         } else {
320                 _orientation = ORIENTATION_PORTRAIT;
321                 _player_form->portraitMode();
322                 _library_form->portraitMode();
323                 _equalizer_dialog->portraitMode();
324                 _directory_form->portraitMode();
325                 _settings_form->portraitMode();
326         }
327 }
328
329 void MainWindow::_fullscreen(bool f) {
330         if (f) showFullScreen();
331         else showNormal();
332 }
333
334 void MainWindow::_add_tracks(QList<Track> tracks) {
335         Playlist cur = _library->getCurrentPlaylist();
336         foreach (Track track, tracks) {
337                 cur.addTrack(track);
338         }
339         _library->saveCurrentPlaylist(cur);
340         _player_form->reload(true);
341 }
342
343 void MainWindow::updateTranslations() {
344         Config config;
345         if (config.getValue("ui/language").toString() != "en") {
346                 _translator->load(QString("/opt/someplayer/someplayer_%1").arg(config.getValue("ui/language").toString()));
347                 QApplication::installTranslator(_translator);
348         } else {
349                 QApplication::removeTranslator(_translator);
350         }
351         ui->retranslateUi(this);
352         _player_form->updateTranslations();
353         _library_form->updateTranslations();
354         _equalizer_dialog->updateTranslations();
355         _manage_library_form->updateTranslations();
356         _directory_form->updateTranslations();
357         _settings_form->updateTranslations();
358         _about_form->updateTranslations();
359 }
360
361 void MainWindow::_hw_zoom_policy_changed() {
362         Config config;
363         QString state = config.getValue("hw/zoomkeys").toString();
364         if (state == "enabled") {
365                 _dbus_client.enableKeys();
366                 connect(&_dbus_client, SIGNAL(zoomKeyPressed(quint32)), this, SLOT(_zoom_key_pressed(quint32)));
367         } else {
368                 _dbus_client.disableKeys();
369                 disconnect(&_dbus_client, SIGNAL(zoomKeyPressed(quint32)), this, SLOT(_zoom_key_pressed(quint32)));
370         }
371 }
372
373 void MainWindow::_set_display_state(bool state) {
374         _display_unlocked = state;
375         if (!_display_unlocked) { // remember volume level when blocking screen
376                 _system_volume = _dbus_client.getVolume();
377         }
378 }
379
380 void MainWindow::_zoom_key_pressed(quint32 code) {
381         if (_display_unlocked) {
382                 return;
383         }
384         Config config;
385         QString behavior = config.getValue("hw/zoom_action").toString();
386         bool inverted = config.getValue("hw/zoom_inverted").toBool();
387         if (inverted) {
388                 if (code == MM_KEY_DOWN) {
389                         code = MM_KEY_UP;
390                 } else {
391                         code = MM_KEY_DOWN;
392                 }
393         }
394         if (code == MM_KEY_DOWN) {
395                 if (behavior == "track") {
396                         if (_orientation == ORIENTATION_LANDSCAPE) {
397                                 _player_form->next();
398                         } else {
399                                 _player_form->prev();
400                         }
401                         _dbus_client.setVolume(_system_volume);
402                 }
403         } else if (code == MM_KEY_UP) {
404                 if (behavior == "track") {
405                         if (_orientation == ORIENTATION_LANDSCAPE) {
406                                 _player_form->prev();
407                         } else {
408                                 _player_form->next();
409                         }
410                         _dbus_client.setVolume(_system_volume);
411                 }
412         }
413 }
414
415 void MainWindow::_fmtx_settings_changed() {
416         Config config;
417         if (config.getValue("fmtx/enabled").toString() == "yes") {
418                 QString station_name = config.getValue("fmtx/station_name").toString();
419                 int frequency = config.getValue("fmtx/frequency").toInt();
420                 system(QString("fmtx_client -p1 -f%1 -s\"%2\" -t\"%3\" 2>&1 >/dev/null")
421                        .arg(frequency)
422                        .arg(station_name)
423                        .arg(_player_form->playerCaption()).toAscii());
424         } else {
425                 system("fmtx_client -p 0 2>&1 >/dev/null");
426         }
427 }
428
429 bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
430         if (event->type() != QEvent::KeyPress) {
431                 return QObject::eventFilter(obj, event);
432         }
433         QKeyEvent *keyEvent = (QKeyEvent *) event;
434         int mod = keyEvent->modifiers();
435
436         switch (keyEvent->key()) {
437         case Qt::Key_Space:
438                 if ((mod & Qt::ControlModifier) == 0) {
439                         _player_form->toggle();
440                 }
441                 break;
442         case Qt::Key_Enter:
443                 // TODO
444                 break;
445         case Qt::Key_Right:
446                 _player_form->next();
447                 break;
448         case Qt::Key_Left:
449                 _player_form->prev();
450                 break;
451         case Qt::Key_T:
452                 _player_form->stop();
453                 break;
454         case Qt::Key_R:
455                 _player_form->toggleRandom();
456                 break;
457         case Qt::Key_V:
458                 _player_form->toggleView();
459                 break;
460         case Qt::Key_M:
461                 _directory_form->show();
462                 break;
463         case Qt::Key_L:
464                 _library_form->show();
465                 break;
466         case Qt::Key_E:
467                 _player_form->toggleRepeat();
468                 break;
469         case Qt::Key_S:
470                 _player_form->toggleToolsWidget();
471                 break;
472         case Qt::Key_H:
473                 _about_form->onlineHelp();
474                 break;
475         default:
476                 return QObject::eventFilter(obj, event);
477         }
478
479         return true;
480 }
481
482
483 void MainWindow::_import_playlits() {
484         QString directory = QFileDialog::getExistingDirectory (this, tr("Select directory"), "", QFileDialog::ShowDirsOnly );
485         _library->scanAndImportPlaylists(directory);
486 }