/*
 * Copyright (C) 2002 Richard Moore <rich@kde.org>
 *               2004 Dirk Ziegelmeier <dziegel@gmx.de>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 */

#include <qslider.h>
//Added by qt3to4:
#include <Q3PtrList>

#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
#include <kaction.h>
#include <kiconloader.h>
#include <KStandardAction>

#include "actions.h"
#include "kdetv.h"
#include "channel.h"
#include "sourcemanager.h"
#include "volumecontroller.h"
#include "kdetvwidget.h"

#include "libkdetvactions.h"
#include "libkdetvactions.moc"

LibKdetvActions::LibKdetvActions( Kdetv *kdetv, const char *name )
    : QObject( kdetv, name ? name : "kdetv_actions" ),
      ktv(kdetv )
{
    connect(ktv->sourceManager(), SIGNAL(deviceChanged(const QString&)),
            this, SLOT( deviceChanged(const QString&)));
    connect(ktv->sourceManager(), SIGNAL(audioModeChanged(const QString&)),
            this, SLOT( checkAudioMode(const QString&)));

}

LibKdetvActions::~LibKdetvActions()
{
    qDeleteAll(  _devicesActionList );
    _devicesActionList.clear();
    qDeleteAll( _audioModeActionList );
    _audioModeActionList.clear();
}

void LibKdetvActions::createActions( KActionCollection *acts )
{
    // Standard Actions
    showSettings = KStandardAction::preferences( ktv, SLOT( settings() ), acts );

    showKeys = KStandardAction::keyBindings( this, SIGNAL( launchBindings() ), acts);

    // Application Actions
    chanLoadDefaults = acts->addAction("channel_load_defaults");
    chanLoadDefaults->setText(i18n("&Local Defaults..."));
    chanLoadDefaults->setIcon(KIcon("1leftarrow"));
    connect( chanLoadDefaults, SIGNAL(triggered()), ktv, SLOT(importDefault()));


    chanImportLegacy = acts->addAction("channel_import_legacy");
    chanImportLegacy->setText(i18n("Old &KWinTV Channels"));
    chanImportLegacy->setIcon(KIcon("1leftarrow"));
    connect( chanImportLegacy, SIGNAL(triggered()), ktv, SLOT(importLegacyChannels()));

    ctrl = acts->addAction("controls");
    ctrl->setText(i18n("&Video Hardware Controls"));
    ctrl->setIcon(KIcon("hwinfo"));
    connect( ctrl, SIGNAL(triggered()), ktv, SLOT(controls()));


    fltChainStatus = acts->addAction("filter_chain_status");
    fltChainStatus->setText(i18n("&Filter Chain Status"));
    fltChainStatus->setIcon(KIcon("filter"));
    connect( fltChainStatus, SIGNAL(triggered()), ktv, SLOT(filterChainStatus()));

    chanDown = acts->addAction("channel_down");
    chanDown->setText(i18n("Channel &Down"));
    chanDown->setIcon(KIcon("down"));
    chanDown->setShortcut( Qt::Key_Down );
    connect( chanDown, SIGNAL(triggered()), ktv, SLOT(channelDown()));


    chanUp = acts->addAction("channel_up");
    chanUp->setText(i18n("Channel &Up"));
    chanUp->setIcon(KIcon("up"));
    chanUp->setShortcut( Qt::Key_Up );
    connect( chanUp, SIGNAL(triggered()), ktv, SLOT(channelUp()));

    chanWiz = acts->addAction("channel_wizard");
    chanWiz->setText(i18n("Channel &Wizard"));
    chanWiz->setIcon(KIcon("wizard"));
    connect(chanWiz , SIGNAL(triggered()), ktv, SLOT(launchWizard()));

    chanEdit  = acts->addAction("channel_editor");
    chanEdit->setText(i18n("Channel &Editor"));
    chanEdit->setIcon(KIcon("edit"));
    connect(chanEdit , SIGNAL(triggered()), ktv, SLOT(editChannels()));


    snap = acts->addAction("save_snapshot");
    snap->setText(i18n("Save &Snapshot"));
    snap->setIcon(KIcon("camera"));
    snap->setShortcut( Qt::Key_S );
    connect(snap , SIGNAL(triggered()), ktv, SLOT(snapshot()));


    volMute = new KToggleAction( i18n("Mute"), this );

    QAction *act = acts->addAction("volume_mute" );
    volMute->setText(i18n("Mute"));
    volMute->setIcon(KIcon("player_mute"));
    volMute->setShortcut( Qt::Key_Asterisk );
    connect(act, SIGNAL( toggled(bool) ),
            ktv,            SLOT( toggleMute() ));


    volUp = acts->addAction("volume_up");
    volUp->setText(i18n("Volume Up"));
    volUp->setIcon(KIcon("player_volume_up"));
    volUp->setShortcut( Qt::Key_Right );
    connect(volUp , SIGNAL(triggered()), ktv, SLOT(volumeUp()));

    volDown= acts->addAction("volume_down");
    volDown->setText(i18n("Volume Down"));
    volDown->setIcon(KIcon("player_volume_down"));
    volDown->setShortcut( Qt::Key_Left );
    connect(volDown , SIGNAL(triggered()), ktv, SLOT(volumeDown()));

    prevChannel= acts->addAction("last_channel");
    prevChannel->setText(i18n("Previous Channel"));
    prevChannel->setIcon(KIcon("back"));
    prevChannel->setShortcut( Qt::Key_L );
    connect( prevChannel, SIGNAL(triggered()), ktv, SLOT(previousChannel()));

    // Custom Actions
    chan = new LCDNumberAction( i18n("Current Channel"), 0, 0, 0, acts,
                                "channel_number" );
    chan->setNumDigits( 3 );
    chan->setIcon(KIcon( "kdetv" ));

    connect(ktv, SIGNAL(channelChanged(int)),
            chan, SLOT(display(int)) );

    /*
     * This is confusing in configure toolbars dialog
     connect( ktv, SIGNAL(channelChanged(const QString &)), chan,
     SLOT(setText(const QString &)) );
    */

    volSlider = new SliderAction( 0, 100, 1, 50, i18n("Volume"), acts,
                                  "volume_slider" );
    volSlider->setTickInterval( 5 );
    volSlider->setIcon(KIcon( "kcmsound" ));

    connect(volSlider, SIGNAL(valueChanged(int)),
            ktv, SLOT(setVolume(int)));
    connect(ktv->volumeController(), SIGNAL(volumeChanged(int)),
            this, SLOT(updateSlider(int)));
    connect(ktv->volumeController(), SIGNAL(muted(bool)),
            this, SLOT(updateMuteButton(bool)));
}

const QList<QAction*>& LibKdetvActions::devicesActionList( KActionCollection *acts )
{
    const QStringList& devs = ktv->sourceManager()->deviceList();
    _devicesActionList.clear();

    if (devs.count() != 0) {
        for (QStringList::ConstIterator p = devs.constBegin();
             p != devs.constEnd();
             ++p) {
            DeviceAction* a = new DeviceAction(*p, 0, *p, ktv, acts);
#warning "port it";
            //a->setExclusiveGroup("kdetv_device_actions");
            _devicesActionList.append(a);
        }
    } else {
        KAction* a = new KAction(i18n("No Devices Found. Read FAQ at www.kdetv.org"), this);
        _devicesActionList.append(a);
    }

    checkDevice(ktv->sourceManager()->device());
    return _devicesActionList;
}

const QList<QAction*>& LibKdetvActions::audioModesActionList( KActionCollection *acts )
{
    const QStringList& am = ktv->sourceManager()->audioModes();
    _audioModeActionList.clear();

    if (am.count() != 0) {
        for (QStringList::ConstIterator p = am.constBegin();
             p != am.constEnd();
             ++p) {
            AudioModeAction* a = new AudioModeAction(*p, 0, *p, ktv, acts);
#warning "port it";
            //a->setExclusiveGroup("kdetv_audiomode_actions");
            _audioModeActionList.append(a);
        }
    } else {
        KAction* a = new KAction(i18n("Audio Mode Selection is Not Supported by Current Device/Source"), this);
        _audioModeActionList.append(a);
    }

    checkAudioMode(ktv->sourceManager()->audioMode());
    return _audioModeActionList;
}

void LibKdetvActions::deviceChanged( const QString& dev )
{
    showSettings->setEnabled( true );

    if ( !dev.isEmpty() ) {
        chan->display( ktv->channel() != NULL ?
                       QString::number(ktv->channel()->number())
                       : QString("---") );
        chanDown->setEnabled( ktv->sourceManager()->isTuner(dev) );
        chanUp->setEnabled( ktv->sourceManager()->isTuner(dev) );
        snap->setEnabled( true );
        chanWiz->setEnabled( true );
        ctrl->setEnabled( true );
        prevChannel->setEnabled( true );
        chanEdit->setEnabled( true );
        if(ktv->sourceManager()->supportsImageFiltering()) {
            fltChainStatus->setText(i18n("&Filter Chain Status"));
            fltChainStatus->setEnabled( true );
        } else {
            fltChainStatus->setText(i18n("Current Video Plugin Does Not Support Image Filtering"));
            fltChainStatus->setEnabled( false );
        }
    } else {
        chan->display( "---" );
        chanDown->setEnabled( false );
        chanUp->setEnabled( false );
        snap->setEnabled( false );
        chanWiz->setEnabled( false );
        ctrl->setEnabled( false );
        prevChannel->setEnabled( false );
        chanEdit->setEnabled( false );
        fltChainStatus->setEnabled( false );
    }

    checkDevice(dev);
}

void LibKdetvActions::checkDevice(const QString& dev)
{
    for (int i = 0; i < _devicesActionList.size(); ++i) {
        if (_devicesActionList.at(i)->text() == dev)
        {
            static_cast<KToggleAction*>(_devicesActionList.at(i))->setChecked(true);
            break;
        }
    }
}

void LibKdetvActions::checkAudioMode(const QString& mode)
{
    for (int i = 0; i < _audioModeActionList.size(); ++i) {
        if (_audioModeActionList.at(i)->text() == mode)
        {
            static_cast<KToggleAction*>(_audioModeActionList.at(i))->setChecked(true);
            break;
        }
    }
}

void LibKdetvActions::updateSlider(int vol)
{
    volUp->setEnabled(vol != 100);
    volDown->setEnabled(vol != 0);
    volSlider->setValue(vol);
}

void LibKdetvActions::updateMuteButton(bool muted)
{
    volMute->setChecked(muted);
}

void LibKdetvActions::enableAudioActions(bool enable)
{
    volSlider->setEnabled(enable);
    volUp->setEnabled(enable);
    volDown->setEnabled(enable);
    volSlider->setEnabled(enable);
}

