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