summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/manager/hciconfwrapper.cpp
blob: 47e170e88c5486d89ee4077ba3743d30f8a01df7 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "hciconfwrapper.h"

#include <qfile.h>
#include <qtextstream.h>
#include <qregexp.h>

#include <opie2/odebug.h>
using namespace Opie::Core;

namespace OpieTooth {


    HciConfWrapper::HciConfWrapper( const QString &fileName) {
        m_fileName = fileName;
    }

    HciConfWrapper::~HciConfWrapper() {
    }


    void HciConfWrapper::setPinHelper( const QString& app  ) {
        setValue( "pin_helper"  , app  );
    }

    void HciConfWrapper::setName( const QString &name ) {
        odebug << "NAME : " << name << oendl;
        setValue( "name" , "\"" +  name + "\"" );
    }

    void HciConfWrapper::setIscan( bool enable) {

        if ( enable ) {
            setValue( "iscan" , "enable" );
        } else {
            setValue( "iscan" , "disable" );
        }
    }

    void HciConfWrapper::setPscan( bool enable) {

        if ( enable ) {
            setValue( "pscan" , "enable" );
        } else {
            setValue( "pscan" , "disable" );
        }
    }


    void HciConfWrapper::setAuth( bool enable) {

        if ( enable ) {
            setValue( "auth" , "enable" );
        } else {
            setValue( "auth" , "disable" );
        }
    }


    void HciConfWrapper::setEncrypt( bool enable) {

        if ( enable ) {
            setValue( "encrypt" , "enable" );
        } else {
            setValue( "encrypt" , "disable" );
        }
    }


    void HciConfWrapper::setValue(const QString &key, const  QString &value ) {

        if (m_file.isEmpty() ) // load first
            return;

        QStringList::Iterator it;

        QString str;
        for (it = m_file.begin(); it != m_file.end(); ++it ) {
            str = (*it);
            if( (str.contains(key)) > 0 ) {
                odebug << "Found" << oendl;
                // still need to look if its commented out!!!
                str.simplifyWhiteSpace();
                odebug << key << oendl;
                if (str.startsWith("#")) {
                    str = (key + " " + value + ";");
                } else {
                    str = str.replace( QRegExp( "\\s*"+key+"\\s+[^\\s][^;]*;" ),  key + " " + value + ";");
                }
                odebug << str << oendl;
                it = m_file.remove( it );
                it = m_file.insert( it,  str );
                //return; the regexp is too wide  -zecke // all set
            }
        }


    }

    /**
     * This loads the config file and stores it inside
     * the m_file
     */
    void HciConfWrapper::load() {
        owarn << "loaded" << oendl;
        m_file.clear();
        QFile file( m_fileName );
        if (!file.open( IO_ReadOnly ) ) {
            odebug << "Could not open" << oendl;
            return;
        }

            /**
             * readAll() into a QByteArray
             * QStringList::split('\n', array )
             * would this be faster? -zecke
             */
            QTextStream stream(&file );
            QString tmp;
            while ( !stream.atEnd() ) {
                tmp = stream.readLine();
                m_file.append( tmp );
            }
    }
    void HciConfWrapper::save() {
        owarn << "save" << oendl;
        if (m_file.isEmpty() ) // load first
            return;

        QFile file( m_fileName );
        if ( !file.open(IO_WriteOnly ) ) {
            owarn << "could not open " << m_fileName.latin1() << "" << oendl;
            return;
        }

        QTextStream stream(&file );
        QStringList::Iterator it;
        for ( it = m_file.begin(); it != m_file.end(); ++it ) {
            stream << (*it) << endl;
        }
        owarn << "saved" << oendl;
    };
}