/* 
 * This file is part of DesktopBSD, see the README file.
 *
 * Author: Peter Hofer <ph@desktopbsd.net>
 *         (C) 2004, 2005
 *
 * Copyright: BSD, see the COPYING file included in this distribution
 *
 */

#include <devdlistener.h>

#include <device.h>

#include <qstring.h>
#include <qmap.h>
#include <qpixmap.h>
#include <qprocess.h>

#include <kapp.h>
#include <kglobal.h>
#include <dcopclient.h>
#include <kpopupmenu.h>
#include <kiconloader.h>
#include <kpassivepopup.h>
#include <kmessagebox.h>


DevdListener::DevdListener() : DCOPObject(K_DCOP_OBJID) {
   DCOPClient  *dcl = kapp->dcopClient();
   if(!dcl->isRegistered())
      dcl->registerAs(K_DCOP_OBJID);
   dcl->setDefaultObject(K_DCOP_OBJID);
}

void  DevdListener::attachDevice(const QString&  devname) {
   QString  desc = Device(devname).getDesc();
   devDescs[devname] = desc;
	passivePopup("<i>" + desc + "</i>", tr("Device attached"));
}

void  DevdListener::launchKControl() {
	if(!QProcess(QString("/usr/local/bin/kcontrol")).start())
   	KMessageBox::error(NULL,  tr("Cannot launch the KDE Control Center!"));
}

void  DevdListener::detachDevice(const QString&  devname) {
   const QString&  devdesc = devDescs[devname];
   passivePopup(devdesc.isNull() ? devname : devdesc, tr("Device detached"));
   devDescs.remove(devname);
}

void DevdListener::acpiLineStateChanged(const QString & state) {
	QString source = tr("battery");
	if (state == "1" || state == "0x01")
		source = tr("AC Line");

	passivePopup(tr("Power source changed to <i>%1</i>.").arg(source));
}


void DevdListener::acpiTemperatureCritical() {
	passivePopup(tr("<font color=red><b>WARNING</b>: System temperature too high, shutting down soon!</font>"), "", 0);
}


void DevdListener::passivePopup(const QString & message, const QString & title, int timeout) {
	static QPixmap icon = KGlobal::iconLoader()->loadIcon("dbsd-hwnotify", KIcon::Toolbar, KIcon::SizeSmallMedium);

   KPassivePopup::message(title.isEmpty() ? tr("Hardware Notification") : title,
                          message,
                          icon,
                          (QWidget *) NULL,
								  NULL,
								  timeout);
}
