File indexing completed on 2024-04-28 12:31:56
0001 /* 0002 This file is part of the PolKit1-qt project 0003 SPDX-FileCopyrightText: 2009 Radek Novacek <rnovacek@redhat.com> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "polkitqt1-agent-session.h" 0009 0010 #include <QDebug> 0011 0012 #include "polkitqt1-identity.h" 0013 0014 #define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE 1 0015 #include <polkitagent/polkitagent.h> 0016 0017 using namespace PolkitQt1::Agent; 0018 0019 class Session::Private 0020 { 0021 public: 0022 Private() {} 0023 ~Private(); 0024 0025 static void completed(PolkitAgentSession *s, gboolean gained_authorization, gpointer user_data); 0026 static void request(PolkitAgentSession *s, gchar *request, gboolean echo_on, gpointer user_data); 0027 static void showError(PolkitAgentSession *s, gchar *text, gpointer user_data); 0028 static void showInfo(PolkitAgentSession *s, gchar *text, gpointer user_data); 0029 0030 AsyncResult *result; 0031 PolkitAgentSession *polkitAgentSession; 0032 }; 0033 0034 Session::Private::~Private() 0035 { 0036 // polkitAgentSession is freed in Session d'tor 0037 } 0038 0039 Session::Session(const PolkitQt1::Identity &identity, const QString &cookie, AsyncResult *result, QObject *parent) 0040 : QObject(parent) 0041 , d(new Private) 0042 { 0043 d->result = result; 0044 d->polkitAgentSession = polkit_agent_session_new(identity.identity(), cookie.toUtf8().data()); 0045 g_signal_connect(G_OBJECT(d->polkitAgentSession), "completed", G_CALLBACK(Private::completed), this); 0046 g_signal_connect(G_OBJECT(d->polkitAgentSession), "request", G_CALLBACK(Private::request), this); 0047 g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-error", G_CALLBACK(Private::showError), this); 0048 g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-info", G_CALLBACK(Private::showInfo), this); 0049 } 0050 0051 Session::Session(PolkitAgentSession *pkAgentSession, QObject *parent) 0052 : QObject(parent) 0053 , d(new Private) 0054 { 0055 d->polkitAgentSession = pkAgentSession; 0056 if (d->polkitAgentSession) { 0057 g_object_ref(d->polkitAgentSession); 0058 } 0059 g_signal_connect(G_OBJECT(d->polkitAgentSession), "completed", G_CALLBACK(Private::completed), this); 0060 g_signal_connect(G_OBJECT(d->polkitAgentSession), "request", G_CALLBACK(Private::request), this); 0061 g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-error", G_CALLBACK(Private::showError), this); 0062 g_signal_connect(G_OBJECT(d->polkitAgentSession), "show-info", G_CALLBACK(Private::showInfo), this); 0063 } 0064 0065 Session::~Session() 0066 { 0067 if (d->polkitAgentSession) 0068 g_object_unref(d->polkitAgentSession); 0069 0070 delete d; 0071 } 0072 0073 void Session::initiate() 0074 { 0075 polkit_agent_session_initiate(d->polkitAgentSession); 0076 } 0077 0078 void Session::setResponse(const QString &response) 0079 { 0080 polkit_agent_session_response(d->polkitAgentSession, response.toUtf8().data()); 0081 } 0082 0083 void Session::cancel() 0084 { 0085 polkit_agent_session_cancel(d->polkitAgentSession); 0086 } 0087 0088 AsyncResult *Session::result() 0089 { 0090 return d->result; 0091 } 0092 0093 void Session::Private::completed(PolkitAgentSession *s, gboolean gained_authorization, gpointer user_data) 0094 { 0095 qDebug() << "COMPLETED"; 0096 Session *session = (Session *)user_data; 0097 Q_EMIT(session)->completed(gained_authorization); 0098 0099 //free session here as polkit documentation asks 0100 g_object_unref(session->d->polkitAgentSession); 0101 session->d->polkitAgentSession = nullptr; 0102 } 0103 0104 void Session::Private::request(PolkitAgentSession *s, gchar *request, gboolean echo_on, gpointer user_data) 0105 { 0106 qDebug() << "REQUEST"; 0107 Q_EMIT((Session *)user_data)->request(QString::fromUtf8(request), echo_on); 0108 } 0109 0110 void Session::Private::showError(PolkitAgentSession *s, gchar *text, gpointer user_data) 0111 { 0112 qDebug() << "showError"; 0113 Q_EMIT((Session *)user_data)->showError(QString::fromUtf8(text)); 0114 } 0115 0116 void Session::Private::showInfo(PolkitAgentSession *s, gchar *text, gpointer user_data) 0117 { 0118 qDebug() << "showInfo"; 0119 Q_EMIT((Session *)user_data)->showInfo(QString::fromUtf8(text)); 0120 } 0121 0122 // 0123 0124 class AsyncResult::Private 0125 { 0126 public: 0127 Private(GSimpleAsyncResult *r) : result(r) {}; 0128 0129 GSimpleAsyncResult *result; 0130 }; 0131 0132 AsyncResult::AsyncResult(GSimpleAsyncResult *result) 0133 : d(new Private(result)) 0134 { 0135 } 0136 0137 AsyncResult::~AsyncResult() 0138 { 0139 if (d->result) 0140 g_object_unref(d->result); 0141 } 0142 0143 void AsyncResult::setCompleted() 0144 { 0145 if (d->result == nullptr) 0146 return; 0147 g_simple_async_result_complete(d->result); 0148 // Assure that completed won't be called twice 0149 g_object_unref(d->result); 0150 d->result = nullptr; 0151 } 0152 0153 void AsyncResult::setError(const QString &text) 0154 { 0155 Q_ASSERT(d->result); 0156 g_simple_async_result_set_error(d->result, POLKIT_ERROR, POLKIT_ERROR_FAILED, "%s", text.toUtf8().data()); 0157 }