2 * SomePlayer - An alternate music player for Maemo 5
3 * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
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.
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.
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.
20 #include "mainwindow.h"
21 #include "ui_mainwindow.h"
22 #include <QFileDialog>
23 #include <QMessageBox>
24 #include <QInputDialog>
27 #include "player/player.h"
30 #include "timerdialog.h"
32 using namespace SomePlayer::DataObjects;
33 using namespace SomePlayer::Storage;
35 MainWindow::MainWindow(QWidget *parent) :
37 ui(new Ui::MainWindow)
40 _library = new Library(config.applicationDir(), config.applicationDir());
42 connect(ui->actionAbout_Qt, SIGNAL(triggered()), this, SLOT(aboutQt()));
43 connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
45 _player_form = new PlayerForm(_library, ui->stackedWidget);
46 _library_form = new LibraryForm(_library, ui->stackedWidget);
47 _busy_widget = new BusyWidget(ui->stackedWidget);
48 _timer = new QTimer(this);
49 ui->stackedWidget->insertWidget(0, _player_form);
50 ui->stackedWidget->insertWidget(1, _library_form);
51 ui->stackedWidget->insertWidget(2, _busy_widget);
52 QAction *add_directory = ui->menuLibrary->addAction("Add directory");
53 QAction *save_playlist = ui->menuLibrary->addAction("Save playlist");
54 QAction *clear_playlist = ui->menuLibrary->addAction("Clear current playlist");
55 QAction *add_files = ui->menuLibrary->addAction("Add file to current playlist");
56 QAction *set_timer = ui->menuBar->addAction("Set timer");
57 connect(_player_form, SIGNAL(library()), this, SLOT(library()));
58 connect(_library_form, SIGNAL(player()), this, SLOT(player()));
59 connect(add_directory, SIGNAL(triggered()), this, SLOT(_add_directory()));
60 connect(save_playlist, SIGNAL(triggered()), this, SLOT(_save_playlist()));
61 connect(clear_playlist, SIGNAL(triggered()), this, SLOT(_clear_current_playlist()));
62 connect(add_files, SIGNAL(triggered()), this, SLOT(_add_files()));
63 connect(set_timer, SIGNAL(triggered()), this, SLOT(_set_timer()));
64 connect(_library, SIGNAL(done()), this, SLOT(library()));
65 connect(_library, SIGNAL(done()), _library_form, SLOT(refresh()));
66 connect(_library, SIGNAL(addingTracks(int)), _busy_widget, SLOT(setMax(int)));
67 connect(_library, SIGNAL(trackAdded()), _busy_widget, SLOT(tick()));
68 connect(_library_form, SIGNAL(done()), this, SLOT(library()));
69 connect(_library_form, SIGNAL(busy(QString)), this, SLOT(showBusyWidget(QString)));
70 connect(ui->searchButton, SIGNAL(clicked()), this, SLOT(_toggle_search_line()));
71 connect(_player_form, SIGNAL(showSearchPanel()), this, SLOT(showSearchPanel()));
72 connect(_player_form, SIGNAL(hideSearchPanel()), this, SLOT(hideSearchPanel()));
73 connect(ui->searchLine, SIGNAL(textChanged(QString)), this, SLOT(_search(QString)));
74 connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(_nextItem()));
75 connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(_prevItem()));
76 connect(ui->fscreenButton, SIGNAL(clicked()), this, SLOT(_toggle_full_screen()));
77 connect(_timer, SIGNAL(timeout()), this, SLOT(_timeout()));
82 MainWindow::~MainWindow()
89 void MainWindow::aboutQt() {
90 QMessageBox::aboutQt(this, "About Qt");
93 void MainWindow::about() {
94 QMessageBox::about(this, QString("About SomePlayer v")+_SOMEPLAYER_VERSION_, "Alternate music player for Maemo 5 "
95 "written in C++ with Qt4\n\n"
96 "Author: Nikolay Tischenko aka \"somebody\" <niktischenko@gmail.com>");
99 void MainWindow::player() {
100 ui->stackedWidget->setCurrentIndex(0);
101 _player_form->reload();
102 setWindowTitle("SomePlayer");
105 void MainWindow::library() {
106 ui->menuBar->setEnabled(true);
107 ui->stackedWidget->setCurrentIndex(1);
109 setWindowTitle("SomePlayer Library");
112 void MainWindow::_add_directory() {
113 QString directory = QFileDialog::getExistingDirectory (this, "Select directory", "/home/user/MyDocs", QFileDialog::ShowDirsOnly );
114 if (!directory.isEmpty()) {
115 showBusyWidget("<H1>Scanning... Please wait</H1>");
116 _library->addDirectory(directory);
120 void MainWindow::_save_playlist() {
121 QString name = QInputDialog::getText(this, "Playlist name", "Name:");
122 QList<QString> playlists = _library->getPlaylistsNames();
124 if (playlists.contains(name)) {
125 if (QMessageBox::question(this, "Append to playlist?", "Playlist with name \""+name+"\" already exists.\n"
126 "Dow you want to append current playlist to it?",
127 QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
134 Playlist cur = _library->getCurrentPlaylist();
135 Playlist target = _library->getPlaylist(name);
136 QList<Track> tracks = cur.tracks();
137 foreach (Track track, tracks) {
138 target.addTrack(track);
140 _library->savePlaylist(target);
142 Playlist playlist = _library->getCurrentPlaylist();
143 playlist.setName(name);
144 _library->savePlaylist(playlist);
148 void MainWindow::_clear_current_playlist() {
149 Playlist playlist = _library->getCurrentPlaylist();
151 _library->saveCurrentPlaylist(playlist);
152 _player_form->reload();
155 void MainWindow::showBusyWidget(QString caption) {
156 _busy_widget->setText(caption);
157 ui->menuBar->setEnabled(false);
159 ui->stackedWidget->setCurrentIndex(2);
162 void MainWindow::_toggle_search_line() {
163 if (ui->searchLine->isVisible()) {
164 ui->searchLine->setText("");
165 ui->searchLine->hide();
166 ui->nextButton->hide();
167 ui->prevButton->hide();
170 ui->searchLine->show();
171 ui->nextButton->show();
172 ui->prevButton->show();
176 void MainWindow::showSearchPanel() {
177 ui->searchButton->show();
178 ui->searchLine->setFocus();
181 void MainWindow::hideSearchPanel() {
182 ui->searchLine->setText("");
183 ui->searchLine->hide();
184 ui->nextButton->hide();
185 ui->prevButton->hide();
186 ui->searchButton->hide();
190 void MainWindow::_search(QString pattern) {
191 if (ui->stackedWidget->currentIndex() == 0) { // player
192 _player_form->search(pattern);
193 } else if (ui->stackedWidget->currentIndex() == 1) { // library
194 _library_form->search(pattern);
198 void MainWindow::_nextItem() {
199 if (ui->stackedWidget->currentIndex() == 0) { // player
200 _player_form->nextItem();
201 } else if (ui->stackedWidget->currentIndex() == 1) { // library
202 _library_form->nextItem();
206 void MainWindow::_prevItem() {
207 if (ui->stackedWidget->currentIndex() == 0) { // player
208 _player_form->prevItem();
209 } else if (ui->stackedWidget->currentIndex() == 1) { // library
210 _library_form->prevItem();
214 void MainWindow::_cancelSearch() {
215 if (ui->stackedWidget->currentIndex() == 0) { // player
216 _player_form->cancelSearch();
217 } else if (ui->stackedWidget->currentIndex() == 1) { // library
218 _library_form->cancelSearch();
222 void MainWindow::_toggle_full_screen() {
223 if (isFullScreen()) {
224 ui->fscreenButton->setIcon(QIcon(":/icons/fullscreen.png"));
227 ui->fscreenButton->setIcon(QIcon(":/icons/window.png"));
232 void MainWindow::_add_files() {
233 QStringList files = QFileDialog::getOpenFileNames(this, "Add file");
234 if (!files.isEmpty()) _player_form->addFiles(files);
237 void MainWindow::_set_timer() {
238 TimerDialog dialog(this);
240 if (_timer->isActive()) {
241 dialog.showDisable();
242 int msec = _timer->interval();
243 int h = msec/3600000;
244 int m = msec/60000 - h * 60;
245 int s = msec/1000 - h * 3600 - m * 60;
246 dialog.setTime(h, m, s);
248 if (QDialog::Accepted == dialog.exec()) {
249 if (!dialog.timerDisabled()) {
251 dialog.getTime(&h, &m, &s);
252 _timer->setInterval(h*3600000+m*60000+s*1000);
254 } else if (_timer->isActive()) {
260 void MainWindow::_timeout() {
261 _player_form->stop();