From e7329f1acbb02d6f4b1ab2be66282291bff60ccf Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Mon, 21 Mar 2011 11:47:21 +0100 Subject: minor code convention fixes --- src/downloadManager.cpp | 214 -------------------------------------------- src/downloadManager.h | 81 ----------------- src/fbgui.cpp | 15 ++-- src/fbgui.pro | 26 +++--- src/javascriptInterface.cpp | 60 ------------- src/javascriptInterface.h | 50 ----------- src/loggerEngine.cpp | 67 -------------- src/loggerEngine.h | 53 ----------- src/sysInfo.cpp | 109 ---------------------- src/sysInfo.h | 43 --------- 10 files changed, 19 insertions(+), 699 deletions(-) delete mode 100644 src/downloadManager.cpp delete mode 100644 src/downloadManager.h delete mode 100644 src/javascriptInterface.cpp delete mode 100644 src/javascriptInterface.h delete mode 100644 src/loggerEngine.cpp delete mode 100644 src/loggerEngine.h delete mode 100644 src/sysInfo.cpp delete mode 100644 src/sysInfo.h diff --git a/src/downloadManager.cpp b/src/downloadManager.cpp deleted file mode 100644 index 43c27e9..0000000 --- a/src/downloadManager.cpp +++ /dev/null @@ -1,214 +0,0 @@ -#include "downloadManager.h" -#include "fbgui.h" - -int downloadManager::downloaded = 0; -// ---------------------------------------------------------------------------------------- -downloadManager::downloadManager(){ - qxtLog->debug() << "Initializing download manager..."; - checkDownloadDirectory(); - qnam = new QNetworkAccessManager(); - dip = false; -} -// ---------------------------------------------------------------------------------------- -void downloadManager::checkDownloadDirectory() -{ - // check if downloadPath exists, if not create it. - downloadDir = QDir(downloadPath); - if (!downloadDir.exists()){ - qxtLog->debug() << "Download directory: " << downloadDir.path() << " doesn't exist."; - QDir::current().mkdir(downloadPath); - if (downloadDir.exists()){ - qxtLog->debug() << "Created download directory: " << downloadDir.path(); - } - else { - qxtLog->debug() << "Failed to create directory: " << downloadDir.path(); - // try to save to /tmp/fbgui - downloadDir.setPath(QDir::tempPath () + "/fbgui"); - if (!downloadDir.exists()){ - QDir::current().mkdir(QDir::tempPath () + "/fbgui"); - if (!downloadDir.exists()){ - // TODO: dont exit, this shouldn't happen anyway (right?) - qxtLog->debug() << "Fatal, no target for downloads. Exiting..."; - exit(EXIT_FAILURE); - } - } - qxtLog->debug() << "Saving downloads to: " << downloadDir.absolutePath(); - } - } - else qxtLog->debug() << "Download directory: " << downloadDir.path() << " exists."; -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadFile(const QString& filename) -{ - QUrl fileUrl(baseURL.resolved(QUrl(filename))); - this->processDownloadRequest(fileUrl); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadFile(const QUrl& fileUrl) -{ - this->processDownloadRequest(fileUrl); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::processDownloadRequest(const QUrl& url) -{ - if (url.isEmpty()){ - qxtLog->debug() << "No URL specified for download."; - return; - } - // if download in progress, enqueue file and return. - if (dip){ - dlQ.enqueue(url); - qxtLog->debug() << "Download in progress! Queued:" << url.toString() - << "(" << dlQ.size() << " in queue)"; - return; - } - // no running downloads: enqueue and start next download. - dlQ.enqueue(url); - qxtLog->debug() << "Enqueueing:" << url.toString(); - startNextDownload(); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::startNextDownload() -{ - if (dlQ.isEmpty()){ - emit downloadQueueEmpty(); - qxtLog->debug() << "Download manager ready. (1)"; - return; - } - qxtLog->debug() << "Starting next download: " << dlQ.head().toString() - << " (" << dlQ.size() - 1 << " in queue.)"; - - // dequeue next URL to download. - QUrl url = dlQ.dequeue(); - - // get filename from URL. - QString tmp = url.path(); - tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1); - - // check if filename exists on target file system - if (downloadDir.exists(tmp)){ - qxtLog->debug() << "File already exists: " << downloadDir.absoluteFilePath(tmp); - outfile.setFileName(QString(downloadDir.absolutePath() + "/" + tmp + ".\%1").arg(downloaded)); - } - else - outfile.setFileName(downloadDir.absoluteFilePath(tmp)); - qxtLog->debug() << "Saving to: " << outfile.fileName(); - - // try to open for writing - if (!outfile.open(QIODevice::WriteOnly)){ - qxtLog->debug() << "No write access to " << outfile.fileName() << " . Skipping download..."; - return; - } - - // send the request for the file - QNetworkRequest request(url); - currentDownload = qnam->get(request); - lastProgress = 0; - currentProgress = 0; - dip = true; - dltime.start(); - QObject::connect(currentDownload, SIGNAL(readyRead()), this, SLOT(downloadReady())); - QObject::connect(currentDownload, SIGNAL(metaDataChanged()), this, SLOT(processMetaInfo())); - QObject::connect(currentDownload, SIGNAL(downloadProgress(qint64, qint64)), - this, SLOT(downloadProgress(qint64, qint64))); - QObject::connect(currentDownload, SIGNAL(finished()), this, SLOT(downloadFinished())); -} -// ---------------------------------------------------------------------------------------- -// Private slots to process downloads -// ---------------------------------------------------------------------------------------- -void downloadManager::processMetaInfo() -{ - // fetch filesize from header & filename from URL (for now) - const QByteArray cltag = "Content-Length"; - QByteArray clinfo = currentDownload->rawHeader(cltag); - QFileInfo fi(outfile); - emit downloadInfo(outfile.fileName(), clinfo.toDouble()); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadReady() -{ - // data ready, save it - outfile.write(currentDownload->readAll()); -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) -{ - if (bytesIn > bytesTotal) return; - // calculate current speed - double speed = bytesIn * 1000 / dltime.elapsed(); - QString unit; - if (speed < 1024) { - unit = "bytes/sec"; - } - else if (speed < 1024*1024) { - speed /= 1024; - unit = "KB/s"; - } - else { - speed /= 1024*1024; - unit = "MB/s"; - } - // update progress only if difference higher than the updateInterval setting - currentProgress = ((bytesIn * 100) / bytesTotal); - if (currentProgress - lastProgress >= updateInterval){ - lastProgress = currentProgress; - emit updateProgress(currentProgress, speed, unit); - qxtLog->debug() << "Download progress of " << currentDownload->url().toString() - << ": " << bytesIn << "/" << bytesTotal << "(" << currentProgress << "\%)"; - } - return; -} -// ---------------------------------------------------------------------------------------- -void downloadManager::downloadFinished() -{ - // check for errors - if (currentDownload->error()){ - currentDownload->deleteLater(); - outfile.remove(); - int statusCode = currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - qxtLog->debug() << "Download of " << currentDownload->url().toString() - << " failed with HTTP error code: " << statusCode; - emit notify(QString("Download failed! HTTP Status Code: %1").arg(statusCode)); - } - else{ - // end download - currentDownload->deleteLater(); - outfile.close(); - downloaded++; - qxtLog->debug() << "Download of " << currentDownload->url().toString() - << " finished. (downloaded = "<< downloaded << ")"; - emit notify(QString("Successfully downloaded %1").arg(currentDownload->url().toString())); - } - dip = false; - // process next in queue - if (dlQ.isEmpty()){ - emit downloadQueueEmpty(); - qxtLog->debug() << "Download manager ready. (2)"; - return; - } - startNextDownload(); -} -/******************************************************************************************************** -* - ** dead code: Header filename fetching & renaming ** - -const QByteArray cd = "Content-Disposition"; -QByteArray cdc = currentDownload->rawHeader(cd); -int x = cdc.indexOf("filename=\"") + 10; -cdc.remove(0, x).chop(1); -if (!cdc.isEmpty()) - currentTargetFilename = cdc; -else - currentTargetFilename = QString("download.\%1").arg(downloaded); - -QString tmp = outfile.fileName(); -tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1); -qDebug() << "Trying to rename " << tmp << " to --> " << currentTargetFilename; - -if (outfile.rename(downloadPath + "/" + currentTargetFilename)) { - qxtLog->debug() << "Renamed file!"; -} -else { - qxtLog->debug() << "Failure to rename file!"; -} -*/ diff --git a/src/downloadManager.h b/src/downloadManager.h deleted file mode 100644 index c80e58a..0000000 --- a/src/downloadManager.h +++ /dev/null @@ -1,81 +0,0 @@ -/* -# Copyright (c) 2010,2011 - RZ Uni Freiburg -# Copyright (c) 2010,2011 - OpenSLX Project -# -# This program/file is free software distributed under the GPL version 2. -# See http://openslx.org/COPYING -# -# If you have any feedback please consult http://openslx.org/feedback and -# send your feedback to feedback@openslx.org -# -# General information about OpenSLX can be found under http://openslx.org -# -# -# Helper class managing downloads. -# -*/ - -#ifndef DOWNLOADMANAGER_H -#define DOWNLOADMANAGER_H - -#include "fbgui.h" - -extern bool debug; -extern QUrl baseURL; -extern QString binPath; -extern QString downloadPath; -extern int updateInterval; - - -class downloadManager : public QObject -{ - Q_OBJECT - -public: - downloadManager(); - -private: - // checks for valid download directory, ran once in constructor - void checkDownloadDirectory(); - // private control function for queueing mechanism. - void processDownloadRequest(const QUrl& url); - - // base objects for downloading - QNetworkAccessManager* qnam; - QQueue dlQ; - QNetworkReply* currentDownload; - QFile outfile; - QDir downloadDir; - // download progress variables - QTime dltime; - int currentProgress, lastProgress; - // download in progress flag - bool dip; - // static counter - static int downloaded; - -signals: - // notify sends a message to the javascript interface. - void notify(const QString& msg); - // downloadInfo sends static information (name, size) to the interface. - void downloadInfo(const QString& filename, const double& filesize); - // updateProgress sends download progress information to the interface. - void updateProgress(const int& percent, const double& speed, const QString& unit); - // signal emitted when download queue is empty. - void downloadQueueEmpty(); - -public slots: - // public slots to receive download requests. - void downloadFile(const QUrl& fileUrl); - void downloadFile(const QString& fileUrl); - -private slots: - // private slots to manage the downloading process - void startNextDownload(); - void processMetaInfo(); - void downloadReady(); - void downloadProgress(qint64 bytesIn, qint64 bytesTotal); - void downloadFinished(); -}; - -#endif // DOWNLOADMANAGER_H diff --git a/src/fbgui.cpp b/src/fbgui.cpp index bec4eb5..a48b069 100644 --- a/src/fbgui.cpp +++ b/src/fbgui.cpp @@ -1,7 +1,7 @@ #include "fbgui.h" -#include "loggerEngine.h" -#include "downloadManager.h" -#include "javascriptInterface.h" +#include "loggerengine.h" +#include "downloadmanager.h" +#include "javascriptinterface.h" #include #include @@ -20,7 +20,7 @@ fbgui::fbgui() qxtLog->disableLoggerEngine("DEFAULT"); qxtLog->enableLogLevels(QxtLogger::DebugLevel); if (debugMode == 0){ - qxtLog->addLoggerEngine("std_logger", new loggerEngine_std); + qxtLog->addLoggerEngine("std_logger", new LoggerEngine_std); qxtLog->initLoggerEngine("std_logger"); qxtLog->setMinimumLevel("std_logger", QxtLogger::DebugLevel); qxtLog->debug() << "Initializing fbgui..."; @@ -39,13 +39,13 @@ fbgui::fbgui() setCentralWidget(_webView); // initialize javascript interface - javascriptInterface* jsi = new javascriptInterface(_webView->page()->mainFrame()); + JavascriptInterface* jsi = new JavascriptInterface(_webView->page()->mainFrame()); QObject::connect(jsi, SIGNAL(quitFbgui()), this, SLOT(close())); QObject::connect(_webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), jsi, SLOT(attachToDOM())); // initialize download manager - downloadManager* dm = new downloadManager(); + DownloadManager* dm = new DownloadManager(); QObject::connect(dm, SIGNAL(downloadInfo(const QString&, const double&)), jsi, SLOT(downloadInfo(const QString&, const double&))); QObject::connect(dm, SIGNAL(notify(const QString&)), @@ -69,7 +69,6 @@ void fbgui::createActions() _quit->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_X)); this->addAction(_quit); connect(_quit, SIGNAL(triggered()), this, SLOT(close())); - } //------------------------------------------------------------------------------------------- void fbgui::setupDebugSplit() @@ -82,7 +81,7 @@ void fbgui::setupDebugSplit() _debugConsole->setTextColor(Qt::cyan); _debugConsole->insertPlainText("Debug console initialized.\n"); // enable custom logger engine - qxtLog->addLoggerEngine("fb_logger", new loggerEngine_fb(_debugConsole)); + qxtLog->addLoggerEngine("fb_logger", new LoggerEngine_fb(_debugConsole)); qxtLog->initLoggerEngine("fb_logger"); qxtLog->setMinimumLevel("fb_logger", QxtLogger::DebugLevel); // display browser and debug in a splitter diff --git a/src/fbgui.pro b/src/fbgui.pro index 0ca0078..353518b 100644 --- a/src/fbgui.pro +++ b/src/fbgui.pro @@ -1,13 +1,11 @@ TEMPLATE = app TARGET = fbgui CONFIG += qt \ - qxt \ debug QT += core \ gui \ - webkit \ - network -QXT += core + network \ + webkit LIBS += -L/usr/local/qjson/lib \ -lqjson \ -L/usr/local/Qxt/lib \ @@ -15,16 +13,16 @@ LIBS += -L/usr/local/qjson/lib \ INCLUDEPATH += /usr/local/qjson/include \ /usr/local/Qxt/include \ /usr/local/Qxt/include/QxtCore -HEADERS += loggerEngine.h \ - downloadManager.h \ - fbgui.h \ - javascriptInterface.h \ - sysInfo.h -SOURCES += loggerEngine.cpp \ - downloadManager.cpp \ - main.cpp \ +HEADERS += sysinfo.h \ + loggerengine.h \ + javascriptinterface.h \ + downloadmanager.h \ + fbgui.h +SOURCES += sysinfo.cpp \ + loggerengine.cpp \ + javascriptinterface.cpp \ + downloadmanager.cpp \ fbgui.cpp \ - javascriptInterface.cpp \ - sysInfo.cpp + main.cpp FORMS += RESOURCES += fbgui.qrc diff --git a/src/javascriptInterface.cpp b/src/javascriptInterface.cpp deleted file mode 100644 index a15cf8d..0000000 --- a/src/javascriptInterface.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "fbgui.h" -#include "javascriptInterface.h" -#include "sysInfo.h" - - -//------------------------------------------------------------------------------------------------------- -javascriptInterface::javascriptInterface(QWebFrame *parent){ - qxtLog->debug() << "Initializing javascript interface..."; - _parent = parent; -} -//------------------------------------------------------------------------------------------------------- -javascriptInterface::~javascriptInterface() {} -//------------------------------------------------------------------------------------------------------- -const QString javascriptInterface::getSysInfo(const QString& info){ - sysInfo si; - return si.getInfo(info); -} -//------------------------------------------------------------------------------------------------------- -void javascriptInterface::attachToDOM(){ - _parent->addToJavaScriptWindowObject(QString("fbgui"), this); -} -//------------------------------------------------------------------------------------------------------- -void javascriptInterface::startDownload(const QString& filename){ - // ignore if empty filename - if (filename.isEmpty()){ - _parent->evaluateJavaScript("alert(\"No filename!\")"); - return; - } - emit requestFile(filename); -} -//------------------------------------------------------------------------------------------------------- -void javascriptInterface::downloadInfo(const QString& filename, const double& filesize){ - QString code = QString("downloadInfo('\%1', \%2)").arg(filename).arg(filesize); - _parent->evaluateJavaScript(code); -} -//------------------------------------------------------------------------------------------------------- -void javascriptInterface::notify(const QString& msg){ - - QString code = QString("notify('\%1')").arg(msg); - _parent->evaluateJavaScript(code); -} -//------------------------------------------------------------------------------------------------------- -void javascriptInterface::updateProgressBar(const int& percent, const double& speed, const QString& unit){ - if (percent == 0) return; - QString code = QString("updateProgress(\%1, \%2, '\%3')").arg(percent).arg(speed).arg(unit); - _parent->evaluateJavaScript(code); -} -//------------------------------------------------------------------------------------------------------- -void javascriptInterface::setCallbackOnDlQueueFinished(QString& jsFunction){ - _callBackOnDownloadsFinished = jsFunction; -} -//------------------------------------------------------------------------------------------------------- -void javascriptInterface::callbackOnDlQueueFinished(){ - QString code = QString("\%1").arg(_callBackOnDownloadsFinished); - _parent->evaluateJavaScript(code); -} -//------------------------------------------------------------------------------------------------------- -void javascriptInterface::quit(){ - emit quitFbgui(); -} diff --git a/src/javascriptInterface.h b/src/javascriptInterface.h deleted file mode 100644 index 20bd206..0000000 --- a/src/javascriptInterface.h +++ /dev/null @@ -1,50 +0,0 @@ -/* -# Copyright (c) 2010,2011 - RZ Uni Freiburg -# Copyright (c) 2010,2011 - OpenSLX Project -# -# This program/file is free software distributed under the GPL version 2. -# See http://openslx.org/COPYING -# -# If you have any feedback please consult http://openslx.org/feedback and -# send your feedback to feedback@openslx.org -# -# General information about OpenSLX can be found under http://openslx.org -# -# -# Interface for javascript. -# -*/ - -#ifndef JAVASCRIPTINTERFACE_H_ -#define JAVASCRIPTINTERFACE_H_ - -#include "fbgui.h" - -class javascriptInterface : public QObject -{ - Q_OBJECT -private: - QWebFrame* _parent; - QString _callBackOnDownloadsFinished; - -public: - javascriptInterface(QWebFrame* parent); - virtual ~javascriptInterface(); - -signals: - void requestFile(const QString& filename); - void quitFbgui(); - -public slots: - void attachToDOM(); - void startDownload(const QString& filename); - void setCallbackOnDlQueueFinished(QString& fctOnDownloadsFinished); - void callbackOnDlQueueFinished(); - void updateProgressBar(const int& percent, const double& speed, const QString& unit); - void downloadInfo(const QString& filename, const double& filesize); - void notify(const QString& msg); - const QString getSysInfo(const QString& info); - void quit(); -}; - -#endif // JAVASCRIPTINTERFACE_H_ diff --git a/src/loggerEngine.cpp b/src/loggerEngine.cpp deleted file mode 100644 index 9121af5..0000000 --- a/src/loggerEngine.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "loggerEngine.h" - -// -------------------------------------------------------------------------------------------------- -// Custom Logger Engine for debugging on framebuffer -//--------------------------------------------------------------------------------------------------- -loggerEngine_fb::loggerEngine_fb(QTextEdit *parent) : QxtLoggerEngine(){ - // TODO: silly parent storing ... to change! - _debugConsole = parent; - _initialized = false; - setLogLevelsEnabled(QxtLogger::DebugLevel); - enableLogging(); -} -loggerEngine_fb::~loggerEngine_fb(){} - -void loggerEngine_fb::initLoggerEngine(){ - _initialized = true; - return; -} - -void loggerEngine_fb::killLoggerEngine(){return;} - -void loggerEngine_fb::setLogLevelEnabled(QxtLogger::LogLevels level, bool enable){ - QxtLoggerEngine::setLogLevelsEnabled(level, enable); - if (!enable) QxtLoggerEngine::setLogLevelsEnabled(QxtLogger::DebugLevel); -} -bool loggerEngine_fb::isInitialized() const{ - return _initialized; -} - -void loggerEngine_fb::writeFormatted(QxtLogger::LogLevel level, const QList & msgs){ - // TODO: handle different log levels - if (msgs.isEmpty()) return; - Q_FOREACH(const QVariant& out, msgs) - { - if (!out.isNull()) - _debugConsole->insertPlainText(out.toString()); - } - _debugConsole->insertPlainText(QString("\n")); - // autoscroll - QTextCursor c = _debugConsole->textCursor(); - c.movePosition(QTextCursor::End); - _debugConsole->setTextCursor(c); -} -//--------------------------------------------------------------------------------------------------- -// Modified QxtBasicSTDLoggerEngine -//--------------------------------------------------------------------------------------------------- -loggerEngine_std::loggerEngine_std() : QxtBasicSTDLoggerEngine(){} - -loggerEngine_std::~loggerEngine_std(){} - -void loggerEngine_std::writeToStdErr(const QString& str_level, const QList &msgs){ - if (msgs.isEmpty()) return; - QString header = '[' + QTime::currentTime().toString("hh:mm:ss.zzz") + "] "; - QTextStream* errstream = stdErrStream(); - Q_ASSERT(errstream); - *errstream << header; - Q_FOREACH(const QVariant& out, msgs) - { - if (!out.isNull()) - *errstream << out.toString(); - } - *errstream << endl; -} -void loggerEngine_std::writeToStdOut(const QString& level, const QList & msgs){ - // reimplementing this is needed for compiling, - // we only need write to std::err, so this function is not needed -} diff --git a/src/loggerEngine.h b/src/loggerEngine.h deleted file mode 100644 index be1ac87..0000000 --- a/src/loggerEngine.h +++ /dev/null @@ -1,53 +0,0 @@ -/* -# Copyright (c) 2010,2011 - RZ Uni Freiburg -# Copyright (c) 2010,2011 - OpenSLX Project -# -# This program/file is free software distributed under the GPL version 2. -# See http://openslx.org/COPYING -# -# If you have any feedback please consult http://openslx.org/feedback and -# send your feedback to feedback@openslx.org -# -# General information about OpenSLX can be found under http://openslx.org -# -# -# Custom logger engines based on Qxt library. -# -*/ -#ifndef LOGGERENGINE_H_ -#define LOGGERENGINE_H_ - -#include -#include -#include -//--------------------------------------------------------------------------------------------------- -// custom engine feeding the debug console widget -//--------------------------------------------------------------------------------------------------- -class loggerEngine_fb : public QxtLoggerEngine { -public: - loggerEngine_fb(QTextEdit* parent); - ~loggerEngine_fb(); - QTextEdit *_debugConsole; - bool _initialized; - - // reimplemented virtual functions of QxtLoggerEngine - void initLoggerEngine(); - void killLoggerEngine(); - void writeFormatted(QxtLogger::LogLevel level, const QList & messages); - void setLogLevelEnabled(QxtLogger::LogLevels level, bool enable = true); - bool isInitialized() const; - -}; -//--------------------------------------------------------------------------------------------------- -// minor changes to QxtBasicSTDLoggerEngine -//--------------------------------------------------------------------------------------------------- -class loggerEngine_std : public QxtBasicSTDLoggerEngine { -public: - loggerEngine_std(); - ~loggerEngine_std(); - // reimplemented virtual functions of QxtBasicSTDLoggerEngineqqq - void writeToStdOut(const QString& level, const QList &msgs); - void writeToStdErr(const QString& str_level, const QList &msgs); -}; - -#endif // LOGGERENGINE_H_ diff --git a/src/sysInfo.cpp b/src/sysInfo.cpp deleted file mode 100644 index e7ed8a8..0000000 --- a/src/sysInfo.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#include "sysInfo.h" - - -// ------------------------------------------------------------------------------------------------ -sysInfo::sysInfo(){} -// ------------------------------------------------------------------------------------------------ -sysInfo::~sysInfo(){} -// ------------------------------------------------------------------------------------------------ -const QString sysInfo::getInfo(const QString& infoName){ - qxtLog->debug() << "[sysInfo] requested " << infoName; - if (infoName == QString("mac")) - return getMACAddress(); - else if (infoName == QString("ip")) - return getIPAddress(); - else if (infoName == QString("all")) - return getAllInfos(); - else if (infoName == QString("json")) - return getNames(); - /* unknown code */ - return "info_error"; -} -// ------------------------------------------------------------------------------------------------ -const QString sysInfo::getMACAddress(){ - // Returns MAC address of eth0 for now - QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0")); - if (!qni.isValid()){ - qxtLog->debug() << "No interface matching \"eth0\" found."; - return "no_eth0"; - } - //eth0_index = qni.index(); - return qni.hardwareAddress(); -} -// ------------------------------------------------------------------------------------------------ -const QString sysInfo::getIPAddress(){ - // Again for eth0 only at the moment. - // TODO: this doesn't quite work yet... - QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0")); - QList addrlist = qni.allAddresses(); - // This is does not return the right IP atm... - foreach(QHostAddress addr, addrlist){ - if (addr.protocol() == QAbstractSocket::IPv4Protocol && addr != QHostAddress::LocalHost){ - return addr.toString(); - } - } - // still here? - qxtLog->debug() << "ip_error"; - return "ip_error"; -} -// ------------------------------------------------------------------------------------------------ -const QByteArray sysInfo::getNames(){ - - QVariantMap foo; - foo.insert("name", "foo"); - foo.insert("type", 123); - - QVariantMap fooo; - fooo.insert("name", "boo"); - fooo.insert("type", 321); - - QVariantList jsonV; - jsonV << foo << fooo; - - QJson::Serializer serializer; - QByteArray json = serializer.serialize(jsonV); - - qxtLog->debug() << json; - return json; - -} -// ------------------------------------------------------------------------------------------------ -QString sysInfo::getAllInfos(){ - QVariantMap infos; - infos.insert("mac", getMACAddress()); - infos.insert("ip", getIPAddress()); - infos.insert("whoami", getScriptOutput("whoami")); - //infos.insert("pwd", getScriptOutput("pwd")); - - //QJson::Serializer serializer; - QByteArray json = serializer.serialize(infos); - - qxtLog->debug() << json; - return json; -} -// ------------------------------------------------------------------------------------------------ -QString sysInfo::getScriptOutput(QString cmd) -{ - QProcess *process = new QProcess(); - qDebug()<<"try to open: "<start(cmd, QIODevice::ReadOnly); - - if (!process->waitForStarted() ) - qDebug()<<"process couldn't get opened"; - - QString output; - process->waitForFinished(); - - QTextStream *txt_stream = new QTextStream(process); - - while(!txt_stream->atEnd() ) - { - qDebug()<<"read output: "; - QString tmp_str = txt_stream->readLine(); - output += tmp_str; - qDebug()< - -class sysInfo { -public: - sysInfo(); - ~sysInfo(); - // public access, valid infoName: "mac", "ip", ... - const QString getInfo(const QString& infoName); - -private: - // private system information readers - const QString getMACAddress(); - const QString getIPAddress(); - QString getAllInfos(); - QString getScriptOutput(QString cmd); - - // JSon testing - QJson::Serializer serializer; - const QByteArray getNames(); -}; - -#endif // SYSTINFO_H -- cgit v1.2.3-55-g7522