From 21aa80032f685b3ded10fe293ca083d2f46ae18c Mon Sep 17 00:00:00 2001 From: Manuel Schneider Date: Thu, 21 Nov 2013 15:38:19 +0100 Subject: alpha --- src/config.cpp | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/config.cpp (limited to 'src/config.cpp') diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..1e3f827 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "config.h" + +#define GROUP_GENERAL "General" +#define GROUP_MODELINES "Modelines" +#define GROUP_SPECIFIC "SpecificSettings" +#define DISPLAY_KEY "display" +#define DISPLAY_DEFAULT ":0" +#define IFACE_KEY "interface" +#define IFACE_DEFAULT "eth0" + +Config * Config::Instance = NULL; + +//_____________________________________________________________________________ + + +Config::Config() +{ + // Defaults + display = ":0"; + interface = "eth0"; +} + +//_____________________________________________________________________________ + + +void Config::loadSettings(QString _file) +{ + // Open setting file + settingsPath = _file; + QSettings settings(settingsPath, QSettings::NativeFormat); + + // Get general information + settings.beginGroup(GROUP_GENERAL); + display = settings.value(DISPLAY_KEY, DISPLAY_DEFAULT).toString(); + interface = settings.value(IFACE_KEY, IFACE_DEFAULT).toString(); + settings.endGroup(); + + + /* Check for ip specific settings */ + + // Get local ip + QString IPV4 = getIPV4ofInterface(interface); + + // Find any information saved about this ip + settings.beginGroup(GROUP_SPECIFIC); + if ( settings.contains(IPV4) ) + ipSpecificXConf = settings.value(IPV4).toStringList(); + settings.endGroup(); + + + /* Get the "must-have-modelines" */ + + settings.beginGroup(GROUP_MODELINES); + + // Get all keys in this group (Keys are modenames) + QStringList modeKeys = settings.allKeys(); + + // Get the modeline for each key + for (QStringList::const_iterator i = modeKeys.constBegin(); i != modeKeys.constEnd(); ++i) + { + // Prepend the name and save in list + modeLines.insert(*i, settings.value(*i).toStringList()); + } + settings.endGroup(); + +} + +//_____________________________________________________________________________ + + +QString Config::getIPV4ofInterface(QString _if) const +{ + struct ifaddrs * ifAddrStruct=NULL; + struct ifaddrs * ifa=NULL; + void * tmpAddrPtr=NULL; + QString result; + + getifaddrs(&ifAddrStruct); + + // Iterate through the adresses. + for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) + { + // If the address is IP V4 and the interface is _if + if (ifa ->ifa_addr->sa_family==AF_INET && ( strcmp(ifa->ifa_name, _if.toUtf8().constData()) == 0) ) + { + // Get the IP + tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; + + // convert to readable form + char addressBuffer[INET_ADDRSTRLEN]; + inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); + result = addressBuffer; + } + } + + // clean up + if (ifAddrStruct!=NULL) + freeifaddrs(ifAddrStruct); + + return result; +} -- cgit v1.2.3-55-g7522