Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / kinfocenter / proxywidget.cpp
blob88baa46a4141b1bfe18d4cb96be1c972784473a6
1 /*
2 Copyright (c) 1999 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "proxywidget.h"
20 #include "global.h"
21 #include "kinfocenter_interface.h"
23 #include <kpushbutton.h>
24 #include <QByteArray>
25 #include <QFrame>
26 #include <QHBoxLayout>
27 #include <QVBoxLayout>
28 #include <QResizeEvent>
29 #include <Q3ScrollView>
30 #include <QLabel>
31 #include <QtDBus/QtDBus>
32 #include <Q3WhatsThis>
34 #include <klocale.h>
35 #include <kapplication.h>
36 #include <kcmodule.h>
37 #include <kseparator.h>
38 #include <kdialog.h>
39 #include <kstandardguiitem.h>
40 #include <kdebug.h>
42 #include <unistd.h> // for getuid()
44 #include "proxywidget.moc"
46 class WhatsThis : public Q3WhatsThis
48 public:
49 WhatsThis( ProxyWidget* parent )
50 : Q3WhatsThis( parent ), proxy( parent ) {}
51 ~WhatsThis(){}
54 QString text( const QPoint & ) {
55 if ( !proxy->quickHelp().isEmpty() )
56 return proxy->quickHelp();
57 else
58 return i18n("The currently loaded configuration module.");
61 private:
62 ProxyWidget* proxy;
65 ////////////////////////////////////////////////////////////////////////////////////////////////////////
68 class RootInfoWidget : public QLabel
70 public:
71 RootInfoWidget(QWidget *parent, const char *name);
72 void setRootMessage(const QString& s) { setText(s); }
75 RootInfoWidget::RootInfoWidget(QWidget *parent, const char *name = 0)
76 : QLabel(parent)
78 setObjectName( name );
79 setFrameShape(QFrame::Box);
80 setFrameShadow(QFrame::Raised);
82 setText(i18n("<b>Changes in this module require root access.</b><br />"
83 "Click the \"Administrator Mode\" button to "
84 "allow modifications in this module."));
86 this->setWhatsThis( i18n("This module requires special permissions, probably "
87 "for system-wide modifications; therefore, it is "
88 "required that you provide the root password to be "
89 "able to change the module's properties. If you "
90 "do not provide the password, the module will be "
91 "disabled."));
95 ////////////////////////////////////////////////////////////////////////////////////////////////////////
97 class ProxyView : public Q3ScrollView
99 public:
100 ProxyView(KCModule *client, const QString& title, QWidget *parent, bool run_as_root, const char *name);
102 private:
103 virtual void resizeEvent(QResizeEvent *);
105 QWidget *contentWidget;
106 KCModule *client;
107 bool scroll;
110 class ProxyContentWidget : public QWidget
112 public:
113 ProxyContentWidget( QWidget* parent ) : QWidget( parent ) {}
114 ~ProxyContentWidget(){}
116 // this should be really done by qscrollview in AutoOneFit mode!
117 QSize sizeHint() const { return minimumSizeHint(); }
121 ProxyView::ProxyView(KCModule *_client, const QString&, QWidget *parent, bool run_as_root, const char *name)
122 : Q3ScrollView(parent, name), client(_client)
124 setResizePolicy(Q3ScrollView::AutoOneFit);
125 setFrameStyle( NoFrame );
126 contentWidget = new ProxyContentWidget( viewport() );
128 QVBoxLayout* vbox = new QVBoxLayout( contentWidget );
129 vbox->setMargin(0);
131 if (run_as_root && _client->useRootOnlyMessage()) // notify the user
133 RootInfoWidget *infoBox = new RootInfoWidget(contentWidget);
134 vbox->addWidget( infoBox );
135 QString msg = _client->rootOnlyMessage();
136 if (!msg.isEmpty())
137 infoBox->setRootMessage(msg);
138 vbox->setSpacing(KDialog::spacingHint());
140 client->setParent(contentWidget);
141 client->move(0,0);
142 client->show();
143 vbox->addWidget( client );
144 vbox->activate(); // make sure we have a proper minimumSizeHint
145 addChild(contentWidget);
148 void ProxyView::resizeEvent(QResizeEvent *e)
150 Q3ScrollView::resizeEvent(e);
154 ////////////////////////////////////////////////////////////////////////////////////////////////////////
157 ProxyWidget::ProxyWidget(KCModule *client, const QString &title,
158 bool run_as_root)
159 : QWidget(0)
160 , _client(client)
162 setWindowTitle(title);
163 #ifdef __GNUC__
164 #warning "kde4: DBUS port"
165 #endif
166 #if 0
167 if (getuid()==0 ) {
168 // Make root modules look as similar as possible...
169 DCOPCString replyType;
170 QByteArray replyData;
172 if (kapp->dcopClient()->call("kcontrol", "moduleIface", "getPalette()", QByteArray(),
173 replyType, replyData))
174 if ( replyType == "QPalette") {
175 QDataStream reply( replyData );
176 QPalette pal;
177 reply >> pal;
178 setPalette(pal);
180 /* // Doesn't work ...
181 if (kapp->dcopClient()->call("kcontrol", "moduleIface", "getStyle()", QByteArray(),
182 replyType, replyData))
183 if ( replyType == "QString") {
184 QDataStream reply( replyData, QIODevice::ReadOnly );
185 QString style;
186 reply >> style;
187 setStyle(style);
190 if (kapp->dcopClient()->call("kcontrol", "moduleIface", "getFont()", QByteArray(),
191 replyType, replyData))
192 if ( replyType == "QFont") {
193 QDataStream reply( replyData );
194 QFont font;
195 reply >> font;
196 setFont(font);
199 #endif
200 view = new ProxyView(client, title, this, run_as_root, "proxyview");
201 (void) new WhatsThis( this );
203 connect(_client, SIGNAL(changed(bool)), SLOT(clientChanged(bool)));
204 connect(_client, SIGNAL(quickHelpChanged()), SIGNAL(quickHelpChanged()));
206 _sep = new KSeparator(Qt::Horizontal, this);
208 _help = new KPushButton( KStandardGuiItem::help(), this );
209 _default = new KPushButton( KStandardGuiItem::defaults(), this );
210 _apply = new KPushButton( KStandardGuiItem::apply(), this );
211 _reset = new KPushButton( KStandardGuiItem::reset(), this );
212 _root = new KPushButton( KGuiItem(i18n( "&Administrator Mode" )), this );
214 bool mayModify = (!run_as_root || !_client->useRootOnlyMessage()) && !KCGlobal::isInfoCenter();
216 // only enable the requested buttons
217 int b = _client->buttons();
218 _help->setVisible(false & (b & KCModule::Help));
219 _default->setVisible(mayModify && (b & KCModule::Default));
220 _apply->setVisible(mayModify && (b & KCModule::Apply));
221 _reset->setVisible(mayModify && (b & KCModule::Apply));
222 _root->setVisible(run_as_root);
224 // disable initial buttons
225 _apply->setEnabled( false );
226 _reset->setEnabled( false );
228 connect(_help, SIGNAL(clicked()), SLOT(helpClicked()));
229 connect(_default, SIGNAL(clicked()), SLOT(defaultClicked()));
230 connect(_apply, SIGNAL(clicked()), SLOT(applyClicked()));
231 connect(_reset, SIGNAL(clicked()), SLOT(resetClicked()));
232 connect(_root, SIGNAL(clicked()), SLOT(rootClicked()));
234 QVBoxLayout *top = new QVBoxLayout(this);
235 top->setMargin(KDialog::marginHint());
236 top->setSpacing(KDialog::spacingHint());
237 top->addWidget(view);
238 top->addWidget(_sep);
240 QHBoxLayout *buttons = new QHBoxLayout();
241 top->addItem(buttons);
242 buttons->setSpacing(4);
243 buttons->addWidget(_help);
244 buttons->addWidget(_default);
245 if (run_as_root)
247 buttons->addWidget(_root);
250 buttons->addStretch(1);
251 if (mayModify)
253 buttons->addWidget(_apply);
254 buttons->addWidget(_reset);
257 top->activate();
260 ProxyWidget::~ProxyWidget()
262 delete _client;
265 QString ProxyWidget::quickHelp() const
267 if (_client)
268 return _client->quickHelp();
269 else
270 return "";
273 void ProxyWidget::helpClicked()
275 if (getuid()!=0)
276 emit helpRequest();
277 else
279 OrgKdeKinfocenterInterface kinfocenter("org.kde.kinfocenter", "/moduleIface", QDBusConnection::sessionBus());
280 kinfocenter.invokeHelp();
284 void ProxyWidget::defaultClicked()
286 clientChanged(true);
287 _client->defaults();
290 void ProxyWidget::applyClicked()
292 _client->save();
293 clientChanged(false);
296 void ProxyWidget::resetClicked()
298 _client->load();
299 clientChanged(false);
302 void ProxyWidget::rootClicked()
304 emit runAsRoot();
307 void ProxyWidget::clientChanged(bool state)
309 _apply->setEnabled(state);
310 _reset->setEnabled(state);
312 // forward the signal
313 emit changed(state);
316 const KAboutData *ProxyWidget::aboutData() const
318 return _client->aboutData();
321 // vim: sw=2 sts=2 et