summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/manager/pandialog.cpp
blob: 10ca7a89b6e7ba6a5a272f1ae4b8dd01e9306394 (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
/* $Id$ */
/* PAN connection 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 "pandialog.h"
#include <qpushbutton.h>
#include <qlayout.h>
#include <qcheckbox.h>
#include <qlabel.h>
#include <qstring.h>
#include <qmultilineedit.h>
#include <opie2/oprocess.h>
#include <opie2/odebug.h>
using namespace Opie::Core;

using namespace OpieTooth;

using namespace Opie::Core;

PanDialog::PanDialog( const QString& device, QWidget* parent,  
    const char* name, bool modal, WFlags fl )
    : QDialog( parent, name, modal, fl ) {
    m_panConnect = NULL;
    
    if ( !name )
        setName( "PANDialog" );
    setCaption( tr( "PAN connection " ) ) ;

    m_device = device;

    layout = new QVBoxLayout( this );

    outPut = new QMultiLineEdit( this );
    QFont outPut_font(  outPut->font() );
    outPut_font.setPointSize( 8 );
    outPut->setFont( outPut_font );
    outPut->setWordWrap( QMultiLineEdit::WidgetWidth );

    connectButton = new QPushButton( this );
    connectButton->setText( tr( "Connect" ) );
    
    doEncryption = new QCheckBox(this, "encrypt");
    doEncryption->setText( tr( "encrypt" ) );
    doSecure = new QCheckBox(this, "secure connection");
    doSecure->setText( tr( "secure connection" ) );

    layout->addWidget(doEncryption);
    layout->addWidget(doSecure);
    layout->addWidget(outPut);
    layout->addWidget(connectButton);

    connect( connectButton, SIGNAL( clicked() ), this,  SLOT( connectToDevice() ) );
}

PanDialog::~PanDialog() {
}

void PanDialog::connectToDevice() {
    bool doEnc = doEncryption->isChecked();
    bool doSec = doSecure->isChecked();

    if (m_panConnect) {
        outPut->append(tr("Work in progress"));
        return;
    }
    m_panConnect = new OProcess();
    outPut->clear();
    
    // Fill process command line
    *m_panConnect << tr("pand")
            << tr("--connect") << m_device
            << tr("--nodetach");
    if (doEnc)
        *m_panConnect << tr("--encrypt");
    if (doSec)
        *m_panConnect << tr("--secure");
    if (!m_panConnect->start(OProcess::NotifyOnExit, 
        OProcess::All)) {
        outPut->append(tr("Couldn't start"));
        delete m_panConnect;
        m_panConnect = NULL;
    }
    else
    {
        m_panConnect->resume();
        outPut->append(tr("Started"));
        connect(m_panConnect, 
		    SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int)),
            this, SLOT(fillOutPut(Opie::Core::OProcess*, char*, int)));
        connect(m_panConnect, 
            SIGNAL(receivedStderr(Opie::Core::OProcess*, char*, int)),
            this,    SLOT(fillErr(Opie::Core::OProcess*, char*, int)));
        connect(m_panConnect, 
            SIGNAL(processExited(Opie::Core::OProcess*)),
            this, SLOT(slotProcessExited(Opie::Core::OProcess*)));
    }
}

void PanDialog::fillOutPut( OProcess*, char* cha, int len ) {
    QCString str(cha, len);
    outPut->append(str);
}

void PanDialog::fillErr(OProcess*, char* buf, int len)
{
    QCString str(buf, len);
    outPut->append(str);
}

void PanDialog::slotProcessExited(OProcess* proc) {
    if (m_panConnect->normalExit()) {
        outPut->append( tr("Finished with result ") );
        outPut->append( QString::number(proc->exitStatus()) );
    }
    else
        outPut->append( tr("Exited abnormally") );
    delete m_panConnect;
    m_panConnect = NULL;
}

void PanDialog::closeEvent(QCloseEvent* e)
{
    if (m_panConnect && m_panConnect->isRunning())
        m_panConnect->kill();
    QDialog::closeEvent(e);
}

//eof