/***************************************************************************
 *   Copyright (C) 2003-2005 by liuspider                                  *
 *   liuspider@users.sourceforge.net                                       *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

#define USE_SCIM_KDE_SETTINGS
#include "kdetraystatus.h"

#include <qpixmap.h>
#include <qimage.h>
#include <qiconset.h>
#include <kpopupmenu.h>
#include <kiconloader.h>

#include "src/skimglobalactions.h"
#include "utils/scimactions.h"

KDETrayStatus::KDETrayStatus(SkimPluginManager* ms, QWidget *parent, const char *name)
 : KSystemTray(parent, name)
{
    allModules = ms;
    KAction *action;

    if( ScimKdeSettings::show_Quit() )
      actionCollection()->action(0)->setShortcut(""); // clear shortcut, it is of no use
    else
      actionCollection()->clear();  //clear default 'quit' action
    
    contextMenu()->clear(); //get rid of default "title bar"
    
    KIconLoader *kil = KGlobal::iconLoader();
    
    //Set the Small icon size to the one specified for KDE MainToolbar group icons
    int defaultPanelIconSize = IconSize(KIcon::MainToolbar);
    QIconSet::setIconSize(QIconSet::Small, QSize(defaultPanelIconSize, defaultPanelIconSize));
    
    QIconSet skim_icon(kil->loadIcon("skim", KIcon::Panel));
    setPixmap( skim_icon.pixmap(QIconSet::Small, QIconSet::Normal) );
    #if ENABLE_DEBUG
    action = allModules->getActionCollection()->action( "reload" );
    if(action)
      action->plug(contextMenu());
      contextMenu()->insertSeparator();
    #endif

    action = allModules->getActionCollection()->action( "configure" );
    if(action)
      action->plug(contextMenu());
      
    action = allModules->getActionCollection()->action( "aboutapp" );
    if(action)
      action->plug(contextMenu());
          
    action = allModules->getActionCollection()->action( "aboutkde" );
    if(action)
      action->plug(contextMenu());

    connect( this, SIGNAL( quitSelected() ), allModules, SLOT( shutdown() ));
    connect( allModules->getInputServer(), 
      SIGNAL(updateFactoryInfoReq(const QString &, const QString &)),
      this, SLOT(updateIcon(const QString &, const QString &)));
    
    serveraction =
      dynamic_cast<ScimComboAction *>(allModules->getActionCollection()->action( "change_server" ));

    show();
}

KDETrayStatus::~KDETrayStatus()
{
}

void KDETrayStatus::mousePressEvent( QMouseEvent *e )
{
    if ( !rect().contains( e->pos() ) )
        return;

    switch ( e->button() ) {
    case LeftButton:
        if(serveraction)
          serveraction->slotActivated();
        break;
    default:
        KSystemTray::mousePressEvent(e);
        break;
    }
}

void KDETrayStatus::updateIcon(const QString &, const QString & icon)
{
    QPixmap newIcon(icon.ascii());
    QIconSet qicon(newIcon);

    setPixmap( qicon.pixmap(QIconSet::Small, QIconSet::Normal) );
}

#include "kdetraystatus.moc"
