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