summaryrefslogtreecommitdiff
path: root/core/applets/irdaapplet/irda.cpp
blob: 28a79d1e981d6a694157dc9ce4b85b50620f6bb1 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**********************************************************************
** Copyright (C) 2002 David Woodhouse <dwmw2@infradead.org>
** Heavily based on volumeapplet, (C) 2002 L.J. Potter ljp@llornkcor.com
**  All rights reserved.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
**********************************************************************/

#include "irda.h"
#include <qapplication.h>
#include <stdlib.h>


#include <qpe/resource.h>
#include <qpe/qpeapplication.h>
#include <qpe/timestring.h>
#include <qpe/resource.h>
#include <qpe/config.h>
#include <qpe/applnk.h>
#include <qpe/config.h>
#include <qpe/ir.h>
#include <qpe/qcopenvelope_qws.h>

#include <qdir.h>
#include <qfileinfo.h>
#include <qpoint.h>
#include <qpushbutton.h>
#include <qpainter.h>
#include <qcombobox.h>
#include <qspinbox.h>
#include <qslider.h>
#include <qlayout.h>
#include <qframe.h>
#include <qpixmap.h>
#include <qstring.h>
#include <qfile.h>
#include <qtimer.h>
#include <qpopupmenu.h>

#include <net/if.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>


//===========================================================================

IrdaApplet::IrdaApplet( QWidget *parent, const char *name )
    : QWidget( parent, name ) {
    setFixedHeight( 18 );
    setFixedWidth( 14 );
    sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
    irdaOnPixmap = Resource::loadPixmap( "irdaapplet/irdaon" );
    irdaOffPixmap = Resource::loadPixmap( "irdaapplet/irdaoff" );
    irdaDiscoveryOnPixmap = Resource::loadPixmap( "irdaapplet/magglass" );
    receiveActivePixmap = Resource::loadPixmap("irdaapplet/receive");
    receiveActive = false;
    startTimer(5000);
    timerEvent(NULL);
}

IrdaApplet::~IrdaApplet() {
    close(sockfd);
}

int IrdaApplet::checkIrdaStatus() {
    struct ifreq ifr;

    strcpy(ifr.ifr_name, "irda0");

    if (ioctl(sockfd, SIOCGIFFLAGS, &ifr))
        return -1;

    return (ifr.ifr_flags & IFF_UP)?1:0;
}

int IrdaApplet::setIrdaStatus(int c) {
    struct ifreq ifr;

    strcpy(ifr.ifr_name, "irda0");

    if (ioctl(sockfd, SIOCGIFFLAGS, &ifr))
        return -1;

    if (c)
        ifr.ifr_flags |= IFF_UP;
    else
        ifr.ifr_flags &= ~IFF_UP;

    if (ioctl(sockfd, SIOCSIFFLAGS, &ifr))
        return -1;

    return 0;
}

int IrdaApplet::checkIrdaDiscoveryStatus() {
    QFile discovery("/proc/sys/net/irda/discovery");
    char status;

    discovery.open( IO_ReadOnly|IO_Raw );
    discovery.readBlock (&status, 1);
    discovery.close();

    return atoi(&status);
}

int IrdaApplet::setIrdaDiscoveryStatus(int d) {
    QFile discovery("/proc/sys/net/irda/discovery");

    discovery.open( IO_WriteOnly|IO_Raw );

    if (d)
        discovery.putch('1');
    else
        discovery.putch('0');

    discovery.close();

    return 0;
}


void IrdaApplet::mousePressEvent( QMouseEvent *) {
    QPopupMenu *menu = new QPopupMenu();
    QString cmd;
    int ret=0;

    /* Refresh active state */
    timerEvent(NULL);

//	menu->insertItem( tr("More..."), 4 );
    if (irdaactive)
        menu->insertItem( tr("Disable IrDA"), 0 );
    else
        menu->insertItem( tr("Enable IrDA"), 1 );

    if (irdaDiscoveryActive)
        menu->insertItem( tr("Disable Discovery"), 2 );
    else
        menu->insertItem( tr("Enable Discovery"), 3 );

    if( receiveActive ){
        menu->insertItem( tr("Disable Receive"), 5 );
    } else {
        menu->insertItem( tr("Enable Receive"), 4 );
    }

    QPoint p = mapToGlobal( QPoint(1, menu->sizeHint().height()-1) );
    ret = menu->exec(p, 2);

    qDebug("ret was %d\n", ret);

    switch(ret) {
    case 0:
        setIrdaStatus(0);
        timerEvent(NULL);
        break;
    case 1:
        setIrdaStatus(1);
        timerEvent(NULL);
        break;
    case 2:
        setIrdaDiscoveryStatus(0);
        timerEvent(NULL);
        break;
    case 3:
        setIrdaDiscoveryStatus(1);
        timerEvent(NULL); // NULL is undefined in c++ use 0 or 0l
        break;
    case 4: { // enable receive
        qWarning("Enable receive" );
        QCopEnvelope e("QPE/Obex", "receive(int)" );
        e << 1;
        receiveActive = true;
        receiveStateChanged = true;
        timerEvent(NULL);
        break;
    }
    case 5: { // disable receive
        qWarning("Disable receive" );
        QCopEnvelope e("QPE/Obex", "receive(int)" );
        e << 0;
        receiveActive = false;
        receiveStateChanged = true;
        timerEvent(NULL);
        break;
    }
    case 6:
        qDebug("FIXME: Bring up pretty menu...\n");
        // With table of currently-detected devices.
    }
    delete menu; // Can somebody explain why use a QPopupMenu* and not QPopupMenu nor QAction. with out delete we will leak cause QPopupMenu doesn't have a parent in this case
}

void IrdaApplet::timerEvent( QTimerEvent * ) {
    int oldactive = irdaactive;
    int olddiscovery = irdaDiscoveryActive;
    bool receiveUpdate = false;

    if (receiveStateChanged) {
        receiveUpdate = true;
        receiveStateChanged = false;
    }

    irdaactive = checkIrdaStatus();
    irdaDiscoveryActive = checkIrdaDiscoveryStatus();

    if ((irdaactive != oldactive) || (irdaDiscoveryActive != olddiscovery) || receiveUpdate ) {
        paintEvent(NULL);
    }

}

void IrdaApplet::paintEvent( QPaintEvent* ) {
    QPainter p(this);
    qDebug("paint irda pixmap");

    p.eraseRect ( 0, 0, this->width(), this->height() );
    if (irdaactive > 0) {
        p.drawPixmap( 0, 1,  irdaOnPixmap );
    } else {
        p.drawPixmap( 0, 1,  irdaOffPixmap );
    }

    if (irdaDiscoveryActive > 0) {
        p.drawPixmap( 0, 1,  irdaDiscoveryOnPixmap );
    }
    if (receiveActive) {
        p.drawPixmap( 0, 1, receiveActivePixmap);
    }
}