dialog.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * BURG - Brand-new Universal loadeR from GRUB
  3. * Copyright 2009 Bean Lee - All Rights Reserved
  4. *
  5. * BURG is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * BURG is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with BURG. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/mm.h>
  19. #include <grub/misc.h>
  20. #include <grub/widget.h>
  21. #include <grub/dialog.h>
  22. #include <grub/menu_data.h>
  23. #include <grub/auth.h>
  24. GRUB_EXPORT(grub_dialog_create);
  25. GRUB_EXPORT(grub_dialog_set_parm);
  26. GRUB_EXPORT(grub_dialog_get_parm);
  27. GRUB_EXPORT(grub_dialog_popup);
  28. GRUB_EXPORT(grub_dialog_update_screen);
  29. GRUB_EXPORT(grub_dialog_free);
  30. GRUB_EXPORT(grub_dialog_message);
  31. GRUB_EXPORT(grub_dialog_password);
  32. grub_uitree_t
  33. grub_dialog_create (const char *name, int copy, int index,
  34. grub_uitree_t *menu, grub_uitree_t *save)
  35. {
  36. grub_uitree_t node;
  37. grub_uitree_t parent;
  38. parent = grub_uitree_find (&grub_uitree_root, name);
  39. if (! parent)
  40. return 0;
  41. node = parent->child;
  42. while ((index) && (node))
  43. {
  44. index--;
  45. node = node->next;
  46. }
  47. if (! node)
  48. return 0;
  49. if (copy)
  50. node = grub_uitree_clone (node);
  51. else
  52. {
  53. *menu = parent;
  54. *save = parent->child;
  55. parent->child = node->next;
  56. }
  57. return node;
  58. }
  59. static grub_uitree_t
  60. find_prop (grub_uitree_t node, char *path, char **out)
  61. {
  62. while (1)
  63. {
  64. char *n;
  65. grub_uitree_t child;
  66. n = grub_menu_next_field (path, '.');
  67. if (! n)
  68. {
  69. *out = path;
  70. return node;
  71. }
  72. child = grub_uitree_find (node, path);
  73. node = (child) ? child : grub_uitree_find_id (node, path);
  74. grub_menu_restore_field (n, '.');
  75. if (! node)
  76. return 0;
  77. path = n;
  78. }
  79. }
  80. static grub_uitree_t
  81. find_parm (grub_uitree_t node, char *parm, char *name,
  82. char **out, char **next)
  83. {
  84. *next = 0;
  85. *out = name;
  86. if (grub_strchr (name, '.'))
  87. node = find_prop (node, name, out);
  88. else if (parm)
  89. do
  90. {
  91. char *n, *v;
  92. n = grub_menu_next_field (parm, ':');
  93. v = grub_menu_next_field (parm, '=');
  94. if ((v) && (! grub_strcmp (parm, name)))
  95. {
  96. grub_menu_restore_field (v, '=');
  97. *next = n;
  98. return find_prop (node, v, out);
  99. }
  100. grub_menu_restore_field (v, '=');
  101. grub_menu_restore_field (n, ':');
  102. parm = n;
  103. } while (parm);
  104. return node;
  105. }
  106. int
  107. grub_dialog_set_parm (grub_uitree_t node, char *parm,
  108. char *name, const char *value)
  109. {
  110. char *out, *next;
  111. int result;
  112. node = find_parm (node, parm, name, &out, &next);
  113. result = (node) ? (grub_uitree_set_prop (node, out, value) == 0) : 0;
  114. grub_menu_restore_field (next, ':');
  115. return result;
  116. }
  117. char *
  118. grub_dialog_get_parm (grub_uitree_t node, char *parm, char *name)
  119. {
  120. char *out, *next, *result;
  121. node = find_parm (node, parm, name, &out, &next);
  122. result = (node) ? grub_uitree_get_prop (node, out) : 0;
  123. grub_menu_restore_field (next, ':');
  124. return result;
  125. }
  126. void
  127. grub_dialog_update_screen (grub_uitree_t node)
  128. {
  129. grub_uitree_t root, child;
  130. grub_widget_t widget;
  131. grub_menu_region_update_list_t head;
  132. widget = node->data;
  133. root = node->parent;
  134. head = 0;
  135. ((grub_widget_t) root->data)->class->draw (root->data, &head,
  136. widget->org_x, widget->org_y,
  137. widget->width, widget->height);
  138. for (child = root->child; ((child) && (child != node)); child = child->next)
  139. {
  140. int x, y, width, height;
  141. grub_widget_t c;
  142. c = child->data;
  143. if (! c)
  144. continue;
  145. x = widget->org_x - c->org_x;
  146. y = widget->org_y - c->org_y;
  147. width = widget->width;
  148. height = widget->height;
  149. if (grub_menu_region_check_rect (&x, &y, &width, &height, 0, 0,
  150. c->width, c->height))
  151. grub_widget_draw_region (&head, child, x, y, width, height);
  152. }
  153. grub_menu_region_apply_update (head);
  154. }
  155. grub_err_t
  156. grub_dialog_popup (grub_uitree_t node)
  157. {
  158. grub_err_t r;
  159. grub_uitree_t save;
  160. grub_widget_create (node);
  161. grub_widget_init (node);
  162. node->flags |= GRUB_WIDGET_FLAG_FIXED_XY;
  163. save = grub_widget_current_node;
  164. r = grub_widget_input (node, 1);
  165. grub_widget_current_node = save;
  166. if (! grub_widget_refresh)
  167. grub_dialog_update_screen (node);
  168. grub_widget_free (node);
  169. return r;
  170. }
  171. void
  172. grub_dialog_free (grub_uitree_t node, grub_uitree_t menu, grub_uitree_t save)
  173. {
  174. grub_tree_remove_node (GRUB_AS_TREE (node));
  175. if (save)
  176. {
  177. node->next = menu->child;
  178. node->parent = menu;
  179. menu->child = save;
  180. }
  181. else
  182. grub_uitree_free (node);
  183. }
  184. static grub_uitree_t
  185. create_dialog (const char *name)
  186. {
  187. grub_uitree_t node;
  188. if (! grub_widget_screen)
  189. return 0;
  190. node = grub_dialog_create (name, 1, 0, 0, 0);
  191. if (! node)
  192. return 0;
  193. grub_tree_add_child (GRUB_AS_TREE (grub_widget_screen),
  194. GRUB_AS_TREE (node), -1);
  195. return node;
  196. }
  197. void
  198. grub_dialog_message (const char *text)
  199. {
  200. grub_uitree_t node;
  201. node = create_dialog ("dialog_message");
  202. if (! node)
  203. return;
  204. if (text)
  205. {
  206. char *parm;
  207. parm = grub_uitree_get_prop (node, "parameters");
  208. grub_dialog_set_parm (node, parm, "text", text);
  209. }
  210. grub_dialog_popup (node);
  211. grub_dialog_free (node, 0, 0);
  212. }
  213. int
  214. grub_dialog_password (const char *userlist)
  215. {
  216. grub_uitree_t node;
  217. int result = 0;
  218. if (grub_auth_check_password (userlist, 0, 0))
  219. return 1;
  220. node = create_dialog ("dialog_password");
  221. if (! node)
  222. return 0;
  223. grub_errno = 0;
  224. if (grub_dialog_popup (node) == 0)
  225. {
  226. char *parm, *user;
  227. char pass[GRUB_AUTH_MAX_PASSLEN];
  228. parm = grub_uitree_get_prop (node, "parameters");
  229. user = grub_dialog_get_parm (node, parm, "username");
  230. if (user)
  231. {
  232. char *p;
  233. grub_memset (pass, 0, sizeof (pass));
  234. p = grub_dialog_get_parm (node, parm, "password");
  235. grub_strcpy (pass, p);
  236. grub_memset (p, 0, grub_strlen (p));
  237. result = (grub_auth_check_password (userlist, user, pass));
  238. grub_memset (pass, 0, sizeof (pass));
  239. }
  240. if (! result)
  241. grub_dialog_message ("Access denied.");
  242. }
  243. grub_dialog_free (node, 0, 0);
  244. return result;
  245. }