 /* 
 
        The wireless menus are derived from parts of KNetworkmanager by
        Timo Hoenig        <thoenig@suse.de>, <thoenig@nouse.net>
        Will Stephenson    <wstephenson@suse.de>, <wstephenson@kde.org>
        Valentine Sinitsyn <e_val@inbox.ru>
        
        (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 "kwlantraywin.h"
#include "kwlaninterface.h"
#include "kdialup.h"
#include "kstartsupplicantdlg.h"
#include "kwlansettingsdlg.h"
#include "globals.h"
#include "kwlnetworklist.h"
#include "kprofileconfigdlg.h"
#include "dialup.h"

#include <kstandardaction.h>
#include <QMenu>
#include <kaction.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kdebug.h>
#include <kpassivepopup.h>
#include <QPixmap>
#include <QProgressBar>
#include <QStyle>
#include <QRadioButton>


bool KWirelessActionWidget::event(QEvent *event){
   switch (event->type())
    {
        case QEvent::Enter:
            setBackgroundRole(QPalette::Highlight);
            update();
            break;
        case QEvent::Leave:
            setBackgroundRole(QPalette::Window);
            update();
            break;
        default:
            return QWidget::event(event);
    }
    return TRUE;
}




KWirelessActionWidget::KWirelessActionWidget (QWidget* parent, const kwlNetwork &prof, bool isSelected)
    : QWidget (parent)
{
    QString ssid       = prof.m_ssid;
    m_parent = parent;
    bool adhoc = prof.m_adhoc;
    bool isConfigured = prof.m_isConfigured;
    bool isEncrypted;
    setAutoFillBackground(TRUE);
    if (prof.m_flags.isEmpty()) isEncrypted = FALSE;
    else isEncrypted = TRUE;
    // Some drivers don't return a value for signal or quality
    // if signal is nat set, use quality as strength level; 
    int qual = prof.m_quality.toInt();
    int sig = prof.m_signal.toInt();
    int strength;
    if (sig == 0) strength = qual;
    else strength = sig;
	// TODO make all this neat or at least static
    /* px between pmLock and pbarStrength */
    //int space     = 5;
    /* the following code should not be used as teaching material in any kind */
	// set the widget height to that of a small icon
    int widgetHeight = SmallIcon ("security-high").height ();
    //int widgetHeight = QStyle::PM_SmallIconSize;
	// figure out the height of the text
    int textHeight = kapp->fontMetrics ().size (Qt::AlignLeft | Qt::AlignVCenter, ssid).height ();
	// the item is height of the text plus the default frame width
    int height = textHeight + kapp->style()->pixelMetric (QStyle::PM_DefaultFrameWidth);
    /* _height needs to be at least the height of a SmallIcon plus a border of one px (top & bottom)*/
    if (height < widgetHeight) {
        height = widgetHeight + 2;
    }

    /* border required for pmLock and pbarStrength */
    int border = 1;
    if (border == kapp->style ()->pixelMetric (QStyle::PM_DefaultFrameWidth))
        border += 2 * kapp->style ()->pixelMetric (QStyle::PM_DefaultFrameWidth);
    if (border == 0)
        border ++;
    QProgressBar *strengthGraph = new QProgressBar (parent); //, "strengthGraph");

    QHBoxLayout *layout = new QHBoxLayout(this);

    layout->setSpacing(2);
    layout->setMargin(2);
    

    
    strengthGraph->setRange (0,100);
    //strengthGraph->setSizePolicy (QSizePolicy ((QSizePolicy::SizeType) 0, (QSizePolicy::SizeType) 0, 0, 0, strengthGraph->sizePolicy ().hasHeightForWidth ()));
    strengthGraph->setFixedHeight (height - 2);
    strengthGraph->setValue (strength);
    
    QRadioButton *rbSelected = new QRadioButton(parent);
    rbSelected->setText(ssid);
    rbSelected->setChecked(isSelected);
    rbSelected->setFixedWidth(100);
    layout->addWidget(rbSelected);
    
    connect (rbSelected,SIGNAL(clicked(bool)), this, SLOT(slotTriggered(bool)));

    QPixmap pmLock;
    if (isEncrypted) {
        pmLock = SmallIcon ("security-high");//, QIcon::Automatic);
    }
    else pmLock = SmallIcon ("security-low");//, QIcon::Automatic);
    QLabel *lbLock = new QLabel(parent);
    lbLock->setPixmap(pmLock);
    layout->addWidget(lbLock);
    QPixmap pmNew = SmallIcon ("bookmarks");//, QIcon::Automatic);
    if (!isConfigured) {
        QLabel *lbNew = new QLabel(parent);
        lbNew->setPixmap(pmNew);
        layout->addWidget(lbNew);
    } 
    else  layout->addSpacing(pmNew.size ().width ());

    QPixmap pmAdHoc = SmallIcon ("computer");//, QIconSet::Automatic);
    if (adhoc) {
        QLabel *lbAdHoc = new QLabel(parent);
        lbAdHoc->setPixmap(pmAdHoc);
        layout->addWidget(lbAdHoc);
    } 
    else  layout->addSpacing(pmAdHoc.size ().width ());
    layout->addWidget(strengthGraph);
    setLayout(layout);
    
}

void KWirelessActionWidget::slotTriggered(bool checked)
{
    emit sigTriggered(checked);
}

KWirelessActionWidget::~KWirelessActionWidget ()
{
}
        
KWirelessAction::KWirelessAction(QWidget * parent, const kwlNetwork & prof, bool isSelected) : QWidgetAction(parent)
{
    //m_widget = new QWidget(parent);
    m_widget = new KWirelessActionWidget(parent,prof,isSelected);
    connect(m_widget,SIGNAL(sigTriggered(bool)),this, SLOT(slotTriggered(bool)));
    QString ssid       = prof.m_ssid;
    setText(ssid);
return;

}

void KWirelessAction::slotTriggered(bool sel)
{
    emit triggered(sel);
}



QWidget* KWirelessAction::createWidget ( QWidget * parent )
{
    if (m_widget)
        m_widget->setParent(parent);
    return  m_widget;
}

KwlanTrayWin::KwlanTrayWin(QWidget *parent, const char *name)
    : KSystemTrayIcon(name,parent)
{
    m_interface = 0L;
    m_ifname="";

    setIcon(loadIcon("kwlan"));
    m_profileMenu = new QMenu();
    m_wpaMenu = new QMenu();
    m_dialupMenu = new QMenu();
    QActionGroup *wpaActions = new QActionGroup(m_wpaMenu);
    connect (wpaActions,SIGNAL(triggered (QAction *)), this, SLOT(slotHandleWpaMenu(QAction*)));
    m_wpaMenu->clear();

    m_startWpa = m_wpaMenu->addAction(i18n("Start WPA Supplicant"));
    wpaActions->addAction(m_startWpa);
    m_stopWpa = m_wpaMenu->addAction(i18n("Stop WPA Supplicant"));
    wpaActions->addAction(m_stopWpa);
    m_logWpa = m_wpaMenu->addAction(i18n("Log Viewer"));
    wpaActions->addAction(m_logWpa);
    m_settingsWpa = m_wpaMenu->addAction(i18n("Settings"));
    wpaActions->addAction(m_settingsWpa);
    //connect (m_wpaMenu,SIGNAL(activated(int )), this, SLOT(handleWpaMenuEvent(int )));
    m_stopWpa->setEnabled(FALSE);
    m_interfacesMenu = new QMenu();
    m_configureInterfacesMenu = new QMenu();
    m_timer = new QTimer(this);
    if (m_timer){
        connect (m_timer, SIGNAL(timeout()),SLOT(updateIcon()));
        m_timer->start(100);
    }
    m_configuration = 0L;
    m_contextMenu = new QMenu();
    setContextMenu(m_contextMenu);
    connect(contextMenu(), SIGNAL(aboutToShow()), this, SLOT(contextMenuAboutToShow()));
}

KwlanTrayWin::~KwlanTrayWin() {
    if (m_timer){
        m_timer->stop();
        delete (m_timer);
    } 
}

void KwlanTrayWin::setInterface(KwlanInterface *interface)
{
    if (!interface) return;
    m_interface = interface;
    connect (this, SIGNAL(interfaceEnable( bool )), m_interface, SLOT (enableInterface(bool)));
    connect (m_interface, SIGNAL(interfaceGone(KwlanInterface*)), this, SLOT (slotInterfaceGone(KwlanInterface* )));
    connect (this, SIGNAL(viewLog()),m_interface,SLOT(logViewer()));
    connect (this, SIGNAL(scan()), m_interface, SLOT(slotScan()));
    connect (this, SIGNAL(profileChanged(QString)), m_interface, SLOT(profileActivate(QString)));
    connect (m_interface, SIGNAL(wpaStatusChanged(bool)), this, SLOT(enableStartWpaMenu(bool)));
    connect (m_interface,SIGNAL(sigMessage( QString )),this,SLOT(say( QString )));
    m_confFile = QString("kwlanrc.") + interface->getInterfaceName();
    m_configuration = new Configuration(m_confFile);
    enableStartWpaMenu( interface->getWpaStarted());
    updateNetworks();

}

 
void KwlanTrayWin::contextMenuAboutToShow( ) {
    m_contextMenu->clear();
    QString  curProf;
    updateNetworks();
    if (! m_interface) 
    {
        kDebug() << "Context menu cannot be shown: No interface found!!!";
        return;
    }

    curProf = m_interface->getCurrentProfile();
    int id;
    m_contextMenu->setTitle(i18n("Kwlan - KDE network manager"));
    m_contextMenu->setIcon(SmallIcon ("kwlan"));
    
    if (m_interface->getConfigureInterface())
    {
        // Now fill in the profiles into profile menu
        if (m_interface->getType()!=KwlanInterface::PPP)
        {
            if (!(m_interface->getMainWin())) return;
            QStringList interfaces = ((m_interface->getMainWin())->getInterfaces());
            QActionGroup *interfaceActions = new QActionGroup(m_interfacesMenu);
            interfaceActions->setExclusive(FALSE);
            connect (interfaceActions,SIGNAL(triggered (QAction *)), this, SLOT(slotHandleInterfacesMenu(QAction*)));
            QActionGroup *configureInterfaceActions = new QActionGroup(m_configureInterfacesMenu);
            configureInterfaceActions->setExclusive(FALSE);
            connect (configureInterfaceActions,SIGNAL(triggered (QAction *)), this, SLOT(slotHandleConfigureInterfacesMenu(QAction*)));
            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_configureInterfacesMenu->setTitle(i18n("Configure interfaces"));
            m_contextMenu->addMenu(m_configureInterfacesMenu);
            
            QActionGroup *profileActions = new QActionGroup(m_profileMenu);
            connect (profileActions,SIGNAL(triggered (QAction *)), this, SLOT(slotHandleProfilesMenu(QAction*)));
            m_profileMenu->clear();
            kwlNetworkList::iterator prof; 
            for (prof = m_profiles.begin(); prof != m_profiles.end(); prof++)
            {
                QString text = (*prof).m_ssid;
                if (m_interface->getInterfaceData().wirelessDevice)
                {
                    KWirelessAction *act = new KWirelessAction(m_profileMenu, *prof, ((*prof).m_ssid == curProf));
                    m_profileMenu->addAction(act);
                    text = (*prof).m_ssid;
                    profileActions->addAction(act);
                }
                else {
                    QAction *act;
                    act = m_profileMenu->addAction(text);
                    profileActions->addAction(act);
                    act->setCheckable(TRUE);
                    act->setChecked(((*prof).m_ssid == curProf));
                }
                m_networkMap[text]=*prof;
            }
            m_profileMenu->setTitle(i18n("Activate Profile"));
            m_contextMenu->addMenu(m_profileMenu);
            m_wpaMenu->setTitle(i18n("Wpa"));
            m_contextMenu->addMenu(m_wpaMenu);
            m_contextMenu->addSeparator();
        }
    }
    // Now show dialup Menu
    if (m_interface->getType()!=KwlanInterface::PPP)
    {
        QStringList dialupNetworks = listDialupNetworks();
        QActionGroup *dialupActions = new QActionGroup(m_dialupMenu);
        dialupActions->setExclusive(TRUE);
        connect (dialupActions,SIGNAL(triggered (QAction *)), this, SLOT(slotHandleDialupMenu(QAction*)));
        m_dialupMenu->clear();
        for (QStringList::iterator it = dialupNetworks.begin(); it !=dialupNetworks.end();it++)
        {
            QAction *act;
            act = m_dialupMenu->addAction(*it);
            dialupActions->addAction(act);
            act->setCheckable(FALSE);
        }
        m_dialupMenu->setTitle(i18n("Connect dialup"));
        m_contextMenu->addMenu(m_dialupMenu);
        m_contextMenu->addSeparator();
    }
    //now set the interface enable / disable menu
    if (m_interface->getConfigureInterface() || (m_interface->getType() == KwlanInterface::PPP))
    {
        QString enableMenu;
        if (m_interface->getInterfaceData().up) enableMenu = i18n("Disable");
        else enableMenu = i18n("Enable");
        //m_interfaceEnableMenu->setText(enableMenu);
        //m_contextMenu->addMenu(m_interfaceEnableMenu);
        m_interfaceEnableAction = m_contextMenu->addAction(enableMenu);
        connect (m_interfaceEnableAction,SIGNAL(activated()),this, SLOT(interfaceEnable()));
        //m_interfaceEnableMenu->plug(r);
        m_contextMenu->addSeparator();
    }
    if (m_interface->getType() != KwlanInterface::PPP) 
    {
        m_about = m_contextMenu->addAction(SmallIcon ("kwlan"),i18n("About Kwlan"));
        connect (m_about,SIGNAL(activated()),this, SLOT(slotAboutSelected()));
        m_quit = m_contextMenu->addAction(SmallIcon ("application-exit"),i18n("Quit"));
        connect (m_quit,SIGNAL(activated()), this, SLOT(slotQuitSelected()));
    }
    
}
void KwlanTrayWin::slotHandleInterfacesMenu( QAction *action)
{
    //if (::debugOutput) 
        kDebug()<< "Monitor interface " <<action->text() << " is " << action->isChecked();
    mainConfiguration().writeMonitorInterface( action->text(), action->isChecked());
    emit sigMonitorInterface( action->text(),action->isChecked());
}

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

void KwlanTrayWin::slotHandleDialupMenu(QAction *action)
{
    if (::debugOutput) 
        kDebug()<< "Connect dialup:  " <<action->text();
    // get network
    KDialup *dialup = new KDialup();
    QString sel= action->text();
    dialup->connectNetwork(sel);

}

void KwlanTrayWin::updateNetworks()
{
    kwlNetworkList networks;
    m_profiles.clear();
    //clearProfiles();
    if (!m_interface) return;
    //networks= m_interface->listProfiles();
    if (mainConfiguration().readShowAvailableOnly())
        networks = m_interface->getAvailableProfiles();
    else {
        QStringList tmpNetworks = m_interface->listProfiles();
        for (QStringList::Iterator it = tmpNetworks.begin(); it != tmpNetworks.end();it++)
            networks.append(kwlNetwork(*it));
    } 
    for (kwlNetworkList::Iterator it=networks.begin();it != networks.end();it++){
        //if ((*it).m_isConfigured)
            m_profiles.append(*it);
    }
}

            
void KwlanTrayWin::interfaceEnable()
{
    if (!m_interface) return;
    emit (interfaceEnable(!m_interface->getInterfaceData().up));
}
const QString & KwlanTrayWin::getInterfaceName() const {
    return m_ifname;
}
        
void KwlanTrayWin::slotSettings()
{
    if (!m_interface) return;
    KwlanSettingsDlg *settings = new KwlanSettingsDlg();
    Configuration *configuration = m_interface->getConfiguration();
    settings->setConfiguraton( configuration);

    if (!settings) return;
    settings->setWindowTitle(i18n("Settings for %1",m_ifname));
    settings->show();
    settings->exec();
}


void KwlanTrayWin::updateIcon()
{
    //QPixmap trayIcon;
    // Do we have a wireless interface?
    if (!m_interface ) return;
    interfaceData data = m_interface->getInterfaceData();
    QString statusIcon = getIcon();
    if (data.connected)
    {
        if (data.prevTxBytes == data.txBytes)
        {
            if (data.prevRxBytes == data.rxBytes) statusIcon +=QString("-none");
            else statusIcon +=QString("-rx");
        }
        else if (data.prevRxBytes == data.rxBytes) statusIcon +=QString("-tx");
        else  statusIcon += QString("-both");
    }
    else statusIcon += QString("-disconnected") ;
    setIcon(loadIcon(statusIcon));
}


void KwlanTrayWin::setInterfaceName(const QString &ifname)
{
    m_ifname = ifname;
}

void KwlanTrayWin::setIpAddress(const QString &ipaddress){
    m_ipaddress = ipaddress;
}


void KwlanTrayWin::slotHandleProfilesMenu(QAction *act)
{
    if (:: debugOutput)
        kDebug() <<"select Profile " << m_networkMap[act->text()].m_ssid;
    emit profileChanged(m_networkMap[act->text()].m_ssid);
}

void KwlanTrayWin::wpaStart()
{ 
    QString driver;
    if (!m_interface) return;
    if (m_interface->getInterfaceData().wirelessDevice)
    {
        KStartSupplicantDlg *startSupplicant = new KStartSupplicantDlg();
        if (!startSupplicant) return;
        if (!m_configuration) return;
        m_configuration->readDriver(driver);
        QString iface = m_interface->getInterfaceName();
        startSupplicant->setData( & iface ,& driver);
        startSupplicant->show();
        startSupplicant->exec();
        if (startSupplicant->m_start){
            m_interface->startWpa(startSupplicant->getDriver());
        }
    }

    else {
        //wired device, so drvier is wired
        m_interface->startWpa(QString("wired"));
    }
}

void KwlanTrayWin::wpaStop()
{
    if (!m_interface) return;
    m_interface->stopWpa();
}
void KwlanTrayWin::slotHandleWpaMenu(QAction *action)
{
    if (action == m_startWpa)
        wpaStart();
    else if (action == m_stopWpa)
        wpaStop();
    else if (action == m_logWpa)
        emit (viewLog());
    else if (action == m_settingsWpa)
        slotSettings();
}

void KwlanTrayWin::enableStartWpaMenu(bool enable)
{
    m_startWpa->setEnabled(!enable);
    m_stopWpa->setEnabled(enable);
    m_logWpa->setEnabled(enable);
}

void KwlanTrayWin::slotInterfaceGone(KwlanInterface *interface)
{
    if (::debugOutput) kDebug() << "Closing traywin...";
    m_interface=0L;
    m_timer->stop();
    //QWidget::close(TRUE);
    deleteLater();
}

void KwlanTrayWin::slotShowTrayWin(QString interface, bool showWin)
{
    if (interface != m_ifname) return;
    if (showWin) show();
    else hide();
}


void KwlanTrayWin::mousePressEvent ( QMouseEvent * event )
{
    if (debugOutput) kDebug() << "Tray Win clicked";
    //if ( rect().contains( event->pos() ) )
        emit sigTrayWinClicked(m_ifname);
    //slotClicked(event);
   //KSystemTrayIcon::mousePressEvent(event);
}

QString KwlanTrayWin::getIcon()
{
    QString statusIcon = QString("");
    wirelessData wData = m_interface->getWirelessData();
    if (m_interface->getType() == KwlanInterface::PPP)
        statusIcon = "kwlan-dialup";
    else if (!m_interface->getInterfaceData().wirelessDevice)
        statusIcon = "kwlan-wired";
    else {
        if (m_interface->getInterfaceData().connected)
        {
            int index = wData.linkQualityNum;
            if (index > 66)
                statusIcon = QString("kwlan-wireless3");
            else if (index > 33)
                statusIcon = QString("kwlan-wireless2");
            else
                statusIcon = QString("kwlan-wireless1");
        }
        else statusIcon = QString("kwlan-wireless");
    }
    return statusIcon;
}

void KwlanTrayWin::say(QString message)
{
    if (this->isVisible())
//        showMessage("Kwlan", message, qApp->windowIcon().pixmap(IconSize(KIconLoader::Small)), 3000);
        showMessage(i18n("Kwlan"), message,QSystemTrayIcon::Information , 3000);
}
    
void KwlanTrayWin::slotQuitSelected()
{
    emit(quitSelected());
}

void KwlanTrayWin::slotAboutSelected()
{
    emit (aboutSelected());
}
    
       
                
bool KwlanTrayWin::event(QEvent *event)                
{
    if (event->type() == QEvent::ToolTip) {
        slotUpdateToolTip();
    }
    return QSystemTrayIcon::event(event);
}

void KwlanTrayWin::slotUpdateToolTip() {
    QString tooltip;
        
    // We can't show tooltip if no interface was specified
    if (!getInterface())
    {
        kDebug() << "No interface for tooltip found";
        return;
    }
    const QString &ifname = getInterfaceName();
    interfaceData data = getInterface()->getInterfaceData();
    wirelessData wData;
    if (data.wirelessDevice)
         wData = getInterface()->getWirelessData();
    //QString network = parent->getProfile();
    QString network = getInterface()->getCurrentProfile();
    // Get the URLs of the icons
    QString dialup,wireless_connected,wireless_disconnected,wired_connected,wired_disconnected;
    KIconLoader *kIcon = new KIconLoader();
    dialup = kIcon->iconPath("kwlan-dialup",KIconLoader::Desktop);
    //kDebug() << "Icon for dialup = " << dialup;
    wireless_connected = kIcon->iconPath(getIcon()+QString("-none"),KIconLoader::Desktop);
    wireless_disconnected = kIcon->iconPath("kwlan-wireless-disconnected",KIconLoader::Desktop);
    wired_connected = kIcon->iconPath("kwlan-wired-none",KIconLoader::Desktop);
    wired_disconnected = kIcon->iconPath("kwlan-wired-disconnected",KIconLoader::Desktop);
    //if (data.connected) {
    if (getInterface()->getState()==KwlanInterface::CONFIGURED) {
        if (getInterface()->getType() == KwlanInterface::PPP)
        {
            tooltip = QString("<center><img align=center src=%1></center>").arg(dialup);
        }
        if (data.wirelessDevice) {
            tooltip = QString("<center><img align=center src=%1></center>").arg(wireless_connected);
        }
        else {
            tooltip = QString("<center><img align=center src=%1></center>").arg(wired_connected);
        }
        tooltip += QString("<center><table border=0 bgcolor=#000000 cellspacing=1 cellpadding=1>");
        if (network.isEmpty())
            tooltip += i18n("<tr><td colspan=2 bgcolor=#04CC1A><center><b>Interface:</b> [ %1 ]</center></td>",ifname);
        else
            tooltip += i18n("<tr><td colspan=2 bgcolor=#04CC1A><center><b>Interface:</b> [ %1 ]<br>Connected to %2</b></center></td>",ifname,network);        
        if (data.wirelessDevice) {
            if (wData.essid.isEmpty())
                tooltip += i18n("<tr><td bgcolor=#CCCCCC><b>Connected to:</b></td><td bgcolor=#CCCCCC><center>Hidden</center></td></tr>");
            else
            tooltip += i18n("<tr><td bgcolor=#CCCCCC><b>Connected to:</b></td><td bgcolor=#CCCCCC><center>%1</center></td></tr>",wData.essid);
            
            if (!wData.bitRate.isEmpty())
                tooltip += i18n("<tr><td bgcolor=#EEEEEE><b>Bitrate:</b></td><td bgcolor=#EEEEEE><center>%1 Mbit/s</center></td></tr>",wData.bitRate);
            if (!wData.linkQuality.isEmpty())
                tooltip += i18n("<tr><td bgcolor=#CCCCCC><b>Link quality:</b></td><td bgcolor=#CCCCCC><center>%1</center></td></tr>",wData.linkQuality);
            if (!wData.signal.isEmpty())
                tooltip += i18n("<tr><td bgcolor=#EEEEEE><b>Signal Level:</b></td><td bgcolor=#EEEEEE><center>%1</center></td></tr>",wData.signal);
            if (!wData.frequency.isEmpty())
                tooltip += i18n("<tr><td bgcolor=#CCCCCC><b>Frequency:</b></td><td bgcolor=#CCCCCC><center>%1</center></td></tr>",wData.frequency);
            if (!wData.channel.isEmpty())
                tooltip += i18n("<tr><td bgcolor=#EEEEEE><b>Channel:</b></td><td bgcolor=#EEEEEE><center>%1</center></td></tr>",wData.channel);
        }
        tooltip += i18n("<tr><td bgcolor=#CCCCCC><b>IP address:</b></td><td bgcolor=#CCCCCC><center>%1</center></td></tr>",data.ipAddress);
        tooltip += i18n("<tr><td bgcolor=#EEEEEE><b>Netmask:</b></td><td bgcolor=#EEEEEE><center>%1</center></td></tr>",data.subnetMask);
        tooltip += i18n("<tr><td bgcolor=#CCCCCC><b>Broadcast address:</b></td><td bgcolor=#CCCCCC><center>%1</center></td></tr>",data.broadcastAddress);
        tooltip += i18n("<tr><td bgcolor=#EEEEEE><b>Bytes received:</b></td><td bgcolor=#EEEEEE><center>%1</center></td></tr>",data.rxString);
        tooltip += i18n("<tr><td bgcolor=#CCCCCC><b>Bytes sent:</b></td><td bgcolor=#CCCCCC><center>%1</center></td></tr>",data.txString);
        tooltip += i18n("<tr><td bgcolor=#EEEEEE><b>Packets received:</b></td><td bgcolor=#EEEEEE><center>%1</center></td></tr>",data.rxPackets);
        tooltip += i18n("<tr><td bgcolor=#CCCCCC><b>Packets sent:</b></td><td bgcolor=#CCCCCC><center>%1</center></td></tr>",data.txPackets);
        tooltip += QString("</table></center>");
    }
    else {
        if (data.wirelessDevice) {
            tooltip = QString("<center><img align=center src=%1></center>").arg(wireless_disconnected);
        }
        else {
            tooltip = QString("<center><img align=center src=%1></center>").arg(wired_disconnected);
        }
        tooltip += QString("<center><table border=0 bgcolor=#000000 cellspacing=1 cellpadding=1>");
        switch (getInterface()->getState())
        {
            case KwlanInterface::DISABLED:
                tooltip += i18n("<tr><td bgcolor=#DD0500><center>[ %1 ] <b>is disabled</b></center></td></tr>",ifname);
                if (data.wirelessDevice && wData.radioOff) {
                    tooltip += i18n("<tr><td bgcolor=#CCCCCC><center>The radio is off</center></td></tr>");
                }
                break;
            case KwlanInterface::CONNECTED:
                tooltip += i18n("<tr><td bgcolor=#DD0500><center>[ %1 ] <b> is requesting ip address</b></center></td></tr>",ifname);
                break;
            case KwlanInterface::NOTCONNECTED:
                tooltip += i18n("<tr><td bgcolor=#DD0500><center>[ %1 ] <b>is not connected</b></center></td></tr>",ifname);
        }
        tooltip += QString("</table></center>");
    }
                 
    //tip(parent->rect(), tooltip);
    setToolTip(tooltip);
}



#include "kwlantraywin.moc"
                 
