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 ****************************************************************************/
27 #if defined(Q_OS_WIN32)
35 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
37 #include <sys/utsname.h>
41 #if defined(Q_OS_LINUX)
44 #if defined(Q_OS_MACX)
45 #include <sys/param.h>
46 #include <sys/ucred.h>
47 #include <sys/mount.h>
49 #if defined(Q_OS_WIN32)
58 // recursive function to delete a dir with files
59 bool recRmdir( const QString
&dirName
)
61 QString dirN
= dirName
;
63 // make list of entries in directory
64 QStringList list
= dir
.entryList(QDir::AllEntries
| QDir::NoDotAndDotDot
);
66 QString curItem
, lstAt
;
67 for(int i
= 0; i
< list
.size(); i
++){ // loop through all items of list
68 QString name
= list
.at(i
);
69 curItem
= dirN
+ "/" + name
;
70 fileInfo
.setFile(curItem
);
71 if(fileInfo
.isDir()) // is directory
72 recRmdir(curItem
); // call recRmdir() recursively for deleting subdirectory
74 QFile::remove(curItem
); // ok, delete file
77 return dir
.rmdir(dirN
); // delete empty dir and return if (now empty) dir-removing was successfull
81 //! @brief resolves the given path, ignoring case.
82 //! @param path absolute path to resolve.
83 //! @return returns exact casing of path, empty string if path not found.
84 QString
resolvePathCase(QString path
)
89 elems
= path
.split("/", QString::SkipEmptyParts
);
91 #if defined(Q_OS_WIN32)
92 // on windows we must make sure to start with the first entry (i.e. the
93 // drive letter) instead of a single / to make resolving work.
95 realpath
= elems
.at(0) + "/";
101 for(int i
= start
; i
< elems
.size(); i
++) {
103 = QDir(realpath
).entryList(QDir::AllEntries
|QDir::Hidden
|QDir::System
);
104 if(direlems
.contains(elems
.at(i
), Qt::CaseInsensitive
)) {
105 // need to filter using QRegExp as QStringList::filter(QString)
106 // matches any substring
107 QString expr
= QString("^" + elems
.at(i
) + "$");
108 QRegExp rx
= QRegExp(expr
, Qt::CaseInsensitive
);
109 QStringList a
= direlems
.filter(rx
);
113 if(!realpath
.endsWith("/"))
120 qDebug() << __func__
<< path
<< "->" << realpath
;
125 //! @brief get system proxy value.
126 QUrl
systemProxy(void)
128 #if defined(Q_OS_LINUX)
129 return QUrl(getenv("http_proxy"));
130 #elif defined(Q_OS_WIN32)
132 wchar_t proxyval
[80];
136 DWORD enalen
= sizeof(DWORD
);
138 ret
= RegOpenKeyEx(HKEY_CURRENT_USER
,
139 _TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"),
140 0, KEY_QUERY_VALUE
, &hk
);
141 if(ret
!= ERROR_SUCCESS
) return QUrl("");
143 ret
= RegQueryValueEx(hk
, _TEXT("ProxyServer"), NULL
, NULL
, (LPBYTE
)proxyval
, &buflen
);
144 if(ret
!= ERROR_SUCCESS
) return QUrl("");
146 ret
= RegQueryValueEx(hk
, _TEXT("ProxyEnable"), NULL
, NULL
, (LPBYTE
)&enable
, &enalen
);
147 if(ret
!= ERROR_SUCCESS
) return QUrl("");
151 //qDebug() << QString::fromWCharArray(proxyval) << QString("%1").arg(enable);
153 return QUrl("http://" + QString::fromWCharArray(proxyval
));
161 QString
installedVersion(QString mountpoint
)
163 // read rockbox-info.txt
164 QFile
info(mountpoint
+"/.rockbox/rockbox-info.txt");
165 if(!info
.open(QIODevice::ReadOnly
))
170 QString target
, features
,version
;
171 while (!info
.atEnd()) {
172 QString line
= info
.readLine();
174 if(line
.contains("Version:"))
176 return line
.remove("Version:").trimmed();
183 int installedTargetId(QString mountpoint
)
185 // read rockbox-info.txt
186 QFile
info(mountpoint
+"/.rockbox/rockbox-info.txt");
187 if(!info
.open(QIODevice::ReadOnly
))
192 QString target
, features
,version
;
193 while (!info
.atEnd()) {
194 QString line
= info
.readLine();
196 if(line
.contains("Target id:"))
198 return line
.remove("Target id:").trimmed().toInt();
206 QString
getUserName(void)
208 #if defined(Q_OS_WIN32)
209 wchar_t userbuf
[UNLEN
];
210 DWORD usersize
= UNLEN
;
213 status
= GetUserNameW(userbuf
, &usersize
);
215 return QString::fromWCharArray(userbuf
);
217 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
219 user
= getpwuid(geteuid());
220 return QString(user
->pw_name
);
225 #if defined(Q_OS_WIN32)
226 enum userlevel
getUserPermissions(void)
229 NET_API_STATUS napistatus
;
230 wchar_t userbuf
[UNLEN
];
231 DWORD usersize
= UNLEN
;
233 enum userlevel result
;
235 status
= GetUserNameW(userbuf
, &usersize
);
239 napistatus
= NetUserGetInfo(NULL
, userbuf
, (DWORD
)1, (LPBYTE
*)&buf
);
241 switch(buf
->usri1_priv
) {
242 case USER_PRIV_GUEST
:
248 case USER_PRIV_ADMIN
:
255 NetApiBufferFree(buf
);
260 QString
getUserPermissionsString(void)
263 int perm
= getUserPermissions();
266 result
= QObject::tr("Guest");
269 result
= QObject::tr("Admin");
272 result
= QObject::tr("User");
275 result
= QObject::tr("Error");
282 QString
getOsVersionString(void)
285 #if defined(Q_OS_WIN32)
287 ZeroMemory(&osvi
, sizeof(OSVERSIONINFO
));
288 osvi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFO
);
291 result
= QString("Windows version %1.%2, ").arg(osvi
.dwMajorVersion
).arg(osvi
.dwMinorVersion
);
292 result
+= QString("build %1 (%2)").arg(osvi
.dwBuildNumber
).arg(QString::fromWCharArray(osvi
.szCSDVersion
));
294 #if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
299 result
= QString("CPU: %1<br/>System: %2<br/>Release: %3<br/>Version: %4")
300 .arg(u
.machine
).arg(u
.sysname
).arg(u
.release
).arg(u
.version
);
306 /** @brief detect devices based on usb pid / vid.
307 * @return list with usb VID / PID values.
309 QList
<uint32_t> listUsbIds(void)
311 QList
<uint32_t> usbids
;
313 #if defined(Q_OS_LINUX) | defined(Q_OS_MACX)
321 qDebug() << "bus:" << b
->dirname
<< b
->devices
;
323 qDebug() << "devices present.";
324 struct usb_device
*u
;
328 id
= u
->descriptor
.idVendor
<< 16 | u
->descriptor
.idProduct
;
329 if(id
) usbids
.append(id
);
337 #if defined(Q_OS_WIN32)
339 SP_DEVINFO_DATA infoData
;
342 // Iterate over all devices
343 // by doing it this way it's unneccessary to use GUIDs which might be not
344 // present in current MinGW. It also seemed to be more reliably than using
346 // See KB259695 for an example.
347 deviceInfo
= SetupDiGetClassDevs(NULL
, NULL
, NULL
, DIGCF_ALLCLASSES
| DIGCF_PRESENT
);
349 infoData
.cbSize
= sizeof(SP_DEVINFO_DATA
);
351 for(i
= 0; SetupDiEnumDeviceInfo(deviceInfo
, i
, &infoData
); i
++) {
353 LPTSTR buffer
= NULL
;
354 DWORD buffersize
= 0;
356 // get device desriptor first
357 // for some reason not doing so results in bad things (tm)
358 while(!SetupDiGetDeviceRegistryProperty(deviceInfo
, &infoData
,
359 SPDRP_DEVICEDESC
,&data
, (PBYTE
)buffer
, buffersize
, &buffersize
)) {
360 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER
) {
361 if(buffer
) free(buffer
);
362 // double buffer size to avoid problems as per KB888609
363 buffer
= (LPTSTR
)malloc(buffersize
* 2);
370 // now get the hardware id, which contains PID and VID.
371 while(!SetupDiGetDeviceRegistryProperty(deviceInfo
, &infoData
,
372 SPDRP_HARDWAREID
,&data
, (PBYTE
)buffer
, buffersize
, &buffersize
)) {
373 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER
) {
374 if(buffer
) free(buffer
);
375 // double buffer size to avoid problems as per KB888609
376 buffer
= (LPTSTR
)malloc(buffersize
* 2);
383 unsigned int vid
, pid
, rev
;
384 if(_stscanf(buffer
, _TEXT("USB\\Vid_%x&Pid_%x&Rev_%x"), &vid
, &pid
, &rev
) != 3) {
385 qDebug() << "Error getting USB ID -- possibly no USB device";
389 id
= vid
<< 16 | pid
;
391 qDebug("VID: %04x PID: %04x", vid
, pid
);
393 if(buffer
) free(buffer
);
395 SetupDiDestroyDeviceInfoList(deviceInfo
);