summaryrefslogtreecommitdiffstats
path: root/mdkapplet-add-media-helper
blob: 0130b12bd23dfa67c21a88676d8ae23216b9e7b9 (plain)
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
#!/usr/bin/perl
###########################################################################
# Copyright (C) 2010 Mandriva
#
# Thierry Vignaud <tvignaud@mandriva.com>
# João Victor Duarte Martins <jvictor@mandriva.com>
#
# 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, 
					      \&authenticate_callback);
    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 authenticate_callback {
    my ($email, $passwd) = @_;
    my $product = $product{$up_product};
    my $profile = mdkonline::get_my_mdv_profile($email, $passwd);

    if ($profile->{code} != 0) {
        my $in = interactive->vnew;
        $in->ask_warn(N("Error"), 
		      N("An error occurred") . "\n" . $profile->{message});
        goto &run_authentication_dialog;
    } 
    elsif (!$product->{has_rights}->($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");
}