~ubuntu-branches/ubuntu/hardy/kdebase-workspace/hardy

« back to all changes in this revision

Viewing changes to kcontrol/infocenter/usbview/usbdb.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2008-03-03 11:37:30 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20080303113730-ppdchskh93rr77le
Tags: 4:4.0.2-0ubuntu1
New upstream release

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
 
 
12
 
#include <iostream>
13
 
 
14
 
 
15
 
#include <QFile>
16
 
#include <QRegExp>
17
 
//Added by qt3to4:
18
 
#include <QTextStream>
19
 
 
20
 
 
21
 
#include <kstandarddirs.h>
22
 
 
23
 
 
24
 
#include "usbdb.h"
25
 
 
26
 
 
27
 
USBDB::USBDB()
28
 
{
29
 
  QString db = "/usr/share/hwdata/usb.ids"; /* on Fedora */
30
 
  if (!QFile::exists(db))
31
 
        db = KStandardDirs::locate("data", "kcmusb/usb.ids");
32
 
  if (db.isEmpty())
33
 
    return;
34
 
 
35
 
  _classes.setAutoDelete(true);
36
 
  _ids.setAutoDelete(true);
37
 
 
38
 
  QFile f(db);
39
 
 
40
 
  if (f.open(QIODevice::ReadOnly))
41
 
    {
42
 
      QTextStream ts(&f);
43
 
      ts.setCodec("UTF-8");
44
 
 
45
 
      QString line, name;
46
 
      int id=0, subid=0, protid=0;
47
 
      QRegExp vendor("[0-9a-fA-F]+ ");
48
 
      QRegExp product("\\s+[0-9a-fA-F]+ ");
49
 
      QRegExp cls("C [0-9a-fA-F][0-9a-fA-F]");
50
 
      QRegExp subclass("\\s+[0-9a-fA-F][0-9a-fA-F]  ");
51
 
      QRegExp prot("\\s+[0-9a-fA-F][0-9a-fA-F]  ");
52
 
      while (!ts.atEnd())
53
 
        {
54
 
          line = ts.readLine();
55
 
          if (line.left(1) == "#" || line.trimmed().isEmpty())
56
 
            continue;
57
 
 
58
 
          // skip AT lines
59
 
          if (line.left(2) == "AT")
60
 
            continue;
61
 
 
62
 
          if (cls.indexIn(line) == 0 && cls.matchedLength() == 4)
63
 
            {
64
 
              id = line.mid(2,2).toInt(0, 16);
65
 
              name = line.mid(4).trimmed();
66
 
              _classes.insert(QString("%1").arg(id), new QString(name));
67
 
            }
68
 
          else if (prot.indexIn(line) == 0 && prot.matchedLength() > 5)
69
 
            {
70
 
              line = line.trimmed();
71
 
              protid = line.left(2).toInt(0, 16);
72
 
              name = line.mid(4).trimmed();
73
 
              _classes.insert(QString("%1-%2-%3").arg(id).arg(subid).arg(protid), new QString(name));
74
 
            }
75
 
          else if (subclass.indexIn(line) == 0 && subclass.matchedLength() > 4)
76
 
            {
77
 
              line = line.trimmed();
78
 
              subid = line.left(2).toInt(0, 16);
79
 
              name = line.mid(4).trimmed();
80
 
              _classes.insert(QString("%1-%2").arg(id).arg(subid), new QString(name));
81
 
            }
82
 
          else if (vendor.indexIn(line) == 0 && vendor.matchedLength() == 5)
83
 
            {
84
 
              id = line.left(4).toInt(0,16);
85
 
              name = line.mid(6);
86
 
              _ids.insert(QString("%1").arg(id), new QString(name));
87
 
            }
88
 
          else if (product.indexIn(line) == 0 && product.matchedLength() > 5 )
89
 
            {
90
 
              line = line.trimmed();
91
 
              subid = line.left(4).toInt(0,16);
92
 
              name = line.mid(6);
93
 
              _ids.insert(QString("%1-%2").arg(id).arg(subid), new QString(name));
94
 
            }
95
 
 
96
 
        }
97
 
 
98
 
      f.close();
99
 
    }
100
 
}
101
 
 
102
 
 
103
 
QString USBDB::vendor(int id)
104
 
{
105
 
  QString *s = _ids[QString("%1").arg(id)];
106
 
  if ((id!= 0) && s)
107
 
    {
108
 
      return *s;
109
 
    }
110
 
  return QString();
111
 
}
112
 
 
113
 
 
114
 
QString USBDB::device(int vendor, int id)
115
 
{
116
 
  QString *s = _ids[QString("%1-%2").arg(vendor).arg(id)];
117
 
  if ((id != 0) && (vendor != 0) && s)
118
 
    return *s;
119
 
  return QString();
120
 
}
121
 
 
122
 
 
123
 
QString USBDB::cls(int cls)
124
 
{
125
 
  QString *s = _classes[QString("%1").arg(cls)];
126
 
  if (s)
127
 
    return *s;
128
 
  return QString();
129
 
}
130
 
 
131
 
 
132
 
QString USBDB::subclass(int cls, int sub)
133
 
{
134
 
  QString *s = _classes[QString("%1-%2").arg(cls).arg(sub)];
135
 
  if (s)
136
 
    return *s;
137
 
  return QString();
138
 
}
139
 
 
140
 
 
141
 
QString USBDB::protocol(int cls, int sub, int prot)
142
 
{
143
 
  QString *s = _classes[QString("%1-%2-%3").arg(cls).arg(sub).arg(prot)];
144
 
  if (s)
145
 
    return *s;
146
 
  return QString();
147
 
}
148