diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,9 +41,12 @@ set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) +find_package(PolkitQt-1 REQUIRED) find_package(ECM ${KF5_MIN_VERSION} REQUIRED NO_MODULE) set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/") +include_directories(${POLKITQT-1_INCLUDE_DIR}) + include(KDEInstallDirs) include(KDECMakeSettings) include(KDECompilerSettings NO_POLICY_SCOPE) diff --git a/FindPolkitQt-1.cmake b/FindPolkitQt-1.cmake new file mode 100644 --- /dev/null +++ b/FindPolkitQt-1.cmake @@ -0,0 +1,36 @@ +/************************************************************************* + * Copyright (c) 2010 Dario Freddi * + * * + * 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 3 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, see .* + *************************************************************************/ + +# Support POLKITQT-1_MIN_VERSION for compatibility: + +if ( NOT PolkitQt-1_FIND_VERSION AND POLKITQT-1_MIN_VERSION ) + set ( PolkitQt-1_FIND_VERSION ${POLKITQT-1_MIN_VERSION} ) +endif ( NOT PolkitQt-1_FIND_VERSION AND POLKITQT-1_MIN_VERSION ) + +set( _PolkitQt-1_FIND_QUIETLY ${PolkitQt-1_FIND_QUIETLY} ) +find_package( PolkitQt-1 ${PolkitQt-1_FIND_VERSION} QUIET NO_MODULE PATHS ${LIB_INSTALL_DIR}/PolkitQt-1/cmake ) +set( PolkitQt-1_FIND_QUIETLY ${_PolkitQt-1_FIND_QUIETLY} ) + +include( FindPackageHandleStandardArgs ) +find_package_handle_standard_args( PolkitQt-1 DEFAULT_MSG PolkitQt-1_CONFIG ) + +if (POLKITQT-1_FOUND) + if (NOT POLKITQT-1_INSTALL_DIR STREQUAL CMAKE_INSTALL_PREFIX) + message("WARNING: Installation prefix does not match PolicyKit install prefixes. You probably will need to move files installed " + "in POLICY_FILES_INSTALL_DIR and by dbus_add_activation_system_service to the ${POLKITQT-1_INSTALL_DIR} prefix") + endif (NOT POLKITQT-1_INSTALL_DIR STREQUAL CMAKE_INSTALL_PREFIX) +endif (POLKITQT-1_FOUND) diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -19,6 +19,7 @@ set(UTIL_SRC ${HelperInterface_SRCS} util/capacity.cpp + util/externalcommand_authagent.cpp util/externalcommand.cpp util/globallog.cpp util/helpers.cpp @@ -29,6 +30,7 @@ set(UTIL_LIB_HDRS util/libpartitionmanagerexport.h util/capacity.h + util/externalcommand_authagent.h util/externalcommand.h util/globallog.h util/helpers.h diff --git a/src/util/externalcommand_authagent.h b/src/util/externalcommand_authagent.h new file mode 100644 --- /dev/null +++ b/src/util/externalcommand_authagent.h @@ -0,0 +1,62 @@ +/************************************************************************* + * Copyright (C) 2019 by Shubham * + * * + * 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 3 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, see .* + *************************************************************************/ + +#ifndef EXTERNALCOMMAND_AGENTLISTENER_H +#define EXTERNALCOMMAND_AGENTLISTENER_H + +#include + +#include +#include +#include +#include + +using namespace PolkitQt1; + +class AgentListener : public Agent::Listener +{ + Q_OBJECT + Q_DISABLE_COPY(AgentListener) +public: + AgentListener(QWidget *parent = nullptr); + ~AgentListener(); + +public Q_SLOTS: + bool registerAgentListener(const PolkitQt1::Subject &subject, const QString &objectPath) override; + void initiateAuthentication(const QString &actionId, + const QString &message, + const QString &iconName, + const PolkitQt1::Details &details, + const QString &cookie, + const PolkitQt1::Identity::List &identities, + PolkitQt1::Agent::AsyncResult *result) override; + void initiateAuthenticationFinish() override; + void cancelAuthentication() override; + + void setResponse(const QString &response); + void request(const QString &request, bool echo); + void showError(const QString& text); + void showInfo(const QString& text); + void completed(bool gainedAuthorization); + +private: + PolkitQt1::Session *m_authenticationSession; + Listener *m_agentListener; +}; + +#endif // EXTERNALCOMMAND_AGENTLISTENER_H + diff --git a/src/util/externalcommand_authagent.cpp b/src/util/externalcommand_authagent.cpp new file mode 100644 --- /dev/null +++ b/src/util/externalcommand_authagent.cpp @@ -0,0 +1,128 @@ +/************************************************************************* + * Copyright (C) 2019 by Shubham * + * * + * 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 3 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, see .* + *************************************************************************/ + +#include +#include +#include + +#include +#include +#include +#include + +#include "util/externalcommand_authagent.h" + +using namespace PolkitQt1::Agent; + +AgentListener::AgentListener(QWidget *parent) + : Listener(qobject_cast(parent)), + m_agentListener(qobject_cast(parent)), + m_authenticationSession(nullptr) +{ + +} + +AgentListener::~AgentListener() +{ + // Agent Listener destroyed + delete m_agentListener; + delete m_authenticationSession; +} + +bool AgentListener::registerListener(const PolkitQt1::Subject &subject, const QString &objectPath) override +{ + return m_agentListener->listener()->registerListener(subject, objectPath); +} + +void AgentListener::initiateAuthentication(const QString &actionId, + const QString &message, + const QString &iconName, + const PolkitQt1::Details &details, + const QString &cookie, + const PolkitQt1::Identity::List &identities, + AsyncResult *result) override +{ + + if (registerListener(subject, path)) { + for (const PolkitQt1::Identity &identity, identities) { + qDebug() << identity.toString(); + m_authenticationSession = new Session(identity, cookie, result); + + connect(m_authenticationSession, &PolkitQt1::Session::request, this, &AgentListener::request); + connect(m_authenticationSession, &PolkitQt1::Session::completed, this, &AgentListener::completed); + connect(m_authenticationSession, &PolkitQt1::Session::showError, this, &AgentListener::showError); + connect(m_authenticationSession, &PolkitQt1::Session::showInfo, this, &AgentListener::showInfo); + + // Using PolkitQt1::Session class member functions/API to initiate the authentication process + m_authenticationSession->initiate(); + } + } +} + +bool AgentListener::intiateAuthenticationFinish() override +{ + qDebug() << "Initializing Authentication finish"; + return true; +} + +bool AgentListener::cancelAuthentication() override +{ + qDebug() << "Cancelling the Authentication session"; + + m_authenticationSession->cancel(); + return true; +} + +void AgentListener::request(const QString &request, bool echo) +{ + Q_UNUSED(echo) + + qDebug() << "Request: " << request; + + Session *m_authenticationSession = dynamic_cast(sender()); + m_authenticationSession->setResponse(QStringLiteral("")); + + delete m_authenticationSession; +} + +void AgentListener::setResponse(const QString &response) +{ + Session *m_authenticationSession = dynamic_cast(sender()); + m_authenticationSession->setResponse(QStringLiteral("")); + + delete m_authenticationSession; +} + +void AgentListener::showError(QString& text) +{ + qDebug() << "Error: " << text; +} + +void AgentListener::showInfo(QString& text) +{ + qDebug() << "Information: " << text; +} + +void AgentListener::completed(bool gainedAuthorization) +{ + qDebug() << "Action completed: " << gainedAuthorization; + + Session *m_authenticationSession = dynamic_cast(sender()); + m_authenticationSession->result()->setCompleted(); + + delete m_authenticationSession; +}