summaryrefslogtreecommitdiff
path: root/core/applets/irdaapplet/irda.cpp
Unidiff
Diffstat (limited to 'core/applets/irdaapplet/irda.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/applets/irdaapplet/irda.cpp159
1 files changed, 159 insertions, 0 deletions
diff --git a/core/applets/irdaapplet/irda.cpp b/core/applets/irdaapplet/irda.cpp
new file mode 100644
index 0000000..2eff2a9
--- a/dev/null
+++ b/core/applets/irdaapplet/irda.cpp
@@ -0,0 +1,159 @@
1/**********************************************************************
2** Copyright (C) 2002 David Woodhouse <dwmw2@infradead.org>
3** Heavily based on volumeapplet, (C) 2002 L.J. Potter ljp@llornkcor.com
4** All rights reserved.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14**********************************************************************/
15
16#include "irda.h"
17#include <qapplication.h>
18#include <stdlib.h>
19
20
21#include <qpe/resource.h>
22#include <qpe/qpeapplication.h>
23#include <qpe/timestring.h>
24#include <qpe/resource.h>
25#include <qpe/config.h>
26#include <qpe/applnk.h>
27#include <qpe/config.h>
28
29#include <qdir.h>
30#include <qfileinfo.h>
31#include <qpoint.h>
32#include <qpushbutton.h>
33#include <qpainter.h>
34#include <qcombobox.h>
35#include <qspinbox.h>
36#include <qslider.h>
37#include <qlayout.h>
38#include <qframe.h>
39#include <qpixmap.h>
40#include <qstring.h>
41#include <qfile.h>
42#include <qtimer.h>
43#include <qpopupmenu.h>
44
45#include <net/if.h>
46#include <netinet/in.h>
47#include <sys/types.h>
48#include <sys/socket.h>
49#include <sys/ioctl.h>
50
51
52//===========================================================================
53
54IrdaApplet::IrdaApplet( QWidget *parent, const char *name )
55 : QWidget( parent, name )
56{
57 setFixedHeight( 18 );
58 setFixedWidth( 14 );
59 sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
60 irdaOnPixmap = Resource::loadPixmap( "irdaapplet/irdaon" );
61 irdaOffPixmap = Resource::loadPixmap( "irdaapplet/irdaoff" );
62 startTimer(5000);
63 timerEvent(NULL);
64}
65
66IrdaApplet::~IrdaApplet()
67{
68 close(sockfd);
69}
70
71int IrdaApplet::checkIrdaStatus()
72{
73 struct ifreq ifr;
74
75 strcpy(ifr.ifr_name, "irda0");
76
77 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr))
78 return -1;
79
80 return (ifr.ifr_flags & IFF_UP)?1:0;
81}
82
83int IrdaApplet::setIrdaStatus(int c)
84{
85 struct ifreq ifr;
86
87 strcpy(ifr.ifr_name, "irda0");
88
89 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr))
90 return -1;
91
92 if (c)
93 ifr.ifr_flags |= IFF_UP;
94 else
95 ifr.ifr_flags &= ~IFF_UP;
96
97 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr))
98 return -1;
99
100 return 0;
101}
102
103void IrdaApplet::mousePressEvent( QMouseEvent *)
104{
105 QPopupMenu *menu = new QPopupMenu();
106 QString cmd;
107 int ret=0;
108
109 /* Refresh active state */
110 timerEvent(NULL);
111
112 //menu->insertItem( tr("More..."), 2 );
113 if (irdaactive)
114 menu->insertItem( tr("Disable IrDA"), 0 );
115 else
116 menu->insertItem( tr("Enable IrDA"), 1 );
117
118 QPoint p = mapToGlobal( QPoint(1, -menu->sizeHint().height()-1) );
119 ret = menu->exec(p, 1);
120
121 qDebug("ret was %d\n", ret);
122
123 switch(ret) {
124 case 0:
125 setIrdaStatus(0);
126 timerEvent(NULL);
127 break;
128 case 1:
129 setIrdaStatus(1);
130 timerEvent(NULL);
131 break;
132 case 2:
133 qDebug("FIXME: Bring up pretty menu...\n");
134 // With 'discovery' button to enable/disable,
135 // and table of currently-detected devices.
136 }
137
138}
139
140void IrdaApplet::timerEvent( QTimerEvent * )
141{
142 int oldactive = irdaactive;
143
144 irdaactive = checkIrdaStatus();
145 if (irdaactive != oldactive)
146 paintEvent(NULL);
147
148}
149
150void IrdaApplet::paintEvent( QPaintEvent* )
151{
152 QPainter p(this);
153 qDebug("paint irda pixmap");
154
155 if (irdaactive > 0)
156 p.drawPixmap( 0, 1, irdaOnPixmap );
157 else
158 p.drawPixmap( 0, 1, irdaOffPixmap );
159}