add more spacing
[personal-kdebase.git] / apps / kinfocenter / usbview / usbdb.cpp
blob16b542f4cfa8ae44e5b0bfd53e0c1334135c950f
1 /***************************************************************************
2 * Copyright (C) 2001 by Matthias Hoelzer-Kluepfel <mhk@caldera.de> *
3 * *
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. *
8 * *
9 ***************************************************************************/
11 #include "usbdb.h"
13 #include <iostream>
15 #include <QFile>
16 #include <QRegExp>
17 //Added by qt3to4:
18 #include <QTextStream>
20 #include <kstandarddirs.h>
22 USBDB::USBDB() {
23 QString db = "/usr/share/hwdata/usb.ids"; /* on Fedora */
24 if (!QFile::exists(db))
25 db = KStandardDirs::locate("data", "kcmusb/usb.ids");
26 if (db.isEmpty())
27 return;
29 QFile f(db);
31 if (f.open(QIODevice::ReadOnly)) {
32 QTextStream ts(&f);
33 ts.setCodec("UTF-8");
35 QString line, name;
36 int id=0, subid=0, protid=0;
37 QRegExp vendor("[0-9a-fA-F]+ ");
38 QRegExp product("\\s+[0-9a-fA-F]+ ");
39 QRegExp cls("C [0-9a-fA-F][0-9a-fA-F]");
40 QRegExp subclass("\\s+[0-9a-fA-F][0-9a-fA-F] ");
41 QRegExp prot("\\s+[0-9a-fA-F][0-9a-fA-F] ");
42 while (!ts.atEnd()) {
43 line = ts.readLine();
44 if (line.left(1) == "#" || line.trimmed().isEmpty())
45 continue;
47 // skip AT lines
48 if (line.left(2) == "AT")
49 continue;
51 if (cls.indexIn(line) == 0 && cls.matchedLength() == 4) {
52 id = line.mid(2,2).toInt(0, 16);
53 name = line.mid(4).trimmed();
54 _classes.insert(QString("%1").arg(id), name);
55 } else if (prot.indexIn(line) == 0 && prot.matchedLength() > 5) {
56 line = line.trimmed();
57 protid = line.left(2).toInt(0, 16);
58 name = line.mid(4).trimmed();
59 _classes.insert(QString("%1-%2-%3").arg(id).arg(subid).arg(protid), name);
60 } else if (subclass.indexIn(line) == 0 && subclass.matchedLength() > 4) {
61 line = line.trimmed();
62 subid = line.left(2).toInt(0, 16);
63 name = line.mid(4).trimmed();
64 _classes.insert(QString("%1-%2").arg(id).arg(subid), name);
65 } else if (vendor.indexIn(line) == 0 && vendor.matchedLength() == 5) {
66 id = line.left(4).toInt(0, 16);
67 name = line.mid(6);
68 _ids.insert(QString("%1").arg(id), name);
69 } else if (product.indexIn(line) == 0 && product.matchedLength() > 5) {
70 line = line.trimmed();
71 subid = line.left(4).toInt(0, 16);
72 name = line.mid(6);
73 _ids.insert(QString("%1-%2").arg(id).arg(subid), name);
78 f.close();
82 QString USBDB::vendor(int id) {
83 QString s = _ids[QString("%1").arg(id)];
84 if (id != 0) {
85 return s;
87 return QString();
90 QString USBDB::device(int vendor, int id) {
91 QString s = _ids[QString("%1-%2").arg(vendor).arg(id)];
92 if ((id != 0) && (vendor != 0))
93 return s;
94 return QString();
97 QString USBDB::cls(int cls) {
98 return _classes[QString("%1").arg(cls)];
101 QString USBDB::subclass(int cls, int sub) {
102 return _classes[QString("%1-%2").arg(cls).arg(sub)];
105 QString USBDB::protocol(int cls, int sub, int prot) {
106 return _classes[QString("%1-%2-%3").arg(cls).arg(sub).arg(prot)];