diff --git a/kpowercontrol/CMakeLists.txt b/kpowercontrol/CMakeLists.txt new file mode 100644 index 00000000..b9f6965f --- /dev/null +++ b/kpowercontrol/CMakeLists.txt @@ -0,0 +1,31 @@ +project(kpowercontrol) + +if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) + find_package(KDE4 4.21.0 REQUIRED) + include(KDE4Defaults) + include_directories(${KDE4_INCLUDES}) + add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS}) +endif() + +########### next target ############### + +set(kpowercontrol_SRCS + main.cpp + kpowercontrol.cpp +) + +add_executable(kpowercontrol ${kpowercontrol_SRCS}) + +target_link_libraries(kpowercontrol + ${KDE4_KDEUI_LIBS} + ${KDE4_KPOWERMANAGER_LIBS} +) + +install(TARGETS kpowercontrol ${INSTALL_TARGETS_DEFAULT_ARGS}) + +########### next target ############### + +install( + PROGRAMS kpowercontrol.desktop + DESTINATION ${KDE4_XDG_APPS_INSTALL_DIR} +) diff --git a/kpowercontrol/kpowercontrol.cpp b/kpowercontrol/kpowercontrol.cpp new file mode 100644 index 00000000..d22707c8 --- /dev/null +++ b/kpowercontrol/kpowercontrol.cpp @@ -0,0 +1,95 @@ +/* This file is part of the KDE project + Copyright (C) 2022 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "kpowercontrol.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +KPowerControl::KPowerControl(QObject* parent) + : KStatusNotifierItem(parent), + m_menu(nullptr), + m_helpmenu(nullptr) +{ + setTitle(i18n("Power management")); + setCategory(KStatusNotifierItem::SystemServices); + setIconByName("preferences-system-power-management"); + setStatus(KStatusNotifierItem::Passive); + + m_menu = new KMenu(associatedWidget()); + setContextMenu(m_menu); + + QDBusConnectionInterface* sessionbusiface = QDBusConnection::sessionBus().interface(); + if (sessionbusiface->isServiceRegistered("org.freedesktop.PowerManagement")) { + connect( + &m_powermanager, SIGNAL(profileChanged(QString)), + this, SLOT(slotProfileChanged(QString)) + ); + + foreach (const QString &profile, m_powermanager.profiles()) { + KAction* profileaction = actionCollection()->addAction(QString::fromLatin1("profile_%1").arg(profile)); + profileaction->setText(profile); + profileaction->setCheckable(true); + profileaction->setChecked(profile == m_powermanager.profile()); + connect(profileaction, SIGNAL(triggered()), this, SLOT(slotChangeProfile())); + m_menu->addAction(profileaction); + } + + m_menu->addSeparator(); + } else { + setOverlayIconByName("dialog-error"); + showMessage(i18n("Power management"), i18n("Power manager is not activer"), "dialog-error"); + } + + m_helpmenu = new KHelpMenu(associatedWidget(), KGlobal::mainComponent().aboutData()); + m_menu->addMenu(m_helpmenu->menu()); +} + +KPowerControl::~KPowerControl() +{ +} + +void KPowerControl::slotChangeProfile() +{ + KAction* profileaction = qobject_cast(sender()); + if (m_powermanager.setProfile(profileaction->iconText())) { + // do not wait for the signal to be emited + slotProfileChanged(profileaction->iconText()); + } else { + setOverlayIconByName("dialog-error"); + showMessage(i18n("Power management"), i18n("Could not change power manager profile"), "dialog-error"); + } +} + +void KPowerControl::slotProfileChanged(const QString &profile) +{ + const QString profile_objectname = QString::fromLatin1("profile_%1").arg(profile); + foreach (QAction* qaction, actionCollection()->actions()) { + const QString qactionobjectname = qaction->objectName(); + if (qactionobjectname.startsWith(QLatin1String("profile_"))) { + qaction->setChecked(qactionobjectname == profile_objectname); + } + } +} diff --git a/kpowercontrol/kpowercontrol.desktop b/kpowercontrol/kpowercontrol.desktop new file mode 100755 index 00000000..728463b2 --- /dev/null +++ b/kpowercontrol/kpowercontrol.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Icon=preferences-system-power-management +Name=KPowerControl +GenericName=Power management +Comment=A panel applet for power management +Exec=kpowercontrol --icon '%i' --caption '%c' +Terminal=false +Type=Application +Categories=Qt;KDE;System; +X-KDE-StartupNotify=false diff --git a/kpowercontrol/kpowercontrol.h b/kpowercontrol/kpowercontrol.h new file mode 100644 index 00000000..3779baf6 --- /dev/null +++ b/kpowercontrol/kpowercontrol.h @@ -0,0 +1,44 @@ +/* This file is part of the KDE project + Copyright (C) 2022 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KPOWERCONTROL_H +#define KPOWERCONTROL_H + +#include +#include +#include +#include + +class KPowerControl : public KStatusNotifierItem +{ + Q_OBJECT +public: + KPowerControl(QObject* parent = nullptr); + ~KPowerControl(); + +private Q_SLOTS: + void slotChangeProfile(); + void slotProfileChanged(const QString &profile); + +private: + KMenu* m_menu; + KHelpMenu* m_helpmenu; + KPowerManager m_powermanager; +}; + +#endif // KPOWERCONTROL_H diff --git a/kpowercontrol/main.cpp b/kpowercontrol/main.cpp new file mode 100644 index 00000000..38fa9b28 --- /dev/null +++ b/kpowercontrol/main.cpp @@ -0,0 +1,65 @@ +/* This file is part of the KDE project + Copyright (C) 2022 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include +#include + +#include "kpowercontrol.h" + +int main(int argc, char** argv) +{ + KAboutData aboutData( + "kpowercontrol", 0, ki18n("KPowerControl"), + "1.0.0", ki18n("A panel applet for power management."), + KAboutData::License_GPL_V2, + ki18n("(c) 2022 Ivailo Monev"), + KLocalizedString(), + "http://github.com/fluxer/katana" + ); + + aboutData.addAuthor( + ki18n("Ivailo Monev"), + ki18n("Maintainer"), + "xakepa10@gmail.com" + ); + aboutData.setProgramIconName(QLatin1String("preferences-system-power-management")); + + KCmdLineArgs::init(argc, argv, &aboutData); + KUniqueApplication::addCmdLineOptions(); + + KUniqueApplication kpowercontrolapp; + kpowercontrolapp.setQuitOnLastWindowClosed(false); + if (!KUniqueApplication::start()) { + kDebug() << "kpowercontrol is already running!"; + return 0; + } + + QDBusConnectionInterface* sessionbusiface = QDBusConnection::sessionBus().interface(); + if (!sessionbusiface->isServiceRegistered("org.freedesktop.PowerManagement")) { + kDebug() << "Activating org.freedesktop.PowerManagement"; + sessionbusiface->startService("org.freedesktop.PowerManagement"); + } + + KPowerControl kpowercontrol(&kpowercontrolapp); + + return kpowercontrolapp.exec(); +}