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