A lot of changes (animations, initial library manager)
[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         _library_form->setAttribute(Qt::WA_Maemo5StackedWindow);
54         _library_form->setWindowFlags(_library_form->windowFlags() | Qt::Window);
55         _busy_widget = new BusyWidget(this);
56         ui->centralWidget->layout()->addWidget(_busy_widget);
57         _busy_widget->hide();
58         _timer = new QTimer(this);
59         _equalizer_dialog = new EqualizerDialog(this);
60         _manage_library_form = new ManageLibraryForm(_library, this);
61         _manage_library_form->setAttribute(Qt::WA_Maemo5StackedWindow);
62         _manage_library_form->setWindowFlags(Qt::Window | _manage_library_form->windowFlags());
63         connect(_player_form, SIGNAL(library()), this, SLOT(library()));
64         connect(_library_form, SIGNAL(refreshPlayer()), this, SLOT(player()));
65         connect(ui->actionManageLibrary, SIGNAL(triggered()), this, SLOT(_manage_library()));
66         connect(ui->actionSavePlaylist, SIGNAL(triggered()), this, SLOT(_save_playlist()));
67         connect(_player_form, SIGNAL(clearPlaylist()), this, SLOT(_clear_current_playlist()));
68         connect(ui->actionSetTimer, SIGNAL(triggered()), this, SLOT(_set_timer()));
69         connect(ui->actionEqualizer, SIGNAL(triggered()), this, SLOT(_equalizer()));
70         connect(_library, SIGNAL(done()), this, SLOT(library()));
71         connect(_library, SIGNAL(done()), _library_form, SLOT(refresh()));
72         connect(_library, SIGNAL(addingTracks(int)), _busy_widget, SLOT(setMax(int)));
73         connect(_library, SIGNAL(trackAdded()), _busy_widget, SLOT(tick()));
74         connect(_timer, SIGNAL(timeout()), this, SLOT(_timeout()));
75         connect(_equalizer_dialog, SIGNAL(valueChanged(int,int)), this, SLOT(_equalizer_value_changed(int, int)));
76         connect(_equalizer_dialog, SIGNAL(equalizerEnabled()), _player_form, SLOT(enableEqualizer()));
77         connect(_equalizer_dialog, SIGNAL(equalizerDisabled()), _player_form, SLOT(disableEqualizer()));
78         connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(_orientation_changed()));
79         connect(_player_form, SIGNAL(fullscreen(bool)), this, SLOT(_fullscreen(bool)));
80         connect(_library_form, SIGNAL(addAndPlay(Track)), _player_form, SLOT(play(Track)));
81         _player_form->reload(true);
82         QString mode = config.getValue("ui/orientation").toString();
83         if (mode == "landscape") {
84                 setAttribute(Qt::WA_Maemo5LandscapeOrientation);
85                 _player_form->landscapeMode();
86                 _library_form->landscapeMode();
87                 _equalizer_dialog->landscapeMode();
88         } else if (mode == "portrait") {
89                 setAttribute(Qt::WA_Maemo5PortraitOrientation);
90                 _player_form->portraitMode();
91                 _library_form->portraitMode();
92                 _equalizer_dialog->portraitMode();
93         } else if (mode == "auto") { // initialization in landscape
94                 _player_form->landscapeMode();
95                 _library_form->landscapeMode();
96                 _equalizer_dialog->landscapeMode();
97                 setAttribute(Qt::WA_Maemo5AutoOrientation);
98         }
99         _library_form->updateIcons();
100         _player_form->updateIcons();
101         _player_form->checkGradient();
102         _library_form->checkGradient();
103         setWindowTitle("SomePlayer");
104 }
105
106 MainWindow::~MainWindow()
107 {
108         delete _player_form;
109         delete _library_form;
110         delete ui;
111 }
112
113 void MainWindow::about() {
114         QMessageBox::about(this, QString("About SomePlayer v")+_SOMEPLAYER_VERSION_, "Alternate music player for Maemo 5 "
115                                            "written in C++ with Qt4\n\n"
116                                            "Author: Nikolay Tischenko aka \"somebody\" <niktischenko@gmail.com>");
117 }
118
119 void MainWindow::player() {
120         _player_form->reload(true);
121         _orientation_changed(); // workaround
122 }
123
124 void MainWindow::library() {
125         ui->menuBar->setEnabled(true);
126         _library_form->show();
127         _orientation_changed(); // workaround
128         _busy_widget->hide();
129         _manage_library_form->hide();
130         _player_form->show();
131 }
132
133 void MainWindow::_manage_library() {
134         _manage_library_form->show();
135 }
136
137 void MainWindow::_save_playlist() {
138         QList<QString> playlists = _library->getPlaylistsNames();
139         playlists.removeOne(_CURRENT_PLAYLIST_SUBST_);
140         SavePlaylistDialog dialog(this);
141         dialog.setPlaylistNames(playlists);
142         if (dialog.exec() == QDialog::Accepted) {
143                 QString name = dialog.selectedName();
144                 bool append = false;
145                 if (playlists.contains(name)) {
146                         if (QMessageBox::question(this, "Append to playlist?", "Playlist with name \""+name+"\" already exists.\n"
147                                                   "Dow you want to append current playlist to it?",
148                                                   QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
149                                 append = true;
150                         } else {
151                                 append = false;
152                         }
153                 }
154                 if (append) {
155                         Playlist cur = _library->getCurrentPlaylist();
156                         Playlist target = _library->getPlaylist(name);
157                         QList<Track> tracks = cur.tracks();
158                         foreach (Track track, tracks) {
159                                 target.addTrack(track);
160                         }
161                         _library->savePlaylist(target);
162                 } else {
163                         Playlist playlist = _library->getCurrentPlaylist();
164                         playlist.setName(name);
165                         _library->savePlaylist(playlist);
166                 }
167         }
168 }
169
170 void MainWindow::_clear_current_playlist() {
171         Playlist playlist = _library->getCurrentPlaylist();
172         playlist.clear();
173         _library->saveCurrentPlaylist(playlist);
174         _player_form->reload(true);
175 }
176
177 void MainWindow::showBusyWidget(QString caption) {
178         _busy_widget->setText(caption);
179         ui->menuBar->setEnabled(false);
180         _player_form->hide();
181         _busy_widget->show();
182 }
183
184 void MainWindow::_add_files() {
185         QStringList files = QFileDialog::getOpenFileNames(this, "Add file");
186         if (!files.isEmpty()) _player_form->addFiles(files);
187 }
188
189 void MainWindow::_set_timer() {
190         TimerDialog dialog(this);
191         dialog.init();
192         if (_timer->isActive()) {
193                 dialog.showDisable();
194                 int msec = _timer->interval();
195                 int h = msec/3600000;
196                 int m = msec/60000 - h * 60;
197                 int s = msec/1000 - h * 3600 - m * 60;
198                 dialog.setTime(h, m, s);
199         }
200         if (QDialog::Accepted == dialog.exec()) {
201                 if (!dialog.timerDisabled()) {
202                         int h, m, s;
203                         dialog.getTime(&h, &m, &s);
204                         _timer->setInterval(h*3600000+m*60000+s*1000);
205                         _timer->start();
206                 } else if (_timer->isActive()) {
207                         _timer->stop();
208                 }
209         }
210 }
211
212 void MainWindow::_timeout() {
213         _player_form->stop();
214         _timer->stop();
215 }
216
217 void MainWindow::_equalizer() {
218         if (_player_form->isEqualizerAvailable()) {
219                 double val = 0;
220                 for (int i = 0; i < 10; i++) {
221                         _player_form->equalizerValue(i, &val);
222                         _equalizer_dialog->setValue(i, (int)(val * 10 + 0.5));
223                 }
224                 _equalizer_dialog->setEqualizerEnabled(_player_form->isEqualizerEnabled());
225                 _equalizer_dialog->reloadPresets();
226                 _equalizer_dialog->exec();
227         } else {
228                 QMessageBox::information(this, "Error", "No equalizer support. Please install gstreamer0.10-plugins-good-extra");
229         }
230 }
231
232 void MainWindow::_equalizer_value_changed(int band, int val) {
233         _player_form->setEqualizerValue(band, (val / 10.0));
234 }
235
236 void MainWindow::settings() {
237         SettingsDialog dialog;
238         dialog.exec();
239         Config config;
240         _library_form->refresh();
241         QString mode = config.getValue("ui/orientation").toString();
242         if (mode == "landscape") {
243                 setAttribute(Qt::WA_Maemo5LandscapeOrientation);
244         } else if (mode == "portrait") {
245                 setAttribute(Qt::WA_Maemo5PortraitOrientation);
246         } else if (mode == "auto") {
247                 setAttribute(Qt::WA_Maemo5AutoOrientation);
248         }
249         _player_form->updateIcons();
250         _library_form->updateIcons();
251         _player_form->checkGradient();
252         _library_form->checkGradient();
253 }
254
255 void MainWindow::_orientation_changed() {
256         QRect screenGeometry = QApplication::desktop()->screenGeometry();
257         if (screenGeometry.width() > screenGeometry.height()) {
258                 _player_form->landscapeMode();
259                 _library_form->landscapeMode();
260                 _equalizer_dialog->landscapeMode();
261         } else {
262                 _player_form->portraitMode();
263                 _library_form->portraitMode();
264                 _equalizer_dialog->portraitMode();
265         }
266 }
267
268 void MainWindow::_fullscreen(bool f) {
269         if (f) showFullScreen();
270         else showNormal();
271 }