summaryrefslogtreecommitdiff
path: root/core/applets/obex/processwrapper.cc
blob: ebc77945a641fc12f11e05783d4a1338d0af5d8e (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
//
m_rec == KProcess
m_count = int // the number of tries to recieve
m_send == KProcess
m_file == QString filename to send
m_outp == the output of the process

//


void receive() {
    m_rec = new KProcess();
    *m_rec << "irobex_palm3";
    // connect to the necessary slots
    connect(m_rec,  SIGNAL(processExited(KProcess*) ),
            this,  SLOT(slotExited(KProcess*) ) );

    connect(m_rec,  SIGNAL(receivedStdout(KProcess*, char*,  int ) ),
            this,  SLOT(slotStdOut(KProcess*, char*, int) ) );

    if(!m_rec->start(KProcess::NotifyOnExit, KProcess::AllOutput) ) {
        qWarning("could not start :(");
        emit done( false );
    }
    emit try(m_count )
}
void send(const QString &fileName) {
    m_count = 0;
    m_file = fileName;
    sendNow();
}
void sendNow() {
    if ( m_count >= 15 ) { // could not send
        emit error(-1 );
    }
    // KProcess inititialisation
    m_send = new KProcess();
    m_send << "irobex_palm3";
    m_send << m_file;

    // connect to slots Exited and and StdOut
    connect(m_send,  SIGNAL(processExited(KProcess*) ),
            this, SLOT(slotExited(KProcess*)) );
    connect(m_send,  SIGNAL(receivedStdout(KProcess*, char*,  int ) )
            this, SLOT(slotStdOut(KProcess*, char*, int) ) );
    // now start it
    if (!m_send->start(/*KProcess::NotifyOnExit,  KProcess::AllOutput*/ ) ) {
        m_count = 15;
        emit error(-1 );
    }
    // end
    m_count++;
    emit try( m_count );
}

void recieved() {
    if (m_rec->normalExit() ) {
        if ( m_rec->exitStatus() == 0 ) { // we got one
            QString filename = parseOut();
            emit recievedFile( filename );
        }
    }else{
        emit error(-1);
    };
    delete m_rec;
}


void slotExited(KProcess* proc) {
    if (proc == m_rec ) { // recieve process
        recieved();
    }else if ( proc == m_send ) {
        send();
    }
}


void send() {
    if (m_send->normalExit() ) {
        if ( m_send->exitStatus() == 0 ) {
            delete m_send;
            m_send=0;
            emit sent();
        }else if (m_send->exitStatus() == 255 ) { // it failed maybe the other side wasn't ready
            // let's try it again
            delete m_send;
            m_send = 0;
            sendNow();
        }
    }else {
        emit error( -1 );
        delete m_send;
        m_send = 0;
    }

}
void parseOut() {
    QStringList list = QStringList::split("\n",  m_outp);
    QStringList::Iterator it;
    for (it = list.begin(); it != list.end(); ++it ) {
        if ( (*it).startsWith("Wrote"  ) ) {
            QStringList pathes = QStringList::split(' ', (*it) );
            QString path = pathes[1];
        }
    }
}

void slotStdOut(KProcess* proc, char* buf, int len) {
    if ( proc == m_rec ) { // only recieve
        QCString cstring( buf,  len );
        m_outp.append( cstring.data() );
    }

}