From 65a27fd40e069cd1cdae0bc5b6b35670f37e208e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Duarte=20Martins?= Date: Thu, 13 May 2010 17:00:13 +0000 Subject: * mdkapplet-add-media-helper: Added to gather common add media code (substitutes both mdkapplet-enterprise-update-helper and mdkapplet-restricted-helper). * mdkapplet_gui.pm (run_ask_credentials_dialog): Added here for reusability. --- mdkapplet-add-media-helper | 235 +++++++++++++++++++++++++++++++++++++ mdkapplet-enterprise-update-helper | 169 -------------------------- mdkapplet-restricted-helper | 199 ------------------------------- mdkapplet_gui.pm | 79 +++++++++++++ 4 files changed, 314 insertions(+), 368 deletions(-) create mode 100755 mdkapplet-add-media-helper delete mode 100755 mdkapplet-enterprise-update-helper delete mode 100755 mdkapplet-restricted-helper diff --git a/mdkapplet-add-media-helper b/mdkapplet-add-media-helper new file mode 100755 index 00000000..e0b04847 --- /dev/null +++ b/mdkapplet-add-media-helper @@ -0,0 +1,235 @@ +#!/usr/bin/perl +########################################################################### +# Copyright (C) 2010 Mandriva +# +# Thierry Vignaud +# João Victor Duarte Martins +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License Version 2 as +# published by the Free Software Foundation. +# +# 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, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. +########################################################################### + +use strict; +use lib qw(/usr/lib/libDrakX /usr/lib/libDrakX/drakfirsttime); +use standalone; # for explanations +use common; +use run_program; +use interactive; +use interactive::gtk; +use mygtk2 qw(gtknew); # only gtknew, any other stuff would break ugtk2 +use ugtk2 qw(:all); +use mdkonline qw($product_id); +use mdkapplet_gui; +use urpm::cfg; + +use XML::Simple; +use URI::Escape; +use LWP::UserAgent; +use HTTP::Request::Common; +use HTTP::Request; + +BEGIN { unshift @::textdomains, 'mdkonline' } + +# %product is keyed by product id and it contains the data to add +# restricted media for each product. +my %product = ( + powerpack => { + info_url => 'https://my.mandriva.com/powerpack/', + has_rights => sub { + my $profile = shift @_; + $profile->{data}{'can-access-restricted-repositories'} eq 'YES'; + }, + media_name => 'Restricted', + add_medium => \&add_medium_powerpack + + }, + server => { + info_url => 'http://www2.mandriva.com/linux/server/', + has_rights => sub { + my $profile = shift @_; + $profile->{data}{groups}{'es5-prod'} or + $profile->{data}{groups}{'es5-demo'}; + }, + media_name => 'Update', + add_medium => \&add_medium_enterprise + }, + ); + +# Distribution upgrade version and product +my $up_version; +my $up_product; + +# Dialogs title and banner text +my $title = N("Adding an additional package medium"); + + +# ###################################################################### +# Main Program + +{ + # Parsing command line arguments. + my $usage = + "Usage: $0 [OPTION]... VERSION\n" . + "Add package medias for VERSION (of current product by default).\n" . + "\n" . + " --rpm-root=PATH Use PATH as root for rpm\n" . + " --urpmi-root=PATH Use PATH as root for rpm and urpmi\n" . + " --product=NAME Upgrade to VERSION of product named NAME\n"; + + + foreach (@ARGV) { + if (/^--(rpm-root|urpmi-root)=(.+)/) { + $::rpmdrake_options{$1}[0] = $2; + } + elsif (/^--product=(.+)/) { + $up_product = lc $1; + } + elsif (/^([^-]{2}.+)/) { + $up_version = $1; + } + else { + die $usage; + } + } + $up_version or die $usage; + + # FIXME Couldn't that be automatic called when mdkonline.pm is used? + $product_id or mdkonline::get_product_id(); + + # Product id data should be used in lowercase. + $up_product = lc $product_id->{product} unless $up_product; + + # Sanitizes product command line argument. + unless (exists $product{$up_product}) { + my $available = join ", ", map { "'$_'" } keys %product; + $available =~ s/(.+), ([^,]+)/$1 and $2/; + die N("Supported products are %s, '%s' is not on the list.\n", + $available, + $up_product); + } + + run_authentication_dialog(); +} + +sub run_authentication_dialog { + my $description = N("Please fill in your account ID to add an " . + "additional package medium"); + mdkapplet_gui::run_ask_credentials_dialog($title, + $description, + \&add_restricted_medium); + ugtk2::exit(0); +} + +sub run_no_rights_dialog() { + my $w = mdkapplet_gui::new_portable_dialog($title); + my @widgets = ( + mdkonline::get_banner($title), + gtknew('Label_Left', + text => N("Your Mandriva account does not have %s " . + "download subscription enabled.", + mdkonline::translate_product()), + @mdkapplet_gui::common), + gtknew('HButtonBox', + layout => 'start', + children_tight => [ + interactive::gtk::add_padding( + mdkapplet_gui::new_link_button( + $product{$up_product}->{info_url}, + N("More Information") + ) + ) + ]), + create_okcancel($w, N("Close"), undef) + ); + mdkapplet_gui::fill_n_run_portable_dialog($w, \@widgets); +} + +sub add_restricted_medium { + my ($email, $passwd) = @_; + my $product = $product{$up_product}; + my $my_profile = mdkonline::xml2perl( + mdkonline::get_from("https://my.mandriva.com/rest/authenticate", + [ 'username', $email, + 'password', $passwd, + 'return', 'userdata' ] + )); + if ($my_profile->{code} != 0) { + my $in = interactive->vnew; + $in->ask_warn(N("Error"), + N("An error occurred") . "\n" . $my_profile->{message}); + goto &run_authentication_dialog; + } + elsif (!$product->{has_rights}->($my_profile)) { + run_no_rights_dialog(); + } + else { + add_medium_for_product($product, $email, $passwd); + } +} + +sub add_medium_for_product { + my ($product, $email, $passwd) = @_; + + my $error = 0; + my $arch = urpm::cfg::get_arch(); + my @archs = ($arch, if_($arch eq 'x86_64', 'i586')); + + for $arch (@archs) { + unless ($product->{add_medium}->($email, $passwd, $up_version, $arch)) { + $error = 1; + interactive->vnew->ask_warn( + N("Error"), + N("An error occurred while adding medium") + ); + goto &run_authentication_dialog; + } + } + + unless ($error) { + my $w = ugtk2->new(N("Successfully added media!"), + grab => 1); + $w->_ask_okcancel(N("Successfully added media %s.", + $product->{media_name}), + N("Ok"), + undef); + ugtk2::main($w); + } +} + +sub add_medium_enterprise { + my ($email, $password, $version, $arch) = @_; + my $uri = sprintf("https://%s:%s\@download.mandriva.com/%s/rpms/%s/", + uri_escape($email), + uri_escape($password), + $version, + $arch); + my @options = mdkonline::get_urpmi_options(); + run_program::raw(@options, '--update', '--distrib', $uri); +} + +sub add_medium_powerpack { + my ($email, $password, $version, $arch) = @_; + my $uri = sprintf("https://%s:%s\@dl.mandriva.com/rpm/comm/%s/", + uri_escape($email), + uri_escape($password), + $version); + my @options = mdkonline::get_urpmi_options(); + run_program::raw(@options, + "Restricted $arch " . int(rand(100000)), + "$uri$arch") or return 0; + run_program::raw(@options, + '--update', + "Restricted Updates $arch " . int(rand(100000)), + "${uri}updates/$arch"); +} diff --git a/mdkapplet-enterprise-update-helper b/mdkapplet-enterprise-update-helper deleted file mode 100755 index 31550d36..00000000 --- a/mdkapplet-enterprise-update-helper +++ /dev/null @@ -1,169 +0,0 @@ -#!/usr/bin/perl -################################################################################ -# Mandriva Online # -# # -# Copyright (C) 2009-2010 Mandriva # -# # -# Thierry Vignaud # -# # -# This program is free software; you can redistribute it and/or modify # -# it under the terms of the GNU General Public License Version 2 as # -# published by the Free Software Foundation. # -# # -# 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, write to the Free Software # -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # -################################################################################ - -use strict; -use lib qw(/usr/lib/libDrakX /usr/lib/libDrakX/drakfirsttime); -use standalone; # for explanations -use common; -use run_program; -use feature 'state'; - -BEGIN { unshift @::textdomains, 'mdkonline' } - -use mygtk2 qw(gtknew); #- do not import gtkadd which conflicts with ugtk2 version -use ugtk2 qw(:all); -use urpm::cfg; -use mdkonline; -use mdkapplet_gui; -use interactive; - -use XML::Simple; -use URI::Escape; -use LWP::UserAgent; -use HTTP::Request::Common; -use HTTP::Request; - -my $version; -foreach my $opt (@ARGV) { - if ($opt =~ /--(rpm-root|urpmi-root)=(.*)/) { - $::rpmdrake_options{$1}[0] = $2; - } else { - $version = $opt; - } -} -$version or die "usage: mdkapplet-enterprise-update-helper \n"; - -get_restricted_authentication(); - -ugtk2::exit(0); - - -sub get_restricted_authentication() { - my $w = ugtk2->new(N("Adding an additional package medium"), width => $width + 20); - my ($password_w, $email_w, $password); - - $password_w = gtknew('Entry'); - $password_w->set_visibility(0); - # WARNING: WON'T WORK IF EVER EMBEDDED - $w->{real_window}->signal_connect(destroy => sub { $password = $password_w->get_text }); - require interactive::gtk; - - my $res = - fill_n_run_portable_dialog( - $w, - [ - get_banner(N("Adding an additional package medium")), - gtknew('Label_Left', text => N("Please fill in your account ID to add an additional package medium"), - @common), - gtknew('HButtonBox', layout => 'start', children_tight => [ - interactive::gtk::add_padding( - new_link_button('https://my.mandriva.com/info', N("More information on your user account"))) - ]), - gtknew('Table', col_spacings => 5, row_spacings => 5, children => [ - [ N("Your email"), $email_w = gtknew('Entry') ], - [ N("Your password"), $password_w ], - ]), - gtknew('HButtonBox', layout => 'start', children_tight => [ - interactive::gtk::add_padding( - new_link_button('https://my.mandriva.com/reset/password/', N("Forgotten password"))) - ]), - create_okcancel($w, N("Next"), N("Cancel")), - ]); - - my $email = $email_w->get_text; - if ($res) { - if ($email && $password) { - add_enterprise_update_medium($email, $password); - } else { - interactive->vnew->ask_warn(N("Error"), N("Password and email cannot be empty.")); - goto &get_restricted_authentication; - } - } else { - ugtk2::exit(0); - } -} - -sub has_mes5_rights { - my ($ref) = @_; - $ref->{data}{groups}{'es5-prod'} || $ref->{data}{groups}{'es5-demo'}; -} - -my $error; -sub add_enterprise_update_medium { - my ($email, $password) = @_; - - my $res = get_from("https://my.mandriva.com/rest/authenticate", - [ 'username', $email, 'password', $password, - 'return', 'userdata' ]); - my $ref = xml2perl($res); - - if ($ref->{code} != 0) { - my $in = interactive->vnew; - $in->ask_warn(N("Error"), N("An error occurred") . "\n" . $ref->{message}); - goto &get_restricted_authentication; - } elsif (!has_mes5_rights($ref)) { - no_rights_dialog(); - } else { - $error = 0; - my $arch = urpm::cfg::get_arch(); - actually_add_enterprise_update_medium($ref, $password, $arch) - or adding_media_failed(); - if (!$error) { - #interactive->vnew->ask_okcancel(N("Error"), N("An error occurred while adding medium")); - my $w = ugtk2->new(N("Successfully added media %s.", 'Update'), grab => 1); - $w->_ask_okcancel(N("Successfully added media %s.", 'Update'), N("Ok"), undef); - ugtk2::main($w); - } - } -} - -sub no_rights_dialog() { - my $w = ugtk2->new(N("Adding an additional package medium"), width => $width + 20); - - fill_n_run_portable_dialog( - $w, - [ - get_banner(N("Adding an additional package medium")), - gtknew('Label_Left', text => N("Your Mandriva account does not have %s download subscription enabled.", translate_product()), - @common), - gtknew('HButtonBox', layout => 'start', children_tight => [ - interactive::gtk::add_padding( - new_link_button('http://www2.mandriva.com/linux/server/', N("More Information"))) - ]), - create_okcancel($w, N("Close"), undef), - ]); -} - -sub adding_media_failed { - $error = 1; - interactive->vnew->ask_warn(N("Error"), N("An error occurred while adding medium")); - goto &get_restricted_authentication; -} - -sub actually_add_enterprise_update_medium { - my ($ref, $password, $arch) = @_; - $password = uri_escape($password); - my @options = get_urpmi_options(); - my $email = uri_escape($ref->{data}{email}); - my $uri = "https://$email:$password\@download.mandriva.com/$version/rpms/$arch/"; - run_program::raw(@options, '--update', '--distrib', $uri); -} diff --git a/mdkapplet-restricted-helper b/mdkapplet-restricted-helper deleted file mode 100755 index 34ac0a0c..00000000 --- a/mdkapplet-restricted-helper +++ /dev/null @@ -1,199 +0,0 @@ -#!/usr/bin/perl -################################################################################ -# Mandriva Online # -# # -# Copyright (C) 2008-2010 Mandriva # -# # -# Thierry Vignaud # -# # -# This program is free software; you can redistribute it and/or modify # -# it under the terms of the GNU General Public License Version 2 as # -# published by the Free Software Foundation. # -# # -# 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, write to the Free Software # -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # -################################################################################ - -use strict; -use lib qw(/usr/lib/libDrakX /usr/lib/libDrakX/drakfirsttime); -use standalone; # for explanations -use common; -use run_program; -use feature 'state'; - -BEGIN { unshift @::textdomains, 'mdkonline' } - -use mygtk2 qw(gtknew); #- do not import gtkadd which conflicts with ugtk2 version -use ugtk2 qw(:all); -use urpm::cfg; -use mdkonline; -use mdkapplet_gui; -use interactive; - -use XML::Simple; -use URI::Escape; -use LWP::UserAgent; -use HTTP::Request::Common; -use HTTP::Request; -use interactive::gtk; - -my $version; -foreach my $opt (@ARGV) { - if ($opt =~ /--(rpm-root|urpmi-root)=(.*)/) { - $::rpmdrake_options{$1}[0] = $2; - } else { - $version = $opt; - } -} -$version or die "usage: mdkapplet-restricted-helper \n"; - -# make it work on 2008.X: -eval { interactive::gtk::add_padding(Gtk2::Label->new) }; -if ($@) { - *interactive::gtk::add_padding = sub { $_[0] }; -} - -get_restricted_authentication(); - -ugtk2::exit(0); - -my $email; - -sub get_restricted_authentication() { - my $w = ugtk2->new(N("Adding an additional package medium"), - width => $width + 20); - my $password_w = gtknew('Entry'); - my $email_w = gtknew('Entry', text => $email); - my $password; - my $clicked; - - $password_w->set_visibility(0); - - $w->{ok_clicked} = sub { - $password = $password_w->get_text; - $email = $email_w->get_text; - $clicked = 1; - Gtk2->main_quit; - }; - - my @widgets = ( - get_banner(N("Adding an additional package medium")), - gtknew('Label_Left', - text => N("Please fill in your account ID to add an " . - "additional package medium"), - @common), - gtknew('HButtonBox', - layout => 'start', - children_tight => [ - interactive::gtk::add_padding( - new_link_button('https://my.mandriva.com/info', - N("More information on your user " . - "account")) - ) - ]), - gtknew('Table', - col_spacings => 5, - row_spacings => 5, - children => [ [ N("Your email"), $email_w ], - [ N("Your password"), $password_w ] ]), - gtknew('HButtonBox', - layout => 'start', - children_tight => [ - interactive::gtk::add_padding( - new_link_button('https://my.mandriva.com/reset' . - '/password/', - N("Forgotten password")) - ) - ]), - create_okcancel($w, N("Next"), N("Cancel")), - ); - - fill_n_run_portable_dialog($w, \@widgets); - - if ($clicked) { - $clicked = 0; - if ($email && $password) { - add_restricted_medium($email, $password); - } - else { - interactive->vnew->ask_warn( - N("Error"), - N("Password and email cannot be empty.") - ); - goto &get_restricted_authentication; - } - } -} - -my $error; -sub add_restricted_medium { - my ($email, $password) = @_; - - my $res = get_from("https://my.mandriva.com/rest/authenticate", - [ 'username', $email, 'password', $password, - 'return', 'userdata' ]); - my $ref = xml2perl($res); - - if ($ref->{code} != 0) { - my $in = interactive->vnew; - $in->ask_warn(N("Error"), N("An error occurred") . "\n" . $ref->{message}); - goto &get_restricted_authentication; - } elsif ($ref->{data}{'can-access-restricted-repositories'} ne 'YES') { - no_rights_dialog(); - } else { - $error = 0; - my $arch = urpm::cfg::get_arch(); - actually_add_restricted_medium($ref, $password, $arch) - or adding_media_failed(); - # FIXME: is not enough if we (unlikely) ever support sparc64, ppc64 and the like: - if ($arch eq 'x86_64') { - actually_add_restricted_medium($ref, $password, 'i586') - or adding_media_failed(); - } - if (!$error) { - #interactive->vnew->ask_okcancel(N("Error"), N("An error occurred while adding medium")); - my $w = ugtk2->new(N("Successfully added media %s.", 'Restricted'), grab => 1); - $w->_ask_okcancel(N("Successfully added media %s.", 'Restricted'), N("Ok"), undef); - ugtk2::main($w); - } - } -} - -sub no_rights_dialog() { - my $w = ugtk2->new(N("Adding an additional package medium"), width => $width + 20); - - fill_n_run_portable_dialog( - $w, - [ - get_banner(N("Adding an additional package medium")), - gtknew('Label_Left', text => N("Your Mandriva account does not have Powerpack download subscription enabled."), - @common), - gtknew('HButtonBox', layout => 'start', children_tight => [ - interactive::gtk::add_padding( - new_link_button('https://my.mandriva.com/powerpack/', N("More Information"))) - ]), - create_okcancel($w, N("Close"), undef), - ]); -} - -sub adding_media_failed { - $error = 1; - interactive->vnew->ask_warn(N("Error"), N("An error occurred while adding medium")); - goto &get_restricted_authentication; -} - -sub actually_add_restricted_medium { - my ($ref, $password, $arch) = @_; - $password = uri_escape($password); - my @options = get_urpmi_options(); - my $email = uri_escape($ref->{data}{email}); - my $uri = "https://$email:$password\@dl.mandriva.com/rpm/comm/$version/"; - run_program::raw(@options, "Restricted $arch " . int(rand(100000)), "$uri$arch") or return 0; - run_program::raw(@options, '--update', "Restricted Updates $arch " . int(rand(100000)), "${uri}updates/$arch"); -} diff --git a/mdkapplet_gui.pm b/mdkapplet_gui.pm index 66131c61..65181e8a 100644 --- a/mdkapplet_gui.pm +++ b/mdkapplet_gui.pm @@ -23,6 +23,7 @@ package mdkapplet_gui; ################################################################################ use strict; +use feature 'state'; use lib qw(/usr/lib/libDrakX); use common; @@ -40,9 +41,13 @@ our @EXPORT = qw( setVar ); +our @EXPORT_OK = qw(run_ask_credentials_dialog); use mygtk2 qw(gtknew); #- do not import gtkadd which conflicts with ugtk2 version use ugtk2 qw(:all); +use mdkonline qw(); # you don't want to polute the namespace +use interactive; +use interactive::gtk; use lib qw(/usr/lib/libDrakX/drakfirsttime); ugtk2::add_icon_path("/usr/share/mdkonline/pixmaps/"); @@ -54,6 +59,12 @@ our $localfile = "$localdir/mdkonline"; mkdir_p($localdir) if !-d $localdir; -e "$ENV{HOME}/.mdkonline" and system("mv", "$ENV{HOME}/.mdkonline", $localfile); +# make it work on 2008.X: +eval { interactive::gtk::add_padding(Gtk2::Label->new) }; +if ($@) { + *interactive::gtk::add_padding = sub { $_[0] }; +} + our %local_config; read_local_config(); @@ -130,3 +141,71 @@ sub iso8601_date_to_locale { POSIX::strftime("%x", 0, 0, 0, $3, $2-1, $1-1900); } +sub run_ask_credentials_dialog { + my ($title, $description, $callback) = @_; + + my $w = new_portable_dialog($title); + my $password_text; + state $email_text; + my $password_w = gtknew('Entry'); + my $email_w = gtknew('Entry', text => $email_text); + my $ok_clicked; + + $password_w->set_visibility(0); + + $w->{ok_clicked} = sub { + $password_text = $password_w->get_text; + $email_text = $email_w->get_text; + $ok_clicked = 1; + Gtk2->main_quit; + }; + + my @widgets = ( + mdkonline::get_banner($title), + gtknew('Label_Left', + text => $description, + @common), + gtknew('HButtonBox', + layout => 'start', + children_tight => [ + interactive::gtk::add_padding( + new_link_button( + 'https://my.mandriva.com/info', + N("More information on your user account") + ) + ) + ]), + gtknew('Table', + col_spacings => 5, + row_spacings => 5, + children => [ [ N("Your email"), $email_w ], + [ N("Your password"), $password_w ] ]), + gtknew('HButtonBox', + layout => 'start', + children_tight => [ + interactive::gtk::add_padding( + new_link_button( + 'https://my.mandriva.com/reset/password/', + N("Forgotten password") + ) + ) + ]), + ugtk2::create_okcancel($w, N("Next"), N("Cancel")), + ); + + fill_n_run_portable_dialog($w, \@widgets); + + if ($ok_clicked) { + $ok_clicked = 0; + if ($email_text && $password_text) { + $callback->($email_text, $password_text); + } + else { + interactive->vnew->ask_warn( + N("Error"), + N("Password and email cannot be empty.") + ); + goto &authentication_dialog; + } + } +} -- cgit v1.2.1