aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ManaTools/Rpmdragora/gurpm.pm
blob: 4bc59a4b94a86e8902c355407e525f82dcc810f9 (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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# vim: set et ts=4 sw=4:
package ManaTools::Rpmdragora::gurpm;

#============================================================= -*-perl-*-

=head1 NAME

    ManaTools::Rpmdragora::gurpm - Module that shows the urpmi
                                    progress status

=head1 SYNOPSIS

    my %option = (title => "Urpmi action ivoked", text => "Please wait", );
    my $gurpmi = ManaTools::Rpmdragora::gurpm->new(%option);
    $gurpmi->progress(45);

    #add to an existing dialog
    %option = (title => "Urpmi action ivoked", text => "Please wait", main_dialog => $dialog, parent => $parent_container);
    $gurpmi = ManaTools::Rpmdragora::gurpm->new(%option);
    $gurpmi->progress(20);

=head1 DESCRIPTION

    This class is used to show the progress of an urpmi operation on
    its progress bar. It can be istantiated as a popup dialog or used
    to add label and progress bar into a YLayoutBox container.

=head1 SUPPORT

    You can find documentation for this module with the perldoc command:

    perldoc ManaTools::Rpmdragora::gurpm

=head1 AUTHOR

    Angelo Naselli <anaselli@linux.it>

    Matteo Pasotti <matteo.pasotti@gmail.com>

=head1 COPYRIGHT and LICENSE

    Copyright (c) 2002 Guillaume Cottenceau
    Copyright (c) 2002-2007 Thierry Vignaud <tvignaud@mandriva.com>
    Copyright (c) 2003, 2004, 2005 MandrakeSoft SA
    Copyright (c) 2005-2007 Mandriva SA
    Copyright (c) 2013-2017 Matteo Pasotti <matteo.pasotti@gmail.com>
    Copyright (C) 2015-2017, Angelo Naselli <anaselli@linux.it>

    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

=cut


use Moose;
use Carp;
use Time::HiRes;

use yui;
use feature 'state';


has 'title' => (
    is => 'rw',
    isa => 'Str',
);

has 'text' => (
    is => 'rw',
    isa => 'Str',
);

has 'main_dialog' => (
    is => 'rw',
    isa => 'yui::YDialog',
);

has 'parent' => (
    is => 'rw',
    isa => 'yui::YReplacePoint',
);

has 'label_widget' => (
    is => 'rw',
    isa => 'yui::YLabel',
    init_arg  => undef,
);

has 'progressbar' => (
    is => 'rw',
    isa => 'yui::YProgressBar',
    init_arg  => undef,
);

#=============================================================

=head2 BUILD

=head3 DESCRIPTION

    The BUILD method is called after a Moose object is created,
    in this methods Services loads all the service information.

=cut

#=============================================================
sub BUILD {
    my $self = shift;

    my $factory = yui::YUI::widgetFactory;
    my $vbox;

    if (! $self->main_dialog) {
        if ($self->parent) {
            carp "WARNING: parent parameter is skipped without main_dialog set\n" ;
            $self->parent(undef);
        }
        $self->main_dialog($factory->createPopupDialog());
        $vbox =  $factory->createVBox($self->main_dialog);
    }
    else {
        die "parent parameter is mandatory with main_dialog" if !$self->parent;
        $self->main_dialog->startMultipleChanges();
        $self->parent->deleteChildren();
        $vbox = $factory->createVBox($self->parent);
        $factory->createVSpacing($vbox, 0.5);
    }

    $self->label_widget( $factory->createLabel($vbox, $self->text) );
    $self->label_widget->setStretchable( $yui::YD_HORIZ, 1 );
    $self->progressbar( $factory->createProgressBar($vbox, "") );

    if ($self->parent) {
        $factory->createVSpacing($vbox, 0.5);
        $self->parent->showChild();
        $self->main_dialog->recalcLayout();
        $self->main_dialog->doneMultipleChanges();
    }

    $self->main_dialog->pollEvent();
    $self->flush();
}


#=============================================================

=head2 flush

=head3 DESCRIPTION

    Polls a dialog event to refresh the dialog

=cut

#=============================================================
sub flush {
    my ($self) = @_;

    $self->main_dialog->startMultipleChanges();
    $self->main_dialog->recalcLayout();
    $self->main_dialog->doneMultipleChanges();

    if ($self->main_dialog->isTopmostDialog()) {
        $self->main_dialog->waitForEvent(10);
        $self->main_dialog->pollEvent();
    }
    else {
        carp "This dialog is not a top most dialog\n";
    }
    yui::YUI::app()->redrawScreen();
}

#=============================================================

=head2 label

=head3 INPUT

    $text: text to be shown on label

=head3 DESCRIPTION

    Sets the label text

=cut

#=============================================================
sub label {
    my ($self, $text) = @_;

    $self->main_dialog->startMultipleChanges();
    $self->label_widget->setValue($text) if $text;
    $self->main_dialog->doneMultipleChanges();

    $self->flush();
}

#=============================================================

=head2 progress

=head3 INPUT

    $value: integer value in the range 0..100

=head3 DESCRIPTION

    Sets the progress bar percentage value

=cut

#=============================================================
sub progress {
    my ($self, $value) = @_;
    state $time = 0;

    $value = 0 if !defined($value) || $value < 0;
    $value = 100 if 100 < $value;

    $self->progressbar->setValue($value);
    return if Time::HiRes::clock_gettime() - $time < 0.333;
    $time = Time::HiRes::clock_gettime();

    $self->flush();
}

#=============================================================

=head2 DEMOLISH

=head3 INPUT

    $val: boolean value indicating whether or not this method
        was called as part of the global destruction process
        (when the Perl interpreter exits)

=head3 DESCRIPTION

    Moose provides a hook for object destruction with the
    DEMOLISH method as it does for construtor with BUILD

=cut

#=============================================================
sub DEMOLISH {
    my ($self, $val) = @_;

    $self->main_dialog->destroy if !$self->parent;
}

# TODO cancel button cannot be easily managed in libyui polling events
# removed atm
#
# sub validate_cancel {
#     my ($self, $cancel_msg, $cancel_cb) = @_;
#     $self->{main_dialog}->startMultipleChanges();
#     if (!$self->{cancel}) {
# 		$self->{cancel} = $self->{factory}->createIconButton($self->{vbox},"",$cancel_msg);
#         #gtkpack__(
# 	    #$self->{vbox},
# 	    #$self->{hbox_cancel} = gtkpack__(
# 		#gtknew('HButtonBox'),
# 		#$self->{cancel} = gtknew('Button', text => $cancel_msg, clicked => \&$cancel_cb),
# 	    #),
# 	#);
#     }
#     #$self->{cancel}->set_sensitive(1);
#     #$self->{cancel}->show;
#     $self->flush();
# }
#
# sub invalidate_cancel {
#     my ($self) = @_;
#     $self->{cancel} and $self->{cancel}->setEnabled(0);
# }
#
# sub invalidate_cancel_forever {
#     my ($self) = @_;
#     #$self->{hbox_cancel} or return;
#     #$self->{hbox_cancel}->destroy;
#     # FIXME: temporary workaround that prevents
#     # Gtk2::Label::set_text() set_text_internal() -> queue_resize() ->
#     # size_allocate() call chain to mess up when ->shrink_topwindow()
#     # has been called (#32613):
#     #$self->shrink_topwindow;
# }

1;