Dialog class
| Added on 2009-02-04 19:15:46 |
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
########################################################################## # This file is part of AmarokNowPlaying pour Facebook - Show what you # # are listening to on your Facebook profile. # # Copyright (C) 2008 Jany Belluz <jany.belluz@hotmail.fr> # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see <http://www.gnu.org/licenses/>. # ########################################################################## # Dialog library (uses `kdialog` and/or `zenity`) # ########################################################################## # Examples: # To show a text information dialog, use: # Dialog.message("My title", "My text") class Dialog # Was this class initialized? # La classe est-elle prête ? @@ready = false # Which programs are available? # Quels programmes sont disponibles ? @@available = [] # Which program is used? (determined after automated initialization) # Quel programme est utilisé ? (choisi après l'initialisation de la classe) @@program = nil # What to put before titles # Préfixe des titres @@pre_title = 'Dialog -' # use Dialog.pre_title = 'pouet' # Debug mode @@debug = false # Constants for return codes # Constantes de codes de retour OK_YES_CONTINUE = 0 NO_CANCEL_CLOSE = 1 ERROR = 2 ZENITY_TIMED_OUT = 3 # Last return code, filled on each call to a function # Dernier code de retour, déterminé à chaque appel de fonction @@last_return_code = ERROR # use Dialog.last_return_code # Last output, filled on each call to a function # Dernière information renvoyée, déterminée à chaque appel de fonction @@last_out = '' attr_reader :last_out def Dialog.init # External Dependencies # TODO Redirect output if system("kdialog", "--version") @@program = 'kdialog' @@available.to_a.push 'kdialog' puts '"kdialog" found' if @@debug end if system("zenity", "--version") @@program = 'zenity' if @@program == nil @@available.to_a.push 'zenity' puts '"zenity" found' if @@debug end puts @@program.inspect if @@debug puts @@available.inspect if @@debug @@ready = true end def Dialog.set_prefered( prefered_prog = 'kdialog' ) Dialog.init unless @@ready if @@available.to_a.include? prefered_prog.to_s @@program = prefered_prog.to_s end end def Dialog.pre_title= (pre_title) @@pre_title = pre_title.to_s.chomp @@pre_title['"'] = '\"' if @@pre_title.include? '"' end def Dialog.pre_title @@pre_title end def Dialog.last_return_code @@last_return_code end # Type must must be one of 'info', 'warning', 'error', 'question' # Returns nothing def Dialog.message( message, title = '', type = 'info' ) Dialog.init unless @@ready title = Dialog.title_ize(title) message = Dialog.sanitize(message) type = type.to_s type = 'info' unless [ 'info', 'warning', 'error', 'question' ].include?(type) if @@program == 'kdialog' type = 'msgbox' if type == 'info' type = 'sorry' if type == 'warning' type = 'yesno' if type == 'question' @@last_out = `kdialog --title "#{ title }" --#{ type } "#{ message }"`.to_s.chomp @@last_return_code = Dialog.from_kdialog_return_code($?.exitstatus) elsif @@program == 'zenity' @@last_out = `zenity --title="#{ title }" --#{ type } --text="#{ message }"`.to_s.chomp @@last_return_code = Dialog.from_zenity_return_code($?.exitstatus) end end # 'hide_input' must be true or false # Returns the entered string def Dialog.input( message, title = '', default = '', hide_input = false ) Dialog.init unless @@ready title = Dialog.title_ize(title) message = Dialog.sanitize(message) default = Dialog.sanitize(default) if @@program == 'kdialog' type = (hide_input ? 'password' : 'inputbox') @@last_out = `kdialog --title "#{ title }" --#{ type } "#{ message }" "#{ default }"`.to_s.chomp @@last_return_code = Dialog.from_kdialog_return_code($?.exitstatus) elsif @@program == 'zenity' @@last_out = `zenity --entry --title="#{ title }" --text="#{ message }" --entry-text="#{ default }" #{ hide_input ? '--hide-text' : '' }`.to_s.chomp @@last_return_code = Dialog.from_zenity_return_code($?.exitstatus) end return @@last_out end # Entries must be an array, it returns the selected element(s) # Type must be one of 'radio' (= one selection), 'check' (= many) # Selected must be one element or an array of elements from entries, wich # will be pre-selected. # Returns an array of chosen elements, even if only one was chosen. def Dialog.chooser( message, entries, title = '', type = 'radio', selected = [] ) Dialog.init unless @@ready @@last_out = [] # Only one entry, why to ask the user for his choice ? if ! entries.respond_to? 'each_index' @@last_out = [ entries.to_s ] return @@last_out end title = Dialog.title_ize(title) message = Dialog.sanitize(message) type = type.to_s type = 'radio' unless [ 'radio', 'check' ].include?(type) entries_list = '' if @@program == 'kdialog' if selected.respond_to? 'each_index' # 'selected' is supposed to be an array of strings entries.each_index do |i| entries_list += "#{ i } \"#{ Dialog.sanitize(entries[i]) }\" " + ( selected.include?(entries[i].to_s) ? 'on ' : 'off ' ).to_s end else entries.each_index do |i| entries_list += "#{ i } \"#{ Dialog.sanitize(entries[i]) }\" " + ( entries[i].to_s == selected.to_s ? 'on ' : 'off ' ).to_s end end puts indexes = `kdialog --title "#{ title }" --#{ type }list "#{ message }" #{ entries_list }`.chomp.to_s.split('" "') indexes.each { |i| @@last_out.push entries[i.to_i].to_s } @@last_return_code = Dialog.from_kdialog_return_code($?.exitstatus) # `exit #{ @@last_return_code }` # Sets $?.exitstatus to this class standard ones FIXME elsif @@program == 'zenity' if selected.respond_to? 'each_index' # 'selected' is supposed to be an array of strings entries.each_index do |i| entries_list += ( selected.include?(entries[i].to_s) ? 'TRUE ' : 'FALSE ' ).to_s + "\"#{ Dialog.sanitize(entries[i]) }\" " end else entries.each_index do |i| entries_list += ( entries[i].to_s == selected.to_s ? 'TRUE ' : 'FALSE ' ).to_s + "\"#{ Dialog.sanitize(entries[i]) }\" " end end @@last_out = `zenity --list --#{ type }list --title="#{ title }" --text="#{ message }" --column="" --column="" #{ entries_list }`.chomp.split('|') @@last_return_code = Dialog.from_zenity_return_code($?.exitstatus) end return @@last_out end private # Utilities def Dialog.from_kdialog_return_code( code ) code = code.to_i if code == 0 return OK_YES_CONTINUE elsif code == 1 || code == 2 return NO_CANCEL_CLOSE else return ERROR end end def Dialog.from_zenity_return_code( code ) code = code.to_i if code == 0 return OK_YES_CONTINUE elsif code == 1 return NO_CANCEL_CLOSE elsif code == 5 return ZENITY_TIMED_OUT else return ERROR end end def Dialog.title_ize( title ) return Dialog.sanitize( @@pre_title.to_s + ((' ' + title.to_s + '.') unless title.to_s.empty?).to_s ) end def Dialog.sanitize( text ) text = text.to_s text['"'] = '\"' if text.include? '"' return text end end # Tests # Dialog.set_prefered( 'kdialog' ) # # puts Dialog.chooser("Pouet", [ 'banane', 'pomme', 'poire' ], 'Test', 'check', [ 'banane', 'pomme' ]).inspect # puts Dialog.chooser("Pouet", [ 'banane', 'pomme', 'poire' ], 'Test', 'radio', [ 'banane', 'pomme' ]).inspect # puts Dialog.chooser("Pouet", [ 'banane', 'pomme', 'poire' ], 'Test', 'radio').inspect # puts Dialog.chooser("Pouet", [ 'banane', 'pomme', 'poire' ], 'Test').inspect # puts Dialog.chooser("Pouet", [ 1, 'pomme', :Figue ], 'Test').inspect # puts Dialog.message("Pouet", "Test").inspect # puts Dialog.message("Pouet", "Test", "warning").inspect # puts Dialog.message("Pouet ?", "Test", "question").inspect # puts Dialog.message("Pouet !!", "Test", "error").inspect # puts Dialog.input("Pouet !!", "Test II", true).inspect # # Dialog.set_prefered( 'zenity' ) # # puts Dialog.chooser("Pouet", [ 'banane', 'pomme', 'poire' ], 'Test', 'check', [ 'banane', 'pomme' ]).inspect # puts Dialog.chooser("Pouet", [ 'banane', 'pomme', 'poire' ], 'Test', 'radio', [ 'banane', 'pomme' ]).inspect # puts Dialog.chooser("Pouet", [ 'banane', 'pomme', 'poire' ], 'Test', 'radio').inspect # puts Dialog.chooser("Pouet", [ 'banane', 'pomme', 'poire' ], 'Test').inspect # puts Dialog.chooser("Pouet", [ 1, 'pomme', :Figue ], 'Test').inspect # puts Dialog.message("Pouet", "Test").inspect # puts Dialog.message("Pouet", "Test", "warning").inspect # puts Dialog.message("Pouet ?", "Test", "question").inspect # puts Dialog.message("Pouet !!", "Test", "error").inspect # puts Dialog.input("Pouet !!", "Test II", true).inspect # # puts 'Done.'

