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