FMTX support
[someplayer] / src / mainwindow.cpp
index a3acf75..2785cc7 100644 (file)
@@ -24,6 +24,8 @@
 #include <QInputDialog>
 #include <QFile>
 #include <QDesktopWidget>
+#include <QTranslator>
+#include <QKeyEvent>
 
 #include "player/player.h"
 
 #include "timerdialog.h"
 #include "equalizerdialog.h"
 #include "saveplaylistdialog.h"
-#include "settingsdialog.h"
+#include "settingsform.h"
+#include <QtGui/QX11Info>
+#include <X11/Xlib.h>
+#include <X11/Xatom.h>
+
 
 using namespace SomePlayer::DataObjects;
 using namespace SomePlayer::Storage;
+using namespace SomePlayer::Playback;
 
 MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
 {
+       _display_unlocked = true; // in most cases
        Config config;
        _library = new Library(config.applicationDir(), config.applicationDir());
+       _translator = new QTranslator(this);
        ui->setupUi(this);
        connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
        connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(settings()));
@@ -55,6 +64,8 @@ MainWindow::MainWindow(QWidget *parent) :
        _timer = new QTimer(this);
        _equalizer_dialog = new EqualizerDialog(this);
        _manage_library_form = new ManageLibraryForm(_library, this);
+       _settings_form = new SettingsForm(this);
+       _settings_form->hide();
        connect(_player_form, SIGNAL(library()), this, SLOT(library()));
        connect(_library_form, SIGNAL(refreshPlayer()), this, SLOT(player()));
        connect(ui->actionManageLibrary, SIGNAL(triggered()), this, SLOT(_manage_library()));
@@ -75,6 +86,21 @@ MainWindow::MainWindow(QWidget *parent) :
        connect(_directory_form, SIGNAL(addAndPlay(Track)), _player_form, SLOT(play(Track)));
        connect(_player_form, SIGNAL(dirView()), _directory_form, SLOT(show()));
        connect(_directory_form, SIGNAL(addTracks(QList<Track>)), this, SLOT(_add_tracks(QList<Track>)));
+       connect(_settings_form, SIGNAL(iconsChanged()), _player_form, SLOT(updateIcons()));
+       connect(_settings_form, SIGNAL(iconsChanged()), _library_form, SLOT(updateIcons()));
+       connect(_settings_form, SIGNAL(iconsChanged()), _manage_library_form, SLOT(updateIcons()));
+       connect(_settings_form, SIGNAL(iconsChanged()), _directory_form, SLOT(updateIcons()));
+       connect(_settings_form, SIGNAL(gradientChanged()), _player_form, SLOT(checkGradient()));
+       connect(_settings_form, SIGNAL(gradientChanged()), _library_form, SLOT(checkGradient()));
+       connect(_settings_form, SIGNAL(gradientChanged()), _directory_form, SLOT(checkGradient()));
+       connect(_settings_form, SIGNAL(libraryOptionsChanged()), _library_form, SLOT(refresh()));
+       connect(_settings_form, SIGNAL(orientationChanged()), this, SLOT(_change_orientation()));
+       connect(_settings_form, SIGNAL(translationChanged()), this, SLOT(updateTranslations()));
+       connect(_settings_form, SIGNAL(trackColorChanged()), _player_form, SLOT(updateTrackColor()));
+       connect(_settings_form, SIGNAL(hwZoomPolicyChanged()), this, SLOT(_hw_zoom_policy_changed()));
+       connect(&_dbus_client, SIGNAL(displayStateChanged(bool)), this, SLOT(_set_display_state(bool)));
+       connect(_settings_form, SIGNAL(fmtxSettingsChanged()), this, SLOT(_fmtx_settings_changed()));
+       connect(_player_form, SIGNAL(trackChanged()), this, SLOT(_update_fmtx_text()));
        _player_form->reload(true);
        QString mode = config.getValue("ui/orientation").toString();
        if (mode == "landscape") {
@@ -83,17 +109,20 @@ MainWindow::MainWindow(QWidget *parent) :
                _library_form->landscapeMode();
                _equalizer_dialog->landscapeMode();
                _directory_form->lanscapeMode();
+               _settings_form->landscapeMode();
        } else if (mode == "portrait") {
                setAttribute(Qt::WA_Maemo5PortraitOrientation);
                _player_form->portraitMode();
                _library_form->portraitMode();
                _equalizer_dialog->portraitMode();
                _directory_form->portraitMode();
+               _settings_form->portraitMode();
        } else if (mode == "auto") { // initialization in landscape
                _player_form->landscapeMode();
                _library_form->landscapeMode();
                _equalizer_dialog->landscapeMode();
                _directory_form->lanscapeMode();
+               _settings_form->landscapeMode();
                setAttribute(Qt::WA_Maemo5AutoOrientation);
        }
        _library_form->updateIcons();
@@ -102,7 +131,8 @@ MainWindow::MainWindow(QWidget *parent) :
        _directory_form->updateIcons();
        _player_form->checkGradient();
        _library_form->checkGradient();
-       _directory_form->updateGradient();
+       _directory_form->checkGradient();
+       _hw_zoom_policy_changed();
        setWindowTitle("SomePlayer");
 }
 
@@ -233,7 +263,7 @@ void MainWindow::_timeout() {
                QString hp = h < 10 ? QString("0%1").arg(h) : QString("%1").arg(h);
                QString mp = m < 10 ? QString("0%1").arg(m) : QString("%1").arg(m);
                QString sp = s < 10 ? QString("0%1").arg(s) : QString("%1").arg(s);
-               _player_form->showCountdown(hp+":"+mp+":"+sp);
+               _player_form->showCountdown(tr("Music off: ")+hp+":"+mp+":"+sp);
        }
 }
 
@@ -257,10 +287,11 @@ void MainWindow::_equalizer_value_changed(int band, int val) {
 }
 
 void MainWindow::settings() {
-       SettingsDialog dialog;
-       dialog.exec();
+       _settings_form->show();
+}
+
+void MainWindow::_change_orientation() {
        Config config;
-       _library_form->refresh();
        QString mode = config.getValue("ui/orientation").toString();
        if (mode == "landscape") {
                setAttribute(Qt::WA_Maemo5LandscapeOrientation);
@@ -269,13 +300,6 @@ void MainWindow::settings() {
        } else if (mode == "auto") {
                setAttribute(Qt::WA_Maemo5AutoOrientation);
        }
-       _player_form->updateIcons();
-       _library_form->updateIcons();
-       _manage_library_form->updateIcons();
-       _player_form->checkGradient();
-       _library_form->checkGradient();
-       _directory_form->updateIcons();
-       _directory_form->updateGradient();
 }
 
 void MainWindow::_orientation_changed() {
@@ -285,11 +309,13 @@ void MainWindow::_orientation_changed() {
                _library_form->landscapeMode();
                _equalizer_dialog->landscapeMode();
                _directory_form->lanscapeMode();
+               _settings_form->landscapeMode();
        } else {
                _player_form->portraitMode();
                _library_form->portraitMode();
                _equalizer_dialog->portraitMode();
                _directory_form->portraitMode();
+               _settings_form->portraitMode();
        }
 }
 
@@ -306,3 +332,79 @@ void MainWindow::_add_tracks(QList<Track> tracks) {
        _library->saveCurrentPlaylist(cur);
        _player_form->reload(true);
 }
+
+void MainWindow::updateTranslations() {
+       Config config;
+       if (config.getValue("ui/language").toString() != "en") {
+               _translator->load(QString("/opt/someplayer/someplayer_%1").arg(config.getValue("ui/language").toString()));
+               QApplication::installTranslator(_translator);
+       } else {
+               QApplication::removeTranslator(_translator);
+       }
+       ui->retranslateUi(this);
+       _player_form->updateTranslations();
+       _library_form->updateTranslations();
+       _equalizer_dialog->updateTranslations();
+       _manage_library_form->updateTranslations();
+       _directory_form->updateTranslations();
+       _settings_form->updateTranslations();
+}
+
+void MainWindow::_hw_zoom_policy_changed() {
+       Config config;
+       QString state = config.getValue("hw/zoomkeys").toString();
+       if (state == "enabled") {
+               _dbus_client.enableKeys();
+               connect(&_dbus_client, SIGNAL(zoomKeyPressed(quint32)), this, SLOT(_zoom_key_pressed(quint32)));
+       } else {
+               _dbus_client.disableKeys();
+               disconnect(&_dbus_client, SIGNAL(zoomKeyPressed(quint32)), this, SLOT(_zoom_key_pressed(quint32)));
+       }
+}
+
+void MainWindow::_set_display_state(bool state) {
+       _display_unlocked = state;
+       if (!_display_unlocked) { // remember volume level when blocking screen
+               _system_volume = _dbus_client.getVolume();
+       }
+}
+
+void MainWindow::_zoom_key_pressed(quint32 code) {
+       if (_display_unlocked) {
+               return;
+       }
+       Config config;
+       QString behavior = config.getValue("hw/zoom_action").toString();
+       if (code == MM_KEY_DOWN) {
+               if (behavior == "track") {
+                       _player_form->prev();
+                       _dbus_client.setVolume(_system_volume);
+               }
+       } else if (code == MM_KEY_UP) {
+               if (behavior == "track") {
+                       _player_form->next();
+                       _dbus_client.setVolume(_system_volume);
+               }
+       }
+}
+
+void MainWindow::_fmtx_settings_changed() {
+       Config config;
+       if (config.getValue("fmtx/enabled").toString() == "yes") {
+               QString station_name = config.getValue("fmtx/station_name").toString();
+               int frequency = config.getValue("fmtx/frequency").toInt();
+               system(QString("fmtx_client -p1 -f%1 -s\"%2\" -t\"%3\" 2>&1 >/dev/null")
+                      .arg(frequency)
+                      .arg(station_name)
+                      .arg(_player_form->playerCaption()).toAscii());
+       } else {
+               system("fmtx_client -p 0 2>&1 >/dev/null");
+       }
+}
+
+void MainWindow::_update_fmtx_text() {
+       Config config;
+       if (config.getValue("fmtx/enabled").toString() == "yes") {
+               system(QString("fmtx_client -t \"%1\" 2>&1 >/dev/null").arg(_player_form->playerCaption()).toAscii());
+       }
+}