summaryrefslogtreecommitdiffstats
path: root/src/config.cpp
blob: 1e3f82779b4948e080081c3d3d0971ab23465e63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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;
}