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