Rearange menu of mpegplayer. Add new menu with "settings" and "quit", and remove...
[kugel-rb.git] / rbutil / rbutilqt / base / system.cpp
blob054f1c59efc462f4adb7b1ee59697b3485367fc2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Wenger
10 * $Id$
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 ****************************************************************************/
21 #include "system.h"
23 #include <QtCore>
24 #include <QDebug>
26 #include <cstdlib>
27 #include <stdio.h>
29 // Windows Includes
30 #if defined(Q_OS_WIN32)
31 #if defined(UNICODE)
32 #define _UNICODE
33 #endif
34 #include <windows.h>
35 #include <tchar.h>
36 #include <lm.h>
37 #include <windows.h>
38 #include <setupapi.h>
39 #endif
41 // Linux and Mac includes
42 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
43 #if defined(LIBUSB1)
44 #include <libusb-1.0/libusb.h>
45 #else
46 #include <usb.h>
47 #endif
48 #include <sys/utsname.h>
49 #include <unistd.h>
50 #include <pwd.h>
51 #endif
53 // Linux includes
54 #if defined(Q_OS_LINUX)
55 #include <mntent.h>
56 #endif
58 // Mac includes
59 #if defined(Q_OS_MACX)
60 #include <sys/param.h>
61 #include <sys/ucred.h>
62 #include <sys/mount.h>
63 #endif
65 #include "utils.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)
74 LPUSER_INFO_1 buf;
75 NET_API_STATUS napistatus;
76 wchar_t userbuf[UNLEN];
77 DWORD usersize = UNLEN;
78 BOOL status;
79 enum userlevel result;
81 status = GetUserNameW(userbuf, &usersize);
82 if(!status)
83 return ERR;
85 napistatus = NetUserGetInfo(NULL, userbuf, (DWORD)1, (LPBYTE*)&buf);
87 switch(buf->usri1_priv) {
88 case USER_PRIV_GUEST:
89 result = GUEST;
90 break;
91 case USER_PRIV_USER:
92 result = USER;
93 break;
94 case USER_PRIV_ADMIN:
95 result = ADMIN;
96 break;
97 default:
98 result = ERR;
99 break;
101 NetApiBufferFree(buf);
103 return result;
106 /** @brief detects user permissions (only Windows at moment).
107 * @return a user readable string with the permission.
109 QString System::userPermissionsString(void)
111 QString result;
112 int perm = userPermissions();
113 switch(perm) {
114 case GUEST:
115 result = QObject::tr("Guest");
116 break;
117 case ADMIN:
118 result = QObject::tr("Admin");
119 break;
120 case USER:
121 result = QObject::tr("User");
122 break;
123 default:
124 result = QObject::tr("Error");
125 break;
127 return result;
129 #endif
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;
140 BOOL status;
142 status = GetUserNameW(userbuf, &usersize);
144 return QString::fromWCharArray(userbuf);
145 #endif
146 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
147 struct passwd *user;
148 user = getpwuid(geteuid());
149 return QString(user->pw_name);
150 #endif
154 /** @brief detects the OS Version
155 * @return String with OS Version.
157 QString System::osVersionString(void)
159 QString result;
160 #if defined(Q_OS_WIN32)
161 OSVERSIONINFO osvi;
162 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
163 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
164 GetVersionEx(&osvi);
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));
170 else
171 result += QString("build %1").arg(osvi.dwBuildNumber);
172 #endif
173 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
174 struct utsname u;
175 int ret;
176 ret = uname(&u);
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);
180 #endif
181 result += QString("<br/>Qt version %1").arg(qVersion());
182 return result;
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;
196 // usb pid detection
197 qDebug() << "[System] Searching for USB devices";
198 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
199 #if defined(LIBUSB1)
200 libusb_device **devs;
201 int res;
202 ssize_t count;
203 res = libusb_init(NULL);
205 count = libusb_get_device_list(NULL, &devs);
206 libusb_device *dev;
207 int i = 0;
208 while((dev = devs[i++]) != NULL) {
209 QString name;
210 unsigned char buf[256];
211 uint32_t id;
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);
222 libusb_close(dh);
224 if(name.isEmpty())
225 name = QObject::tr("(no description available)");
226 if(id) {
227 usbids.insert(id, name);
228 qDebug("[System] USB: 0x%08x, %s", id, name.toLocal8Bit().data());
233 libusb_free_device_list(devs, 1);
234 libusb_exit(NULL);
235 #else
236 usb_init();
237 usb_find_busses();
238 usb_find_devices();
239 struct usb_bus *b;
240 b = usb_busses;
242 while(b) {
243 if(b->devices) {
244 struct usb_device *u;
245 u = b->devices;
246 while(u) {
247 uint32_t id;
248 id = u->descriptor.idVendor << 16 | u->descriptor.idProduct;
249 // get identification strings
250 usb_dev_handle *dev;
251 QString name;
252 char string[256];
253 int res;
254 dev = usb_open(u);
255 if(dev) {
256 if(u->descriptor.iManufacturer) {
257 res = usb_get_string_simple(dev, u->descriptor.iManufacturer,
258 string, sizeof(string));
259 if(res > 0)
260 name += QString::fromAscii(string) + " ";
262 if(u->descriptor.iProduct) {
263 res = usb_get_string_simple(dev, u->descriptor.iProduct,
264 string, sizeof(string));
265 if(res > 0)
266 name += QString::fromAscii(string);
269 usb_close(dev);
270 if(name.isEmpty()) name = QObject::tr("(no description available)");
272 if(id) {
273 usbids.insert(id, name);
274 qDebug() << "[System] USB:" << QString("0x%1").arg(id, 8, 16) << name;
276 u = u->next;
279 b = b->next;
281 #endif
282 #endif
284 #if defined(Q_OS_WIN32)
285 HDEVINFO deviceInfo;
286 SP_DEVINFO_DATA infoData;
287 DWORD i;
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
292 // a GUID.
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++) {
299 DWORD data;
300 LPTSTR buffer = NULL;
301 DWORD buffersize = 0;
302 QString description;
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);
313 else {
314 break;
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);
326 else {
327 break;
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);
339 else {
340 break;
344 unsigned int vid, pid, rev;
345 if(_stscanf(buffer, _TEXT("USB\\Vid_%x&Pid_%x&Rev_%x"), &vid, &pid, &rev) == 3) {
346 uint32_t id;
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);
355 #endif
356 return usbids;
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)
368 HKEY hk;
369 wchar_t proxyval[80];
370 DWORD buflen = 80;
371 long ret;
372 DWORD enable;
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("");
386 RegCloseKey(hk);
388 //qDebug() << QString::fromWCharArray(proxyval) << QString("%1").arg(enable);
389 if(enable != 0)
390 return QUrl("http://" + QString::fromWCharArray(proxyval));
391 else
392 return QUrl("");
393 #else
394 return QUrl("");
395 #endif