Move utils.cpp functions into separate class and split it up.

Move class-less functions in utils.cpp into a new Utils class and make the old
functions static. This prevents clashes with system C functions. Rename some
functions to avoid macro problems (check() is a macro on OS X). Split out the
RockboxInfo class into a separate file.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@25441 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Dominik Riebeling 2010-04-02 21:24:19 +00:00
parent c5d9516a68
commit 9fedc8187f
20 changed files with 170 additions and 105 deletions

View file

@ -54,6 +54,7 @@
#include "system.h"
#include "utils.h"
#include "rockboxinfo.h"
Autodetection::Autodetection(QObject* parent): QObject(parent)
{

View file

@ -114,7 +114,7 @@ bool BootloaderInstallBase::backup(QString to)
}
QString tofile = to + "/" + QFileInfo(m_blfile).fileName();
qDebug() << "[BootloaderInstallBase] trying to backup" << m_blfile << "to" << tofile;
if(!QFile::copy(resolvePathCase(m_blfile), tofile)) {
if(!QFile::copy(Utils::resolvePathCase(m_blfile), tofile)) {
emit logItem(tr("Creating backup copy failed."), LOGERROR);
return false;
}
@ -247,7 +247,7 @@ void BootloaderInstallBase::setBlFile(QStringList sl)
{
// figue which of the possible bootloader filenames is correct.
for(int a = 0; a < sl.size(); a++) {
if(!resolvePathCase(sl.at(a)).isEmpty()) {
if(!Utils::resolvePathCase(sl.at(a)).isEmpty()) {
m_blfile = sl.at(a);
}
}

View file

@ -45,16 +45,16 @@ void BootloaderInstallFile::installStage2(void)
QCoreApplication::processEvents();
// if an old bootloader is present (Gigabeat) move it out of the way.
QString fwfile(resolvePathCase(m_blfile));
QString fwfile(Utils::resolvePathCase(m_blfile));
if(!fwfile.isEmpty()) {
QString moved = resolvePathCase(m_blfile) + ".ORIG";
QString moved = Utils::resolvePathCase(m_blfile) + ".ORIG";
qDebug() << "[BootloaderInstallFile] renaming" << fwfile << "to" << moved;
QFile::rename(fwfile, moved);
}
// if no old file found resolve path without basename
QFileInfo fi(m_blfile);
QString absPath = resolvePathCase(fi.absolutePath());
QString absPath = Utils::resolvePathCase(fi.absolutePath());
// if it's not possible to locate the base path try to create it
if(absPath.isEmpty()) {
@ -67,10 +67,10 @@ void BootloaderInstallFile::installStage2(void)
QString basePath = pathElements.join("/");
// check for base and bail out if not found. Otherwise create folder.
absPath = resolvePathCase(basePath);
absPath = Utils::resolvePathCase(basePath);
QDir d(absPath);
d.mkpath(lastElement);
absPath = resolvePathCase(fi.absolutePath());
absPath = Utils::resolvePathCase(fi.absolutePath());
if(absPath.isEmpty()) {
emit logItem(tr("Error accessing output folder"), LOGERROR);
@ -98,13 +98,13 @@ bool BootloaderInstallFile::uninstall(void)
qDebug() << "[BootloaderInstallFile] Uninstalling bootloader";
emit logItem(tr("Removing Rockbox bootloader"), LOGINFO);
// check if a .ORIG file is present, and allow moving it back.
QString origbl = resolvePathCase(m_blfile + ".ORIG");
QString origbl = Utils::resolvePathCase(m_blfile + ".ORIG");
if(origbl.isEmpty()) {
emit logItem(tr("No original firmware file found."), LOGERROR);
emit done(true);
return false;
}
QString fwfile = resolvePathCase(m_blfile);
QString fwfile = Utils::resolvePathCase(m_blfile);
if(!QFile::remove(fwfile)) {
emit logItem(tr("Can't remove Rockbox bootloader file."), LOGERROR);
emit done(true);
@ -128,10 +128,10 @@ bool BootloaderInstallFile::uninstall(void)
BootloaderInstallBase::BootloaderType BootloaderInstallFile::installed(void)
{
qDebug() << "[BootloaderInstallFile] checking installed bootloader";
if(!resolvePathCase(m_blfile).isEmpty()
&& !resolvePathCase(m_blfile + ".ORIG").isEmpty())
if(!Utils::resolvePathCase(m_blfile).isEmpty()
&& !Utils::resolvePathCase(m_blfile + ".ORIG").isEmpty())
return BootloaderRockbox;
else if(!resolvePathCase(m_blfile).isEmpty())
else if(!Utils::resolvePathCase(m_blfile).isEmpty())
return BootloaderOther;
else
return BootloaderUnknown;

View file

@ -44,9 +44,9 @@ void BootloaderInstallMi4::installStage2(void)
QCoreApplication::processEvents();
// move old bootloader out of the way
QString fwfile(resolvePathCase(m_blfile));
QString fwfile(Utils::resolvePathCase(m_blfile));
QFile oldbl(fwfile);
QString moved = QFileInfo(resolvePathCase(m_blfile)).absolutePath()
QString moved = QFileInfo(Utils::resolvePathCase(m_blfile)).absolutePath()
+ "/OF.mi4";
if(!QFileInfo(moved).exists()) {
qDebug() << "[BootloaderInstallMi4] renaming" << fwfile << "to" << moved;
@ -83,20 +83,20 @@ bool BootloaderInstallMi4::uninstall(void)
// check if OF file present
emit logItem(tr("Checking for original firmware file"), LOGINFO);
QString original = QFileInfo(resolvePathCase(m_blfile)).absolutePath()
QString original = QFileInfo(Utils::resolvePathCase(m_blfile)).absolutePath()
+ "/OF.mi4";
if(resolvePathCase(original).isEmpty()) {
if(Utils::resolvePathCase(original).isEmpty()) {
emit logItem(tr("Error finding original firmware file"), LOGERROR);
return false;
}
// finally remove RB bootloader
QString resolved = resolvePathCase(m_blfile);
QString resolved = Utils::resolvePathCase(m_blfile);
QFile blfile(resolved);
blfile.remove();
QFile::rename(resolvePathCase(original), m_blfile);
QFile::rename(Utils::resolvePathCase(original), m_blfile);
emit logItem(tr("Rockbox bootloader successful removed"), LOGINFO);
logInstall(LogRemove);
emit done(false);
@ -114,7 +114,7 @@ BootloaderInstallBase::BootloaderType BootloaderInstallMi4::installed(void)
// make sure to resolve case to prevent case issues
QString resolved;
resolved = resolvePathCase(m_blfile);
resolved = Utils::resolvePathCase(m_blfile);
if(resolved.isEmpty()) {
qDebug() << "[BootloaderInstallMi4] installed: BootloaderNone";
return BootloaderNone;

View file

@ -89,7 +89,7 @@ EncExes::EncExes(QString name,QObject *parent) : EncBase(parent)
void EncExes::generateSettings()
{
QString exepath =RbSettings::subValue(m_name,RbSettings::EncoderPath).toString();
if(exepath == "") exepath = findExecutable(m_name);
if(exepath == "") exepath = Utils::findExecutable(m_name);
insertSetting(eEXEPATH,new EncTtsSetting(this,EncTtsSetting::eSTRING,
tr("Path to Encoder:"),exepath,EncTtsSetting::eBROWSEBTN));

View file

@ -0,0 +1,67 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2007 by Dominik Wenger
* $Id$
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "rockboxinfo.h"
#include <QtCore>
#include <QDebug>
RockboxInfo::RockboxInfo(QString mountpoint)
{
qDebug() << "[RockboxInfo] trying to find rockbox-info at" << mountpoint;
QFile file(mountpoint + "/.rockbox/rockbox-info.txt");
m_success = false;
if(!file.exists())
return;
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
// read file contents
while (!file.atEnd())
{
QString line = file.readLine();
if(line.contains("Version:"))
{
m_version = line.remove("Version:").trimmed();
}
else if(line.contains("Target: "))
{
m_target = line.remove("Target: ").trimmed();
}
else if(line.contains("Features:"))
{
m_features = line.remove("Features:").trimmed();
}
else if(line.contains("Target id:"))
{
m_targetid = line.remove("Target id:").trimmed();
}
else if(line.contains("Memory:"))
{
m_ram = line.remove("Memory:").trimmed().toInt();
}
}
file.close();
m_success = true;
return;
}

View file

@ -0,0 +1,49 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2007 by Dominik Wenger
* $Id$
*
* 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.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef ROCKBOXINFO_H
#define ROCKBOXINFO_H
#include <QString>
class RockboxInfo
{
public:
RockboxInfo(QString mountpoint);
QString version() {return m_version;}
QString features(){return m_features;}
QString targetID() {return m_targetid;}
QString target() {return m_target;}
int ram() { return m_ram; }
bool success() { return m_success; }
private:
QString m_version;
QString m_features;
QString m_targetid;
QString m_target;
int m_ram;
bool m_success;
};
#endif

View file

@ -34,7 +34,7 @@ TTSExes::TTSExes(QString name,QObject* parent) : TTSBase(parent)
void TTSExes::generateSettings()
{
QString exepath =RbSettings::subValue(m_name,RbSettings::TtsPath).toString();
if(exepath == "") exepath = findExecutable(m_name);
if(exepath == "") exepath = Utils::findExecutable(m_name);
insertSetting(eEXEPATH,new EncTtsSetting(this,EncTtsSetting::eSTRING,
tr("Path to TTS engine:"),exepath,EncTtsSetting::eBROWSEBTN));

View file

@ -32,7 +32,7 @@ void TTSFestival::generateSettings()
// server path
QString exepath = RbSettings::subValue("festival-server",
RbSettings::TtsPath).toString();
if(exepath == "" ) exepath = findExecutable("festival");
if(exepath == "" ) exepath = Utils::findExecutable("festival");
insertSetting(eSERVERPATH,new EncTtsSetting(this,
EncTtsSetting::eSTRING, "Path to Festival server:",
exepath,EncTtsSetting::eBROWSEBTN));
@ -40,7 +40,7 @@ void TTSFestival::generateSettings()
// client path
QString clientpath = RbSettings::subValue("festival-client",
RbSettings::TtsPath).toString();
if(clientpath == "" ) clientpath = findExecutable("festival_client");
if(clientpath == "" ) clientpath = Utils::findExecutable("festival_client");
insertSetting(eCLIENTPATH,new EncTtsSetting(this,EncTtsSetting::eSTRING,
tr("Path to Festival client:"),
clientpath,EncTtsSetting::eBROWSEBTN));

View file

@ -32,7 +32,7 @@ void Uninstaller::deleteAll(ProgressloggerInterface* dp)
QString rbdir(m_mountpoint + ".rockbox/");
m_dp->addItem(tr("Starting Uninstallation"),LOGINFO);
m_dp->setProgressMax(0);
recRmdir(rbdir);
Utils::recursiveRmdir(rbdir);
m_dp->setProgressMax(1);
m_dp->setProgressValue(1);
m_dp->addItem(tr("Finished Uninstallation"),LOGOK);

View file

@ -18,6 +18,7 @@
****************************************************************************/
#include "utils.h"
#include "rockboxinfo.h"
#include "system.h"
#include "rbsettings.h"
#include "systeminfo.h"
@ -41,7 +42,7 @@
#endif
// recursive function to delete a dir with files
bool recRmdir( const QString &dirName )
bool Utils::recursiveRmdir( const QString &dirName )
{
QString dirN = dirName;
QDir dir(dirN);
@ -54,19 +55,21 @@ bool recRmdir( const QString &dirName )
curItem = dirN + "/" + name;
fileInfo.setFile(curItem);
if(fileInfo.isDir()) // is directory
recRmdir(curItem); // call recRmdir() recursively for deleting subdirectory
recursiveRmdir(curItem); // call recRmdir() recursively for
// deleting subdirectory
else // is file
QFile::remove(curItem); // ok, delete file
}
dir.cdUp();
return dir.rmdir(dirN); // delete empty dir and return if (now empty) dir-removing was successfull
return dir.rmdir(dirN); // delete empty dir and return if (now empty)
// dir-removing was successfull
}
//! @brief resolves the given path, ignoring case.
//! @param path absolute path to resolve.
//! @return returns exact casing of path, empty string if path not found.
QString resolvePathCase(QString path)
QString Utils::resolvePathCase(QString path)
{
QStringList elems;
QString realpath;
@ -110,7 +113,7 @@ QString resolvePathCase(QString path)
//! @brief figure the free disk space on a filesystem
//! @param path path on the filesystem to check
//! @return size in bytes
qulonglong filesystemFree(QString path)
qulonglong Utils::filesystemFree(QString path)
{
qlonglong size = 0;
#if defined(Q_OS_LINUX) || defined(Q_OS_MACX)
@ -135,7 +138,7 @@ qulonglong filesystemFree(QString path)
}
//! \brief searches for a Executable in the Environement Path
QString findExecutable(QString name)
QString Utils::findExecutable(QString name)
{
QString exepath;
//try autodetect tts
@ -167,7 +170,7 @@ QString findExecutable(QString name)
* @param permission if it should check for permission
* @return string with error messages if problems occurred, empty strings if none.
*/
QString check(bool permission)
QString Utils::checkEnvironment(bool permission)
{
QString text = "";
@ -201,47 +204,3 @@ QString check(bool permission)
return text;
}
RockboxInfo::RockboxInfo(QString mountpoint)
{
qDebug() << "[RockboxInfo] trying to find rockbox-info at" << mountpoint;
QFile file(mountpoint + "/.rockbox/rockbox-info.txt");
m_success = false;
if(!file.exists())
return;
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
// read file contents
while (!file.atEnd())
{
QString line = file.readLine();
if(line.contains("Version:"))
{
m_version = line.remove("Version:").trimmed();
}
else if(line.contains("Target: "))
{
m_target = line.remove("Target: ").trimmed();
}
else if(line.contains("Features:"))
{
m_features = line.remove("Features:").trimmed();
}
else if(line.contains("Target id:"))
{
m_targetid = line.remove("Target id:").trimmed();
}
else if(line.contains("Memory:"))
{
m_ram = line.remove("Memory:").trimmed().toInt();
}
}
file.close();
m_success = true;
return;
}

View file

@ -26,30 +26,14 @@
#include <QString>
#include <QUrl>
bool recRmdir( const QString &dirName );
QString resolvePathCase(QString path);
qulonglong filesystemFree(QString path);
QString findExecutable(QString name);
QString check(bool permission);
class RockboxInfo
class Utils
{
public:
RockboxInfo(QString mountpoint);
QString version() {return m_version;}
QString features(){return m_features;}
QString targetID() {return m_targetid;}
QString target() {return m_target;}
int ram() { return m_ram; }
bool success() { return m_success; }
private:
QString m_version;
QString m_features;
QString m_targetid;
QString m_target;
int m_ram;
bool m_success;
static bool recursiveRmdir(const QString &dirName);
static QString resolvePathCase(QString path);
static qulonglong filesystemFree(QString path);
static QString findExecutable(QString name);
static QString checkEnvironment(bool permission);
};
#endif

View file

@ -19,6 +19,7 @@
#include "voicefile.h"
#include "utils.h"
#include "rockboxinfo.h"
#include "rbsettings.h"
#include "systeminfo.h"

View file

@ -148,7 +148,7 @@ void ZipInstaller::downloadDone(bool error)
// check for free space. Make sure after installation will still be
// some room for operating (also includes calculation mistakes due to
// cluster sizes on the player).
if(filesystemFree(m_mountpoint) < (uz.totalSize() + 1000000)) {
if(Utils::filesystemFree(m_mountpoint) < (uz.totalSize() + 1000000)) {
emit logItem(tr("Not enough disk space! Aborting."), LOGERROR);
emit logProgress(1, 1);
emit done(true);

View file

@ -756,8 +756,8 @@ void Config::testTts()
}
tts->stop();
#if defined(Q_OS_LINUX)
QString exe = findExecutable("aplay");
if(exe == "") exe = findExecutable("play");
QString exe = Utils::findExecutable("aplay");
if(exe == "") exe = Utils::findExecutable("play");
if(exe != "")
{
QProcess::execute(exe+" "+filename);

View file

@ -25,6 +25,7 @@
#include "serverinfo.h"
#include "systeminfo.h"
#include "utils.h"
#include "rockboxinfo.h"
InstallWindow::InstallWindow(QWidget *parent) : QDialog(parent)
{
@ -168,7 +169,7 @@ void InstallWindow::accept()
RbSettings::sync();
QString warning = check(false);
QString warning = Utils::checkEnvironment(false);
if(!warning.isEmpty())
{
if(QMessageBox::warning(this, tr("Really continue?"), warning,

View file

@ -31,6 +31,7 @@
#include "themesinstallwindow.h"
#include "uninstallwindow.h"
#include "utils.h"
#include "rockboxinfo.h"
#include "rbzip.h"
#include "sysinfo.h"
#include "system.h"
@ -556,7 +557,7 @@ bool RbUtilQt::installAuto()
file.replace("%RELVERSION%", ServerInfo::value(ServerInfo::CurReleaseVersion).toString());
// check installed Version and Target
QString warning = check(false);
QString warning = Utils::checkEnvironment(false);
if(!warning.isEmpty())
{
if(QMessageBox::warning(this, tr("Really continue?"), warning,

View file

@ -67,6 +67,7 @@ SOURCES += \
base/bootloaderinstallchinachip.cpp \
base/bootloaderinstallams.cpp \
base/bootloaderinstalltcc.cpp \
base/rockboxinfo.cpp \
../../tools/mkboot.c \
../../tools/iriver.c \
@ -132,6 +133,7 @@ HEADERS += \
base/bootloaderinstallchinachip.h \
base/bootloaderinstallams.h \
base/bootloaderinstalltcc.h \
base/rockboxinfo.h \
../../tools/mkboot.h \
../../tools/iriver.h \

View file

@ -65,7 +65,7 @@ QString Sysinfo::getInfo()
for(int i = 0; i < drives.size(); i++) {
info += tr("%1, %2 MiB available")
.arg(QDir::toNativeSeparators(drives.at(i)))
.arg(filesystemFree(drives.at(i)) / (1024*1024));
.arg(Utils::filesystemFree(drives.at(i)) / (1024*1024));
if(i + 1 < drives.size())
info += "<br/>";
}

View file

@ -50,7 +50,7 @@ ThemesInstallWindow::ThemesInstallWindow(QWidget *parent) : QDialog(parent)
ThemesInstallWindow::~ThemesInstallWindow()
{
if(infocachedir!="")
recRmdir(infocachedir);
Utils::recursiveRmdir(infocachedir);
}