summaryrefslogtreecommitdiffstats
path: root/src/AuthDialog.cpp
blob: 7363ae9c66a03b22cd177c830f2e71c21bdba9ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*  This file is part of the KDE project
    Copyright (C) 2007-2008 Gökçen Eraslan <gokcen@pardus.org.tr>
    Copyright (C) 2008 Dirk Mueller <mueller@kde.org>
    Copyright (C) 2008 Daniel Nicoletti <dantti85-pk@yahoo.com.br>
    Copyright (C) 2008-2010 Dario Freddi <drf@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#include "AuthDialog.h"

#include <TQtCore/TQProcess>
#include <TQtGui/TQPainter>
#include <TQtGui/TQStandardItemModel>
#include <KDebug>

#include <KToolInvocation>
#include <KUser>

#include <PolkitTQt1/Authority>
#include <PolkitTQt1/Details>

#include <KWindowSystem>
#include <KNotification>

AuthDialog::AuthDialog(const TQString &actionId,
                       const TQString &message,
                       const TQString &iconName,
                       const PolkitTQt1::Details &details,
                       const PolkitTQt1::Identity::List &identities,
                       WId parent)
        : KDialog(0)
{
    // KAuth is able to circumvent polkit's limitations, and manages to send the wId to the auth agent.
    // If we received it, we use KWindowSystem to associate this dialog correctly.
    if (parent > 0) {
        kDebug() << "Associating the dialog with " << parent << " this dialog is " << winId();

        // Set the parent
        KWindowSystem::setMainWindow(this, parent);

        // Set modal
        KWindowSystem::setState(winId(), NET::Modal);
    }

    setupUi(mainWidget());
    setButtons(Ok | Cancel | Details);

    if (message.isEmpty()) {
        kWarning() << "Could not get action message for action.";
        lblHeader->hide();
    } else {
        kDebug() << "Message of action: " << message;
        lblHeader->setText("<h3>" + message + "</h3>");
        setCaption(message);
        m_message = message;
    }

    // loads the standard key icon
    TQPixmap icon = KIconLoader::global()->loadIcon("dialog-password",
                                                    KIconLoader::NoGroup,
                                                    KIconLoader::SizeHuge,
                                                    KIconLoader::DefaultState);
    // create a painter to paint the action icon over the key icon
    TQPainter painter(&icon);
    const int iconSize = icon.size().width();
    // the the emblem icon to size 32
    int overlaySize = 32;
    // try to load the action icon
    const TQPixmap pixmap = KIconLoader::global()->loadIcon(iconName,
                                                           KIconLoader::NoGroup,
                                                           overlaySize,
                                                           KIconLoader::DefaultState,
                                                           TQStringList(),
                                                           0,
                                                           true);
    // if we're able to load the action icon paint it over the
    // key icon.
    if (!pixmap.isNull()) {
        TQPoint startPoint;
        // bottom right corner
        startPoint = TQPoint(iconSize - overlaySize - 2,
                            iconSize - overlaySize - 2);
        painter.drawPixmap(startPoint, pixmap);
    }

    setWindowIcon(icon);
    lblPixmap->setPixmap(icon);

    // find action description for actionId
    foreach(const PolkitTQt1::ActionDescription &desc, PolkitTQt1::Authority::instance()->enumerateActionsSync()) {
        if (actionId == desc.actionId()) {
            m_actionDescription = desc;
            kDebug() << "Action description has been found" ;
            break;
        }
    }

    AuthDetails *detailsDialog = new AuthDetails(details, m_actionDescription, m_appname, this);
    setDetailsWidget(detailsDialog);

    userCB->hide();
    lePassword->setFocus();

    errorMessageKTW->hide();

    // If there is more than 1 identity we will show the combobox for user selection
    if (identities.size() > 1) {
        connect(userCB, SIGNAL(currentIndexChanged(int)),
                this, SLOT(on_userCB_currentIndexChanged(int)));

        createUserCB(identities);
    } else {
        userCB->addItem("", TQVariant(identities[0].toString()));
        userCB->setCurrentIndex(0);
    }
}

AuthDialog::~AuthDialog()
{
}

void AuthDialog::accept()
{
    // Do nothing, do not close the dialog. This is needed so that the dialog stays
    lePassword->setEnabled(false);
    return;
}

void AuthDialog::setRequest(const TQString &request, bool requiresAdmin)
{
    kDebug() << request;
    PolkitTQt1::Identity identity = adminUserSelected();
    if (request.startsWith(TQLatin1String("password:"), TQt::CaseInsensitive)) {
        if (requiresAdmin) {
            if (!identity.isValid()) {
                lblPassword->setText(i18n("Password for root:"));
            } else {
                lblPassword->setText(i18n("Password for %1:",
                                          identity.toString().remove("unix-user:")));
            }
        } else {
            lblPassword->setText(i18n("Password:"));
        }
    } else if (request.startsWith(TQLatin1String("password or swipe finger:"),
                                  TQt::CaseInsensitive)) {
        if (requiresAdmin) {
            if (!identity.isValid()) {
                lblPassword->setText(i18n("Password or swipe finger for root:"));
            } else {
                lblPassword->setText(i18n("Password or swipe finger for %1:",
                                          identity.toString().remove("unix-user:")));
            }
        } else {
            lblPassword->setText(i18n("Password or swipe finger:"));
        }
    } else {
        lblPassword->setText(request);
    }

}

void AuthDialog::setOptions()
{
    lblContent->setText(i18n("An application is attempting to perform an action that requires privileges."
                             " Authentication is required to perform this action."));
}

void AuthDialog::createUserCB(const PolkitTQt1::Identity::List &identities)
{
    /* if we've already built the list of admin users once, then avoid
        * doing it again.. (this is mainly used when the user entered the
        * wrong password and the dialog is recycled)
        */
    if (identities.count() && (userCB->count() - 1) != identities.count()) {
        // Clears the combobox in the case some user be added
        userCB->clear();

        // Adds a Dummy user
        userCB->addItem(i18n("Select User"), qVariantFromValue<TQString> (TQString()));
        qobject_cast<TQStandardItemModel *>(userCB->model())->item(userCB->count()-1)->setEnabled(false);

        // For each user
        int index = 1; // Start at 1 because of the "Select User" entry
        int currentUserIndex = -1;
        const KUser currentUser;
        foreach(const PolkitTQt1::Identity &identity, identities) {
            // First check to see if the user is valid
            kDebug() << "User: " << identity.toString();
            const KUser user(identity.toString().remove("unix-user:"));
            if (!user.isValid()) {
                kWarning() << "User invalid: " << user.loginName();
                continue;
            }

            // Display user Full Name IF available
            TQString display;
            if (!user.property(KUser::FullName).toString().isEmpty()) {
                display = i18nc("%1 is the full user name, %2 is the user login name", "%1 (%2)", user.property(KUser::FullName).toString(), user.loginName());
            } else {
                display = user.loginName();
            }

            KIcon icon;
            // load user icon face
            if (!user.faceIconPath().isEmpty()) {
                icon = KIcon(user.faceIconPath());
            } else {
                icon = KIcon("user-identity");
            }
            // appends the user item
            userCB->addItem(icon, display, qVariantFromValue<TQString> (identity.toString()));

            if (user == currentUser) {
                currentUserIndex = index;
            }
            ++index;
        }

        // Show the widget and set focus
        if (currentUserIndex != -1) {
            userCB->setCurrentIndex(currentUserIndex);
        }
        userCB->show();
    }
}

PolkitTQt1::Identity AuthDialog::adminUserSelected() const
{
    if (userCB->currentIndex() == -1)
        return PolkitTQt1::Identity();

    TQString id = userCB->itemData(userCB->currentIndex()).toString();
    if (id.isEmpty())
        return PolkitTQt1::Identity();
    return PolkitTQt1::Identity::fromString(id);
}

void AuthDialog::on_userCB_currentIndexChanged(int /*index*/)
{
    PolkitTQt1::Identity identity = adminUserSelected();
    // itemData is Null when "Select user" is selected
    if (!identity.isValid()) {
        lePassword->setEnabled(false);
        lblPassword->setEnabled(false);
        enableButtonOk(false);
    } else {
        lePassword->setEnabled(true);
        lblPassword->setEnabled(true);
        enableButtonOk(true);
        // We need this to restart the auth with the new user
        emit adminUserSelected(identity);
        // git password label focus
        lePassword->setFocus();
    }
}

TQString AuthDialog::password() const
{
    return lePassword->text();
}

void AuthDialog::authenticationFailure()
{
    errorMessageKTW->setText(i18n("Authentication failure, please try again."), KTitleWidget::ErrorMessage);
    TQFont bold = font();
    bold.setBold(true);
    lblPassword->setFont(bold);
    lePassword->setEnabled(true);
    lePassword->clear();
    lePassword->setFocus();
}

void AuthDialog::showEvent(TQShowEvent *event)
{
    KDialog::showEvent(event);
    if (winId() != KWindowSystem::activeWindow())
    {
        KNotification *notification = new KNotification("authenticate", this,
                                                        KNotification::Persistent | KNotification::CloseWhenWidgetActivated);
        kDebug() << "Notificate: " << notification->eventId();
        notification->setText(m_message);
        TQPixmap icon = KIconLoader::global()->loadIcon("dialog-password",
                                                        KIconLoader::NoGroup,
                                                        KIconLoader::SizeHuge,
                                                        KIconLoader::DefaultState);
        notification->setPixmap(icon);
        notification->setActions(TQStringList() << i18n("Switch to dialog") << i18n("Cancel"));

        connect(notification, SIGNAL(activated(uint)), this, SLOT(notificationActivated(uint)));
        notification->sendEvent();
    }

}

void AuthDialog::notificationActivated(unsigned int action)
{
    kDebug() << "notificationActivated: " << action;
    if (action == 1)
    {
        KWindowSystem::forceActiveWindow(winId());
    }
}

AuthDetails::AuthDetails(const PolkitTQt1::Details &details,
                         const PolkitTQt1::ActionDescription &actionDescription,
                         const TQString &appname,
                         TQWidget *parent)
        : TQWidget(parent)
{
    setupUi(this);

    app_label->setText(appname);

    foreach(const TQString &key, details.keys()) { //krazy:exclude=foreach (Details is not a map/hash, but rather a method)
        int row = gridLayout->rowCount() + 1;

        TQLabel *keyLabel = new TQLabel(this);
        keyLabel->setText(i18nc("%1 is the name of a detail about the current action "
                                "provided by polkit", "%1:", key));
        gridLayout->addWidget(keyLabel, row, 0);

        TQLabel *valueLabel = new TQLabel(this);
        valueLabel->setText(details.lookup(key));
        gridLayout->addWidget(valueLabel, row, 1);
    }

    action_label->setText(actionDescription.description());

    action_label->setTipText(i18n("Click to edit %1", actionDescription.actionId()));
    action_label->setUrl(actionDescription.actionId());

    TQString vendor    = actionDescription.vendorName();
    TQString vendorUrl = actionDescription.vendorUrl();

    if (!vendor.isEmpty()) {
        vendorUL->setText(vendor);
        vendorUL->setTipText(i18n("Click to open %1", vendorUrl));
        vendorUL->setUrl(vendorUrl);
    } else if (!vendorUrl.isEmpty()) {
        vendorUL->setText(vendorUrl);
        vendorUL->setTipText(i18n("Click to open %1", vendorUrl));
        vendorUL->setUrl(vendorUrl);
    } else {
        vendorL->hide();
        vendorUL->hide();
    }

    connect(vendorUL, SIGNAL(leftClickedUrl(TQString)), SLOT(openUrl(TQString)));
    connect(action_label, SIGNAL(leftClickedUrl(TQString)), SLOT(openAction(TQString)));
}

void AuthDetails::openUrl(const TQString& url)
{
    KToolInvocation::invokeBrowser(url);
}

void AuthDetails::openAction(const TQString &url)
{
    TQProcess::startDetached("polkit-kde-authorization", TQStringList() << url);
}

#include "AuthDialog.moc"