installer: Add 'nss-certs' to the networking services.
[jackhill/guix/guix.git] / gnu / installer / newt / wifi.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (gnu installer newt wifi)
20 #:use-module (gnu installer connman)
21 #:use-module (gnu installer steps)
22 #:use-module (gnu installer newt utils)
23 #:use-module (gnu installer newt page)
24 #:use-module (guix i18n)
25 #:use-module (guix records)
26 #:use-module (ice-9 format)
27 #:use-module (ice-9 popen)
28 #:use-module (ice-9 receive)
29 #:use-module (ice-9 regex)
30 #:use-module (ice-9 rdelim)
31 #:use-module (srfi srfi-1)
32 #:use-module (srfi srfi-34)
33 #:use-module (srfi srfi-35)
34 #:use-module (newt)
35 #:export (run-wifi-page))
36
37 ;; This record associates a connman service to its key the listbox.
38 (define-record-type* <service-item>
39 service-item make-service-item
40 service-item?
41 (service service-item-service) ; connman <service>
42 (key service-item-key)) ; newt listbox-key
43
44 (define (strength->string strength)
45 "Convert STRENGTH as an integer percentage into a text printable strength
46 bar using unicode characters. Taken from NetworkManager's
47 nmc_wifi_strength_bars."
48 (let ((quarter #\x2582)
49 (half #\x2584)
50 (three-quarter #\x2586)
51 (full #\x2588))
52 (cond
53 ((> strength 80)
54 ;; ▂▄▆█
55 (string quarter half three-quarter full))
56 ((> strength 55)
57 ;; ▂▄▆_
58 (string quarter half three-quarter #\_))
59 ((> strength 30)
60 ;; ▂▄__
61 (string quarter half #\_ #\_))
62 ((> strength 5)
63 ;; ▂___
64 (string quarter #\_ #\_ #\_))
65 (else
66 ;; ____
67 (string quarter #\_ #\_ #\_ #\_)))))
68
69 (define (force-wifi-scan)
70 "Force a wifi scan. Raise a condition if no wifi technology is available."
71 (let* ((technologies (connman-technologies))
72 (wifi-technology
73 (find (lambda (technology)
74 (string=? (technology-type technology) "wifi"))
75 technologies)))
76 (if wifi-technology
77 (connman-scan-technology wifi-technology)
78 (raise (condition
79 (&message
80 (message (G_ "Unable to find a wifi technology"))))))))
81
82 (define (draw-scanning-page)
83 "Draw a page to indicate a wifi scan in in progress."
84 (draw-info-page (G_ "Scanning wifi for available networks, please wait.")
85 (G_ "Scan in progress")))
86
87 (define (run-wifi-password-page)
88 "Run a page prompting user for a password and return it."
89 (run-input-page (G_ "Please enter the wifi password.")
90 (G_ "Password required")))
91
92 (define (run-wrong-password-page service-name)
93 "Run a page to inform user of a wrong password input."
94 (run-error-page
95 (format #f (G_ "The password you entered for ~a is incorrect.")
96 service-name)
97 (G_ "Wrong password")))
98
99 (define (run-unknown-error-page service-name)
100 "Run a page to inform user that a connection error happened."
101 (run-error-page
102 (format #f
103 (G_ "An error occurred while trying to connect to ~a, please retry.")
104 service-name)
105 (G_ "Connection error")))
106
107 (define (password-callback)
108 (run-wifi-password-page))
109
110 (define (connect-wifi-service listbox service-items)
111 "Connect to the wifi service selected in LISTBOX. SERVICE-ITEMS is the list
112 of <service-item> records present in LISTBOX."
113 (let* ((listbox-key (current-listbox-entry listbox))
114 (item (find (lambda (item)
115 (eq? (service-item-key item) listbox-key))
116 service-items))
117 (service (service-item-service item))
118 (service-name (service-name service))
119 (form (draw-connecting-page service-name)))
120 (dynamic-wind
121 (const #t)
122 (lambda ()
123 (guard (c ((connman-password-error? c)
124 (run-wrong-password-page service-name)
125 #f)
126 ((connman-already-connected-error? c)
127 #t)
128 ((connman-connection-error? c)
129 (run-unknown-error-page service-name)
130 #f))
131 (connman-connect-with-auth service password-callback)))
132 (lambda ()
133 (destroy-form-and-pop form)))))
134
135 (define (run-wifi-scan-page)
136 "Force a wifi scan and draw a page during the operation."
137 (let ((form (draw-scanning-page)))
138 (force-wifi-scan)
139 (destroy-form-and-pop form)))
140
141 (define (wifi-services)
142 "Return all the connman services of wifi type."
143 (let ((services (connman-services)))
144 (filter (lambda (service)
145 (and (string=? (service-type service) "wifi")
146 (not (string-null? (service-name service)))))
147 services)))
148
149 (define* (fill-wifi-services listbox wifi-services)
150 "Append all the services in WIFI-SERVICES to the given LISTBOX."
151 (clear-listbox listbox)
152 (map (lambda (service)
153 (let* ((text (service->text service))
154 (key (append-entry-to-listbox listbox text)))
155 (service-item
156 (service service)
157 (key key))))
158 wifi-services))
159
160 ;; Maximum length of a wifi service name.
161 (define service-name-max-length (make-parameter 20))
162
163 ;; Height of the listbox displaying wifi services.
164 (define wifi-listbox-height (make-parameter 20))
165
166 ;; Information textbox width.
167 (define info-textbox-width (make-parameter 40))
168
169 (define (service->text service)
170 "Return a string composed of the name and the strength of the given
171 SERVICE. A '*' preceding the service name indicates that it is connected."
172 (let* ((name (service-name service))
173 (padded-name (string-pad-right name
174 (service-name-max-length)))
175 (strength (service-strength service))
176 (strength-string (strength->string strength))
177 (state (service-state service))
178 (connected? (or (string=? state "online")
179 (string=? state "ready"))))
180 (format #f "~c ~a ~a~%"
181 (if connected? #\* #\ )
182 padded-name
183 strength-string)))
184
185 (define (run-wifi-page)
186 "Run a page displaying available wifi networks in a listbox. Connect to the
187 network when the corresponding listbox entry is selected. A button allow to
188 force a wifi scan."
189 (let* ((listbox (make-listbox
190 -1 -1
191 (wifi-listbox-height)
192 (logior FLAG-SCROLL FLAG-BORDER FLAG-RETURNEXIT)))
193 (form (make-form))
194 (buttons-grid (make-grid 1 1))
195 (middle-grid (make-grid 2 1))
196 (info-text (G_ "Please select a wifi network."))
197 (info-textbox
198 (make-reflowed-textbox -1 -1 info-text
199 (info-textbox-width)
200 #:flags FLAG-BORDER))
201 (exit-button (make-button -1 -1 (G_ "Exit")))
202 (scan-button (make-button -1 -1 (G_ "Scan")))
203 (services (wifi-services))
204 (service-items '()))
205
206 (if (null? services)
207 (append-entry-to-listbox listbox (G_ "No wifi detected"))
208 (set! service-items (fill-wifi-services listbox services)))
209
210 (set-grid-field middle-grid 0 0 GRID-ELEMENT-COMPONENT listbox)
211 (set-grid-field middle-grid 1 0 GRID-ELEMENT-COMPONENT scan-button
212 #:anchor ANCHOR-TOP
213 #:pad-left 2)
214 (set-grid-field buttons-grid 0 0 GRID-ELEMENT-COMPONENT exit-button)
215
216 (add-components-to-form form
217 info-textbox
218 listbox scan-button
219 exit-button)
220 (make-wrapped-grid-window
221 (basic-window-grid info-textbox middle-grid buttons-grid)
222 (G_ "Wifi"))
223
224 (receive (exit-reason argument)
225 (run-form form)
226 (dynamic-wind
227 (const #t)
228 (lambda ()
229 (when (eq? exit-reason 'exit-component)
230 (cond
231 ((components=? argument scan-button)
232 (run-wifi-scan-page)
233 (run-wifi-page))
234 ((components=? argument exit-button)
235 (raise
236 (condition
237 (&installer-step-abort))))
238 ((components=? argument listbox)
239 (let ((result (connect-wifi-service listbox service-items)))
240 (unless result
241 (run-wifi-page)))))))
242 (lambda ()
243 (destroy-form-and-pop form))))))