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