1 |
package mgaapplet_gui; |
2 |
|
3 |
################################################################################ |
4 |
# Mandriva Online # |
5 |
# # |
6 |
# Copyright (C) 2003-2010 Mandriva # |
7 |
# # |
8 |
# Daouda Lo # |
9 |
# Thierry Vignaud <tvignaud at mandriva dot com> # |
10 |
# # |
11 |
# This program is free software; you can redistribute it and/or modify # |
12 |
# it under the terms of the GNU General Public License Version 2 as # |
13 |
# published by the Free Software Foundation. # |
14 |
# # |
15 |
# This program is distributed in the hope that it will be useful, # |
16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
18 |
# GNU General Public License for more details. # |
19 |
# # |
20 |
# You should have received a copy of the GNU General Public License # |
21 |
# along with this program; if not, write to the Free Software # |
22 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # |
23 |
################################################################################ |
24 |
|
25 |
use strict; |
26 |
use feature 'state'; |
27 |
use lib qw(/usr/lib/libDrakX); |
28 |
use common; |
29 |
|
30 |
our @ISA = qw(Exporter); |
31 |
our @EXPORT = qw( |
32 |
@common |
33 |
%local_config |
34 |
$localdir |
35 |
$localfile |
36 |
$width |
37 |
fill_n_run_portable_dialog |
38 |
iso8601_date_to_locale |
39 |
new_link_button |
40 |
new_portable_dialog |
41 |
setVar |
42 |
); |
43 |
|
44 |
our @EXPORT_OK = qw( |
45 |
$powerpack_ad |
46 |
run_ask_credentials_dialog |
47 |
run_no_rights_dialog |
48 |
open_ask_powerpack_dialog |
49 |
); |
50 |
|
51 |
use mygtk2 qw(gtknew); #- do not import gtkadd which conflicts with ugtk2 version |
52 |
use ugtk2 qw(:all); |
53 |
use mgaonline qw(); # you don't want to polute the namespace |
54 |
use interactive; |
55 |
use interactive::gtk; |
56 |
use lib qw(/usr/lib/libDrakX/drakfirsttime); |
57 |
|
58 |
ugtk2::add_icon_path("/usr/share/mgaonline/pixmaps/"); |
59 |
|
60 |
our $localdir = "$ENV{HOME}/.MgaOnline"; |
61 |
our $localfile = "$localdir/mgaonline"; |
62 |
|
63 |
#compatibility |
64 |
mkdir_p($localdir) if !-d $localdir; |
65 |
-e "$ENV{HOME}/.mgaonline" and system("mv", "$ENV{HOME}/.mgaonline", $localfile); |
66 |
|
67 |
# make it work on 2008.X: |
68 |
eval { interactive::gtk::add_padding(Gtk2::Label->new) }; |
69 |
if ($@) { |
70 |
*interactive::gtk::add_padding = sub { $_[0] }; |
71 |
} |
72 |
|
73 |
our %local_config; |
74 |
read_local_config(); |
75 |
|
76 |
our $width = 500; |
77 |
our @common = ( |
78 |
# explicitely wrap (for 2008.1): |
79 |
line_wrap => 1, |
80 |
# workaround infamous 6 years old gnome bug #101968: |
81 |
width => $width - 50, |
82 |
); |
83 |
|
84 |
# List of widgets advertising Powerpack |
85 |
our $powerpack_ad = [ |
86 |
gtknew('Label_Left', |
87 |
text => N("Mageia Powerpack brings you the best of Linux experience for desktop: stability and efficiency of open source solutions together with exclusive softwares and Mageia official support."), |
88 |
@common), |
89 |
gtknew('HButtonBox', |
90 |
layout => 'center', |
91 |
children_tight => [ |
92 |
new_link_button( |
93 |
'http://www.mageia.org/', # FIXME: URL! |
94 |
N("Mageia Linux Features") |
95 |
) |
96 |
]), |
97 |
gtknew('Label_Left', |
98 |
text => 'You can order now access for Powerpack', |
99 |
), |
100 |
gtknew('HButtonBox', |
101 |
layout => 'center', |
102 |
children_tight => [ |
103 |
new_link_button( |
104 |
'http://www.mageia.org/', # FIXME: URL! |
105 |
N("Online subscription") |
106 |
) |
107 |
]), |
108 |
]; |
109 |
|
110 |
sub new_portable_dialog { |
111 |
my ($title) = @_; |
112 |
ugtk2->new($title, width => $width + 20); |
113 |
} |
114 |
|
115 |
sub fill_n_run_portable_dialog { |
116 |
my ($w, $widgets) = @_; |
117 |
|
118 |
# use wizard button order (for both 2008.1 & 2009.0): |
119 |
{ |
120 |
local $::isWizard = 1; |
121 |
local $w->{pop_it} = 0; |
122 |
local $::isInstall = 1; |
123 |
my %children; |
124 |
if ($::isEmbedded) { |
125 |
my (@children_tight, $child); |
126 |
@children_tight = @$widgets; |
127 |
$child = pop @children_tight; |
128 |
%children = ( |
129 |
children => [ |
130 |
(map { (0, $_) } @children_tight), |
131 |
1, gtknew('Label'), |
132 |
0, $child, |
133 |
] |
134 |
); |
135 |
} else { |
136 |
%children = (children_tight => $widgets); |
137 |
} |
138 |
|
139 |
gtkadd($w->{window}, gtknew('VBox', %children)); |
140 |
} |
141 |
|
142 |
$w->{ok}->grab_focus; |
143 |
$w->main; |
144 |
} |
145 |
|
146 |
sub new_link_button { |
147 |
my ($url, $text) = @_; |
148 |
my $link = Gtk2::LinkButton->new($url, $text); |
149 |
$link->set_uri_hook(sub { |
150 |
my (undef, $url) = @_; |
151 |
run_program::raw({ detach => 1, setuid => get_parent_uid() }, 'www-browser', $url); |
152 |
}); |
153 |
$link; |
154 |
} |
155 |
|
156 |
sub read_local_config() { |
157 |
%local_config = getVarsFromSh($localfile); |
158 |
} |
159 |
|
160 |
sub setVar { |
161 |
my ($var, $st) = @_; |
162 |
my %s = getVarsFromSh($localfile); |
163 |
$s{$var} = $st; |
164 |
setVarsInSh($localfile, \%s); |
165 |
read_local_config(); |
166 |
} |
167 |
|
168 |
sub iso8601_date_to_locale { |
169 |
my ($date) = @_; |
170 |
return $date if $date !~ /(\d\d\d\d)-?(\d\d)-?(\d\d)/; |
171 |
require POSIX; |
172 |
POSIX::strftime("%x", 0, 0, 0, $3, $2-1, $1-1900); |
173 |
} |
174 |
|
175 |
# %options keys: |
176 |
# |
177 |
# 'top_extra': reference to a list of widgets to shown on top of dialog. |
178 |
# |
179 |
sub run_ask_credentials_dialog { |
180 |
my ($title, $description, $callback, %options) = @_; |
181 |
|
182 |
my $w = new_portable_dialog($title); |
183 |
my $password_text; |
184 |
state $email_text; |
185 |
my $password_w = gtknew('Entry'); |
186 |
my $email_w = gtknew('Entry', text => $email_text); |
187 |
my $ok_clicked; |
188 |
|
189 |
$password_w->set_visibility(0); |
190 |
|
191 |
$w->{ok_clicked} = sub { |
192 |
$password_text = $password_w->get_text; |
193 |
$email_text = $email_w->get_text; |
194 |
$ok_clicked = 1; |
195 |
Gtk2->main_quit; |
196 |
}; |
197 |
|
198 |
my @widgets = ( |
199 |
if_(!$::isEmbedded, mgaonline::get_banner($title)), |
200 |
if_($options{top_extra}, |
201 |
@{ $options{top_extra} }, |
202 |
gtknew('HSeparator'), |
203 |
), |
204 |
gtknew('Label_Left', |
205 |
text => $description, |
206 |
@common), |
207 |
gtknew('HButtonBox', |
208 |
layout => 'start', |
209 |
children_tight => [ |
210 |
interactive::gtk::add_padding( |
211 |
new_link_button( |
212 |
'http://www.mageia.org/', # FIXME: URL! |
213 |
N("More information on your user account") |
214 |
) |
215 |
) |
216 |
]), |
217 |
gtknew('Table', |
218 |
col_spacings => 5, |
219 |
row_spacings => 5, |
220 |
children => [ [ N("Your email"), $email_w ], |
221 |
[ N("Your password"), $password_w ] ]), |
222 |
gtknew('HButtonBox', |
223 |
layout => 'start', |
224 |
children_tight => [ |
225 |
interactive::gtk::add_padding( |
226 |
new_link_button( |
227 |
'http://www.mageia.org/', # FIXME: URL! |
228 |
N("Forgotten password") |
229 |
) |
230 |
) |
231 |
]), |
232 |
ugtk2::create_okcancel($w, N("Next"), N("Cancel")), |
233 |
); |
234 |
|
235 |
fill_n_run_portable_dialog($w, \@widgets); |
236 |
|
237 |
if ($ok_clicked) { |
238 |
$ok_clicked = 0; |
239 |
if ($email_text && $password_text) { |
240 |
$callback->($email_text, $password_text); |
241 |
} |
242 |
else { |
243 |
interactive->vnew->ask_warn( |
244 |
N("Error"), |
245 |
N("Password and email cannot be empty.") |
246 |
); |
247 |
goto &run_ask_credentials_dialog; |
248 |
} |
249 |
} |
250 |
} |
251 |
|
252 |
sub run_no_rights_dialog { |
253 |
my ($title, $info, $info_url) = @_; |
254 |
my $w = new_portable_dialog($title); |
255 |
my @widgets = ( |
256 |
mgaonline::get_banner($title), |
257 |
gtknew('Label_Left', |
258 |
text => $info, |
259 |
@mgaapplet_gui::common), |
260 |
gtknew('HButtonBox', |
261 |
layout => 'start', |
262 |
children_tight => [ |
263 |
interactive::gtk::add_padding( |
264 |
new_link_button($info_url, N("More Information")) |
265 |
) |
266 |
]), |
267 |
create_okcancel($w, N("Close"), undef) |
268 |
); |
269 |
fill_n_run_portable_dialog($w, \@widgets); |
270 |
} |
271 |
|
272 |
# Returns a string of user's choice: 'powerpack' or 'free'. |
273 |
sub open_ask_powerpack_dialog { |
274 |
my ($current_product, $new_version) = @_; |
275 |
|
276 |
# Setup powerpack offering radio buttons... |
277 |
|
278 |
my @radio_widgets; |
279 |
my $rbutton; |
280 |
# pwp/flash users will be offered powerpack by default |
281 |
my $want_powerpack = $current_product =~ /powerpack|flash/i; |
282 |
foreach my $product ($want_powerpack |
283 |
? ('powerpack', 'free') : ('free', 'powerpack')) { |
284 |
my $info = mgaonline::get_product_info($product); |
285 |
$rbutton |
286 |
= Gtk2::RadioButton->new_with_label($rbutton |
287 |
? $rbutton->get_group |
288 |
: undef, |
289 |
$info->{name}); |
290 |
$rbutton->signal_connect('toggled', |
291 |
sub { |
292 |
my ($button, $is_pwp) = @_; |
293 |
$want_powerpack = $is_pwp |
294 |
if $button->get_active; |
295 |
}, |
296 |
$product eq 'powerpack'); |
297 |
push @radio_widgets, [ $rbutton, $info->{description} ]; |
298 |
} |
299 |
|
300 |
# Setup dialog widgets... |
301 |
|
302 |
my $title = N("Choose your upgrade version"); |
303 |
my $w = new_portable_dialog($title); |
304 |
my @widgets |
305 |
= (mgaonline::get_banner($current_product =~ /powerpack/i |
306 |
? N("Your Powerpack access has ended") |
307 |
: $title), |
308 |
gtknew('Label_Left', |
309 |
text => N("%s is now available, you can upgrade to:", |
310 |
$new_version), |
311 |
@common), |
312 |
gtknew('Table', children => \@radio_widgets, row_spacings => 10), |
313 |
ugtk2::create_okcancel($w, N("Next"), N("Cancel")), |
314 |
); |
315 |
|
316 |
fill_n_run_portable_dialog($w, \@widgets) or return undef; |
317 |
return $want_powerpack; |
318 |
} |