AdvancedSettingsWidget::AdvancedSettingsWidget( QWidget* parent )
    : SettingsWidget( parent ),
      ui( new Ui::AdvancedSettingsWidget )
{
    ui->setupUi( this );

#ifdef Q_WS_X11
    ui->shortcuts->hide();
#else
    AudioscrobblerSettings settings;
    ui->sce->setTextValue( settings.raiseShortcutDescription() );
    ui->sce->setModifiers( settings.raiseShortcutModifiers() );
    ui->sce->setKey( settings.raiseShortcutKey() );

    connect( ui->sce, SIGNAL(editTextChanged(QString)), this, SLOT(onSettingsChanged()));
#endif
    connect( ui->proxyHost, SIGNAL(textChanged(QString)), this, SLOT(onSettingsChanged()));
    connect( ui->proxyPort, SIGNAL(textChanged(QString)), this, SLOT(onSettingsChanged()));
    connect( ui->proxyUsername, SIGNAL(textChanged(QString)), this, SLOT(onSettingsChanged()));
    connect( ui->proxyPassword, SIGNAL(textChanged(QString)), this, SLOT(onSettingsChanged()));
}
void
AdvancedSettingsWidget::saveSettings()
{
    qDebug() << "has unsaved changes?" << hasUnsavedChanges();

    if ( hasUnsavedChanges() )
    {
        AudioscrobblerSettings settings;
        settings.setRaiseShortcutKey( ui->sce->key() );
        settings.setRaiseShortcutModifiers( ui->sce->modifiers() );
        settings.setRaiseShortcutDescription( ui->sce->textValue() );

        aApp->setRaiseHotKey( ui->sce->modifiers(), ui->sce->key() );

        if ( ui->proxyHost->text().trimmed().isEmpty()
             && ui->proxyPort->text().trimmed().isEmpty()
             && ui->proxyUsername->text().trimmed().isEmpty()
             && ui->proxyPassword->text().trimmed().isEmpty() )
        {

        }
    }
}
void
Application::init()
{
    // Initialise the unicorn base class first!
    unicorn::Application::init();

    if ( !currentSession() )
    {
        // there won't be a current session if one wasn't created by the wizard

        QMap<QString, QString> lastSession = unicorn::Session::lastSessionData();
        if ( lastSession.contains( "username" ) && lastSession.contains( "sessionKey" ) )
            changeSession( lastSession[ "username" ], lastSession[ "sessionKey" ] );
    }

    initiateLogin( !currentSession() );

//    QNetworkDiskCache* diskCache = new QNetworkDiskCache(this);
//    diskCache->setCacheDirectory( lastfm::dir::cache().path() );
//    lastfm::nam()->setCache( diskCache );

    m_menuBar = new QMenuBar( 0 );

/// tray
    tray(); // this will initialise m_tray if it doesn't already exist

    /// tray menu
    QMenu* menu = new QMenu;
    menu->addMenu( new UserMenu() )->setText( "Accounts" );

    m_show_window_action = menu->addAction( tr("Show Scrobbler"));
    m_show_window_action->setShortcut( Qt::CTRL + Qt::META + Qt::Key_S );
    menu->addSeparator();

    {
        m_love_action = menu->addAction( tr("Love") );
        m_love_action->setCheckable( true );
        QIcon loveIcon;
        loveIcon.addFile( ":/meta_love_OFF_REST.png", QSize( 16, 16 ), QIcon::Normal, QIcon::Off );
        loveIcon.addFile( ":/meta_love_ON_REST.png", QSize( 16, 16 ), QIcon::Normal, QIcon::On );

        m_love_action->setIcon( loveIcon );
        m_love_action->setEnabled( false );
        connect( m_love_action, SIGNAL(triggered(bool)), SLOT(changeLovedState(bool)));
    }
    {
        m_tag_action = menu->addAction(tr("Tag")+ELLIPSIS);
        m_tag_action->setIcon( QIcon( ":/meta_tag_REST.png" ) );
        m_tag_action->setEnabled( false );
        connect( m_tag_action, SIGNAL(triggered()), SLOT(onTagTriggered()));
    }

    {
        m_share_action = menu->addAction(tr("Share")+ELLIPSIS);
        m_share_action->setIcon( QIcon( ":/meta_share_REST.png" ) );
        m_share_action->setEnabled( false );
        connect( m_share_action, SIGNAL(triggered()), SLOT(onShareTriggered()));
    }

    {
        m_ban_action = new QAction( tr( "Ban" ), this );
        QIcon banIcon;
        banIcon.addFile( ":/controls_ban_REST.png" );
        m_ban_action->setIcon( banIcon );
        m_ban_action->setEnabled( false );
    }
    {
        m_play_action = new QAction( tr( "Play" ), this );
        m_play_action->setCheckable( true );
        QIcon playIcon;
        playIcon.addFile( ":/controls_pause_REST.png", QSize(), QIcon::Normal, QIcon::On );
        playIcon.addFile( ":/controls_play_REST.png", QSize(), QIcon::Normal, QIcon::Off );
        m_play_action->setIcon( playIcon );
    }
    {
        m_skip_action = new QAction( tr( "Skip" ), this );
        QIcon skipIcon;
        skipIcon.addFile( ":/controls_skip_REST.png" );
        m_skip_action->setIcon( skipIcon );
        m_skip_action->setEnabled( false );
    }

#ifdef Q_WS_X11
    menu->addSeparator();
    m_scrobble_ipod_action = menu->addAction( tr( "Scrobble iPod..." ) );
    connect( m_scrobble_ipod_action, SIGNAL( triggered() ), ScrobbleService::instance().deviceScrobbler(), SLOT( onScrobbleIpodTriggered() ) );
#endif

    menu->addSeparator();

    m_visit_profile_action = menu->addAction( tr( "Visit Last.fm profile" ) );
    connect( m_visit_profile_action, SIGNAL( triggered() ), SLOT( onVisitProfileTriggered() ) );

    menu->addSeparator();

    m_submit_scrobbles_toggle = menu->addAction( tr("Enable Scrobbling") );
    m_submit_scrobbles_toggle->setCheckable( true );
    bool scrobblingOn = unicorn::UserSettings().value( "scrobblingOn", true ).toBool();
    m_submit_scrobbles_toggle->setChecked( scrobblingOn );
    ScrobbleService::instance().scrobbleSettingsChanged();

    connect( m_submit_scrobbles_toggle, SIGNAL(toggled(bool)), SLOT(onScrobbleToggled(bool)) );
    connect( this, SIGNAL(scrobbleToggled(bool)), &ScrobbleService::instance(), SLOT(scrobbleSettingsChanged()) );

    menu->addSeparator();

    QAction* quit = menu->addAction(tr("Quit %1").arg( applicationName()));

    connect(quit, SIGNAL(triggered()), SLOT(quit()));


    m_tray->setContextMenu(menu);


/// MainWindow
    m_mw = new MainWindow( m_menuBar );
    m_mw->addWinThumbBarButton( m_love_action );
    m_mw->addWinThumbBarButton( m_ban_action );
    m_mw->addWinThumbBarButton( m_play_action );
    m_mw->addWinThumbBarButton( m_skip_action );

    m_toggle_window_action = new QAction( this ), SLOT( trigger());
#ifndef Q_OS_LINUX
     AudioscrobblerSettings settings;
     setRaiseHotKey( settings.raiseShortcutModifiers(), settings.raiseShortcutKey() );
#endif
    m_skip_action->setShortcut( Qt::CTRL + Qt::Key_Right );
    m_tag_action->setShortcut( Qt::CTRL + Qt::Key_T );
    m_share_action->setShortcut( Qt::CTRL + Qt::Key_S );
    m_love_action->setShortcut( Qt::CTRL + Qt::Key_L );
    m_ban_action->setShortcut( Qt::CTRL + Qt::Key_B );

    // make the love buttons sychronised
    connect(this, SIGNAL(lovedStateChanged(bool)), m_love_action, SLOT(setChecked(bool)));

    // tell the radio that the scrobbler's love state has changed
    connect(this, SIGNAL(lovedStateChanged(bool)), SLOT(sendBusLovedStateChanged(bool)));

    // update the love buttons if love was pressed in the radio
    connect(this, SIGNAL(busLovedStateChanged(bool)), m_love_action, SLOT(setChecked(bool)));
    connect(this, SIGNAL(busLovedStateChanged(bool)), SLOT(onBusLovedStateChanged(bool)));

    // tell everyone that is interested that data about the current track has been fetched
    connect( m_mw, SIGNAL(trackGotInfo(XmlQuery)), SIGNAL(trackGotInfo(XmlQuery)));
    connect( m_mw, SIGNAL(albumGotInfo(XmlQuery)), SIGNAL(albumGotInfo(XmlQuery)));
    connect( m_mw, SIGNAL(artistGotInfo(XmlQuery)), SIGNAL(artistGotInfo(XmlQuery)));
    connect( m_mw, SIGNAL(artistGotEvents(XmlQuery)), SIGNAL(artistGotEvents(XmlQuery)));
    connect( m_mw, SIGNAL(trackGotTopFans(XmlQuery)), SIGNAL(trackGotTopFans(XmlQuery)));
    connect( m_mw, SIGNAL(trackGotTags(XmlQuery)), SIGNAL(trackGotTags(XmlQuery)));
    connect( m_mw, SIGNAL(finished()), SIGNAL(finished()));

    connect( m_mw, SIGNAL(trackGotInfo(XmlQuery)), this, SLOT(onTrackGotInfo(XmlQuery)));

    connect( m_show_window_action, SIGNAL( triggered()), SLOT( showWindow()), Qt::QueuedConnection );
    connect( m_toggle_window_action, SIGNAL( triggered()), SLOT( toggleWindow()), Qt::QueuedConnection );

    connect( this, SIGNAL(messageReceived(QStringList)), SLOT(onMessageReceived(QStringList)) );
    connect( this, SIGNAL( sessionChanged( unicorn::Session* ) ), &ScrobbleService::instance(), SLOT( onSessionChanged( unicorn::Session* ) ) );

    connect( &ScrobbleService::instance(), SIGNAL(trackStarted(Track,Track)), SLOT(onTrackStarted(Track,Track)));
    connect( &ScrobbleService::instance(), SIGNAL(paused(bool)), SLOT(onTrackPaused(bool)));

    connect( &RadioService::instance(), SIGNAL(trackSpooled(Track)), SLOT(onTrackSpooled(Track)) );

    // clicking on a system tray message should show the scrobbler
    connect( m_tray, SIGNAL(messageClicked()), m_show_window_action, SLOT(trigger()));

    // make sure cached scrobbles get submitted when the connection comes back online
    connect( m_icm, SIGNAL(up(QString)), &ScrobbleService::instance(), SLOT(submitCache()) );

    emit messageReceived( arguments() );

#ifdef Q_OS_MAC
    m_notify = new Notify( this );
    connect( m_notify, SIGNAL(clicked()), SLOT(showWindow()) );

    new CommandReciever( this );
#endif
}