]>
Commit | Line | Data |
---|---|---|
bc05a8fc TL |
1 | package Proxmox::UI; |
2 | ||
3 | # a few simple abstractions to be a bit more general over what UI is in use, this is basically an | |
4 | # UIĀ², a User-Interface Interface | |
5 | ||
6 | use strict; | |
7 | use warnings; | |
8 | ||
9 | use Carp; | |
10 | ||
0903eb5c TL |
11 | use Proxmox::Sys::File qw(file_read_all); |
12 | ||
bc05a8fc TL |
13 | use Proxmox::UI::Gtk3; |
14 | use Proxmox::UI::StdIO; | |
15 | ||
5c6ca00a | 16 | my ($_ui, $_env) = (undef, undef); |
bc05a8fc | 17 | |
5c6ca00a TL |
18 | # state belongs fully to the UI |
19 | # env is a reference to the return value of Proxmox::Install::ISOEnv | |
bc05a8fc | 20 | sub init_gtk { |
5c6ca00a | 21 | my ($state, $env) = @_; |
bc05a8fc | 22 | |
5c6ca00a | 23 | croak "overriding existing UI!" if defined($_ui); |
bc05a8fc | 24 | |
5c6ca00a TL |
25 | $_ui = Proxmox::UI::Gtk3->new($state, $env); |
26 | $_env = $env; | |
bc05a8fc | 27 | |
5c6ca00a | 28 | return $_ui; |
bc05a8fc TL |
29 | } |
30 | sub init_stdio { | |
5c6ca00a | 31 | my ($state, $env) = @_; |
bc05a8fc | 32 | |
5c6ca00a | 33 | croak "overriding existing UI!" if defined($_ui); |
bc05a8fc | 34 | |
5c6ca00a TL |
35 | $_ui = Proxmox::UI::StdIO->new($state, $env); |
36 | $_env = $env; | |
bc05a8fc | 37 | |
5c6ca00a | 38 | return $_ui; |
bc05a8fc TL |
39 | } |
40 | ||
41 | sub get_ui { | |
5c6ca00a TL |
42 | return $_ui // croak "no UI initialized!"; |
43 | } | |
44 | ||
45 | my sub get_env { | |
46 | return $_env // croak "env not initialized!"; | |
bc05a8fc TL |
47 | } |
48 | ||
49 | sub message { | |
50 | my ($msg) = @_; | |
51 | get_ui()->message($msg); | |
52 | } | |
53 | ||
54 | sub error { | |
55 | my ($msg) = @_; | |
56 | get_ui()->error($msg); | |
57 | } | |
58 | ||
a8fbe0ff TL |
59 | sub finished { |
60 | my ($success, $msg) = @_; | |
61 | get_ui()->finished(!!$success, $msg); | |
62 | } | |
63 | ||
bc05a8fc TL |
64 | sub prompt { |
65 | my ($query) = @_; | |
66 | return get_ui()->prompt($query); | |
67 | } | |
68 | ||
0903eb5c TL |
69 | sub display_html { |
70 | my ($filename, $transform) = @_; | |
71 | ||
72 | my $env = get_env(); | |
73 | my $html_dir = "$env->{locations}->{lib}/html"; | |
74 | ||
75 | my $path; | |
76 | if (-f "$html_dir/$env->{product}/$filename") { | |
77 | $path = "$html_dir/$env->{product}/$filename"; | |
78 | } else { | |
79 | $path = "$html_dir/$filename"; | |
80 | } | |
81 | ||
82 | my $raw_html = file_read_all($path); | |
83 | ||
84 | $raw_html = $transform->($raw_html, $env) if $transform; | |
85 | ||
86 | $raw_html =~ s/__FULL_PRODUCT_NAME__/$env->{cfg}->{fullname}/g; | |
87 | ||
88 | return get_ui()->display_html($raw_html, $html_dir); | |
bc05a8fc TL |
89 | } |
90 | ||
3f463615 TL |
91 | sub progress { |
92 | my ($frac, $start, $end, $text) = @_; | |
93 | ||
94 | my $part = $end - $start; | |
95 | my $ratio = $start + $frac * $part; | |
96 | ||
97 | get_ui()->progress($ratio, $text); | |
98 | ||
99 | return $ratio; | |
100 | } | |
101 | ||
102 | sub process_events { | |
103 | get_ui()->process_events(); | |
104 | } | |
105 | ||
bc05a8fc | 106 | 1; |