summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/manager/rfcommconfhandler.cpp
blob: 054b44612075ee4f943d3f7c02c1cbeb71d7d375 (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

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

#include "rfcommconfhandler.h"


using namespace OpieTooth;

// move to lib


RfCommConfObject::RfCommConfObject(int number, QString mac, int channel, 
    QString comment, bool bind) {
    m_number = number;
    m_mac = mac;
    m_channel = channel;
    m_comment = comment;
    m_doBind = bind;
}

void RfCommConfObject::setNumber( int number )  {
    m_number = number;
}

void RfCommConfObject::setMac( QString mac )  {
    m_mac = mac;
}

void RfCommConfObject::setChannel( int channel )  {
    m_channel = channel;
}

void RfCommConfObject::setComment( QString comment )  {
    m_comment = comment;
}


RfCommConfObject::~RfCommConfObject()  {
}


RfCommConfHandler::RfCommConfHandler( const QString & filename ) {
    m_filename = filename;
    load();
}

RfCommConfHandler::~RfCommConfHandler() {

}

void RfCommConfHandler::save( QMap<QString, RfCommConfObject*> devices )  {

////    For debugging purposes
////    QFile rfCommConf( "/mnt/net/opie/bin/rfcomm.conf" );
    QFile rfCommConf( m_filename );
    QTextStream outStream( &rfCommConf );
    if (  rfCommConf.open( IO_WriteOnly ) )  {
        QMap<QString,  RfCommConfObject*>::Iterator it;
        outStream << "#\n";
        outStream << "# RFCOMM configuration file.\n";
        outStream << "#\n";
        outStream << "# $Id$\n";
        outStream << "#\n\n";
        for( it = devices.begin(); it != devices.end(); ++it )  {
            outStream << "rfcomm" + QString("%1").arg( it.data()->number() ) + " {\n";
            outStream << "\t# Automatically bind the device at startup\n";
            outStream << "\tbind " << ((it.data()->isBind())? "yes": "no") << ";\n";
            outStream << "\n";
            outStream << "\t# Bluetooth address of the device\n";
            outStream << "\tdevice " + it.data()->mac() + ";\n";
            outStream << "\n";
            outStream << "\t# RFCOMM channel for the connection\n";
            outStream << "\tchannel\t" + QString( "%1" ).arg( it.data()->channel() ) + ";\n";
            outStream << "\n";
            outStream << "\t# Description of the connection\n";
            outStream << "\tcomment \"" + it.data()->comment() + "\";\n";
            outStream << "}\n\n";
        }
        rfCommConf.close();
    }
}


QMap<QString, RfCommConfObject*> RfCommConfHandler::foundEntries()  {
    return m_foundEntries;
}

void RfCommConfHandler::load()  {
    //Keywords
    QCString k_rfcomm("rfcomm");
    QCString k_device("device ");
    QCString k_channel("channel ");
    QCString k_comment("comment ");
    QCString k_bind("bind ");

    m_foundEntries.clear();
    QFile rfCommConf(m_filename); //File we read
    if (rfCommConf.open(IO_ReadOnly))  {

        QStringList list;
        QTextStream inStream( &rfCommConf );
        list = QStringList::split( "\n", inStream.read() );

        QString mac;
        QString channel;
        QString comment;
        QString bind;
        bool bbind;
        QString number;

        for (QStringList::Iterator line = list.begin(); 
            line != list.end(); line++)  {

            QString tmpLine = (*line).simplifyWhiteSpace();

            if (tmpLine.startsWith(k_rfcomm))  {
                number = tmpLine.mid( k_rfcomm.length(), 1 );
                odebug << tmpLine << oendl;
                odebug << "device " << number << oendl;
            } else if ( tmpLine.startsWith( "}" ) ) {
                m_foundEntries.insert(number, 
                    new RfCommConfObject(number.toInt(), mac, channel.toInt(), 
                    comment, bbind));
            } else if ( tmpLine.startsWith(k_device) )  {
                mac = tmpLine.mid(k_device.length(), 
                    tmpLine.find(';') - k_device.length());
                odebug << "mac " + mac << oendl;
            } else if ( tmpLine.startsWith(k_channel) ) {
                channel = tmpLine.mid(k_channel.length(), 
                    tmpLine.find(';') - k_channel.length());
                odebug << "Channel: " << channel << oendl;
            } else if ( tmpLine.startsWith(k_comment) ) {
                comment = tmpLine.mid(k_comment.length(), 
                    tmpLine.find(';') - k_comment.length());
                if (comment.left(1) == "\"") 
                    comment.remove(0, 1);
                if (comment.right(1) == "\"") 
                    comment.remove(comment.length() - 1, 1);
                odebug << "Comment: " + comment << oendl;
            } else if ( tmpLine.startsWith(k_bind) ) {
                bind = tmpLine.mid(k_bind.length(), 
                    tmpLine.find(';') - k_bind.length());
                if (bind == "no")
                    bbind = false;
                else if (bind == "yes")
                    bbind = true;
                else
                    bbind = true;
                odebug << "bind: " + bind << oendl;
            }
        }
        rfCommConf.close();
    }
    save(m_foundEntries);
    odebug << QString( "ENTries: %1").arg( m_foundEntries.count() ) << oendl;
}

//eof