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