aboutsummaryrefslogtreecommitdiff
path: root/lib/AlgaOS/Updater/GUI.pm
blob: b88f747d02a8f5a986fcd5bf93a7cd5244527c38 (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
package AlgaOS::Updater::GUI;

use v5.40.0;
use strict;
use warnings;

use Moo;
use AlgaOS::Updater;
use List::Util;
use JSON;
use POSIX qw/WNOHANG/;
use PBKDF2::Tiny;
use Crypt::URandom qw/urandom/;
use File::ShareDir ':ALL';
my $dist_dir_files = dist_dir('AlgaOS-Updater');

my @perl_args_chroot = @ARGV;

BEGIN {
    *CORE::GLOBAL::exit = sub {
        POSIX::_exit( $_[0] // 0 );
    };
}

has app         => ( is => 'lazy' );
has win         => ( is => 'rw' );
has const       => ( is => 'lazy' );
has pipe_name   => ( is => 'ro', required => 1 );
has secret      => ( is => 'ro', required => 1 );
has _grid_row   => ( is => 'rw', default  => sub { 0 } );
has _scroll     => ( is => 'rw' );
has started_gui => ( is => 'rw' );
has pid         => ( is => 'rw' );

sub call_and_increment_grid_row( $self, $coderef ) {
    $coderef->();
    $self->_grid_row( $self->_grid_row + 1 );
}

sub _build_const {
    return AlgaOS::Updater::Constants->new;
}

sub _build_app {
    return Gtk::Application->new( "com.algaos.Updater", 0 );
}

sub activate($self) {
    if ( $self->started_gui ) {
        return;
    }
    $self->started_gui(1);
    my $const = $self->const;
    my $win   = Gtk::ApplicationWindow->new( $self->app );
    $win->set_title("Update AlgaOS");
    my $display  = $win->get_display;
    my $provider = Gtk::CssProvider->new;
    $provider->load_from_path( $dist_dir_files . '/style.css' );
    $display->add_css_provider( $provider,
        $const->GTK_STYLE_PROVIDER_PRIORITY_APPLICATION );
    my $width  = 400;
    my $height = ( 365 * 440 ) / 770;
    $win->set_default_size( $width, $height );
    $win->set_resizable(0);
    $self->win($win);
    my $overlay = Gtk::Overlay->new;
    my $file    = Gio::File->new( $dist_dir_files . '/beach0.jpg' );
    my $texture = Gdk::Texture->new($file);
    my $picture = Gtk::Picture->new($texture);
    $picture->set_size_request( $width, $height );
    $overlay->set_child($picture);
    my $scroll = Gtk::ScrolledWindow->new;
    $self->_scroll($scroll);
    $overlay->add_overlay($scroll);
    $win->set_child($overlay);
    $win->connect(
        'close-request' => sub {
            $self->win(undef);
            $self->started_gui(0);
            return 0;
        }
    );
    $win->present;
}

sub is_there_updates {
    my $output = `emerge -p --quiet -uUDN \@system`;

    my $updatable = $output =~ /^\[(?:ebuild|binary)\s+/;

    if ($updatable) {
        exit 0;
    }
    else {
        exit 1;
    }
}

sub _check_pid($self) {
    if ( 0 < waitpid $self->pid, WNOHANG ) {
        $self->activate if $? == 0;
        $self->pid(0);
        return 1;
    }
    return 0;
}

sub _start_pid($self) {
    $self->pid(fork);
    if ( !$self->pid ) {
        $self->is_there_updates;
    }
}

sub _handle_pid($self) {
    if ( $self->pid ) {
        $self->_check_pid;
    }
    else {
        $self->_start_pid;
    }
}

sub _handle_pid_and_schedule($self) {
    my $multiply_by_hour = 0;
    my $time             = 1000;
    $self->_handle_pid;
    if ( !$self->pid ) {
        $multiply_by_hour = 1;
    }
    if ($multiply_by_hour) {
        $time *= 3600;
    }
    say "Waiting @{[$time / 1000]} seconds";
    $self->app->timeout_add(
        $time,
        sub {
            $self->_handle_pid_and_schedule;
            return 0;
        }
    );
    return 0;
}

sub run($self) {
    $self->app->connect(
        'startup' => sub {
            $self->app->timeout_add(
                3000,
                sub {
                    $self->_handle_pid_and_schedule;
                }
            );
        }
    );
    $self->app->connect(
        'activate' => sub {
            $self->activate;
        }
    );

    $self->app->hold;
    $self->app->run(@ARGV);
}
1;