4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 #include <Qt/qsocketnotifier.h>
25 #include <Qt/qobject.h>
26 #include <Qt/qtimer.h>
28 #include <qsocketnotifier.h>
32 #include <avahi-common/timeval.h>
35 class AvahiWatch : public QObject
39 AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata);
41 AvahiWatchEvent getEvents() const { return m_incallback ? m_lastEvent : (AvahiWatchEvent)0; }
42 void setWatchedEvents(AvahiWatchEvent event);
49 QSocketNotifier* m_in;
50 QSocketNotifier* m_out;
52 AvahiWatchCallback m_callback;
53 AvahiWatchEvent m_lastEvent;
59 class AvahiTimeout : public QObject
64 AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void* userdata);
66 void update(const struct timeval* tv);
73 AvahiTimeoutCallback m_callback;
79 AvahiWatch::AvahiWatch(int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void* userdata) :
80 m_in(0), m_out(0), m_callback(callback), m_fd(fd), m_userdata(userdata), m_incallback(false)
82 setWatchedEvents(event);
85 void AvahiWatch::gotIn()
87 m_lastEvent = AVAHI_WATCH_IN;
89 m_callback(this,m_fd,m_lastEvent,m_userdata);
93 void AvahiWatch::gotOut()
95 m_lastEvent = AVAHI_WATCH_IN;
97 m_callback(this,m_fd,m_lastEvent,m_userdata);
101 void AvahiWatch::setWatchedEvents(AvahiWatchEvent event)
103 if (!(event & AVAHI_WATCH_IN)) { delete m_in; m_in=0; }
104 if (!(event & AVAHI_WATCH_OUT)) { delete m_out; m_out=0; }
105 if (event & AVAHI_WATCH_IN) {
106 m_in = new QSocketNotifier(m_fd,QSocketNotifier::Read, this);
107 connect(m_in,SIGNAL(activated(int)),SLOT(gotIn()));
109 if (event & AVAHI_WATCH_OUT) {
110 m_out = new QSocketNotifier(m_fd,QSocketNotifier::Write, this);
111 connect(m_out,SIGNAL(activated(int)),SLOT(gotOut()));
115 AvahiTimeout::AvahiTimeout(const struct timeval* tv, AvahiTimeoutCallback callback, void *userdata) :
116 m_callback(callback), m_userdata(userdata)
118 connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
120 m_timer.setSingleShot(true);
125 void AvahiTimeout::update(const struct timeval *tv)
129 AvahiUsec u = avahi_age(tv)/1000;
131 m_timer.start( (u>0) ? 0 : -u);
133 m_timer.start( (u>0) ? 0 : -u,true);
138 void AvahiTimeout::timeout()
140 m_callback(this,m_userdata);
143 static AvahiWatch* q_watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback,
146 return new AvahiWatch(fd, event, callback, userdata);
149 static void q_watch_update(AvahiWatch *w, AvahiWatchEvent events)
151 w->setWatchedEvents(events);
154 static AvahiWatchEvent q_watch_get_events(AvahiWatch *w)
156 return w->getEvents();
159 static void q_watch_free(AvahiWatch *w)
164 static AvahiTimeout* q_timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback,
167 return new AvahiTimeout(tv, callback, userdata);
170 static void q_timeout_update(AvahiTimeout *t, const struct timeval *tv)
175 static void q_timeout_free(AvahiTimeout *t)
180 static AvahiPoll qt_poll;
182 const AvahiPoll* avahi_qt_poll_get(void)
185 qt_poll.watch_new = q_watch_new;
186 qt_poll.watch_free = q_watch_free;
187 qt_poll.watch_update = q_watch_update;
188 qt_poll.watch_get_events = q_watch_get_events;
190 qt_poll.timeout_new = q_timeout_new;
191 qt_poll.timeout_free = q_timeout_free;
192 qt_poll.timeout_update = q_timeout_update;
197 #include "qt-watch.moc4"
199 #include "qt-watch.moc3"