KAuth

kauthobjectdecorator.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 "kauthobjectdecorator.h"
9 
10 #include "kauthaction.h"
11 #include "kauthdebug.h"
12 #include "kauthexecutejob.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 (qobject_cast<QAbstractButton *>(decoratedObject)) {
45  q->connect(decoratedObject, SIGNAL(clicked()), q, SLOT(slotActivated()));
46  return;
47  }
48 
49  if (qobject_cast<QAction *>(decoratedObject)) {
50  q->connect(decoratedObject, SIGNAL(triggered(bool)), q, SLOT(slotActivated()));
51  return;
52  }
53 
54  qCWarning(KAUTH) << Q_FUNC_INFO << "We're not decorating an action or a button";
55 }
56 
57 void ObjectDecoratorPrivate::linkActionToWidget()
58 {
59  QWidget *widget = qobject_cast<QWidget *>(decoratedObject);
60  if (widget) {
61  authAction.setParentWidget(widget);
62  return;
63  }
64 
65  QAction *action = qobject_cast<QAction *>(decoratedObject);
66  if (action) {
67  authAction.setParentWidget(action->parentWidget());
68  return;
69  }
70 
71  qCWarning(KAUTH) << Q_FUNC_INFO << "We're not decorating an action or a widget";
72 }
73 
74 void ObjectDecoratorPrivate::slotActivated()
75 {
76  if (authAction.isValid()) {
77  KAuth::ExecuteJob *job = authAction.execute(KAuth::Action::AuthorizeOnlyMode);
78  q->connect(job, SIGNAL(statusChanged(KAuth::Action::AuthStatus)), q, SLOT(authStatusChanged(KAuth::Action::AuthStatus)));
79  if (job->exec()) {
80  Q_EMIT q->authorized(authAction);
81  } else {
82  decoratedObject->setProperty("enabled", false);
83  }
84  }
85 }
86 
87 void ObjectDecoratorPrivate::authStatusChanged(KAuth::Action::AuthStatus status)
88 {
89  switch (status) {
91  decoratedObject->setProperty("enabled", true);
92  if (!oldIcon.isNull()) {
93  decoratedObject->setProperty("icon", QVariant::fromValue(oldIcon));
94  oldIcon = QIcon();
95  }
96  break;
98  decoratedObject->setProperty("enabled", true);
99  oldIcon = decoratedObject->property("icon").value<QIcon>();
100  decoratedObject->setProperty("icon", QIcon::fromTheme(QLatin1String("dialog-password")));
101  break;
102  default:
103  decoratedObject->setProperty("enabled", false);
104  if (!oldIcon.isNull()) {
105  decoratedObject->setProperty("icon", QVariant::fromValue(oldIcon));
106  oldIcon = QIcon();
107  }
108  }
109 }
110 
112  : QObject(parent)
113  , d(new ObjectDecoratorPrivate(this))
114 {
115  d->connectDecorated();
116 }
117 
119 {
120  delete d;
121 }
122 
124 {
125  return d->authAction;
126 }
127 
129 {
130  if (actionName.isEmpty()) {
132  } else {
133  setAuthAction(KAuth::Action(actionName));
134  }
135 }
136 
138 {
139  if (d->authAction == action) {
140  return;
141  }
142 
143  if (d->authAction.isValid()) {
144  if (!d->oldIcon.isNull()) {
145  d->decoratedObject->setProperty("icon", QVariant::fromValue(d->oldIcon));
146  d->oldIcon = QIcon();
147  }
148  }
149 
150  if (action.isValid()) {
151  d->authAction = action;
152 
153  // Set the parent widget
154  d->linkActionToWidget();
155 
156  d->authStatusChanged(d->authAction.status());
157  }
158 }
159 
160 } // namespace KAuth
161 
162 #include "moc_kauthobjectdecorator.cpp"
The user could obtain the authorization after authentication.
Definition: kauthaction.h:82
Job for executing an Action.
void setAuthAction(const KAuth::Action &action)
Sets the action object associated with this decorator.
bool exec()
bool isValid() const
Returns if the object represents a valid action.
ObjectDecorator(QObject *parent)
Instantiate a new decorator attached to an object.
QWidget * parentWidget() const const
The authorization has been granted by the authorization backend.
Definition: kauthaction.h:81
bool isEmpty() const const
AuthStatus
The three values set by authorization methods.
Definition: kauthaction.h:77
QVariant fromValue(const T &value)
QIcon fromTheme(const QString &name)
Class to access, authorize and execute actions.
Definition: kauthaction.h:70
~ObjectDecorator()
Destructs the decorator.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KAuth::Action authAction() const
Returns the action object associated with this decorator, or 0 if it does not have one...
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:53:41 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.