From 6fde95f2921208361f3e5de1f419930bdefdc7fd Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Tue, 8 Mar 2011 20:09:24 +0100 Subject: host lookup, better MAC/IP info functions, IP one doesn't quite work yet --- src/DownloadManager.cpp | 43 +++++++++++++++++++++++------------------ src/fbgui.cpp | 9 +++++++++ src/main.cpp | 10 ++++++---- src/sysInfo.cpp | 51 ++++++++++++++++++++++++++----------------------- 4 files changed, 66 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/DownloadManager.cpp b/src/DownloadManager.cpp index eec6909..a80ab32 100644 --- a/src/DownloadManager.cpp +++ b/src/DownloadManager.cpp @@ -3,6 +3,21 @@ int DownloadManager::downloaded = 0; // ---------------------------------------------------------------------------------------- +DownloadManager::DownloadManager() +{ + qnam = new QNetworkAccessManager(); + dip = false; + downloadDir = QDir(downloadPath); + // Check if downloadPath exists, if not create it. + if (!downloadDir.exists()){ + if (debug) qDebug() << "Download directory: " << downloadDir.path() << "doesn't exist."; + QDir::current().mkdir(downloadPath); + if (downloadDir.exists() && debug) + qDebug() << "Created download directory: " << downloadDir.path(); + } + else if (debug) qDebug() << "Download directory: " << downloadDir.path() << "exists."; +} +// ---------------------------------------------------------------------------------------- void DownloadManager::downloadFile(QString& filename){ if (debug) qDebug() << "DM received signal for: " << filename; QUrl fileUrl; @@ -17,7 +32,12 @@ void DownloadManager::downloadFile(QUrl& fileUrl){ // ---------------------------------------------------------------------------------------- void DownloadManager::processDownloadRequest(QUrl& url) { - // Forge URL from the given filename and the base URL. + // Test on empty URL in case such a call happens, which should not + // happen given how javascriptInterface::startDownload(..) is implemented. + if (url.isEmpty()){ + if (debug) qDebug() << "No URL specified for download."; + return; + } // If download in progress, enqueue file and return. if (dip) { @@ -36,7 +56,6 @@ void DownloadManager::startNextDownload() { if (debug) qDebug() << "Starting next download: " << dlQ.head().toString() << "(" << dlQ.size() << "in queue.)"; - if (dlQ.isEmpty()) { if (debug) qDebug() << "Download queue empty! Exiting..."; @@ -59,7 +78,7 @@ void DownloadManager::startNextDownload() // If error upon opening, skip this file. if (!outfile.open(QIODevice::WriteOnly)) { - if (debug) qDebug() << "Couldn't open file! Exiting..."; + if (debug) qDebug() << "Couldn't open file! Skipping: " << url.toString(); startNextDownload(); return; } @@ -80,7 +99,7 @@ void DownloadManager::startNextDownload() QObject::connect(currentDownload, SIGNAL(finished()), this, SLOT(downloadFinished())); } // ---------------------------------------------------------------------------------------- -// Slots for the signals emmited by currentDownload. +// Private slots for queueing functionality. // ---------------------------------------------------------------------------------------- // This slot listens to readyRead() emmited when data is available for reading. void DownloadManager::downloadReady() @@ -119,18 +138,4 @@ void DownloadManager::downloadFinished() // Queue not empty: initialise next download. startNextDownload(); } -// ---------------------------------------------------------------------------------------- -// Constructor. -DownloadManager::DownloadManager() -{ - qnam = new QNetworkAccessManager(); - dip = false; - downloadDir = QDir(downloadPath); - // Check if downloadPath exists, if not create it. - if (!downloadDir.exists()){ - if (debug) qDebug() << "Download directory: " << downloadDir.path() << "doesn't exist."; - QDir::current().mkdir(downloadPath); - if (downloadDir.exists() && debug) qDebug() << "Created download directory: " << downloadPath; - } - else if (debug) qDebug() << "Download directory: " << downloadDir.path() << "exists."; -} + diff --git a/src/fbgui.cpp b/src/fbgui.cpp index ac078e0..6c2eead 100644 --- a/src/fbgui.cpp +++ b/src/fbgui.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -17,6 +18,14 @@ bool debug = false; fbgui::fbgui(QApplication *parent) { if (debug) qDebug() << "Application dir path: " << QApplication::applicationDirPath(); + + /* Lookup hostname */ + QHostInfo hostInfo = QHostInfo::fromName(baseURL.host()); + if (hostInfo.error() != QHostInfo::NoError){ + if (debug) qDebug() << "Lookup of " << baseURL.host() << "failed." << "Exiting..."; + exit(EXIT_FAILURE); + } + /* Browser init. */ QWebView* webView = new QWebView(this); webView->load(baseURL); diff --git a/src/main.cpp b/src/main.cpp index 44efa47..bd4951d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -82,17 +82,19 @@ int main(int argc, char *argv[]) baseURL = confFileSettings.value("default/url").toUrl(); else baseURL = DEFAULT_URL; + if (debug) qDebug() << "Base URL: " << baseURL.toString(); // Setting target downloads directory. if (clo.contains("downloadDir")){ - downloadPath = binPath + clo.value("downloadDir"); + downloadPath = clo.value("downloadDir"); } else if (confFileSettings.contains("default/downloadDirectory")){ - downloadPath = binPath + confFileSettings.value("default/downloadDirectory").toString(); + downloadPath = confFileSettings.value("default/downloadDirectory").toString(); } else - downloadPath = binPath + "/downloads"; - if (debug) qDebug() << "Downloads saved in: " << downloadPath; + downloadPath = "/downloads"; // Default download dir. + + if (debug) qDebug() << "Download directory: " << downloadPath; // Start fbgui. fbgui gui(&app); gui.setAttribute(Qt::WA_QuitOnClose, true); diff --git a/src/sysInfo.cpp b/src/sysInfo.cpp index 8c93c52..266c210 100644 --- a/src/sysInfo.cpp +++ b/src/sysInfo.cpp @@ -3,43 +3,46 @@ #include #include +//static int eth0_index = 0; +// ------------------------------------------------------------------------------------------------ sysInfo::sysInfo(){ if (debug) qDebug() << "sysInfo created."; + // Maybe search for eth0, etc } +// ------------------------------------------------------------------------------------------------ sysInfo::~sysInfo(){} +// ------------------------------------------------------------------------------------------------ QString sysInfo::getInfo(QString& infoName){ if (debug) qDebug() << "sysInfo : getInfo(" << infoName << ")"; if (infoName == QString("mac")) return getMACAddress(); else if (infoName == QString("ip")) return getIPAddress(); - // still here? --> error - return "error"; + // still here? + return "info_error"; } +// ------------------------------------------------------------------------------------------------ QString sysInfo::getMACAddress(){ - QNetworkInterface *qNetI = new QNetworkInterface(); - QList list; - list=qNetI->allInterfaces(); - QString str; - QString macAddress; - for (int i = 0; i < list.size(); ++i) { - str = list.at(i).name(); - macAddress = list.at(i).hardwareAddress(); - if (debug) qDebug() << str << " : " << macAddress; + /* Returns MAC address of eth0 for now. */ + QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0")); + if (!qni.isValid()){ + if (debug) qDebug() << "No interface matching \"eth0\" found."; + return "no_eth0"; } - // returns only the latest found interface ... to fix - return macAddress; + //eth0_index = qni.index(); + return qni.hardwareAddress(); } - +// ------------------------------------------------------------------------------------------------ QString sysInfo::getIPAddress(){ - QString ipAddress = ""; - QNetworkInterface *qNetI = new QNetworkInterface(); - QList list; - list=qNetI->allAddresses(); - for (int i = 0; i < list.size(); ++i) { - ipAddress = list.at(i).toString(); - if (debug) qDebug() << "IP Address" << " : " << ipAddress; - } - // returns only the latest found interface ... to fix - return ipAddress; + // Again for eth0 only at the moment. + // TODO: this doesn't quite work yet... + QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0")); + QList addrlist = qni.allAddresses(); + foreach(QHostAddress addr, addrlist){ + if (addr.protocol() == QAbstractSocket::IPv6Protocol) + qDebug() << "eth0: IPv4 Address: " << addr.toString(); + return addr.toString(); + } + // still here? + return "ip_error"; } -- cgit v1.2.3-55-g7522