Qt 4.8
qsocketnotifier.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qsocketnotifier.h"
43 
44 #include "qplatformdefs.h"
45 
47 #include "qcoreapplication.h"
48 
49 #include "qobject_p.h"
50 #include <private/qthread_p.h>
51 
53 
178  : QObject(parent)
179 {
180  sockfd = socket;
181  sntype = type;
182  snenabled = true;
183 
184  Q_D(QObject);
185  if (socket < 0)
186  qWarning("QSocketNotifier: Invalid socket specified");
187  else if (!d->threadData->eventDispatcher)
188  qWarning("QSocketNotifier: Can only be used with threads started with QThread");
189  else
190  d->threadData->eventDispatcher->registerSocketNotifier(this);
191 }
192 
193 #ifdef QT3_SUPPORT
194 
209  const char *name)
210  : QObject(parent)
211 {
213  if (socket < 0)
214  qWarning("QSocketNotifier: Invalid socket specified");
215  sockfd = socket;
216  sntype = type;
217  snenabled = true;
218 
219  Q_D(QObject);
220  if (!d->threadData->eventDispatcher) {
221  qWarning("QSocketNotifier: Can only be used with threads started with QThread");
222  } else {
223  d->threadData->eventDispatcher->registerSocketNotifier(this);
224  }
225 }
226 #endif
227 
232 {
233  setEnabled(false);
234 }
235 
236 
302 {
303  if (sockfd < 0)
304  return;
305  if (snenabled == enable) // no change
306  return;
307  snenabled = enable;
308 
309  Q_D(QObject);
310  if (!d->threadData->eventDispatcher) // perhaps application/thread is shutting down
311  return;
312  if (snenabled)
313  d->threadData->eventDispatcher->registerSocketNotifier(this);
314  else
315  d->threadData->eventDispatcher->unregisterSocketNotifier(this);
316 }
317 
318 
322 {
323  // Emits the activated() signal when a QEvent::SockAct is
324  // received.
325  if (e->type() == QEvent::ThreadChange) {
326  if (snenabled) {
328  Q_ARG(bool, snenabled));
329  setEnabled(false);
330  }
331  }
332  QObject::event(e); // will activate filters
333  if (e->type() == QEvent::SockAct) {
335  return true;
336  }
337  return false;
338 }
339 
double d
Definition: qnumeric_p.h:62
~QSocketNotifier()
Destroys this socket notifier.
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static QString fromAscii(const char *, int size=-1)
Returns a QString initialized with the first size characters from the string str. ...
Definition: qstring.cpp:4276
Type
This enum describes the various types of events that a socket notifier can recognize.
#define Q_ARG(type, data)
Definition: qobjectdefs.h:246
void activated(int socket)
This signal is emitted whenever the socket notifier is enabled and a socket event corresponding to it...
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
#define Q_D(Class)
Definition: qglobal.h:2482
Type type() const
Returns the socket event type specified to the constructor.
void setObjectName(const QString &name)
Definition: qobject.cpp:1112
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
QSocketNotifier(int socket, Type, QObject *parent=0)
Constructs a socket notifier with the given parent.
const char * name
#define emit
Definition: qobjectdefs.h:76
Q_CORE_EXPORT void qWarning(const char *,...)
Q_INVOKABLE QObject(QObject *parent=0)
Constructs an object with parent object parent.
Definition: qobject.cpp:753
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
int socket() const
Returns the socket identifier specified to the constructor.