1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Wenger
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
30 #if defined(Q_OS_WIN32)
41 // Linux and Mac includes
42 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
44 #include <libusb-1.0/libusb.h>
48 #include <sys/utsname.h>
54 #if defined(Q_OS_LINUX)
59 #if defined(Q_OS_MACX)
60 #include <sys/param.h>
61 #include <sys/ucred.h>
62 #include <sys/mount.h>
66 #include "rbsettings.h"
68 /** @brief detect permission of user (only Windows at moment).
69 * @return enum userlevel.
71 #if defined(Q_OS_WIN32)
72 enum System::userlevel
System::userPermissions(void)
75 NET_API_STATUS napistatus
;
76 wchar_t userbuf
[UNLEN
];
77 DWORD usersize
= UNLEN
;
79 enum userlevel result
;
81 status
= GetUserNameW(userbuf
, &usersize
);
85 napistatus
= NetUserGetInfo(NULL
, userbuf
, (DWORD
)1, (LPBYTE
*)&buf
);
87 switch(buf
->usri1_priv
) {
101 NetApiBufferFree(buf
);
106 /** @brief detects user permissions (only Windows at moment).
107 * @return a user readable string with the permission.
109 QString
System::userPermissionsString(void)
112 int perm
= userPermissions();
115 result
= QObject::tr("Guest");
118 result
= QObject::tr("Admin");
121 result
= QObject::tr("User");
124 result
= QObject::tr("Error");
132 /** @brief detects current Username.
133 * @return string with Username.
135 QString
System::userName(void)
137 #if defined(Q_OS_WIN32)
138 wchar_t userbuf
[UNLEN
];
139 DWORD usersize
= UNLEN
;
142 status
= GetUserNameW(userbuf
, &usersize
);
144 return QString::fromWCharArray(userbuf
);
146 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
148 user
= getpwuid(geteuid());
149 return QString(user
->pw_name
);
154 /** @brief detects the OS Version
155 * @return String with OS Version.
157 QString
System::osVersionString(void)
160 #if defined(Q_OS_WIN32)
162 ZeroMemory(&osvi
, sizeof(OSVERSIONINFO
));
163 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
166 result
= QString("Windows version %1.%2, ").arg(osvi
.dwMajorVersion
).arg(osvi
.dwMinorVersion
);
167 if(osvi
.szCSDVersion
)
168 result
+= QString("build %1 (%2)").arg(osvi
.dwBuildNumber
)
169 .arg(QString::fromWCharArray(osvi
.szCSDVersion
));
171 result
+= QString("build %1").arg(osvi
.dwBuildNumber
);
173 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
178 result
= QString("CPU: %1<br/>System: %2<br/>Release: %3<br/>Version: %4")
179 .arg(u
.machine
).arg(u
.sysname
).arg(u
.release
).arg(u
.version
);
181 result
+= QString("<br/>Qt version %1").arg(qVersion());
185 QList
<uint32_t> System::listUsbIds(void)
187 return listUsbDevices().keys();
190 /** @brief detect devices based on usb pid / vid.
191 * @return list with usb VID / PID values.
193 QMap
<uint32_t, QString
> System::listUsbDevices(void)
195 QMap
<uint32_t, QString
> usbids
;
197 qDebug() << "[System] Searching for USB devices";
198 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
200 libusb_device
**devs
;
203 res
= libusb_init(NULL
);
205 count
= libusb_get_device_list(NULL
, &devs
);
208 while((dev
= devs
[i
++]) != NULL
) {
210 unsigned char buf
[256];
212 struct libusb_device_descriptor descriptor
;
213 if(libusb_get_device_descriptor(dev
, &descriptor
) == 0) {
214 id
= descriptor
.idVendor
<< 16 | descriptor
.idProduct
;
216 libusb_device_handle
*dh
;
217 if(libusb_open(dev
, &dh
) == 0) {
218 libusb_get_string_descriptor_ascii(dh
, descriptor
.iManufacturer
, buf
, 256);
219 name
+= QString::fromAscii((char*)buf
) + " ";
220 libusb_get_string_descriptor_ascii(dh
, descriptor
.iProduct
, buf
, 256);
221 name
+= QString::fromAscii((char*)buf
);
225 name
= QObject::tr("(no description available)");
227 usbids
.insert(id
, name
);
228 qDebug("[System] USB: 0x%08x, %s", id
, name
.toLocal8Bit().data());
233 libusb_free_device_list(devs
, 1);
244 struct usb_device
*u
;
248 id
= u
->descriptor
.idVendor
<< 16 | u
->descriptor
.idProduct
;
249 // get identification strings
256 if(u
->descriptor
.iManufacturer
) {
257 res
= usb_get_string_simple(dev
, u
->descriptor
.iManufacturer
,
258 string
, sizeof(string
));
260 name
+= QString::fromAscii(string
) + " ";
262 if(u
->descriptor
.iProduct
) {
263 res
= usb_get_string_simple(dev
, u
->descriptor
.iProduct
,
264 string
, sizeof(string
));
266 name
+= QString::fromAscii(string
);
270 if(name
.isEmpty()) name
= QObject::tr("(no description available)");
273 usbids
.insert(id
, name
);
274 qDebug() << "[System] USB:" << QString("0x%1").arg(id
, 8, 16) << name
;
284 #if defined(Q_OS_WIN32)
286 SP_DEVINFO_DATA infoData
;
289 // Iterate over all devices
290 // by doing it this way it's unneccessary to use GUIDs which might be not
291 // present in current MinGW. It also seemed to be more reliably than using
293 // See KB259695 for an example.
294 deviceInfo
= SetupDiGetClassDevs(NULL
, NULL
, NULL
, DIGCF_ALLCLASSES
| DIGCF_PRESENT
);
296 infoData
.cbSize
= sizeof(SP_DEVINFO_DATA
);
298 for(i
= 0; SetupDiEnumDeviceInfo(deviceInfo
, i
, &infoData
); i
++) {
300 LPTSTR buffer
= NULL
;
301 DWORD buffersize
= 0;
304 // get device desriptor first
305 // for some reason not doing so results in bad things (tm)
306 while(!SetupDiGetDeviceRegistryProperty(deviceInfo
, &infoData
,
307 SPDRP_DEVICEDESC
,&data
, (PBYTE
)buffer
, buffersize
, &buffersize
)) {
308 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER
) {
309 if(buffer
) free(buffer
);
310 // double buffer size to avoid problems as per KB888609
311 buffer
= (LPTSTR
)malloc(buffersize
* 2);
318 // now get the hardware id, which contains PID and VID.
319 while(!SetupDiGetDeviceRegistryProperty(deviceInfo
, &infoData
,
320 SPDRP_LOCATION_INFORMATION
,&data
, (PBYTE
)buffer
, buffersize
, &buffersize
)) {
321 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER
) {
322 if(buffer
) free(buffer
);
323 // double buffer size to avoid problems as per KB888609
324 buffer
= (LPTSTR
)malloc(buffersize
* 2);
330 description
= QString::fromWCharArray(buffer
);
332 while(!SetupDiGetDeviceRegistryProperty(deviceInfo
, &infoData
,
333 SPDRP_HARDWAREID
,&data
, (PBYTE
)buffer
, buffersize
, &buffersize
)) {
334 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER
) {
335 if(buffer
) free(buffer
);
336 // double buffer size to avoid problems as per KB888609
337 buffer
= (LPTSTR
)malloc(buffersize
* 2);
344 unsigned int vid
, pid
, rev
;
345 if(_stscanf(buffer
, _TEXT("USB\\Vid_%x&Pid_%x&Rev_%x"), &vid
, &pid
, &rev
) == 3) {
347 id
= vid
<< 16 | pid
;
348 usbids
.insert(id
, description
);
349 qDebug("[System] USB VID: %04x, PID: %04x", vid
, pid
);
351 if(buffer
) free(buffer
);
353 SetupDiDestroyDeviceInfoList(deviceInfo
);
360 /** @brief detects current system proxy
361 * @return QUrl with proxy or empty
363 QUrl
System::systemProxy(void)
365 #if defined(Q_OS_LINUX)
366 return QUrl(getenv("http_proxy"));
367 #elif defined(Q_OS_WIN32)
369 wchar_t proxyval
[80];
373 DWORD enalen
= sizeof(DWORD
);
375 ret
= RegOpenKeyEx(HKEY_CURRENT_USER
,
376 _TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"),
377 0, KEY_QUERY_VALUE
, &hk
);
378 if(ret
!= ERROR_SUCCESS
) return QUrl("");
380 ret
= RegQueryValueEx(hk
, _TEXT("ProxyServer"), NULL
, NULL
, (LPBYTE
)proxyval
, &buflen
);
381 if(ret
!= ERROR_SUCCESS
) return QUrl("");
383 ret
= RegQueryValueEx(hk
, _TEXT("ProxyEnable"), NULL
, NULL
, (LPBYTE
)&enable
, &enalen
);
384 if(ret
!= ERROR_SUCCESS
) return QUrl("");
388 //qDebug() << QString::fromWCharArray(proxyval) << QString("%1").arg(enable);
390 return QUrl("http://" + QString::fromWCharArray(proxyval
));