Net-OpenVPN-TrayIcon

 view release on metacpan or  search on metacpan

lib/Net/OpenVPN/TrayIcon.pm  view on Meta::CPAN

        when (2){   # middle click
            $self->menu;
        }
        when (3){   # right click
            $self->menu;
        }
        default {
        }
    }

    return 1;
}

sub config_view {
    my ($self) = @_;

    # main config window
    my $gtk_window = Gtk2::Window->new('toplevel');
    $gtk_window->set_title('Config');
    $gtk_window->set_position('center');

    # vbox container (rows, columns, homogeneous)
    my $gtk_table = Gtk2::Table->new( 3, 1, 0 );

    # notebook
    my $gtk_notebook = Gtk2::Notebook->new;
    $gtk_notebook->set_tab_pos('top');

    # first page
    my $gtk_vbox_page_1 = Gtk2::VBox->new( 0, 1 );
}

sub menu {
    my ($self) = @_;

    my $gtk_menu = Gtk2::Menu->new();
    my $gtk_menu_start = Gtk2::ImageMenuItem->new_with_label('start VPN');
    $gtk_menu_start->signal_connect(
        activate => sub {
            system 'sudo ' . $self->config->{start_cmnd};
            $self->dispatch->{set_icon}->('default');
            $self->dispatch->{set_tip}->('OpenVPN checking connection..');
            $self->dispatch->{active_if_running}->();
        },
    );

    my $gtk_menu_stop = Gtk2::ImageMenuItem->new_with_label('stop VPN');
    $gtk_menu_stop->signal_connect(
        activate => sub {
            system 'sudo ' . $self->config->{stop_cmnd};
            $self->dispatch->{set_icon}->('default');
            $self->dispatch->{set_tip}->('OpenVPN checking connection..');
            $self->dispatch->{inactive_if_not_running}->();
        },
    );

    $gtk_menu->add($gtk_menu_start);
    $gtk_menu->add($gtk_menu_stop);
    $gtk_menu->show_all;

    $gtk_menu->popup( undef, undef, undef, undef, 0, 0 );
    $self->gtk_menu($gtk_menu);

    return 1;
}

sub _build_dispatch_table {
    my ($self) = @_;

    my %table = (
        set_icon => sub {
            my ($type) = @_;
            $self->gtk_icon->set_from_pixbuf($self->icons->{$type});
            return 1;
        },
        set_tip => sub {
            my ($tip) = @_;
            $self->gtk_tooltip->set_tip( $self->gtk_tray_icon, $tip);
            return 1;
        },
        active_if_running => sub {
            my $parent_pid = $$;
            my $child_pid = fork;

            if ($child_pid == 0){ # the child
                if ($self->_vpn_is_running(200)){
                    if ($self->_check_if_connected(10000)){
                        kill USR1 => $parent_pid;
                    }
                }
                else {
                    kill USR2 => $parent_pid;
                }

                POSIX::_exit(0);
            }
            else {
                # keep going
            }
        },
        inactive_if_not_running => sub {
            my $parent_pid = $$;
            my $child_pid = fork;

            if ($child_pid == 0){ # the child
                if ($self->_vpn_is_not_running(200)){
                    kill USR2 => $parent_pid;
                }
                else {
                    kill USR1 => $parent_pid;
                }

                POSIX::_exit(0);
            }
            else {
                # keep going
            }
        },

    );



( run in 1.033 second using v1.01-cache-2.11-cpan-364913b4093 )