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