Changeset View
Changeset View
Standalone View
Standalone View
src/util/externalcommand_polkitbackend.h
- This file was added.
1 | /************************************************************************* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 by Shubham <aryan100jangid@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or * | ||||
5 | * modify it under the terms of the GNU General Public License as * | ||||
6 | * published by the Free Software Foundation; either version 3 of * | ||||
7 | * the License, or (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>.* | ||||
16 | *************************************************************************/ | ||||
17 | | ||||
18 | #ifndef KPMCORE_EXTERNALCOMMAND_POLKITBACKEND_H | ||||
19 | #define KPMCORE_EXTERNALCOMMAND_POLKITBACKEND_H | ||||
20 | | ||||
21 | #include <QEventLoop> | ||||
22 | #include <QHash> | ||||
23 | #include <QObject> | ||||
24 | #include <QString> | ||||
25 | #include <QVariant> | ||||
26 | | ||||
27 | #include <PolkitQt1/Authority> | ||||
28 | | ||||
29 | using namespace PolkitQt1; | ||||
30 | | ||||
31 | namespace Auth | ||||
32 | { | ||||
33 | | ||||
34 | /** A Polkit Qt backend class for authorizing actions. | ||||
35 | | ||||
36 | This class is used to authorize various actions if they | ||||
37 | ask for privileged execution. It starts by verifying the | ||||
38 | action under consideration if it is the one it is | ||||
39 | saying and authorizes it based on the credentials | ||||
40 | provided. | ||||
41 | | ||||
42 | @author Shubham <aryan100jangid@gmail.com> | ||||
43 | **/ | ||||
44 | class PolkitQt1Backend : public QObject | ||||
45 | { | ||||
46 | Q_OBJECT | ||||
47 | Q_DISABLE_COPY(PolkitQt1Backend) | ||||
48 | | ||||
49 | public: | ||||
50 | /** | ||||
51 | * \brief Constructor of PolkitQt1Backend class | ||||
52 | */ | ||||
53 | PolkitQt1Backend(); | ||||
54 | | ||||
55 | /** | ||||
56 | * \brief Destructor of PolkitQt1Backend class | ||||
57 | */ | ||||
58 | ~PolkitQt1Backend(); | ||||
59 | | ||||
60 | /** | ||||
61 | * \brief Initializes the KDE Polkit Authentication Agent. | ||||
62 | * | ||||
63 | * \param action Action in question | ||||
64 | * \param parent Parent widget | ||||
65 | * | ||||
66 | */ | ||||
67 | void initPolkitAgent(const QString &action, QWidget *parent = nullptr) const; | ||||
68 | | ||||
69 | /** | ||||
70 | * \brief A function to check for the action's current status. | ||||
71 | * | ||||
72 | * \param action Action in question | ||||
73 | * \param calledID The Application process ID of the action | ||||
74 | * | ||||
75 | * \return the result of action status ie. If action is Authorized or not. | ||||
76 | */ | ||||
77 | Authority::Result actionStatus(const QString &action, const QByteArray &callerID) const; | ||||
78 | | ||||
79 | /** | ||||
80 | * \brief Function to get the current Application process ID | ||||
81 | * | ||||
82 | * \return Application process ID of the action | ||||
83 | */ | ||||
84 | QByteArray callerID() const; | ||||
85 | | ||||
86 | /** | ||||
87 | * \brief Tries to authorize to the \p action in question. | ||||
88 | * | ||||
89 | * \param action Action in question. | ||||
90 | * \param callerID The Application process ID of the action | ||||
91 | * | ||||
92 | * \return \c true if authority authorizes the action successfully, \c false Action is not authorized. | ||||
93 | * | ||||
94 | */ | ||||
95 | bool authorizeAction(const QString &action, const QByteArray &callerID); | ||||
96 | | ||||
97 | /** | ||||
98 | * \brief Stops the running \p action from executing. | ||||
99 | * | ||||
100 | * \param action Action in question. | ||||
101 | * | ||||
102 | */ | ||||
103 | bool revokeAuthorization(const QString &action, const QByteArray &callerID); | ||||
104 | | ||||
105 | public Q_SLOTS: | ||||
106 | void authStatusChanged(); | ||||
107 | | ||||
108 | private: | ||||
109 | QHash<QString, Authority::Result> m_cachedResults; | ||||
110 | bool m_flyingActions; // Already running actions | ||||
111 | }; | ||||
112 | | ||||
113 | /** A Polkit event loop class. | ||||
114 | | ||||
115 | This class is used to implement a polkit event | ||||
116 | loop and has the capability of returning the | ||||
117 | current authorization result of the action in | ||||
118 | que. | ||||
119 | | ||||
120 | @author Shubham <aryan100jangid@gmail.com> | ||||
121 | **/ | ||||
122 | class PolkitEventLoop : public QEventLoop | ||||
123 | { | ||||
124 | Q_OBJECT | ||||
125 | | ||||
126 | public: | ||||
127 | PolkitEventLoop(QObject *parent = nullptr); | ||||
128 | ~PolkitEventLoop(); | ||||
129 | | ||||
130 | Authority::Result result() const; | ||||
131 | | ||||
132 | public: | ||||
133 | static Authority::Result m_result; | ||||
134 | }; | ||||
135 | | ||||
136 | } // namespace Auth | ||||
137 | | ||||
138 | #endif // KPMCORE_EXTERNALCOMMAND_POLKITBACKEND_H |