PolkitQt-1

polkitqt1-identity.cpp
1 /*
2  This file is part of the Polkit-qt project
3  SPDX-FileCopyrightText: 2009 Lukas Tinkl <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "polkitqt1-identity.h"
9 
10 #include <polkit/polkit.h>
11 
12 #include <QDebug>
13 
14 namespace PolkitQt1
15 {
16 
17 class Q_DECL_HIDDEN Identity::Data : public QSharedData
18 {
19 public:
20  Data() : identity(nullptr) {}
21  Data(const Data& other)
22  : QSharedData(other)
23  , identity(other.identity)
24  {
25  if (identity) {
26  g_object_ref(identity);
27  }
28  }
29  ~Data()
30  {
31  if (identity) {
32  g_object_unref(identity);
33  }
34  }
35 
36  PolkitIdentity *identity;
37 };
38 
39 Identity::Identity()
40  : d(new Data)
41 {
42 }
43 
44 Identity::Identity(PolkitIdentity *polkitIdentity)
45  : d(new Data)
46 {
47  d->identity = polkitIdentity;
48 
49  if (d->identity) {
50  g_object_ref(d->identity);
51  }
52 }
53 
54 Identity::Identity(const PolkitQt1::Identity& other)
55  : d(other.d)
56 {
57 
58 }
59 
60 Identity::~Identity()
61 {
62 }
63 
64 Identity& Identity::operator=(const PolkitQt1::Identity& other)
65 {
66  d = other.d;
67  return *this;
68 }
69 
70 bool Identity::isValid() const
71 {
72  return d->identity != nullptr;
73 }
74 
75 PolkitIdentity *Identity::identity() const
76 {
77  return d->identity;
78 }
79 
80 void Identity::setIdentity(PolkitIdentity *identity)
81 {
82  if (d->identity == identity) {
83  return;
84  }
85 
86  if (d->identity) {
87  g_object_unref(d->identity);
88  }
89 
90  d->identity = identity;
91 
92  if (d->identity) {
93  g_object_ref(d->identity);
94  }
95 }
96 
98 {
99  Q_ASSERT(d->identity);
100  return QString::fromUtf8(polkit_identity_to_string(d->identity));
101 }
102 
104 {
105  GError *error = nullptr;
106  PolkitIdentity *pkIdentity = polkit_identity_from_string(string.toUtf8().data(), &error);
107  if (error != nullptr) {
108  qWarning() << QString("Cannot create Identity from string: %1").arg(error->message);
109  return Identity();
110  }
111  return Identity(pkIdentity);
112 }
113 
114 UnixGroupIdentity Identity::toUnixGroupIdentity()
115 {
116  UnixGroupIdentity *ugid = static_cast< UnixGroupIdentity* >(this);
117  if (!ugid) {
118  return UnixGroupIdentity();
119  }
120 
121  return *ugid;
122 }
123 
124 UnixUserIdentity Identity::toUnixUserIdentity()
125 {
126  UnixUserIdentity *uuid = static_cast< UnixUserIdentity* >(this);
127  if (!uuid) {
128  return UnixUserIdentity();
129  }
130 
131  return *uuid;
132 }
133 
134 UnixUserIdentity::UnixUserIdentity(const QString &name)
135  : Identity()
136 {
137  GError *error = nullptr;
138  setIdentity(polkit_unix_user_new_for_name(name.toUtf8().data(), &error));
139  if (error != nullptr) {
140  qWarning() << QString("Cannot create UnixUserIdentity: %1").arg(error->message);
141  setIdentity(nullptr);
142  }
143 }
144 
145 UnixUserIdentity::UnixUserIdentity(uid_t uid)
146  : Identity()
147 {
148  setIdentity(polkit_unix_user_new(uid));
149 }
150 
151 UnixUserIdentity::UnixUserIdentity(PolkitUnixUser *pkUnixUser)
152  : Identity((PolkitIdentity *)pkUnixUser)
153 {
154 
155 }
156 
157 UnixUserIdentity::UnixUserIdentity()
158  : Identity()
159 {
160 
161 }
162 
164 {
165  return polkit_unix_user_get_uid((PolkitUnixUser *) identity());
166 }
167 
169 {
170  polkit_unix_user_set_uid((PolkitUnixUser *) identity(), uid);
171 }
172 
173 UnixGroupIdentity::UnixGroupIdentity(const QString &name)
174  : Identity()
175 {
176  GError *error = nullptr;
177  setIdentity(polkit_unix_group_new_for_name(name.toUtf8().data(), &error));
178  if (error != nullptr) {
179  qWarning() << QString("Cannot create UnixGroupIdentity: %1").arg(error->message);
180  setIdentity(nullptr);
181  }
182 }
183 
184 UnixGroupIdentity::UnixGroupIdentity(gid_t gid)
185  : Identity()
186 {
187  setIdentity(polkit_unix_group_new(gid));
188 }
189 
190 UnixGroupIdentity::UnixGroupIdentity(PolkitUnixGroup *pkUnixGroup)
191  : Identity((PolkitIdentity *) pkUnixGroup)
192 {
193 
194 }
195 
196 UnixGroupIdentity::UnixGroupIdentity()
197  : Identity()
198 {
199 
200 }
201 
203 {
204  return polkit_unix_group_get_gid((PolkitUnixGroup *) identity());
205 }
206 
208 {
209  polkit_unix_group_set_gid((PolkitUnixGroup *) identity(), gid);
210 }
211 
212 }
void setUid(uid_t uid)
Sets the id of user.
QString fromUtf8(const char *str, int size)
QString toString() const
Serialization of object to the string.
Abstract class representing identities.
static Identity fromString(const QString &string)
Creates the Identity object from string representation.
uid_t uid() const
Gets an user id.
void setGid(gid_t gid)
Sets the id of group.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
gid_t gid() const
Gets a group id.
Namespace wrapping Polkit-Qt classes.
PolkitIdentity * identity() const
Gets PolkitIdentity object.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Jun 26 2022 04:06:09 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.