summaryrefslogtreecommitdiffstats
path: root/kcontrol/usbview/usbdb.cpp
blob: c786ca40e27b924b1498f334407fdf7ffa74ca87 (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
/***************************************************************************
 *   Copyright (C) 2001 by Matthias Hoelzer-Kluepfel <mhk@caldera.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 <config.h>

#include <iostream>


#include <tqfile.h>
#include <tqregexp.h>


#include <kstandarddirs.h>


#include "usbdb.h"


USBDB::USBDB()
{
#ifndef USBIDS_FILE
  TQString db = "/usr/share/hwdata/usb.ids"; /* on Fedora */
  if (!TQFile::exists(db))
	db = locate("data", "kcmusb/usb.ids");
#else
  TQString db = USBIDS_FILE;
#endif
  if (db.isEmpty())
    return;

  _classes.setAutoDelete(true);
  _ids.setAutoDelete(true);

  TQFile f(db);

  if (f.open(IO_ReadOnly))
    {
      TQTextStream ts(&f);

      TQString line, name;
      int id=0, subid=0, protid=0;
      TQRegExp vendor("[0-9a-fA-F]+ ");
      TQRegExp product("\\s+[0-9a-fA-F]+ ");
      TQRegExp cls("C [0-9a-fA-F][0-9a-fA-F]");
      TQRegExp subclass("\\s+[0-9a-fA-F][0-9a-fA-F]  ");
      TQRegExp prot("\\s+[0-9a-fA-F][0-9a-fA-F]  ");
      while (!ts.eof())
	{
	  line = ts.readLine();
	  if (line.left(1) == "#" || line.stripWhiteSpace().isEmpty())
	    continue;

	  // skip AT lines
	  if (line.left(2) == "AT")
	    continue;

	  if (cls.search(line) == 0 && cls.matchedLength() == 4)
	    {
	      id = line.mid(2,2).toInt(0, 16);
	      name = line.mid(4).stripWhiteSpace();
	      _classes.insert(TQString("%1").arg(id), new TQString(name));
	    }
	  else if (prot.search(line) == 0 && prot.matchedLength() > 5)
	    {
	      line = line.stripWhiteSpace();
	      protid = line.left(2).toInt(0, 16);
	      name = line.mid(4).stripWhiteSpace();
	      _classes.insert(TQString("%1-%2-%3").arg(id).arg(subid).arg(protid), new TQString(name));
	    }
	  else if (subclass.search(line) == 0 && subclass.matchedLength() > 4)
	    {
	      line = line.stripWhiteSpace();
	      subid = line.left(2).toInt(0, 16);
	      name = line.mid(4).stripWhiteSpace();
	      _classes.insert(TQString("%1-%2").arg(id).arg(subid), new TQString(name));
	    }
	  else if (vendor.search(line) == 0 && vendor.matchedLength() == 5)
	    {
	      id = line.left(4).toInt(0,16);
	      name = line.mid(6);
	      _ids.insert(TQString("%1").arg(id), new TQString(name));
	    }
	  else if (product.search(line) == 0 && product.matchedLength() > 5 )
	    {
	      line = line.stripWhiteSpace();
	      subid = line.left(4).toInt(0,16);
	      name = line.mid(6);
	      _ids.insert(TQString("%1-%2").arg(id).arg(subid), new TQString(name));
	    }

	}

      f.close();
    }
}


TQString USBDB::vendor(int id)
{
  TQString *s = _ids[TQString("%1").arg(id)];
  if ((id!= 0) && s)
    {
      return *s;
    }
  return TQString::null;
}


TQString USBDB::device(int vendor, int id)
{
  TQString *s = _ids[TQString("%1-%2").arg(vendor).arg(id)];
  if ((id != 0) && (vendor != 0) && s)
    return *s;
  return TQString::null;
}


TQString USBDB::cls(int cls)
{
  TQString *s = _classes[TQString("%1").arg(cls)];
  if (s)
    return *s;
  return TQString::null;
}


TQString USBDB::subclass(int cls, int sub)
{
  TQString *s = _classes[TQString("%1-%2").arg(cls).arg(sub)];
  if (s)
    return *s;
  return TQString::null;
}


TQString USBDB::protocol(int cls, int sub, int prot)
{
  TQString *s = _classes[TQString("%1-%2-%2").arg(cls).arg(sub).arg(prot)];
  if (s)
    return *s;
  return TQString::null;
}