summaryrefslogtreecommitdiffstats
path: root/kcontrol/smartcard/smartcard.cpp
blob: b1678d2eaa631b6ea8403661726734d265ca173f (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**
 * smartcard.cpp
 *
 * Copyright (c) 2001 George Staikos <staikos@kde.org>
 * Copyright (c) 2001 Fernando Llobregat <fernando.llobregat@free.fr>
 *
 *  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 <tqcheckbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqpushbutton.h>

#include <dcopclient.h>

#include <kaboutdata.h>
#include <kapplication.h>
#include <kcarddb.h>
#include <kcardfactory.h>
#include <kcardgsm_impl.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kdialog.h>
#include <kglobal.h>
#include <klistview.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpopupmenu.h>

#include "smartcard.h"

KSmartcardConfig::KSmartcardConfig(TQWidget *parent, const char *name)
  : KCModule(parent, name),DCOPObject(name)
{

  TQVBoxLayout *layout = new TQVBoxLayout(this, KDialog::marginHint(), KDialog::spacingHint());
  config = new KConfig("ksmartcardrc", false, false);

  DCOPClient *dc = KApplication::kApplication()->dcopClient();

  _ok = false;
  dc->remoteInterfaces("kded", "kardsvc", &_ok);

  KAboutData *about =
  new KAboutData(I18N_NOOP("kcmsmartcard"), I18N_NOOP("TDE Smartcard Control Module"),
                0, 0, KAboutData::License_GPL,
                I18N_NOOP("(c) 2001 George Staikos"));

  about->addAuthor("George Staikos", 0, "staikos@kde.org");
  setAboutData( about );

  if (_ok) {


     base = new SmartcardBase(this);
     layout->add(base);

     _popUpKardChooser = new KPopupMenu(this,"KpopupKardChooser");
     _popUpKardChooser->insertItem(i18n("Change Module..."),
				   this,
				   TQT_SLOT(slotLaunchChooser()));
     // The config backend

     connect(base->launchManager, TQT_SIGNAL(clicked()), TQT_SLOT( changed() ));
     connect(base->beepOnInsert,  TQT_SIGNAL(clicked()), TQT_SLOT( changed() ));
     connect(base->enableSupport, TQT_SIGNAL(clicked()), TQT_SLOT( changed() ));


     connect(base->enablePolling, TQT_SIGNAL(clicked()), TQT_SLOT( changed() ));
     connect(base->_readerHostsListView,
	     TQT_SIGNAL(rightButtonPressed(TQListViewItem *,const TQPoint &,int)),
	     this,
	     TQT_SLOT(slotShowPopup(TQListViewItem *,const TQPoint &,int)));



     if (!connectDCOPSignal("",
			    "",
			    "signalReaderListChanged(TQStringList)",
			    "loadReadersTab(TQStringList)",
			    FALSE))

       kdDebug()<<"Error connecting to DCOP server" <<endl;


     if (!connectDCOPSignal("",
			    "",
			    "signalCardStateChanged(TQString,bool,TQString)",
			    "updateReadersState (TQString,bool,TQString) ",
			    FALSE))

       kdDebug()<<"Error connecting to DCOP server" <<endl;
     _cardDB= new KCardDB();
     load();
  } else {
     layout->add(new NoSmartcardBase(this));
  }
}




KSmartcardConfig::~KSmartcardConfig()
{
    delete config;
    delete _cardDB;
}

void KSmartcardConfig::slotLaunchChooser(){


  if ( KCardDB::launchSelector(base->_readerHostsListView->currentItem()->parent()->text(0))){

    KMessageBox::sorry(this,i18n("Unable to launch KCardChooser"));
  }


}

void KSmartcardConfig::slotShowPopup(TQListViewItem * item ,const TQPoint & _point,int i)
{

  //The popup only appears in cards, not in the slots1
  if (item->isSelectable()) return;
  _popUpKardChooser->exec(_point);

}


void KSmartcardConfig::updateReadersState (TQString readerName,
                                           bool isCardPresent,
                                           TQString atr) {

    KListViewItem * tID=(KListViewItem *) base->_readerHostsListView->findItem(readerName, 0);
    if (tID==0) return;

    KListViewItem * tIDChild=(KListViewItem*) tID->firstChild();
    if (tIDChild==NULL) return;

    delete tIDChild;

    if (!isCardPresent)
                (void) new KListViewItem(tID,i18n("No card inserted"));
    else{

        getSupportingModule(tID,atr);
    }


}



void KSmartcardConfig::loadReadersTab( TQStringList lr){

  //Prepare data for dcop calls
  TQByteArray data, retval;
  TQCString rettype;
  TQDataStream arg(data, IO_WriteOnly);
  TQCString modName = "kardsvc";
  arg << modName;

  //  New view items
  KListViewItem * temp;

  //If the smartcard support is disabled we unload the kardsvc KDED module
  //  and return

  base->_readerHostsListView->clear();

  if (!config->readBoolEntry("Enable Support", false)){




    //  New view items
    KListViewItem * temp;
    kapp->dcopClient()->call("kded", "kded", "unloadModule(TQCString)",
			     data, rettype, retval);

    (void) new KListViewItem(base->_readerHostsListView,
			     i18n("Smart card support disabled"));


    return;

  }

  if (lr.isEmpty()){


    (void) new KListViewItem(base->_readerHostsListView,
			     i18n("No readers found. Check 'pcscd' is running"));
    return;
  }

  for (TQStringList::Iterator _slot=lr.begin();_slot!=lr.end();++_slot){

   temp= new KListViewItem(base->_readerHostsListView,*_slot);


   TQByteArray dataATR;
   TQDataStream argATR(dataATR,IO_WriteOnly);
   argATR << *_slot;

   kapp->dcopClient()->call("kded", "kardsvc", "getCardATR(TQString)",
			   dataATR, rettype, retval);


   TQString cardATR;
   TQDataStream retReaderATR(retval, IO_ReadOnly);
   retReaderATR>>cardATR;

   if (cardATR.isNull()){

     (void) new KListViewItem(temp,i18n("NO ATR or no card inserted"));
     continue;
   }

   getSupportingModule(temp,cardATR);




  }

}


void KSmartcardConfig::getSupportingModule( KListViewItem * ant,
                                            TQString & cardATR) const{


    if (cardATR.isNull()){

        (void) new KListViewItem(ant,i18n("NO ATR or no card inserted"));
        return;
    }


    TQString modName=_cardDB->getModuleName(cardATR);
    if (!modName.isNull()){
        TQStringList mng= TQStringList::split(",",modName);
        TQString type=mng[0];
        TQString subType=mng[1];
        TQString subSubType=mng[2];
        KListViewItem * hil =new KListViewItem(ant,
                                               i18n("Managed by: "),
                                               type,
                                               subType,
                                               subSubType);
        hil->setSelectable(FALSE);
    }
    else{


        KListViewItem * hil =new KListViewItem(ant,
                                               i18n("No module managing this card"));
        hil->setSelectable(FALSE);
    }

  }
void KSmartcardConfig::load()
{
	load( false );

void KSmartcardConfig::load(bool useDefaults )
{

  //Prepare data for dcop calls
  TQByteArray data, retval;
  TQCString rettype;
  TQDataStream arg(data, IO_WriteOnly);
  TQCString modName = "kardsvc";
  arg << modName;

  //Update the toggle buttons with the current configuration

  config->setReadDefaults( useDefaults );

  if (_ok) {
  base->enableSupport->setChecked(config->readBoolEntry("Enable Support",
							false));
  base->enablePolling->setChecked(config->readBoolEntry("Enable Polling",
							true));
  base->beepOnInsert->setChecked(config->readBoolEntry("Beep on Insert",
						       true));
  base->launchManager->setChecked(config->readBoolEntry("Launch Manager",
							true));
  }

  // We call kardsvc to retrieve the current readers
  kapp->dcopClient()->call("kded", "kardsvc", "getSlotList ()",
			   data, rettype, retval);
  TQStringList readers;
  readers.clear();
  TQDataStream retReader(retval, IO_ReadOnly);
  retReader>>readers;

  //And we update the panel
  loadReadersTab(readers);

  emit changed(useDefaults);

}


void KSmartcardConfig::save()
{
if (_ok) {
  config->writeEntry("Enable Support", base->enableSupport->isChecked());
  config->writeEntry("Enable Polling", base->enablePolling->isChecked());
  config->writeEntry("Beep on Insert", base->beepOnInsert->isChecked());
  config->writeEntry("Launch Manager", base->launchManager->isChecked());


  TQByteArray data, retval;
  TQCString rettype;
  TQDataStream arg(data, IO_WriteOnly);
  TQCString modName = "kardsvc";
  arg << modName;

  // Start or stop the server as needed
  if (base->enableSupport->isChecked()) {

    kapp->dcopClient()->call("kded", "kded", "loadModule(TQCString)",
			     data, rettype, retval);
    config->sync();

    kapp->dcopClient()->call("kded", "kardsvc", "reconfigure()",
			     data, rettype, retval);
  } else {



    kapp->dcopClient()->call("kded", "kded", "unloadModule(TQCString)",
			     data, rettype, retval);
  }


}
  emit changed(false);
}

void KSmartcardConfig::defaults()
{
	load( true );
}



TQString KSmartcardConfig::quickHelp() const
{
  return i18n("<h1>smartcard</h1> This module allows you to configure TDE support"
     " for smartcards. These can be used for various tasks such as storing"
     " SSL certificates and logging in to the system.");
}

extern "C"
{
  KDE_EXPORT KCModule *create_smartcard(TQWidget *parent, const char *)
  {
    return new KSmartcardConfig(parent, "kcmsmartcard");
  }

  KDE_EXPORT void init_smartcard()
  {
    KConfig *config = new KConfig("ksmartcardrc", false, false);
    bool start = config->readBoolEntry("Enable Support", false);
    delete config;

    if (start) {
	TQByteArray data, retval;
	TQCString rettype;
	TQDataStream arg(data, IO_WriteOnly);
	TQCString modName = "kardsvc";
	arg << modName;
	kapp->dcopClient()->call("kded", "kded", "loadModule(TQCString)",
			         data, rettype, retval);
    }
  }
}


#include "smartcard.moc"