diff options
Diffstat (limited to 'agent')
| -rw-r--r-- | agent/CMakeLists.txt | 25 | ||||
| -rw-r--r-- | agent/listeneradapter.cpp | 146 | ||||
| -rw-r--r-- | agent/listeneradapter_p.h | 83 | ||||
| -rw-r--r-- | agent/polkitqt1-agent-listener.cpp | 124 | ||||
| -rw-r--r-- | agent/polkitqt1-agent-listener.h | 161 | ||||
| -rw-r--r-- | agent/polkitqt1-agent-session.cpp | 169 | ||||
| -rw-r--r-- | agent/polkitqt1-agent-session.h | 180 | ||||
| -rw-r--r-- | agent/polkitqtlistener.cpp | 150 | ||||
| -rw-r--r-- | agent/polkitqtlistener_p.h | 49 |
9 files changed, 1087 insertions, 0 deletions
diff --git a/agent/CMakeLists.txt b/agent/CMakeLists.txt new file mode 100644 index 000000000..80d1bd3b2 --- /dev/null +++ b/agent/CMakeLists.txt @@ -0,0 +1,25 @@ +include_directories( + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_SOURCE_DIR} +) + +set(polkit_qt_agent_SRCS + polkitqt1-agent-session.cpp + polkitqt1-agent-listener.cpp + listeneradapter.cpp + polkitqtlistener.cpp +) +automoc4_add_library(polkit-qt-agent-1 SHARED ${polkit_qt_agent_SRCS}) + +target_link_libraries(polkit-qt-agent-1 + ${POLKIT_LIBRARIES} + ${QT_QTCORE_LIBRARY} + ${POLKIT_AGENT_LIBRARY} + polkit-qt-core-1 +) + +set_target_properties(polkit-qt-agent-1 PROPERTIES VERSION ${POLKITQT-1_LIBRARY_VERSION} + SOVERSION ${POLKITQT-1_ABI_VERSION} + DEFINE_SYMBOL MAKE_POLKITQT1_LIB) + +install(TARGETS polkit-qt-agent-1 ${INSTALL_TARGETS_DEFAULT_ARGS}) diff --git a/agent/listeneradapter.cpp b/agent/listeneradapter.cpp new file mode 100644 index 000000000..b25449009 --- /dev/null +++ b/agent/listeneradapter.cpp @@ -0,0 +1,146 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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 "listeneradapter_p.h" +#include <QtCore/QDebug> +#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE 1 +#include <polkitagent/polkitagent.h> + +namespace PolkitQt1 +{ + +namespace Agent +{ + +class ListenerAdapterHelper +{ +public: + ListenerAdapterHelper() : q(0) {} + ~ListenerAdapterHelper() { + delete q; + } + ListenerAdapter *q; +}; + +Q_GLOBAL_STATIC(ListenerAdapterHelper, s_globalListenerAdapter) + +ListenerAdapter *ListenerAdapter::instance() +{ + if (!s_globalListenerAdapter()->q) { + new ListenerAdapter(); + } + + return s_globalListenerAdapter()->q; +} + +ListenerAdapter::ListenerAdapter(QObject *parent) + : QObject(parent) +{ + Q_ASSERT(!s_globalListenerAdapter()->q); + s_globalListenerAdapter()->q = this; +} + +Listener* ListenerAdapter::findListener(PolkitAgentListener *listener) +{ + Listener *listItem; + + Q_FOREACH(listItem, m_listeners) { + Q_ASSERT(listItem); + + if (listItem->listener() == listener) { + return listItem; + } + } + + return NULL; +} + +void ListenerAdapter::polkit_qt_listener_initiate_authentication(PolkitAgentListener *listener, + const gchar *action_id, + const gchar *message, + const gchar *icon_name, + PolkitDetails *details, + const gchar *cookie, + GList *identities, + GCancellable *cancellable, + GSimpleAsyncResult *result) +{ + qDebug() << "polkit_qt_listener_initiate_authentication callback for " << listener; + + PolkitQt1::Identity::List idents; + PolkitQt1::Details dets(details); + + Listener *list = findListener(listener); + + for (GList *identity = g_list_first(identities); identity != NULL; identity = g_list_next(identity)) { + idents.append(PolkitQt1::Identity((PolkitIdentity *)identity->data)); + } + + list->initiateAuthentication(QString::fromUtf8(action_id), + QString::fromUtf8(message), + QString::fromUtf8(icon_name), + dets, + QString::fromUtf8(cookie), + idents, + new AsyncResult(result)); +} + +gboolean ListenerAdapter::polkit_qt_listener_initiate_authentication_finish(PolkitAgentListener *listener, + GAsyncResult *res, + GError **error) +{ + qDebug() << "polkit_qt_listener_initiate_authentication_finish callback for " << listener; + + GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT(res); + if (g_simple_async_result_propagate_error(simple, error)) { + return false; + } + return true; +} + +void ListenerAdapter::cancelled_cb(PolkitAgentListener *listener) +{ + qDebug() << "cancelled_cb for " << listener; + + Listener *list = findListener(listener); + + list->cancelAuthentication(); +} + +void ListenerAdapter::addListener(Listener *listener) +{ + qDebug() << "Adding new listener " << listener << "for " << listener->listener(); + + m_listeners.append(listener); +} + +void ListenerAdapter::removeListener(Listener *listener) +{ + qDebug() << "Removing listener " << listener; + + // should be safe as we don't have more than one same listener registered in one time + m_listeners.removeOne(listener); +} + +} + +} + +#include "listeneradapter_p.moc" diff --git a/agent/listeneradapter_p.h b/agent/listeneradapter_p.h new file mode 100644 index 000000000..126cd134c --- /dev/null +++ b/agent/listeneradapter_p.h @@ -0,0 +1,83 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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 LISTENER_ADAPTER_P_H +#define LISTENER_ADAPTER_P_H + +#include <QtCore/QObject> + +#include "polkitqt1-agent-listener.h" +#include "polkitqt1-identity.h" +#include "polkitqt1-details.h" + +typedef struct _GList GList; +typedef struct _GCancellable GCancellable; +typedef struct _GAsyncResult GAsyncResult; +typedef struct _GSimpleAsyncResult GSimpleAsyncResult; +typedef struct _GError GError; +typedef int gboolean; +typedef char gchar; + +namespace PolkitQt1 +{ + +namespace Agent +{ + +class AsyncResult; +class Listener; +class ListenerAdapter : public QObject +{ + Q_OBJECT + Q_DISABLE_COPY(ListenerAdapter) +public: + static ListenerAdapter* instance(); + ~ListenerAdapter() {} + + void polkit_qt_listener_initiate_authentication(PolkitAgentListener *listener, + const gchar *action_id, + const gchar *message, + const gchar *icon_name, + PolkitDetails *details, + const gchar *cookie, + GList *identities, + GCancellable *cancellable, + GSimpleAsyncResult *result); + + gboolean polkit_qt_listener_initiate_authentication_finish(PolkitAgentListener *listener, + GAsyncResult *res, + GError **error); + void cancelled_cb(PolkitAgentListener *listener); +private: + void addListener(Listener *listener); + void removeListener(Listener *listener); + Listener* findListener(PolkitAgentListener *listener); + + explicit ListenerAdapter(QObject *parent = 0); + QList<Listener *> m_listeners; + + friend class Listener; +}; + +} + +} + +#endif diff --git a/agent/polkitqt1-agent-listener.cpp b/agent/polkitqt1-agent-listener.cpp new file mode 100644 index 000000000..369dd7540 --- /dev/null +++ b/agent/polkitqt1-agent-listener.cpp @@ -0,0 +1,124 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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 "polkitqt1-agent-listener.h" + +#include <QtCore/QDebug> + +#include "polkitqtlistener_p.h" + +#include "polkitqt1-authority.h" +#include "polkitqt1-subject.h" +#include "polkitqt1-identity.h" +#include "polkitqt1-details.h" + +namespace PolkitQt1 +{ + +namespace Agent +{ + +class ListenerPrivate +{ +public: + PolkitAgentListener *listener; +#ifndef POLKIT_QT_1_COMPATIBILITY_MODE + void *registeredHandle; +#endif +}; + +Listener::Listener(QObject *parent) + : QObject(parent), d(new ListenerPrivate) +{ + g_type_init(); + + d->listener = polkit_qt_listener_new(); + + qDebug() << "New PolkitAgentListener " << d->listener; + + ListenerAdapter::instance()->addListener(this); +} + +Listener::Listener(PolkitAgentListener *listener, QObject *parent) + : QObject(parent), d(new ListenerPrivate) +{ + g_type_init(); + + d->listener = listener; +} + +Listener::~Listener() +{ + qDebug("Destroying listener"); + + ListenerAdapter::instance()->removeListener(this); +#ifndef POLKIT_QT_1_COMPATIBILITY_MODE + if (d->registeredHandle) { + polkit_agent_listener_unregister(d->registeredHandle); + } +#endif + if (d->listener != NULL) { + g_object_unref(d->listener); + } +} + +bool Listener::registerListener(const PolkitQt1::Subject &subject, const QString &objectPath) +{ + GError *error = NULL; + +#ifndef POLKIT_QT_1_COMPATIBILITY_MODE + d->registeredHandle = polkit_agent_listener_register(d->listener, + POLKIT_AGENT_REGISTER_FLAGS_NONE, +#else + bool r = polkit_agent_register_listener(d->listener, +#endif + subject.subject(), + objectPath.toAscii().data(), +#ifndef POLKIT_QT_1_COMPATIBILITY_MODE + NULL, +#endif + &error); + + if (error != NULL) { + qWarning() << QString("Cannot register authentication agent: %1").arg(error->message); + g_error_free(error); + return false; + } +#ifndef POLKIT_QT_1_COMPATIBILITY_MODE + if (d->registeredHandle == NULL) { + qWarning() << QString("Cannot register authentication agent!"); + return false; + } + return true; +#else + return r; +#endif +} + +const PolkitAgentListener *Listener::listener() +{ + return d->listener; +} + +} + +} + +#include "polkitqt1-agent-listener.moc" diff --git a/agent/polkitqt1-agent-listener.h b/agent/polkitqt1-agent-listener.h new file mode 100644 index 000000000..38c2ae46f --- /dev/null +++ b/agent/polkitqt1-agent-listener.h @@ -0,0 +1,161 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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 POLKITQT1_AGENT_LISTENER_H +#define POLKITQT1_AGENT_LISTENER_H + +#include "polkitqt1-export.h" + +#include <QtCore/QObject> + +#include "polkitqt1-agent-session.h" + +#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE 1 + +typedef struct _PolkitAgentListener PolkitAgentListener; + +namespace PolkitQt1 +{ +class Subject; +class Identity; +class Details; +} + +namespace PolkitQt1 +{ + +namespace Agent +{ + +class ListenerPrivate; +/** + * \class Listener polkitqt1-agent-listener.h Listener + * \author Jaroslav Reznik <jreznik@redhat.com> + * + * \brief Listener is abstract class used for implementing authentication agents. + * + * To implement an authentication agent, just subclass this class and implement + * virtual functions initiateAuthentication, initiateAuthenticationFinish + * and cancelAuthentication. + * + * You can also use Session class to authenticate users however it isn't required. + * \sa Session + */ +class POLKITQT1_EXPORT Listener : public QObject +{ + Q_OBJECT + Q_DISABLE_COPY(Listener) +public: + /** + * \brief Constructor of Listener class + */ + Listener(QObject *parent = 0); + + /** + * \brief Constructor of Listener class from PolkitAgentListener + * + * \warning Use this only if you are completely aware of what are you doing! + * + * \param listener Pointer to the PolkitAgentListener + * \param parent + */ + explicit Listener(PolkitAgentListener *listener, QObject *parent = 0); + + virtual ~Listener(); + + /** + * \brief Registers listener with polkit daemon as an authentication agent for \p subject. + * + * This is implemented by registering a DBus object at \p objectPath on the unique + * name assigned by the system message bus. + * + * Whenever the polkit daemon needs to authenticate a processes that is related to \p subject, + * the methods initiateAuthentication and initiateAuthenticationFinish will be evoked. + * + * \param subject Subject that listener will be registered for + * \param objectPath DBus object path + * \return \c True if the polkitqt1-agent-listener.has been registered, \c False otherwise + */ + bool registerListener(const PolkitQt1::Subject &subject, const QString &objectPath); + + /** + * \brief Returns pointer to the PolkitAgentListener. + * + * \warning Use this only if you are completely aware of what are you doing! + * + * \return PolkitAgentListener + */ + const PolkitAgentListener *listener(); + +public Q_SLOTS: + /** + * \brief Initiate authentication for the action + * + * This method will be called on a registered authentication agent when the user owning + * the session needs to prove he is one of the identities listed in \p identities. + * + * \note You have to reimplement this method in the subclass. + * + * \param actionId The action to authenticate for + * \param message The message to present to the user + * \param iconName The name of the icon which is representing the action + * \param details Details describing the action + * \param cookie The cookie for the authentization request + * \param identities A list of Identity object that the user can choose to authenticate as + * \param result This AsyncResult MUST be completed by using complete() method when the + * authentication is done. You can pass it to the constructor of the Session class + * and then call session->result()->complete() to mark the action as done. + */ + virtual void initiateAuthentication(const QString &actionId, + const QString &message, + const QString &iconName, + const PolkitQt1::Details &details, + const QString &cookie, + const PolkitQt1::Identity::List &identities, + AsyncResult *result) = 0; + + /** + * TODO: Is this method really required ? + * \brief Finishes an authentication request from the polkit daemon. + * + * \note You have to reimplement this method in the subclass. + * + * \see initiateAuthentication + */ + virtual bool initiateAuthenticationFinish() = 0; + + /** + * TODO: Is this method really required ? + * \brief Cancels an authentication request from the polkit daemon. + * + * \note You have to reimplement this method in the subclass. + * + * \see initiateAuthentication + */ + virtual void cancelAuthentication() = 0; + +private: + ListenerPrivate * const d; +}; +} + +} + +#endif diff --git a/agent/polkitqt1-agent-session.cpp b/agent/polkitqt1-agent-session.cpp new file mode 100644 index 000000000..52ddacf5e --- /dev/null +++ b/agent/polkitqt1-agent-session.cpp @@ -0,0 +1,169 @@ +/* + * This file is part of the PolKit1-qt project + * Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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 "polkitqt1-agent-session.h" + +#include <QtCore/QDebug> + +#include "polkitqt1-identity.h" + +#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE 1 +#include <polkitagent/polkitagent.h> + +using namespace PolkitQt1::Agent; + +class Session::Private +{ +public: + Private() {} + ~Private(); + + static void completed(PolkitAgentSession *s, gboolean gained_authorization, gpointer user_data); + static void request(PolkitAgentSession *s, gchar *request, gboolean echo_on, gpointer user_data); + static void showError(PolkitAgentSession *s, gchar *text, gpointer user_data); + static void showInfo(PolkitAgentSession *s, gchar *text, gpointer user_data); + + AsyncResult *result; + PolkitAgentSession *polkitAgentSession; +}; + +Session::Private::~Private() +{ + // polkitAgentSession is freed in Session d'tor +} + +Session::Session(const PolkitQt1::Identity &identity, const QString &cookie, AsyncResult *result, QObject *parent) + : QObject(parent) + , d(new Private) +{ + d->result = result; + d->polkitAgentSession = polkit_agent_session_new(identity.identity(), cookie.toUtf8().data()); + g_signal_connect(G_OBJECT(d->polkitAgentSession), "completed", G_CALLBACK(Private::completed), this); + g_signal_connect(G_OBJECT(d->polkitAgentSession), "request", G_CALLBACK(Private::request), this); + g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-error", G_CALLBACK(Private::showError), this); + g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-info", G_CALLBACK(Private::showInfo), this); +} + +Session::Session(PolkitAgentSession *pkAgentSession, QObject *parent) + : QObject(parent) + , d(new Private) +{ + d->polkitAgentSession = pkAgentSession; + g_signal_connect(G_OBJECT(d->polkitAgentSession), "completed", G_CALLBACK(Private::completed), this); + g_signal_connect(G_OBJECT(d->polkitAgentSession), "request", G_CALLBACK(Private::request), this); + g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-error", G_CALLBACK(Private::showError), this); + g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-info", G_CALLBACK(Private::showInfo), this); +} + +Session::~Session() +{ + if (d->polkitAgentSession) + g_object_unref(d->polkitAgentSession); + + delete d; +} + +void Session::initiate() +{ + polkit_agent_session_initiate(d->polkitAgentSession); +} + +void Session::setResponse(const QString &response) +{ + polkit_agent_session_response(d->polkitAgentSession, response.toUtf8().data()); +} + +void Session::cancel() +{ + polkit_agent_session_cancel(d->polkitAgentSession); +} + +AsyncResult *Session::result() +{ + return d->result; +} + +void Session::Private::completed(PolkitAgentSession *s, gboolean gained_authorization, gpointer user_data) +{ + qDebug() << "COMPLETED"; + Session *session = (Session *)user_data; + Q_EMIT(session)->completed(gained_authorization); + + //free session here as polkit documentation asks + g_object_unref(session->d->polkitAgentSession); + session->d->polkitAgentSession = 0; +} + +void Session::Private::request(PolkitAgentSession *s, gchar *request, gboolean echo_on, gpointer user_data) +{ + qDebug() << "REQUEST"; + Q_EMIT((Session *)user_data)->request(QString::fromUtf8(request), echo_on); +} + +void Session::Private::showError(PolkitAgentSession *s, gchar *text, gpointer user_data) +{ + qDebug() << "showError"; + Q_EMIT((Session *)user_data)->showError(QString::fromUtf8(text)); +} + +void Session::Private::showInfo(PolkitAgentSession *s, gchar *text, gpointer user_data) +{ + qDebug() << "showInfo"; + Q_EMIT((Session *)user_data)->showInfo(QString::fromUtf8(text)); +} + +// + +class AsyncResult::Private +{ +public: + Private(GSimpleAsyncResult *r) : result(r) {}; + + GSimpleAsyncResult *result; +}; + +AsyncResult::AsyncResult(GSimpleAsyncResult *result) + : d(new Private(result)) +{ +} + +AsyncResult::~AsyncResult() +{ + if (d->result) + g_object_unref(d->result); +} + +void AsyncResult::setCompleted() +{ + if (d->result == NULL) + return; + g_simple_async_result_complete(d->result); + // Assure that completed won't be called twice + g_object_unref(d->result); + d->result = NULL; +} + +void AsyncResult::setError(const QString &text) +{ + Q_ASSERT(d->result); + g_simple_async_result_set_error(d->result, POLKIT_ERROR, POLKIT_ERROR_FAILED, "%s", text.toUtf8().data()); +} + +#include "polkitqt1-agent-session.moc" diff --git a/agent/polkitqt1-agent-session.h b/agent/polkitqt1-agent-session.h new file mode 100644 index 000000000..0f763e83d --- /dev/null +++ b/agent/polkitqt1-agent-session.h @@ -0,0 +1,180 @@ +/* + * This file is part of the PolKit1-qt project + * Copyright (C) 2009 Radek Novacek <rnovacek@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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 POLKITQT1_AGENT_SESSION_H +#define POLKITQT1_AGENT_SESSION_H + +#include <QtCore/QObject> +#include "polkitqt1-identity.h" + +typedef struct _GSimpleAsyncResult GSimpleAsyncResult; +typedef struct _PolkitAgentSession PolkitAgentSession; + +namespace PolkitQt1 +{ + +/** + * \namespace Agent Agent + * + * \brief Namespace wrapping Polkit-Qt Agent classes + * + * This namespace wraps all Polkit-Qt Agent classes. + */ + +namespace Agent +{ + +/** + * \internal + * \brief Encapsulation of GSimpleAsyncResult to QObject class + */ +class POLKITQT1_EXPORT AsyncResult +{ +public: + explicit AsyncResult(GSimpleAsyncResult *result); + virtual ~AsyncResult(); + + /** + * \brief Mark the action that is tied to this result as completed. + */ + void setCompleted(); + + /** + * \brief Sets an error for the asynchronous result. + * Method complete() must be called anyway. + * + * \param text text of the error message + */ + void setError(const QString &text); + +private: + class Private; + Private * const d; +}; + +/** + * \class Session polkitqt1-agent-session.h Session + * \author Radek Novacek <rnovacek@redhat.com> + * + * This class is interface for interacting with native + * authentication system for obtaining authorizations. + * + */ +class POLKITQT1_EXPORT Session : public QObject +{ + Q_OBJECT + Q_DISABLE_COPY(Session) +public: + /** + * Create a new authentication session. + * + * \param identity The identity to authenticate + * \param cookie The cookie obtained from the PolicyKit daemon + * \param result Result of the authentication action. Must be finished using complete() method. + * \param parent + */ + Session(const PolkitQt1::Identity& identity, const QString &cookie, AsyncResult *result = 0, QObject *parent = 0); + + /** + * Create a new authentication session from PolkitAgentSession object + * + * \warning Use this only if you are completely aware of what are you doing! + * + * \param pkAgentSession PolkitAgentSession object + * \param parent + */ + explicit Session(PolkitAgentSession *pkAgentSession, QObject *parent = 0); + + /** + * Destroy authentication session. + */ + ~Session(); + + /** + * Initiate the authentication session. + * + * Use cancel() to cancel the session. + */ + void initiate(); + + /** + * Method for providing response to requests received via request signal. + * + * \param response Response from the user, typically a password + */ + void setResponse(const QString &response); + + /** + * Cancel the authentication session. + * This will emit the completed() signal. + */ + void cancel(); + + /** + * Get AsyncResult that can be used to finish authentication operation + * + * \return AsyncResult object or NULL if it is not set + */ + AsyncResult *result(); + +Q_SIGNALS: + /** + * This signal will be emitted when the authentication + * polkitqt1-agent-session.has been completed or cancelled. + * + * \param gainedAuthorization \c True if authorization was successfully obtained. + */ + void completed(bool gainedAuthorization); + + /** + * This signal will be emitted when user is requested to answer a question. + * + * \param request The request to show the user, e.g. "name: " or "password: ". + * \param echo \c True if the response to the request SHOULD be echoed on the screen, + * \c False if the response MUST NOT be echoed to the screen. + */ + void request(const QString &request, bool echo); + + /** + * This signal will be emitted when there is information + * related to an error condition to be displayed to the user. + * + * \param text An error string to display to the user. + */ + void showError(const QString &text); + + /** + * This signal will be emitted when there is information + * to be displayed to the user. + * + * \param text A string to be displayed to the user. + */ + void showInfo(const QString &text); + +private: + class Private; + Private * const d; +}; + +} + +} + +#endif // SESSION_H diff --git a/agent/polkitqtlistener.cpp b/agent/polkitqtlistener.cpp new file mode 100644 index 000000000..a9a93aad1 --- /dev/null +++ b/agent/polkitqtlistener.cpp @@ -0,0 +1,150 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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. + * + * polkit-qt-listener based on code by David Zeuthen <davidz@redhat.com> + */ + + +#include "polkitqtlistener_p.h" +#include <stdio.h> + +#include <QtCore/QDebug> + +#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE 1 + +using namespace PolkitQt1::Agent; + +/** + * \internal + */ +struct _PolkitQtListener { + PolkitAgentListener parent_instance; +}; + +/** + * \internal + */ +struct _PolkitQtListenerClass { + PolkitAgentListenerClass parent_class; +}; + +static void polkit_qt_listener_initiate_authentication(PolkitAgentListener *listener, + const gchar *action_id, + const gchar *message, + const gchar *icon_name, + PolkitDetails *details, + const gchar *cookie, + GList *identities, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); + +static gboolean polkit_qt_listener_initiate_authentication_finish(PolkitAgentListener *listener, + GAsyncResult *res, + GError **error); + +G_DEFINE_TYPE(PolkitQtListener, polkit_qt_listener, POLKIT_AGENT_TYPE_LISTENER); + +static void polkit_qt_listener_init(PolkitQtListener *listener) +{ + g_type_init(); +} + +static void polkit_qt_listener_finalize(GObject *object) +{ + PolkitQtListener *listener; + + listener = POLKIT_QT_LISTENER(object); + + if (G_OBJECT_CLASS(polkit_qt_listener_parent_class)->finalize != NULL) { + G_OBJECT_CLASS(polkit_qt_listener_parent_class)->finalize(object); + } +} + +static void polkit_qt_listener_class_init(PolkitQtListenerClass *klass) +{ + GObjectClass *gobject_class; + PolkitAgentListenerClass *listener_class; + + gobject_class = G_OBJECT_CLASS(klass); + listener_class = POLKIT_AGENT_LISTENER_CLASS(klass); + + gobject_class->finalize = polkit_qt_listener_finalize; + + listener_class->initiate_authentication = polkit_qt_listener_initiate_authentication; + listener_class->initiate_authentication_finish = polkit_qt_listener_initiate_authentication_finish; +} + +PolkitAgentListener *polkit_qt_listener_new(void) +{ + return POLKIT_AGENT_LISTENER(g_object_new(POLKIT_QT_TYPE_LISTENER, NULL)); +} + +static void cancelled_cb(GCancellable *cancellable, gpointer user_data) +{ + ListenerAdapter::instance()->cancelled_cb((PolkitAgentListener *)user_data); +} + +static void polkit_qt_listener_initiate_authentication(PolkitAgentListener *agent_listener, + const gchar *action_id, + const gchar *message, + const gchar *icon_name, + PolkitDetails *details, + const gchar *cookie, + GList *identities, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + qDebug() << "Listener adapter polkit_qt_listener_initiate_authentication"; + PolkitQtListener *listener = POLKIT_QT_LISTENER(agent_listener); + + // The result of asynchronous method will be created here and it will be pushed to the listener. + GSimpleAsyncResult *result = g_simple_async_result_new((GObject *) listener, callback, user_data, agent_listener); + qDebug() << "GSimpleAsyncResult:" << result; + + ListenerAdapter::instance()->polkit_qt_listener_initiate_authentication(agent_listener, + action_id, + message, + icon_name, + details, + cookie, + identities, + cancellable, + result); + + if (cancellable != NULL) { + g_signal_connect(cancellable, + "cancelled", + G_CALLBACK(cancelled_cb), + agent_listener); + } + +} + +static gboolean polkit_qt_listener_initiate_authentication_finish(PolkitAgentListener *listener, + GAsyncResult *res, + GError **error) +{ + qDebug() << "Listener adapter polkit_qt_listener_initiate_authentication_finish"; + return ListenerAdapter::instance()->polkit_qt_listener_initiate_authentication_finish(listener, + res, + error); +} + diff --git a/agent/polkitqtlistener_p.h b/agent/polkitqtlistener_p.h new file mode 100644 index 000000000..1447691d2 --- /dev/null +++ b/agent/polkitqtlistener_p.h @@ -0,0 +1,49 @@ +/* + * This file is part of the Polkit-qt project + * Copyright (C) 2009 Jaroslav Reznik <jreznik@redhat.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * 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. + * + * polkit-qt-listener based on code by David Zeuthen <davidz@redhat.com> + */ + +#ifndef POLKITQT_LISTENER_P_H +#define POLKITQT_LISTENER_P_H + +#define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE 1 + +#include "listeneradapter_p.h" + +#include <polkitagent/polkitagent.h> + +G_BEGIN_DECLS + +#define POLKIT_QT_TYPE_LISTENER (polkit_qt_listener_get_type()) +#define POLKIT_QT_LISTENER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), POLKIT_QT_TYPE_LISTENER, PolkitQtListener)) +#define POLKIT_QT_LISTENER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), POLKIT_QT_TYPE_LISTENER, PolkitQtListenerClass)) +#define POLKIT_QT_LISTENER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), POLKIT_QT_TYPE_LISTENER, PolkitQtListenerClass)) +#define POLKIT_QT_IS_LISTENER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), POLKIT_QT_TYPE_LISTENER)) +#define POLKIT_QT_IS_LISTENER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), POLKIT_QT_TYPE_LISTENER)) + +typedef struct _PolkitQtListener PolkitQtListener; +typedef struct _PolkitQtListenerClass PolkitQtListenerClass; + +GType polkit_qt_listener_get_type(void) G_GNUC_CONST; +PolkitAgentListener *polkit_qt_listener_new(void); + +G_END_DECLS + +#endif /* POLKIT_QT_LISTENER_H */ |
