not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / smartcard / smartcard.cpp
blob2304db8620f795754a25a4be3f04d21fb7758011
1 /**
2 * smartcard.cpp
4 * Copyright (c) 2001 George Staikos <staikos@kde.org>
5 * Copyright (c) 2001 Fernando Llobregat <fernando.llobregat@free.fr>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <QCheckBox>
23 #include <QLabel>
24 #include <QLayout>
25 #include <QLineEdit>
26 #include <QPushButton>
28 #include <dcopclient.h>
30 #include <kaboutdata.h>
31 #include <kapplication.h>
32 #include <kcarddb.h>
33 #include <kcardfactory.h>
34 #include <kcardgsm_impl.h>
35 #include <kconfig.h>
36 #include <kdebug.h>
37 #include <kdialog.h>
38 #include <kglobal.h>
39 #include <k3listview.h>
40 #include <klocale.h>
41 #include <kmessagebox.h>
42 #include <kmenu.h>
43 #include <kgenericfactory.h>
45 #include "smartcard.h"
47 typedef KGenericFactory<KSmartcardConfig> KSmartcardConfigFactory;
48 K_EXPORT_COMPONENT_FACTORY(kcm_smartcard, KSmartcardConfigFactory("kcmsmartcard"))
50 KSmartcardConfig::KSmartcardConfig(QWidget *parent, const QStringList &)
51 : KCModule(KSmartcardConfig::componentData(), parent)
52 , DCOPObject("kcmsmartcard")
54 QVBoxLayout *layout = new QVBoxLayout(this);
55 layout->setSpacing(KDialog::spacingHint());
56 layout->setMargin(KDialog::marginHint());
57 config = new KConfig("ksmartcardrc", false, false);
59 DCOPClient *dc = KApplication::kApplication()->dcopClient();
61 _ok = false;
62 dc->remoteInterfaces("kded", "kardsvc", &_ok);
64 KAboutData *about =
65 new KAboutData(I18N_NOOP("kcmsmartcard"), 0, ki18n("KDE Smartcard Control Module"),
66 0, KLocalizedString(), KAboutData::License_GPL,
67 ki18n("(c) 2001 George Staikos"));
69 about->addAuthor(ki18n("George Staikos"), KLocalizedString(), "staikos@kde.org");
70 setAboutData( about );
72 if (_ok) {
75 base = new SmartcardBase(this);
76 layout->add(base);
78 _popUpKardChooser = new KMenu(this,"KpopupKardChooser");
79 _popUpKardChooser->insertItem(i18n("Change Module..."),
80 this,
81 SLOT(slotLaunchChooser()));
82 // The config backend
84 connect(base->launchManager, SIGNAL(clicked()), SLOT( changed() ));
85 connect(base->beepOnInsert, SIGNAL(clicked()), SLOT( changed() ));
86 connect(base->enableSupport, SIGNAL(clicked()), SLOT( changed() ));
89 connect(base->enablePolling, SIGNAL(clicked()), SLOT( changed() ));
90 connect(base->_readerHostsListView,
91 SIGNAL(rightButtonPressed(QListViewItem *,const QPoint &,int)),
92 this,
93 SLOT(slotShowPopup(QListViewItem *,const QPoint &,int)));
97 if (!connectDCOPSignal("",
98 "",
99 "signalReaderListChanged(QStringList)",
100 "loadReadersTab(QStringList)",
101 false))
103 kDebug()<<"Error connecting to DCOP server";
106 if (!connectDCOPSignal("",
108 "signalCardStateChanged(QString,bool,QString)",
109 "updateReadersState (QString,bool,QString) ",
110 false))
112 kDebug()<<"Error connecting to DCOP server";
113 _cardDB= new KCardDB();
114 load();
115 } else {
116 layout->add(new NoSmartcardBase(this));
123 KSmartcardConfig::~KSmartcardConfig()
125 delete config;
126 delete _cardDB;
129 void KSmartcardConfig::slotLaunchChooser(){
132 if ( KCardDB::launchSelector(base->_readerHostsListView->currentItem()->parent()->text(0))){
134 KMessageBox::sorry(this,i18n("Unable to launch KCardChooser"));
140 void KSmartcardConfig::slotShowPopup(QListViewItem * item ,const QPoint & _point,int i)
143 //The popup only appears in cards, not in the slots1
144 if (item->isSelectable()) return;
145 _popUpKardChooser->exec(_point);
149 void KSmartcardConfig::loadSmartCardSupportTab(){
153 //Update the toggle buttons with the current configuration
155 if (_ok) {
156 base->enableSupport->setChecked(config->readEntry("Enable Support",
157 false));
158 base->enablePolling->setChecked(config->readEntry("Enable Polling",
159 true));
160 base->beepOnInsert->setChecked(config->readEntry("Beep on Insert",
161 true));
162 base->launchManager->setChecked(config->readEntry("Launch Manager",
163 true));
170 void KSmartcardConfig::updateReadersState (QString &readerName,
171 bool isCardPresent,
172 QString &atr) {
174 K3ListViewItem * tID=(K3ListViewItem *) base->_readerHostsListView->findItem(readerName, 0);
175 if (tID==0) return;
177 K3ListViewItem * tIDChild=(K3ListViewItem*) tID->firstChild();
178 if (tIDChild==NULL) return;
180 delete tIDChild;
182 if (!isCardPresent)
183 (void) new K3ListViewItem(tID,i18n("No card inserted"));
184 else{
186 getSupportingModule(tID,atr);
194 void KSmartcardConfig::loadReadersTab( QStringList &lr){
196 //Prepare data for dcop calls
197 QByteArray data, retval;
198 QCString rettype;
199 QDataStream arg(&data, QIODevice::WriteOnly);
201 arg.setVersion(QDataStream::Qt_3_1);
202 DCOPCString modName = "kardsvc";
203 arg << modName;
205 // New view items
206 K3ListViewItem * temp;
208 //If the smartcard support is disabled we unload the kardsvc KDED module
209 // and return
211 base->_readerHostsListView->clear();
213 if (!config->readEntry("Enable Support", false)) {
218 // New view items
219 K3ListViewItem * temp;
220 kapp->dcopClient()->call("kded", "kded", "unloadModule(QCString)",
221 data, rettype, retval);
223 (void) new K3ListViewItem(base->_readerHostsListView,
224 i18n("Smart card support disabled"));
227 return;
231 if (lr.isEmpty()){
234 (void) new K3ListViewItem(base->_readerHostsListView,
235 i18n("No readers found. Check 'pcscd' is running"));
236 return;
239 for (QStringList::Iterator _slot=lr.begin();_slot!=lr.end();++_slot){
241 temp= new K3ListViewItem(base->_readerHostsListView,*_slot);
244 QByteArray dataATR;
245 QDataStream argATR(&dataATR,QIODevice::WriteOnly);
247 argATR.setVersion(QDataStream::Qt_3_1);
248 argATR << *_slot;
250 kapp->dcopClient()->call("kded", "kardsvc", "getCardATR(QString)",
251 dataATR, rettype, retval);
254 QString cardATR;
255 QDataStream retReaderATR(retval);
256 retReaderATR>>cardATR;
258 if (cardATR.isNull()){
260 (void) new K3ListViewItem(temp,i18n("NO ATR or no card inserted"));
261 continue;
264 getSupportingModule(temp,cardATR);
274 void KSmartcardConfig::getSupportingModule( K3ListViewItem * ant,
275 QString & cardATR) const{
278 if (cardATR.isNull()){
280 (void) new K3ListViewItem(ant,i18n("NO ATR or no card inserted"));
281 return;
285 QString modName=_cardDB->getModuleName(cardATR);
286 if (!modName.isNull()){
287 QStringList mng= modName.split( ",");
288 QString type=mng[0];
289 QString subType=mng[1];
290 QString subSubType=mng[2];
291 K3ListViewItem * hil =new K3ListViewItem(ant,
292 i18n("Managed by: "),
293 type,
294 subType,
295 subSubType);
296 hil->setSelectable(false);
298 else{
301 K3ListViewItem * hil =new K3ListViewItem(ant,
302 i18n("No module managing this card"));
303 hil->setSelectable(false);
308 void KSmartcardConfig::load()
312 //Prepare data for dcop calls
313 QByteArray data, retval;
314 QCString rettype;
315 QDataStream arg(&data, QIODevice::WriteOnly);
317 arg.setVersion(QDataStream::Qt_3_1);
318 DCOPCString modName = "kardsvc";
319 arg << modName;
321 loadSmartCardSupportTab();
324 // We call kardsvc to retrieve the current readers
325 kapp->dcopClient()->call("kded", "kardsvc", "getSlotList ()",
326 data, rettype, retval);
327 QStringList readers;
328 readers.clear();
329 QDataStream retReader(retval);
330 retReader>>readers;
332 //And we update the panel
333 loadReadersTab(readers);
335 emit changed(false);
340 void KSmartcardConfig::save()
342 if (_ok) {
343 config->writeEntry("Enable Support", base->enableSupport->isChecked());
344 config->writeEntry("Enable Polling", base->enablePolling->isChecked());
345 config->writeEntry("Beep on Insert", base->beepOnInsert->isChecked());
346 config->writeEntry("Launch Manager", base->launchManager->isChecked());
349 QByteArray data, retval;
350 QCString rettype;
351 QDataStream arg(&data, QIODevice::WriteOnly);
353 arg.setVersion(QDataStream::Qt_3_1);
354 DCOPCString modName = "kardsvc";
355 arg << modName;
357 // Start or stop the server as needed
358 if (base->enableSupport->isChecked()) {
360 kapp->dcopClient()->call("kded", "kded", "loadModule(QCString)",
361 data, rettype, retval);
362 config->sync();
364 kapp->dcopClient()->call("kded", "kardsvc", "reconfigure()",
365 data, rettype, retval);
366 } else {
370 kapp->dcopClient()->call("kded", "kded", "unloadModule(QCString)",
371 data, rettype, retval);
376 emit changed(false);
379 void KSmartcardConfig::defaults()
381 if (_ok) {
382 base->enableSupport->setChecked(false);
383 base->enablePolling->setChecked(true);
384 base->beepOnInsert->setChecked(true);
385 base->launchManager->setChecked(true);
387 emit changed(true);
393 QString KSmartcardConfig::quickHelp() const
395 return i18n("<h1>smartcard</h1> This module allows you to configure KDE support"
396 " for smartcards. These can be used for various tasks such as storing"
397 " SSL certificates and logging in to the system.");
400 extern "C"
402 KDE_EXPORT void kcminit_smartcard()
404 KConfig *config = new KConfig("ksmartcardrc", false, false);
405 bool start = config->readEntry("Enable Support", false);
406 delete config;
408 if (start) {
409 QByteArray data, retval;
410 QCString rettype;
411 QDataStream arg(&data, QIODevice::WriteOnly);
413 arg.setVersion(QDataStream::Qt_3_1);
414 DCOPCString modName = "kardsvc";
415 arg << modName;
416 kapp->dcopClient()->call("kded", "kded", "loadModule(QCString)",
417 data, rettype, retval);
423 #include "smartcard.moc"