PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/Zenity.php

https://github.com/machour/php-gnome
PHP | 329 lines | 152 code | 24 blank | 153 comment | 29 complexity | 2106180b108b218829a25a14f2c8ca2c MD5 | raw file
  1. <?php
  2. /**
  3. * Zenity class definition file
  4. *
  5. * Mehdi Achour <machour@gmail.com>
  6. */
  7. /**
  8. * Use ZenityWrapper for execution
  9. */
  10. require_once 'ZenityWrapper.php';
  11. /**
  12. * Zenity - A simple Zenity PHP wrapper
  13. *
  14. * @todo Add helper methods for global options settings
  15. *
  16. * This class extends the ZenityWrapper class and implements a simplier interface
  17. * for the end user
  18. */
  19. class Zenity extends ZenityWrapper
  20. {
  21. /**
  22. * Displays a notification in the notification bar
  23. *
  24. * @param string $text Set the dialog text
  25. *
  26. * @todo Add support for --listen option
  27. */
  28. public function showNotification($text) {
  29. return parent::notification(array('text' => $text));
  30. }
  31. /**
  32. * Displays a confirmation dialog
  33. *
  34. * @param string $text Set the dialog text
  35. * @param boolean $nowrap Do not enable text wrapping in the dialog
  36. * @return Returns TRUE if the user clicked the Yes button, FALSE otherwise
  37. */
  38. public function question($text, $nowrap = false) {
  39. $args = array('text' => $text);
  40. if ($nowrap) {
  41. $args['nowrap'] = true;
  42. }
  43. return parent::question($args);
  44. }
  45. /**
  46. * Displays an information
  47. *
  48. * @param string $text Set the dialog text
  49. * @param boolean $nowrap Do not enable text wrapping in the dialog
  50. * @return boolean TRUE on success, FALSE on failure
  51. */
  52. public function info($text, $nowrap = false) {
  53. $args = array('text' => $text);
  54. if ($nowrap) {
  55. $args['nowrap'] = true;
  56. }
  57. return parent::info($args);
  58. }
  59. /**
  60. * Displays a warning
  61. *
  62. * @param string $text Set the dialog text
  63. * @param boolean $nowrap Do not enable text wrapping in the dialog
  64. * @return boolean TRUE on success, FALSE on failure
  65. */
  66. public function warning($text, $nowrap = false) {
  67. $args = array('text' => $text);
  68. if ($nowrap) {
  69. $args['nowrap'] = true;
  70. }
  71. return parent::warning($args);
  72. }
  73. /**
  74. * Displays an error
  75. *
  76. * @param string $text Set the dialog text
  77. * @param boolean $nowrap Do not enable text wrapping in the dialog
  78. * @return boolean TRUE on success, FALSE on failure
  79. */
  80. public function error($text, $nowrap = false) {
  81. $args = array('text' => $text);
  82. if ($nowrap) {
  83. $args['nowrap'] = true;
  84. }
  85. return parent::error($args);
  86. }
  87. /**
  88. * Displays a password entry dialog
  89. *
  90. * @param string $text Set the dialog text
  91. * @param string $default The default entry text
  92. * @return string The typed password
  93. */
  94. public function inputPassword($text, $default = '') {
  95. $args = array('text' => $text, 'hide-text' => true);
  96. if ($default) {
  97. $args['entry-text'] = $default;
  98. }
  99. return parent::entry($args);
  100. }
  101. /**
  102. * Displays a plain text entry dialog
  103. *
  104. * @param string $text Set the dialog text
  105. * @param string $default The default entry text
  106. * @return string The typed text
  107. */
  108. public function inputText($text, $default = '') {
  109. $args = array('text' => $text);
  110. if ($default) {
  111. $args['entry-text'] = $default;
  112. }
  113. return parent::entry($args);
  114. }
  115. /**
  116. * Displays a text info dialog
  117. *
  118. * @param boolean $editable Specify that the dialog contents are editable
  119. * @param string $filename Set the file to display in the dialog
  120. * @return string The dialog text
  121. */
  122. public function inputMultilineText($editable = false, $filename = false) {
  123. $args = array();
  124. if ($editable) {
  125. $args['editable'] = true;
  126. }
  127. if ($filename && is_file($filename) && is_readable($filename)) {
  128. $args['filename'] = $filename;
  129. }
  130. return parent::textInfo($args);
  131. }
  132. /**
  133. * @todo Finalize this
  134. */
  135. public function selectFiles($text) {
  136. return parent::fileSelection(array('text' => $text, 'multiple' => true));
  137. }
  138. /**
  139. * @todo Finalize this
  140. */
  141. public function selectFile($text) {
  142. return parent::fileSelection(array('text' => $text));
  143. }
  144. /**
  145. * @todo Finalize this
  146. */
  147. public function selectDirectories($text) {
  148. return parent::fileSelection(array('text' => $text, 'multiple' => true, 'directory' => true));
  149. }
  150. /**
  151. * @todo Finalize this
  152. */
  153. public function selectDirectory($text) {
  154. return parent::fileSelection(array('text' => $text, 'directory' => true));
  155. }
  156. /**
  157. * Displays a list dialog
  158. *
  159. * @todo Add support for --editable --hide-column -print-column
  160. * @param string $text Set the dialog text
  161. * @param array $fieldsNames An array of fields names
  162. * @param array $fieldsValues An array of fields values
  163. * @param boolean $multiple Allow multiple selections
  164. * @param array $defaults An array of default selection flags, one entry per row
  165. * @param string $extra_label The optional checkbox or radio column label
  166. * @param string $type One of 'checklist' or 'radiolist'
  167. * @return mixed A single or array of selected index(es) in $fieldsValues, or
  168. * FALSE if no rows were selected
  169. */
  170. public function selectList($text, $fieldsNames, $fieldsValues, $multiple = false, $defaults = array(), $extra_label = '', $type = '') {
  171. $args = array('text' => $text);
  172. // Add and hide the internal id column
  173. array_unshift($fieldsNames, '@@@PHPZENITYID@@@');
  174. if ($type == 'checklist' || $type == 'radiolist') {
  175. $args[$type] = true;
  176. array_unshift($fieldsNames, $extra_label);
  177. $args['hide-column'] = 2;
  178. } else {
  179. $args['hide-column'] = 1;
  180. }
  181. $args['column'] = $fieldsNames;
  182. $i = 0;
  183. foreach ($fieldsValues as &$field) {
  184. array_unshift($field, $i);
  185. if (!empty($defaults)) {
  186. if (isset($defaults[$i]) && $defaults[$i]) {
  187. array_unshift($field, 'TRUE');
  188. } else {
  189. array_unshift($field, 'FALSE');
  190. }
  191. }
  192. $i++;
  193. }
  194. $args['value'] = $fieldsValues;
  195. if ($multiple) {
  196. $args['multiple'] = true;
  197. }
  198. $args['separator'] = '@@@PHPZENITYSEP@@@';
  199. $ret = parent::doList($args);
  200. if ($multiple) {
  201. $ret = explode($args['separator'], $ret);
  202. array_walk($ret, 'intval');
  203. return $ret;
  204. } else {
  205. return (int) $ret;
  206. }
  207. }
  208. /**
  209. * Displays a radio list dialog
  210. *
  211. * @param string $text Set the dialog text
  212. * @param array $fieldsNames An array of fields names
  213. * @param array $fieldsValues An array of fields values
  214. * @param string $radioLabel The radio column label
  215. * @param array $defaults An array of default selection flags, one entry per row
  216. * @return mixed A single or array of selected index(es) in $fieldsValues, or
  217. * FALSE if no rows were selected
  218. */
  219. public function selectRadiolist($text, $fieldsNames, $fieldsValues, $radioLabel, $defaults = array()) {
  220. return $this->selectList($text, $fieldsNames, $fieldsValues, false, $defaults, $radioLabel, 'radiolist');
  221. }
  222. /**
  223. * Displays a list dialog
  224. *
  225. * @param string $text Set the dialog text
  226. * @param array $fieldsNames An array of fields names
  227. * @param array $fieldsValues An array of fields values
  228. * @param string $checkLabel The checkbox column label
  229. * @param boolean $multiple Allow multiple selections
  230. * @param array $defaults An array of default selection flags, one entry per row
  231. * @return mixed A single or array of selected index(es) in $fieldsValues, or
  232. * FALSE if no rows were selected
  233. */
  234. public function selectChecklist($text, $fieldsNames, $fieldsValues, $checkLabel, $multiple, $defaults = array()) {
  235. return $this->selectList($text, $fieldsNames, $fieldsValues, $multiple, $defaults, $checkLabel, 'checklist');
  236. }
  237. /**
  238. * Display a calendar dialog
  239. *
  240. * @param string $text Specifies the text that is displayed in the progress dialog
  241. * @param string $format The date format to be retrieved. See strftime() for syntax.
  242. * @param int $day Set the calendar day
  243. * @param int $month Set the calendar month
  244. * @param int $year Set the calendar year
  245. * @return string The selected date, formatted accordingly to $format
  246. */
  247. public function calendar($text, $format = "%d/%m/%Y", $day = false, $month = false, $year = false) {
  248. $args = array('text' => $text, 'date-format' => $format);
  249. if ($day !== false) {
  250. $args['day'] = $day;
  251. }
  252. if ($month !== false) {
  253. $args['month'] = $month;
  254. }
  255. if ($year !== false) {
  256. $args['year'] = $year;
  257. }
  258. return parent::calendar($args);
  259. }
  260. /**
  261. * Display a progress dialog
  262. *
  263. * @param string $text Specifies the text that is displayed in the progress dialog
  264. * @param int $initial Specifies the initial percentage that is set in the progress dialog
  265. * @param boolean $pulsate Specifies that the progress bar pulsates until an EOF character is read from standard input
  266. * @param boolean $autoclose Closes the progress dialog when 100% has been reached
  267. * @param boolean $input Specifies the path to the executable script that will send input to the Progress dialog
  268. * @param boolean $autokill Kill the parent process (the $input script execution) is the Cancel button is clicked
  269. */
  270. public function showProgress($text, $initial = 0, $pulsate = false, $autoclose = false, $input = false, $autokill = false) {
  271. $args = array('text' => $text);
  272. $args['percentage'] = $initial;
  273. if ($pulsate) {
  274. $args['pulsate'] = true;
  275. }
  276. if ($autokill) {
  277. $args['auto-kill'] = true;
  278. }
  279. if ($autoclose) {
  280. $args['auto-close'] = true;
  281. }
  282. return parent::progress($args, $input);
  283. }
  284. /**
  285. * Display a scale dialog
  286. *
  287. * @todo Add --print-partial and --hide-value
  288. * @param string $text Specifies the text that is displayed in the progress dialog
  289. * @param int $min Sets the minimum value of the scale
  290. * @param int $max Sets the maximum value of the scale
  291. * @param int $step Sets the step size of the scale
  292. * @param int $default Sets the initial value
  293. * @return int The selected value
  294. */
  295. public function scale($text, $min = 0, $max = 100, $step = 1, $default = false) {
  296. $args = array('text' => $text);
  297. $args['min-value'] = $min;
  298. $args['max-value'] = $max;
  299. $args['step'] = $step;
  300. if ($default !== false) {
  301. $args['value'] = $default;
  302. }
  303. return parent::scale($args);
  304. }
  305. }