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