If we don't understand the AP security (such as EAP) then disable the
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-ssidmenuwidget.cpp
1 /*
2  * dhcpcd-qt
3  * Copyright 2014-2015 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <QWidget>
28 #include <QEvent>
29 #include <QHBoxLayout>
30 #include <QRadioButton>
31 #include <QIcon>
32 #include <QLabel>
33 #include <QPixmap>
34 #include <QStyle>
35
36 #include "dhcpcd.h"
37 #include "dhcpcd-qt.h"
38 #include "dhcpcd-ssidmenuwidget.h"
39
40 #define ICON_SIZE       16
41
42 DhcpcdSsidMenuWidget::DhcpcdSsidMenuWidget(QWidget *parent,
43     DhcpcdWi *wi, DHCPCD_WI_SCAN *scan)
44     : QFrame(parent)
45 {
46
47         this->wi = wi;
48         this->scan = scan;
49         this->active = true;
50
51         QHBoxLayout *layout = new QHBoxLayout(this);
52         layout->setContentsMargins(1, 1, 1, 1);
53         selicon = new QLabel(this);
54         selicon->setMinimumSize(ICON_SIZE, ICON_SIZE);
55         layout->addWidget(selicon);
56
57         ssid = new QLabel(this);
58         ssid->setSizePolicy(QSizePolicy::MinimumExpanding,
59             QSizePolicy::MinimumExpanding);
60         layout->addWidget(ssid);
61
62         encicon = new QLabel(this);
63         layout->addWidget(encicon);
64         layout->setAlignment(encicon, Qt::AlignRight);
65
66         stricon = new QLabel(this);
67         layout->addWidget(stricon);
68         layout->setAlignment(stricon, Qt::AlignRight);
69
70         setScan(scan);
71         installEventFilter(this);
72
73         setStyleSheet("QFrame { border-radius: 4px; } "
74             "QFrame:hover {"
75             "color:palette(highlighted-text);"
76             "background-color:palette(highlight);"
77             "}");
78 }
79
80 DHCPCD_WI_SCAN *DhcpcdSsidMenuWidget::getScan()
81 {
82
83         return scan;
84 }
85
86 bool DhcpcdSsidMenuWidget::isAssociated()
87 {
88
89         return associated;
90 }
91
92 bool DhcpcdSsidMenuWidget::isActive()
93 {
94
95         return active;
96 }
97
98 void DhcpcdSsidMenuWidget::setActive(bool active)
99 {
100
101         if (this->active != active) {
102                 this->ssid->setEnabled(active);
103                 this->active = active;
104         }
105 }
106
107 void DhcpcdSsidMenuWidget::setScan(DHCPCD_WI_SCAN *scan)
108 {
109         DHCPCD_WPA *wpa;
110         DHCPCD_IF *i;
111         QIcon icon;
112         QPixmap picon;
113         bool active;
114
115         this->scan = scan;
116         wpa = wi->getWpa();
117         i = dhcpcd_wpa_if(wpa);
118         associated = dhcpcd_wi_associated(i, scan);
119
120         /*
121          * Icons are rescaled because they are not always the size requested
122          * due to a bug in Qt-4.8.
123          * https://qt.gitorious.org/qt/qt/merge_requests/2566
124          */
125
126         if (associated) {
127                 icon = DhcpcdQt::getIcon("actions", "dialog-ok-apply");
128                 picon = icon.pixmap(ICON_SIZE);
129                 if (picon.height() != ICON_SIZE)
130                         picon = picon.scaledToHeight(ICON_SIZE,
131                             Qt::SmoothTransformation);
132                 selicon->setPixmap(picon);
133                 ssid->setStyleSheet("font:bold;");
134         } else {
135                 selicon->setPixmap(NULL);
136                 ssid->setStyleSheet(NULL);
137         }
138         ssid->setText(scan->ssid);
139         active = true;
140         if (scan->flags & WSF_SECURE) {
141                 if (scan->flags & WSF_PSK)
142                         icon = DhcpcdQt::getIcon("status",
143                             "network-wireless-encrypted");
144                 else {
145                         icon = DhcpcdQt::getIcon("status",
146                             "network-error");
147                         active = false;
148                 }
149         } else
150                 icon = DhcpcdQt::getIcon("status", "dialog-warning");
151         picon = icon.pixmap(ICON_SIZE);
152         if (picon.height() != ICON_SIZE)
153                 picon = picon.scaledToHeight(ICON_SIZE, Qt::SmoothTransformation);
154         encicon->setPixmap(picon);
155
156         icon = DhcpcdQt::getIcon("status",
157             DhcpcdQt::signalStrengthIcon(scan));
158         picon = icon.pixmap(ICON_SIZE);
159         if (picon.height() != ICON_SIZE)
160                 picon = picon.scaledToHeight(ICON_SIZE, Qt::SmoothTransformation);
161         stricon->setPixmap(picon);
162
163         setActive(active);
164 }
165
166 bool DhcpcdSsidMenuWidget::eventFilter(QObject *, QEvent *event)
167 {
168
169         if (event->type() == QEvent::MouseButtonPress) {
170                 if (active)
171                         emit triggered();
172         }
173         return false;
174 }
175
176 void DhcpcdSsidMenuWidget::enterEvent(QEvent *)
177 {
178
179         if (!active)
180                 return;
181
182         if (associated)
183                 ssid->setStyleSheet("color:palette(highlighted-text); font:bold;");
184         else
185                 ssid->setStyleSheet("color:palette(highlighted-text);");
186         emit hovered();
187 }
188
189 void DhcpcdSsidMenuWidget::leaveEvent(QEvent *)
190 {
191
192         if (!active)
193                 return;
194
195         if (associated)
196                 ssid->setStyleSheet("color:palette(text); font:bold;");
197         else
198                 ssid->setStyleSheet("color:palette(text);");
199 }