kpowercontrol: new app for power management

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-04-21 02:12:53 +03:00
parent c7297bb9f8
commit f847278b75
5 changed files with 245 additions and 0 deletions

View file

@ -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}
)

View file

@ -0,0 +1,95 @@
/* This file is part of the KDE project
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
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 <QApplication>
#include <QDBusConnectionInterface>
#include <kactioncollection.h>
#include <kcomponentdata.h>
#include <klocale.h>
#include <kglobal.h>
#include <kaction.h>
#include <kicon.h>
#include <kdebug.h>
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<KAction*>(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);
}
}
}

View file

@ -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

View file

@ -0,0 +1,44 @@
/* This file is part of the KDE project
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
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 <kstatusnotifieritem.h>
#include <kmenu.h>
#include <khelpmenu.h>
#include <kpowermanager.h>
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

65
kpowercontrol/main.cpp Normal file
View file

@ -0,0 +1,65 @@
/* This file is part of the KDE project
Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
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 <QDBusConnectionInterface>
#include <kuniqueapplication.h>
#include <klocale.h>
#include <kcmdlineargs.h>
#include <kaboutdata.h>
#include <kdebug.h>
#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();
}