summaryrefslogtreecommitdiffstats
path: root/examples/PkExample.cpp
blob: 7366b4eaafabaaceff5e1324f384bce7036c227c (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
// This is an example not a library
/***************************************************************************
 *   Copyright (C) 2009 Radek Novacek    <rnovacek@redhat.com>             *
 *   Copyright (C) 2008 Daniel Nicoletti <dantti85-pk@yahoo.com.br>        *
 *                                                                         *
 *   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 program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 ***************************************************************************/

#include "PkExample.h"

#include "gui/polkitqt1-gui-actionbutton.h"
#include "gui/polkitqt1-gui-actionbuttons.h"
#include "core/polkitqt1-authority.h"
#include <TQtCore/TQDebug>

#include <TQtDBus/TQDBusMessage>
#include <TQtDBus/TQDBusConnection>

using namespace PolkitTQt;
using namespace PolkitTQt::Gui;

PkExample::PkExample(TQMainWindow *parent)
        : TQMainWindow(parent)
{
    setupUi(this);

    ActionButton *bt;

    // Here we create an ActionButton that is a subclass of Action
    // always pass a TQButton pointer and action id
    // You can change the action id later if you want
    bt = new ActionButton(kickPB, "org.qt.policykit.examples.kick", this);
    // Here we are setting the text and icon to all four states
    // an action might have
    bt->setText("Kick!");
    bt->setIcon(TQPixmap(":/Icons/custom-no.png"));
    // By using set{Yes|No|Auth}Enabled you can set the states
    // when the button is enabled
    bt->setEnabled(true, Action::No);
    // As ActionButton is also an Action we cast it to add to menu
    menuActions->addAction(qobject_cast<Action *>(bt));
    toolBar->addAction(qobject_cast<Action *>(bt));
    // this signal is emitted when the user click on the action,
    // it will only happen if it was inserted in a TQMenu or a TQToolBar
    connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction()));
    // This signal was propagated from the TQButton just for
    // convenience in this case we don't have any benefit but the code
    // look cleaner
    connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate()));
    // this is the Action activated signal, it is always emmited whenever
    // someone click and get authorized to do the action
    connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated()));

    bt = new ActionButton(cryPB, "org.qt.policykit.examples.cry", this);
    bt->setText("Cry!");
    bt->setIcon(TQPixmap(":/Icons/custom-yes.png"));
    menuActions->addAction(qobject_cast<Action *>(bt));
    toolBar->addAction(qobject_cast<Action *>(bt));
    connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction()));
    connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate()));
    connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated()));

    bt = new ActionButton(bleedPB, "org.qt.policykit.examples.bleed", this);
    bt->setText("Bleed!");
    bt->setIcon(TQPixmap(":/Icons/action-locked-default.png"));
    menuActions->addAction(qobject_cast<Action *>(bt));
    toolBar->addAction(qobject_cast<Action *>(bt));
    connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction()));
    connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate()));
    connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated()));

    // This action is more customized
    bt = new ActionButton(playPB, "org.qt.policykit.examples.play", this);
    bt->setText("Play!");
    bt->setVisible(true, Action::No | Action::Auth | Action::Yes);
    bt->setEnabled(true, Action::No | Action::Auth | Action::Yes);
    // here we set the behavior of PolKitResul = No
    bt->setText("Can't play!", Action::No);
    bt->setIcon(TQPixmap(":/Icons/custom-no.png"), Action::No);
    bt->setToolTip("If your admin wasn't annoying, you could do this", Action::No);
    // here we set the behavior of PolKitResul = Auth
    bt->setText("Play?", Action::Auth);
    bt->setIcon(TQPixmap(":/Icons/action-locked-default.png"), Action::Auth);
    bt->setToolTip("Only card carrying tweakers can do this!", Action::Auth);
    // here we set the behavior of PolKitResul = Yes
    bt->setText("Play!", Action::Yes);
    bt->setIcon(TQPixmap(":/Icons/custom-yes.png"), Action::Yes);
    bt->setToolTip("Go ahead, play!", Action::Yes);

    menuActions->addAction(qobject_cast<Action *>(bt));
    toolBar->addAction(qobject_cast<Action *>(bt));
    connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction()));
    connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate()));
    connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated()));

    bt = new ActionButton(deletePB, "org.qt.policykit.examples.delete", this);
    bt->setText("Delete!");
    bt->setIcon(TQPixmap(":/Icons/custom-no.png"), Action::No);
    bt->setIcon(TQPixmap(":/Icons/action-locked-default.png"), Action::Auth);
    bt->setIcon(TQPixmap(":/Icons/custom-yes.png"), Action::Yes);
    menuActions->addAction(qobject_cast<Action *>(bt));
    toolBar->addAction(qobject_cast<Action *>(bt));
    connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction()));
    connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate()));
    connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated()));

    bt = new ActionButton(listenPB, "org.qt.policykit.examples.listen", this);
    bt->setText("Listen!");
    bt->setIcon(TQPixmap(":/Icons/custom-no.png"), Action::No);
    bt->setIcon(TQPixmap(":/Icons/action-locked-default.png"), Action::Auth);
    bt->setIcon(TQPixmap(":/Icons/custom-yes.png"), Action::Yes);
    menuActions->addAction(qobject_cast<Action *>(bt));
    toolBar->addAction(qobject_cast<Action *>(bt));
    connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction()));
    connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate()));
    connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated()));

    bt = new ActionButton(setPB, "org.qt.policykit.examples.set", this);
    bt->setText("Set!");
    bt->setIcon(TQPixmap(":/Icons/custom-no.png"), Action::No);
    bt->setIcon(TQPixmap(":/Icons/action-locked-default.png"), Action::Auth);
    bt->setIcon(TQPixmap(":/Icons/custom-yes.png"), Action::Yes);
    menuActions->addAction(qobject_cast<Action *>(bt));
    toolBar->addAction(qobject_cast<Action *>(bt));
    connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction()));
    connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate()));
    connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated()));

    bt = new ActionButton(shoutPB, "org.qt.policykit.examples.shout", this);
    bt->setIcon(TQPixmap(":/Icons/custom-no.png"), Action::No);
    bt->setIcon(TQPixmap(":/Icons/action-locked-default.png"), Action::Auth);
    bt->setIcon(TQPixmap(":/Icons/custom-yes.png"), Action::Yes);
    bt->setText("Can't shout!", Action::No);
    bt->setText("Shout?", Action::Auth);
    bt->setText("Shout!", Action::Yes);
    menuActions->addAction(qobject_cast<Action *>(bt));
    toolBar->addAction(qobject_cast<Action *>(bt));
    connect(bt, SIGNAL(triggered(bool)), this, SLOT(activateAction()));
    connect(bt, SIGNAL(clicked(TQButton *, bool)), bt, SLOT(activate()));
    connect(bt, SIGNAL(authorized()), this, SLOT(actionActivated()));

    // test configChanged
}

PkExample::~PkExample()
{
}

void PkExample::activateAction()
{
    // Here we cast the sender() to an Action and call activate()
    // on it.
    // Be careful in doing the same for ActionButton won't work as expected
    // as action->activate() is calling Action::activate() and
    // not ActionButton::activate which are different.
    // You can cast then to ActionButton but be carefull
    // an Action casted to ActionButton may crash you app
    Action *action = qobject_cast<Action *>(sender());
    // calling activate with winId() makes the auth dialog
    // be correct parented with your application.
    action->activate();
}

void PkExample::actionActivated()
{
    // This slot is called whenever an action is allowed
    // here you will do your dirt job by calling a helper application
    // that might erase your hardrive ;)
    Action *action = qobject_cast<Action *>(sender());

    // this is our Special Action that after allowed will call the helper
    if (action->is("org.qt.policykit.examples.set")) {
        tqDebug() << "toggled for: org.qt.policykit.examples.set";

        TQDBusMessage message;
        message = TQDBusMessage::createMethodCall("org.qt.policykit.examples",
                  "/",
                  "org.qt.policykit.examples",
                  TQLatin1String("set"));
        TQList<TQVariant> argumentList;
        argumentList << qVariantFromValue(setCB->currentText());
        message.setArguments(argumentList);
        // notice the systemBus here..
        TQDBusMessage reply = TQDBusConnection::systemBus().call(message);
        if (reply.type() == TQDBusMessage::ReplyMessage
                && reply.arguments().size() == 1) {
            // the reply can be anything, here we receive a bool
            TQListWidgetItem *item;
            if (reply.arguments().first().toBool())
                item = new TQListWidgetItem(TQPixmap(":/Icons/custom-yes.png"),
                                           TQString("Implicit authorization for shout has been set to %0")
                                           .arg(setCB->currentText()));
            else
                item = new TQListWidgetItem(TQPixmap(":/Icons/custom-no.png"),
                                           TQString("Can't change the implicit authorization. Denied."));
            actionList->addItem(item);
            tqDebug() << reply.arguments().first().toString();
        } else if (reply.type() == TQDBusMessage::MethodCallMessage) {
            tqWarning() << "Message did not receive a reply (timeout by message bus)";
        }
        return;
    }

    // As debug message says we are pretending to be the mechanism for the
    // following action, here you will actually call your DBus helper that
    // will run as root (setuid is not needed, see DBus docs).
    // In the helper application you will issue checkAuthorizationSync,
    // passing the action id and the caller pid (which DBus will tell you).
    tqDebug() << "pretending to be the mechanism for action:" << action->actionId();

    Authority::Result result;
    UnixProcessSubject subject(static_cast<uint>(TQCoreApplication::applicationPid()));

    result = Authority::instance()->checkAuthorizationSync(action->actionId(), subject,
             Authority::AllowUserInteraction);
    if (result == Authority::Yes) {
        // in the helper you will do the action
        tqDebug() << "caller is authorized to do:" << action->actionId();
        TQListWidgetItem *item = new TQListWidgetItem(TQPixmap(":/Icons/custom-yes.png"), action->actionId());
        actionList->addItem(item);
    } else {
        // OR return false to notify the caller that the action is not authorized.
        tqDebug() << "caller is NOT authorized to do:" << action->actionId();
        TQListWidgetItem *item = new TQListWidgetItem(TQIcon(":/Icons/custom-no.png"), action->actionId());
        actionList->addItem(item);
    }
}