]> git.meshlink.io Git - catta/blob - avahi-qt/qt-watch.cpp
fe8f2c30f7380362d9e02d8fd4eaf115427668c5
[catta] / avahi-qt / qt-watch.cpp
1 /***
2   This file is part of avahi.
3  
4   avahi is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8  
9   avahi is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13  
14   You should have received a copy of the GNU Lesser General Public
15   License along with avahi; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20 #include <sys/time.h>
21 #ifdef QT4
22 #include <Qt/qsocketnotifier.h>
23 #include <Qt/qobject.h>
24 #include <Qt/qtimer.h>
25 #else
26 #include <qsocketnotifier.h>
27 #include <qobject.h>
28 #include <qtimer.h>
29 #endif
30 #include "qt-watch.h"
31
32 class AvahiWatch : public QObject 
33 {
34     Q_OBJECT
35 public:
36     AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata);
37     ~AvahiWatch() {}
38     AvahiWatchEvent getEvents() const { return m_lastEvent; }
39     void setWatchedEvents(AvahiWatchEvent event);
40
41 private slots:
42     void gotIn();
43     void gotOut();
44
45 private:
46     QSocketNotifier* m_in;
47     QSocketNotifier* m_out;
48     //FIXME: ERR and HUP?
49     AvahiWatchCallback m_callback;
50     AvahiWatchEvent m_lastEvent;
51     int m_fd;
52     void* m_userdata;
53 };
54
55 class AvahiTimeout : public QObject 
56 {
57     Q_OBJECT
58     
59 public:
60     AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void* userdata);
61     ~AvahiTimeout() {}
62     void update(const struct timeval* tv);
63     
64 private slots:
65     void timeout();
66     
67 private:
68     QTimer m_timer;
69     AvahiTimeoutCallback m_callback;
70     void* m_userdata;
71 };
72
73
74
75 AvahiWatch::AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata) : 
76     m_in(0), m_out(0),  m_callback(callback), m_fd(fd), m_userdata(userdata)
77 {
78     setWatchedEvents(event);
79 }
80
81 void AvahiWatch::gotIn()
82 {
83     m_lastEvent = AVAHI_WATCH_IN;
84     m_callback(this,m_fd,m_lastEvent,m_userdata);
85 }
86
87 void AvahiWatch::gotOut()
88 {
89     m_lastEvent = AVAHI_WATCH_IN;
90     m_callback(this,m_fd,m_lastEvent,m_userdata);
91 }
92
93 void AvahiWatch::setWatchedEvents(AvahiWatchEvent event) 
94 {
95     if (!(event & AVAHI_WATCH_IN)) { delete m_in; m_in=0; }
96     if (!(event & AVAHI_WATCH_OUT)) { delete m_out; m_out=0; }
97     if (event & AVAHI_WATCH_IN) { 
98         m_in = new QSocketNotifier(m_fd,QSocketNotifier::Read, this);
99         connect(m_in,SIGNAL(activated(int)),SLOT(gotIn()));
100     }
101     if (event & AVAHI_WATCH_OUT) { 
102         m_out = new QSocketNotifier(m_fd,QSocketNotifier::Write, this);
103         connect(m_out,SIGNAL(activated(int)),SLOT(gotOut()));
104     }
105 }    
106
107 AvahiTimeout::AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void *userdata) : 
108     m_callback(callback), m_userdata(userdata)
109 {
110     connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
111     update(tv);
112 }
113
114 void AvahiTimeout::update(const struct timeval *tv)
115 {
116     m_timer.stop();
117     if (tv) {
118         struct timeval now;
119         gettimeofday(&now, 0);
120         m_timer.start((tv->tv_sec-now.tv_sec)*1000+(tv->tv_usec-now.tv_usec)/1000);
121     }
122 }
123
124 void AvahiTimeout::timeout()
125 {
126     m_callback(this,m_userdata);
127 }
128
129 static AvahiWatch* q_watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, 
130     void *userdata) 
131 {
132     return new AvahiWatch(fd, event, callback, userdata);
133 }
134
135 static void q_watch_update(AvahiWatch *w, AvahiWatchEvent events) 
136 {
137     w->setWatchedEvents(events);
138 }
139
140 static AvahiWatchEvent q_watch_get_events(AvahiWatch *w) 
141 {
142     return w->getEvents();
143 }
144     
145 static void q_watch_free(AvahiWatch *w) 
146 {
147     delete w;
148 }
149     
150 static AvahiTimeout* q_timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, 
151     void *userdata) 
152 {
153     return new AvahiTimeout(tv, callback, userdata);
154 }
155
156 static void q_timeout_update(AvahiTimeout *t, const struct timeval *tv) 
157 {
158     t->update(tv);
159 }
160
161 static void q_timeout_free(AvahiTimeout *t) 
162 {
163     delete t;
164 }
165
166
167 void create_qt_poll(AvahiPoll *api) 
168 {
169     api->userdata=0;
170     api->watch_new = q_watch_new;
171     api->watch_free = q_watch_free;
172     api->watch_update = q_watch_update;
173     api->watch_get_events = q_watch_get_events;
174     
175     api->timeout_new = q_timeout_new;
176     api->timeout_free = q_timeout_free;
177     api->timeout_update = q_timeout_update;
178 }
179
180 #include "qt-watch.moc"