PolkitQt-1

polkitqt1-identity.cpp
1/*
2 This file is part of the Polkit-qt project
3 SPDX-FileCopyrightText: 2009 Lukas Tinkl <ltinkl@redhat.com>
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
14namespace PolkitQt1
15{
16
17class Q_DECL_HIDDEN Identity::Data : public QSharedData
18{
19public:
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
39Identity::Identity()
40 : d(new Data)
41{
42}
43
44Identity::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
54Identity::Identity(const PolkitQt1::Identity& other)
55 : d(other.d)
56{
57
58}
59
60Identity::~Identity()
61{
62}
63
64Identity& Identity::operator=(const PolkitQt1::Identity& other)
65{
66 d = other.d;
67 return *this;
68}
69
70bool Identity::isValid() const
71{
72 return d->identity != nullptr;
73}
74
75PolkitIdentity *Identity::identity() const
76{
77 return d->identity;
78}
79
80void 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
97QString Identity::toString() const
98{
99 Q_ASSERT(d->identity);
100 return QString::fromUtf8(polkit_identity_to_string(d->identity));
101}
102
103Identity Identity::fromString(const QString &string)
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
114UnixGroupIdentity Identity::toUnixGroupIdentity()
115{
116 UnixGroupIdentity *ugid = static_cast< UnixGroupIdentity* >(this);
117 if (!ugid) {
118 return UnixGroupIdentity();
119 }
120
121 return *ugid;
122}
123
124UnixUserIdentity Identity::toUnixUserIdentity()
125{
126 UnixUserIdentity *uuid = static_cast< UnixUserIdentity* >(this);
127 if (!uuid) {
128 return UnixUserIdentity();
129 }
130
131 return *uuid;
132}
133
134UnixUserIdentity::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
145UnixUserIdentity::UnixUserIdentity(uid_t uid)
146 : Identity()
147{
148 setIdentity(polkit_unix_user_new(uid));
149}
150
151UnixUserIdentity::UnixUserIdentity(PolkitUnixUser *pkUnixUser)
152 : Identity((PolkitIdentity *)pkUnixUser)
153{
154
155}
156
157UnixUserIdentity::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
173UnixGroupIdentity::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
184UnixGroupIdentity::UnixGroupIdentity(gid_t gid)
185 : Identity()
186{
187 setIdentity(polkit_unix_group_new(gid));
188}
189
190UnixGroupIdentity::UnixGroupIdentity(PolkitUnixGroup *pkUnixGroup)
191 : Identity((PolkitIdentity *) pkUnixGroup)
192{
193
194}
195
196UnixGroupIdentity::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}
Abstract class representing identities.
PolkitIdentity * identity() const
Gets PolkitIdentity object.
An object representing a group identity on a UNIX system.
gid_t gid() const
Gets a group id.
void setGid(gid_t gid)
Sets the id of group.
uid_t uid() const
Gets an user id.
void setUid(uid_t uid)
Sets the id of user.
char * data()
QString arg(Args &&... args) const const
QString fromUtf8(QByteArrayView str)
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:35 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.