PolkitQt-1

polkitqt1-subject.cpp
1 /*
2  This file is part of the Polkit-qt project
3  SPDX-FileCopyrightText: 2009 Jaroslav Reznik <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "polkitqt1-subject.h"
9 #include "polkitqt1-identity.h"
10 #include "polkitqt1-config.h"
11 
12 #include <QDebug>
13 #include <polkit/polkit.h>
14 
15 namespace PolkitQt1
16 {
17 
18 class Q_DECL_HIDDEN Subject::Data : public QSharedData
19 {
20 public:
21  Data()
22  : QSharedData()
23  , subject(nullptr)
24  {}
25  Data(const Data& other)
26  : QSharedData(other)
27  , subject(other.subject)
28  {
29  g_object_ref(subject);
30  }
31  ~Data()
32  {
33  g_object_unref(subject);
34  }
35 
36  PolkitSubject *subject;
37 };
38 
39 Subject::Subject()
40  : d(new Data)
41 {
42 }
43 
44 Subject::Subject(PolkitSubject *subject)
45  : d(new Data)
46 {
47  d->subject = subject;
48 
49  if (d->subject != nullptr) {
50  g_object_ref(d->subject);
51  }
52 }
53 
54 Subject::Subject(const PolkitQt1::Subject& other)
55  : d(other.d)
56 {
57 
58 }
59 
60 Subject& Subject::operator=(const PolkitQt1::Subject& other)
61 {
62  d = other.d;
63  return *this;
64 }
65 
66 Subject::~Subject()
67 {
68 }
69 
70 bool Subject::isValid() const
71 {
72  return d->subject != nullptr;
73 }
74 
75 PolkitSubject *Subject::subject() const
76 {
77  return d->subject;
78 }
79 
80 void Subject::setSubject(PolkitSubject *subject)
81 {
82  if (d->subject != nullptr) {
83  g_object_unref(d->subject);
84  }
85  d->subject = subject;
86 }
87 
89 {
90  Q_ASSERT(d->subject);
91  return QString::fromUtf8(polkit_subject_to_string(d->subject));
92 }
93 
95 {
97  GError *error = nullptr;
98  subject.d->subject = polkit_subject_from_string(string.toUtf8().data(), &error);
99  if (error != nullptr) {
100  qWarning() << QString("Cannot create Subject from string: %1").arg(error->message);
101  return nullptr;
102  }
103  return subject;
104 }
105 
107  : Subject()
108 {
109  setSubject(polkit_unix_process_new_for_owner(pid, 0, -1));
110 }
111 
112 UnixProcessSubject::UnixProcessSubject(qint64 pid, quint64 startTime)
113  : Subject()
114 {
115  setSubject(polkit_unix_process_new_for_owner(pid, startTime, -1));
116 }
117 
118 UnixProcessSubject::UnixProcessSubject(PolkitUnixProcess *pkUnixProcess)
119  : Subject((PolkitSubject *) pkUnixProcess)
120 {
121 
122 }
123 
125 {
126  return polkit_unix_process_get_pid((PolkitUnixProcess *) subject());
127 }
128 
130 {
131  return polkit_unix_process_get_start_time((PolkitUnixProcess *) subject());
132 }
133 
135 {
136  return polkit_unix_process_get_uid((PolkitUnixProcess *) subject());
137 }
138 
140 {
141  polkit_unix_process_set_pid((PolkitUnixProcess *) subject(), pid);
142 }
143 
144 // ----- SystemBusName
146  : Subject()
147 {
148  setSubject(polkit_system_bus_name_new(name.toUtf8().data()));
149 }
150 
151 SystemBusNameSubject::SystemBusNameSubject(PolkitSystemBusName *pkSystemBusName)
152  : Subject((PolkitSubject *) pkSystemBusName)
153 {
154 
155 }
156 
158 {
159  return QString::fromUtf8(polkit_system_bus_name_get_name((PolkitSystemBusName *) subject()));
160 }
161 
163 {
164  polkit_system_bus_name_set_name((PolkitSystemBusName *) subject(), name.toUtf8().data());
165 }
166 
168 {
169 #if HAVE_POLKIT_SYSTEM_BUS_NAME_GET_USER_SYNC
170  return UnixUserIdentity(polkit_system_bus_name_get_user_sync((PolkitSystemBusName *) subject(), nullptr, nullptr));
171 #else
172  qWarning("Polkit is too old, returning invalid user from SystemBusNameSubject::user()!");
173  return UnixUserIdentity();
174 #endif
175 }
176 
177 // ----- SystemSession
179  : Subject()
180 {
181  setSubject(polkit_unix_session_new(sessionId.toUtf8().data()));
182 }
183 
185  : Subject()
186 {
187  GError *error = nullptr;
188  setSubject(polkit_unix_session_new_for_process_sync(pid, nullptr, &error));
189  if (error != nullptr) {
190  qWarning() << QString("Cannot create unix session: %1").arg(error->message);
191  setSubject(nullptr);
192  }
193 }
194 
195 UnixSessionSubject::UnixSessionSubject(PolkitSystemBusName *pkUnixSession)
196  : Subject((PolkitSubject *) pkUnixSession)
197 {
198 
199 }
200 
202 {
203  return QString::fromUtf8(polkit_unix_session_get_session_id((PolkitUnixSession *) subject()));
204 }
205 
207 {
208  polkit_unix_session_set_session_id((PolkitUnixSession *) subject(), sessionId.toUtf8().data());
209 }
210 
211 }
This class represents PolicyKit subjects.
qint64 startTime() const
Returns Unix process start time.
QString toString() const
Serialization of object to the string.
QString fromUtf8(const char *str, int size)
QString name() const
Returns system bus name.
Subject
UnixSessionSubject(const QString &sessionId)
Subject constructor, takes one parameter - session id.
void setName(const QString &name)
Sets system bus name.
UnixProcessSubject(qint64 pid)
Subject constructor, takes one parameter - PID.
QByteArray toUtf8() const const
PolkitSubject * subject() const
Gets PolkitSubject object.
void setPid(qint64 pid)
Sets Unix process PID.
QString sessionId() const
Returns session id.
qint64 uid() const
Returns Unix process UID.
qint64 pid() const
Returns Unix process PID.
UnixUserIdentity user()
Returns the UnixUserIdentity for this subject.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
SystemBusNameSubject(const QString &name)
Subject constructor, takes one parameter - system bus name.
Namespace wrapping Polkit-Qt classes.
void setSessionId(const QString &sessionId)
Sets session id.
static Subject fromString(const QString &string)
Creates the Subject object from string reprezentation.
char * data()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Dec 7 2023 03:58:11 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.