Permalink
Browse files

initial revision of the policykit integration (non-functional)

  • Loading branch information...
1 parent 3c295b8 commit ba9bd6d638420219a69408b3f55b3d2bf39c7a1a @pvanek pvanek committed Jan 2, 2012
View
@@ -75,6 +75,7 @@ add_subdirectory( razorqt-x11info )
add_subdirectory( razorqt-runner )
add_subdirectory( razorqt-config )
add_subdirectory( razorqt-power )
+add_subdirectory( razorqt-policykit )
########### Add uninstall target ###############
@@ -0,0 +1,50 @@
+find_package(PolkitQt-1)
+if (NOT POLKITQT-1_LIB_DIR AND NOT POLKITQT-1_INCLUDE_DIR)
+ message(WARNING "Qt bindings for policykit are not found. Razor policykit integration won't be built")
+else ()
+ message(STATUS "PolkitQt-1: ${POLKITQT-1_VERSION_STRING}")
+ message(STATUS " libs: ${POLKITQT-1_CORE_LIBRARY} ${POLKITQT-1_AGENT_LIBRARY}")
+ message(STATUS " include: ${POLKITQT-1_INCLUDE_DIR}")
+
+ # TODO/FIXME: pkg-config for hardcoded paths
+ # pkg-config --cflags glib-2.0
+ # pkg-config --cflags polkit-agent-1
+ include_directories(
+ ${CMAKE_CURRENT_SOURCE_DIR}/src
+ ${CMAKE_CURRENT_BINARY_DIR}
+ ${CMAKE_SOURCE_DIR}/libraries
+ /usr/include/glib-2.0 /usr/lib64/glib-2.0/include
+ /usr/include/polkit-1 /usr/include/glib-2.0 /usr/lib64/glib-2.0/include
+ ${POLKITQT-1_INCLUDE_DIR}
+ )
+
+ set(razor-policykit-agent_SRCS
+ src/main.cpp
+ src/policykitagent.cpp
+ src/policykitagentgui.cpp
+ )
+
+ set(razor-policykit-agent_H
+ src/policykitagent.h
+ src/policykitagentgui.h
+ )
+
+ QT4_WRAP_CPP(razor-policykit-agent_MOCS ${razor-policykit-agent_H})
+ QT4_WRAP_UI(razor-policykit-agent_UI src/policykitagentgui.ui)
+
+ add_executable(razor-policykit-agent
+ ${razor-policykit-agent_SRCS} ${razor-policykit-agent_MOCS}
+ ${razor-policykit-agent_UI}
+ )
+
+ target_link_libraries(razor-policykit-agent
+ ${QT_QTCORE_LIBRARY}
+ ${QT_QTGUI_LIBRARY}
+ ${POLKITQT-1_CORE_LIBRARY} ${POLKITQT-1_AGENT_LIBRARY}
+ glib-2.0 gobject-2.0
+ qtxdg razorqt
+ )
+
+ # TODO/FIXME: install!
+
+endif()
@@ -0,0 +1,20 @@
+#include <QApplication>
+
+#include <glib-object.h>
+
+#include <qtxdg/xdgicon.h>
+#include <razorqt/razorsettings.h>
+
+#include "policykitagent.h"
+
+int main(int argc, char *argv[])
+{
+ g_type_init();
+
+ QApplication app(argc, argv);
+ XdgIcon::setThemeName(RazorSettings::globalSettings()->value("icon_theme").toString());
+ app.setWindowIcon(QIcon(QString(SHARE_DIR) + "/graphics/razor_logo.png"));
+
+ RazorPolicykit::PolicykitAgent agent;
+ return app.exec();
+}
@@ -0,0 +1,125 @@
+#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE 1
+
+#include <polkitagent/polkitagent.h>
+#include <polkitqt1-agent-session.h>
+#include <polkitqt1-subject.h>
+
+#include <QtCore/QDebug>
+#include <QtCore/QDebug>
+#include <QtGui/QInputDialog>
+
+#include "policykitagent.h"
+#include "policykitagentgui.h"
+
+
+namespace RazorPolicykit
+{
+
+PolicykitAgent::PolicykitAgent(QObject *parent)
+ : PolkitQt1::Agent::Listener(parent),
+ m_inProgress(false),
+ m_gui(0)
+{
+ PolkitQt1::UnixSessionSubject session(getpid());
+ registerListener(session, "/org/razorqt/PolicyKit1/AuthenticationAgent");
+}
+
+PolicykitAgent::~PolicykitAgent()
+{
+ if (m_gui)
+ {
+ m_gui->blockSignals(true);
+ m_gui->deleteLater();
+ }
+}
+
+
+void PolicykitAgent::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)
+{
+ qWarning() << "initiateAuthentication() inprogress:" << m_inProgress;
+ if (m_inProgress)
+ {
+ qWarning() << "Another auth in progress"; // TODO/FIXME: message
+ return;
+ }
+ m_inProgress = true;
+
+ if (m_gui)
+ {
+ delete m_gui;
+ m_gui = 0;
+ }
+ m_gui = new PolicykitAgentGUI(actionId, message, iconName, details, identities);
+
+ foreach (PolkitQt1::Identity i, identities)
+ {
+ PolkitQt1::Agent::Session *session;
+ session = new PolkitQt1::Agent::Session(i, cookie, result);
+ Q_ASSERT(session);
+ connect(session, SIGNAL(request(QString, bool)), this, SLOT(request(QString, bool)));
+ connect(session, SIGNAL(completed(bool)), this, SLOT(completed(bool)));
+ connect(session, SIGNAL(showError(QString)), this, SLOT(showError(QString)));
+ connect(session, SIGNAL(showInfo(QString)), this, SLOT(showInfo(QString)));
+ session->initiate();
+ }
+}
+
+bool PolicykitAgent::initiateAuthenticationFinish()
+{
+ qWarning() << "initiateAuthenticationFinish()";
+ m_inProgress = false;
+ return true;
+}
+
+void PolicykitAgent::cancelAuthentication()
+{
+ qWarning() << "Cancelling authentication";
+ m_inProgress = false;
+}
+
+void PolicykitAgent::request(const QString &request, bool echo)
+{
+ qWarning() << "Request: " << request;
+
+ PolkitQt1::Agent::Session *session = qobject_cast<PolkitQt1::Agent::Session *>(sender());
+ Q_ASSERT(session);
+ Q_ASSERT(m_gui);
+
+ // TODO/FIXME: map identity to session somehow - to setup correct identity in the combobox
+ m_gui->setPrompt(request, echo);
+ if (m_gui->exec())
+ session->setResponse(m_gui->response());
+ else
+ session->setResponse("");
+}
+
+void PolicykitAgent::completed(bool gainedAuthorization)
+{
+ qWarning() << "Completed: " << gainedAuthorization;
+ PolkitQt1::Agent::Session * session = qobject_cast<PolkitQt1::Agent::Session *>(sender());
+ Q_ASSERT(session);
+
+ session->result()->setCompleted();
+
+ delete session;
+
+ m_inProgress = false;
+}
+
+void PolicykitAgent::showError(const QString &text)
+{
+ qWarning() << "Error: " << text;
+}
+
+void PolicykitAgent::showInfo(const QString &text)
+{
+ qWarning() << "Info: " << text;
+}
+
+} //namespace
@@ -0,0 +1,73 @@
+// This is an example not a library
+/***************************************************************************
+ * Copyright (C) 2008 Daniel Nicoletti <dantti85-pk@yahoo.com.br> *
+ * Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> *
+ * *
+ * 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 2 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, write to the *
+ * Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
+ ***************************************************************************/
+
+#ifndef POLICYKITAGENT_H
+#define POLICYKITAGENT_H
+
+#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE 1
+
+#include <polkitqt1-agent-session.h>
+#include <polkitqt1-identity.h>
+#include <polkitqt1-details.h>
+#include <polkitqt1-agent-listener.h>
+
+#include <QtDBus/QDBusContext>
+#include <QtGui/QApplication>
+
+
+
+namespace RazorPolicykit
+{
+
+class PolicykitAgentGUI;
+
+class PolicykitAgent : public PolkitQt1::Agent::Listener
+{
+ Q_OBJECT
+
+public:
+ PolicykitAgent(QObject *parent = 0);
+ ~PolicykitAgent();
+
+public slots:
+ 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);
+ bool initiateAuthenticationFinish();
+ void cancelAuthentication();
+
+ void request(const QString &request, bool echo);
+ void completed(bool gainedAuthorization);
+ void showError(const QString &text);
+ void showInfo(const QString &text);
+
+private:
+ bool m_inProgress;
+ PolicykitAgentGUI * m_gui;
+};
+
+} // namespace
+
+#endif
@@ -0,0 +1,53 @@
+#include <qtxdg/xdgicon.h>
+
+#include "policykitagentgui.h"
+
+namespace RazorPolicykit
+{
+
+
+PolicykitAgentGUI::PolicykitAgentGUI(const QString &actionId,
+ const QString &message,
+ const QString &iconName,
+ const PolkitQt1::Details &details,
+ const PolkitQt1::Identity::List &identities)
+ : QDialog()
+{
+ setupUi(this);
+ Q_UNUSED(actionId);
+// setWindowTitle(actionId);
+// actionIdLabel->setText(actionId);
+ messageLabel->setText(message);
+ iconLabel->setPixmap(XdgIcon::fromTheme(iconName).pixmap(64, 64));
+
+ foreach (PolkitQt1::Identity identity, identities)
+ {
+ m_identityMap[identity.toString()] = identity;
+ identityComboBox->addItem(identity.toString());
+ }
+
+ foreach (QString i, details.keys())
+ {
+ QStringList list;
+ list << i << details.lookup(i);
+ detailsWidget->addTopLevelItem(new QTreeWidgetItem(list));
+ }
+}
+
+void PolicykitAgentGUI::setPrompt(const QString &text, bool echo)
+{
+ promptLabel->setText(text);
+ passwordEdit->setEchoMode(echo ? QLineEdit::Normal : QLineEdit::Password);
+}
+
+PolkitQt1::Identity PolicykitAgentGUI::identity()
+{
+ return m_identityMap[identityComboBox->currentText()];
+}
+
+QString PolicykitAgentGUI::response()
+{
+ return passwordEdit->text();
+}
+
+} // namespace
@@ -0,0 +1,33 @@
+#ifndef POLICYKITAGENTGUI_H
+#define POLICYKITAGENTGUI_H
+
+#include <polkitqt1-details.h>
+#include <polkitqt1-identity.h>
+
+#include "ui_policykitagentgui.h"
+
+namespace RazorPolicykit
+{
+
+class PolicykitAgentGUI : public QDialog, public Ui::PolicykitAgentGUI
+{
+ Q_OBJECT
+
+public:
+ PolicykitAgentGUI(const QString &actionId,
+ const QString &message,
+ const QString &iconName,
+ const PolkitQt1::Details &details,
+ const PolkitQt1::Identity::List &identities);
+
+ void setPrompt(const QString &text, bool echo);
+ PolkitQt1::Identity identity();
+ QString response();
+
+private:
+ QHash<QString,PolkitQt1::Identity> m_identityMap;
+};
+
+} // namespace
+
+#endif
Oops, something went wrong.

0 comments on commit ba9bd6d

Please sign in to comment.