summaryrefslogtreecommitdiff
blob: 574d234b2b80a239b844474eb98a64e431251b9d (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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
--- kdebluetooth/kbluetoothd/kcm_btpaired/filesettingsbase.ui
+++ kdebluetooth/kbluetoothd/kcm_btpaired/filesettingsbase.ui
@@ -1,4 +1,4 @@
-<!DOCTYPE UI><UI version="3.2" stdsetdef="1">
+<!DOCTYPE UI><UI version="3.3" stdsetdef="1">
 <class>FileSettingsBase</class>
 <widget class="QWidget">
     <property name="name">
@@ -24,7 +24,7 @@
                 <cstring>textLabel3</cstring>
             </property>
             <property name="text">
-                <string>Here you have to specify the correct start/stop commands for BlueZ's &lt;i&gt;hcid&lt;/i&gt; and the location of BlueZ's &lt;i&gt;link_key&lt;/i&gt; file for the distribution you are using.</string>
+                <string>Here you have to specify the correct start/stop commands for BlueZ's &lt;i&gt;hcid&lt;/i&gt; and the directory of the BlueZ's &lt;i&gt;link_key&lt;/i&gt; files for the distribution you are using.</string>
             </property>
             <property name="alignment">
                 <set>WordBreak|AlignVCenter</set>
@@ -123,7 +123,7 @@
                         <cstring>textLabel1_2</cstring>
                     </property>
                     <property name="text">
-                        <string>Link key file:</string>
+                        <string>Link key directory:</string>
                     </property>
                 </widget>
                 <widget class="KURLRequester" row="0" column="1">
@@ -138,6 +138,9 @@
                             <verstretch>0</verstretch>
                         </sizepolicy>
                     </property>
+                    <property name="mode">
+                        <number>26</number>
+                    </property>
                     <property name="toolTip" stdset="0">
                         <string></string>
                     </property>
--- kdebluetooth/kbluetoothd/kcm_btpaired/pairedtab.cpp
+++ kdebluetooth/kbluetoothd/kcm_btpaired/pairedtab.cpp
@@ -44,6 +44,7 @@
 using namespace std;
 using namespace KBluetooth;
 
+/* linkkey file is now ASCII format
 struct HcidLinkKeyStruct {
         bdaddr_t sba;
         bdaddr_t dba;
@@ -54,19 +55,17 @@
         uint8_t  type;
         time_t   time;
 };
+*/
 
 PairedTab::PairedTab(QWidget *parent, const char* name) :
     PairedTabBase(parent, name)
 {
     KConfig* config = KApplication::kApplication()->config();
-    linkKeyFilename = config->readEntry("linkKeyFile", "/etc/bluetooth/link_key");
-    config->writeEntry("linkKeyFile", linkKeyFilename);
-    hcidStartCommand = config->readEntry("hcidStartCommand", "/etc/init.d/bluez-utils start");
-    hcidStopCommand = config->readEntry("hcidStopCommand", "/etc/init.d/bluez-utils stop");
+    linkKeyPath = config->readEntry("linkKeyPath", "/var/lib/bluetooth/");
+    config->writeEntry("linkKeyPath", linkKeyPath);
+    hcidStartCommand = config->readEntry("hcidStartCommand", "/etc/init.d/bluetooth start");
+    hcidStopCommand = config->readEntry("hcidStopCommand", "/etc/init.d/bluetooth stop");
     bDirty = false;
-    linkKeyFileValid = false;
-    reloadList();
-    updateGUI();
     linkKeyFileWatch = new KDirWatch(this);
     connect(linkKeyFileWatch, SIGNAL(dirty(const QString&)),
         this, SLOT(slotKeyFileChanged()));
@@ -84,13 +83,51 @@
     baseDialog->hide();
     connect(baseDialog, SIGNAL(applyClicked()), this, SLOT(slotApplyFileSettings()));
     connect(baseDialog, SIGNAL(okClicked()), this, SLOT(slotApplyFileSettings()));
+
+    watchKeys();
         
-    linkKeyFileWatch->addFile(linkKeyFilename);
     linkKeyFileWatch->startScan(true, true);
+
+    reloadList();
+    updateGUI();
+}
+
+void PairedTab::watchKeys() {
+
+    kdDebug() << "watchKeys()" << endl;    
+
+    QDir keyDir(linkKeyPath); 
+    keyDir.setFilter( QDir::Dirs );
+    keyDir.setNameFilter("*:*:*:*:*");
+
+    QFileInfo *fi;
+    const QFileInfoList *list = keyDir.entryInfoList();
+    QFileInfoListIterator it(*list);
+
+    linkKeyFilename.clear();
+
+    while ((fi = it.current()) != 0) {
+
+	    QString btDeviceDir = fi->fileName();
+	    QString filename = keyDir.absPath();
+	    filename.append("/").append(btDeviceDir).append("/").append("linkkeys");
+
+	    QFile keyFile(filename);
+
+	    if (keyFile.exists()) {
+	         linkKeyFileWatch->addFile(filename);
+		 linkKeyFilename.append(filename);
+	    }
+
+	    ++it;
+    }
 }
 
 void PairedTab::reloadList()
 {
+
+    kdDebug() << "relodList()" << endl;
+
     if (bDirty) {
         if (KMessageBox::warningContinueCancel(this, i18n(
 "The link key file has changed on disk. Do you want to reload the table and \
@@ -98,82 +135,96 @@
             return;
         }
     }    
-
-    QFile file(linkKeyFilename);
-    
-    kdDebug() << "reloadList()" << endl;
     pairingList.clear();
-    linkKeyFileValid = false;
-    if (file.open(IO_ReadOnly)) {    
-        linkKeyFileValid = true;
-        bool readSizeMismatch = false;
-        while (true) {
-            PairingInfo newInfo;
-            HcidLinkKeyStruct linkKeyStruct;
-            int rSize = 0;
-            if ((rSize = file.readBlock((char*)&linkKeyStruct, sizeof(HcidLinkKeyStruct))) 
-                == sizeof(HcidLinkKeyStruct)) 
-            {
-                newInfo.localAddr = DeviceAddress(linkKeyStruct.sba);
-                newInfo.remoteAddr = DeviceAddress(linkKeyStruct.dba);
-                newInfo.time.setTime_t(linkKeyStruct.time);
-                newInfo.type = linkKeyStruct.type;
-                for (int n=0; n<16; ++n) {
-                    newInfo.linkKey[n] = linkKeyStruct.key[n];
-                }
-                newInfo.remoteName = QString(newInfo.remoteAddr);
-                NameCache::getCachedName(newInfo.remoteAddr, newInfo.remoteName);
-                newInfo.remoteClass = 0;
-                NameCache::getCachedClass(newInfo.remoteAddr, newInfo.remoteClass);
-                pairingList.push_back(newInfo);
-            }
-            else {
-                if (rSize != 0) {
-                    readSizeMismatch = true;
-                }
-                break;
-            }
-            kdDebug() << "localAddr read:" << QString(newInfo.localAddr) << endl;
-        }     
-        
-        linkKeyFileValid = !readSizeMismatch;
-        file.close();
+
+    QStringList::iterator it;
+    for (it = linkKeyFilename.begin(); it !=  linkKeyFilename.end(); ++it) {
+
+	    QFile file((*it));
+
+	    if (file.open(IO_ReadOnly)) {    
+
+		QTextStream stream(&file);
+		QString line, localAddr, remoteAddr, symLink, remoteClass;
+
+		while (!stream.atEnd()) {
+		    PairingInfo newInfo;
+
+		    line = stream.readLine();
+
+		    QFileInfo fi(file);
+		    QDir dir(fi.dir());
+		    localAddr = dir.dirName();
+
+		    remoteAddr = line.left(17);
+
+		    newInfo.localAddr = DeviceAddress(localAddr);
+		    newInfo.remoteAddr = DeviceAddress(remoteAddr);
+
+		    // XXX: not provided by linkkey file at the moment
+	            // newInfo.time.setTime_t(linkKeyStruct.time);
+
+		    newInfo.linkKey = line.mid(18, 32); 
+		    
+		    newInfo.remoteName = QString(newInfo.remoteAddr);
+		    NameCache::getCachedName(newInfo.remoteAddr, newInfo.remoteName);
+		    
+		    newInfo.remoteClass = 0;	// not provided by linkkey file at the moment(?) FIXME 
+
+		    NameCache::getCachedClass(newInfo.remoteAddr, newInfo.remoteClass);
+		    pairingList.push_back(newInfo);
+		}     
+		
+		file.close();
+	    }
     }
 }
 
 bool PairedTab::saveList()
 {
+    kdDebug() << "saveList()" << endl;
+
     if (bDirty == false) return true;    
 
-    kdDebug() << "saveList()" << endl;
-    
     if (!stopDaemon()) return false;
-    
-    QFile file(linkKeyFilename);
-    if (file.open(IO_WriteOnly)) {
-        for (unsigned int n=0; n<pairingList.size(); ++n) {
-            HcidLinkKeyStruct info;
-            info.dba = pairingList[n].remoteAddr.getBdaddr(false);
-            for (int p=0; p<16; ++p) {
-                info.key[p] = pairingList[n].linkKey[p];
-            }
-            info.sba = pairingList[n].localAddr.getBdaddr(false);
-            info.time = pairingList[n].time.toTime_t();
-            info.type = pairingList[n].type;
-            file.writeBlock(reinterpret_cast<char*>(&info), sizeof(info));
-        }
-        file.close();
-        
-        if (!startDaemon()) return false;
 
-        bDirty = false;
-        return true;
+    QStringList::iterator it;
+    for (it = linkKeyFilename.begin(); it !=  linkKeyFilename.end(); ++it) {
+
+	    QFile file((*it));
+	    QFileInfo fi(file);
+	    QDir deviceDir(fi.dir());
+	   
+	    // clear linkkey file
+	    file.remove();
+
+	    if (file.open(IO_WriteOnly)) {
+		QTextStream  stream(&file);
+		for (unsigned int n=0; n<pairingList.size(); ++n) {
+
+                    if (pairingList[n].localAddr == deviceDir.dirName())
+			   continue;
+
+		    stream << pairingList[n].remoteAddr << " ";
+		    stream << pairingList[n].linkKey << " ";
+		    stream << pairingList[n].remoteClass << '\n';
+		}
+		file.close();
+
+		bDirty = false;
+	    }
     }
-    else {
-        KMessageBox::error(this, i18n("Could not write \
-link key file. Your changes cannot be saved."), i18n("Error"));
-        return false;
+
+    if (bDirty) {
+	KMessageBox::error(this, i18n("Could not write " 
+	"all link key files. Your complete changes cannot be saved."), i18n("Error"));	// XXX String update
+
+	return false;
     }
+
+    if (!startDaemon()) return false;
+
+    return true;
 }
 
 bool PairedTab::stopDaemon()
@@ -192,34 +243,53 @@
 
 void PairedTab::updateGUI()
 {
-    vector<PairingInfo>::iterator pairIt;
+    kdDebug() << "updateGUI()" << endl;	
+    vector<PairingInfo>::iterator pairIt, remoteIt;
+    KBluetooth::DeviceAddress prevLocalAddr;
+    QListViewItem *viewItem = NULL;
     pairingListView->clear();
     for (pairIt = pairingList.begin(); pairIt != pairingList.end(); ++pairIt) {
-        QListViewItem *viewItem = new QListViewItem(pairingListView);
-        pairIt->listViewItem = viewItem;
-        
-        viewItem->setText(0, QString(pairIt->remoteName));
-        QListViewItem *remoteAddrItem = new QListViewItem(viewItem);
-        remoteAddrItem->setPixmap(0, KGlobal::iconLoader()->loadIcon(
-            "pda_blue", KIcon::Small, 16));
-        remoteAddrItem->setSelectable(false);
-        remoteAddrItem->setText(0, QString(pairIt->remoteAddr));
-        
-        QListViewItem *localAddrItem = new QListViewItem(viewItem);
-        localAddrItem->setPixmap(0, KGlobal::iconLoader()->loadIcon(
-            "usbpendrive_unmount", KIcon::Small, 16));
-        localAddrItem->setSelectable(false);
-        localAddrItem->setText(0, QString(pairIt->localAddr));
-        
+	
+	if (prevLocalAddr != pairIt->localAddr) {    
+		kdDebug() << ">> " << pairIt->localAddr << endl;   
+		viewItem = new QListViewItem(pairingListView);
+		viewItem->setSelectable(false);
+		viewItem->setText(0, QString(pairIt->localAddr));
+		viewItem->setOpen(true);
+		viewItem->setPixmap(0, KGlobal::iconLoader()->loadIcon(
+		    "usbpendrive_unmount", KIcon::Small, 16));
+	}
+
+	
+	remoteIt = pairIt;
+	if (remoteIt->localAddr == pairIt->localAddr) {
+		kdDebug() << "\t>> " << remoteIt->remoteAddr << endl;
+		QListViewItem *remoteAddrItem = new QListViewItem(viewItem);
+		remoteIt->listViewItem = remoteAddrItem;
+		QString iconName = DeviceClassMimeConverter::classToIconName(remoteIt->remoteClass);
+		remoteAddrItem->setPixmap(0, KGlobal::iconLoader()->loadIcon(
+		    iconName, KIcon::Small, 16));
+
+		remoteAddrItem->setSelectable(true);
+
+		kdDebug() << "remoteName: " << remoteIt->remoteName << endl;
+		if (remoteIt->remoteName == remoteIt->remoteAddr)
+			remoteAddrItem->setText(0, QString(remoteIt->remoteAddr));
+		else
+			remoteAddrItem->setText(0, QString(remoteIt->remoteAddr).append(" / ").append(remoteIt->remoteName));
+	}
+
+
+	/* not provided by the linkkey file at the moment       	
+
         QListViewItem *timeItem = new QListViewItem(viewItem);
         timeItem->setPixmap(0, KGlobal::iconLoader()->loadIcon(
             "clock", KIcon::Small, 16));
         timeItem->setSelectable(false);
         timeItem->setText(0, pairIt->time.toString());
+	*/	
+	prevLocalAddr = pairIt->localAddr;
         
-        QString iconName = DeviceClassMimeConverter::classToIconName(pairIt->remoteClass);
-        viewItem->setPixmap(0, KGlobal::iconLoader()->loadIcon(
-            iconName, KIcon::Small, 16));
     }
 }
 
@@ -232,9 +302,14 @@
 
 void PairedTab::slotRemovePairing()
 {
+    kdDebug() << "slotRemovePairing()" << endl;
     bool bDirty = false;
+    kdDebug() << "list size: " << pairingList.size() << endl;
     for (int n=pairingList.size()-1; n>=0; --n) {
+        kdDebug() <<  n << " remote: " << pairingList[n].remoteAddr << pairingList[n].listViewItem->text(0) << pairingList[n].listViewItem->isSelected() << endl;
         if (pairingList[n].listViewItem->isSelected()) {
+	    kdDebug() << "erase pair: " << n << endl;
+
             pairingList.erase(pairingList.begin()+n);
             bDirty = true;
         }
@@ -248,20 +323,22 @@
 
 void PairedTab::slotSelectionChanged()
 {
-    for (QListViewItem* i=pairingListView->firstChild(); i != NULL;
-         i = i->nextSibling()) 
-    {
-        if (i->isSelected()) {
-            removePairingButton->setEnabled(true);
-            return;
-        }
+    for (QListViewItem* i=pairingListView->firstChild(); i != NULL; i = i->nextSibling()) {
+        for (QListViewItem *child=i->firstChild(); child != NULL; child = child->nextSibling())
+	{	
+            if (child->isSelected()) {
+                removePairingButton->setEnabled(true);
+                return;
+            }
+	}
     }
+
     removePairingButton->setEnabled(false);
 }
 
 void PairedTab::slotFileSettings()
 {
-    fileSettingsDialog->linkKeyFileEdit->setURL(linkKeyFilename);
+    fileSettingsDialog->linkKeyFileEdit->setURL(linkKeyPath);
     fileSettingsDialog->startCommandEdit->setURL(hcidStartCommand);
     fileSettingsDialog->stopCommandEdit->setURL(hcidStopCommand);
     
@@ -270,20 +347,26 @@
 
 void PairedTab::slotApplyFileSettings()
 {
-    linkKeyFileWatch->removeFile(linkKeyFilename);
+    QStringList::iterator it;
+    for (it = linkKeyFilename.begin(); it !=  linkKeyFilename.end(); ++it) {
+        linkKeyFileWatch->removeFile((*it));
+    }
     
-    linkKeyFilename = fileSettingsDialog->linkKeyFileEdit->url();
+    linkKeyPath = fileSettingsDialog->linkKeyFileEdit->url();
     hcidStartCommand = fileSettingsDialog->startCommandEdit->url();
     hcidStopCommand = fileSettingsDialog->stopCommandEdit->url();
+
+    kdDebug() << "keypath: " << fileSettingsDialog->linkKeyFileEdit->url() << endl;
     
     KConfig* config = KApplication::kApplication()->config();
-    config->writeEntry("linkKeyFile", linkKeyFilename);
+    config->writeEntry("linkKeyPath", linkKeyPath);
     config->writeEntry("hcidStartCommand", hcidStartCommand);
     config->writeEntry("hcidStopCommand", hcidStopCommand);
+
+    watchKeys();
+
     reloadList();
     updateGUI();
-
-    linkKeyFileWatch->addFile(linkKeyFilename);
 }
 
 
--- kdebluetooth/kbluetoothd/kcm_btpaired/pairedtab.h
+++ kdebluetooth/kbluetoothd/kcm_btpaired/pairedtab.h
@@ -43,7 +43,7 @@
         KBluetooth::DeviceAddress remoteAddr;
         QString remoteName;
         int remoteClass;
-        uint8_t linkKey[16];
+        QString linkKey;
         uint8_t type;
         QDateTime time;
         QListViewItem *listViewItem;
@@ -52,12 +52,14 @@
     bool linkKeyFileValid;
     bool bDirty;
     
-    QString linkKeyFilename;
+    QValueList <QString> linkKeyFilename;
+    QString linkKeyPath;
     QString hcidStartCommand;
     QString hcidStopCommand;
     KDirWatch *linkKeyFileWatch;
     FileSettingsBase *fileSettingsDialog;
     
+    void watchKeys();
     void reloadList();
     bool saveList();
     bool startDaemon();