summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/lib/obexpush.cpp
blob: e0a4bee6e65fa6d91c00c3ebb232b7e0c155a7e4 (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
/* $Id$ */
/* OBEX push functions implementation */
/***************************************************************************
 *                                                                         *
 *   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 "obexpush.h"

#include <qfileinfo.h>
#include <qfile.h>

#include <opie2/odebug.h>
#include <opie2/odebug.h>

using namespace Opie::Core;
using namespace OpieTooth;

ObexPush::ObexPush()
{
    pushProc = new OProcess();
    connect(pushProc, SIGNAL(processExited(Opie::Core::OProcess*)),
        this, SLOT(slotPushExited(Opie::Core::OProcess*)));
    connect(pushProc, SIGNAL(receivedStdout(Opie::Core::OProcess*, char*, int)),
        this, SLOT(slotPushOut(Opie::Core::OProcess*, char*, int)));
    connect(pushProc, SIGNAL(receivedStderr(Opie::Core::OProcess*, char*, int)),
        this, SLOT(slotPushErr(Opie::Core::OProcess*, char*, int)));

}

ObexPush::~ObexPush()
{
    if (pushProc->isRunning())
        pushProc->kill();
    delete pushProc;
    pushProc = NULL;
}

/**
 * Function that sends a file
 * @param mac destination device MAC address
 * @param port service port number
 * @param src source file name
 * @param dst destination file name
 * @return 0 on success, -1 on error, 1 if sending process is running
 */
int ObexPush::send(QString& mac, int port, QString& src, QString& dst)
{
    QString dev = mac;
    QString execName = "ussp-push";
    if (pushProc->isRunning())
        return 1;
    pushProc->clearArguments();
    dev += "@";
    dev += QString::number(port); 
    if (!dst.isEmpty())
        *pushProc << execName << "--timeo 30" << dev 
            << QFile::encodeName(src) << QFile::encodeName(dst);
    else
        *pushProc << execName << "--timeo 30" << dev 
            << QFile::encodeName(src) 
            << QFile::encodeName(QFileInfo(src).fileName());
    odebug << execName << " " << dev << " " << src << " " 
        << ((!dst.isEmpty())? dst: QFileInfo(src).fileName()) << oendl;
    pushProc->setUseShell(true);
    if (!pushProc->start(OProcess::NotifyOnExit, OProcess::All))
        return -1;
    else 
        return 0;
}

void ObexPush::slotPushExited(Opie::Core::OProcess* proc)
{
    if (pushProc != proc)
        return;
    odebug << "Process exited" << oendl;
    if (pushProc->normalExit())
        emit sendComplete(pushProc->exitStatus());
    else
        emit sendError(-1);
}

/**
 * Function makes a notification from slotPushOut and slotPushErr
 */
void ObexPush::notifyInfo(Opie::Core::OProcess* proc, char* buf, int len)
{
    if (proc != pushProc)
        return;
    QCString str(buf, len);
    odebug << str << oendl;
    emit status(str);
}

void ObexPush::slotPushOut(Opie::Core::OProcess* proc, char* buf, int len)
{
    notifyInfo(proc, buf, len);
}

void ObexPush::slotPushErr(Opie::Core::OProcess* proc, char* buf, int len)
{
    notifyInfo(proc, buf, len);
}

//eof