Imported Upstream version 17.22.1
[platform/upstream/libzypp.git] / zypp / zyppng / base / socketnotifier.cc
1 #include "socketnotifier.h"
2 #include "private/abstracteventsource_p.h"
3
4 namespace zyppng {
5
6 class SocketNotifierPrivate : public AbstractEventSourcePrivate
7 {
8   ZYPP_DECLARE_PUBLIC(SocketNotifier)
9 public:
10
11   signal<void (const SocketNotifier &, int)> _activated;
12
13   int _socket = -1;
14   int _mode = SocketNotifier::Read;
15   bool _enabled = false;
16
17 };
18
19 SocketNotifier::SocketNotifier (int socket, int evTypes , bool enable)
20   : AbstractEventSource ( * new SocketNotifierPrivate )
21 {
22   Z_D();
23   d->_socket = socket;
24   d->_mode = evTypes;
25
26   setEnabled( enable );
27 }
28
29 SocketNotifier::Ptr SocketNotifier::create(int socket, int evTypes, bool enable )
30 {
31   return std::shared_ptr<SocketNotifier>( new SocketNotifier( socket, evTypes, enable ) );
32 }
33
34 void SocketNotifier::setMode(int mode)
35 {
36   Z_D();
37   if ( mode == d->_mode )
38     return;
39
40   d->_mode = mode;
41
42   if ( d->_enabled && d->_socket >= 0)
43     updateFdWatch( d->_socket, d->_mode );
44 }
45
46 int SocketNotifier::mode() const
47 {
48   return d_func()->_mode;
49 }
50
51 void SocketNotifier::setEnabled( bool enabled )
52 {
53   Z_D();
54   if ( enabled == d->_enabled || d->_socket < 0 )
55     return;
56
57   d->_enabled = enabled;
58
59   if ( enabled )
60     updateFdWatch( d->_socket, d->_mode );
61   else
62     removeFdWatch( -1 );
63 }
64
65 int SocketNotifier::socket() const
66 {
67   return d_func()->_socket;
68 }
69
70 SignalProxy<void (const SocketNotifier &, int)> SocketNotifier::sigActivated()
71 {
72   return d_func()->_activated;
73 }
74
75 void SocketNotifier::onFdReady( int, int events )
76 {
77   d_func()->_activated( *this, events );
78 }
79
80 void SocketNotifier::onSignal( int )
81 { }
82
83
84 }
85
86