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