summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/manager/hciconfwrapper.cpp
blob: 2413f2b89eeff446fdec7edea4641e9a495f28d2 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* $Id$ */
/* hcid.conf parser */
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#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" );
            setValue( "security" , "auto" );
        } else {
            setValue( "auth" , "disable" );
            setValue( "security" , "none" );
        }
    }


    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;
        QString tmpLine;
        bool wasCommented = false; //If the optin string was commented out
        QRegExp rx1("^" + key + "[ ]+"); //Regexp fo key searching
        QRegExp rx2(";[ ]*" + key + "[ ]+"); //Regexp fo key searching
        for (it = m_file.begin(); it != m_file.end(); ++it ) {
            wasCommented = false;
            str = (*it);
            tmpLine = str.simplifyWhiteSpace();
            //If it's commented out, remove the comment and check again
            //Now, let's check if this is a real keyword (not word_keyword)
            if (tmpLine.startsWith("#")) { 
                tmpLine.remove(0, 1);
                tmpLine = tmpLine.simplifyWhiteSpace();
                wasCommented = true;
            }
            if( (tmpLine.contains(rx1)) > 0 || (tmpLine.contains(rx2)) > 0) {
                odebug << "Found " + key << oendl;
                
                if (wasCommented)
                    str = ("\t" + key + " " + value + ";");
                else
                    str = str.replace(QRegExp("\\s*" + key + "\\s+[^\\s][^;]*;"), 
                        "\t" + 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;
    };
}