File indexing completed on 2024-04-21 04:43:22

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 #ifndef POLKITQT1_AGENT_SESSION_H
0009 #define POLKITQT1_AGENT_SESSION_H
0010 
0011 #include <QObject>
0012 #include "polkitqt1-identity.h"
0013 #include "polkitqt1-agent-export.h"
0014 
0015 typedef struct _GSimpleAsyncResult GSimpleAsyncResult;
0016 typedef struct _PolkitAgentSession PolkitAgentSession;
0017 
0018 namespace PolkitQt1
0019 {
0020 
0021 /**
0022  * \namespace Agent Agent
0023  *
0024  * \brief Namespace wrapping Polkit-Qt Agent classes
0025  *
0026  * This namespace wraps all Polkit-Qt Agent classes.
0027  */
0028 
0029 namespace Agent
0030 {
0031 
0032 /**
0033  * \internal
0034  * \brief Encapsulation of GSimpleAsyncResult to QObject class
0035  */
0036 class POLKITQT1_AGENT_EXPORT AsyncResult
0037 {
0038 public:
0039     explicit AsyncResult(GSimpleAsyncResult *result);
0040     virtual ~AsyncResult();
0041 
0042     /**
0043      * \brief Mark the action that is tied to this result as completed.
0044      */
0045     void setCompleted();
0046 
0047     /**
0048      * \brief Sets an error for the asynchronous result.
0049      * Method complete() must be called anyway.
0050      *
0051      * \param text text of the error message
0052      */
0053     void setError(const QString &text);
0054 
0055 private:
0056     class Private;
0057     Private * const d;
0058 };
0059 
0060 /**
0061  * \class Session polkitqt1-agent-session.h Session
0062  * \author Radek Novacek <rnovacek@redhat.com>
0063  *
0064  * This class is interface for interacting with native
0065  * authentication system for obtaining authorizations.
0066  *
0067  */
0068 class POLKITQT1_AGENT_EXPORT Session : public QObject
0069 {
0070     Q_OBJECT
0071     Q_DISABLE_COPY(Session)
0072 public:
0073     /**
0074      * Create a new authentication session.
0075      *
0076      * \param identity The identity to authenticate
0077      * \param cookie The cookie obtained from the PolicyKit daemon
0078      * \param result Result of the authentication action. Must be finished using complete() method.
0079      * \param parent
0080      */
0081     Session(const PolkitQt1::Identity& identity, const QString &cookie, AsyncResult *result = nullptr, QObject *parent = nullptr);
0082 
0083     /**
0084      * Create a new authentication session from PolkitAgentSession object
0085      *
0086      * \warning Use this only if you are completely aware of what are you doing!
0087      *
0088      * \param pkAgentSession PolkitAgentSession object
0089      * \param parent
0090      */
0091     explicit Session(PolkitAgentSession *pkAgentSession, QObject *parent = nullptr);
0092 
0093     /**
0094      * Destroy authentication session.
0095      */
0096     ~Session() override;
0097 
0098     /**
0099      * Initiate the authentication session.
0100      *
0101      * Use cancel() to cancel the session.
0102      */
0103     void initiate();
0104 
0105     /**
0106      * Method for providing response to requests received via request signal.
0107      *
0108      * \param response Response from the user, typically a password
0109      */
0110     void setResponse(const QString &response);
0111 
0112     /**
0113      * Cancel the authentication session.
0114      * This will emit the completed() signal.
0115      */
0116     void cancel();
0117 
0118     /**
0119      * Get AsyncResult that can be used to finish authentication operation
0120      *
0121      * \return AsyncResult object or NULL if it is not set
0122      */
0123     AsyncResult *result();
0124 
0125 Q_SIGNALS:
0126     /**
0127      * This signal will be emitted when the authentication
0128      * polkitqt1-agent-session.has been completed or cancelled.
0129      *
0130      * \param gainedAuthorization \c True if authorization was successfully obtained.
0131      */
0132     void completed(bool gainedAuthorization);
0133 
0134     /**
0135      * This signal will be emitted when user is requested to answer a question.
0136      *
0137      * \param request The request to show the user, e.g. "name: " or "password: ".
0138      * \param echo \c True if the response to the request SHOULD be echoed on the screen,
0139      *             \c False if the response MUST NOT be echoed to the screen.
0140      */
0141     void request(const QString &request, bool echo);
0142 
0143     /**
0144      * This signal will be emitted when there is information
0145      * related to an error condition to be displayed to the user.
0146      *
0147      * \param text An error string to display to the user.
0148      */
0149     void showError(const QString &text);
0150 
0151     /**
0152      * This signal will be emitted when there is information
0153      * to be displayed to the user.
0154      *
0155      * \param text A string to be displayed to the user.
0156      */
0157     void showInfo(const QString &text);
0158 
0159 private:
0160     class Private;
0161     Private * const d;
0162 };
0163 
0164 }
0165 
0166 }
0167 
0168 #endif // SESSION_H