user.scm 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. (define-module (gnu installer newt user)
  19. #:use-module (gnu installer user)
  20. #:use-module (gnu installer newt page)
  21. #:use-module (gnu installer newt utils)
  22. #:use-module (guix i18n)
  23. #:use-module (newt)
  24. #:use-module (ice-9 match)
  25. #:use-module (ice-9 receive)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-26)
  28. #:export (run-user-page))
  29. (define (run-user-add-page)
  30. (define (pad-label label)
  31. (string-pad-right label 20))
  32. (let* ((label-name
  33. (make-label -1 -1 (pad-label (G_ "Name"))))
  34. (label-home-directory
  35. (make-label -1 -1 (pad-label (G_ "Home directory"))))
  36. (entry-width 30)
  37. (entry-name (make-entry -1 -1 entry-width))
  38. (entry-home-directory (make-entry -1 -1 entry-width))
  39. (entry-grid (make-grid 2 2))
  40. (button-grid (make-grid 1 1))
  41. (ok-button (make-button -1 -1 (G_ "OK")))
  42. (grid (make-grid 1 2))
  43. (title (G_ "User creation"))
  44. (set-entry-grid-field
  45. (cut set-grid-field entry-grid <> <> GRID-ELEMENT-COMPONENT <>))
  46. (form (make-form)))
  47. (set-entry-grid-field 0 0 label-name)
  48. (set-entry-grid-field 1 0 entry-name)
  49. (set-entry-grid-field 0 1 label-home-directory)
  50. (set-entry-grid-field 1 1 entry-home-directory)
  51. (set-grid-field button-grid 0 0 GRID-ELEMENT-COMPONENT ok-button)
  52. (add-component-callback
  53. entry-name
  54. (lambda (component)
  55. (set-entry-text entry-home-directory
  56. (string-append "/home/" (entry-value entry-name)))))
  57. (add-components-to-form form
  58. label-name label-home-directory
  59. entry-name entry-home-directory
  60. ok-button)
  61. (make-wrapped-grid-window (vertically-stacked-grid
  62. GRID-ELEMENT-SUBGRID entry-grid
  63. GRID-ELEMENT-SUBGRID button-grid)
  64. title)
  65. (let ((error-page
  66. (lambda ()
  67. (run-error-page (G_ "Empty inputs are not allowed.")
  68. (G_ "Empty input")))))
  69. (receive (exit-reason argument)
  70. (run-form form)
  71. (dynamic-wind
  72. (const #t)
  73. (lambda ()
  74. (when (eq? exit-reason 'exit-component)
  75. (cond
  76. ((components=? argument ok-button)
  77. (let ((name (entry-value entry-name))
  78. (home-directory (entry-value entry-home-directory)))
  79. (if (or (string=? name "")
  80. (string=? home-directory ""))
  81. (begin
  82. (error-page)
  83. (run-user-add-page))
  84. (user
  85. (name name)
  86. (home-directory home-directory))))))))
  87. (lambda ()
  88. (destroy-form-and-pop form)))))))
  89. (define (run-user-page)
  90. (define (run users)
  91. (let* ((listbox (make-listbox
  92. -1 -1 10
  93. (logior FLAG-SCROLL FLAG-BORDER)))
  94. (info-textbox
  95. (make-reflowed-textbox
  96. -1 -1
  97. (G_ "Please add at least one user to system\
  98. using the 'Add' button.")
  99. 40 #:flags FLAG-BORDER))
  100. (add-button (make-compact-button -1 -1 (G_ "Add")))
  101. (del-button (make-compact-button -1 -1 (G_ "Delete")))
  102. (listbox-button-grid
  103. (apply
  104. vertically-stacked-grid
  105. GRID-ELEMENT-COMPONENT add-button
  106. `(,@(if (null? users)
  107. '()
  108. (list GRID-ELEMENT-COMPONENT del-button)))))
  109. (ok-button (make-button -1 -1 (G_ "OK")))
  110. (exit-button (make-button -1 -1 (G_ "Exit")))
  111. (title "User creation")
  112. (grid
  113. (vertically-stacked-grid
  114. GRID-ELEMENT-COMPONENT info-textbox
  115. GRID-ELEMENT-SUBGRID (horizontal-stacked-grid
  116. GRID-ELEMENT-COMPONENT listbox
  117. GRID-ELEMENT-SUBGRID listbox-button-grid)
  118. GRID-ELEMENT-SUBGRID (horizontal-stacked-grid
  119. GRID-ELEMENT-COMPONENT ok-button
  120. GRID-ELEMENT-COMPONENT exit-button)))
  121. (sorted-users (sort users (lambda (a b)
  122. (string<= (user-name a)
  123. (user-name b)))))
  124. (listbox-elements
  125. (map
  126. (lambda (user)
  127. `((key . ,(append-entry-to-listbox listbox
  128. (user-name user)))
  129. (user . ,user)))
  130. sorted-users))
  131. (form (make-form)))
  132. (add-form-to-grid grid form #t)
  133. (make-wrapped-grid-window grid title)
  134. (if (null? users)
  135. (set-current-component form add-button)
  136. (set-current-component form ok-button))
  137. (receive (exit-reason argument)
  138. (run-form form)
  139. (dynamic-wind
  140. (const #t)
  141. (lambda ()
  142. (when (eq? exit-reason 'exit-component)
  143. (cond
  144. ((components=? argument add-button)
  145. (run (cons (run-user-add-page) users)))
  146. ((components=? argument del-button)
  147. (let* ((current-user-key (current-listbox-entry listbox))
  148. (users
  149. (map (cut assoc-ref <> 'user)
  150. (remove (lambda (element)
  151. (equal? (assoc-ref element 'key)
  152. current-user-key))
  153. listbox-elements))))
  154. (run users)))
  155. ((components=? argument ok-button)
  156. (when (null? users)
  157. (run-error-page (G_ "Please create at least one user.")
  158. (G_ "No user"))
  159. (run users))
  160. users))))
  161. (lambda ()
  162. (destroy-form-and-pop form))))))
  163. (run '()))