~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kinfocenter/Modules/usbview/usbdb.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 ***************************************************************************/
 
10
 
 
11
#include "usbdb.h"
 
12
 
 
13
#include <iostream>
 
14
 
 
15
#include <QFile>
 
16
#include <QRegExp>
 
17
//Added by qt3to4:
 
18
#include <QTextStream>
 
19
 
 
20
#include <kstandarddirs.h>
 
21
 
 
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;
 
28
 
 
29
        QFile f(db);
 
30
 
 
31
        if (f.open(QIODevice::ReadOnly)) {
 
32
                QTextStream ts(&f);
 
33
                ts.setCodec("UTF-8");
 
34
 
 
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;
 
46
 
 
47
                        // skip AT lines
 
48
                        if (line.left(2) == "AT")
 
49
                                continue;
 
50
 
 
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);
 
74
                        }
 
75
 
 
76
                }
 
77
 
 
78
                f.close();
 
79
        }
 
80
}
 
81
 
 
82
QString USBDB::vendor(int id) {
 
83
        QString s = _ids[QString("%1").arg(id)];
 
84
        if (id != 0) {
 
85
                return s;
 
86
        }
 
87
        return QString();
 
88
}
 
89
 
 
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();
 
95
}
 
96
 
 
97
QString USBDB::cls(int cls) {
 
98
        return _classes[QString("%1").arg(cls)];
 
99
}
 
100
 
 
101
QString USBDB::subclass(int cls, int sub) {
 
102
        return _classes[QString("%1-%2").arg(cls).arg(sub)];
 
103
}
 
104
 
 
105
QString USBDB::protocol(int cls, int sub, int prot) {
 
106
        return _classes[QString("%1-%2-%3").arg(cls).arg(sub).arg(prot)];
 
107
}
 
108