/* 
   
        (c) 2006 by Thomas Michel <tom.michel@arcor.de>

        Kwlan is free software; you can redistribute it and/or modify
        it under the terms of the GNU Library General Public License as
        published by the Free Software Foundation; either version 2 of
        the License, or (at your option) any later version.

        Kwlan is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU Library General Public License for more details.

        You should have received a copy of the GNU Library General Public License
        along with this library; see the file COPYING.LIB.  If not, write to
        the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
        Boston, MA 02110-1301, USA.
*/
#include "kwlanmaintraywin.h"
#include "kwlanmainwin.h"
#include "kwlanmainsettingsdlg.h"
#include "configuration.h"
#include "globals.h"


#include <kstandardaction.h>
#include <QMenu>
#include <kaction.h>
#include <kactioncollection.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kdebug.h>




/// KwlanMainTrayWin is tray window for main application

KwlanMainTrayWin::KwlanMainTrayWin(QWidget *parent, const char *name)
    : KSystemTrayIcon(name,parent)
{
    m_interfacesMenu = new QMenu();
    m_configureInterfacesMenu = new QMenu();

    setIcon(loadIcon("kwlan"));
    setToolTip(i18n("Kwlan - Network manager for KDE"));
    
    //m_tooltip = new KwlanMainToolTip((QWidget *)this);
    m_contextMenu = new QMenu();
    setContextMenu(m_contextMenu);
    connect(contextMenu(), SIGNAL(aboutToShow()), this, SLOT(contextMenuAboutToShow()));
}

KwlanMainTrayWin::~KwlanMainTrayWin() {
}

void KwlanMainTrayWin::contextMenuAboutToShow( ) 
{
    m_contextMenu->clear();
    m_contextMenu->setTitle(i18n("Kwlan - KDE Network Manager"));
    m_contextMenu->setIcon(SmallIcon("kwlan"));
    m_interfacesMenu->clear();
    m_configureInterfacesMenu->clear();
    if (parent())
    {
        QStringList interfaces = ((KwlanMainWin *)parent())->getInterfaces();
        QActionGroup *interfaceActions = new QActionGroup(m_interfacesMenu);
        interfaceActions->setExclusive(FALSE);
        QActionGroup *configureInterfaceActions = new QActionGroup(m_configureInterfacesMenu);
        connect (interfaceActions,SIGNAL(triggered (QAction *)), this, SLOT(slotHandleInterfacesMenu(QAction*)));
        connect (configureInterfaceActions,SIGNAL(triggered (QAction *)), this, SLOT(slotHandleConfigureInterfacesMenu(QAction*)));
        configureInterfaceActions->setExclusive(TRUE);
        m_configureInterfacesMenu->clear();
        m_interfacesMenu->clear();
        for (QStringList::iterator it = interfaces.begin(); it !=interfaces.end();it++)
        {
            QAction *act;
            act = m_interfacesMenu->addAction(*it);
            interfaceActions->addAction(act);
            act->setCheckable(TRUE);
            act->setChecked(mainConfiguration().readMonitorInterface( *it));
                
            act = m_configureInterfacesMenu->addAction(*it);
            configureInterfaceActions->addAction(act);
            act->setCheckable(TRUE);
            act->setChecked(mainConfiguration().readConfigureInterface( *it));
        }
        m_interfacesMenu->setTitle(i18n("Monitor interfaces"));
        m_contextMenu->addMenu(m_interfacesMenu);
        m_contextMenu->setTitle(i18n("Configure interfaces"));
        m_contextMenu->addMenu(m_configureInterfacesMenu);
    }
    // Now create the settungs Menu
    m_settingsMenu = m_contextMenu->addAction(i18n("Settings"));
    connect (m_settingsMenu, SIGNAL(triggered()), this, SLOT(slotSettingsMenu()));
    m_contextMenu->addSeparator();
    // standard about and quit actions
    m_about = m_contextMenu->addAction(SmallIcon ("kwlan"),i18n("About Kwlan"));
    connect (m_about,SIGNAL(activated()),this, SLOT(slotAboutSelected()));
    m_quit = m_contextMenu->addAction(SmallIcon ("exit"),i18n("Quit"));
    connect (m_quit,SIGNAL(activated()), this, SLOT(slotQuitSelected()));
    
}

void KwlanMainTrayWin::slotHandleInterfacesMenu( QAction *act)
{
    if (::debugOutput) 
        kDebug()<< "Monitor interface " <<act->text();
    mainConfiguration().writeMonitorInterface( act->text(), act->isChecked());
    emit sigMonitorInterface( act->text(),act->isChecked());
}

void KwlanMainTrayWin::slotHandleConfigureInterfacesMenu( QAction  *act)
{
    if (::debugOutput) kDebug()<< "Configure interface " <<act->text();
    mainConfiguration().writeConfigureInterface( act->text(), act->isChecked());
    emit sigConfigureInterface( act->text(),act->isChecked());
}

void KwlanMainTrayWin::slotSettingsMenu()
{
    KwlanMainSettingsDlg *settings = new KwlanMainSettingsDlg();
    if (!settings) return;
    settings->show();
    settings->exec();
}

void KwlanMainTrayWin::slotQuitSelected()
{
    emit(quitSelected());
}

void KwlanMainTrayWin::slotAboutSelected()
{
    emit (aboutSelected());
}

/*
KwlanMainToolTip::KwlanMainToolTip(QWidget *parent) {
}
    
KwlanMainToolTip::~KwlanMainToolTip() {
    
}
        
void KwlanMainToolTip::maybeTip(const QPoint &p) {
    QString tooltip;
    tooltip = i18n("Kwlan - Network manager for KDE");
    showText(p,tooltip);
}
*/

#include "kwlanmaintraywin.moc"
                 
