Added D-Bus support
[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         _icons_theme = config.getValue("ui/iconstheme").toString();
45         _library = new Library(config.applicationDir(), config.applicationDir());
46         ui->setupUi(this);
47         connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
48         connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(settings()));
49         setAnimated(true);
50         _player_form = new PlayerForm(_library, ui->stackedWidget);
51         _library_form = new LibraryForm(_library, ui->stackedWidget);
52         _busy_widget = new BusyWidget(ui->stackedWidget);
53         _timer = new QTimer(this);
54         _equalizer_dialog = new EqualizerDialog(this);
55         ui->stackedWidget->insertWidget(0, _player_form);
56         ui->stackedWidget->insertWidget(1, _library_form);
57         ui->stackedWidget->insertWidget(2, _busy_widget);
58         QAction *add_directory = ui->menuLibrary->addAction("Add directory");
59         QAction *save_playlist = ui->menuLibrary->addAction("Save playlist");
60         QAction *clear_playlist = ui->menuLibrary->addAction("Clear current playlist");
61         QAction *add_files = ui->menuLibrary->addAction("Add file to current playlist");
62         QAction *set_timer = ui->menuBar->addAction("Set timer");
63         QAction *equalizer = ui->menuBar->addAction("Equalizer");
64         connect(_player_form, SIGNAL(library()), this, SLOT(library()));
65         connect(_library_form, SIGNAL(player(bool)), this, SLOT(player(bool)));
66         connect(add_directory, SIGNAL(triggered()), this, SLOT(_add_directory()));
67         connect(save_playlist, SIGNAL(triggered()), this, SLOT(_save_playlist()));
68         connect(clear_playlist, SIGNAL(triggered()), this, SLOT(_clear_current_playlist()));
69         connect(add_files, SIGNAL(triggered()), this, SLOT(_add_files()));
70         connect(set_timer, SIGNAL(triggered()), this, SLOT(_set_timer()));
71         connect(equalizer, SIGNAL(triggered()), this, SLOT(_equalizer()));
72         connect(_library, SIGNAL(done()), this, SLOT(library()));
73         connect(_library, SIGNAL(done()), _library_form, SLOT(refresh()));
74         connect(_library, SIGNAL(addingTracks(int)), _busy_widget, SLOT(setMax(int)));
75         connect(_library, SIGNAL(trackAdded()), _busy_widget, SLOT(tick()));
76         connect(_library_form, SIGNAL(done()), this, SLOT(library()));
77         connect(_library_form, SIGNAL(busy(QString)), this, SLOT(showBusyWidget(QString)));
78         connect(ui->searchButton, SIGNAL(clicked()), this, SLOT(_toggle_search_line()));
79         connect(_player_form, SIGNAL(showSearchPanel()), this, SLOT(showSearchPanel()));
80         connect(_player_form, SIGNAL(hideSearchPanel()), this, SLOT(hideSearchPanel()));
81         connect(ui->searchLine, SIGNAL(textChanged(QString)), this, SLOT(_search(QString)));
82         connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(_nextItem()));
83         connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(_prevItem()));
84         connect(ui->fscreenButton, SIGNAL(clicked()), this, SLOT(_toggle_full_screen()));
85         connect(_timer, SIGNAL(timeout()), this, SLOT(_timeout()));
86         connect(_equalizer_dialog, SIGNAL(valueChanged(int,int)), this, SLOT(_equalizer_value_changed(int, int)));
87         connect(_equalizer_dialog, SIGNAL(equalizerEnabled()), _player_form, SLOT(enableEqualizer()));
88         connect(_equalizer_dialog, SIGNAL(equalizerDisabled()), _player_form, SLOT(disableEqualizer()));
89         connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(_orientation_changed()));
90         updateIcons();
91         _library_form->updateIcons();
92         _player_form->updateIcons();
93         hideSearchPanel();
94         _player_form->reload(true);
95         library();
96 }
97
98 MainWindow::~MainWindow()
99 {
100         delete _player_form;
101         delete _library_form;
102         delete ui;
103 }
104
105 void MainWindow::about() {
106         QMessageBox::about(this, QString("About SomePlayer v")+_SOMEPLAYER_VERSION_, "Alternate music player for Maemo 5 "
107                                            "written in C++ with Qt4\n\n"
108                                            "Author: Nikolay Tischenko aka \"somebody\" <niktischenko@gmail.com>");
109 }
110
111 void MainWindow::player(bool reread) {
112         Config config;
113         setAttribute(Qt::WA_Maemo5AutoOrientation, config.getValue("ui/portraitmode").toString() != "disabled");
114         ui->stackedWidget->setCurrentIndex(0);
115         _player_form->reload(reread);
116         setWindowTitle("SomePlayer");
117 }
118
119 void MainWindow::library() {
120         setAttribute(Qt::WA_Maemo5AutoOrientation, false);
121         ui->menuBar->setEnabled(true);
122         ui->stackedWidget->setCurrentIndex(1);
123         showSearchPanel();
124         setWindowTitle("SomePlayer Library");
125 }
126
127 void MainWindow::_add_directory() {
128         QString directory = QFileDialog::getExistingDirectory (this, "Select directory", "/home/user/MyDocs", QFileDialog::ShowDirsOnly );
129         if (!directory.isEmpty()) {
130                 showBusyWidget("<H1>Scanning... Please wait</H1>");
131                 _library->addDirectory(directory);
132         }
133 }
134
135 void MainWindow::_save_playlist() {
136         QList<QString> playlists = _library->getPlaylistsNames();
137         playlists.removeOne(_CURRENT_PLAYLIST_SUBST_);
138         SavePlaylistDialog dialog(this);
139         dialog.setPlaylistNames(playlists);
140         if (dialog.exec() == QDialog::Accepted) {
141                 QString name = dialog.selectedName();
142                 bool append = false;
143                 if (playlists.contains(name)) {
144                         if (QMessageBox::question(this, "Append to playlist?", "Playlist with name \""+name+"\" already exists.\n"
145                                                   "Dow you want to append current playlist to it?",
146                                                   QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
147                                 append = true;
148                         } else {
149                                 append = false;
150                         }
151                 }
152                 if (append) {
153                         Playlist cur = _library->getCurrentPlaylist();
154                         Playlist target = _library->getPlaylist(name);
155                         QList<Track> tracks = cur.tracks();
156                         foreach (Track track, tracks) {
157                                 target.addTrack(track);
158                         }
159                         _library->savePlaylist(target);
160                 } else {
161                         Playlist playlist = _library->getCurrentPlaylist();
162                         playlist.setName(name);
163                         _library->savePlaylist(playlist);
164                 }
165         }
166 }
167
168 void MainWindow::_clear_current_playlist() {
169         Playlist playlist = _library->getCurrentPlaylist();
170         playlist.clear();
171         _library->saveCurrentPlaylist(playlist);
172         _player_form->reload(true);
173 }
174
175 void MainWindow::showBusyWidget(QString caption) {
176         _busy_widget->setText(caption);
177         ui->menuBar->setEnabled(false);
178         hideSearchPanel();
179         ui->stackedWidget->setCurrentIndex(2);
180 }
181
182 void MainWindow::_toggle_search_line() {
183         if (ui->searchLine->isVisible()) {
184                 ui->searchLine->setText("");
185                 ui->searchLine->hide();
186                 ui->nextButton->hide();
187                 ui->prevButton->hide();
188                 _cancelSearch();
189         } else {
190                 ui->searchLine->show();
191                 ui->nextButton->show();
192                 ui->prevButton->show();
193                 ui->searchLine->setFocus(Qt::MouseFocusReason);
194         }
195 }
196
197 void MainWindow::showSearchPanel() {
198         ui->searchButton->show();
199 }
200
201 void MainWindow::hideSearchPanel() {
202         ui->searchLine->setText("");
203         ui->searchLine->hide();
204         ui->nextButton->hide();
205         ui->prevButton->hide();
206         ui->searchButton->hide();
207         _cancelSearch();
208 }
209
210 void MainWindow::_search(QString pattern) {
211         if (ui->stackedWidget->currentIndex() == 0) { // player
212                 _player_form->search(pattern);
213         } else if (ui->stackedWidget->currentIndex() == 1) { // library
214                 _library_form->search(pattern);
215         }
216 }
217
218 void MainWindow::_nextItem() {
219         if (ui->stackedWidget->currentIndex() == 0) { // player
220                 _player_form->nextItem();
221         } else if (ui->stackedWidget->currentIndex() == 1) { // library
222                 _library_form->nextItem();
223         }
224 }
225
226 void MainWindow::_prevItem() {
227         if (ui->stackedWidget->currentIndex() == 0) { // player
228                 _player_form->prevItem();
229         } else if (ui->stackedWidget->currentIndex() == 1) { // library
230                 _library_form->prevItem();
231         }
232 }
233
234 void MainWindow::_cancelSearch() {
235         if (ui->stackedWidget->currentIndex() == 0) { // player
236                 _player_form->cancelSearch();
237         } else if (ui->stackedWidget->currentIndex() == 1) { // library
238                 _library_form->cancelSearch();
239         }
240 }
241
242 void MainWindow::_toggle_full_screen() {
243         if (isFullScreen()) {
244                 ui->fscreenButton->setIcon(QIcon(":/icons/"+_icons_theme+"/fullscreen.png"));
245                 showNormal();
246         } else {
247                 ui->fscreenButton->setIcon(QIcon(":/icons/"+_icons_theme+"/window.png"));
248                 showFullScreen();
249         }
250 }
251
252 void MainWindow::_add_files() {
253         QStringList files = QFileDialog::getOpenFileNames(this, "Add file");
254         if (!files.isEmpty()) _player_form->addFiles(files);
255 }
256
257 void MainWindow::_set_timer() {
258         TimerDialog dialog(this);
259         dialog.init();
260         if (_timer->isActive()) {
261                 dialog.showDisable();
262                 int msec = _timer->interval();
263                 int h = msec/3600000;
264                 int m = msec/60000 - h * 60;
265                 int s = msec/1000 - h * 3600 - m * 60;
266                 dialog.setTime(h, m, s);
267         }
268         if (QDialog::Accepted == dialog.exec()) {
269                 if (!dialog.timerDisabled()) {
270                         int h, m, s;
271                         dialog.getTime(&h, &m, &s);
272                         _timer->setInterval(h*3600000+m*60000+s*1000);
273                         _timer->start();
274                 } else if (_timer->isActive()) {
275                         _timer->stop();
276                 }
277         }
278 }
279
280 void MainWindow::_timeout() {
281         _player_form->stop();
282         _timer->stop();
283 }
284
285 void MainWindow::_equalizer() {
286         if (_player_form->isEqualizerAvailable()) {
287                 double val = 0;
288                 for (int i = 0; i < 10; i++) {
289                         _player_form->equalizerValue(i, &val);
290                         _equalizer_dialog->setValue(i, (int)(val * 10 + 0.5));
291                 }
292                 _equalizer_dialog->setEqualizerEnabled(_player_form->isEqualizerEnabled());
293                 _equalizer_dialog->reloadPresets();
294                 _equalizer_dialog->exec();
295         } else {
296                 QMessageBox::information(this, "Error", "No equalizer support. Please install gstreamer0.10-plugins-good-extra");
297         }
298 }
299
300 void MainWindow::_equalizer_value_changed(int band, int val) {
301         _player_form->setEqualizerValue(band, (val / 10.0));
302 }
303
304 void MainWindow::settings() {
305         SettingsDialog dialog;
306         dialog.exec();
307         updateIcons();
308         Config config;
309         _player_form->updateIcons();
310         _library_form->updateIcons();
311         _library_form->refresh();
312         if (ui->stackedWidget->currentIndex() == 0) { // player view
313                 setAttribute(Qt::WA_Maemo5AutoOrientation, config.getValue("ui/portraitmode").toString() != "disabled");
314         }
315 }
316
317 void MainWindow::updateIcons() {
318         Config config;
319         _icons_theme = config.getValue("ui/iconstheme").toString();
320         ui->fscreenButton->setIcon(QIcon(":/icons/"+_icons_theme+"/fullscreen.png"));
321         ui->prevButton->setIcon(QIcon(":/icons/"+_icons_theme+"/back.png"));
322         ui->nextButton->setIcon(QIcon(":/icons/"+_icons_theme+"/forward.png"));
323         ui->searchButton->setIcon(QIcon(":/icons/"+_icons_theme+"/search.png"));
324
325 }
326
327 void MainWindow::_orientation_changed() {
328         QRect screenGeometry = QApplication::desktop()->screenGeometry();
329         if (screenGeometry.width() > screenGeometry.height()) {
330                 _player_form->landscapeMode();
331                 ui->toolsWidget->setVisible(true);
332         } else {
333                 _player_form->portraitMode();
334                 ui->toolsWidget->setVisible(false);
335         }
336 }