mirror of
https://bitbucket.org/smil3y/kde-workspace.git
synced 2025-02-24 02:42:50 +00:00

can implement condition for when device changes occurs too but I have no plans to use it for something, not yet that is. the remove condition can (and probably will be) used to start kvkbd when there is no keyboard plugged in altough that will require program to check when to do that Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
114 lines
No EOL
4.4 KiB
C
114 lines
No EOL
4.4 KiB
C
/* This file is part of the KDE project
|
|
Copyright (C) 2023 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 SOLIDUISERVER_COMMON_H
|
|
#define SOLIDUISERVER_COMMON_H
|
|
|
|
#include <kdebug.h>
|
|
#include <kshell.h>
|
|
#include <ktoolinvocation.h>
|
|
#include <solid/device.h>
|
|
#include <solid/block.h>
|
|
#include <solid/storageaccess.h>
|
|
#include <solid/opticaldrive.h>
|
|
|
|
static void kSolidMountUDI(const QString &solidudi)
|
|
{
|
|
Solid::Device soliddevice(solidudi);
|
|
Solid::StorageAccess* solidstorageacces = soliddevice.as<Solid::StorageAccess>();
|
|
if (!solidstorageacces) {
|
|
kWarning() << "not storage access" << solidudi;
|
|
return;
|
|
}
|
|
solidstorageacces->setup();
|
|
}
|
|
|
|
static void kSolidUnmountUDI(const QString &solidudi)
|
|
{
|
|
Solid::Device soliddevice(solidudi);
|
|
Solid::StorageAccess* solidstorageacces = soliddevice.as<Solid::StorageAccess>();
|
|
if (!solidstorageacces) {
|
|
kWarning() << "not storage access" << solidudi;
|
|
return;
|
|
}
|
|
solidstorageacces->teardown();
|
|
}
|
|
|
|
static void kSolidEjectUDI(const QString &solidudi)
|
|
{
|
|
Solid::Device soliddevice(solidudi);
|
|
Solid::OpticalDrive *solidopticaldrive = soliddevice.as<Solid::OpticalDrive>();
|
|
if (!solidopticaldrive) {
|
|
kWarning() << "not optical drive" << solidudi;
|
|
return;
|
|
}
|
|
solidopticaldrive->eject();
|
|
}
|
|
|
|
// simplified version of KMacroExpander specialized for solid actions
|
|
static QStringList kSolidActionCommand(const QString &command, const QString &solidudi, const bool mount)
|
|
{
|
|
Solid::Device soliddevice(solidudi);
|
|
Solid::StorageAccess* solidstorageacces = soliddevice.as<Solid::StorageAccess>();
|
|
if (mount && solidstorageacces && !solidstorageacces->isAccessible()) {
|
|
kSolidMountUDI(solidudi);
|
|
}
|
|
|
|
QString actioncommand = command;
|
|
if (actioncommand.contains(QLatin1String("%f")) || actioncommand.contains(QLatin1String("%F"))) {
|
|
if (!solidstorageacces) {
|
|
kWarning() << "device is not storage access" << soliddevice.udi();
|
|
} else {
|
|
const QString devicefilepath = solidstorageacces->filePath();
|
|
actioncommand = actioncommand.replace(QLatin1String("%f"), devicefilepath);
|
|
actioncommand = actioncommand.replace(QLatin1String("%F"), devicefilepath);
|
|
}
|
|
}
|
|
if (actioncommand.contains(QLatin1String("%d")) || actioncommand.contains(QLatin1String("%D"))) {
|
|
const Solid::Block* solidblock = soliddevice.as<Solid::Block>();
|
|
if (!solidblock) {
|
|
kWarning() << "device is not block" << soliddevice.udi();
|
|
} else {
|
|
const QString devicedevice = solidblock->device();
|
|
actioncommand = actioncommand.replace(QLatin1String("%d"), devicedevice);
|
|
actioncommand = actioncommand.replace(QLatin1String("%D"), devicedevice);
|
|
}
|
|
}
|
|
if (actioncommand.contains(QLatin1String("%i")) || actioncommand.contains(QLatin1String("%I"))) {
|
|
const QString deviceudi = soliddevice.udi();
|
|
actioncommand = actioncommand.replace(QLatin1String("%i"), deviceudi);
|
|
actioncommand = actioncommand.replace(QLatin1String("%I"), deviceudi);
|
|
}
|
|
return KShell::splitArgs(actioncommand);
|
|
}
|
|
|
|
static void kExecuteAction(const KServiceAction &kserviceaction, const QString &solidudi, const bool mount)
|
|
{
|
|
QStringList actioncommand = kSolidActionCommand(kserviceaction.exec(), solidudi, mount);
|
|
if (actioncommand.size() == 0) {
|
|
kWarning() << "invalid action command" << kserviceaction.name();
|
|
return;
|
|
}
|
|
const QString actionexe = actioncommand.takeFirst();
|
|
const int actionresult = KToolInvocation::kdeinitExec(actionexe, actioncommand);
|
|
if (actionresult != 0) {
|
|
kWarning() << "could not execute action for" << kserviceaction.name() << actionresult;
|
|
}
|
|
}
|
|
|
|
#endif // SOLIDUISERVER_COMMON_H
|