summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/manager/rfcommassigndialogimpl.cpp
blob: 7943debd0681a303843e8fa575e956f9c0522cef (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
/* $Id$ */
/* RFCOMM binding table edit dialog */
/***************************************************************************
 *                                                                         *
 *   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 "rfcommassigndialogimpl.h"
#include "rfcommassigndialogitem.h"
#include "rfcommconfhandler.h"

/* OPIE */
#include <qpe/config.h>
#include <qpe/qpeapplication.h>
#include <opie2/odebug.h>
using namespace Opie::Core;

/* QT */
#include <qlayout.h>

using namespace OpieTooth;

// TODO: write only the file in bluebase?
// muss rfcommd dann neu gestartet werden
// sollte rfcomm bind all nicht eh default config sein ( polled das? - d.h. sobald nen gerät in der nähe ist bindet es?


RfcommAssignDialog::RfcommAssignDialog( QWidget* parent,  const char* name, bool modal, WFlags fl )
        : RfcommAssignDialogBase( parent, name, modal, fl )
{
    m_range = 5;

    m_scrollView = new QScrollView(this);
    m_scrollView->setResizePolicy( QScrollView::AutoOneFit );
    m_scrollView->setHScrollBarMode( QScrollView::AlwaysOff );

    RfcommAssignDialogBaseLayout->addWidget(m_scrollView);

    m_box = new QVBox(m_scrollView->viewport());
    m_scrollView->addChild(m_box);

    confHandler = new RfCommConfHandler( "/etc/bluetooth/rfcomm.conf" );

    loadConfig();
}

RfcommAssignDialog::~RfcommAssignDialog()
{
    if ( confHandler )
    {
        delete confHandler;
    }
}


void RfcommAssignDialog::newDevice(const QString & mac, int channel)
{

    for ( int i = 0 ; i < m_range; i++ )
    {
        QMap<QString, RfCommConfObject*>::Iterator it;
        it = confHandler->foundEntries().find(QString::number(i));
        // make sure that rfcommX is not assigned yet
        if ( it == confHandler->foundEntries().end() )
        {
            QDialog dialog( this,  "newdevice", true, WStyle_ContextHelp );
            dialog.setSizePolicy( QSizePolicy( (QSizePolicy::SizeType)7, 
                (QSizePolicy::SizeType)7, sizePolicy().hasHeightForWidth()));
            QVBoxLayout layout(&dialog);
            layout.setSpacing( 0 );
            layout.setMargin( 0 );
            
            RfcommDialogItem newDev(&dialog);
            newDev.setIdent( i );
            newDev.setMac( mac );
            newDev.setChannel( channel );
            layout.addWidget(&newDev);

            if ( QPEApplication::execDialog( &dialog ) == QDialog::Accepted )
            {
                QMap<int, RfcommDialogItem*>::Iterator it;
                it = m_itemList.find( i );
                RfcommDialogItem *rfcomm = it.data();
                rfcomm->setIdent( i );
                rfcomm->setMac( mac );
                rfcomm->setChannel( newDev.channel() );
                rfcomm->setComment( newDev.comment() );
                rfcomm->setBind( newDev.isBind() );
                odebug << "New device set up" << oendl; 
            }
            break;
        }
    }
}

/*
 * Load rfcomm bind configuration
 */
void RfcommAssignDialog::loadConfig()
{
    for ( int i = 0 ; i < m_range; i++ )
    {
        RfcommDialogItem *rfcomm = new RfcommDialogItem( m_box );
        m_itemList.insert(i, rfcomm);
        rfcomm->setIdent(i);
        QMap<QString, RfCommConfObject*>::Iterator it;
        it = confHandler->foundEntries().find(QString::number(i));
        if (it != confHandler->foundEntries().end())
        {
            rfcomm->setMac(it.data()->mac());
            rfcomm->setChannel(it.data()->channel());
            rfcomm->setComment(it.data()->comment());
            rfcomm->setBind(it.data()->isBind());
        }
    }
}


/*
 * Save rfcomm bind configuration
 */
void RfcommAssignDialog::saveConfig()
{
    QMap< int, RfcommDialogItem*>::Iterator it;
    QMap< QString, RfCommConfObject*> outMap;

    for( it = m_itemList.begin(); it != m_itemList.end(); ++it )
    {
        RfcommDialogItem *rfcomm = it.data();
        if (rfcomm->mac().isEmpty())
            continue;
        outMap.insert(QString::number(it.key()), 
            new RfCommConfObject(it.key(), 
                rfcomm->mac(), rfcomm->channel(), rfcomm->comment(), 
                rfcomm->isBind()));
    }
    confHandler->save(outMap);
}

/*
 * Reaction on the OK button
 */
void RfcommAssignDialog::accept()
{
    odebug << "save configuration" << oendl;
    saveConfig();
    RfcommAssignDialogBase::accept();
}

//eof