/***************************************************************************
                         kbandwidthview.h  -  description
                             -------------------
    copyright            : (C) 2007 by Hans Dembinski
    email                : kbandwidth@5th-heaven.org
    original author      : Niko Sommer
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 VER "2.0.4"

#include <kglobal.h>
#include <klocale.h>
#include <kconfig.h>
#include <kapplication.h>
#include <kmessagebox.h>
#include <kiconloader.h>
#include <kpopupmenu.h>
#include <kstdaction.h>
#include <kaction.h>

#include <qcursor.h>

#include "kbandwidth.h"

KBandwidth::KBandwidth(const QString& configFile, Type type, int actions, QWidget *parent, const char *name)
    : KPanelApplet(configFile, type, actions, parent, name)
{
  // Get the current application configuration handle
  kConfig = config();
  mainView = new KBandwidthView(kConfig,this);

  connect(mainView, SIGNAL(configChanged()),this,SIGNAL(updateLayout()));

  mainView->show();

  createMenu(this);
}


KBandwidth::~KBandwidth()
{}

void KBandwidth::mousePressEvent( QMouseEvent* event ){
  if( event->button() == RightButton)
    contMenu->exec( QCursor::pos() );
  else if ( event->button() == LeftButton)
  {
    contMenu->exec( QCursor::pos() );
  }
}

void KBandwidth::createMenu(QWidget * parent)
{
  contMenu = new KPopupMenu(parent);
  contMenu->insertTitle(SmallIcon("kbandwidth"),QString("KBandwidth"));

  KAction* settingAction = KStdAction::preferences(this, SLOT(preferences()), 0 );
  settingAction->setText(i18n("&Preferences"));

  KAction* helpAction    = KStdAction::help(this, SLOT(help()), 0 );
  helpAction->setShortcut(0);

  KAction* aboutAction   = KStdAction::aboutApp(this, SLOT(about()), 0 );
  aboutAction->setText(i18n("About KBandwidth"));

  settingAction->plug(contMenu);
  helpAction->plug(contMenu);
  aboutAction->plug(contMenu);
}


void KBandwidth::about()
{
  QString mess = i18n("KBandwidth applet - Version %1\nMaintained and enhanced by Hans Dembinski <kbandwidth@5th-heaven.org>.\nOriginal work from Niko Sommer.")
    .arg(VER);

  KMessageBox::information(0, mess);
}

void KBandwidth::help()
{
  QString mess = i18n("<p><b>Kbandwidth</b> is usable at once and you don't need to set up anything.</p><p>At the beginning, measurements will not be accurate, because the applet needs some time to figure out your maximum bandwidth.</p><p>If you don't like KBandwidth to figure out everything for itself, you can also set up the bandwidth limits by hand in the preferences dialog.</p>");

  KMessageBox::information(0, mess);
}

void KBandwidth::preferences()
{
  PreferencesDialog *p = new PreferencesDialog(this);
  p->show();
}

int KBandwidth::widthForHeight(int height) const
{
  const int newWidth = mainView->setHorizontal(height);
  return newWidth;
}

int KBandwidth::heightForWidth(int width) const
{
  const int newHeight = mainView->setVertical(width);
  return newHeight;
}

void KBandwidth::resizeEvent(QResizeEvent *e)
{
}

extern "C"
{
  KPanelApplet* init( QWidget *parent, const QString configFile)
  {
    KGlobal::locale()->insertCatalogue("KBandwidth");

    return new KBandwidth(configFile, KPanelApplet::Normal,
                          KPanelApplet::About | KPanelApplet::Help | KPanelApplet::Preferences,
                          parent, "KBandwidth");
  }
}
