Add an initial Qt port.
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-wi.cpp
1 #include <QObject>
2 #include <QSocketNotifier>
3 #include <QTimer>
4
5 #include <cerrno>
6
7 #include "config.h"
8 #include "dhcpcd-wi.h"
9
10 DhcpcdWi::DhcpcdWi(DhcpcdQt *parent, DHCPCD_WPA *wpa)
11 {
12
13         this->dhcpcdQt = parent;
14         this->wpa = wpa;
15         scans = NULL;
16
17         int fd = dhcpcd_wpa_get_fd(wpa);
18         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
19         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
20         retryOpenTimer = NULL;
21 }
22
23 DhcpcdWi::~DhcpcdWi()
24 {
25
26         dhcpcd_wi_scans_free(scans);
27         if (notifier != NULL)
28                 delete notifier;
29 }
30
31 DHCPCD_WPA * DhcpcdWi::getWpa()
32 {
33
34         return wpa;
35 }
36
37 DHCPCD_WI_SCAN *DhcpcdWi::getScans()
38 {
39
40         return scans;
41 }
42 void DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
43 {
44
45         dhcpcd_wi_scans_free(this->scans);
46         this->scans = scans;
47 }
48
49 void DhcpcdWi::wpaOpen()
50 {
51         int fd = dhcpcd_wpa_open(wpa);
52         static int last_error;
53
54         if (fd == -1) {
55                 if (errno != last_error) {
56                         last_error = errno;
57                         qCritical("%s: dhcpcd_wpa_open: %s",
58                             dhcpcd_wpa_if(wpa)->ifname,
59                             strerror(last_error));
60                 }
61                 return;
62         }
63
64         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
65         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
66         if (retryOpenTimer) {
67                 delete retryOpenTimer;
68                 retryOpenTimer = NULL;
69         }
70 }
71
72 void DhcpcdWi::dispatch()
73 {
74
75         if (dhcpcd_wpa_get_fd(wpa) == -1) {
76                 delete notifier;
77                 notifier = NULL;
78                 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
79                 if (i == NULL ||
80                     strcmp(i->reason, "DEPARTED") == 0 ||
81                     strcmp(i->reason, "STOPPED") == 0)
82                         return;
83                 qWarning("%s: %s",
84                     i->ifname,
85                     qPrintable(tr("dhcpcd WPA connection lost")));
86                 if (retryOpenTimer == NULL) {
87                         retryOpenTimer = new QTimer(this);
88                         connect(retryOpenTimer, SIGNAL(timeout()),
89                             this, SLOT(wpaOpen()));
90                         retryOpenTimer->start(DHCPCD_RETRYOPEN);
91                 }
92                 return;
93         }
94
95         dhcpcd_wpa_dispatch(wpa);
96 }