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
299
300
301
302
303
304
|
#!/usr/bin/perl
################################################################################
# Backup Applet #
# #
# Copyright (C) 2008 Mandriva #
# #
# Thierry Vignaud <tvignaud at mandriva dot 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 POSIX ":sys_wait_h";
use Config;
use lib qw(/usr/lib/libDrakX);
use standalone; # for explanations
use interactive;
use common;
use run_program;
use detect_devices;
use feature 'state';
use Gtk2::Pango;
BEGIN { unshift @::textdomains, 'draksnapshot' }
use mygtk2 qw(gtknew); #- do not import gtkadd which conflicts with ugtk2 version
use ugtk2 qw(:all);
use lib qw(/usr/lib/libDrakX/drakfirsttime);
use Gtk2::Notify '-init', 'draksnapshot';
ugtk2::add_icon_path("/usr/share/draksnapshot/pixmaps/");
my ($menu, $timeout, @discs, $state_global);
my $localfile = "$ENV{HOME}/.draksnapshot";
my $insensitive_while_running_a_child;
my %state = (
okay => {
menu => [],
do_not_use_bubble => 1,
},
disk_found => {
menu => [ 'configure' ],
tt => [ N_("USB discs are available for backups") ]
},
config_in_progress => {
menu => [],
do_not_use_bubble => 1,
tt => [ N_("Configurator is currently running") ]
},
);
my %actions = (
'configure' => { name => N("Configure"), launch => \&configure }
);
my $icon = Gtk2::StatusIcon->new;
$icon->signal_connect(popup_menu => sub {
my ($_icon, $button, $time) = @_;
$menu and $menu->popup(undef, undef, undef, undef, $button, $time);
});
$icon->signal_connect(activate => \&configure);
my ($opt) = @ARGV;
if ($opt eq '--force' || $opt eq '-f') { setAutoStart('TRUE') }
shouldStart() or die "$localfile should be set to TRUE: please use --force or -f option to launch applet\n";
go2State('okay');
gtkflush();
silentCheck();
setup_cyclic_check();
$SIG{USR1} = 'IGNORE';
$SIG{USR2} = 'IGNORE';
$SIG{CHLD} = \&harvester;
run_program::raw({ detach => 1 }, 'ionice', '-p', $$, '-n7');
Gtk2->main;
ugtk2::exit(0);
my ($config_pid, $checker_pid);
my %comm_codes = (
discs => {
code => 2,
status => 'locked',
log => N_("urpmi database locked") . ", skipping updating urpmi database",
},
nothing => {
code => 3,
status => 'critical',
log => N_("Error updating media"),
},
);
# Signal management
sub harvester {
my ($_signame, $_clean) = @_;
my ($childpid, @pids);
my $configurator_returned;
do {
$childpid = waitpid(-1, &WNOHANG);
if ($config_pid && $config_pid == $childpid) {
undef $config_pid;
$configurator_returned = 1;
# we should better check it has really been configured indeed.
setAutoStart('FALSE');
ugtk2::exit(0);
} elsif ($checker_pid && $checker_pid == $childpid) {
undef $checker_pid;
my $status = $? >> 8;
my ($state) = grep { $_->{code} eq $status } values %comm_codes;
if ($state) {
logIt($state->{log});
go2State($state->{status});
}
}
push @pids, $childpid;
WIFEXITED($?) and refresh_gui(1);
} while $childpid > 0;
Glib::Timeout->add(200, sub { silentCheck(); 0 }) if $configurator_returned;
return @pids;
}
sub fork_exec {
my $pid = run_program::raw({ detach => 1 }, @_);
refresh_gui(1);
return $pid;
}
sub refresh_gui {
my ($sens) = @_;
#!$conf_launched and silentCheck(); $conf_launched = 0;
my $w = $::main_window ? $::main_window->window : undef;
$insensitive_while_running_a_child = !$sens;
$sens ? gtkset_mousecursor_normal($w) : gtkset_mousecursor_wait($w);
gtkflush();
}
sub configure() {
$config_pid = fork_exec('/usr/sbin/draksnapshot-config');
go2State('config_in_progress');
}
sub silentCheck() {
state @old_discs;
return if $config_pid;
my $w = $::main_window ? $::main_window->window : undef;
gtkset_mousecursor_wait($w);
$checker_pid = fork();
if (defined $checker_pid) {
return if $checker_pid; # parent
# immediate exit, else forked gtk+ object destructors will badly catch up parent applet
my $_safe = before_leaving { POSIX::_exit(0) };
# force refreshing cache:
detect_devices::probeall_update_cache();
@discs = grep { $_->{usb_bus} && detect_devices::may_be_a_hd($_) } detect_devices::getSCSI();
}
go2State('okay') if !@discs;
# are there any updates ?
my @a = difference2(\@discs, \@old_discs);
if (@a && $state_global ne 'disk_found') {
go2State('disk_found');
}
@old_discs = @discs;
gtkset_mousecursor_normal($w) if !$insensitive_while_running_a_child;
}
sub setup_cyclic_check() {
$timeout = Glib::Timeout->add(
5000,
sub { # default to 2sec
silentCheck();
1;
});
}
sub go2State {
my $state = shift;
$menu->destroy if $menu;
$menu = setState($state);
$state_global = $state;
}
sub isNetwork() {
require network::network;
require network::tools;
my $net = {};
network::network::read_net_conf($net);
my ($_gw_intf, $_is_up, $gw_address, $_dns_server) = network::tools::get_internet_connection($net);
to_bool($gw_address);
}
sub shouldStart() {
my %p = getVarsFromSh($localfile);
to_bool($p{AUTOSTART} ne 'FALSE');
}
sub setState {
my ($state_type) = @_;
my $checkme;
my $arr = $state{$state_type}{menu};
state $pixbuf;
if (!$pixbuf) {
$pixbuf = gtkcreate_pixbuf('draksnapshot');
$icon->set_from_pixbuf($pixbuf);
}
$icon->set_tooltip(formatAlaTeX(translate($state{$state_type}{tt}[0])));
$icon->set_visible($state_type ne 'okay');
gtkflush(); # so that bubbles are displayed on right icon
if ($state{$state_type}{tt}[0] && $icon->isa('Gtk2::StatusIcon') && !$state{$state_type}{do_not_use_bubble}) {
my $bubble = Gtk2::Notify->new_with_status_icon(N("Warning"), formatAlaTeX(translate($state{$state_type}{tt}[0])) . "\n",
'/usr/share/icons/draksnapshot.png', $icon);
$bubble->set_timeout(5000);
$bubble->show;
}
my $menu = Gtk2::Menu->new;
foreach (@$arr) {
$menu->append(gtksignal_connect(gtkshow(Gtk2::MenuItem->new_with_label($actions{$_}{name})), activate => $actions{$_}{launch}));
}
$menu->append(gtkshow(Gtk2::SeparatorMenuItem->new));
$menu->append(
gtksignal_connect(
gtkshow(Gtk2::MenuItem->new_with_label(N("About..."))),
activate => sub {
my $ver = 1; # automatically set from spec file
my $w = gtknew('AboutDialog', name => N("DrakSnapshot %s", $ver),
copyright => N("Copyright (C) %s by Mandriva", '2001-2007'),
license => join('', cat_('/usr/share/common-licenses/GPL')),
icon => '/usr/share/icons/mini/draksnapshot.png',
comments => N("DrakSnapshot enables to backup your machine through periodic snapshots."),
website => 'http://www.mandriva.com',
website_label => N("Mandriva WebSite"),
authors => 'Thierry Vignaud <vignaud@mandriva.com>',
translator_credits =>
#-PO: put here name(s) and email(s) of translator(s) (eg: "John Smith <jsmith@nowhere.com>")
N("_: Translator(s) name(s) & email(s)\n"),
transient_for => $::main_window, modal => 1, position_policy => 'center-on-parent',
);
$w->show_all;
$w->run;
return 1;
}));
$menu->append(gtksignal_connect(gtkset_active($checkme = Gtk2::CheckMenuItem->new_with_label(N("Always launch on startup")), shouldStart()), toggled => sub { setAutoStart(uc(bool2text($checkme->get_active))) }));
$checkme->show;
$menu->append(gtksignal_connect(gtkshow(Gtk2::MenuItem->new_with_label(N("Quit"))), activate => sub { mainQuit() }));
$menu;
}
sub logIt {
my $log = shift;
log::explanations($log);
}
sub setVar {
my ($file, $var, $st) = @_;
my %s = getVarsFromSh($file);
$s{$var} = $st;
setVarsInSh($file, \%s);
}
sub setAutoStart {
my ($state) = @_;
if (-f $localfile) {
setVar($localfile, 'AUTOSTART', $state);
} else {
output_p($localfile, "AUTOSTART=$state\n");
}
}
sub mainQuit() {
Glib::Source->remove($timeout) if $timeout;
Gtk2->main_quit;
}
|