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
|
//-*-c++-*-
/***************************************************************************
* Copyright (C) 2003 by Fred Schaettgen *
* kdebluetooth@schaettgen.de *
* *
* 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. *
***************************************************************************/
#include "tdeiobluetooth.h"
#include <sys/stat.h>
#include <dcopclient.h>
#include <tdeapplication.h>
#include <tdecmdlineargs.h>
#include <tqregexp.h>
#include <devicemimeconverter.h>
#include <deviceImpl.h>
#include <btuuids.h>
using namespace TDEBluetooth;
static const TDECmdLineOptions options[] =
{
{ "+protocol", I18N_NOOP( "Protocol name" ), 0 },
{ "+pool", I18N_NOOP( "Socket name" ), 0 },
{ "+app", I18N_NOOP( "Socket name" ), 0 },
TDECmdLineLastOption
};
extern "C"
{
KDE_EXPORT int kdemain(int argc, char **argv) {
TDEInstance instance( "tdeio_bluetooth" );
kdDebug() << "*** Starting tdeio_bluetooth " << endl;
if (argc != 4) {
kdDebug() << "Usage: tdeio_bluetooth protocol domain-socket1 domain-socket2" << endl;
exit(-1);
}
putenv(strdup("SESSION_MANAGER="));
TDECmdLineArgs::init(argc, argv, "tdeio_bluetooth", 0, 0, 0, 0);
TDECmdLineArgs::addCmdLineOptions( options );
TDEApplication app( false, false, false );
app.dcopClient()->attach();
TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
TDEioBluetooth slave( args->arg(0), args->arg(1), args->arg(2) );
kdDebug() << "*** protocol " << args->arg(0) << endl;
kdDebug() << "*** pool socket " << args->arg(1) << endl;
kdDebug() << "*** app socket " << args->arg(2) << endl;
slave.dispatchLoop();
kdDebug() << "*** tdeio_bluetooth Done" << endl;
return 0;
}
}
TDEioBluetooth::TDEioBluetooth(const TQCString &protocol, const TQCString &pool_socket, const TQCString &app_socket) :
ForwardingSlaveBase(protocol, pool_socket, app_socket)
{
kdDebug() << k_funcinfo << endl;
TDELocale::setMainCatalogue("tdebluez");
TQT_DBusError error;
adapter = 0;
manager = new TDEBluetooth::ObjectManagerImpl("org.bluez", "/"/*, this, "ObexObjectManager"*/);
if (!manager)
{
ForwardingSlaveBase::error(TDEIO::ERR_SERVICE_NOT_AVAILABLE, i18n("Bluetooth Manager not found"));
closeConnection();
}
else
{
if (!manager->getAdapters().isEmpty())
{
TDEBluetooth::ObjectManagerImpl::AdapterList al = manager->getAdapters();
TDEBluetooth::ObjectManagerImpl::AdapterList::Iterator ait = al.begin();
for (ait; ait != al.end(); ++ait)
{
TDEBluetooth::AdapterImpl *ad = new TDEBluetooth::AdapterImpl("org.bluez", (*ait));
ad->setConnection((*(manager->getConnection())));
// FIXME implement multiple adapters
if (ad->getPowered(error))
{
adapter = ad;
break;
}
}
connect(manager, TQ_SIGNAL(deviceAdded(const TQString &)),
this, TQ_SLOT(slotAddDevice(const TQString &)));
connect(manager, TQ_SIGNAL(deviceRemoved(const TQString &)),
this, TQ_SLOT(slotRemoveDevice(const TQString &)));
connect(manager, TQ_SIGNAL(adapterPowerOnChanged(const TQString&, bool )),
this, TQ_SLOT(slotAdapterPowerOnChanged(const TQString &, bool )));
}
else
{
ForwardingSlaveBase::error(TDEIO::ERR_SERVICE_NOT_AVAILABLE, i18n("No adapter found"));
closeConnection();
}
}
}
TDEioBluetooth::~TDEioBluetooth()
{
kdDebug() << k_funcinfo << endl;
if (manager) delete manager;
if (adapter) delete adapter;
}
void TDEioBluetooth::closeConnection()
{
kdDebug() << k_funcinfo << endl;
exit();
}
void TDEioBluetooth::stat(const KURL &url)
{
kdDebug() << __func__ << "(" << url.prettyURL() << ")" << endl;
TDEIO::UDSEntry entry;
if (!adapter)
{
// ForwardingSlaveBase::warning(i18n("Bluetooth Adapter not found"));
// ForwardingSlaveBase::error(TDEIO::ERR_SERVICE_NOT_AVAILABLE, i18n("Bluetooth Adapter not found"));
TQString name = "No device found";
TQ_UINT32 devClass = 0;
addAtom(entry, TDEIO::UDS_NAME, name);
addAtom(entry, TDEIO::UDS_FILE_TYPE, S_IFDIR);
addAtom(entry, TDEIO::UDS_ACCESS, 0555);
addAtom(entry, TDEIO::UDS_MIME_TYPE, DeviceMimeConverter::classToMimeType(devClass));
addAtom(entry, TDEIO::UDS_ICON_NAME, DeviceMimeConverter::classToIconName(devClass));
return;
}
TQT_DBusError dbuserror;
TQString path = url.path();
if (path.isEmpty() || path == "/")
{
// The root is "virtual" - it's not a single physical directory
createTopLevelEntry(entry);
}
else if (path.find(TQRegExp("/\\[([0-9A-F]{2}:){5}[0-9A-F]{2}\\]"), 0) != -1)
{
createDirEntry(entry, path, path);
}
else
{
ForwardingSlaveBase::error(TDEIO::ERR_MALFORMED_URL, i18n("Could not stat %1.").arg(url.prettyURL()));
}
statEntry(entry);
finished();
}
void TDEioBluetooth::listDir(const KURL &url)
{
kdDebug() << k_funcinfo << endl;
if (!adapter)
{
ForwardingSlaveBase::error(TDEIO::ERR_SERVICE_NOT_AVAILABLE, i18n("Bluetooth Adapter not found"));
return;
}
TDEIO::UDSEntry entry;
TQValueList<TDEIO::UDSEntry> list;
TQT_DBusError error;
TDEIO::UDSEntryList entries;
TQString path = url.path();
TQString name = adapter->getName(error);
kdDebug() << __func__ << "(" << path << ")" << endl;
TQRegExp rx("/" + name + "/\\[([0-9A-F]{2}:){5}[0-9A-F]{2}\\]");
kdDebug() << "Regex: " << rx.search(path) << endl;
if (rx.search(path) == 0)
{
listServices(list, url);
}
else if (path == "/" + name)
{
listDevices(list, url);
}
else if (path == "/")
{
createTopLevelEntry(entry);
listEntry(entry, false);
}
else
{
ForwardingSlaveBase::listDir(url);
}
if (list.count() > 0)
{
kdDebug() << __func__ << "(" << path << ")" << endl;
totalSize(list.count() + 1);
TDEIO::UDSEntryListIterator it = list.begin();
TDEIO::UDSEntryListIterator end = list.end();
for (; it != end; ++it)
{
entries.append(*it);
}
listEntries(entries);
}
listEntry(entry, true);
finished();
return;
}
bool TDEioBluetooth::rewriteURL(const KURL &url, KURL &newUrl)
{
kdDebug() << k_funcinfo << endl;
TQString path = url.path();
TQString protocol = url.protocol();
if (protocol == "obexopp" || protocol == "obexftp")
{
newUrl = url;
return true;
}
else
{
ForwardingSlaveBase::error(TDEIO::ERR_MALFORMED_URL, url.prettyURL());
return false;
}
}
void TDEioBluetooth::createTopLevelEntry(TDEIO::UDSEntry &entry)
{
kdDebug() << k_funcinfo << endl;
TQT_DBusError error;
TQString name = adapter->getName(error);
TQ_UINT32 devClass = adapter->getClass(error);
addAtom(entry, TDEIO::UDS_NAME, name);
addAtom(entry, TDEIO::UDS_FILE_TYPE, S_IFDIR);
addAtom(entry, TDEIO::UDS_ACCESS, 0555);
addAtom(entry, TDEIO::UDS_MIME_TYPE, DeviceMimeConverter::classToMimeType(devClass));
addAtom(entry, TDEIO::UDS_ICON_NAME, DeviceMimeConverter::classToIconName(devClass));
}
bool TDEioBluetooth::listDevice(TDEIO::UDSEntry &entry, const TQString &path, const KURL &url)
{
kdDebug() << __func__ << "(" << url << ")" << endl;
TQT_DBusError error;
TDEBluetooth::DeviceImpl *dev = new TDEBluetooth::DeviceImpl("org.bluez", path);
dev->setConnection((*(manager->getConnection())));
const TQString addr = dev->getAddress(error);
TQString name = dev->getName(error);
TQString alias = dev->getAlias(error);
const int devClass = dev->getClass(error);
TQString aname = adapter->getName(error);
delete dev;
entry.clear();
if (!alias.isEmpty())
name = alias;
else
name = alias = addr;
createDirEntry(entry, name, TQString("bluetooth:/%1/[%2]").arg(aname).arg(addr),
DeviceMimeConverter::classToMimeType(devClass));
return true;
}
bool TDEioBluetooth::listDevices(TQValueList<TDEIO::UDSEntry> &list, const KURL &url)
{
kdDebug() << __func__ << "(" << url << ")" << endl;
TDEIO::UDSEntry entry;
TDEBluetooth::ObjectManagerImpl::DeviceList dl = manager->getDevices();
TDEBluetooth::ObjectManagerImpl::DeviceList::Iterator dit = dl.begin();
for (dit; dit != dl.end(); ++dit)
{
entry.clear();
listDevice(entry, (*dit), url);
list.append(entry);
}
return true;
}
bool TDEioBluetooth::listServices(TQValueList<TDEIO::UDSEntry> &list, const KURL &url)
{
kdDebug() << __func__ << "url: " << url << endl;
TDEIO::UDSEntry entry;
TQString path = url.path();
kdDebug() << __func__ << "path: " << path << endl;
int pos = path.find(TQRegExp("/\\[([0-9A-F]{2}:){5}[0-9A-F]{2}\\]"), 0);
if (pos != -1)
{
TQString address = path.remove(0, pos + 2).remove(17, path.length());
kdDebug() << __func__ << "address: " << address << endl;
TDEBluetooth::ObjectManagerImpl::DeviceList dl = manager->getDevices();
TDEBluetooth::ObjectManagerImpl::DeviceList::Iterator dit = dl.begin();
for (dit; dit != dl.end(); ++dit)
{
TDEBluetooth::DeviceImpl *d = new TDEBluetooth::DeviceImpl("org.bluez", (*dit));
d->setConnection((*(manager->getConnection())));
TQT_DBusError dbuserror;
TQString addr = d->getAddress(dbuserror);
if (addr == address)
{
TQStringList uuids = d->getUUIDs(dbuserror);
for (TQStringList::Iterator it = uuids.begin();
it != uuids.end(); ++it)
{
entry.clear();
// accepted services OBEX OPP, PCE, FTP
//FIXME temporary disabled
// if ((*it) == "00001105-0000-1000-8000-00805f9b34fb")
// {
// createDirEntry(entry, resolveUUID((*it)), TQString("obexopp:/[%1]/").arg(address), "bluetooth/obex-objectpush-profile");
// addAtom(entry, TDEIO::UDS_NAME, "obexopp");
// list.append(entry);
// }
// else if ((*it) == "00001106-0000-1000-8000-00805f9b34fb")
// {
// createDirEntry(entry, resolveUUID((*it)), TQString("obexftp:/[%1]/").arg(address), "bluetooth/obex-ftp-profile");
// addAtom(entry, TDEIO::UDS_NAME, "obexftp");
// list.append(entry);
// }
}
break;
}
delete d;
}
}
else
{
ForwardingSlaveBase::error(TDEIO::ERR_MALFORMED_URL, url.url());
}
return true;
}
bool TDEioBluetooth::createDirEntry(TDEIO::UDSEntry &entry, const TQString &name, const TQString &dir, const TQString &mimeType)
{
kdDebug() << k_funcinfo << endl;
addAtom(entry, TDEIO::UDS_NAME, name);
if (dir != TQString::null)
{
addAtom(entry, TDEIO::UDS_URL, dir);
}
addAtom(entry, TDEIO::UDS_MIME_TYPE, mimeType);
if (mimeType == "inode/directory")
addAtom(entry, TDEIO::UDS_FILE_TYPE, S_IFDIR);
else
addAtom(entry, TDEIO::UDS_FILE_TYPE, S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
return true;
}
void TDEioBluetooth::slotAddDevice(const TQString &path)
{
kdDebug() << k_funcinfo << endl;
listDir("bluetooth:/");
}
void TDEioBluetooth::slotRemoveDevice(const TQString &path)
{
kdDebug() << __func__ << "(" << path << ")" << endl;
listDir("bluetooth:/");
// TQT_DBusError error;
// TDEBluetooth::DeviceImpl *d= new TDEBluetooth::DeviceImpl("org.bluez",path);
// d->setConnection((*(manager->getConnection())));
// const TQString address = d->getAddress(error);
// delete d;
//
// listDevice(address);
// listEntry(TDEIO::UDSEntry(), true);
}
void TDEioBluetooth::slotAddService(const KURL &url, const TQString uuid)
{
kdDebug() << __func__ << "(URL=" << url << ", UUID=" << uuid << ")" << endl;
// TQT_DBusError error;
// TDEBluetooth::DeviceImpl *d= new TDEBluetooth::DeviceImpl("org.bluez",path);
// d->setConnection((*(manager->getConnection())));
// const TQString address = d->getAddress(error);
// TQ_UINT32 devclass = d->getClass(error);
// const TQString devicon = DeviceMimeConverter::classToIconName(devclass);
// delete d;
//
// TQMap<TQString,int>::iterator f=qDevicesList.find(address);
// if(f!=qDevicesList.end() && f.data() == devclass) return;
// listEntry(UDSEntry(), true);
}
void TDEioBluetooth::slotAdapterPowerOnChanged(TQString const& path, bool state)
{
kdDebug() << __func__ << "(" << path << ")" << endl;
//
// TQT_DBusError error;
// TDEBluetooth::DeviceImpl *d= new TDEBluetooth::DeviceImpl("org.bluez",path);
// d->setConnection((*(manager->getConnection())));
// const TQString address = d->getAddress(error);
// TQ_UINT32 devclass = d->getClass(error);
// const TQString devicon = DeviceMimeConverter::classToIconName(devclass);
// delete d;
//
// TQMap<TQString,int>::iterator f=qDevicesList.find(address);
// if(f!=qDevicesList.end() && f.data() == devclass) return;
// qDevicesList.insert(address, devclass);
//
// listDevice(address);
// listEntry(UDSEntry(), true);
}
void TDEioBluetooth::addAtom(TDEIO::UDSEntry &entry, TDEIO::UDSAtomTypes type, TQString s)
{
kdDebug() << k_funcinfo << endl;
TDEIO::UDSAtom atom;
atom.m_uds = type;
atom.m_str = s;
entry.append(atom);
}
void TDEioBluetooth::addAtom(TDEIO::UDSEntry &entry, TDEIO::UDSAtomTypes type, long l)
{
kdDebug() << k_funcinfo << endl;
TDEIO::UDSAtom atom;
atom.m_uds = type;
atom.m_long = l;
entry.append(atom);
}
#include "tdeiobluetooth.moc"
|