KAuth

objectdecorator.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2009-2012 Dario Freddi <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "objectdecorator.h"
9 
10 #include "action.h"
11 #include "executejob.h"
12 #include "kauthdebug.h"
13 
14 #include <QAbstractButton>
15 #include <QAction>
16 #include <QIcon>
17 
18 namespace KAuth
19 {
20 class ObjectDecoratorPrivate
21 {
22 public:
23  ObjectDecoratorPrivate(ObjectDecorator *parent)
24  : q(parent)
25  , decoratedObject(parent->parent())
26  {
27  }
28 
29  ObjectDecorator *const q;
30 
31  QObject *const decoratedObject;
32  KAuth::Action authAction;
33  // TODO: Remove whenever QIcon overlays will get fixed
34  QIcon oldIcon;
35 
36  void connectDecorated();
37  void linkActionToWidget();
38  void slotActivated();
39  void authStatusChanged(KAuth::Action::AuthStatus status);
40 };
41 
42 void ObjectDecoratorPrivate::connectDecorated()
43 {
44  if (auto *button = qobject_cast<QAbstractButton *>(decoratedObject)) {
45  q->connect(button, &QAbstractButton::clicked, q, [this] {
46  slotActivated();
47  });
48  return;
49  }
50 
51  if (auto *action = qobject_cast<QAction *>(decoratedObject)) {
52  q->connect(action, &QAction::triggered, q, [this] {
53  slotActivated();
54  });
55  return;
56  }
57 
58  qCWarning(KAUTH) << Q_FUNC_INFO << "We're not decorating an action or a button";
59 }
60 
61 void ObjectDecoratorPrivate::linkActionToWidget()
62 {
63  QWidget *widget = qobject_cast<QWidget *>(decoratedObject);
64  if (widget) {
65  authAction.setParentWidget(widget);
66  return;
67  }
68 
69  QAction *action = qobject_cast<QAction *>(decoratedObject);
70  if (action) {
71  authAction.setParentWidget(action->parentWidget());
72  return;
73  }
74 
75  qCWarning(KAUTH) << Q_FUNC_INFO << "We're not decorating an action or a widget";
76 }
77 
78 void ObjectDecoratorPrivate::slotActivated()
79 {
80  if (authAction.isValid()) {
81  KAuth::ExecuteJob *job = authAction.execute(KAuth::Action::AuthorizeOnlyMode);
83  authStatusChanged(status);
84  });
85 
86  if (job->exec()) {
87  Q_EMIT q->authorized(authAction);
88  } else {
89  decoratedObject->setProperty("enabled", false);
90  }
91  }
92 }
93 
94 void ObjectDecoratorPrivate::authStatusChanged(KAuth::Action::AuthStatus status)
95 {
96  switch (status) {
98  decoratedObject->setProperty("enabled", true);
99  if (!oldIcon.isNull()) {
100  decoratedObject->setProperty("icon", QVariant::fromValue(oldIcon));
101  oldIcon = QIcon();
102  }
103  break;
105  decoratedObject->setProperty("enabled", true);
106  oldIcon = decoratedObject->property("icon").value<QIcon>();
107  decoratedObject->setProperty("icon", QIcon::fromTheme(QLatin1String("dialog-password")));
108  break;
109  default:
110  decoratedObject->setProperty("enabled", false);
111  if (!oldIcon.isNull()) {
112  decoratedObject->setProperty("icon", QVariant::fromValue(oldIcon));
113  oldIcon = QIcon();
114  }
115  }
116 }
117 
119  : QObject(parent)
120  , d(new ObjectDecoratorPrivate(this))
121 {
122  d->connectDecorated();
123 }
124 
126 
128 {
129  return d->authAction;
130 }
131 
133 {
134  if (actionName.isEmpty()) {
136  } else {
137  setAuthAction(KAuth::Action(actionName));
138  }
139 }
140 
142 {
143  if (d->authAction == action) {
144  return;
145  }
146 
147  if (d->authAction.isValid()) {
148  if (!d->oldIcon.isNull()) {
149  d->decoratedObject->setProperty("icon", QVariant::fromValue(d->oldIcon));
150  d->oldIcon = QIcon();
151  }
152  }
153 
154  if (action.isValid()) {
155  d->authAction = action;
156 
157  // Set the parent widget
158  d->linkActionToWidget();
159 
160  d->authStatusChanged(d->authAction.status());
161  }
162 }
163 
164 } // namespace KAuth
165 
166 #include "moc_objectdecorator.cpp"
QVariant fromValue(const T &value)
@ AuthorizedStatus
The authorization has been granted by the authorization backend.
Definition: action.h:86
void clicked(bool checked)
QWidget * parentWidget() const const
QIcon fromTheme(const QString &name)
Job for executing an Action.
Definition: executejob.h:39
AuthStatus
The three values set by authorization methods.
Definition: action.h:82
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
bool isValid() const
Returns if the object represents a valid action.
Definition: action.cpp:156
bool isEmpty() const const
Q_SCRIPTABLE CaptureState status()
void statusChanged(KAuth::Action::AuthStatus status)
Signal emitted when the authentication status changes.
Definition: action.cpp:18
void triggered(bool checked)
ObjectDecorator(QObject *parent)
Instantiate a new decorator attached to an object.
Class to access, authorize and execute actions.
Definition: action.h:75
~ObjectDecorator() override
Destructs the decorator.
void setAuthAction(const KAuth::Action &action)
Sets the action object associated with this decorator.
@ AuthRequiredStatus
The user could obtain the authorization after authentication.
Definition: action.h:87
bool exec()
KAuth::Action authAction() const
Returns the action object associated with this decorator, or an invalid action if it does not have on...
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Sep 30 2023 04:03:35 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.