diff --git a/lib/Exd/Gui.pm b/lib/Exd/Gui.pm index 13417c1..8506ea1 100644 --- a/lib/Exd/Gui.pm +++ b/lib/Exd/Gui.pm @@ -203,6 +203,10 @@ sub _daemon_runner($self) { $self->_on_check_activated( $instance_id, $packet->{data} ); next; } + if ( $packet->{type} eq 'get-price' ) { + $self->_on_get_price($instance_id); + next; + } warn 'Packet not recognized: ' . Data::Dumper::Dumper $packet; } exit; @@ -215,6 +219,38 @@ sub _build__exd($self) { return Exd->new; } +sub _on_get_price( $self, $instance_id ) { + my $pid = fork; + if ( !$pid ) { + eval { + my $ua = Mojo::UserAgent->new; + my $url = + $self->_exd->licenser_server . '/price/' . $self->_exd->uuid; + my $price_json = $ua->get($url)->result->json; + if ( !defined $price_json ) { + warn 'Server error retrieving price'; + exit; + } + my ( $price, $discount ) = $price_json->@{ 'price', 'discount' }; + $self->send_packet_to_window( + $instance_id, + { + type => 'price', + data => { + price => $price, + discount => $discount, + } + } + ); + exit; + }; + if ($@) { + warn $@; + exit; + } + } +} + sub _on_check_activated( $self, $instance_id, $data ) { my $pid = fork; if ( !$pid ) { diff --git a/lib/Exd/Gui/Instance.pm b/lib/Exd/Gui/Instance.pm index 14fc88b..eb9aacf 100644 --- a/lib/Exd/Gui/Instance.pm +++ b/lib/Exd/Gui/Instance.pm @@ -20,6 +20,7 @@ has app => ( is => 'rw', required => 1, weak_ref => 1 ); has instance_id => ( is => 'rw', required => 1 ); has window => ( is => 'rw' ); has _editor => ( is => 'rw', ); +has _window_price_discount => ( is => 'rw', ); has _safe_to_exit => ( is => 'rw', default => sub { 1 } ); has _transparent_avoid_input => ( is => 'rw' ); has _paywall => ( is => 'rw' ); @@ -28,6 +29,8 @@ has _pay_url => ( is => 'lazy' ); has _activated => ( is => 'rw' ); has _last_valid_preview_file => ( is => 'rw' ); has _is_showing_paywall => ( is => 'rw' ); +has _price => ( is => 'rw' ); +has _discount => ( is => 'rw' ); has file_format => ( is => 'rw', default => sub { @@ -129,7 +132,8 @@ sub start( $self, $exd_file ) { my $select_printer = Gtk4::Button->new_with_label('Select Printer'); my $open_gallery = Gtk4::Button->new_with_label('Open image gallery'); my $edit_fonts = Gtk4::Button->new_with_label('Open font gallery'); - my $printers_and_supplies = Gtk4::Button->new_with_label('Buy printers and supplies'); + my $printers_and_supplies = + Gtk4::Button->new_with_label('Buy printers and supplies'); $printers_and_supplies->signal_connect( clicked => sub { $self->_show_recommended_printers; @@ -221,7 +225,7 @@ sub start( $self, $exd_file ) { if ( !$self->_activated ) { my $uuid = $self->_exd->uuid; my $instance_id = $self->instance_id; - $self->_show_paywall($overlay); + $self->_add_timeout_paywall($overlay); $self->app->send_packet_check_if_activated( $self, $uuid ); } $win->set_child($overlay); @@ -236,9 +240,7 @@ sub _show_about_dialog($self) { my $picture = Gtk4::Picture->new; $picture->set_property( 'width-request', 256 ); $picture->set_property( 'height-request', 256 ); - $picture->set_filename( - Exd->root->child('exd-logo.png') - . '' ); + $picture->set_filename( Exd->root->child('exd-logo.png') . '' ); my $label = Gtk4::Label->new(undef); $label->set_markup(<<"EOF"); Welcome to Hiperthermia. @@ -295,7 +297,8 @@ sub _show_paywall( $self, $overlay ) { $paywall->set_property( 'height-request', 800 ); $paywall->add_css_class('paywall'); my $inner_paywall_box = Gtk4::Box->new( 'vertical', 10 ); - my $title = Gtk4::Label->new('This program is not activated'); + my $title = Gtk4::Label->new; + $title->set_markup('This program is not activated'); my $activate = Gtk4::Button->new_with_label('Pay and activate'); my $about = Gtk4::Button->new_with_label('More about the program'); my $remind_me_later = Gtk4::Button->new_with_label('Remind me later'); @@ -320,9 +323,8 @@ sub _show_paywall( $self, $overlay ) { $self->_add_timeout_paywall($overlay); } ); - my $picture = Gtk4::Image->new_from_file( - Exd->root->child('exd-logo.png') - . '' ); + my $picture = + Gtk4::Image->new_from_file( Exd->root->child('exd-logo.png') . '' ); $picture->set_pixel_size(256); $inner_paywall_box->append($picture); $inner_paywall_box->append($title); @@ -330,8 +332,18 @@ sub _show_paywall( $self, $overlay ) { my $debug_message = Gtk4::Label->new( 'This is debug compilation, do not use your real payment data but a Stripe test card' ); - $inner_paywall_box->append($debug_message); +# $inner_paywall_box->append($debug_message); } + my $window_price_discount = Gtk4::ScrolledWindow->new; + $window_price_discount->set_hexpand(1); + $window_price_discount->set_property('width-request', 600); + $self->_window_price_discount($window_price_discount); + if ( defined $self->_price ) { + $self->_on_receive_price; + } else { + $self->_send_get_price; + } + $inner_paywall_box->append($window_price_discount); $inner_paywall_box->append($activate); $inner_paywall_box->append($remind_me_later); $inner_paywall_box->append($about); @@ -445,10 +457,8 @@ sub _create_popover_menu( $self, $box ) { $file_menu->append( 'Open', 'app.open.' . $self->instance_id ); $file_menu->append( 'Save', 'app.save.' . $self->instance_id ); $file_menu->append( 'Save as', 'app.save-as.' . $self->instance_id ); - $help_menu->append( - 'Buy printers and supplies', - 'app.good-printers.' . $self->instance_id - ); + $help_menu->append( 'Buy printers and supplies', + 'app.good-printers.' . $self->instance_id ); $help_menu->append( 'About and contact', 'app.about.' . $self->instance_id ); $menu_model->append_submenu( 'File', $file_menu ); @@ -526,7 +536,7 @@ sub _show_recommended_printers($self) { for my $product (@products) { my $product_box = Gtk4::Box->new( 'horizontal', 10 ); my $image_file = Exd->root->child( $product->{image} ); - my $image = Gtk4::Image->new_from_file($image_file); + my $image = Gtk4::Image->new_from_file($image_file); $image->set_pixel_size(256); $product_box->append($image); my $right_side_product_box = Gtk4::Box->new( 'vertical', 10 ); @@ -683,7 +693,44 @@ sub _run_script( $self, $device = undef, $verbose = 1 ) { ); } +sub _on_receive_price($self) { + my $price = $self->_price; + my $discount = $self->_discount; + if ( !$self->_is_showing_paywall ) { + return; + } + my $window_price_discount = $self->_window_price_discount; + if ( !defined $window_price_discount ) { + return; + } + my $box = Gtk4::Box->new( 'horizontal', 10 ); + $box->set_halign('center'); + my $discount_label = Gtk4::Label->new(undef); + $discount_label->set_markup("-$discount% Cheaper than usual"); + $discount_label->add_css_class('discount'); + if ( !defined $price ) { + return; + } + $price =~ s/(..)$/.$1/; + $price =~ s/^\./0./; + my $price_label = Gtk4::Label->new(undef); + $price_label->set_markup("Activate now for: $priceā‚¬"); + $box->append($price_label); + if ( $discount > 2 ) { + $box->append($discount_label); + } + $window_price_discount->set_child($box); +} + sub receive_packet( $self, $packet ) { + if ( $packet->{type} eq 'price' ) { + my $data = $packet->{data}; + my ( $price, $discount ) = $data->@{ 'price', 'discount' }; + $self->_price($price); + $self->_discount($discount); + $self->_on_receive_price; + return; + } if ( $packet->{type} eq 'log' ) { my $data = $packet->{data}; $data =~ s/\s+$//; @@ -987,6 +1034,15 @@ sub _save_action($self) { ); } +sub _send_get_price( $self ) { + $self->app->send_packet_to_daemon( + $self, + { + type => 'get-price', + } + ); +} + sub _send_save_request( $self, $exd_dir, $dest_file ) { $self->app->send_packet_to_daemon( $self, diff --git a/me.sergiotarxz.Exd.metainfo.xml b/me.sergiotarxz.Exd.metainfo.xml index 4b1b53a..31722b9 100644 --- a/me.sergiotarxz.Exd.metainfo.xml +++ b/me.sergiotarxz.Exd.metainfo.xml @@ -30,6 +30,11 @@ + + +

Make more prominent affiliated printers and supplies and implementing discounts

+
+

Better startup script to run Glib::Object::Introspection INIT code

diff --git a/resources.gresource b/resources.gresource index b022075..e06a69e 100644 Binary files a/resources.gresource and b/resources.gresource differ diff --git a/style.css b/style.css index f81adc0..615b16f 100644 --- a/style.css +++ b/style.css @@ -6,6 +6,10 @@ button { min-width: 0; } +label.discount { + color: red; +} + box.paywall { background: white; }