summaryrefslogtreecommitdiffstats
path: root/src/config.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.cpp')
-rw-r--r--src/config.cpp109
1 files changed, 109 insertions, 0 deletions
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 <QDebug>
+#include <QStringList>
+#include <stdio.h>
+#include <sys/types.h>
+#include <ifaddrs.h>
+#include <netinet/in.h>
+#include <string.h>
+#include <arpa/inet.h>
+#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;
+}