Use more compact syntax for passing and returning kauth helper arguments.

This commit is contained in:
Andrius Štikonas 2016-07-29 14:17:36 +01:00
parent c30a83a5b9
commit 918cf2ffa4
7 changed files with 42 additions and 60 deletions

View File

@ -1,6 +1,6 @@
/************************************************************************* /*************************************************************************
* Copyright (C) 2008,2009 by Volker Lanz <vl@fidra.de> * * Copyright (C) 2008,2009 by Volker Lanz <vl@fidra.de> *
* Copyright (C) 2016 by Andrius Štikonas <andrius@stikonas.eu> * * Copyright (C) 2016 by Andrius Štikonas <andrius@stikonas.eu> *
* * * *
* This program is free software; you can redistribute it and/or * * This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as * * modify it under the terms of the GNU General Public License as *

View File

@ -25,28 +25,24 @@ ActionReply Scan::scandevice(const QVariantMap& args)
{ {
ActionReply reply; ActionReply reply;
QString deviceNode = args[QLatin1String("deviceNode")].toString(); PedDevice* pedDevice = ped_device_get(args[QStringLiteral("deviceNode")].toString().toLocal8Bit().constData());
PedDevice* pedDevice = ped_device_get(deviceNode.toLocal8Bit().constData());
QVariantMap returnArgs;
if (!pedDevice) { if (!pedDevice) {
returnArgs[QLatin1String("pedDeviceError")] = true; reply.addData(QStringLiteral("pedDeviceError"), true);
reply.setData(returnArgs);
return reply; return reply;
} }
returnArgs[QLatin1String("model")] = QString::fromUtf8(pedDevice->model); reply.addData(QStringLiteral("model"), QString::fromUtf8(pedDevice->model));
returnArgs[QLatin1String("path")] = QString::fromUtf8(pedDevice->path); reply.addData(QStringLiteral("path"), QString::fromUtf8(pedDevice->path));
returnArgs[QLatin1String("heads")] = pedDevice->bios_geom.heads; reply.addData(QStringLiteral("heads"), pedDevice->bios_geom.heads);
returnArgs[QLatin1String("sectors")] = pedDevice->bios_geom.sectors; reply.addData(QStringLiteral("sectors"), pedDevice->bios_geom.sectors);
returnArgs[QLatin1String("cylinders")] = pedDevice->bios_geom.cylinders; reply.addData(QStringLiteral("cylinders"), pedDevice->bios_geom.cylinders);
returnArgs[QLatin1String("sectorSize")] = pedDevice->sector_size; reply.addData(QStringLiteral("sectorSize"), pedDevice->sector_size);
PedDisk* pedDisk = ped_disk_new(pedDevice); PedDisk* pedDisk = ped_disk_new(pedDevice);
if (!pedDisk) { if (!pedDisk) {
returnArgs[QLatin1String("pedDiskError")] = true; reply.addData(QStringLiteral("pedDiskError"), true);
reply.setData(returnArgs);
return reply; return reply;
} }
@ -77,13 +73,13 @@ ActionReply Scan::scandevice(const QVariantMap& args)
lastUsableSector -= 32; lastUsableSector -= 32;
} }
returnArgs[QLatin1String("pedDeviceError")] = false; reply.addData(QStringLiteral("pedDeviceError"), false);
returnArgs[QLatin1String("pedDiskError")] = false; reply.addData(QStringLiteral("pedDiskError"), false);
returnArgs[QLatin1String("typeName")] = QString::fromUtf8(pedDisk->type->name); reply.addData(QStringLiteral("typeName"), QString::fromUtf8(pedDisk->type->name));
returnArgs[QLatin1String("maxPrimaryPartitionCount")] = ped_disk_get_max_primary_partition_count(pedDisk); reply.addData(QStringLiteral("maxPrimaryPartitionCount"), ped_disk_get_max_primary_partition_count(pedDisk));
returnArgs[QLatin1String("firstUsableSector")] = firstUsableSector; reply.addData(QStringLiteral("firstUsableSector"), firstUsableSector);
returnArgs[QLatin1String("lastUsableSector")] = lastUsableSector; reply.addData(QStringLiteral("lastUsableSector"), lastUsableSector);
PedPartition* pedPartition = nullptr; PedPartition* pedPartition = nullptr;
QList<QVariant> partitionPath; QList<QVariant> partitionPath;
@ -98,7 +94,7 @@ ActionReply Scan::scandevice(const QVariantMap& args)
if (pedPartition->num < 1) if (pedPartition->num < 1)
continue; continue;
partitionPath.append(QLatin1String(ped_partition_get_path(pedPartition))); partitionPath.append(QString::fromLatin1(ped_partition_get_path(pedPartition)));
partitionType.append(pedPartition->type); partitionType.append(pedPartition->type);
partitionStart.append(pedPartition->geom.start); partitionStart.append(pedPartition->geom.start);
partitionEnd.append(pedPartition->geom.end); partitionEnd.append(pedPartition->geom.end);
@ -134,45 +130,37 @@ ActionReply Scan::scandevice(const QVariantMap& args)
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
} }
returnArgs[QLatin1String("availableFlags")] = availableFlags; reply.addData(QStringLiteral("availableFlags"), availableFlags);
returnArgs[QLatin1String("activeFlags")] = activeFlags; reply.addData(QStringLiteral("activeFlags"), activeFlags);
returnArgs[QLatin1String("partitionPath")] = partitionPath; reply.addData(QStringLiteral("partitionPath"), partitionPath);
returnArgs[QLatin1String("partitionType")] = partitionType; reply.addData(QStringLiteral("partitionType"), partitionType);
returnArgs[QLatin1String("partitionStart")] = partitionStart; reply.addData(QStringLiteral("partitionStart"), partitionStart);
returnArgs[QLatin1String("partitionEnd")] = partitionEnd; reply.addData(QStringLiteral("partitionEnd"), partitionEnd);
returnArgs[QLatin1String("partitionBusy")] = partitionBusy; reply.addData(QStringLiteral("partitionBusy"), partitionBusy);
reply.setData(returnArgs);
return reply; return reply;
} }
ActionReply Scan::readsectorsused(const QVariantMap& args) ActionReply Scan::readsectorsused(const QVariantMap& args)
{ {
ActionReply reply;
QString deviceNode = args[QLatin1String("deviceNode")].toString();
qint64 firstSector = args[QLatin1String("firstSector")].toLongLong();
qint64 rval = -1; qint64 rval = -1;
if (PedDevice* pedDevice = ped_device_get(deviceNode.toLocal8Bit().constData())) if (PedDevice* pedDevice = ped_device_get(args[QStringLiteral("deviceNode")].toString().toLocal8Bit().constData()))
if (PedDisk* pedDisk = ped_disk_new(pedDevice)) if (PedDisk* pedDisk = ped_disk_new(pedDevice))
if (PedPartition* pedPartition = ped_disk_get_partition_by_sector(pedDisk, firstSector)) if (PedPartition* pedPartition = ped_disk_get_partition_by_sector(pedDisk, args[QStringLiteral("firstSector")].toLongLong()))
if (PedFileSystem* pedFileSystem = ped_file_system_open(&pedPartition->geom)) if (PedFileSystem* pedFileSystem = ped_file_system_open(&pedPartition->geom))
if (PedConstraint* pedConstraint = ped_file_system_get_resize_constraint(pedFileSystem)) if (PedConstraint* pedConstraint = ped_file_system_get_resize_constraint(pedFileSystem))
rval = pedConstraint->min_size; rval = pedConstraint->min_size;
QVariantMap returnArgs; ActionReply reply;
returnArgs[QLatin1String("sectorsUsed")] = rval; reply.addData(QStringLiteral("sectorsUsed"), rval);
reply.setData(returnArgs);
return reply; return reply;
} }
ActionReply Scan::detectfilesystem(const QVariantMap& args) ActionReply Scan::detectfilesystem(const QVariantMap& args)
{ {
ActionReply reply; ActionReply reply;
QVariantMap returnArgs; QString deviceNode = args[QStringLiteral("deviceNode")].toString();
QString deviceNode = args[QLatin1String("deviceNode")].toString();
blkid_cache cache; blkid_cache cache;
if (blkid_get_cache(&cache, nullptr) == 0) { if (blkid_get_cache(&cache, nullptr) == 0) {
@ -189,17 +177,16 @@ ActionReply Scan::detectfilesystem(const QVariantMap& args)
QString st = QString::fromUtf8(string); QString st = QString::fromUtf8(string);
free(string); free(string);
if (st == QStringLiteral("msdos")) if (st == QStringLiteral("msdos"))
returnArgs[QLatin1String("fileSystem")] = QStringLiteral("fat16"); reply.addData(QStringLiteral("fileSystem"), QStringLiteral("fat16"));
else else
returnArgs[QLatin1String("fileSystem")] = QStringLiteral("fat32"); reply.addData(QStringLiteral("fileSystem"), QStringLiteral("fat32"));
} }
else else
returnArgs[QLatin1String("fileSystem")] = s; reply.addData(QStringLiteral("fileSystem"), s);
} }
blkid_put_cache(cache); blkid_put_cache(cache);
} }
reply.setData(returnArgs);
return reply; return reply;
} }

View File

@ -75,12 +75,12 @@ static PedExceptionOption pedExceptionHandler(PedException* e)
#if defined LIBPARTED_FS_RESIZE_LIBRARY_SUPPORT #if defined LIBPARTED_FS_RESIZE_LIBRARY_SUPPORT
static qint64 readSectorsUsedLibParted(const Partition& p) static qint64 readSectorsUsedLibParted(const Partition& p)
{ {
QVariantMap args;
args[QLatin1String("deviceNode")] = p.deviceNode();
args[QLatin1String("firstSector")] = p.firstSector();
KAuth::Action action = QStringLiteral("org.kde.kpmcore.scan.readsectorsused"); KAuth::Action action = QStringLiteral("org.kde.kpmcore.scan.readsectorsused");
action.setHelperId(QStringLiteral("org.kde.kpmcore.scan")); action.setHelperId(QStringLiteral("org.kde.kpmcore.scan"));
QVariantMap args = {
{ QStringLiteral("deviceNode"), p.deviceNode() },
{ QStringLiteral("firstSector"), p.firstSector() }
};
action.setArguments(args); action.setArguments(args);
KAuth::ExecuteJob *job = action.execute(); KAuth::ExecuteJob *job = action.execute();
if (!job->exec()) { if (!job->exec()) {
@ -146,11 +146,9 @@ void LibPartedBackend::initFSSupport()
*/ */
Device* LibPartedBackend::scanDevice(const QString& deviceNode) Device* LibPartedBackend::scanDevice(const QString& deviceNode)
{ {
QVariantMap args;
args[QLatin1String("deviceNode")] = deviceNode;
KAuth::Action scanAction = QStringLiteral("org.kde.kpmcore.scan.scandevice"); KAuth::Action scanAction = QStringLiteral("org.kde.kpmcore.scan.scandevice");
scanAction.setHelperId(QStringLiteral("org.kde.kpmcore.scan")); scanAction.setHelperId(QStringLiteral("org.kde.kpmcore.scan"));
QVariantMap args = {{ QStringLiteral("deviceNode"), deviceNode }};
scanAction.setArguments(args); scanAction.setArguments(args);
KAuth::ExecuteJob *job = scanAction.execute(); KAuth::ExecuteJob *job = scanAction.execute();
if (!job->exec()) { if (!job->exec()) {
@ -354,12 +352,11 @@ FileSystem::Type LibPartedBackend::detectFileSystem(const QString& deviceNode)
{ {
FileSystem::Type rval = FileSystem::Unknown; FileSystem::Type rval = FileSystem::Unknown;
QVariantMap args;
args[QLatin1String("deviceNode")] = deviceNode;
KAuth::Action action = QStringLiteral("org.kde.kpmcore.scan.detectfilesystem"); KAuth::Action action = QStringLiteral("org.kde.kpmcore.scan.detectfilesystem");
action.setHelperId(QStringLiteral("org.kde.kpmcore.scan")); action.setHelperId(QStringLiteral("org.kde.kpmcore.scan"));
QVariantMap args = {{ QStringLiteral("deviceNode"), deviceNode }};
action.setArguments(args); action.setArguments(args);
KAuth::ExecuteJob *job = action.execute(); KAuth::ExecuteJob *job = action.execute();
if (!job->exec()) { if (!job->exec()) {
qWarning() << "KAuth returned an error code: " << job->errorString(); qWarning() << "KAuth returned an error code: " << job->errorString();

View File

@ -28,8 +28,8 @@
#include <unistd.h> #include <unistd.h>
LibPartedDevice::LibPartedDevice(const QString& device_node) : LibPartedDevice::LibPartedDevice(const QString& deviceNode) :
CoreBackendDevice(device_node), CoreBackendDevice(deviceNode),
m_PedDevice(nullptr) m_PedDevice(nullptr)
{ {
} }

View File

@ -35,7 +35,7 @@ class LibPartedDevice : public CoreBackendDevice
Q_DISABLE_COPY(LibPartedDevice); Q_DISABLE_COPY(LibPartedDevice);
public: public:
LibPartedDevice(const QString& device_node); LibPartedDevice(const QString& deviceNode);
~LibPartedDevice(); ~LibPartedDevice();
public: public:

View File

@ -51,4 +51,3 @@ bool LibPartedPartition::setFlag(Report& report, PartitionTable::Flag partitionM
return true; return true;
} }

View File

@ -46,5 +46,4 @@ private:
PedPartition* m_PedPartition; PedPartition* m_PedPartition;
}; };
#endif #endif