diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-10 19:54:13 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-02-10 19:54:13 +0000 |
commit | da7847adb43726079c7a4be1f06acbebe0bdde57 (patch) | |
tree | 0138139a2d4a3a213319cc5e23e409b90a9fcc6b /src | |
download | kima-da7847adb43726079c7a4be1f06acbebe0bdde57.tar.gz kima-da7847adb43726079c7a4be1f06acbebe0bdde57.zip |
Added old abandoned KDE3 version of Kima
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/kima@1088422 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'src')
62 files changed, 5908 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..fdddc7c --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,25 @@ +INCLUDES = -I$(top_srcdir)/src/cpufreqd -I$(top_srcdir)/src/sources \ + $(all_includes) +METASOURCES = AUTO + +kde_module_LTLIBRARIES = libkima.la + +libkima_la_SOURCES = kima.cpp flowlayout.cpp sourcelistitem.cpp prefs.ui +libkima_la_LDFLAGS = -module $(KDE_PLUGIN) $(all_libraries) +libkima_la_LIBADD = $(top_builddir)/src/cpufreqd/libcpufreqd.la \ + $(top_builddir)/src/sources/libsources.la $(LIB_KDEUI) + +kima_DATA = kima.desktop +kimadir = $(kde_datadir)/kicker/applets + +KDE_ICON = hwinfo +EXTRA_DIST = $(kima_DATA) + +messages: rc.cpp + $(XGETTEXT) *.cpp */*.cpp -o $(podir)/kima.pot +noinst_HEADERS = kima.h flowlayout.h sourcelistitem.h + +SUBDIRS = cpufreqd sources + +AM_CXXFLAGS = @libhal_CFLAGS@ +LIBS += @libhal_LIBS@ diff --git a/src/cpufreqd/Makefile.am b/src/cpufreqd/Makefile.am new file mode 100644 index 0000000..3b81871 --- /dev/null +++ b/src/cpufreqd/Makefile.am @@ -0,0 +1,8 @@ +INCLUDES = -I$(top_srcdir)/src/cpufreqd -I$(top_srcdir)/src/sources $(all_includes) +METASOURCES = AUTO +libcpufreqd_la_LDFLAGS = $(all_libraries) +noinst_LTLIBRARIES = libcpufreqd.la +libcpufreqd_la_SOURCES = cpufreqd.cpp cpufreqdconnection.cpp \ + cpufreqdprofile.cpp +noinst_HEADERS = cpufreqd.h cpufreqdconnection.h cpufreqdprofile.h cpufreqd.h \ + cpufreqdconnection.h cpufreqdprofile.h diff --git a/src/cpufreqd/cpufreqd.cpp b/src/cpufreqd/cpufreqd.cpp new file mode 100644 index 0000000..833db29 --- /dev/null +++ b/src/cpufreqd/cpufreqd.cpp @@ -0,0 +1,156 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "cpufreqd.h" +#include <qdir.h> +#include <qkeysequence.h> +#include <qstringlist.h> +#include <klocale.h> + +#define CHUNK_SIZE 4096 + +/** + * cpufreqd control module main class + *@author: Valentine Sinitsyn ([email protected]) + *@todo: We assume that cpufreqd is in dynamic mode when applet is started because as for now it has + * no means to detect mode. This could lead to inconsistency if, ex. applet is restarted after making + * some changes to cpufreqd state. + */ + +CPUFreqd::CPUFreqd() { + m_menu = new QPopupMenu(); + QObject::connect(m_menu, SIGNAL(aboutToShow()), this, SLOT(updateMenu())); + + m_dynamic = new QAction(i18n("Select dynamically"), QKeySequence(), this); + QObject::connect(m_dynamic, SIGNAL(activated()), this, SLOT(setDynamic())); + m_dynamic->setToggleAction(true); + m_dynamic->setOn(true); + + m_items = new QActionGroup(this); + m_items->setExclusive(true); + + m_actions = new QPtrList<QAction>(); + m_actions->setAutoDelete(true); + + m_mapper = new QSignalMapper(this); + QObject::connect(m_mapper, SIGNAL(mapped(int)), this, SLOT(setProfile(int))); +} + +CPUFreqd::~CPUFreqd() { +} + +bool CPUFreqd::enabled() const { + return m_conn.available(); +} + +QValueVector<CPUFreqdProfile> & CPUFreqd::getProfiles(bool reconnect) { + char chunk[CHUNK_SIZE]; + QString buffer; + + if (!m_profiles.empty()) + m_profiles.clear(); + + if (!m_conn.open()) + if (reconnect) { + m_dynamic->setOn(m_conn.lookup()); + return getProfiles(false); + } else { + return m_profiles; + } + + if (!m_conn.write(CMD_LIST_PROFILES, 0)) + return m_profiles; + + int bytes = 0; + while ( (bytes = m_conn.read(chunk, CHUNK_SIZE - 1)) ) { + chunk[bytes] = '\0'; + buffer.append(chunk); + } + + QStringList profiles = QStringList::split("\n", buffer); + for (QStringList::Iterator it = profiles.begin(); it != profiles.end(); it++) + m_profiles.push_back( CPUFreqdProfile(*it) ); + + m_conn.close(); + + return m_profiles; +} + +QPopupMenu* CPUFreqd::menu() { + return m_menu; +} + +void CPUFreqd::updateMenu() { + QAction *cur; + + m_menu->clear(); + m_actions->clear(); + + getProfiles(true); + if (!m_profiles.isEmpty()) { + m_dynamic->addTo(m_menu); + m_menu->insertSeparator(); + + for (unsigned int i = 0; i < m_profiles.count(); i++) + if (m_profiles[i].isValid()) { + cur = new QAction(m_profiles[i].name(), QKeySequence(), m_items); + QObject::connect(cur, SIGNAL(activated()), m_mapper, SLOT(map())); + cur->setToggleAction(true); + cur->setOn(m_profiles[i].active()); + + m_mapper->setMapping(cur, i+1); + m_actions->append(cur); + } + m_items->addTo(m_menu); + } else { + int errmsg = m_menu->insertItem(i18n("Can't talk to cpufreqd"), 0, 0); + m_menu->setItemEnabled(errmsg, false); + } +} + +void CPUFreqd::setManual() { + setMode(ARG_MANUAL); + m_dynamic->setOn(false); +} + +void CPUFreqd::setDynamic() { + setMode(ARG_DYNAMIC); + m_dynamic->setOn(true); +} + +void CPUFreqd::setProfile(int id) { + //@fixme: make it return bool and bail out if it falis + if (m_dynamic->isOn()) + setManual(); + + if (!m_conn.open()) + return; + + m_conn.write(CMD_SET_PROFILE, (uint32_t)id); + m_conn.close(); +} + +void CPUFreqd::setMode(uint32_t mode) { + if (!m_conn.open()) + return; + + m_conn.write(CMD_SET_MODE, mode); + m_conn.close(); +} diff --git a/src/cpufreqd/cpufreqd.h b/src/cpufreqd/cpufreqd.h new file mode 100644 index 0000000..42e4b0e --- /dev/null +++ b/src/cpufreqd/cpufreqd.h @@ -0,0 +1,65 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef CPUFREQD_H +#define CPUFREQD_H + +#include "cpufreqdconnection.h" +#include "cpufreqdprofile.h" +#include <qaction.h> +#include <qobject.h> +#include <qpopupmenu.h> +#include <qstring.h> +#include <qsignalmapper.h> +#include <qvaluevector.h> +#include <qptrlist.h> +#include <stdint.h> + +class CPUFreqd: public QObject { + Q_OBJECT + + public: + CPUFreqd(); + virtual ~CPUFreqd(); + + bool enabled() const; + QValueVector<CPUFreqdProfile> & getProfiles(bool reconnect = false); + QPopupMenu* menu(); + + public slots: + void setManual(); + void setDynamic(); + void setProfile(int id); + + private: + CPUFreqdConnection m_conn; + QValueVector<CPUFreqdProfile> m_profiles; + QPopupMenu *m_menu; + QSignalMapper *m_mapper; + QAction* m_dynamic; + QActionGroup* m_items; + QPtrList<QAction>* m_actions; + + void setMode(uint32_t mode); + + private slots: + void updateMenu(); +}; + +#endif diff --git a/src/cpufreqd/cpufreqdconnection.cpp b/src/cpufreqd/cpufreqdconnection.cpp new file mode 100644 index 0000000..4c9ee03 --- /dev/null +++ b/src/cpufreqd/cpufreqdconnection.cpp @@ -0,0 +1,125 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "cpufreqdconnection.h" + +#include <qdir.h> + +#include <fcntl.h> +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> + +/* Excerpt from cpufreqd_remote.h (cpufreqd-2.0.0) + * + * Copyright (C) 2005 Mattia Dongili <[email protected]> + * Hrvoje Zeba <[email protected]> + * + * Format: + * it is an uint32_t used as bitmask + * + * 31-16 15-0 + * <command> <arguments> + * + * The response may be longer than a single line and is + * terminated by the RESPONSE_END (see defines). + */ + +#define CMD_SHIFT 16 +#define ARG_MASK 0x0000ffff + +#define REMOTE_CMD(c) (c >> CMD_SHIFT) +#define REMOTE_ARG(c) (c & ARG_MASK) +#define MAKE_COMMAND(cmd, arg) ((cmd << CMD_SHIFT) | arg) +#define INVALID_CMD 0xffffffff + +/** + * This class encapsulates cpufreqd connection performed via Unix sockets + *@author: Valentine Sinitsyn ([email protected]) + */ + +CPUFreqdConnection::CPUFreqdConnection(): m_fd(-1) { + lookup(); +} + + +CPUFreqdConnection::~CPUFreqdConnection() { +} + +bool CPUFreqdConnection::open() { + struct sockaddr_un sck; + + // socket name is too long - it can't be copied to to sun_path + if (m_socket.isEmpty() || m_socket.length() >= 108) + return false; + + if (m_fd > 0) + close(); + + if ((m_fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) + return false; + + sck.sun_family = AF_UNIX; + //It is guaranted that socket.ascii() is shorter than 108 bytes, but we want to be sure + strncpy(sck.sun_path, m_socket.ascii(), 108); + + if (::connect(m_fd, (struct sockaddr *)&sck, sizeof(sck)) == -1) + return false; + + return true; +} + +ssize_t CPUFreqdConnection::read(void *buf, size_t size) { + return ::read(m_fd, buf, size); +} + +bool CPUFreqdConnection::write(uint32_t cmd, uint32_t arg) { + uint32_t command; + + command = MAKE_COMMAND(cmd, arg); + return (::write(m_fd, &command, sizeof(command)) == (int)sizeof(command)); +} + +void CPUFreqdConnection::close() { + ::close(m_fd); + m_fd = -1; +} + +bool CPUFreqdConnection::available() const { + return !m_socket.isEmpty(); +} + +bool CPUFreqdConnection::lookup() { + QString candidate; + QDir tmp("/tmp", "cpufreqd-*", QDir::Time, QDir::Dirs); + + if (tmp.count()) + candidate = "/tmp/" + tmp[0] + "/cpufreqd"; + + if (candidate != m_socket) { + m_socket = candidate; + return true; + } + + return false; +} diff --git a/src/cpufreqd/cpufreqdconnection.h b/src/cpufreqd/cpufreqdconnection.h new file mode 100644 index 0000000..4f15ad8 --- /dev/null +++ b/src/cpufreqd/cpufreqdconnection.h @@ -0,0 +1,69 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef CPUFREQDCONNECTION_H +#define CPUFREQDCONNECTION_H + +#include <qstring.h> +#include <stdint.h> + +/* Excerpt from cpufreqd_remote.h (cpufreqd-2.0.0) + * + * Copyright (C) 2005 Mattia Dongili <[email protected]> + * Hrvoje Zeba <[email protected]> + * + * Format: + * it is an uint32_t used as bitmask + * + * 31-16 15-0 + * <command> <arguments> + * + * The response may be longer than a single line and is + * terminated by the RESPONSE_END (see defines). + */ + +#define CMD_UPDATE_STATE 1 /* no arguments */ +#define CMD_SET_PROFILE 2 /* <profile index> */ +#define CMD_LIST_PROFILES 3 /* no arguments */ +#define CMD_SET_RULE 4 /* <rule index> */ +#define CMD_LIST_RULES 5 /* no arguments */ +#define CMD_SET_MODE 6 /* <mode> */ + +#define ARG_DYNAMIC (1) +#define ARG_MANUAL (2) + +class CPUFreqdConnection{ + public: + CPUFreqdConnection(); + ~CPUFreqdConnection(); + + bool open(); + ssize_t read(void *buf, size_t size); + bool write(uint32_t cmd, uint32_t arg); + void close(); + + bool available() const; + bool lookup(); + + private: + QString m_socket; + int m_fd; +}; + +#endif diff --git a/src/cpufreqd/cpufreqdprofile.cpp b/src/cpufreqd/cpufreqdprofile.cpp new file mode 100644 index 0000000..96d7fe6 --- /dev/null +++ b/src/cpufreqd/cpufreqdprofile.cpp @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "cpufreqdprofile.h" +#include <qstringlist.h> + +/** + * This class encapsulates cpufreqd profile data: name, frequencies and governors + *@author: Valentine Sinitsyn ([email protected]) + */ + +CPUFreqdProfile::CPUFreqdProfile(QString & descr) { + QStringList fields = QStringList::split("/", descr); + + //@fixme: if name contains a slash, the results will be incorrect + if (fields.count() == 5) { + m_active = fields[0].toInt(); + m_name = fields[1]; + m_minfreq = fields[2].toInt(); + m_maxfreq = fields[3].toInt(); + m_policy = fields[4]; + } +} + +CPUFreqdProfile::~ CPUFreqdProfile() { +} + +bool CPUFreqdProfile::isValid() const { + return (!m_name.isEmpty() && !m_policy.isEmpty() && m_minfreq <= m_maxfreq); +} + +bool CPUFreqdProfile::active() const { + return (m_active == 1); +} + +const QString & CPUFreqdProfile::name() const { + return m_name; +} + +int CPUFreqdProfile::minfreq() const { + return m_minfreq; +} + +int CPUFreqdProfile::maxfreq() const { + return m_maxfreq; +} + +const QString & CPUFreqdProfile::policy() const { + return m_policy; +} diff --git a/src/cpufreqd/cpufreqdprofile.h b/src/cpufreqd/cpufreqdprofile.h new file mode 100644 index 0000000..f57e36d --- /dev/null +++ b/src/cpufreqd/cpufreqdprofile.h @@ -0,0 +1,48 @@ +/*************************************************************************** + * Copyright (C) 2006 by Valentine Sinitsyn * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef CPUFREQDPROFILE_H +#define CPUFREQDPROFILE_H + +#include <qstring.h> + +class CPUFreqdProfile { + public: + CPUFreqdProfile() {}; + CPUFreqdProfile(QString & descr); + virtual ~CPUFreqdProfile(); + + bool isValid() const; + + bool active() const; + const QString & name() const; + int minfreq() const; + int maxfreq() const; + const QString & policy() const; + + private: + int m_active; + QString m_name; + int m_id; + int m_minfreq; + int m_maxfreq; + QString m_policy; +}; + +#endif diff --git a/src/flowlayout.cpp b/src/flowlayout.cpp new file mode 100644 index 0000000..a0ead97 --- /dev/null +++ b/src/flowlayout.cpp @@ -0,0 +1,311 @@ +/*************************************************************************** + * Copyright (C) 2005 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include "flowlayout.h" +#include "sources/source.h" + +//#include "kdebug.h" +//#include <qlabel.h> // debugging + +class FlowLayoutIterator :public QGLayoutIterator{ +public: + FlowLayoutIterator( QPtrList<QLayoutItem>* layoutItems ): + index(0), + mLayoutItems(layoutItems){ + } + uint count() const; + QLayoutItem* current(); + QLayoutItem* next(); + QLayoutItem* takeCurrent(); +private: + int index; + QPtrList<QLayoutItem>* mLayoutItems; +}; +QLayoutItem* FlowLayoutIterator::current(){ + return index < int(mLayoutItems->count()) ? mLayoutItems->at(index) : 0; + /*if(index < int(mLayoutItems->count())){ + QLayoutItem* item = mLayoutItems->at(index); + kdDebug() << "FlowLayoutIterator::current(index " << index << ") returns: " << item << endl; + return item; + }else{ + kdDebug() << "FlowLayoutIterator::current(index " << index << ") returns: NULL" << endl; + return 0; + }*/ +} +QLayoutItem* FlowLayoutIterator::next(){ + index++; + //kdDebug() << "FlowLayoutIterator::next, index: " << index << endl; + return current(); +} +QLayoutItem* FlowLayoutIterator::takeCurrent(){ + return index < int(mLayoutItems->count()) ? mLayoutItems->take(index) : 0; + /*if(index < int(mLayoutItems->count())){ + QLayoutItem* item = mLayoutItems->take(index); + kdDebug() << "FlowLayoutIterator::takeCurrent(index " << index << ") returns: " << item << endl; + return item; + }else{ + kdDebug() << "FlowLayoutIterator::takeCurrent(index " << index << ") returns: NULL" << endl; + return 0; + }*/ + +} + + +FlowLayout::FlowLayout( QWidget* parent, Qt::Orientation orientation, int border, int space, const char* name ) + : QLayout( parent, border, space, name ), + mOrientation(orientation), mLastItem(NULL){ +} +FlowLayout::FlowLayout( QLayout* parent, Qt::Orientation orientation, int space, const char* name ) + : QLayout( parent, space, name ), + mOrientation(orientation), mLastItem(NULL){ +} +FlowLayout::FlowLayout( Qt::Orientation orientation, int space, const char* name ) + : QLayout( space, name ), + mOrientation(orientation), mLastItem(NULL){ +} + + +FlowLayout::~FlowLayout(){ + deleteAllItems(); +} + +int FlowLayout::heightForWidth( int w ) const{ + FlowLayout* mthis = (FlowLayout*)this; + int h = mthis->doLayout( QRect(0,0,w,0), TRUE ); + return h; +} + +int FlowLayout::widthForHeight( int h ) const{ + FlowLayout* mthis = (FlowLayout*)this; + int w = mthis->doLayout( QRect(0,0,0,h), TRUE ); + return w; +} + +void FlowLayout::addItem(QLayoutItem* item){ + //kdDebug() << "FlowLayout::addItem: " << (static_cast<QLabel*>(item->widget()))->text() << ", width: " << item->widget()->width() << ", height: " << item->widget()->height()<< endl; + // we are indirectly called from addSource. this + // is a hint for addSource, to let it know which item + // was added. + mLastItem = item; +} + +void FlowLayout::addSource(Source* src){ + add(src->getWidget()); + mSources[mLastItem] = src; + src->getWidget()->show(); + + // step back until we find an item which has a + // smaller position stored in its config. then, we found + // the right position for the new item. + QLayoutItem * qli = mLayoutItems.last(); + while(qli && mSources[qli]->getPosition() > src->getPosition()) + qli = mLayoutItems.prev(); + mLayoutItems.insert(mLayoutItems.at()+1, mLastItem); +} + +void FlowLayout::remove(QWidget* widget){ + //kdDebug() << "FlowLayout::remove: " << (static_cast<QLabel*>(widget))->text() << endl; + widget->hide(); + QPtrListIterator<QLayoutItem> it(mLayoutItems); + while(it.current() != NULL) { + if(it.current()->widget() == widget) { + mSources.erase(it.current()); + mLayoutItems.remove(it.current()); + // removes and deletes only the QLayoutItem + // (QWidgetItem) + QLayout::remove(widget); + break; + } + ++it; + } +} + +uint FlowLayout::count(){ + return mLayoutItems.count(); +} + +bool FlowLayout::moveItem(const QLayoutItem* which, const QLayoutItem* relate, DIRECTION direction){ + int newPos = mLayoutItems.findRef(relate); + int oldPos = mLayoutItems.findRef(which); + + // check whether the widget is already at a correct position + if(oldPos+1 == newPos && direction == ABOVE || oldPos-1 == newPos && direction == BELOW) + return false; + + // remove the item + mLayoutItems.remove(which); + if(oldPos < newPos) + newPos--; + + newPos += direction; + // actually reinsert the item + mLayoutItems.insert(newPos, which); + activate(); // relayout + // kdDebug() << "oldPos: " << oldPos << ", newPos: " << newPos << endl; + return true; +} + +void FlowLayout::updatePositions(KConfig * inKConfig){ + //kdDebug() << "updating all positions..." << endl; + int pos = 0; + QPtrListIterator<QLayoutItem> it(mLayoutItems); + while(it.current() != NULL) { + mSources[it.current()]->setPosition(pos, inKConfig); + ++it; + ++pos; + } + //kdDebug() << "positions updated!" << endl; +} + +bool FlowLayout::hasHeightForWidth() const{ + return mOrientation != Qt::Horizontal; +} + +bool FlowLayout::hasWidthForHeight() const{ + return mOrientation == Qt::Horizontal; +} + +QSize FlowLayout::sizeHint() const{ + //return minimumSize(); + QSize size(0,0); + QPtrListIterator<QLayoutItem> it(mLayoutItems); + QLayoutItem *o; + while((o=it.current()) != 0){ + ++it; + size = size.expandedTo( o->sizeHint() ); + } + return size; +} + +QSize FlowLayout::minimumSize() const{ + QSize size(0,0); + QPtrListIterator<QLayoutItem> it(mLayoutItems); + QLayoutItem *o; + while((o=it.current()) != 0){ + ++it; + size = size.expandedTo(o->minimumSize()); + } + return size; +} + +QSizePolicy::ExpandData FlowLayout::expanding() const{ + return QSizePolicy::NoDirection; +} + +QLayoutIterator FlowLayout::iterator(){ + return QLayoutIterator(new FlowLayoutIterator(&mLayoutItems)); +} + +Qt::Orientation FlowLayout::getOrientation() const{ + return mOrientation; +} + +void FlowLayout::setOrientation(Qt::Orientation orientation){ + mOrientation = orientation; +} + +void FlowLayout::setGeometry( const QRect& rect ){ + QLayout::setGeometry( rect ); + doLayout( rect ); +} + +int FlowLayout::doLayout( const QRect& rect, bool testonly ){ + if(mOrientation == Qt::Horizontal) + return doLayoutHorizontal(rect, testonly); + else + return doLayoutVertical(rect, testonly); +} + +int FlowLayout::doLayoutHorizontal( const QRect& rect, bool testOnly ){ + //kdDebug() << "spacing: " << spacing() << endl; + + int x = rect.x(); + int y = rect.y(); + int width = 0; // width of this column so far + int height = 0; // height of this column so far + QPtrListIterator<QLayoutItem> it(mLayoutItems); + QLayoutItem* layoutItem; + QPtrList<QLayoutItem> column; // stores the items of one column + while((layoutItem = it.current()) != 0){ + ++it; + //int nextY = y + layoutItem->sizeHint().height() + spacing(); // next y + int nextY = y + layoutItem->sizeHint().height(); // next y + //if( nextY - spacing() > rect.bottom() && width > 0 ) { + if( nextY > rect.bottom() && width > 0 ) { + // next column + y = rect.y(); // reset y + x = x + width + spacing(); // new x + //nextY = y + layoutItem->sizeHint().height() + spacing(); // next y with changed y + nextY = y + layoutItem->sizeHint().height(); // next y with changed y + width = 0; // reset width for the next column + } + if(!testOnly){ + layoutItem->setGeometry( QRect( QPoint( x, y ), layoutItem->sizeHint() ) ); + column.append(layoutItem); + height += layoutItem->sizeHint().height(); // add the height of the current item to the column height + if( it.current() == 0 || nextY + it.current()->sizeHint().height() > rect.bottom() ){ // test it it's the last item (of this column) + // calculate real needed width + int rWidth = 0; + for(QLayoutItem* item = column.first(); item; item = column.next()){ + rWidth = QMAX( rWidth, item->widget()->sizeHint().width() ); + } + // relayout the items of the former column + int space = (rect.height() - height) / (column.count() + 1); + int i = 0; // counts the items of this column + for(QLayoutItem* item = column.first(); item; item = column.next()){ + QRect r = item->geometry(); + item->setGeometry( QRect(r.left(), r.top() + ((++i) * space), rWidth, r.height()) ); + } + column.clear(); // remove the items of the former column + height = 0; // reset height for the next column + } + } + y = nextY; + width = QMAX( width, layoutItem->sizeHint().width() ); + } + return x + width - rect.x(); // width +} + +int FlowLayout::doLayoutVertical( const QRect& rect, bool testOnly ){ + int x = rect.x(); + int y = rect.y(); + int height = 0; // height of this line so far + QPtrListIterator<QLayoutItem> it(mLayoutItems); + QLayoutItem* layoutItem; + while((layoutItem = it.current() ) != 0){ + ++it; + //int nextX = x + layoutItem->sizeHint().width() + spacing(); + int nextX = x + layoutItem->sizeHint().width(); + if(nextX - spacing() > rect.right() && height > 0) { + // next line + x = rect.x(); // reset x + //y = y + height + spacing(); // new y + y = y + height; // new y + //nextX = x + layoutItem->sizeHint().width() + spacing(); // next x + nextX = x + layoutItem->sizeHint().width(); // next x + height = 0; // reset height for the next line + } + const int itemHeight = layoutItem->sizeHint().height(); + if(!testOnly) + layoutItem->setGeometry(QRect(x, y, rect.right(), itemHeight)); + x = nextX; + height = QMAX(height, itemHeight); + } + return y + height - rect.y(); // height +} diff --git a/src/flowlayout.h b/src/flowlayout.h new file mode 100644 index 0000000..1695872 --- /dev/null +++ b/src/flowlayout.h @@ -0,0 +1,85 @@ +/*************************************************************************** + * Copyright (C) 2005 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef FLOWLAYOUT_H +#define FLOWLAYOUT_H + +#include <qlayout.h> +#include <qptrlist.h> + +class Source; +class KConfig; + +class FlowLayout : public QLayout{ + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * the direction for moveItem + */ + enum DIRECTION { + ABOVE = 0, + BELOW = 1 + }; + FlowLayout( QWidget* parent, Qt::Orientation orientation=Qt::Horizontal, int border=0, int space=-1, const char* name=0 ); + FlowLayout( QLayout* parent, Qt::Orientation orientation=Qt::Horizontal, int space=-1, const char* name=0 ); + FlowLayout( Qt::Orientation=Qt::Horizontal, int space=-1, const char* name=0 ); + virtual ~FlowLayout(); + /** + * tells all sources their positions + */ + void updatePositions(KConfig * inKConfig); + void addItem(QLayoutItem* item); + void addSource(Source* src); + void remove(QWidget* widget); + /** + * Returns the number of items in the layout + */ + uint count(); + /** + * moves the item \c which above/beneath item \c relate + * \param dir ABOVE if above, BENEATH if beneath + * \return the new Position or -1 if no real move was made + */ + bool moveItem(const QLayoutItem* which, const QLayoutItem* relate, DIRECTION direction); + bool hasHeightForWidth() const; + int heightForWidth(int w) const; + bool hasWidthForHeight() const; + int widthForHeight(int h) const; + QSize sizeHint() const; + QSize minimumSize() const; + QLayoutIterator iterator(); + QSizePolicy::ExpandData expanding() const; + Qt::Orientation getOrientation() const; +public slots: + void setOrientation(Qt::Orientation orientation); + +protected: + void setGeometry(const QRect&); + +private: + int doLayout( const QRect&, bool testOnly = FALSE ); + int doLayoutHorizontal( const QRect&, bool testOnly ); + int doLayoutVertical( const QRect&, bool testOnly ); + Qt::Orientation mOrientation; + QPtrList<QLayoutItem> mLayoutItems; + // this is the connection between a layout item and its source. + QMap<QLayoutItem*, Source*> mSources; + QLayoutItem* mLastItem; // the item that was last added +}; +#endif diff --git a/src/kima.cpp b/src/kima.cpp new file mode 100644 index 0000000..56d4bfe --- /dev/null +++ b/src/kima.cpp @@ -0,0 +1,432 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include <list> + +// Qt +#include <qlabel.h> +#include <qfile.h> +#include <qspinbox.h> +#include <qcombobox.h> +#include <qtextedit.h> +#include <qtimer.h> +#include <qcursor.h> +#include <klistview.h> +#include <qlistview.h> +#include <qwidgetstack.h> + +// KDE +#include <kmessagebox.h> +#include <kaboutapplication.h> +#include <kpopupmenu.h> +#include <kstdaction.h> +#include <kaction.h> +#include <kactioncollection.h> +#include <kactionclasses.h> +#include <kapplication.h> +#include <kdialogbase.h> +#include <kconfig.h> +//#include <kglobal.h> +#include <kiconloader.h> +#include <klocale.h> + +// Kima +#include "kima.h" +#include "flowlayout.h" +#include "sourcelistitem.h" +#include "prefs.h" + +// Kima Soures +#include "sources/acpithermalsrc.h" +#include "sources/ibmacpithermalsrc.h" +#include "sources/ibmacpifansrc.h" +#include "sources/ibmhdaps.h" +#include "sources/omnibookthermalsrc.h" +#include "sources/ibookg4thermalsrc.h" +#include "sources/hwmonthermalsrc.h" +#include "sources/hwmonfansrc.h" +#include "sources/sysfreqsrc.h" +#include "sources/cpuinfofreqsrc.h" +#include "sources/i8ksrc.h" +#include "sources/nvidiathermalsrc.h" +#include "sources/hddtempsrc.h" +#include "sources/uptimesrc.h" +#include "sources/batterysrc.h" + +//#include "kdebug.h" + +extern "C" { + KPanelApplet* init( QWidget* inParent, const QString& inConfigFile) { + KGlobal::locale()->insertCatalogue("kima"); + return new Kima(inConfigFile, KPanelApplet::Normal, + //KPanelApplet::About | KPanelApplet::Help | KPanelApplet::Preferences, + KPanelApplet::About | KPanelApplet::Preferences, + inParent, "kima"); + } +} + +Kima::Kima(const QString& inConfigFile, Type inType, int inActions, QWidget* inParent, const char* inName): + KPanelApplet(inConfigFile, inType, inActions, inParent, inName), + QToolTip(this), + mCachedWFH(0), + mCachedHeight(0), + mDraggedSourceItem(NULL){ + mAboutDialog = 0; // aboutData is created in the about() method + mPrefsDlg = 0; // the prefs dialog is created in the preferences() method + mPrefs = 0; // the prefs ui is created in the preferences() method + + // Get the current application configuration handle + mKConfig = config(); + mKConfig->setGroup("Kima"); + + // automatically delete pointers + mSources.setAutoDelete(TRUE); + + // create layout + mLayout = new FlowLayout(this, orientation() ); + mLayout->setSpacing(8); + + + // add the sources into the sources list + addSources(NVidiaThermalSrc::createInstances(this)); + addSources(ACPIThermalSrc::createInstances(this)); + addSources(IBMACPIThermalSrc::createInstances(this)); + addSources(IBMACPIFanSrc::createInstances(this)); + addSources(IBMHDAPSSrc::createInstances(this)); + addSources(OmnibookThermalSrc::createInstances(this)); + addSources(IbookG4ThermalSrc::createInstances(this)); + addSources(HwMonThermalSrc::createInstances(this)); + addSources(SysFreqSrc::createInstances(this)); + addSources(CpuinfoFreqSrc::createInstances(this)); + addSources(I8kSrc::createInstances(this)); + addSources(HDDTempSrc::createInstances(this)); + addSources(HwMonFanSrc::createInstances(this)); + addSources(UptimeSrc::createInstances(this)); + addSources(BatterySrc::createInstances(this)); + + + QPtrListIterator<Source> itSync(mSources); + Source* source; + while((source = itSync.current()) != 0){ + ++itSync; + registerSource(source); + } + mLayout->updatePositions(mKConfig); + + // create the menu + mMenu = new KPopupMenu(this); + mMenu->insertTitle(SmallIcon("hwinfo"), i18n("Kima")); + + if (mCpufreqd.enabled()) { + mMenu->insertItem(i18n("&Performance Profiles"), mCpufreqd.menu()); + } + + KActionCollection* actionCollection = new KActionCollection(this, "actionCollection", kapp); + + KAction* action = KStdAction::preferences(this, SLOT(preferences()), actionCollection); + action->setText(i18n("&Preferences")); + action->plug(mMenu); + + action = KStdAction::aboutApp(this, SLOT(about()), actionCollection); + action->setText(i18n("&About Kima")); + action->plug(mMenu); + + //menu->insertSeparator(); + //KStdAction::quit( kapp, SLOT( quit() ), actionCollection )->plug( menu ); +} + +Kima::~Kima() { + // super class deletes the widgets +} + +void Kima::addSources(const std::list<Source*>& inList){ + for(std::list<Source*>::const_iterator it = inList.begin(); it != inList.end(); ++it){ + mSources.append(*it); + } +} + +void Kima::displaySource(bool inDisplay, Source* inSource){ + if(inDisplay){ + //kdDebug() << "Kima::displaySource mLayout->add: " << inSource->getName() << endl; + mLayout->addSource(inSource); // add the source + //kdDebug() << "Pos of " << inSource->getID() << ": " << inSource->getPosition() << endl; + }else{ + //kdDebug() << "Kima::displaySource mLayout->remove: " << inSource->getName() << endl; + mLayout->remove(inSource->getWidget()); + } +} + +void Kima::about() { + if(!mAboutDialog){ + KAboutData* aboutData = new KAboutData("libkima", + "Kima", + "0.7.3.2", + I18N_NOOP("Kicker monitoring applet.\n\n" + "This applet monitors various temperature, frequency and fan sources.\n" + "Make sure you have enabled a supported kernel module.\n"), + KAboutData::License_GPL, + "(c) 2004-2007, Ken Werner"); + aboutData->addAuthor("Ken Werner", 0, "[email protected]"); + aboutData->addAuthor("Johannes Schaub", 0, "[email protected]"); + aboutData->addAuthor("Valentine Sinitsyn", I18N_NOOP("cpufreqd control module"), "[email protected]"); + aboutData->addCredit("Franziska Simmank"); + aboutData->addCredit("Manfred Paul"); + aboutData->addCredit("Rainer Dorsch"); + aboutData->addCredit("Flavio Castelli"); + aboutData->addCredit("Jocke Andersson"); + aboutData->addCredit("Raoul Martin"); + aboutData->addCredit("Donnie Bhayangkara"); + aboutData->addCredit("Gonzalo Nemmi"); + aboutData->addCredit("Mariusz Ciesla"); + aboutData->addCredit("Mark Rosenstand"); + aboutData->addCredit("Santiago Bruno"); + aboutData->addCredit("Dark Apostrophe"); + aboutData->addCredit("Sascha Hoogen"); + aboutData->addCredit("Peter Avramucz"); + aboutData->addCredit("Oskar Ellström"); + aboutData->addCredit("Uğur Çetin"); + mAboutDialog = new KAboutApplication(aboutData, this, "aboutApp", false); + } + mAboutDialog->show(); +} + +void Kima::help() { + KMessageBox::information(0, i18n("This is a help box")); +} + +void Kima::preferences() { + //KMessageBox::information(0, i18n("This is a preferences box")); + if(!mPrefsDlg){ + //create the prefs dialog + mPrefsDlg = new KDialogBase( this, "prefsDlg", false, i18n("Configuration"), KDialogBase::Ok|KDialogBase::Apply|KDialogBase::Cancel, KDialogBase::Ok, false ); + //create the prefs ui + mPrefs = new Prefs( mPrefsDlg, "prefsui" ); //the uic generated class + + //setup the dialog + //mPrefsDlg->setFixedSize(mPrefs->size()); + mPrefsDlg->setMainWidget(mPrefs); //embed the uic generated class into the kde dialog + connect(mPrefsDlg, SIGNAL(applyClicked()), this, SLOT(savePreferences())); + connect(mPrefsDlg, SIGNAL(okClicked()), this, SLOT(savePreferences())); + connect(mPrefsDlg, SIGNAL(cancelClicked()), this, SLOT(cancelPreferences())); + + // create new list items and the widgetStack widgets + QPtrListIterator<Source> it(mSources); + Source* source; + while((source = it.current()) != 0){ + ++it; + // create the specific source prefs and add them to the widgetStack + mPrefs->widgetStack->addWidget(source->createPrefs(mPrefs->widgetStack)); + + // create the SourceListItem to switch and to enable/disable the widgetstack item (the source) + // and connect the apply/ok signals to the item, so it can change it's text to a potentially + // new source name. + SourceListItem * item = new SourceListItem(source, mPrefs->sourceListView, QCheckListItem::CheckBox); + connect(mPrefsDlg, SIGNAL(applyClicked()), item, SLOT(updateText())); + connect(mPrefsDlg, SIGNAL(okClicked()), item, SLOT(updateText())); + } + + if(mPrefs->sourceListView->firstChild()){ + mPrefs->sourceListView->setSelected(mPrefs->sourceListView->firstChild(), true); // select the first list item + raiseSourcePrefsWidget(mPrefs->sourceListView->firstChild()); // raise that item + } + + connect(mPrefs->sourceListView, SIGNAL(selectionChanged(QListViewItem*)), this, SLOT(raiseSourcePrefsWidget(QListViewItem*))); + } + + // update prefs gui + QPtrListIterator<Source> srcIt(mSources); + Source* source; + while((source = srcIt.current()) != 0){ + ++srcIt; + source->updatePrefsGUI(); + } + + // update the sourcelist checkbox + QPtrList<QListViewItem> lst; + QListViewItemIterator lvIt(mPrefs->sourceListView); + while(lvIt.current()){ + SourceListItem* sli = static_cast<SourceListItem*>(lvIt.current()); + sli->setOn(sli->getSource()->isEnabled()); + ++lvIt; + } + + //show the dialog + mPrefsDlg->show(); +} + +void Kima::raiseSourcePrefsWidget(QListViewItem* inListViewItem){ + mPrefs->widgetStack->raiseWidget(static_cast<SourceListItem*>(inListViewItem)->getSource()->getPrefs()); +} + +void Kima::cancelPreferences(){ + // update prefs gui + QPtrListIterator<Source> it(mSources); + Source* source; + while((source = it.current()) != 0){ + ++it; + source->updatePrefsGUI(); + } +} + +void Kima::savePreferences(){ + // save prefs + QPtrListIterator<Source> it(mSources); + Source* source; + while((source = it.current()) != 0){ + ++it; + source->applyPrefs(); + source->savePrefs(mKConfig); + } + // save the position of all sources because they might have changed. + mLayout->updatePositions(mKConfig); + // update the source widgets + updateSourceWidgets(); + // write config + mKConfig->sync(); + // reset cached witdh for height to force recalculation of the applets width + mCachedWFH = 0; +} + +int Kima::widthForHeight(int inHeight) const{ + //kdDebug() << "widthForHeight: " << height << endl; + mLayout->setOrientation(Qt::Horizontal); + if(mCachedHeight != inHeight){ + mCachedHeight = inHeight; + mCachedWFH = 0; + } + mCachedWFH = QMAX(mLayout->widthForHeight(inHeight), mCachedWFH); + return mCachedWFH; +} + +int Kima::heightForWidth(int inWidth) const{ + //kdDebug() << "heightForWidth: " << width << endl; + mLayout->setOrientation(Qt::Vertical); + return mLayout->heightForWidth(inWidth); + //return sizeHint().height(); +} + +void Kima::mousePressEvent(QMouseEvent* inEvent ){ + if(inEvent->button() == QMouseEvent::RightButton){ + mMenu->popup( inEvent->globalPos() ); + if(mDraggedSourceItem) + mDraggedSourceItem->widget()->unsetCursor(); // unset drag cursor + } + else if(inEvent->button() == QMouseEvent::LeftButton){ + QLayoutIterator it = mLayout->iterator(); + while(it.current()){ + QWidget * c = it.current()->widget(); + if(c->rect().contains(c->mapFromGlobal(inEvent->globalPos()))){ + mDraggedSourceItem = it.current(); // remenber the clicked source + c->setCursor(Qt::SizeAllCursor); // set drag cursor + mDragFactor = (c->rect().height()/2.0) / (double)c->mapFromGlobal(inEvent->globalPos()).y(); + break; + } + ++it; + } + } +} + +void Kima::mouseReleaseEvent(QMouseEvent* inEvent){ + //if(inEvent->button() == QMouseEvent::LeftButton && mDraggedSourceItem){ + if(mDraggedSourceItem){ + mCachedWFH = 0; // reset cached witdh for height to force recalculation of the applets width + mDraggedSourceItem->widget()->unsetCursor(); // unset drag cursor + mDraggedSourceItem = NULL; + } +} + +void Kima::mouseMoveEvent( QMouseEvent* inEvent ) { + if(!mDraggedSourceItem) + return; + QLayoutIterator it = mLayout->iterator(); + while(it.current()){ + QWidget * c = it.current()->widget(); + if(c->rect().contains(c->mapFromGlobal(inEvent->globalPos()))){ + if(it.current() == mDraggedSourceItem) + break; + // we are over a source-widget. + // move the dragged widget above / below that + // widget, depending where the cursor is. + QRect crect = c->rect(); + double relPos = c->mapFromGlobal(inEvent->globalPos()).y() * mDragFactor; + if(mLayout->moveItem(mDraggedSourceItem, it.current(), crect.height()/2.0 > relPos ? FlowLayout::ABOVE : FlowLayout::BELOW)){ + mLayout->updatePositions(mKConfig); + updateGeometry(); + mKConfig->sync(); // write config + updateSourceWidgets(); + } + break; + } + ++it; + } + // outside kima? + if(!rect().contains(mapToParent(inEvent->pos()))) + mDraggedSourceItem->widget()->setCursor(Qt::ForbiddenCursor); + else + mDraggedSourceItem->widget()->setCursor(Qt::SizeAllCursor); +} + +void Kima::paintEvent(QPaintEvent* inEvent){ + KPanelApplet::paintEvent(inEvent); + updateSourceWidgets(); +} + +void Kima::updateSourceWidgets(){ + // repaint the source widgets + for(Source* source = mSources.first(); source; source = mSources.next()) + if(source->showOnApplet()) + source->getWidget()->update(); +} + +void Kima::registerSource(Source* source) { + // then, tell the source to set up its widget + source->realizeWidget(); + + // enable transparency + source->getWidget()->setBackgroundMode(X11ParentRelative); + + // load prefs from the configuration + // this call also emits enabledChanged + source->loadPrefs(mKConfig); + + // add the source to the layout if necessary + displaySource(source->isEnabled() && source->showOnApplet(), source); + + // connection to add and remove sources from mLayout + connect(source, SIGNAL(displaySource(bool, Source*)), this, SLOT(displaySource(bool, Source*))); +} + +void Kima::maybeTip(const QPoint& inPos){ + if(!rect().contains(inPos)) + return; + QString text = "<b>" + i18n("Sources:") + "</b><br><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"; + + QPtrListIterator<Source> it(mSources); + Source* source; + while((source = it.current()) != 0){ + ++it; + if(source->isEnabled() && source->isToolTipEnabled()) + text.append("<tr><td>" + + source->getName() + "</td><td>" + + source->getValue() + "</td><td>"); + } + text.append("</table>"); + tip(rect(), text); +} diff --git a/src/kima.desktop b/src/kima.desktop new file mode 100644 index 0000000..9334bc5 --- /dev/null +++ b/src/kima.desktop @@ -0,0 +1,44 @@ +[Desktop Entry] +Encoding=UTF-8 +Type=Plugin +X-KDE-Library = libkima +X-KDE-UniqueApplet = true +#Exec=kima %i %m -caption "%c" +#DocPath=kima/index.html +#Icon=kima +Icon=hwinfo + +Name=Kima +Name[de]=Kima + +Comment=Kicker applet that monitors various temperature, frequency and fan sources +#Comment[ca]= +#Comment[cy]= +#Comment[da]= +Comment[de]=Kicker-Applet, dass verschiedene Temperatur-, Frequenz- und Lüftersensoren anzeigt. +#Comment[el]= +Comment[es]=Un applet para el Kicker que monitorea varias fuentes de temperatura y velocidad de los procesadores y del cooler +#Comment[et]= +Comment[fr]=Kicker applet que montre la température actuelle de CPU et la fréquence actuelle de CPU. +#Comment[hi]= +#Comment[hu]= +#Comment[is]= +Comment[it]=CpuInfo è una semplice Applet per Kicker che visualizza la frequenza e la temperatura della CPU. +#Comment[ja]= +#Comment[nb]= +#Comment[nl]= +Comment[pl]=Aplet Kickera pokazujacy obecna temperature i frekwencje procesora. +#Comment[pt]= +#Comment[pt_BR]= +#Comment[ru]= +#Comment[sl]= +#Comment[sr]= +#Comment[sr@Latn]= +Comment[sv]=Kicker-miniprogram som övervakar olika temperatur-, frekvens- och fläktkällor +#Comment[ta]= +#Comment[tg]= +Comment[tr]=Çeşitli fan, ısı ve frekans kaynaklarını izlemeye yarayan panel programcığı +#Comment[zh_CN]= +Comment[id]=Applet Kicker yang memantau berbagai sumber temperatur, frekuensi, dan putaran kipas + +Terminal=0 diff --git a/src/kima.h b/src/kima.h new file mode 100644 index 0000000..4e1ba1f --- /dev/null +++ b/src/kima.h @@ -0,0 +1,220 @@ +/*************************************************************************** + * Copyright (C) 2005 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef KIMA_H +#define KIMA_H + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <kpanelapplet.h> +#include "sources/source.h" +#include "cpufreqd/cpufreqd.h" + +// Qt +#include <qtooltip.h> +#include <qfont.h> +#include <qcolor.h> +#include <qptrlist.h> +#include <qlabel.h> + +// Forward Declarations +class FlowLayout; +class KAboutApplication; +class KPopupMenu; +class KSelectAction; +class QMouseEvent; +class KDialogBase; +class Prefs; +class QListViewItem; +class KConfig; +class QLayoutItem; + +/** +@author Ken Werner +*/ +class Kima : public KPanelApplet, QToolTip { + Q_OBJECT //macro which activates signals and slots (moc) + +public: + /** + * Construct a @ref KPanelApplet just like any other widget. + * + * @param configFile The configFile handed over in the factory function. + * @param Type The applet @ref type(). + * @param actions Standard RMB menu actions supported by the applet (see @ref action() ). + * @param parent The pointer to the parent widget handed over in the factory function. + * @param name A Qt object name for your applet. + **/ + Kima(const QString& inConfigFile, Type inType = Normal, int inActions = 0, + QWidget* inParent = 0, const char* inName = 0); + /** destructor */ + virtual ~Kima(); + + /** + * Retrieve a suggested width for a given height. + * + * Every applet should reimplement this function. + * + * Depending on the panel orientation the height (horizontal panel) or the + * width (vertical panel) of the applets is fixed. + * The exact values of the fixed size component depend on the panel size. + * + * On a horizontal panel the applet height is fixed, the panel will + * call @ref widthForHeight(int height) with @p height + * equal to 'the fixed applet height' + * when laying out the applets. + * + * The applet can now choose the other size component (width) + * based on the given height. + * + * The width you return is granted. + **/ + virtual int widthForHeight(int inHeight) const; + + /** + * @return A suggested height for a given width. + * + * Every applet should reimplement this function. + * + * Depending on the panel orientation the height (horizontal panel) or the + * width (vertical panel) of the applets is fixed. + * The exact values of the fixed size component depend on the panel size. + * + * On a vertical panel the applet width is fixed, the panel will + * call @ref heightForWidth(int width) with @p width + * equal to 'the fixed applet width' + * when laying out the applets. + * + * The applet can now choose the other size component (height) + * based on the given width. + * + * The height you return is granted. + **/ + virtual int heightForWidth(int inWidth) const; + +public slots: + /** + * called if the preferences dialog is cancelled + **/ + void cancelPreferences(); + + /** + * Saves the preferences + **/ + void savePreferences(); + + /** + * Is called when the user selects "About" from the applets RMB menu. + * Reimplement this function to launch a about dialog. + * + * Note that this is called only when your applet supports the About action. + * See @ref Action and @ref KPanelApplet(). + **/ + virtual void about(); + + /** + * Is called when the user selects "Help" from the applets RMB menu. + * Reimplement this function to launch a manual or help page. + * + * Note that this is called only when your applet supports the Help action. + * See @ref Action and @ref KPanelApplet(). + **/ + virtual void help(); + + /** + * Is called when the user selects "Preferences" from the applets RMB menu. + * Reimplement this function to launch a preferences dialog or kcontrol module. + * + * Note that this is called only when your applet supports the preferences action. + * See @ref Action and @ref KPanelApplet(). + **/ + virtual void preferences(); + + /** + * raises the preferences widgets of the given list item + */ + void raiseSourcePrefsWidget(QListViewItem* inListViewItem); + + /** + * adds or removes the given source from the display (layout) of the kicker applet + */ + void displaySource(bool inDisplay, Source* inSource); + +protected: + void paintEvent(QPaintEvent* inEvent); + void mousePressEvent(QMouseEvent* inEvent); + void mouseMoveEvent(QMouseEvent* inEvent); + void mouseReleaseEvent(QMouseEvent* inEvent); + void maybeTip(const QPoint& inPos); + void registerSource(Source* source); + +protected slots: + void updateSourceWidgets(); + +private: + /** + * Adds the entries of given the STL to mSources + */ + void addSources(const std::list<Source*>& inList); + + /** + * Searches for the Source that has inWidget + * Returns the found Soucre, otherwise NULL + */ + Source* findSource(QWidget* inWidget); + /** + * Searches for the Source that has inPosition + * Returns the found Soucre, otherwise NULL + */ + Source* findSource(int inPosition); + + KConfig* mKConfig; + FlowLayout* mLayout; + mutable int mCachedWFH; + mutable int mCachedHeight; + KAboutApplication* mAboutDialog; + KPopupMenu* mMenu; + KDialogBase* mPrefsDlg; + Prefs* mPrefs; + + /** + * stores sources + */ + QPtrList<Source> mSources; + + /** + * points to the source which is currently + * dragged, otherwise NULL + */ + QLayoutItem* mDraggedSourceItem; + + /** + * 'drag' threshold multiplier + */ + double mDragFactor; + + /** + * contains the cpufreq support + */ + CPUFreqd mCpufreqd; +}; + +#endif // KIMA_H diff --git a/src/prefs.cpp b/src/prefs.cpp new file mode 100644 index 0000000..ed1ec2b --- /dev/null +++ b/src/prefs.cpp @@ -0,0 +1,71 @@ +#include <kdialog.h> +#include <klocale.h> +/**************************************************************************** +** Form implementation generated from reading ui file './prefs.ui' +** +** Created: Thu Jun 21 19:16:50 2007 +** by: The User Interface Compiler ($Id: qt/main.cpp 3.3.7 edited Aug 31 2005 $) +** +** WARNING! All changes made in this file will be lost! +****************************************************************************/ + +#include "prefs.h" + +#include <qvariant.h> +#include <qsplitter.h> +#include <qheader.h> +#include <klistview.h> +#include <qwidgetstack.h> +#include <qlayout.h> +#include <qtooltip.h> +#include <qwhatsthis.h> + +/* + * Constructs a Prefs as a child of 'parent', with the + * name 'name' and widget flags set to 'f'. + */ +Prefs::Prefs( QWidget* parent, const char* name, WFlags fl ) + : QWidget( parent, name, fl ) +{ + if ( !name ) + setName( "Prefs" ); + PrefsLayout = new QVBoxLayout( this, 0, 6, "PrefsLayout"); + + splitter3 = new QSplitter( this, "splitter3" ); + splitter3->setOrientation( QSplitter::Horizontal ); + + sourceListView = new KListView( splitter3, "sourceListView" ); + sourceListView->addColumn( tr2i18n( "Source" ) ); + sourceListView->header()->setClickEnabled( FALSE, sourceListView->header()->count() - 1 ); + sourceListView->header()->setResizeEnabled( FALSE, sourceListView->header()->count() - 1 ); + sourceListView->setResizeMode( KListView::AllColumns ); + + widgetStack = new QWidgetStack( splitter3, "widgetStack" ); + + WStackPage = new QWidget( widgetStack, "WStackPage" ); + widgetStack->addWidget( WStackPage, 0 ); + PrefsLayout->addWidget( splitter3 ); + languageChange(); + resize( QSize(340, 73).expandedTo(minimumSizeHint()) ); + clearWState( WState_Polished ); +} + +/* + * Destroys the object and frees any allocated resources + */ +Prefs::~Prefs() +{ + // no need to delete child widgets, Qt does it all for us +} + +/* + * Sets the strings of the subwidgets using the current + * language. + */ +void Prefs::languageChange() +{ + setCaption( tr2i18n( "Preferences" ) ); + sourceListView->header()->setLabel( 0, tr2i18n( "Source" ) ); +} + +#include "prefs.moc" diff --git a/src/prefs.ui b/src/prefs.ui new file mode 100644 index 0000000..1c4e28c --- /dev/null +++ b/src/prefs.ui @@ -0,0 +1,72 @@ +<!DOCTYPE UI><UI version="3.3" stdsetdef="1"> +<class>Prefs</class> +<author>Ken Werner</author> +<widget class="QWidget"> + <property name="name"> + <cstring>Prefs</cstring> + </property> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>340</width> + <height>73</height> + </rect> + </property> + <property name="caption"> + <string>Preferences</string> + </property> + <vbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <widget class="QSplitter"> + <property name="name"> + <cstring>splitter3</cstring> + </property> + <property name="orientation"> + <enum>Horizontal</enum> + </property> + <widget class="KListView"> + <column> + <property name="text"> + <string>Source</string> + </property> + <property name="clickable"> + <bool>false</bool> + </property> + <property name="resizable"> + <bool>false</bool> + </property> + </column> + <property name="name"> + <cstring>sourceListView</cstring> + </property> + <property name="resizeMode"> + <enum>AllColumns</enum> + </property> + </widget> + <widget class="QWidgetStack"> + <property name="name"> + <cstring>widgetStack</cstring> + </property> + <widget class="QWidget"> + <property name="name"> + <cstring>WStackPage</cstring> + </property> + <attribute name="id"> + <number>0</number> + </attribute> + </widget> + </widget> + </widget> + </vbox> +</widget> +<layoutdefaults spacing="6" margin="11"/> +<includehints> + <includehint>klistview.h</includehint> +</includehints> +</UI> diff --git a/src/sourcelistitem.cpp b/src/sourcelistitem.cpp new file mode 100644 index 0000000..aebe1c9 --- /dev/null +++ b/src/sourcelistitem.cpp @@ -0,0 +1,97 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "sourcelistitem.h" +#include <qcheckbox.h> + +//#include <kdebug.h> + +SourceListItem::SourceListItem(Source* inSource, QCheckListItem* inParent, Type tt): + QCheckListItem(inParent, inSource->getName(), tt){ + mSource = inSource; + initItem(); +} +SourceListItem::SourceListItem(Source* inSource, QCheckListItem* inParent, QListViewItem* after, Type tt): + QCheckListItem(inParent, after, inSource->getName(), tt){ + mSource = inSource; + initItem(); +} +SourceListItem::SourceListItem(Source* inSource, QListViewItem* inParent, Type tt): + QCheckListItem(inParent, inSource->getName(), tt){ + mSource = inSource; + initItem(); +} +SourceListItem::SourceListItem(Source* inSource, QListViewItem* inParent, QListViewItem* after, Type tt): + QCheckListItem(inParent, after, inSource->getName(), tt){ + mSource = inSource; + initItem(); +} +SourceListItem::SourceListItem(Source* inSource, QListView* inParent, Type tt): + QCheckListItem(inParent, inSource->getName(), tt){ + mSource = inSource; + initItem(); +} +SourceListItem::SourceListItem(Source* inSource, QListView* inParent, QListViewItem* after, Type tt): + QCheckListItem(inParent, after, inSource->getName(), tt){ + mSource = inSource; + initItem(); +} +SourceListItem::SourceListItem(Source* inSource, QListViewItem* inParent, const QPixmap& p): + QCheckListItem(inParent, inSource->getName(), p){ + mSource = inSource; + initItem(); +} +SourceListItem::SourceListItem(Source* inSource, QListView* inParent, const QPixmap& p): + QCheckListItem(inParent, inSource->getName(), p){ + mSource = inSource; + initItem(); +} + +SourceListItem::~SourceListItem(){ +} + +void SourceListItem::initItem(){ + //setText(1, mSource->getName()); + //item->setText(2, source->getDescription()); + + connect(mSource, SIGNAL(enabledChanged(bool, Source*)), this, SLOT(setEnabled(bool))); + setOn(mSource->isEnabled()); +} + +void SourceListItem::updateText() { + setText(0, mSource->getName()); +} + +Source* SourceListItem::getSource(){ + return mSource; +} + +void SourceListItem::setName(const QString& inName){ + setText(1, inName); +} + +void SourceListItem::setEnabled(bool inEnabled){ + mSource->setMaybeEnabled(inEnabled); +} + +void SourceListItem::stateChange(bool inOn){ + QCheckListItem::stateChange(inOn); + setEnabled(inOn); +} diff --git a/src/sourcelistitem.h b/src/sourcelistitem.h new file mode 100644 index 0000000..591ffd1 --- /dev/null +++ b/src/sourcelistitem.h @@ -0,0 +1,92 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef SOURCELISTITEM_H +#define SOURCELISTITEM_H + +#include <qlistview.h> +#include <qobject.h> +#include <sources/source.h> + +/** + * SourceListItem + * @author Ken Werner + */ +class SourceListItem : public QObject, public QCheckListItem { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source + */ + SourceListItem(Source* inSource, QCheckListItem* inParent, Type tt = RadioButtonController ); + SourceListItem(Source* inSource, QCheckListItem* inParent, QListViewItem* after, Type tt = RadioButtonController ); + SourceListItem(Source* inSource, QListViewItem* inParent, Type tt = RadioButtonController ); + SourceListItem(Source* inSource, QListViewItem* v, QListViewItem* after, Type tt = RadioButtonController ); + SourceListItem(Source* inSource, QListView* inParent, Type tt = RadioButtonController ); + SourceListItem(Source* inSource, QListView* inParent, QListViewItem* after, Type tt = RadioButtonController ); + SourceListItem(Source* inSource, QListViewItem* inParent, const QPixmap& p ); + SourceListItem(Source* inSource, QListView* inParent, const QPixmap& p ); + + virtual ~SourceListItem(); + + /** + * initializes the item (connects signals and slots) + */ + void initItem(); + + /** + * Returns a pointer to the Source + */ + Source* getSource(); + +public slots: + /** + * Sets whether this source is enabled or not + */ + void setEnabled(bool inEnabled); + + /** + * Sets the Name of this source + */ + void setName(const QString& inName); + + /** + * updates the text of the item + */ + void updateText(); + +signals: + /** + * This signal is emitted whenever the "on" flag (setOn) of this Item has changed + */ + void toggled(bool inToggled); + +protected: + /** + * This virtual function is called when the item changes its state + */ + virtual void stateChange(bool inEnabled); + + /** + * Pointer to the Source + */ + Source* mSource; +}; +#endif //SOURCELISTITEM_H diff --git a/src/sources/Makefile.am b/src/sources/Makefile.am new file mode 100644 index 0000000..4da707d --- /dev/null +++ b/src/sources/Makefile.am @@ -0,0 +1,20 @@ +INCLUDES = -I$(top_srcdir)/src/cpufreqd -I$(top_srcdir)/src/sources $(all_includes) +METASOURCES = AUTO +libsources_la_LDFLAGS = $(all_libraries) +libsources_la_LIBADD = $(LIB_QT) $(LIB_KDECORE) +noinst_LTLIBRARIES = libsources.la +libsources_la_SOURCES = acpithermalsrc.cpp batterysrc.cpp cpuinfofreqsrc.cpp \ + hddtempsrc.cpp hwmonfansrc.cpp hwmonthermalsrc.cpp i8ksrc.cpp ibmacpifansrc.cpp \ + ibmacpithermalsrc.cpp ibmhdaps.cpp ibookg4thermalsrc.cpp labelsource.cpp labelsourcePrefs.ui \ + nvidiathermalsrc.cpp omnibookthermalsrc.cpp source.cpp sourceprefs.ui \ + sysfreqsrc.cpp threadedtrigger.cpp triggeredsource.cpp updateevent.cpp \ + uptimesrc.cpp + +noinst_HEADERS = acpithermalsrc.h batterysrc.h cpuinfofreqsrc.h hddtempsrc.h \ + hwmonfansrc.h hwmonthermalsrc.h i8ksrc.h ibmacpifansrc.h ibmacpithermalsrc.h \ + ibmhdaps.h ibookg4thermalsrc.h labelsource.h nvidiathermalsrc.h \ + omnibookthermalsrc.h source.h sysfreqsrc.h \ + threadedtrigger.h triggeredsource.h updateevent.h uptimesrc.h + +AM_CXXFLAGS = @libhal_CFLAGS@ @nvcontrol_CFLAGS@ +LIBS += @libhal_LIBS@ @nvcontrol_LIBS@ diff --git a/src/sources/acpithermalsrc.cpp b/src/sources/acpithermalsrc.cpp new file mode 100644 index 0000000..fdc0260 --- /dev/null +++ b/src/sources/acpithermalsrc.cpp @@ -0,0 +1,65 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "acpithermalsrc.h" +#include <qtextstream.h> +#include <qdir.h> +#include <klocale.h> + +ACPIThermalSrc::ACPIThermalSrc(QWidget* inParent, const QFile& inSourceFile): + LabelSource(inParent), + mSourceFile(inSourceFile.name()), + mTrigger(this){ + mID = mSourceFile.name().section('/', -2, -2, QString::SectionSkipEmpty); + mName = mID; + mDescription = i18n("This source is provided by the Linux ACPI Thermal Zone driver."); +} + +ACPIThermalSrc::~ACPIThermalSrc(){ +} + +std::list<Source*>ACPIThermalSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + QDir d( "/proc/acpi/thermal_zone" ); + if( d.exists() ){ + d.setFilter( QDir::Dirs | QDir::NoSymLinks ); + d.setSorting( QDir::Name); + for( unsigned int i = 0; i < d.count(); i++ ){ + if((d[i] != ".") && (d[i] != "..")){ + QFile acpiFile(d.canonicalPath() + "/" + d[i] + "/temperature"); + list.push_back(new ACPIThermalSrc(inParent, acpiFile)); + } + } + } + return list; +} + +QString ACPIThermalSrc::fetchValue() { + QString s = "n/a"; + if(mSourceFile.open(IO_ReadOnly)){ + QTextStream textStream(&mSourceFile); + s = textStream.readLine(); + mSourceFile.close(); + s = s.remove("temperature:").stripWhiteSpace(); + s = formatTemperature(s.left(s.length()-1)); + } + return s; +} + diff --git a/src/sources/acpithermalsrc.h b/src/sources/acpithermalsrc.h new file mode 100644 index 0000000..1da73f5 --- /dev/null +++ b/src/sources/acpithermalsrc.h @@ -0,0 +1,64 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef ACPITHERMALSRC_H +#define ACPITHERMALSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * ACPIThermalSrc reads temperature values from /proc/acpi/thermal/ + * which is provided by the thermal.ko kernel module. + * @author Ken Werner + */ +class ACPIThermalSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File + */ + ACPIThermalSrc(QWidget* inParent, const QFile& inSourceFile); + virtual ~ACPIThermalSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //ACPITHERMALSRC_H diff --git a/src/sources/batterysrc.cpp b/src/sources/batterysrc.cpp new file mode 100644 index 0000000..b8f73ac --- /dev/null +++ b/src/sources/batterysrc.cpp @@ -0,0 +1,141 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "batterysrc.h" +#include <klocale.h> +#include <kdebug.h> + +#define DBUS_DEBUG_MESSAGE(func, _dbus_err_) \ + kdDebug() << "error in " << func << ": " << _dbus_err_.name << ", " << _dbus_err_.message << endl + +#define FREE_DBUS_ERROR(func, _dbus_err_) \ + do { \ + if(dbus_error_is_set(&_dbus_err_)) { \ + DBUS_DEBUG_MESSAGE(func, _dbus_err_); \ + dbus_error_free(&_dbus_err_); \ + } \ + } while(0) + +BatterySrc::BatterySrc(QWidget* inParent, QString inUDI): + LabelSource(inParent), + mTrigger(this, 5000), + mUDI(inUDI), + mLibHalContext(NULL){ + mID = mUDI.section('/', -1); + mName = mID; + mDescription = i18n("This source displays the current state of charge of your battery."); + + dbus_error_init(&mDBusError); + if((mLibHalContext = libhal_ctx_new()) == NULL){ + kdDebug() << "error: libmLibHalContext_new" << endl; + return; + } + if(!libhal_ctx_set_dbus_connection(mLibHalContext, dbus_bus_get (DBUS_BUS_SYSTEM, &mDBusError))){ + FREE_DBUS_ERROR("libhal_ctx_set_dbus_connection", mDBusError); + libhal_ctx_free(mLibHalContext); + mLibHalContext = NULL; + return; + } + if(!libhal_ctx_init(mLibHalContext, &mDBusError)){ + FREE_DBUS_ERROR("libhal_ctx_init", mDBusError); + libhal_ctx_free(mLibHalContext); + mLibHalContext = NULL; + return; + } +} + +BatterySrc::~BatterySrc(){ + if(mLibHalContext != NULL){ + if(!libhal_ctx_shutdown(mLibHalContext, &mDBusError)){ + FREE_DBUS_ERROR("libhal_ctx_shutdown", mDBusError); + } + libhal_ctx_free(mLibHalContext); + } +} + +std::list<Source*>BatterySrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + + LibHalContext* libHalContext; + DBusError dBusError; + dbus_error_init(&dBusError); + if((libHalContext = libhal_ctx_new()) == NULL){ + kdDebug() << "error: libmLibHalContext_new" << endl; + return list; + } + if(!libhal_ctx_set_dbus_connection(libHalContext, dbus_bus_get (DBUS_BUS_SYSTEM, &dBusError))){ + FREE_DBUS_ERROR("libhal_ctx_set_dbus_connection", dBusError); + libhal_ctx_free(libHalContext); + return list; + } + if(!libhal_ctx_init(libHalContext, &dBusError)){ + FREE_DBUS_ERROR("libhal_ctx_init", dBusError); + libhal_ctx_free(libHalContext); + return list; + } + + int num = 0; + char** udis = libhal_manager_find_device_string_match(libHalContext, "info.category", "battery", &num, &dBusError); + if(!dbus_error_is_set(&dBusError)){ + for(int i = 0; i < num; ++i){ + if(libhal_device_get_property_type(libHalContext, udis[i], "battery.charge_level.last_full", &dBusError) == LIBHAL_PROPERTY_TYPE_INT32 && libhal_device_get_property_type(libHalContext, udis[i], "battery.charge_level.current", &dBusError) == LIBHAL_PROPERTY_TYPE_INT32){ + if(!dbus_error_is_set(&dBusError)) // todo: check whether this check is redundant + list.push_back(new BatterySrc(inParent, udis[i])); + } + if(dbus_error_is_set(&dBusError)) { + DBUS_DEBUG_MESSAGE("libhal_device_get_property_type", dBusError); + dbus_error_free(&dBusError); + } + } + } else { + DBUS_DEBUG_MESSAGE("libhal_manager_find_device_string_match", dBusError); + dbus_error_free(&dBusError); + } + if(!libhal_ctx_shutdown(libHalContext, &dBusError)){ + FREE_DBUS_ERROR("libhal_ctx_shutdown", dBusError); + } + // clean up + libhal_ctx_free(libHalContext); + libhal_free_string_array(udis); + return list; +} + +QString BatterySrc::fetchValue() { + QString s = "n/a"; + if(mLibHalContext != NULL){ + int last_full = 0; + int current = 0; + + last_full = libhal_device_get_property_int(mLibHalContext, mUDI.ascii(), "battery.charge_level.last_full", &mDBusError); + if(dbus_error_is_set(&mDBusError)){ + DBUS_DEBUG_MESSAGE("libhal_device_get_property_int", mDBusError); + dbus_error_free(&mDBusError); + return s; + } + current = libhal_device_get_property_int(mLibHalContext, mUDI.ascii(), "battery.charge_level.current", &mDBusError); + if(dbus_error_is_set(&mDBusError)){ + DBUS_DEBUG_MESSAGE("libhal_device_get_property_int", mDBusError); + dbus_error_free(&mDBusError); + return s; + } + s = QString::number(qRound((current * 100.0) / last_full)) + " %"; + } + return s; +} diff --git a/src/sources/batterysrc.h b/src/sources/batterysrc.h new file mode 100644 index 0000000..e491fcc --- /dev/null +++ b/src/sources/batterysrc.h @@ -0,0 +1,76 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef BATTERYSRC_H +#define BATTERYSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <qstring.h> +#include <list> +#include <hal/libhal.h> + +/** + * BatterySrc fetches the state of charge of the battery from HAL + * @author Ken Werner + */ +class BatterySrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source + */ + BatterySrc(QWidget* inParent, QString inUDI); + virtual ~BatterySrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; + + /** + * The hal udi of that battery source + */ + QString mUDI; + + /** + * The local hal context + */ + LibHalContext* mLibHalContext; + + /** + * The local dbus error object + */ + DBusError mDBusError; + +}; +#endif //BATTERYSRC_H diff --git a/src/sources/cpuinfofreqsrc.cpp b/src/sources/cpuinfofreqsrc.cpp new file mode 100644 index 0000000..6227439 --- /dev/null +++ b/src/sources/cpuinfofreqsrc.cpp @@ -0,0 +1,57 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "cpuinfofreqsrc.h" +#include <qtextstream.h> +#include <qfile.h> +#include <klocale.h> + +CpuinfoFreqSrc::CpuinfoFreqSrc(QWidget* inParent, const QFile& inSourceFile): + LabelSource(inParent), + mSourceFile(inSourceFile.name()), + mTrigger(this){ + mID = "proc"; + mName = mID; + mDescription = i18n("This source is provided by the Linux kernel proc vfs interface."); +} + +CpuinfoFreqSrc::~CpuinfoFreqSrc(){ +} + +std::list<Source*>CpuinfoFreqSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + QFile cpuinfoFreqFile( "/proc/cpuinfo" ); + if(cpuinfoFreqFile.open(IO_ReadOnly)) + list.push_back(new CpuinfoFreqSrc(inParent, cpuinfoFreqFile)); + return list; +} + +QString CpuinfoFreqSrc::fetchValue(){ + QString s = "n/a"; + if(mSourceFile.open(IO_ReadOnly)){ + while( mSourceFile.readLine( s, 64 ) != -1 ) + if( s.startsWith( "cpu MHz") ) + break; + s = s.section( ':', 1, 1 ).stripWhiteSpace(); + s = KHzinHumanReadable(uint(s.toDouble() * 1000)); + mSourceFile.close(); + } + return s; +} diff --git a/src/sources/cpuinfofreqsrc.h b/src/sources/cpuinfofreqsrc.h new file mode 100644 index 0000000..da3f734 --- /dev/null +++ b/src/sources/cpuinfofreqsrc.h @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef CPUINFOFREQSRC_H +#define CPUINFOFREQSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * CpuinfoFreqSrc reads temperature values from /proc/cpuinfo + */ +class CpuinfoFreqSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File + */ + CpuinfoFreqSrc(QWidget* inParent, const QFile& inSourceFile); + virtual ~CpuinfoFreqSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //CPUINFOFREQSRC_H + diff --git a/src/sources/hddtempsrc.cpp b/src/sources/hddtempsrc.cpp new file mode 100644 index 0000000..eb0419e --- /dev/null +++ b/src/sources/hddtempsrc.cpp @@ -0,0 +1,100 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "hddtempsrc.h" + +#include <qsocketdevice.h> +#include <qhostaddress.h> +#include <qcstring.h> + +#include <klocale.h> + +//#include <kdebug.h> + +const Q_ULONG HDDTempSrc::BUFFERSIZE = 128; +const Q_UINT32 HDDTempSrc::ADDRESS = (127<<24)|1; +const Q_UINT16 HDDTempSrc::PORT = 7634; + +HDDTempSrc::HDDTempSrc(QWidget* inParent, uint inIndex, const QString& inDevice, const QString& inModelName): + LabelSource(inParent), + mIndex(inIndex), + mTrigger(this){ + mID = "HDDTemp" + QString().setNum(inIndex); + mName = mID; + mDescription = i18n("This source is provided by hddtemp. (%1, %2)").arg(inDevice).arg(inModelName); +} + +HDDTempSrc::~HDDTempSrc(){ +} + +std::list<Source*>HDDTempSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + QSocketDevice sd; + sd.setBlocking(true); + if(sd.connect((ADDRESS), PORT)){ + //kdDebug() << "hddtemp is availalble" << endl; + QCString tmp(0); + Q_LONG numBytes = 0; + Q_LONG numTotalBytes = 0; + do{ + tmp.resize(numTotalBytes+BUFFERSIZE); + numBytes = sd.readBlock(tmp.data()+numTotalBytes, BUFFERSIZE); + // numBytes could be -1 too in case of an error! + numTotalBytes += QMAX(numBytes, 0); + }while(numBytes>0); + sd.close(); + tmp.resize(numTotalBytes+1); + //kdDebug() << "following data was read: " << tmp << endl; + // split the string + QStringList sl = QStringList::split(tmp[0], tmp); + // create the sources + if(sl.size() > 0 && sl.size()%4 == 0){ + for(uint i = 0; i < sl.size(); i += 4) + list.push_back(new HDDTempSrc(inParent, i/4, sl[i], sl[i+1])); + } + }//else + //kdDebug() << "hddtemp is not availalble" << endl; // << sd.error() << endl; + return list; +} + +QString HDDTempSrc::fetchValue(){ + QString s = "n/a"; + QSocketDevice sd; + sd.setBlocking(true); + if(sd.connect((ADDRESS), PORT)){ + QCString tmp(0); + Q_LONG numBytes = 0; + Q_LONG numTotalBytes = 0; + do{ + tmp.resize(numTotalBytes+BUFFERSIZE); + numBytes = sd.readBlock(tmp.data()+numTotalBytes, BUFFERSIZE); + // numBytes could be -1 too in case of an error! + numTotalBytes += QMAX(numBytes, 0); + }while(numBytes>0); + sd.close(); + tmp.resize(numTotalBytes+1); + QStringList sl = QStringList::split(tmp[0], tmp); + if(sl.size() > 0 && sl.size()%4 == 0){ + s = formatTemperature(sl[mIndex*4+2]); + } + } + return s; +} + diff --git a/src/sources/hddtempsrc.h b/src/sources/hddtempsrc.h new file mode 100644 index 0000000..60d8151 --- /dev/null +++ b/src/sources/hddtempsrc.h @@ -0,0 +1,74 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef HDDTEMPSRC_H +#define HDDTEMPSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <list> + +/** + * HDDTempSrc reads temperature of your harddisk provided by hddtemp + */ +class HDDTempSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) + + /** + * defines how many bytes are read at once from the network + */ + static const Q_ULONG BUFFERSIZE; + + /** + * defines the address + */ + static const Q_UINT32 ADDRESS; + + /** + * defines the port + */ + static const Q_UINT16 PORT; + +public: + /** + * Creates a new Source from the given File + */ + HDDTempSrc(QWidget* inParent, uint inIndex, const QString& inDevice, const QString& inModelName); + virtual ~HDDTempSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + uint mIndex; + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //HDDTEMPSRC_H + diff --git a/src/sources/hwmonfansrc.cpp b/src/sources/hwmonfansrc.cpp new file mode 100644 index 0000000..16dcd84 --- /dev/null +++ b/src/sources/hwmonfansrc.cpp @@ -0,0 +1,91 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "hwmonfansrc.h" +#include <qtextstream.h> +#include <qdir.h> +#include <klocale.h> + +HwMonFanSrc::HwMonFanSrc(QWidget* inParent, const QFile& inSourceFile, unsigned int inIndex): + LabelSource(inParent), + mSourceFile(inSourceFile.name()), + mTrigger(this, 3000){ + //mName = "HwMon " + inSourceFile.name()[inSourceFile.name().length() - 7]; + mID = "hwmonFan" + QString::number(inIndex); + mName = mID; + mDescription = i18n("This fan source is provided by hwmon. (%1)").arg(inSourceFile.name()); +} + +HwMonFanSrc::~HwMonFanSrc(){ +} + +std::list<Source*>HwMonFanSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + // /sys/class/hwmon/hwmon*/device/fan*_input + QDir d("/sys/class/hwmon/"); + QDir subd; + QString device = "/device"; + // check the i2c interface if hwmon isn't available + // /sys/bus/i2c/devices/*/fan*_input + if(!d.exists()){ + d.setPath("/sys/bus/i2c/devices/"); + device = ""; + } + + if(d.exists()){ + unsigned int index = 1; + d.setFilter(QDir::Dirs); + d.setSorting(QDir::Name); + for(unsigned int i = 0; i < d.count(); i++ ){ + if((d[i] != ".") && (d[i] != "..")){ + subd = d.canonicalPath() + "/" + d[i] + device; + subd.setFilter(QDir::Files); + subd.setSorting(QDir::Name); + for ( unsigned int j = 0; j < subd.count(); j++ ){ + if((subd[j] != ".") && (subd[j] != "..") && (QDir::match(subd.canonicalPath() + "/fan*_input", subd.canonicalPath() + "/" + subd[j]))){ + QFile f(subd.canonicalPath() + "/" + subd[j]); + if(f.open(IO_ReadOnly)){ + QTextStream textStream( &f ); + QString s = textStream.readLine(); + f.close(); + if(!s.startsWith("-")){ + list.push_back(new HwMonFanSrc(inParent, f, index)); + index++; + } + } + } + } + } + } + } + return list; +} + +QString HwMonFanSrc::fetchValue(){ + QString s = "n/a"; + if(mSourceFile.open(IO_ReadOnly)){ + QTextStream textStream( &mSourceFile ); + s = textStream.readLine().stripWhiteSpace(); + mSourceFile.close(); + s.append(" rpm"); + } + return s; +} + diff --git a/src/sources/hwmonfansrc.h b/src/sources/hwmonfansrc.h new file mode 100644 index 0000000..8f8396e --- /dev/null +++ b/src/sources/hwmonfansrc.h @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef HWMONFANSRC_H +#define HWMONFANSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * HwMonThermalSrc reads fan rpm values from /sys/class/hwmon/\*\/device/fan*_input + * that is provided by the hwmon kernel by the lmsensors kernel modules since version 2.6.14. + * /sys/bus/i2c/devices/\*\/fan*_input is used as a fall back. + * see /usr/src/linux/Documentation/hwmon/sysfs-interface + */ +class HwMonFanSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File + */ + HwMonFanSrc(QWidget* inParent, const QFile& inSourceFile, unsigned int inIndex); + virtual ~HwMonFanSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //HWMONFANSRC_H + diff --git a/src/sources/hwmonthermalsrc.cpp b/src/sources/hwmonthermalsrc.cpp new file mode 100644 index 0000000..38ab38f --- /dev/null +++ b/src/sources/hwmonthermalsrc.cpp @@ -0,0 +1,94 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "hwmonthermalsrc.h" +#include <qtextstream.h> +#include <qdir.h> +#include <klocale.h> + +HwMonThermalSrc::HwMonThermalSrc(QWidget* inParent, const QFile& inSourceFile, unsigned int inIndex): + LabelSource(inParent), + mSourceFile(inSourceFile.name()), + mTrigger(this, 3000){ + //mName = "HwMon " + inSourceFile.name()[inSourceFile.name().length() - 7]; + mID = "hwmon" + QString::number(inIndex); + mName = mID; + mDescription = i18n("This thermal source is provided by hwmon. (%1)").arg(inSourceFile.name()); +} + +HwMonThermalSrc::~HwMonThermalSrc(){ +} + +std::list<Source*>HwMonThermalSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + // /sys/class/hwmon/*/device/temp*_input + QDir d("/sys/class/hwmon/"); + QDir subd; + QString device = "/device"; + // check the i2c interface if hwmon isn't available + // /sys/bus/i2c/devices/*/temp*_input + if(!d.exists()){ + d.setPath("/sys/bus/i2c/devices/"); + device = ""; + } + + if(d.exists()){ + unsigned int index = 1; + d.setFilter(QDir::Dirs); + d.setSorting(QDir::Name); + for(unsigned int i = 0; i < d.count(); i++ ){ + if((d[i] != ".") && (d[i] != "..")){ + subd = d.canonicalPath() + "/" + d[i] + device; + subd.setFilter(QDir::Files); + subd.setSorting(QDir::Name); + for(unsigned int j = 0; j < subd.count(); j++){ + if((subd[j] != ".") && (subd[j] != "..") && (QDir::match(subd.canonicalPath() + "/temp*_input", subd.canonicalPath() + "/" + subd[j]))){ + QFile f(subd.canonicalPath() + "/" + subd[j]); + if(f.open(IO_ReadOnly)){ + QTextStream textStream( &f ); + QString s = textStream.readLine(); + f.close(); + if(!s.startsWith("-")){ + list.push_back(new HwMonThermalSrc(inParent, f, index)); + index++; + } + } + } + } + } + } + } + return list; +} + +QString HwMonThermalSrc::fetchValue(){ + QString s = "n/a"; + if(mSourceFile.open(IO_ReadOnly)){ + QTextStream textStream( &mSourceFile ); + s = textStream.readLine(); + mSourceFile.close(); + s = s.stripWhiteSpace(); + if(s.length() > 3) + s = s.remove(s.length() - 3, 3); + s = formatTemperature(s); + } + return s; +} + diff --git a/src/sources/hwmonthermalsrc.h b/src/sources/hwmonthermalsrc.h new file mode 100644 index 0000000..8372745 --- /dev/null +++ b/src/sources/hwmonthermalsrc.h @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef HWMONTHERMALSRC_H +#define HWMONTHERMALSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * HwMonThermalSrc reads temperature values from /sys/class/hwmon/\*\/device/temp*_input + * that is provided by the hwmon kernel by the lmsensors kernel modules since version 2.6.14. + * /sys/bus/i2c/devices/\*\/temp*_input is used as a fall back. + * see /usr/src/linux/Documentation/hwmon/sysfs-interface + */ +class HwMonThermalSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File + */ + HwMonThermalSrc(QWidget* inParent, const QFile& inSourceFile, unsigned int inIndex); + virtual ~HwMonThermalSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //HWMONTHERMALSRC_H + diff --git a/src/sources/i8ksrc.cpp b/src/sources/i8ksrc.cpp new file mode 100644 index 0000000..2123919 --- /dev/null +++ b/src/sources/i8ksrc.cpp @@ -0,0 +1,111 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "i8ksrc.h" +#include <qtextstream.h> +#include <qdir.h> +#include <klocale.h> + +I8kSrc::I8kSrc(QWidget* inParent, const QFile& inSourceFile, unsigned int inIndex): + LabelSource(inParent), + mIndex(inIndex), + mSourceFile(inSourceFile.name()), + mTrigger(this){ + mID = I8kSrc::index2Name(inIndex); + mName = mID; + mDescription = i18n("This source is provided by i8k kernel module."); +} + +I8kSrc::~I8kSrc(){ +} + +std::list<Source*>I8kSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + QFile i8kFile("/proc/i8k"); + if(i8kFile.open(IO_ReadOnly)){ + QTextStream textStream(&i8kFile); + QString s = textStream.readLine(); + i8kFile.close(); + QStringList entries = QStringList::split(' ', s); + if(entries.size() && entries[0] == "1.0"){ // support for version 1.0 + // CPU temp + if(entries.size() >= 4 && !entries[3].startsWith("-")) + list.push_back(new I8kSrc(inParent, i8kFile, 3)); + // left fan + if(entries.size() >= 7 && !entries[4].startsWith("-")) + list.push_back(new I8kSrc(inParent, i8kFile, 6)); + // left fan + if(entries.size() >= 8 && !entries[5].startsWith("-")) + list.push_back(new I8kSrc(inParent, i8kFile, 7)); + } + } + return list; +} +QString I8kSrc::fetchValue(){ + QString s = "n/a"; + if(mSourceFile.open(IO_ReadOnly)){ + QTextStream textStream(&mSourceFile); + s = textStream.readLine(); + mSourceFile.close(); + s = s.section(' ', mIndex, mIndex, QString::SectionSkipEmpty).stripWhiteSpace(); + switch(mIndex){ + case 3: // CPU temperature (Celsius) + s = formatTemperature(s); + break; + case 6: // left fan rpm + case 7: // right fan rpm + if(s.length() > 1) + s.truncate(s.length() - 1); //s = QString::number(s.toInt()/10); + s.append(" rpm"); + break; + default: + break; + } + } + return s; +} + +QString I8kSrc::index2Name(unsigned int inIndex){ + switch(inIndex){ + case 0: // /proc/i8k format version + return "i8k Format Version"; + case 1: // BIOS version + return "Bios"; + case 2: // serial number + return "Serial"; + case 3: // CPU temperature (Celsius) + return "CPU"; + case 4: // left fan status + return "left Fan Status"; + case 5: // right fan status + return "right Fan Status"; + case 6: // left fan rpm + return "left Fan"; + case 7: // right fan rpm + return "right Fan"; + case 8: // ac status + return "AC Status"; + case 9: // buttons status + return "Button Status"; + default: + return "unknown" + QString().setNum(inIndex); + } +} + diff --git a/src/sources/i8ksrc.h b/src/sources/i8ksrc.h new file mode 100644 index 0000000..037e518 --- /dev/null +++ b/src/sources/i8ksrc.h @@ -0,0 +1,74 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef I8KSRC_H +#define I8KSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * I8kSrc reads CPU thermal and fan rpm values from /proc/i8k + * which is provided by the i8k kernel module. + */ +class I8kSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File + */ + I8kSrc(QWidget* inParent, const QFile& inSourceFile, unsigned int inIndex); + virtual ~I8kSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * Returns the name (such as CPU, GPU, ...) of the sensor according to a given index + */ + static QString index2Name(unsigned int inIndex); + + /** + * the index of the source in /proc/acpi/ibm/thermal + */ + unsigned int mIndex; + + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //I8KSRC_H + diff --git a/src/sources/ibmacpifansrc.cpp b/src/sources/ibmacpifansrc.cpp new file mode 100644 index 0000000..f2edf33 --- /dev/null +++ b/src/sources/ibmacpifansrc.cpp @@ -0,0 +1,57 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "ibmacpifansrc.h" +#include <qtextstream.h> +#include <qfile.h> +#include <klocale.h> + +IBMACPIFanSrc::IBMACPIFanSrc(QWidget* inParent, const QFile& inSourceFile): + LabelSource(inParent), + mSourceFile(inSourceFile.name()), + mTrigger(this){ + mID = "Fan"; + mName = mID; + mDescription = i18n("This source is provided by the ACPI driver for IBM ThinkPads."); +} + +IBMACPIFanSrc::~IBMACPIFanSrc(){ +} + +std::list<Source*>IBMACPIFanSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + QFile ibmFile( "/proc/acpi/ibm/fan" ); + if(ibmFile.open(IO_ReadOnly)) + list.push_back(new IBMACPIFanSrc(inParent, ibmFile)); + return list; +} + +QString IBMACPIFanSrc::fetchValue(){ + QString s = "n/a"; + if(mSourceFile.open(IO_ReadOnly)){ + while( mSourceFile.readLine( s, 64 ) != -1 ) + if(s.startsWith("speed:")) + break; + s = s.section( ':', 1, 1 ).stripWhiteSpace() + " rpm"; + mSourceFile.close(); + } + return s; +} + diff --git a/src/sources/ibmacpifansrc.h b/src/sources/ibmacpifansrc.h new file mode 100644 index 0000000..7af22fc --- /dev/null +++ b/src/sources/ibmacpifansrc.h @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef IBMACPIFANSRC_H +#define IBMACPIFANSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * IBMACPIFanSrc reads fan values from /proc/acpi/ibm/thermal + * which is provided by the ibm-acpi kernel module. + */ +class IBMACPIFanSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File and index + */ + IBMACPIFanSrc(QWidget* inParent, const QFile& inSourceFile); + virtual ~IBMACPIFanSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //IBMACPIFANSRC_H diff --git a/src/sources/ibmacpithermalsrc.cpp b/src/sources/ibmacpithermalsrc.cpp new file mode 100644 index 0000000..f19281c --- /dev/null +++ b/src/sources/ibmacpithermalsrc.cpp @@ -0,0 +1,88 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "ibmacpithermalsrc.h" +#include <qtextstream.h> +#include <qfile.h> +#include <klocale.h> +//#include "hal/libhal.h" +// hal-get-property --udi /org/freedesktop/Hal/devices/computer --key system.product + +IBMACPIThermalSrc::IBMACPIThermalSrc(QWidget* inParent, const QFile& inSourceFile, unsigned int inIndex): + LabelSource(inParent), + mIndex(inIndex), + mSourceFile(inSourceFile.name()), + mTrigger(this){ + //mName = mName.setNum(inIndex+1).prepend("IBM"); + mID = IBMACPIThermalSrc::index2Name(inIndex); + mName = mID; + mDescription = i18n("This source is provided by the ACPI driver for IBM ThinkPads."); +} + +IBMACPIThermalSrc::~IBMACPIThermalSrc(){ +} + +std::list<Source*>IBMACPIThermalSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + QFile ibmFile( "/proc/acpi/ibm/thermal" ); + if(ibmFile.open(IO_ReadOnly)){ + QTextStream textStream( &ibmFile ); + QString s = textStream.readLine(); + ibmFile.close(); + s = s.remove("temperatures:"); + QStringList entries = QStringList::split(' ' ,s); + for ( unsigned int i = 0; i < entries.size(); i++ ){ + if(!entries[i].startsWith("-") && !entries[i].startsWith("0")) + list.push_back(new IBMACPIThermalSrc(inParent, ibmFile, i)); + } + } + return list; +} + +QString IBMACPIThermalSrc::fetchValue(){ + QString s = "n/a"; + if(mSourceFile.open(IO_ReadOnly)){ + QTextStream textStream( &mSourceFile ); + s = textStream.readLine(); + mSourceFile.close(); + s = s.section(':', 1, 1).section(' ', mIndex, mIndex, QString::SectionSkipEmpty).stripWhiteSpace(); + s = formatTemperature(s); + } + return s; +} + +QString IBMACPIThermalSrc::index2Name(unsigned int inIndex){ + switch(inIndex){ + case 0: + return "CPU"; + case 1: + return "MiniPCI"; + case 2: + return "HDD"; + case 3: + return "GPU"; + case 4: + return "Battery1"; + case 6: + return "Battery2"; + default: + return "ibmacpi" + QString().setNum(inIndex); + } +} diff --git a/src/sources/ibmacpithermalsrc.h b/src/sources/ibmacpithermalsrc.h new file mode 100644 index 0000000..0ea937e --- /dev/null +++ b/src/sources/ibmacpithermalsrc.h @@ -0,0 +1,73 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef IBMACPITHERMALSRC_H +#define IBMACPITHERMALSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * IBMACPIThermalSrc reads temperature values from /proc/acpi/ibm/thermal + * which is provided by the ibm-acpi kernel module. + */ +class IBMACPIThermalSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File and index + */ + IBMACPIThermalSrc(QWidget* inParent, const QFile& inSourceFile, unsigned int inIndex); + virtual ~IBMACPIThermalSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * Returns the name (such as CPU, GPU, ...) of the sensor according to a given index + */ + static QString index2Name(unsigned int inIndex); + + /** + * the index of the source in /proc/acpi/ibm/thermal + */ + unsigned int mIndex; + + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //IBMACPITHERMALSRC_H diff --git a/src/sources/ibmhdaps.cpp b/src/sources/ibmhdaps.cpp new file mode 100644 index 0000000..05d945b --- /dev/null +++ b/src/sources/ibmhdaps.cpp @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "ibmhdaps.h" +#include <qtextstream.h> +#include <qdir.h> +#include <klocale.h> + +IBMHDAPSSrc::IBMHDAPSSrc(QWidget* inParent, const QFile& inSourceFile): + LabelSource(inParent), + mSourceFile(inSourceFile.name()), + mTrigger(this){ + mID = "HDAPS" + mSourceFile.name().section( '/', -1, -1, QString::SectionSkipEmpty ).remove("temp"); + mName = mID; + mDescription = i18n("This thermal source is provided by the IBM HDAPS driver."); +} + +IBMHDAPSSrc::~IBMHDAPSSrc(){ +} + +std::list<Source*>IBMHDAPSSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + QDir hdapsDir( "/sys/bus/platform/drivers/hdaps/hdaps" ); + if ( hdapsDir.exists() ){ + hdapsDir.setFilter( QDir::Files | QDir::NoSymLinks ); + hdapsDir.setSorting( QDir::Name); + for ( unsigned int i = 0; i < hdapsDir.count(); i++ ){ + if(hdapsDir[i].startsWith("temp")){ + QFile hdapsFile(hdapsDir.canonicalPath() + "/" + hdapsDir[i]); + list.push_back(new IBMHDAPSSrc(inParent, hdapsFile)); + } + } + } + return list; +} + +QString IBMHDAPSSrc::fetchValue(){ + QString s = "n/a"; + if(mSourceFile.open(IO_ReadOnly)){ + QTextStream textStream( &mSourceFile ); + s = textStream.readLine().stripWhiteSpace(); + mSourceFile.close(); + s = formatTemperature(s); + } + return s; +} diff --git a/src/sources/ibmhdaps.h b/src/sources/ibmhdaps.h new file mode 100644 index 0000000..d696f9f --- /dev/null +++ b/src/sources/ibmhdaps.h @@ -0,0 +1,65 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef IBMHDAPSSRC_H +#define IBMHDAPSSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * IBMHDAPSSrc reads temperature values of the IBM Hard Drive Active Protection System (HDAPS) + * from /sys/bus/platform/drivers/hdaps/hdaps/temp1 + * which is provided by the hdapds.ko kernel module. + * @author Ken Werner + */ +class IBMHDAPSSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File + */ + IBMHDAPSSrc(QWidget* inParent, const QFile& inSourceFile); + virtual ~IBMHDAPSSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //IBMHDAPSSRC_H diff --git a/src/sources/ibookg4thermalsrc.cpp b/src/sources/ibookg4thermalsrc.cpp new file mode 100644 index 0000000..ba3a69a --- /dev/null +++ b/src/sources/ibookg4thermalsrc.cpp @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (C) 2006 by * + * Flavio Castelli, [email protected] * + * Ken Werner, [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "ibookg4thermalsrc.h" +#include <qtextstream.h> +#include <klocale.h> + +IbookG4ThermalSrc::IbookG4ThermalSrc(QWidget* inParent, const QFile& inSourceFile, const QString& inName): + LabelSource(inParent), + mSourceFile(inSourceFile.name()), + mTrigger(this){ + mID = inName; + mName = mID; + mDescription = i18n("This source shows a thermal sensor of your iBook."); +} + +IbookG4ThermalSrc::~IbookG4ThermalSrc(){ +} + +std::list<Source*>IbookG4ThermalSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + QFile ibookCPUFile("/sys/devices/temperatures/sensor1_temperature"); + if(ibookCPUFile.open(IO_ReadOnly)){ + list.push_back(new IbookG4ThermalSrc(inParent, ibookCPUFile, QString("CPU"))); + ibookCPUFile.close(); + } + QFile ibookGPUFile("/sys/devices/temperatures/sensor2_temperature"); + if(ibookGPUFile.open(IO_ReadOnly)){ + list.push_back(new IbookG4ThermalSrc(inParent, ibookGPUFile, QString("GPU"))); + ibookGPUFile.close(); + } + return list; +} + +QString IbookG4ThermalSrc::fetchValue(){ + QString s = "n/a"; + if(mSourceFile.open(IO_ReadOnly)){ + QTextStream textStream( &mSourceFile ); + s = textStream.readLine().stripWhiteSpace(); + mSourceFile.close(); + s= formatTemperature(s); + } + return s; +} + diff --git a/src/sources/ibookg4thermalsrc.h b/src/sources/ibookg4thermalsrc.h new file mode 100644 index 0000000..36e8340 --- /dev/null +++ b/src/sources/ibookg4thermalsrc.h @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright (C) 2006 by * + * Flavio Castelli, [email protected] * + * Ken Werner, [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef IBOOKG4THERMALSRC_H +#define IBOOKG4THERMALSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * @author Flavio Castelli + * @author Ken Werner + * IbookG4ThermalSrc reads temperature values from /sys/devices/temperatures/sensor[1,2]_temperature + * It may work on other Apple machines as well (please let me know) + */ +class IbookG4ThermalSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File + */ + IbookG4ThermalSrc(QWidget* inParent, const QFile& inSourceFile, const QString& inName); + virtual ~IbookG4ThermalSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif // IBOOKG4THERMALSRC_H diff --git a/src/sources/labelsource.cpp b/src/sources/labelsource.cpp new file mode 100644 index 0000000..a2e5026 --- /dev/null +++ b/src/sources/labelsource.cpp @@ -0,0 +1,133 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "labelsource.h" +#include <qlabel.h> +#include <kcolorbutton.h> +#include <kfontrequester.h> +#include <qcombobox.h> +#include <qcheckbox.h> +#include <klocale.h> + +//#include "kdebug.h" + +LabelSource::LabelSource(QWidget* inParent): + TriggeredSource(inParent), + mParent(inParent), + mLabelSourcePrefs(NULL){ +} + +LabelSource::~LabelSource(){ +} + +QWidget* LabelSource::getWidget(){ + //kdDebug() << "LabelSource::getWidget called: " << mID << endl; + return mLabel; +} + +void LabelSource::createSubPrefs(QWidget* inParent){ + if(!mLabelSourcePrefs){ + mLabelSourcePrefs = new LabelSourcePrefs(inParent, "labelsourceprefsui"); + // disable nameCheckBox if taskbarCheckBox is disabled + connect(mSourcePrefs->taskbarCheckBox, SIGNAL(toggled(bool)), mLabelSourcePrefs->colorLabel, SLOT(setEnabled(bool))); + connect(mSourcePrefs->taskbarCheckBox, SIGNAL(toggled(bool)), mLabelSourcePrefs->colorButton, SLOT(setEnabled(bool))); + connect(mSourcePrefs->taskbarCheckBox, SIGNAL(toggled(bool)), mLabelSourcePrefs->fontLabel, SLOT(setEnabled(bool))); + connect(mSourcePrefs->taskbarCheckBox, SIGNAL(toggled(bool)), mLabelSourcePrefs->fontRequester, SLOT(setEnabled(bool))); + connect(mSourcePrefs->taskbarCheckBox, SIGNAL(toggled(bool)), mLabelSourcePrefs->alignmentLabel, SLOT(setEnabled(bool))); + connect(mSourcePrefs->taskbarCheckBox, SIGNAL(toggled(bool)), mLabelSourcePrefs->alignmentComboBox, SLOT(setEnabled(bool))); + addPrefs(mLabelSourcePrefs); + } +} + +void LabelSource::updatePrefsGUI(){ + TriggeredSource::updatePrefsGUI(); // update prefs of the super class + mLabelSourcePrefs->colorButton->setColor(mLabel->paletteForegroundColor()); + mLabelSourcePrefs->fontRequester->setFont(mLabel->font()); + switch (mLabel->alignment()) { + case Qt::AlignCenter: + mLabelSourcePrefs->alignmentComboBox->setCurrentItem(1); + break; + case Qt::AlignRight: + mLabelSourcePrefs->alignmentComboBox->setCurrentItem(2); + break; + default: // Qt::AlignLeft + break; + mLabelSourcePrefs->alignmentComboBox->setCurrentItem(0); + } +} + +void LabelSource::setPrefsWidgetsEnabled(bool isEnabled, bool isShownOnApplet){ + TriggeredSource::setPrefsWidgetsEnabled(isEnabled, isShownOnApplet); + // disable/enable some widgets if source is disabled/enabled if(isEnabled) + mLabelSourcePrefs->colorLabel->setEnabled(isEnabled && isShownOnApplet); + mLabelSourcePrefs->colorButton->setEnabled(isEnabled && isShownOnApplet); + mLabelSourcePrefs->fontLabel->setEnabled(isEnabled && isShownOnApplet); + mLabelSourcePrefs->fontRequester->setEnabled(isEnabled && isShownOnApplet); + mLabelSourcePrefs->alignmentLabel->setEnabled(isEnabled && isShownOnApplet); + mLabelSourcePrefs->alignmentComboBox->setEnabled(isEnabled && isShownOnApplet); +} + +void LabelSource::applyPrefs(){ + TriggeredSource::applyPrefs(); // call apply prefs of the super class + mLabel->setPaletteForegroundColor(mLabelSourcePrefs->colorButton->color()); + mLabel->setFont(mLabelSourcePrefs->fontRequester->font()); + int alignID = mLabelSourcePrefs->alignmentComboBox->currentItem(); + Qt::AlignmentFlags align = Qt::AlignCenter; + if(alignID == 0){ + align = Qt::AlignLeft; + }else if(alignID == 2){ + align = Qt::AlignRight; + } + mLabel->setAlignment(align); + updateLabel(mValue); +} + +void LabelSource::savePrefs(KConfig* inKConfig){ + TriggeredSource::savePrefs(inKConfig); + inKConfig->writeEntry(mID + "_color", mLabelSourcePrefs->colorButton->color()); + inKConfig->writeEntry(mID + "_font", mLabelSourcePrefs->fontRequester->font()); + inKConfig->writeEntry(mID + "_align", mLabel->alignment()); +} + +void LabelSource::loadPrefs(KConfig* inKConfig){ + TriggeredSource::loadPrefs(inKConfig); + QColor color = inKConfig->readColorEntry(mID + "_color"); + if(!color.isValid()) + color.setRgb(0,0,0); + mLabel->setPaletteForegroundColor(color); + mLabel->setFont(inKConfig->readFontEntry(mID + "_font")); + mLabel->setAlignment(inKConfig->readNumEntry(mID + "_align")); +} + +void LabelSource::updateLabel(const QString& inValue){ + // update the label text + QString text = (getName().isEmpty() || !showName()) ? inValue : getName() + ": " + inValue; + //kdDebug() << "updateLabel " << getName() << ", value: " << text << endl; + //if(mLabel->text() != text) + mLabel->setText(text); +} + +void LabelSource::realizeWidget(){ + Source::realizeWidget(); + // the prefs dialog is created in the addPrefs method + mLabel = new QLabel(i18n("n/a"), mParent); + mLabel->setTextFormat(Qt::PlainText); + connect(this, SIGNAL(valueUpdated(const QString&)), this, SLOT(updateLabel(const QString&))); +} diff --git a/src/sources/labelsource.h b/src/sources/labelsource.h new file mode 100644 index 0000000..1a50ae0 --- /dev/null +++ b/src/sources/labelsource.h @@ -0,0 +1,107 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef LABELSOURCE_H +#define LABELSOURCE_H + +#include "triggeredsource.h" +#include "labelsourcePrefs.h" + +class QLabel; + +/** + * LabelSource displays its value on a configurable QLabel + * @author Ken Werner + */ +class LabelSource : public TriggeredSource{ + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new DefaultSource from the given File + */ + LabelSource(QWidget* inParent); + + virtual ~LabelSource(); + + /** + * Returns the Label of this source that is displayed in the kicker + */ + QWidget* getWidget(); + + /** + * fills the prefs gui with appropriate values + */ + virtual void updatePrefsGUI(); + + /** + * realizes the widget. that means, create the QLabel. + */ + virtual void realizeWidget(); + +protected slots: + /** + * updates the label + */ + virtual void updateLabel(const QString& inValue); + + /** + * Applies the prefs + */ + virtual void applyPrefs(); + + /** + * Saves the prefs (implicit apply) + */ + virtual void savePrefs(KConfig* inKConfig); + + /** + * Loads the prefs + */ + virtual void loadPrefs(KConfig* inKConfig); + + /** + * This method enables or disables various widgets of the preferences dialog depending on isEnabled and isShownOnApplet + * This method can be extended in sub classes + */ + virtual void setPrefsWidgetsEnabled(bool isEnabled, bool isShownOnApplet); + +protected: + /** + * Allows subclasses adding their own preferences using the addPrefs method + */ + virtual void createSubPrefs(QWidget* inParent); + + /** + * Displays the content of that source + */ + QLabel* mLabel; + + /** + * the parent of the label we are going to create + */ + QWidget* mParent; + +private: + /** + * the LabelSources part of the preferences gui + */ + LabelSourcePrefs* mLabelSourcePrefs; +}; +#endif //LABELSOURCE_H diff --git a/src/sources/labelsourcePrefs.cpp b/src/sources/labelsourcePrefs.cpp new file mode 100644 index 0000000..fda5ea3 --- /dev/null +++ b/src/sources/labelsourcePrefs.cpp @@ -0,0 +1,110 @@ +#include <kdialog.h> +#include <klocale.h> +/**************************************************************************** +** Form implementation generated from reading ui file './labelsourcePrefs.ui' +** +** Created: Thu Jun 21 19:16:51 2007 +** by: The User Interface Compiler ($Id: qt/main.cpp 3.3.7 edited Aug 31 2005 $) +** +** WARNING! All changes made in this file will be lost! +****************************************************************************/ + +#include "labelsourcePrefs.h" + +#include <qvariant.h> +#include <qpushbutton.h> +#include <qframe.h> +#include <qlabel.h> +#include <kcolorbutton.h> +#include <kfontrequester.h> +#include <qcombobox.h> +#include <qlayout.h> +#include <qtooltip.h> +#include <qwhatsthis.h> + +/* + * Constructs a LabelSourcePrefs as a child of 'parent', with the + * name 'name' and widget flags set to 'f'. + */ +LabelSourcePrefs::LabelSourcePrefs( QWidget* parent, const char* name, WFlags fl ) + : QWidget( parent, name, fl ) +{ + if ( !name ) + setName( "LabelSourcePrefs" ); + LabelSourcePrefsLayout = new QVBoxLayout( this, 0, 6, "LabelSourcePrefsLayout"); + + defaultSourcePrefsFrame = new QFrame( this, "defaultSourcePrefsFrame" ); + defaultSourcePrefsFrame->setFrameShape( QFrame::NoFrame ); + defaultSourcePrefsFrame->setFrameShadow( QFrame::Plain ); + defaultSourcePrefsFrameLayout = new QVBoxLayout( defaultSourcePrefsFrame, 0, 6, "defaultSourcePrefsFrameLayout"); + + taskbartitleLabel = new QLabel( defaultSourcePrefsFrame, "taskbartitleLabel" ); + QFont taskbartitleLabel_font( taskbartitleLabel->font() ); + taskbartitleLabel_font.setBold( TRUE ); + taskbartitleLabel->setFont( taskbartitleLabel_font ); + defaultSourcePrefsFrameLayout->addWidget( taskbartitleLabel ); + + layout13 = new QHBoxLayout( 0, 0, 6, "layout13"); + + colorLabel = new QLabel( defaultSourcePrefsFrame, "colorLabel" ); + layout13->addWidget( colorLabel ); + + colorButton = new KColorButton( defaultSourcePrefsFrame, "colorButton" ); + layout13->addWidget( colorButton ); + spacer1 = new QSpacerItem( 350, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ); + layout13->addItem( spacer1 ); + defaultSourcePrefsFrameLayout->addLayout( layout13 ); + + layout17 = new QHBoxLayout( 0, 0, 6, "layout17"); + + fontLabel = new QLabel( defaultSourcePrefsFrame, "fontLabel" ); + layout17->addWidget( fontLabel ); + + fontRequester = new KFontRequester( defaultSourcePrefsFrame, "fontRequester" ); + layout17->addWidget( fontRequester ); + defaultSourcePrefsFrameLayout->addLayout( layout17 ); + + layout18 = new QHBoxLayout( 0, 0, 6, "layout18"); + + alignmentLabel = new QLabel( defaultSourcePrefsFrame, "alignmentLabel" ); + layout18->addWidget( alignmentLabel ); + + alignmentComboBox = new QComboBox( FALSE, defaultSourcePrefsFrame, "alignmentComboBox" ); + layout18->addWidget( alignmentComboBox ); + spacer2 = new QSpacerItem( 350, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ); + layout18->addItem( spacer2 ); + defaultSourcePrefsFrameLayout->addLayout( layout18 ); + LabelSourcePrefsLayout->addWidget( defaultSourcePrefsFrame ); + languageChange(); + resize( QSize(299, 135).expandedTo(minimumSizeHint()) ); + clearWState( WState_Polished ); +} + +/* + * Destroys the object and frees any allocated resources + */ +LabelSourcePrefs::~LabelSourcePrefs() +{ + // no need to delete child widgets, Qt does it all for us +} + +/* + * Sets the strings of the subwidgets using the current + * language. + */ +void LabelSourcePrefs::languageChange() +{ + setCaption( tr2i18n( "LabelSourcePrefs" ) ); + taskbartitleLabel->setText( tr2i18n( "<br>\n" +"Taskbar visual settings" ) ); + colorLabel->setText( tr2i18n( "Foreground color:" ) ); + colorButton->setText( QString::null ); + fontLabel->setText( tr2i18n( "Font:" ) ); + alignmentLabel->setText( tr2i18n( "Alignment:" ) ); + alignmentComboBox->clear(); + alignmentComboBox->insertItem( tr2i18n( "Left" ) ); + alignmentComboBox->insertItem( tr2i18n( "Center" ) ); + alignmentComboBox->insertItem( tr2i18n( "Right" ) ); +} + +#include "labelsourcePrefs.moc" diff --git a/src/sources/labelsourcePrefs.ui b/src/sources/labelsourcePrefs.ui new file mode 100644 index 0000000..c998dd4 --- /dev/null +++ b/src/sources/labelsourcePrefs.ui @@ -0,0 +1,186 @@ +<!DOCTYPE UI><UI version="3.3" stdsetdef="1"> +<class>LabelSourcePrefs</class> +<widget class="QWidget"> + <property name="name"> + <cstring>LabelSourcePrefs</cstring> + </property> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>299</width> + <height>135</height> + </rect> + </property> + <property name="caption"> + <string>LabelSourcePrefs</string> + </property> + <vbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <widget class="QFrame"> + <property name="name"> + <cstring>defaultSourcePrefsFrame</cstring> + </property> + <property name="frameShape"> + <enum>NoFrame</enum> + </property> + <property name="frameShadow"> + <enum>Plain</enum> + </property> + <vbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <widget class="QLabel"> + <property name="name"> + <cstring>taskbartitleLabel</cstring> + </property> + <property name="font"> + <font> + <bold>1</bold> + </font> + </property> + <property name="text"> + <string><br> +Taskbar visual settings</string> + </property> + </widget> + <widget class="QLayoutWidget"> + <property name="name"> + <cstring>layout13</cstring> + </property> + <hbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <widget class="QLabel"> + <property name="name"> + <cstring>colorLabel</cstring> + </property> + <property name="text"> + <string>Foreground color:</string> + </property> + </widget> + <widget class="KColorButton"> + <property name="name"> + <cstring>colorButton</cstring> + </property> + <property name="text"> + <string></string> + </property> + </widget> + <spacer> + <property name="name"> + <cstring>spacer1</cstring> + </property> + <property name="orientation"> + <enum>Horizontal</enum> + </property> + <property name="sizeType"> + <enum>Expanding</enum> + </property> + <property name="sizeHint"> + <size> + <width>350</width> + <height>20</height> + </size> + </property> + </spacer> + </hbox> + </widget> + <widget class="QLayoutWidget"> + <property name="name"> + <cstring>layout17</cstring> + </property> + <hbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <widget class="QLabel"> + <property name="name"> + <cstring>fontLabel</cstring> + </property> + <property name="text"> + <string>Font:</string> + </property> + </widget> + <widget class="KFontRequester"> + <property name="name"> + <cstring>fontRequester</cstring> + </property> + </widget> + </hbox> + </widget> + <widget class="QLayoutWidget"> + <property name="name"> + <cstring>layout18</cstring> + </property> + <hbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <widget class="QLabel"> + <property name="name"> + <cstring>alignmentLabel</cstring> + </property> + <property name="text"> + <string>Alignment:</string> + </property> + </widget> + <widget class="QComboBox"> + <item> + <property name="text"> + <string>Left</string> + </property> + </item> + <item> + <property name="text"> + <string>Center</string> + </property> + </item> + <item> + <property name="text"> + <string>Right</string> + </property> + </item> + <property name="name"> + <cstring>alignmentComboBox</cstring> + </property> + </widget> + <spacer> + <property name="name"> + <cstring>spacer2</cstring> + </property> + <property name="orientation"> + <enum>Horizontal</enum> + </property> + <property name="sizeType"> + <enum>Expanding</enum> + </property> + <property name="sizeHint"> + <size> + <width>350</width> + <height>20</height> + </size> + </property> + </spacer> + </hbox> + </widget> + </vbox> + </widget> + </vbox> +</widget> +<layoutdefaults spacing="6" margin="11"/> +<includehints> + <includehint>kcolorbutton.h</includehint> + <includehint>kfontrequester.h</includehint> +</includehints> +</UI> diff --git a/src/sources/nvidiathermalsrc.cpp b/src/sources/nvidiathermalsrc.cpp new file mode 100644 index 0000000..000ece7 --- /dev/null +++ b/src/sources/nvidiathermalsrc.cpp @@ -0,0 +1,175 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "nvidiathermalsrc.h" +#include <qstringlist.h> +#include <qregexp.h> +#include <klocale.h> +#include <kdebug.h> + +#ifndef HAVE_NVCONTROL + #include <kprocio.h> +#else + #include <qpaintdevice.h> // for the Device* pointer + + // include the NVCtrl include stuff + #include <X11/Xlib.h> + #include <fixx11h.h> // needed for Qt, to include X11 header +extern "C" { + #include <NVCtrlLib.h> +} +#endif + +#ifndef HAVE_NVCONTROL +NVidiaThermalSrc::NVidiaThermalSrc(QWidget* inParent, const QString& inID, const QString& inName): + LabelSource(inParent), mProcess(0) { +#else +NVidiaThermalSrc::NVidiaThermalSrc(QWidget* inParent, const QString& inID, const QString& inName, unsigned int attrib): + LabelSource(inParent), mAttrib(attrib) { +#endif + mID = inID; + mName = inName; + mDescription = i18n("This source is provided by the nVidia GPU card driver tools"); + + mRefreshTimer = new QTimer(this, "default refresh handler" ); + // connect the timer + connect(mRefreshTimer, SIGNAL(timeout()), this, SLOT(fetchValue())); + connect(this, SIGNAL(enabledChanged(bool, Source*)), this, SLOT(enable(bool))); +} + +NVidiaThermalSrc::~NVidiaThermalSrc(){ +#ifndef HAVE_NVCONTROL + delete mProcess; +#endif +} + +std::list<Source*>NVidiaThermalSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; +#ifndef HAVE_NVCONTROL + // see if the path contains nvidia-settings. if yes, + // execute it and look for the query type "GPUCoreTemp" + // and "GPUAmbientTemp" and create two instances for each + // of them + // start nvidia-settings, if available and wait for it to exit. + KProcIO proc; + proc << "nvidia-settings" + << "-n" // don't load config + << "-q" << "GPUCoreTemp" + << "-q" << "GPUAmbientTemp"; + if(!proc.start(KProcess::Block)) + return list; + + // now see what it printed... + QString ln; + QString pout; + while(proc.readln(ln) != -1) + pout+= ln + '\n'; + + if(pout.contains("Attribute 'GPUCoreTemp'")) + list.push_back(new NVidiaThermalSrc(inParent, "GPUCoreTemp", "NVidiaCore")); + if(pout.contains("Attribute 'GPUAmbientTemp'")) + list.push_back(new NVidiaThermalSrc(inParent, "GPUAmbientTemp", "NVidiaAmbient")); +#else + int evt_base = 0, err_base = 0, temp; + Display * dpy = QPaintDevice::x11AppDisplay(); + + // do we have the XNVCtrl extension loaded? + if(!XNVCTRLQueryExtension(dpy, &evt_base, &err_base)) + return list; + + // core temp support? + if(XNVCTRLQueryAttribute (dpy, 0, 0, + NV_CTRL_GPU_CORE_TEMPERATURE, &temp) + ) + list.push_back(new NVidiaThermalSrc(inParent, "GPUCoreTemp", "NVidiaCore", + NV_CTRL_GPU_CORE_TEMPERATURE)); + + // ambient temp support? + if(XNVCTRLQueryAttribute (dpy, 0, 0, + NV_CTRL_AMBIENT_TEMPERATURE, &temp) + ) + list.push_back(new NVidiaThermalSrc(inParent, "GPUAmbientTemp", "NVidiaAmbient", + NV_CTRL_AMBIENT_TEMPERATURE)); +#endif + return list; +} + +void NVidiaThermalSrc::enable(bool inEnable){ + if(inEnable && !mRefreshTimer->isActive()){ // start the timer + fetchValue(); + mRefreshTimer->start(3000); + }else if(!inEnable && mRefreshTimer->isActive()) // stops the timer + mRefreshTimer->stop(); +} + + +void NVidiaThermalSrc::evaluateStdout(){ +#ifndef HAVE_NVCONTROL + QString val = i18n("n/a"); + + // now see what it printed... + QString ln; + QString pout; + while(mProcess->readln(ln) != -1) + pout+= ln + '\n'; + QRegExp regexp("Attribute\\s'" + mID + "'.*(\\d+)\\."); + if(regexp.search(pout) != -1) + val = formatTemperature(regexp.cap(1)); + mValue = val; + emit valueUpdated(mValue); // calls updateLabel(mValue) of LabelSource + + // delete the object, to be ready for a new run + delete mProcess; + mProcess = 0; +#endif +} + +#ifndef HAVE_NVCONTROL +void NVidiaThermalSrc::createProcess() { + mProcess = new KProcIO; + connect(mProcess, SIGNAL(processExited(KProcess*)), this, SLOT(evaluateStdout())); + *mProcess << "nvidia-settings" << "-n" + << "-q" << mID; +} +#endif + +QString NVidiaThermalSrc::fetchValue(){ +#ifndef HAVE_NVCONTROL + if(!mProcess) { + createProcess(); + if(!mProcess->start()) { + mValue = "n/a"; + delete mProcess; + mProcess = 0; + } + } + return getValue(); +#else + int temp; + Display * dpy = QPaintDevice::x11AppDisplay(); + if(XNVCTRLQueryAttribute (dpy, 0, 0, mAttrib, &temp)) + mValue = formatTemperature(QString::number(temp)); + else + mValue = "n/a"; + emit valueUpdated(mValue); // calls updateLabel(mValue) of LabelSource + return mValue; +#endif +} + diff --git a/src/sources/nvidiathermalsrc.h b/src/sources/nvidiathermalsrc.h new file mode 100644 index 0000000..db2d0ab --- /dev/null +++ b/src/sources/nvidiathermalsrc.h @@ -0,0 +1,86 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef NVIDIATHERMALSRC_H +#define NVIDIATHERMALSRC_H + +#include <config.h> +#include "labelsource.h" +#include <list> +#include <qtimer.h> + +class KProcIO; +/** + * NVidiaThermalSrc calls nvidia-settings to fetch the thermal values of the GPU + * @author Ken Werner + */ +class NVidiaThermalSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File + */ +#ifndef HAVE_NVCONTROL + NVidiaThermalSrc(QWidget* inParent, const QString& inID, const QString& inName); +#else + NVidiaThermalSrc(QWidget* inParent, const QString& inID, const QString& inName, unsigned int attrib); +#endif + virtual ~NVidiaThermalSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * runs the nvidia-settings command + */ + virtual QString fetchValue(); + +protected slots: + /** + * enables (starts the timer) or disables (stops the timer) + * this method is called if mSource gets enabled or disabled + */ + void enable(bool inEnable); + +private: +#ifndef HAVE_NVCONTROL + /** + * create mProcess and connects its exited signal to + * the evaluateStdout slot. + */ + void createProcess(); + KProcIO* mProcess; +#else + unsigned int mAttrib; +#endif + /** + * The refresh timer + */ + QTimer* mRefreshTimer; + +private slots: + /** + * updates the label + */ + virtual void evaluateStdout(); +}; +#endif //NVIDIATHERMALSRC_H diff --git a/src/sources/omnibookthermalsrc.cpp b/src/sources/omnibookthermalsrc.cpp new file mode 100644 index 0000000..a8a8203 --- /dev/null +++ b/src/sources/omnibookthermalsrc.cpp @@ -0,0 +1,58 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "omnibookthermalsrc.h" +#include <qtextstream.h> +#include <qfile.h> +#include <klocale.h> + +OmnibookThermalSrc::OmnibookThermalSrc(QWidget* inParent, const QFile& inSourceFile): + LabelSource(inParent), + mSourceFile(inSourceFile.name()), + mTrigger(this){ + mID = "CPU"; + mName = mID; + mDescription = i18n("This source is provided by the Omnibook Configuration Tools & Patches."); +} + +OmnibookThermalSrc::~OmnibookThermalSrc(){ +} + +std::list<Source*>OmnibookThermalSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + QFile omiFile( "/proc/omnibook/temperature" ); + if(omiFile.open(IO_ReadOnly)){ + list.push_back(new OmnibookThermalSrc(inParent, omiFile)); + } + return list; +} + +QString OmnibookThermalSrc::fetchValue(){ + QString s = "n/a"; + if(mSourceFile.open(IO_ReadOnly)){ + QTextStream textStream( &mSourceFile ); + s = textStream.readLine(); + mSourceFile.close(); + s = s.section(':',-1,-1, QString::SectionSkipEmpty).stripWhiteSpace(); + s = formatTemperature(s.left(s.length()-1)); + } + return s; +} + diff --git a/src/sources/omnibookthermalsrc.h b/src/sources/omnibookthermalsrc.h new file mode 100644 index 0000000..e4133ce --- /dev/null +++ b/src/sources/omnibookthermalsrc.h @@ -0,0 +1,64 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef OMNIBOOKTHERMALSRC_H +#define OMNIBOOKTHERMALSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * OmnibookThermalSrc reads temperature values from /proc/omnibook/temperature + * which is provided by the omnibook kernel module. + */ +class OmnibookThermalSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File + */ + OmnibookThermalSrc(QWidget* inParent, const QFile& inSourceFile); + virtual ~OmnibookThermalSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //OMNIBOOKTHERMALSRC_H + diff --git a/src/sources/source.cpp b/src/sources/source.cpp new file mode 100644 index 0000000..605537f --- /dev/null +++ b/src/sources/source.cpp @@ -0,0 +1,222 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "source.h" +#include <math.h> +#include <qlayout.h> +#include <qlineedit.h> +#include <qcheckbox.h> +#include <qlabel.h> +#include <klocale.h> +#include <kglobal.h> + +//#include "kdebug.h" + +Source::Source(QWidget* inParent): + mID(""), + mPosition(0), + mName(""), + mDescription(""), + mEnabled(true), + mMaybeEnabled(true), + mShowOnApplet(true), + mMaybeShowOnApplet(true), + mShowName(true), + mToolTipEnabled(true), + mSourcePrefs(0) { +} + +Source::~Source(){ +} + +const QString& Source::getID() const{ + return mID; +} + + +int Source::getPosition() const{ + return mPosition; +} + +void Source::setPosition(int inPosition, KConfig* inKConfig){ + mPosition = inPosition; + inKConfig->writeEntry(mID + "_position", mPosition); +} + +const QString& Source::getName() const{ + return mName; +} + +const QString& Source::getDescription() const{ + return mDescription; +} + +bool Source::isMetric() const{ + return mIsMetric; +} + +bool Source::isEnabled() const{ + return mEnabled; +} + +bool Source::showOnApplet() const{ + return mShowOnApplet; +} + +bool Source::showName() const{ + return mShowName; +} + +bool Source::isToolTipEnabled() const{ + return mToolTipEnabled; +} + +void Source::setMaybeEnabled(bool inMaybeEnabled){ + if(inMaybeEnabled != mMaybeEnabled){ + mMaybeEnabled = inMaybeEnabled; + // disable/enable some widgets if source is disabled/enabled + setPrefsWidgetsEnabled(mMaybeEnabled, mSourcePrefs->taskbarCheckBox->isChecked()); + } +} + +QWidget* Source::createPrefs(QWidget* inParent){ + if(!mSourcePrefs){ + mSourcePrefs = new SourcePrefs(inParent, "sourceprefsui"); + + // disable nameCheckBox if taskbarCheckBox is disabled + connect(mSourcePrefs->taskbarCheckBox, SIGNAL(toggled(bool)), mSourcePrefs->nameCheckBox, SLOT(setEnabled(bool))); + + // add prefs widgets from sub classes + createSubPrefs(mSourcePrefs); + + // add bottom vspacer + mSourcePrefs->layout()->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding) ); + + updatePrefsGUI(); // fill the prefs gui + } + return mSourcePrefs; +} + +SourcePrefs* Source::getPrefs(){ + return mSourcePrefs; +} + +void Source::updatePrefsGUI(){ + // set values + mSourcePrefs->nameLineEdit->setText(mName); + mSourcePrefs->descriptionLabel->setText(mDescription); + mSourcePrefs->taskbarCheckBox->setChecked(mShowOnApplet); + mSourcePrefs->nameCheckBox->setChecked(mShowName); + mSourcePrefs->tooltipCheckBox->setChecked(mToolTipEnabled); + + // disable/enable some widgets if source is disabled/enabled + setPrefsWidgetsEnabled(mEnabled, mShowOnApplet); +} + +void Source::setPrefsWidgetsEnabled(bool isEnabled, bool isShownOnApplet){ + mSourcePrefs->nameLabel->setEnabled(isEnabled); + mSourcePrefs->nameLineEdit->setEnabled(isEnabled); + mSourcePrefs->taskbarCheckBox->setEnabled(isEnabled); + mSourcePrefs->nameCheckBox->setEnabled(isEnabled && isShownOnApplet); + mSourcePrefs->tooltipCheckBox->setEnabled(isEnabled); +} + +void Source::addPrefs(QWidget* inParent){ + if(inParent != NULL) + mSourcePrefs->layout()->add(inParent); +} + +void Source::applyPrefs(){ + mMaybeShowOnApplet = mSourcePrefs->taskbarCheckBox->isChecked(); + mShowName = mSourcePrefs->nameCheckBox->isChecked(); + mName = mSourcePrefs->nameLineEdit->text(); + mToolTipEnabled = mSourcePrefs->tooltipCheckBox->isChecked(); + + //kdDebug() << "Source::applyPrefs() mEnabled: " << mEnabled << ", mMaybeEnabled: " << mMaybeEnabled << endl; + if(mEnabled != mMaybeEnabled){ + mEnabled = mMaybeEnabled; + //kdDebug() << "Source::applyPrefs() emit enabledChanged: " << mEnabled << endl; + emit enabledChanged(mEnabled, this); + // force hide/show on kicker. if the user just + // disabled/enabled the source, we want to show / hide + // the source too, also if the "show on kicker" property + // did not changed. so, force this here. + mShowOnApplet = !mMaybeShowOnApplet; + } + + if(!mEnabled) + emit displaySource(false, this); + else if(mMaybeShowOnApplet != mShowOnApplet) { + emit displaySource(mMaybeShowOnApplet, this); + } + mShowOnApplet = mMaybeShowOnApplet; +} + +void Source::savePrefs(KConfig* inKConfig){ + inKConfig->writeEntry(mID + "_position", mPosition); + inKConfig->writeEntry(mID + "_enabled", mEnabled); + inKConfig->writeEntry(mID + "_showOnApplet", mShowOnApplet); + inKConfig->writeEntry(mID + "_showName", mShowName); + inKConfig->writeEntry(mID + "_name", mName); + inKConfig->writeEntry(mID + "_toolTipEnabled", mToolTipEnabled); +} + +void Source::loadPrefs(KConfig* inKConfig){ + mPosition = inKConfig->readNumEntry(mID + "_position", mPosition); + mEnabled = inKConfig->readBoolEntry(mID + "_enabled", mEnabled); + mMaybeEnabled = mEnabled; + mShowOnApplet = inKConfig->readBoolEntry(mID + "_showOnApplet", mShowOnApplet); + mMaybeShowOnApplet = mShowOnApplet; + mShowName = inKConfig->readBoolEntry(mID + "_showName", mShowName); + mName = inKConfig->readEntry(mID + "_name", mName); + mToolTipEnabled = inKConfig->readBoolEntry(mID + "_toolTipEnabled", mToolTipEnabled); + + // initializing + // this signal is usually catched by the ThreadedTrigger who enables or disables the fetch loop + emit enabledChanged(mEnabled, this); +} + +// utility methods +QString Source::formatTemperature(const QString& temp) const { + if(mIsMetric) { + return temp + QString::fromUtf8(" °C"); + } else { + return QString::number(celsiusToFahrenheit(temp.toInt())).append(QString::fromUtf8(" °F")); + } +} + +QString Source::KHzinHumanReadable( uint value ) const{ + if( value >= 1000000 ) + return QString::number( round(value/1000000.0, 1) ) + " GHz"; + else if( value >= 1000 ) + return QString::number( round(value/1000.0, -1) ) + " MHz"; + else + return QString::number( value ) + " KHz"; +} +double Source::round(double inValue, int inDigits) const{ + return floor(inValue * pow( 10, inDigits) + 0.5) * pow(10, -inDigits); +} +int Source::celsiusToFahrenheit(int inCelsius) const{ + return qRound(1.8 * inCelsius + 32); +} + +void Source::realizeWidget(){ + mIsMetric = KGlobal::locale()->measureSystem() == KLocale::Metric; +} diff --git a/src/sources/source.h b/src/sources/source.h new file mode 100644 index 0000000..6f72666 --- /dev/null +++ b/src/sources/source.h @@ -0,0 +1,249 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef SOURCE_H +#define SOURCE_H +#include <qstring.h> + +#include "sourceprefs.h" +#include <kconfig.h> + +// Forward Declarationss +class QVBoxLayout; + +/** + * This abstract Source is the super class of all sources and provides just the basics. + * @author Ken Werner + */ +class Source : public QObject { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source + */ + Source(QWidget* inParent); + virtual ~Source(); + + /** + * Returns the internal Name of this source + */ + const QString& getID() const; + + /** + * Returns the position of this source in the layout + */ + int getPosition() const; + + /** + * Sets the position of this source in the layout + */ + void setPosition(int inPosition, KConfig* inKConfig); + + /** + * Returns the Name of this source + */ + const QString& getName() const; + + /** + * Returns the Description of this source + */ + const QString& getDescription() const; + + /** + * Returns true if this source is currently enabled otherwise false + */ + bool isEnabled() const; + + /** + * Returns true if this source is shown on the kicker applet (taskbar) otherwise false + */ + bool showOnApplet() const; + + /** + * Returns true if this source shows it's name in kicker otherwise false + */ + bool showName() const; + + /** + * Returns true if this source is currently enabled otherwise false + */ + bool isToolTipEnabled() const; + + /** + * Returns the widget of this source that is displayed in the kicker + */ + virtual QWidget* getWidget() = 0; + + /** + * Returns the formatted value of this source + */ + virtual QString getValue() const = 0; + + /** + * Creates the preference panel of this source and calls createSubPrefs + */ + virtual QWidget* createPrefs(QWidget* inParent); + + /** + * returnes the preference panel of this source + */ + virtual SourcePrefs* getPrefs(); + + /** + * fills the prefs gui with appropriate values + */ + virtual void updatePrefsGUI(); + + /** + * realizes the event. this function will be called + * by the applet to tell the Source to actually create + * its widget. it will always be called from the GUI thread. + */ + virtual void realizeWidget() = 0; + +public slots: + /** + * Might enable or disable the source + * Source is only enabled/disabled if applyPrefs is called afterwards + */ + virtual void setMaybeEnabled(bool inMaybeEnabled); + + /** + * Applies the preferences + */ + virtual void applyPrefs(); + + /** + * Saves the preferences (implicit apply) + */ + virtual void savePrefs(KConfig* inKConfig); + + /** + * Loads the preferences + */ + virtual void loadPrefs(KConfig* inKConfig); + +signals: + /** + * This signal is emitted whenever the enabled flag (mEnabled) of this source has changed + */ + void enabledChanged(bool inEnabled, Source* inSource); // needed by kima.cpp to add sources to its layout + /** + * This signal is emitted whenever this Source should be added or removed from the display (layout) of the kicker applet + */ + void displaySource(bool inDisplay, Source* inSource); + +protected: + /** + * This method can be overridden in sub classes to add specific the preference panels + */ + virtual void addPrefs(QWidget* inParent); + + /** + * Allows subclasses adding their own preferences using the addPrefs method + */ + virtual void createSubPrefs(QWidget* inParent) = 0; + + /** + * This method enables or disables various widgets of the preferences dialog depending on isEnabled and isShownOnApplet + * This method can be extended in sub classes + */ + virtual void setPrefsWidgetsEnabled(bool isEnabled, bool isShownOnApplet); + + /** + * returns true if the measure system is set metric, false if imperial + */ + bool isMetric() const; + + /** + * The ID of the source + * must be unique among the sources + */ + QString mID; + /** + * The Position of the source in the layout + */ + int mPosition; + /** + * The name of that source showed in the kicker + */ + QString mName; + /** + * The description of that source + */ + QString mDescription; + /** + * Indicates whether that source is enabled (showed on the kicker) or not + */ + bool mEnabled; + /** + * stores the SourcListItem checkbox value + * and is updated through SourceListItem + */ + bool mMaybeEnabled; + /** + * Indicates whether to show that source on the kicker applet (taskbar) or not + * mEnabled controls whether this source is displayed or not, see mIsDisplayed + */ + bool mShowOnApplet; + /** + * Indicates whether that source is shown on the kicker applet (taskbar) or not. this is temporary and stores only what the user configured in the preferences dialog. it is applied to mShowOnApplet in applyPrefs + */ + bool mMaybeShowOnApplet; + /** + * Indicates whether to show the name in kicker or not + */ + bool mShowName; + /** + * Indicates whether that source is showed on the tooltip or not + */ + bool mToolTipEnabled; + + // utility methods + /** + * formats the given temperature into a string which has + * a degree sign / fahrenheit sign depending on the locale used. + */ + QString formatTemperature(const QString& temp) const; + /** + * Translates a given frequency in KHz to a human readable string + */ + QString KHzinHumanReadable(uint value) const; + /** + * Translates degree Celsius to degree Fahrenheit + */ + int celsiusToFahrenheit(int inCelsius) const; + /** + * Returns inValue rounded to inDigits + */ + double round(double inValue, int inDigits=0) const; + + /** + * The preference panel of that source + */ + SourcePrefs* mSourcePrefs; +private: + + /** + * indicates whether the measure system is set metric or imperial + */ + bool mIsMetric; +}; +#endif //SOURCE_H diff --git a/src/sources/sourceprefs.cpp b/src/sources/sourceprefs.cpp new file mode 100644 index 0000000..cabce8b --- /dev/null +++ b/src/sources/sourceprefs.cpp @@ -0,0 +1,88 @@ +#include <kdialog.h> +#include <klocale.h> +/**************************************************************************** +** Form implementation generated from reading ui file './sourceprefs.ui' +** +** Created: Thu Jun 21 19:16:51 2007 +** by: The User Interface Compiler ($Id: qt/main.cpp 3.3.7 edited Aug 31 2005 $) +** +** WARNING! All changes made in this file will be lost! +****************************************************************************/ + +#include "sourceprefs.h" + +#include <qvariant.h> +#include <qpushbutton.h> +#include <qlabel.h> +#include <qlineedit.h> +#include <qcheckbox.h> +#include <qlayout.h> +#include <qtooltip.h> +#include <qwhatsthis.h> + +/* + * Constructs a SourcePrefs as a child of 'parent', with the + * name 'name' and widget flags set to 'f'. + */ +SourcePrefs::SourcePrefs( QWidget* parent, const char* name, WFlags fl ) + : QWidget( parent, name, fl ) +{ + if ( !name ) + setName( "SourcePrefs" ); + SourcePrefsLayout = new QVBoxLayout( this, 0, 6, "SourcePrefsLayout"); + + descriptionLabel = new QLabel( this, "descriptionLabel" ); + SourcePrefsLayout->addWidget( descriptionLabel ); + + hBoxLayout = new QHBoxLayout( 0, 0, 6, "hBoxLayout"); + + nameLabel = new QLabel( this, "nameLabel" ); + hBoxLayout->addWidget( nameLabel ); + + nameLineEdit = new QLineEdit( this, "nameLineEdit" ); + hBoxLayout->addWidget( nameLineEdit ); + spacer8 = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ); + hBoxLayout->addItem( spacer8 ); + SourcePrefsLayout->addLayout( hBoxLayout ); + + taskbarCheckBox = new QCheckBox( this, "taskbarCheckBox" ); + SourcePrefsLayout->addWidget( taskbarCheckBox ); + + hBoxLayout2 = new QHBoxLayout( 0, 0, 6, "hBoxLayout2"); + spacer8_2 = new QSpacerItem( 20, 20, QSizePolicy::Fixed, QSizePolicy::Minimum ); + hBoxLayout2->addItem( spacer8_2 ); + + nameCheckBox = new QCheckBox( this, "nameCheckBox" ); + hBoxLayout2->addWidget( nameCheckBox ); + SourcePrefsLayout->addLayout( hBoxLayout2 ); + + tooltipCheckBox = new QCheckBox( this, "tooltipCheckBox" ); + SourcePrefsLayout->addWidget( tooltipCheckBox ); + languageChange(); + resize( QSize(203, 127).expandedTo(minimumSizeHint()) ); + clearWState( WState_Polished ); +} + +/* + * Destroys the object and frees any allocated resources + */ +SourcePrefs::~SourcePrefs() +{ + // no need to delete child widgets, Qt does it all for us +} + +/* + * Sets the strings of the subwidgets using the current + * language. + */ +void SourcePrefs::languageChange() +{ + setCaption( tr2i18n( "SourcePrefs" ) ); + descriptionLabel->setText( tr2i18n( "description" ) ); + nameLabel->setText( tr2i18n( "Name:" ) ); + taskbarCheckBox->setText( tr2i18n( "Show this source in Taskbar" ) ); + nameCheckBox->setText( tr2i18n( "Show name in Taskbar" ) ); + tooltipCheckBox->setText( tr2i18n( "Show this source in ToolTip" ) ); +} + +#include "sourceprefs.moc" diff --git a/src/sources/sourceprefs.ui b/src/sources/sourceprefs.ui new file mode 100644 index 0000000..6d42984 --- /dev/null +++ b/src/sources/sourceprefs.ui @@ -0,0 +1,127 @@ +<!DOCTYPE UI><UI version="3.3" stdsetdef="1"> +<class>SourcePrefs</class> +<widget class="QWidget"> + <property name="name"> + <cstring>SourcePrefs</cstring> + </property> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>203</width> + <height>127</height> + </rect> + </property> + <property name="caption"> + <string>SourcePrefs</string> + </property> + <vbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <property name="margin"> + <number>0</number> + </property> + <widget class="QLabel"> + <property name="name"> + <cstring>descriptionLabel</cstring> + </property> + <property name="text"> + <string>description</string> + </property> + </widget> + <widget class="QLayoutWidget"> + <property name="name"> + <cstring>hBoxLayout</cstring> + </property> + <hbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <widget class="QLabel"> + <property name="name"> + <cstring>nameLabel</cstring> + </property> + <property name="text"> + <string>Name:</string> + </property> + </widget> + <widget class="QLineEdit"> + <property name="name"> + <cstring>nameLineEdit</cstring> + </property> + </widget> + <spacer> + <property name="name"> + <cstring>spacer8</cstring> + </property> + <property name="orientation"> + <enum>Horizontal</enum> + </property> + <property name="sizeType"> + <enum>Expanding</enum> + </property> + <property name="sizeHint"> + <size> + <width>20</width> + <height>20</height> + </size> + </property> + </spacer> + </hbox> + </widget> + <widget class="QCheckBox"> + <property name="name"> + <cstring>taskbarCheckBox</cstring> + </property> + <property name="text"> + <string>Show this source in Taskbar</string> + </property> + </widget> + <widget class="QLayoutWidget"> + <property name="name"> + <cstring>hBoxLayout2</cstring> + </property> + <hbox> + <property name="name"> + <cstring>unnamed</cstring> + </property> + <spacer> + <property name="name"> + <cstring>spacer8</cstring> + </property> + <property name="orientation"> + <enum>Horizontal</enum> + </property> + <property name="sizeType"> + <enum>Fixed</enum> + </property> + <property name="sizeHint"> + <size> + <width>20</width> + <height>20</height> + </size> + </property> + </spacer> + <widget class="QCheckBox"> + <property name="name"> + <cstring>nameCheckBox</cstring> + </property> + <property name="text"> + <string>Show name in Taskbar</string> + </property> + </widget> + </hbox> + </widget> + <widget class="QCheckBox"> + <property name="name"> + <cstring>tooltipCheckBox</cstring> + </property> + <property name="text"> + <string>Show this source in ToolTip</string> + </property> + </widget> + </vbox> +</widget> +<layoutdefaults spacing="6" margin="11"/> +</UI> diff --git a/src/sources/sysfreqsrc.cpp b/src/sources/sysfreqsrc.cpp new file mode 100644 index 0000000..239f20b --- /dev/null +++ b/src/sources/sysfreqsrc.cpp @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "sysfreqsrc.h" +#include <qtextstream.h> +#include <qfile.h> +#include <qdir.h> +#include <klocale.h> + +SysFreqSrc::SysFreqSrc(QWidget* inParent, const QFile& inSourceFile): + LabelSource(inParent), + mSourceFile(inSourceFile.name()), + mTrigger(this){ + mID = "cpufreq" + mSourceFile.name().section( '/', -3, -3, QString::SectionSkipEmpty ).remove("cpu"); + mName = mID; + mDescription = i18n("This source is provided by the Linux kernel cpufreq subsystem."); +} + +SysFreqSrc::~SysFreqSrc(){ +} + +std::list<Source*>SysFreqSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + + QDir cpuDir( "/sys/devices/system/cpu" ); + if ( cpuDir.exists() ){ + cpuDir.setFilter( QDir::Dirs | QDir::NoSymLinks ); + cpuDir.setSorting( QDir::Name); + for ( unsigned int i = 0; i < cpuDir.count(); i++ ){ + if(cpuDir[i].startsWith("cpu")){ + QFile sysFreqFile(cpuDir.canonicalPath() + "/" + cpuDir[i] + "/cpufreq/scaling_cur_freq"); + if(sysFreqFile.open(IO_ReadOnly)) + list.push_back(new SysFreqSrc(inParent, sysFreqFile)); + } + } + } + return list; +} + +QString SysFreqSrc::fetchValue(){ + QString s = "n/a"; + if(mSourceFile.open(IO_ReadOnly)){ + QTextStream textStream(&mSourceFile); + s = KHzinHumanReadable(textStream.readLine().toUInt()); + mSourceFile.close(); + } + return s; +} + diff --git a/src/sources/sysfreqsrc.h b/src/sources/sysfreqsrc.h new file mode 100644 index 0000000..c2a9a60 --- /dev/null +++ b/src/sources/sysfreqsrc.h @@ -0,0 +1,63 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef SYSFREQSRC_H +#define SYSFREQSRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * SysFreqSrc reads frequency values from /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq + */ +class SysFreqSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File + */ + SysFreqSrc(QWidget* inParent, const QFile& inSourceFile); + virtual ~SysFreqSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + +private: + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //SYSFREQSRC_H + diff --git a/src/sources/threadedtrigger.cpp b/src/sources/threadedtrigger.cpp new file mode 100644 index 0000000..11f07b5 --- /dev/null +++ b/src/sources/threadedtrigger.cpp @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "threadedtrigger.h" +#include <qapplication.h> + +//#include "kdebug.h" + +ThreadedTrigger::ThreadedTrigger(TriggeredSource* inSource, unsigned long inRefreshSleep): + mSource(inSource), + mRefreshSleep(inRefreshSleep), + mRunning(false) + { + connect(mSource, SIGNAL(enabledChanged(bool, Source*)), this, SLOT(enable(bool))); +} + +ThreadedTrigger::~ThreadedTrigger(){ + enable(false); +} + +void ThreadedTrigger::enable(bool inEnable){ + if(inEnable && !mRunning){ // start this thread (calls run()) + //kdDebug() << "start thread " << mSource->getName() << endl; + // start the thread + mRunning = true; + this->start(QThread::LowPriority); + }else if(!inEnable && mRunning){ // stops the thread + //kdDebug() << "stop thread " << mSource->getName() << endl; + mRunning = false; + mWaitMutex.lock(); + mWaitCond.wakeOne(); + mWaitMutex.unlock(); + this->wait(); + } +} + +void ThreadedTrigger::run(){ + mWaitMutex.lock(); + while( mRunning ) { + QString text = mSource->fetchValue(); + UpdateEvent* ue = new UpdateEvent(text); // Qt will delete the ue when done + QApplication::postEvent(mSource, ue); // send the event to the TriggeredSource + if(mWaitCond.wait(&mWaitMutex, mRefreshSleep)) + break; // we were woken up + } + // if we are here, the mutex must be locked: + // 1. QWaitCondition::wait locks it when it returns + // 2. mWaitMutex is locked when we enter the loop + mWaitMutex.unlock(); // unlock it again +} diff --git a/src/sources/threadedtrigger.h b/src/sources/threadedtrigger.h new file mode 100644 index 0000000..8d84d5f --- /dev/null +++ b/src/sources/threadedtrigger.h @@ -0,0 +1,89 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef THREADEDTRIGGER_H +#define THREADEDTRIGGER_H + +#include "source.h" +#include "updateevent.h" +#include "triggeredsource.h" +#include <qwaitcondition.h> +#include <qmutex.h> +#include <qthread.h> +#include <qevent.h> +#include <qobject.h> + +/** + * ThreadedTrigger calls the fetchValue method of a source asynchronous using a thread + * @author Ken Werner + */ +class ThreadedTrigger : public QObject, QThread{ + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new ThreadedTrigger + */ + ThreadedTrigger(TriggeredSource* inSource, unsigned long inRefreshSleep = 1000); + + virtual ~ThreadedTrigger(); + +protected: + /** + * fetches tcalls the fetchValue method periodically + * and sends an UpdateEvent to the source + */ + virtual void run(); + +protected slots: + /** + * enables (starts the thread) or disables (stops the thread) + * this method is called if mSource gets enabled or disabled + */ + void enable(bool inEnable); + +private: + /** + * pointer to the TriggeredSource + **/ + TriggeredSource* mSource; + + /** + * time in milli seconds between two value fetches + */ + unsigned long mRefreshSleep; + + /** + * indicates whether this thread runs + */ + bool mRunning; + + /* + * this is the condition, on which this thread sleeps + * mRefreshSleep milliseconds. + */ + QWaitCondition mWaitCond; + + /* + * this mutex ensures that the thread always sleeps on the + * condition, if we signal it. + */ + QMutex mWaitMutex; +}; +#endif //THREADEDTRIGGER_H diff --git a/src/sources/triggeredsource.cpp b/src/sources/triggeredsource.cpp new file mode 100644 index 0000000..88dde5e --- /dev/null +++ b/src/sources/triggeredsource.cpp @@ -0,0 +1,48 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "triggeredsource.h" +#include "updateevent.h" +#include <qapplication.h> + +//#include "kdebug.h" + +TriggeredSource::TriggeredSource(QWidget* inParent): + Source(inParent){ +} + +TriggeredSource::~TriggeredSource(){ +} + +void TriggeredSource::customEvent(QCustomEvent* event){ + if(event->type() == UpdateEvent::TYPE ){ + UpdateEvent* ue = static_cast<UpdateEvent*>(event); + // update mValue + mValue = ue->getMessage(); + //kdDebug() << "emit valueUpdated(mValue); value: " << mValue << endl; + emit valueUpdated(mValue); + }else{ + Source::customEvent(event); + } +} + +QString TriggeredSource::getValue() const{ + return mValue; +} diff --git a/src/sources/triggeredsource.h b/src/sources/triggeredsource.h new file mode 100644 index 0000000..9beba25 --- /dev/null +++ b/src/sources/triggeredsource.h @@ -0,0 +1,72 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef TRIGGEREDSOURCE_H +#define TRIGGEREDSOURCE_H + +#include "source.h" +#include <qevent.h> + +/** + * TriggeredSource provides fetchValue and + * can be triggered asynchronously by ThreadedTrigger + * @author Ken Werner + */ +class TriggeredSource : public Source{ + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new TriggeredSource + */ + TriggeredSource(QWidget* inParent); + + virtual ~TriggeredSource(); + +public slots: + + /** + * Returns the formatted value of this source + */ + virtual QString getValue() const; + + /** + * Fetches and returns the value of this source + * This method might be called from a thread so don't call it directly + */ + virtual QString fetchValue() = 0; + +signals: + /** + * This signal is emitted whenever the value of this source (mValue) is updated + */ + void valueUpdated(const QString& inValue); + +protected: + /** + * consumes the UpdateEvent + */ + virtual void customEvent(QCustomEvent* event); + + /** + * the buffered value of this source + */ + QString mValue; +}; +#endif //TRIGGEREDSOURCE_H diff --git a/src/sources/updateevent.cpp b/src/sources/updateevent.cpp new file mode 100644 index 0000000..f16f6b5 --- /dev/null +++ b/src/sources/updateevent.cpp @@ -0,0 +1,29 @@ +/*************************************************************************** + * Copyright (C) 2007 by Johannes Schaub * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "updateevent.h" + +UpdateEvent::UpdateEvent(const QString& inMessage): + QCustomEvent(UpdateEvent::TYPE), + mMessage(inMessage){ +} +const QString& UpdateEvent::getMessage() const{ + return mMessage; +} diff --git a/src/sources/updateevent.h b/src/sources/updateevent.h new file mode 100644 index 0000000..ae6ef24 --- /dev/null +++ b/src/sources/updateevent.h @@ -0,0 +1,38 @@ +/*************************************************************************** + * Copyright (C) 2007 by Johannes Schaub * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef UPDATEEVENT_H +#define UPDATEEVENT_H + +#include <qevent.h> + +/** + * UpdateEvent is a custom event to update the QObject of the ThreadedTrigger periodically + */ +class UpdateEvent: public QCustomEvent{ +public: + UpdateEvent(const QString& inMessage); + static const int TYPE = QEvent::User + 1; + const QString& getMessage() const; +private: + QString mMessage; +}; + +#endif //UPDATEEVENT_H diff --git a/src/sources/uptimesrc.cpp b/src/sources/uptimesrc.cpp new file mode 100644 index 0000000..fd392f5 --- /dev/null +++ b/src/sources/uptimesrc.cpp @@ -0,0 +1,70 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "uptimesrc.h" +#include <qtextstream.h> +#include <klocale.h> + +UptimeSrc::UptimeSrc(QWidget* inParent, const QFile& inSourceFile): + LabelSource(inParent), + mSourceFile(inSourceFile.name()), + mTrigger(this, 30000){ // 30 seconds + mEnabled = false; // disable this source by default to to save kicker space + mID = "Uptime"; + mName = mID; + mDescription = i18n("This source is provided by /proc/uptime."); +} + +UptimeSrc::~UptimeSrc(){ +} + +std::list<Source*>UptimeSrc::createInstances(QWidget* inParent){ + std::list<Source*> list; + QFile uptimeFile("/proc/uptime" ); + if(uptimeFile.open(IO_ReadOnly)) + list.push_back(new UptimeSrc(inParent, uptimeFile)); + return list; +} + +QString UptimeSrc::fetchValue(){ + QString s = "n/a"; + int secs; + if(mSourceFile.open(IO_ReadOnly)) { + QTextStream stream( &mSourceFile ); + stream >> secs; + + //QString seconds = QString::number(secs % 60).rightJustify(2, '0'); + QString minutes = QString::number(secs / 60 % 60).rightJustify(2, '0'); + QString hours = QString::number(secs / 3600 % 24).rightJustify(2, '0'); + QString days = QString::number(secs / 86400); + if(days != "0") + s = mTimeFormatLong.arg(days).arg(hours).arg(minutes); + else + s = mTimeFormatShort.arg(hours).arg(minutes); + mSourceFile.close(); + } + return s; +} + +void UptimeSrc::realizeWidget() { + LabelSource::realizeWidget(); + mTimeFormatLong = i18n("%1d %2:%3"); + mTimeFormatShort = i18n("%1:%2"); +} diff --git a/src/sources/uptimesrc.h b/src/sources/uptimesrc.h new file mode 100644 index 0000000..e92a521 --- /dev/null +++ b/src/sources/uptimesrc.h @@ -0,0 +1,74 @@ +/*************************************************************************** + * Copyright (C) 2007 by Ken Werner * + * [email protected] * + * * + * 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. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef UPTIMESRC_H +#define UPTIMESRC_H +#include "labelsource.h" +#include "threadedtrigger.h" +#include <qfile.h> +#include <list> + +/** + * UptimeSrc reads fan values from /proc/acpi/ibm/thermal + * which is provided by the ibm-acpi kernel module. + */ +class UptimeSrc : public LabelSource { + Q_OBJECT //macro which activates signals and slots (moc) +public: + /** + * Creates a new Source from the given File and index + */ + UptimeSrc(QWidget* inParent, const QFile& inSourceFile); + virtual ~UptimeSrc(); + + /** + * Creates instances of that Source (and returns pointers to them) + */ + static std::list<Source*> createInstances(QWidget* inParent); + + /** + * Fetches and returns the value of this source + * This method is called from a thread so don't call it directly + */ + virtual QString fetchValue(); + + + /** + * get the format for the akku time left from i18n. + */ + virtual void realizeWidget(); +private: + /** + * The file from which the value is read + */ + QFile mSourceFile; + + /** + * the format of the time display. short and long version + */ + QString mTimeFormatShort; + QString mTimeFormatLong; + + /** + * triggers the fetchValue method + */ + ThreadedTrigger mTrigger; +}; +#endif //UPTIMESRC_H |