Now 'Artist name' and 'ArTist Name' is the same for library
[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_form, SIGNAL(done()), this, SLOT(library()));
62         connect(_library_form, SIGNAL(busy(QString)), this, SLOT(showBusyWidget(QString)));
63         connect(ui->searchButton, SIGNAL(clicked()), this, SLOT(_toggle_search_line()));
64         connect(_player_form, SIGNAL(showSearchPanel()), this, SLOT(showSearchPanel()));
65         connect(_player_form, SIGNAL(hideSearchPanel()), this, SLOT(hideSearchPanel()));
66         connect(ui->searchLine, SIGNAL(textChanged(QString)), this, SLOT(_search(QString)));
67         connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(_nextItem()));
68         connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(_prevItem()));
69         connect(ui->fscreenButton, SIGNAL(clicked()), this, SLOT(_toggle_full_screen()));
70         hideSearchPanel();
71         library();
72 }
73
74 MainWindow::~MainWindow()
75 {
76         delete _player_form;
77         delete _library_form;
78         delete ui;
79 }
80
81 void MainWindow::aboutQt() {
82         QMessageBox::aboutQt(this, "About Qt");
83 }
84
85 void MainWindow::about() {
86         QMessageBox::about(this, "About SomePlayer", "Alternate music player for Maemo 5 "
87                                            "written in C++ with Qt4\n\n"
88                                            "Author: Nikolay Tischenko aka \"somebody\" <niktischenko@gmail.com>");
89 }
90
91 void MainWindow::player() {
92         ui->stackedWidget->setCurrentIndex(0);
93         _player_form->reload();
94         setWindowTitle("SomePlayer");
95 }
96
97 void MainWindow::library() {
98         ui->menuBar->setEnabled(true);
99         ui->stackedWidget->setCurrentIndex(1);
100         showSearchPanel();
101         setWindowTitle("SomePlayer Library");
102 }
103
104 void MainWindow::_add_directory() {
105         QString directory = QFileDialog::getExistingDirectory (this, "Select directory", "/home/user/MyDocs", QFileDialog::ShowDirsOnly );
106         if (!directory.isEmpty()) {
107                 showBusyWidget("<H1>Scanning... Please wait</H1>");
108                 _library->addDirectory(directory);
109         }
110 }
111
112 void MainWindow::_save_playlist() {
113         QString name = QInputDialog::getText(this, "Playlist name", "Name:");
114         Playlist playlist = _library->getCurrentPlaylist();
115         playlist.setName(name);
116         _library->savePlaylist(playlist);
117 }
118
119 void MainWindow::_clear_current_playlist() {
120         Playlist playlist = _library->getCurrentPlaylist();
121         playlist.clear();
122         _library->saveCurrentPlaylist(playlist);
123         _player_form->reload();
124 }
125
126 void MainWindow::showBusyWidget(QString caption) {
127         _busy_widget->setText(caption);
128         ui->menuBar->setEnabled(false);
129         hideSearchPanel();
130         ui->stackedWidget->setCurrentIndex(2);
131 }
132
133 void MainWindow::_toggle_search_line() {
134         if (ui->searchLine->isVisible()) {
135                 ui->searchLine->setText("");
136                 ui->searchLine->hide();
137                 ui->nextButton->hide();
138                 ui->prevButton->hide();
139                 _cancelSearch();
140         } else {
141                 ui->searchLine->show();
142                 ui->nextButton->show();
143                 ui->prevButton->show();
144         }
145 }
146
147 void MainWindow::showSearchPanel() {
148         ui->searchButton->show();
149         ui->searchLine->setFocus();
150 }
151
152 void MainWindow::hideSearchPanel() {
153         ui->searchLine->setText("");
154         ui->searchLine->hide();
155         ui->nextButton->hide();
156         ui->prevButton->hide();
157         ui->searchButton->hide();
158         _cancelSearch();
159 }
160
161 void MainWindow::_search(QString pattern) {
162         if (ui->stackedWidget->currentIndex() == 0) { // player
163                 _player_form->search(pattern);
164         } else if (ui->stackedWidget->currentIndex() == 1) { // library
165                 _library_form->search(pattern);
166         }
167 }
168
169 void MainWindow::_nextItem() {
170         if (ui->stackedWidget->currentIndex() == 0) { // player
171                 _player_form->nextItem();
172         } else if (ui->stackedWidget->currentIndex() == 1) { // library
173                 _library_form->nextItem();
174         }
175 }
176
177 void MainWindow::_prevItem() {
178         if (ui->stackedWidget->currentIndex() == 0) { // player
179                 _player_form->prevItem();
180         } else if (ui->stackedWidget->currentIndex() == 1) { // library
181                 _library_form->prevItem();
182         }
183 }
184
185 void MainWindow::_cancelSearch() {
186         if (ui->stackedWidget->currentIndex() == 0) { // player
187                 _player_form->cancelSearch();
188         } else if (ui->stackedWidget->currentIndex() == 1) { // library
189                 _library_form->cancelSearch();
190         }
191 }
192
193 void MainWindow::_toggle_full_screen() {
194         if (isFullScreen()) {
195                 ui->fscreenButton->setIcon(QIcon(":/icons/fullscreen.png"));
196                 showNormal();
197         } else {
198                 ui->fscreenButton->setIcon(QIcon(":/icons/window.png"));
199                 showFullScreen();
200         }
201 }
202
203 void MainWindow::_add_files() {
204         QStringList files = QFileDialog::getOpenFileNames(this, "Add file");
205         if (!files.isEmpty()) _player_form->addFiles(files);
206 }