Added license information
[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
31 using namespace SomePlayer::DataObjects;
32
33 MainWindow::MainWindow(QWidget *parent) :
34         QMainWindow(parent),
35         ui(new Ui::MainWindow)
36 {
37         _library = new Library(_DATABASE_PATH_, _PLAYLISTS_PATH_);
38         ui->setupUi(this);
39         connect(ui->actionAbout_Qt, SIGNAL(triggered()), this, SLOT(aboutQt()));
40         connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
41         setAnimated(true);
42         _player_form = new PlayerForm(_library, ui->stackedWidget);
43         _library_form = new LibraryForm(_library, ui->stackedWidget);
44         _busy_widget = new BusyWidget(ui->stackedWidget);
45         ui->stackedWidget->insertWidget(0, _player_form);
46         ui->stackedWidget->insertWidget(1, _library_form);
47         ui->stackedWidget->insertWidget(2, _busy_widget);
48         QAction *add_directory = ui->menuLibrary->addAction("Add directory");
49         QAction *save_playlist = ui->menuLibrary->addAction("Save playlist");
50         QAction *clear_playlist = ui->menuLibrary->addAction("Clear current playlist");
51         connect(_player_form, SIGNAL(library()), this, SLOT(library()));
52         connect(_library_form, SIGNAL(player()), this, SLOT(player()));
53         connect(add_directory, SIGNAL(triggered()), this, SLOT(_add_directory()));
54         connect(save_playlist, SIGNAL(triggered()), this, SLOT(_save_playlist()));
55         connect(clear_playlist, SIGNAL(triggered()), this, SLOT(_clear_current_playlist()));
56         connect(_library, SIGNAL(done()), this, SLOT(library()));
57         connect(_library_form, SIGNAL(done()), this, SLOT(library()));
58         connect(_library_form, SIGNAL(busy(QString)), this, SLOT(showBusyWidget(QString)));
59         connect(ui->searchButton, SIGNAL(clicked()), this, SLOT(_toggle_search_line()));
60         connect(_player_form, SIGNAL(showSearchPanel()), this, SLOT(showSearchPanel()));
61         connect(_player_form, SIGNAL(hideSearchPanel()), this, SLOT(hideSearchPanel()));
62         connect(ui->searchLine, SIGNAL(textChanged(QString)), this, SLOT(_search(QString)));
63         connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(_nextItem()));
64         connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(_prevItem()));
65         connect(ui->fscreenButton, SIGNAL(clicked()), this, SLOT(_toggle_full_screen()));
66         hideSearchPanel();
67         library();
68 }
69
70 MainWindow::~MainWindow()
71 {
72         delete _player_form;
73         delete _library_form;
74         delete ui;
75 }
76
77 void MainWindow::aboutQt() {
78         QMessageBox::aboutQt(this, "About Qt");
79 }
80
81 void MainWindow::about() {
82         QMessageBox::about(this, "About SomePlayer", "Alternate music player for Maemo 5 "
83                                            "written in C++ with Qt4\n\n"
84                                            "Author: Nikolay Tischenko aka \"somebody\" <niktischenko@gmail.com>");
85 }
86
87 void MainWindow::player() {
88         ui->stackedWidget->setCurrentIndex(0);
89         _player_form->reload();
90         setWindowTitle("SomePlayer");
91 }
92
93 void MainWindow::library() {
94         ui->menuBar->setEnabled(true);
95         ui->stackedWidget->setCurrentIndex(1);
96         showSearchPanel();
97         setWindowTitle("SomePlayer Library");
98 }
99
100 void MainWindow::_add_directory() {
101         QString directory = QFileDialog::getExistingDirectory (this, "Select directory", "/home/user/MyDocs", QFileDialog::ShowDirsOnly );
102         if (!directory.isEmpty()) {
103                 showBusyWidget("<H1>Scanning... Please wait</H1>");
104                 _library->addDirectory(directory);
105         }
106 }
107
108 void MainWindow::_save_playlist() {
109         QString name = QInputDialog::getText(this, "Playlist name", "Name:");
110         Playlist playlist = _library->getCurrentPlaylist();
111         playlist.setName(name);
112         _library->savePlaylist(playlist);
113 }
114
115 void MainWindow::_clear_current_playlist() {
116         Playlist playlist = _library->getCurrentPlaylist();
117         playlist.clear();
118         _library->saveCurrentPlaylist(playlist);
119         _player_form->reload();
120 }
121
122 void MainWindow::showBusyWidget(QString caption) {
123         _busy_widget->setText(caption);
124         ui->menuBar->setEnabled(false);
125         hideSearchPanel();
126         ui->stackedWidget->setCurrentIndex(2);
127 }
128
129 void MainWindow::_toggle_search_line() {
130         if (ui->searchLine->isVisible()) {
131                 ui->searchLine->setText("");
132                 ui->searchLine->hide();
133                 ui->nextButton->hide();
134                 ui->prevButton->hide();
135                 _cancelSearch();
136         } else {
137                 ui->searchLine->show();
138                 ui->nextButton->show();
139                 ui->prevButton->show();
140         }
141 }
142
143 void MainWindow::showSearchPanel() {
144         ui->searchButton->show();
145         ui->searchLine->setFocus();
146 }
147
148 void MainWindow::hideSearchPanel() {
149         ui->searchLine->setText("");
150         ui->searchLine->hide();
151         ui->nextButton->hide();
152         ui->prevButton->hide();
153         ui->searchButton->hide();
154         _cancelSearch();
155 }
156
157 void MainWindow::_search(QString pattern) {
158         if (ui->stackedWidget->currentIndex() == 0) { // player
159                 _player_form->search(pattern);
160         } else if (ui->stackedWidget->currentIndex() == 1) { // library
161                 _library_form->search(pattern);
162         }
163 }
164
165 void MainWindow::_nextItem() {
166         if (ui->stackedWidget->currentIndex() == 0) { // player
167                 _player_form->nextItem();
168         } else if (ui->stackedWidget->currentIndex() == 1) { // library
169                 _library_form->nextItem();
170         }
171 }
172
173 void MainWindow::_prevItem() {
174         if (ui->stackedWidget->currentIndex() == 0) { // player
175                 _player_form->prevItem();
176         } else if (ui->stackedWidget->currentIndex() == 1) { // library
177                 _library_form->prevItem();
178         }
179 }
180
181 void MainWindow::_cancelSearch() {
182         if (ui->stackedWidget->currentIndex() == 0) { // player
183                 _player_form->cancelSearch();
184         } else if (ui->stackedWidget->currentIndex() == 1) { // library
185                 _library_form->cancelSearch();
186         }
187 }
188
189 void MainWindow::_toggle_full_screen() {
190         if (isFullScreen()) {
191                 ui->fscreenButton->setIcon(QIcon(":/icons/fullscreen.png"));
192                 showNormal();
193         } else {
194                 ui->fscreenButton->setIcon(QIcon(":/icons/window.png"));
195                 showFullScreen();
196         }
197 }