Implement translations, added russian translation
[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 #include <QTranslator>
28
29 #include "player/player.h"
30
31 #include "library.h"
32 #include "timerdialog.h"
33 #include "equalizerdialog.h"
34 #include "saveplaylistdialog.h"
35 #include "settingsdialog.h"
36
37 using namespace SomePlayer::DataObjects;
38 using namespace SomePlayer::Storage;
39
40 MainWindow::MainWindow(QWidget *parent) :
41         QMainWindow(parent),
42         ui(new Ui::MainWindow)
43 {
44         Config config;
45         _library = new Library(config.applicationDir(), config.applicationDir());
46         ui->setupUi(this);
47         connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
48         connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(settings()));
49         setAnimated(true);
50         setAttribute(Qt::WA_Maemo5StackedWindow);
51         _player_form = new PlayerForm(_library, this);
52         ui->centralWidget->layout()->addWidget(_player_form);
53         _library_form = new LibraryForm(_library, this);
54         _directory_form = new DirectoryView(this);
55         _directory_form->hide();
56         _timer = new QTimer(this);
57         _equalizer_dialog = new EqualizerDialog(this);
58         _manage_library_form = new ManageLibraryForm(_library, this);
59         connect(_player_form, SIGNAL(library()), this, SLOT(library()));
60         connect(_library_form, SIGNAL(refreshPlayer()), this, SLOT(player()));
61         connect(ui->actionManageLibrary, SIGNAL(triggered()), this, SLOT(_manage_library()));
62         connect(ui->actionSavePlaylist, SIGNAL(triggered()), this, SLOT(_save_playlist()));
63         connect(_player_form, SIGNAL(clearPlaylist()), this, SLOT(_clear_current_playlist()));
64         connect(ui->actionSetTimer, SIGNAL(triggered()), this, SLOT(_set_timer()));
65         connect(ui->actionEqualizer, SIGNAL(triggered()), this, SLOT(_equalizer()));
66         connect(_library, SIGNAL(done()), _library_form, SLOT(refresh()));
67         connect(_player_form, SIGNAL(refreshLibrary()), _library_form, SLOT(refresh()));
68         connect(_manage_library_form, SIGNAL(refreshLibrary()), _library_form, SLOT(refresh()));
69         connect(_timer, SIGNAL(timeout()), this, SLOT(_timeout()));
70         connect(_equalizer_dialog, SIGNAL(valueChanged(int,int)), this, SLOT(_equalizer_value_changed(int, int)));
71         connect(_equalizer_dialog, SIGNAL(equalizerEnabled()), _player_form, SLOT(enableEqualizer()));
72         connect(_equalizer_dialog, SIGNAL(equalizerDisabled()), _player_form, SLOT(disableEqualizer()));
73         connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(_orientation_changed()));
74         connect(_player_form, SIGNAL(fullscreen(bool)), this, SLOT(_fullscreen(bool)));
75         connect(_library_form, SIGNAL(addAndPlay(Track)), _player_form, SLOT(play(Track)));
76         connect(_directory_form, SIGNAL(addAndPlay(Track)), _player_form, SLOT(play(Track)));
77         connect(_player_form, SIGNAL(dirView()), _directory_form, SLOT(show()));
78         connect(_directory_form, SIGNAL(addTracks(QList<Track>)), this, SLOT(_add_tracks(QList<Track>)));
79         _player_form->reload(true);
80         QString mode = config.getValue("ui/orientation").toString();
81         if (mode == "landscape") {
82                 setAttribute(Qt::WA_Maemo5LandscapeOrientation);
83                 _player_form->landscapeMode();
84                 _library_form->landscapeMode();
85                 _equalizer_dialog->landscapeMode();
86                 _directory_form->lanscapeMode();
87         } else if (mode == "portrait") {
88                 setAttribute(Qt::WA_Maemo5PortraitOrientation);
89                 _player_form->portraitMode();
90                 _library_form->portraitMode();
91                 _equalizer_dialog->portraitMode();
92                 _directory_form->portraitMode();
93         } else if (mode == "auto") { // initialization in landscape
94                 _player_form->landscapeMode();
95                 _library_form->landscapeMode();
96                 _equalizer_dialog->landscapeMode();
97                 _directory_form->lanscapeMode();
98                 setAttribute(Qt::WA_Maemo5AutoOrientation);
99         }
100         _library_form->updateIcons();
101         _player_form->updateIcons();
102         _manage_library_form->updateIcons();
103         _directory_form->updateIcons();
104         _player_form->checkGradient();
105         _library_form->checkGradient();
106         _directory_form->updateGradient();
107         setWindowTitle("SomePlayer");
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() {
124         _player_form->reload(true);
125         _orientation_changed(); // workaround
126 }
127
128 void MainWindow::library() {
129         ui->menuBar->setEnabled(true);
130         _library_form->show();
131         _orientation_changed(); // workaround
132         _manage_library_form->hide();
133 }
134
135 void MainWindow::_manage_library() {
136         _manage_library_form->refresh();
137         _manage_library_form->show();
138 }
139
140 void MainWindow::_save_playlist() {
141         QList<QString> playlists = _library->getPlaylistsNames();
142         playlists.removeOne(_CURRENT_PLAYLIST_SUBST_);
143         Playlist cur = _library->getCurrentPlaylist();
144         // construct playlist name if possible
145         QString suggest_name;
146         QList<Track> tracks = cur.tracks();
147         QString artist = tracks.at(0).metadata().artist(), album = tracks.at(0).metadata().album();
148         foreach (Track t, tracks) {
149                 if (t.metadata().album() != album)
150                         album = "";
151                 if (t.metadata().artist() != artist)
152                         artist = "";
153         }
154         if (album.isEmpty() && artist.isEmpty()) {
155                 suggest_name = "New playlist";
156         } else if (album.isEmpty()) {
157                 suggest_name = artist;
158         } else {
159                 suggest_name = QString("%1 - %2").arg(artist).arg(album);
160         }
161
162         // constructed
163         SavePlaylistDialog dialog(suggest_name, this);
164         dialog.setPlaylistNames(playlists);
165         if (dialog.exec() == QDialog::Accepted) {
166                 QString name = dialog.selectedName();
167                 bool append = false;
168                 if (playlists.contains(name)) {
169                         if (QMessageBox::question(this, "Append to playlist?", "Playlist with name \""+name+"\" already exists.\n"
170                                                   "Dow you want to append current playlist to it?",
171                                                   QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
172                                 append = true;
173                         } else {
174                                 append = false;
175                         }
176                 }
177                 if (append) {
178                         Playlist target = _library->getPlaylist(name);
179                         QList<Track> tracks = cur.tracks();
180                         foreach (Track track, tracks) {
181                                 target.addTrack(track);
182                         }
183                         _library->savePlaylist(target);
184                 } else {
185                         cur.setName(name);
186                         _library->savePlaylist(cur);
187                 }
188         }
189 }
190
191 void MainWindow::_clear_current_playlist() {
192         Playlist playlist = _library->getCurrentPlaylist();
193         playlist.clear();
194         _library->saveCurrentPlaylist(playlist);
195         _player_form->reload(true);
196 }
197
198 void MainWindow::_set_timer() {
199         TimerDialog dialog(this);
200         dialog.init();
201         if (_timer->isActive()) {
202                 dialog.showDisable();
203                 int msec = _timeout_interval;
204                 int h = msec/3600000;
205                 int m = msec/60000 - h * 60;
206                 int s = msec/1000 - h * 3600 - m * 60;
207                 dialog.setTime(h, m, s);
208         }
209         if (QDialog::Accepted == dialog.exec()) {
210                 if (!dialog.timerDisabled()) {
211                         int h, m, s;
212                         dialog.getTime(&h, &m, &s);
213                         _timeout_interval = h*3600000+m*60000+s*1000;
214                         _timer->setInterval(1000);
215                         _timer->setSingleShot(false);
216                         _timer->start();
217                 } else if (_timer->isActive()) {
218                         _timer->stop();
219                         _player_form->hideCountdown();
220                 }
221         }
222 }
223
224 void MainWindow::_timeout() {
225         _timeout_interval -= 1000;
226         if (_timeout_interval <= 0) {
227                 _player_form->stop();
228                 _player_form->hideCountdown();
229                 _timer->stop();
230         } else {
231                 int h = _timeout_interval / 3600000;
232                 int m = (_timeout_interval / 60000) - 60*h;
233                 int s = (_timeout_interval / 1000) - 3600*h - 60*m;
234                 QString hp = h < 10 ? QString("0%1").arg(h) : QString("%1").arg(h);
235                 QString mp = m < 10 ? QString("0%1").arg(m) : QString("%1").arg(m);
236                 QString sp = s < 10 ? QString("0%1").arg(s) : QString("%1").arg(s);
237                 _player_form->showCountdown(hp+":"+mp+":"+sp);
238         }
239 }
240
241 void MainWindow::_equalizer() {
242         if (_player_form->isEqualizerAvailable()) {
243                 double val = 0;
244                 for (int i = 0; i < 10; i++) {
245                         _player_form->equalizerValue(i, &val);
246                         _equalizer_dialog->setValue(i, (int)(val * 10 + 0.5));
247                 }
248                 _equalizer_dialog->setEqualizerEnabled(_player_form->isEqualizerEnabled());
249                 _equalizer_dialog->reloadPresets();
250                 _equalizer_dialog->exec();
251         } else {
252                 QMessageBox::information(this, "Error", "No equalizer support. Please install gstreamer0.10-plugins-good-extra");
253         }
254 }
255
256 void MainWindow::_equalizer_value_changed(int band, int val) {
257         _player_form->setEqualizerValue(band, (val / 10.0));
258 }
259
260 void MainWindow::settings() {
261         SettingsDialog dialog;
262         dialog.exec();
263         Config config;
264         _library_form->refresh();
265         QString mode = config.getValue("ui/orientation").toString();
266         if (mode == "landscape") {
267                 setAttribute(Qt::WA_Maemo5LandscapeOrientation);
268         } else if (mode == "portrait") {
269                 setAttribute(Qt::WA_Maemo5PortraitOrientation);
270         } else if (mode == "auto") {
271                 setAttribute(Qt::WA_Maemo5AutoOrientation);
272         }
273         _player_form->updateIcons();
274         _library_form->updateIcons();
275         _manage_library_form->updateIcons();
276         _player_form->checkGradient();
277         _library_form->checkGradient();
278         _directory_form->updateIcons();
279         _directory_form->updateGradient();
280         QTranslator *translator = new QTranslator(this);
281         translator->load(QString("/opt/someplayer/someplayer_%1").arg(config.getValue("ui/language").toString()));
282         QApplication::installTranslator(translator);
283 }
284
285 void MainWindow::_orientation_changed() {
286         QRect screenGeometry = QApplication::desktop()->screenGeometry();
287         if (screenGeometry.width() > screenGeometry.height()) {
288                 _player_form->landscapeMode();
289                 _library_form->landscapeMode();
290                 _equalizer_dialog->landscapeMode();
291                 _directory_form->lanscapeMode();
292         } else {
293                 _player_form->portraitMode();
294                 _library_form->portraitMode();
295                 _equalizer_dialog->portraitMode();
296                 _directory_form->portraitMode();
297         }
298 }
299
300 void MainWindow::_fullscreen(bool f) {
301         if (f) showFullScreen();
302         else showNormal();
303 }
304
305 void MainWindow::_add_tracks(QList<Track> tracks) {
306         Playlist cur = _library->getCurrentPlaylist();
307         foreach (Track track, tracks) {
308                 cur.addTrack(track);
309         }
310         _library->saveCurrentPlaylist(cur);
311         _player_form->reload(true);
312 }