DialogManager.pm.in 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # BEGIN COPYRIGHT BLOCK
  2. # This Program is free software; you can redistribute it and/or modify it under
  3. # the terms of the GNU General Public License as published by the Free Software
  4. # Foundation; version 2 of the License.
  5. #
  6. # This Program is distributed in the hope that it will be useful, but WITHOUT
  7. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. #
  10. # You should have received a copy of the GNU General Public License along with
  11. # this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. # Place, Suite 330, Boston, MA 02111-1307 USA.
  13. #
  14. # In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. # right to link the code of this Program with code not covered under the GNU
  16. # General Public License ("Non-GPL Code") and to distribute linked combinations
  17. # including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. # permitted under this exception must only link to the code of this Program
  19. # through those well defined interfaces identified in the file named EXCEPTION
  20. # found in the source code files (the "Approved Interfaces"). The files of
  21. # Non-GPL Code may instantiate templates or use macros or inline functions from
  22. # the Approved Interfaces without causing the resulting work to be covered by
  23. # the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. # additions to the list of Approved Interfaces. You must obey the GNU General
  25. # Public License in all respects for all of the Program code and other code used
  26. # in conjunction with the Program except the Non-GPL Code covered by this
  27. # exception. If you modify this file, you may extend this exception to your
  28. # version of the file, but you are not obligated to do so. If you do not wish to
  29. # provide this exception without modification, you must delete this exception
  30. # statement from your version and license this file solely under the GPL without
  31. # exception.
  32. #
  33. #
  34. # Copyright (C) 2007 Red Hat, Inc.
  35. # All rights reserved.
  36. # END COPYRIGHT BLOCK
  37. #
  38. package DialogManager;
  39. use Exporter ();
  40. @ISA = qw(Exporter);
  41. @EXPORT = qw($BACK $SAME $NEXT $ERR);
  42. @EXPORT_OK = qw($BACK $SAME $NEXT $ERR);
  43. use Dialog;
  44. use SetupLog;
  45. # Dialog responses
  46. $FIRST = -2; # go back to first prompt on a dialog
  47. $BACK = -1; # go back to previous dialog
  48. $SAME = 0; # reshow the same prompt or dialog
  49. $NEXT = 1; # go to the next dialog
  50. $ERR = 2; # fatal error
  51. # The DialogManager controls the flow of the dialogs and contains context shared
  52. # among all of the dialogs (resources, logs, current setup type, etc.)
  53. # all of these are optional
  54. sub new {
  55. my $type = shift;
  56. my $self = {};
  57. $self->{setup} = shift;
  58. $self->{res} = shift;
  59. $self->{type} = shift;
  60. $self->{log} = $self->{setup}->{log};
  61. $self->{inf} = $self->{setup}->{inf};
  62. $self = bless $self, $type;
  63. return $self;
  64. }
  65. sub getType {
  66. my $self = shift;
  67. return $self->{type};
  68. }
  69. sub setType {
  70. my $self = shift;
  71. $self->{type} = shift;
  72. }
  73. sub addDialog {
  74. my $self = shift;
  75. for my $dialog (@_) {
  76. $dialog->setManager($self);
  77. push @{$self->{dialogs}}, $dialog;
  78. }
  79. }
  80. sub resetDialog {
  81. my $self = shift;
  82. @{$self->{dialogs}} = ();
  83. }
  84. # see if the user answered with the special BACK answer
  85. sub isBack {
  86. my $self = shift;
  87. my $ans = shift;
  88. if (!$ans) {
  89. return 0;
  90. }
  91. # the word "back"
  92. if ($ans =~ /^\s*back\s*$/i) {
  93. return 1;
  94. }
  95. # a Ctrl-B sequence
  96. if ($ans eq '') {
  97. return 1;
  98. }
  99. return 0;
  100. }
  101. sub log {
  102. my $self = shift;
  103. if (!$self->{log}) {
  104. print @_;
  105. } else {
  106. $self->{log}->logMessage($INFO, "Setup", @_);
  107. }
  108. }
  109. sub getText {
  110. my $self = shift;
  111. return $self->{res}->getText(@_);
  112. }
  113. sub handleError {
  114. my $self = shift;
  115. my $msg = $self->{res}->getText('setup_err_exit');
  116. $self->{log}->logMessage($FATAL, "Setup", $msg);
  117. }
  118. sub showText {
  119. my $self = shift;
  120. my $msg = shift;
  121. my $text = $self->getText($msg);
  122. print "\n", ("=" x 78), "\n";
  123. # display it,
  124. print $text;
  125. # log it
  126. $self->log($text);
  127. }
  128. sub showPrompt {
  129. my $self = shift;
  130. my $msg = shift;
  131. my $defaultans = shift;
  132. my $ispwd = shift;
  133. my $text = $self->getText($msg);
  134. # display it,
  135. print $text;
  136. # log it
  137. $self->log($text . "\n");
  138. # display the default answer
  139. if ($defaultans) {
  140. print " [$defaultans]";
  141. }
  142. print ": ";
  143. # if we are prompting for a password, disable console echo
  144. if ($ispwd) {
  145. system("@sttyexec@ -echo");
  146. }
  147. # read the answer
  148. my $ans = <STDIN>;
  149. # if we are prompting for a password, enable console echo
  150. if ($ispwd) {
  151. system("@sttyexec@ echo");
  152. print "\n";
  153. }
  154. chop($ans); # trim trailing newline
  155. # see if this is the special BACK response, and finish if so
  156. if ($self->isBack($ans)) {
  157. $self->log("BACK\n");
  158. return $ans;
  159. }
  160. if (!length($ans)) {
  161. $ans = $defaultans;
  162. }
  163. # log the response, if not a password
  164. if (!$ispwd) {
  165. $self->log($ans . "\n");
  166. }
  167. return $ans;
  168. }
  169. sub alert {
  170. my $self = shift;
  171. my $msg = $self->{res}->getText(@_);
  172. print $msg;
  173. $self->{log}->logMessage($WARN, "Setup", $msg);
  174. }
  175. sub run {
  176. my $self = shift;
  177. my $done;
  178. my $index = 0;
  179. my $incr = 1;
  180. my $rc = 0;
  181. while (!$done) {
  182. my $dialog = $self->{dialogs}->[$index];
  183. if ($dialog->isEnabled()) {
  184. my $resp = $NEXT;
  185. $resp = $dialog->run($incr);
  186. if ($resp == $BACK) {
  187. $incr = -1;
  188. } elsif ($resp == $NEXT) {
  189. $incr = 1;
  190. } elsif (($resp == $SAME) or ($resp == $FIRST)) {
  191. $incr = 0;
  192. } else {
  193. $self->handleError($resp);
  194. $done = 1;
  195. $rc = 1;
  196. }
  197. }
  198. $index += $incr;
  199. if ($index < 0) {
  200. $index = 0;
  201. } elsif ($index >= @{$self->{dialogs}}) {
  202. $done = 1;
  203. }
  204. }
  205. return $rc;
  206. }
  207. #############################################################################
  208. # Mandatory TRUE return value.
  209. #
  210. 1;