From 7fb11a82a1f899655e7d5a4a6ff5c39d9dc27b8f Mon Sep 17 00:00:00 2001 From: Dominik Wenger Date: Fri, 27 Jun 2008 21:53:22 +0000 Subject: rbutil: move some detecting code, to make detecing things nicer git-svn-id: svn://svn.rockbox.org/rockbox/trunk@17813 a1c6a512-1295-4272-9138-f99709370657 --- rbutil/rbutilqt/autodetection.cpp | 4 +- rbutil/rbutilqt/configure.cpp | 4 +- rbutil/rbutilqt/install.cpp | 26 +--- rbutil/rbutilqt/rbutilqt.cpp | 28 ++-- rbutil/rbutilqt/rbutilqt.pro | 6 +- rbutil/rbutilqt/sysinfo.cpp | 11 +- rbutil/rbutilqt/utils.cpp | 306 -------------------------------------- rbutil/rbutilqt/utils.h | 13 -- 8 files changed, 31 insertions(+), 367 deletions(-) diff --git a/rbutil/rbutilqt/autodetection.cpp b/rbutil/rbutilqt/autodetection.cpp index f257f5c0c9..c356cc6307 100644 --- a/rbutil/rbutilqt/autodetection.cpp +++ b/rbutil/rbutilqt/autodetection.cpp @@ -40,7 +40,7 @@ #include #include #endif -#include "utils.h" +#include "detect.h" Autodetection::Autodetection(QObject* parent): QObject(parent) { @@ -267,7 +267,7 @@ bool Autodetection::detectUsb() // usb pid detection QList attached; - attached = listUsbIds(); + attached = Detect::listUsbIds(); int i = attached.size(); while(i--) { diff --git a/rbutil/rbutilqt/configure.cpp b/rbutil/rbutilqt/configure.cpp index 4b2a13ace0..c149741d86 100644 --- a/rbutil/rbutilqt/configure.cpp +++ b/rbutil/rbutilqt/configure.cpp @@ -25,7 +25,7 @@ #include "browsedirtree.h" #include "encoders.h" #include "tts.h" -#include "utils.h" +#include "detect.h" #include #if defined(Q_OS_WIN32) @@ -361,7 +361,7 @@ void Config::setSystemProxy(bool checked) proxy.setHost(ui.proxyHost->text()); proxy.setPort(ui.proxyPort->text().toInt()); // show system values in input box - QUrl envproxy = systemProxy(); + QUrl envproxy = Detect::systemProxy(); ui.proxyHost->setText(envproxy.host()); diff --git a/rbutil/rbutilqt/install.cpp b/rbutil/rbutilqt/install.cpp index 8ee1dc1ff5..d45b374f70 100644 --- a/rbutil/rbutilqt/install.cpp +++ b/rbutil/rbutilqt/install.cpp @@ -20,7 +20,7 @@ #include "install.h" #include "ui_installfrm.h" #include "rbzip.h" -#include "utils.h" +#include "detect.h" Install::Install(RbSettings *sett,QWidget *parent) : QDialog(parent) { @@ -35,7 +35,7 @@ Install::Install(RbSettings *sett,QWidget *parent) : QDialog(parent) connect(ui.backup,SIGNAL(stateChanged(int)),this,SLOT(backupCheckboxChanged(int))); //! check if rockbox is already installed - QString version = installedVersion(settings->mountpoint()); + QString version = Detect::installedVersion(settings->mountpoint()); if(version != "") { @@ -120,23 +120,13 @@ void Install::accept() return; } settings->sync(); - - int rbTarget = installedTargetId(settings->mountpoint()); - if(rbTarget != -1 && rbTarget != settings->curTargetId()) + + if(Detect::check(settings,false,settings->curTargetId()) == false) { - if(QMessageBox::question(this, tr("Device mismatch detected"), - tr("Device mismatch detected.\n\n" - "Installed Rockbox is for Device: %1.\n" - "New Rockbox is for Device: %2.\n\n" - "Do you want to continue?").arg(settings->nameOfTargetId(rbTarget),settings->curName()), - QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) - { - logger->addItem(tr("Aborted!"),LOGERROR); - logger->abort(); - return; - } - } - + logger->addItem(tr("Aborted!"),LOGERROR); + logger->abort(); + return; + } //! check if we should backup if(ui.backup->isChecked()) diff --git a/rbutil/rbutilqt/rbutilqt.cpp b/rbutil/rbutilqt/rbutilqt.cpp index 461d45f076..b5e485c57c 100644 --- a/rbutil/rbutilqt/rbutilqt.cpp +++ b/rbutil/rbutilqt/rbutilqt.cpp @@ -35,7 +35,7 @@ #include "utils.h" #include "rbzip.h" #include "sysinfo.h" - +#include "detect.h" #if defined(Q_OS_LINUX) #include @@ -266,7 +266,7 @@ void RbUtilQt::updateSettings() updateDevice(); updateManual(); if(settings->proxyType() == "system") { - HttpGet::setGlobalProxy(systemProxy()); + HttpGet::setGlobalProxy(Detect::systemProxy()); } else if(settings->proxyType() == "manual") { HttpGet::setGlobalProxy(settings->proxy()); @@ -475,23 +475,15 @@ bool RbUtilQt::installAuto() QString myversion = "r" + versmap.value("bleed_rev"); // check installed Version and Target - QString rbVersion = installedVersion(settings->mountpoint()); - int rbTarget = installedTargetId(settings->mountpoint()); - if(rbTarget != -1 && rbTarget != settings->curTargetId()) + QString rbVersion = Detect::installedVersion(settings->mountpoint()); + + if(Detect::check(settings,false,settings->curTargetId()) == false) { - if(QMessageBox::question(this, tr("Target mismatch detected"), - tr("Target mismatch detected. \n\n" - "Installed target: %1.\n" - "New Target: %2.\n\n" - "Do you want to continue?").arg(settings->nameOfTargetId(rbTarget),settings->curName()), - QMessageBox::Yes | QMessageBox::No) == QMessageBox::No) - { - logger->addItem(tr("Aborted!"),LOGERROR); - logger->abort(); - return false; - } + logger->addItem(tr("Aborted!"),LOGERROR); + logger->abort(); + return false; } - + // check version if(rbVersion != "") { @@ -1001,7 +993,7 @@ QUrl RbUtilQt::proxy() return QUrl(settings->proxy()); else if(settings->proxy() == "system") { - return systemProxy(); + return Detect::systemProxy(); } return QUrl(""); } diff --git a/rbutil/rbutilqt/rbutilqt.pro b/rbutil/rbutilqt/rbutilqt.pro index 9be9197f22..aebdca268c 100644 --- a/rbutil/rbutilqt/rbutilqt.pro +++ b/rbutil/rbutilqt/rbutilqt.pro @@ -65,7 +65,8 @@ SOURCES += rbutilqt.cpp \ rbsettings.cpp \ rbunzip.cpp \ rbzip.cpp \ - sysinfo.cpp + sysinfo.cpp \ + detect.cpp HEADERS += rbutilqt.h \ install.h \ @@ -113,7 +114,8 @@ HEADERS += rbutilqt.h \ rbsettings.h \ rbunzip.h \ rbzip.h \ - sysinfo.h + sysinfo.h \ + detect.h # Needed by QT on Win INCLUDEPATH = . irivertools zip zlib ../ipodpatcher ../sansapatcher ../../tools/rbspeex ../../tools diff --git a/rbutil/rbutilqt/sysinfo.cpp b/rbutil/rbutilqt/sysinfo.cpp index 413494c679..4cd2666e8b 100644 --- a/rbutil/rbutilqt/sysinfo.cpp +++ b/rbutil/rbutilqt/sysinfo.cpp @@ -20,7 +20,7 @@ #include #include "sysinfo.h" #include "ui_sysinfofrm.h" -#include "utils.h" +#include "detect.h" Sysinfo::Sysinfo(QWidget *parent) : QDialog(parent) @@ -33,17 +33,16 @@ Sysinfo::Sysinfo(QWidget *parent) : QDialog(parent) connect(ui.buttonRefresh, SIGNAL(clicked()), this, SLOT(updateSysinfo())); } - void Sysinfo::updateSysinfo(void) { QString info; - info += tr("OS
") + getOsVersionString() + "
"; - info += tr("Username:
%1
").arg(getUserName()); + info += tr("OS
") + Detect::osVersionString() + "
"; + info += tr("Username:
%1
").arg(Detect::userName()); #if defined(Q_OS_WIN32) - info += tr("Permissions:
%1
").arg(getUserPermissionsString()); + info += tr("Permissions:
%1
").arg(Detect::userPermissionsString()); #endif info += tr("Attached USB devices:
"); - QList usbids = listUsbIds(); + QList usbids = Detect::listUsbIds(); for(int i = 0; i < usbids.size(); i++) info += tr("VID: %1 PID: %2
") .arg((usbids.at(i)&0xffff0000)>>16, 4, 16, QChar('0')) diff --git a/rbutil/rbutilqt/utils.cpp b/rbutil/rbutilqt/utils.cpp index 9359522f04..236f28babd 100644 --- a/rbutil/rbutilqt/utils.cpp +++ b/rbutil/rbutilqt/utils.cpp @@ -24,37 +24,6 @@ #include #include -#if defined(Q_OS_WIN32) -#if defined(UNICODE) -#define _UNICODE -#endif -#include -#include -#include -#endif -#if defined(Q_OS_LINUX) || defined(Q_OS_MACX) -#include -#include -#include -#include -#endif -#if defined(Q_OS_LINUX) -#include -#endif -#if defined(Q_OS_MACX) -#include -#include -#include -#endif -#if defined(Q_OS_WIN32) -#if defined(UNICODE) -#define _UNICODE -#endif -#include -#include -#include -#endif - // recursive function to delete a dir with files bool recRmdir( const QString &dirName ) { @@ -122,278 +91,3 @@ QString resolvePathCase(QString path) } -//! @brief get system proxy value. -QUrl systemProxy(void) -{ -#if defined(Q_OS_LINUX) - return QUrl(getenv("http_proxy")); -#elif defined(Q_OS_WIN32) - HKEY hk; - wchar_t proxyval[80]; - DWORD buflen = 80; - long ret; - DWORD enable; - DWORD enalen = sizeof(DWORD); - - ret = RegOpenKeyEx(HKEY_CURRENT_USER, - _TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"), - 0, KEY_QUERY_VALUE, &hk); - if(ret != ERROR_SUCCESS) return QUrl(""); - - ret = RegQueryValueEx(hk, _TEXT("ProxyServer"), NULL, NULL, (LPBYTE)proxyval, &buflen); - if(ret != ERROR_SUCCESS) return QUrl(""); - - ret = RegQueryValueEx(hk, _TEXT("ProxyEnable"), NULL, NULL, (LPBYTE)&enable, &enalen); - if(ret != ERROR_SUCCESS) return QUrl(""); - - RegCloseKey(hk); - - //qDebug() << QString::fromWCharArray(proxyval) << QString("%1").arg(enable); - if(enable != 0) - return QUrl("http://" + QString::fromWCharArray(proxyval)); - else - return QUrl(""); -#else - return QUrl(""); -#endif -} - -QString installedVersion(QString mountpoint) -{ - // read rockbox-info.txt - QFile info(mountpoint +"/.rockbox/rockbox-info.txt"); - if(!info.open(QIODevice::ReadOnly)) - { - return ""; - } - - QString target, features,version; - while (!info.atEnd()) { - QString line = info.readLine(); - - if(line.contains("Version:")) - { - return line.remove("Version:").trimmed(); - } - } - info.close(); - return ""; -} - -int installedTargetId(QString mountpoint) -{ - // read rockbox-info.txt - QFile info(mountpoint +"/.rockbox/rockbox-info.txt"); - if(!info.open(QIODevice::ReadOnly)) - { - return -1; - } - - QString target, features,version; - while (!info.atEnd()) { - QString line = info.readLine(); - - if(line.contains("Target id:")) - { - return line.remove("Target id:").trimmed().toInt(); - } - } - info.close(); - return -1; - -} - -QString getUserName(void) -{ -#if defined(Q_OS_WIN32) - wchar_t userbuf[UNLEN]; - DWORD usersize = UNLEN; - BOOL status; - - status = GetUserNameW(userbuf, &usersize); - - return QString::fromWCharArray(userbuf); -#endif -#if defined(Q_OS_LINUX) || defined(Q_OS_MACX) - struct passwd *user; - user = getpwuid(geteuid()); - return QString(user->pw_name); -#endif -} - - -#if defined(Q_OS_WIN32) -enum userlevel getUserPermissions(void) -{ - LPUSER_INFO_1 buf; - NET_API_STATUS napistatus; - wchar_t userbuf[UNLEN]; - DWORD usersize = UNLEN; - BOOL status; - enum userlevel result; - - status = GetUserNameW(userbuf, &usersize); - if(!status) - return ERR; - - napistatus = NetUserGetInfo(NULL, userbuf, (DWORD)1, (LPBYTE*)&buf); - - switch(buf->usri1_priv) { - case USER_PRIV_GUEST: - result = GUEST; - break; - case USER_PRIV_USER: - result = USER; - break; - case USER_PRIV_ADMIN: - result = ADMIN; - break; - default: - result = ERR; - break; - } - NetApiBufferFree(buf); - - return result; -} - -QString getUserPermissionsString(void) -{ - QString result; - int perm = getUserPermissions(); - switch(perm) { - case GUEST: - result = QObject::tr("Guest"); - break; - case ADMIN: - result = QObject::tr("Admin"); - break; - case USER: - result = QObject::tr("User"); - break; - default: - result = QObject::tr("Error"); - break; - } - return result; -} -#endif - -QString getOsVersionString(void) -{ - QString result; -#if defined(Q_OS_WIN32) - OSVERSIONINFO osvi; - ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); - osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - GetVersionEx(&osvi); - - result = QString("Windows version %1.%2, ").arg(osvi.dwMajorVersion).arg(osvi.dwMinorVersion); - result += QString("build %1 (%2)").arg(osvi.dwBuildNumber).arg(QString::fromWCharArray(osvi.szCSDVersion)); -#endif -#if defined(Q_OS_LINUX) || defined(Q_OS_MACX) - struct utsname u; - int ret; - ret = uname(&u); - - result = QString("CPU: %1
System: %2
Release: %3
Version: %4") - .arg(u.machine).arg(u.sysname).arg(u.release).arg(u.version); -#endif - return result; - -} - -/** @brief detect devices based on usb pid / vid. - * @return list with usb VID / PID values. - */ -QList listUsbIds(void) -{ - QList usbids; - // usb pid detection -#if defined(Q_OS_LINUX) | defined(Q_OS_MACX) - usb_init(); - usb_find_busses(); - usb_find_devices(); - struct usb_bus *b; - b = usb_busses; - - while(b) { - qDebug() << "bus:" << b->dirname << b->devices; - if(b->devices) { - qDebug() << "devices present."; - struct usb_device *u; - u = b->devices; - while(u) { - uint32_t id; - id = u->descriptor.idVendor << 16 | u->descriptor.idProduct; - if(id) usbids.append(id); - u = u->next; - } - } - b = b->next; - } -#endif - -#if defined(Q_OS_WIN32) - HDEVINFO deviceInfo; - SP_DEVINFO_DATA infoData; - DWORD i; - - // Iterate over all devices - // by doing it this way it's unneccessary to use GUIDs which might be not - // present in current MinGW. It also seemed to be more reliably than using - // a GUID. - // See KB259695 for an example. - deviceInfo = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT); - - infoData.cbSize = sizeof(SP_DEVINFO_DATA); - - for(i = 0; SetupDiEnumDeviceInfo(deviceInfo, i, &infoData); i++) { - DWORD data; - LPTSTR buffer = NULL; - DWORD buffersize = 0; - - // get device desriptor first - // for some reason not doing so results in bad things (tm) - while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData, - SPDRP_DEVICEDESC,&data, (PBYTE)buffer, buffersize, &buffersize)) { - if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - if(buffer) free(buffer); - // double buffer size to avoid problems as per KB888609 - buffer = (LPTSTR)malloc(buffersize * 2); - } - else { - break; - } - } - - // now get the hardware id, which contains PID and VID. - while(!SetupDiGetDeviceRegistryProperty(deviceInfo, &infoData, - SPDRP_HARDWAREID,&data, (PBYTE)buffer, buffersize, &buffersize)) { - if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - if(buffer) free(buffer); - // double buffer size to avoid problems as per KB888609 - buffer = (LPTSTR)malloc(buffersize * 2); - } - else { - break; - } - } - - unsigned int vid, pid, rev; - if(_stscanf(buffer, _TEXT("USB\\Vid_%x&Pid_%x&Rev_%x"), &vid, &pid, &rev) != 3) { - qDebug() << "Error getting USB ID -- possibly no USB device"; - } - else { - uint32_t id; - id = vid << 16 | pid; - usbids.append(id); - qDebug("VID: %04x PID: %04x", vid, pid); - } - if(buffer) free(buffer); - } - SetupDiDestroyDeviceInfoList(deviceInfo); - -#endif - return usbids; -} diff --git a/rbutil/rbutilqt/utils.h b/rbutil/rbutilqt/utils.h index b4d660520e..ba8b97f6b7 100644 --- a/rbutil/rbutilqt/utils.h +++ b/rbutil/rbutilqt/utils.h @@ -24,21 +24,8 @@ #include #include -#if defined(Q_OS_WIN32) -enum userlevel { ERR, GUEST, USER, ADMIN }; -enum userlevel getUserPermissions(void); -QString getUserPermissionsString(void); -#endif -QString getUserName(void); -QString getOsVersionString(void); -QList listUsbIds(void); - bool recRmdir( const QString &dirName ); QString resolvePathCase(QString path); -QUrl systemProxy(void); -QString installedVersion(QString mountpoint); -int installedTargetId(QString mountpoint); - #endif -- cgit v1.2.3