| 1 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | package App::cpang; | ||
| 4 | 2 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | $App::cpang::VERSION = '0.03'; | |
| 6 | } | ||
| 7 | # ABSTRACT: CPAN GUI in Gtk2 | ||
| 8 | |||
| 9 | 0 | ▶ | use Gtk2 '-init'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Glib qw/ TRUE FALSE /; | ||
| 11 | use Gnome2::Vte; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my $class = shift; | ||
| 15 | my %opts = @_; | ||
| 16 | |||
| 17 | my $self = bless { | ||
| 18 | # public attributes | ||
| 19 | title => $opts{'title'} || 'cpang', | ||
| 20 | |||
| 21 | # private attributes | ||
| 22 | _terminal => Gnome2::Vte::Terminal->new, | ||
| 23 | _vscrollbar => Gtk2::VScrollbar->new, | ||
| 24 | _status => Gtk2::Statusbar->new, | ||
| 25 | }, $class; | ||
| 26 | |||
| 27 | $self->{'_main_window'} = $self->_create_main_window, | ||
| 28 | |||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _create_main_window { | ||
| 33 | my $self = shift; | ||
| 34 | my $window = Gtk2::Window->new; | ||
| 35 | |||
| 36 | # create a nice window | ||
| 37 | $window->set_title( $self->{'title'} ); | ||
| 38 | $window->signal_connect( | ||
| 39 | destroy => sub { Gtk2->main_quit } | ||
| 40 | ); | ||
| 41 | |||
| 42 | $window->set_border_width(5); | ||
| 43 | |||
| 44 | return $window; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub run { | ||
| 48 | my $self = shift; | ||
| 49 | my $terminal = $self->{'_terminal'}; | ||
| 50 | my $vscrollbar = $self->{'_vscrollbar'}; | ||
| 51 | my $status = $self->{'_status'}; | ||
| 52 | my $window = $self->{'_main_window'}; | ||
| 53 | |||
| 54 | # create a vbox and put it in the window | ||
| 55 | my $vbox = Gtk2::VBox->new( FALSE, 5 ); | ||
| 56 | $window->add($vbox); | ||
| 57 | |||
| 58 | # create an hbox and put it in the vbox | ||
| 59 | my $hbox = Gtk2::HBox->new( FALSE, 5 ); | ||
| 60 | $vbox->pack_start( $hbox, FALSE, TRUE, 5 ); | ||
| 61 | |||
| 62 | # create a label and put it in the hbox | ||
| 63 | my $label = Gtk2::Label->new('Module name:'); | ||
| 64 | $hbox->pack_start( $label, FALSE, TRUE, 0 ); | ||
| 65 | |||
| 66 | # create an entry (textbox) and put it in the hbox | ||
| 67 | my $entry = Gtk2::Entry->new; | ||
| 68 | $entry->signal_connect( | ||
| 69 | 'activate' => sub { $self->click( $entry ) } | ||
| 70 | ); | ||
| 71 | |||
| 72 | $hbox->pack_start( $entry, TRUE, TRUE, 0 ); | ||
| 73 | |||
| 74 | # create a button and put it in the hbox | ||
| 75 | my $button = Gtk2::Button->new('Install'); | ||
| 76 | $button->signal_connect( | ||
| 77 | clicked => sub { $self->click( $entry ) } | ||
| 78 | ); | ||
| 79 | $hbox->pack_start( $button, FALSE, TRUE, 0 ); | ||
| 80 | |||
| 81 | # create a terminal and put it in the vbox too | ||
| 82 | $vscrollbar->set_adjustment( $terminal->get_adjustment ); | ||
| 83 | $vbox->pack_start( $terminal, TRUE, TRUE, 0 ); | ||
| 84 | $terminal->signal_connect( | ||
| 85 | child_exited => sub { $entry->set_editable(1) } | ||
| 86 | ); | ||
| 87 | |||
| 88 | $vbox->pack_end ($status, FALSE, FALSE, 0); | ||
| 89 | $window->show_all; | ||
| 90 | $terminal->hide(); | ||
| 91 | Gtk2->main; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub click { | ||
| 95 | my ( $self, $entry ) = @_; | ||
| 96 | my $status = $self->{'_status'}; | ||
| 97 | my $terminal = $self->{'_terminal'}; | ||
| 98 | my $window = $self->{'_main_window'}; | ||
| 99 | my $text = $entry->get_text() || q{}; | ||
| 100 | |||
| 101 | if ($text) { | ||
| 102 | $entry->set_editable(0); | ||
| 103 | $entry->set_text(''); | ||
| 104 | |||
| 105 | $terminal->show(); | ||
| 106 | $status->pop (0); | ||
| 107 | $status->push (0, "Installing $text..."); | ||
| 108 | |||
| 109 | my $cmd_result = $terminal->fork_command( | ||
| 110 | 'cpanm', [ 'cpanm', $text ], | ||
| 111 | undef, '/tmp', FALSE, FALSE, FALSE, | ||
| 112 | ); | ||
| 113 | |||
| 114 | if ( $cmd_result == -1 ) { | ||
| 115 | my $cmd_result = $terminal->fork_command( | ||
| 116 | 'sudo', [ 'sudo', 'cpan', '-i', $text ], | ||
| 117 | undef, '/tmp', FALSE, FALSE, FALSE, | ||
| 118 | ); | ||
| 119 | |||
| 120 | if ( $cmd_result == -1 ) { | ||
| 121 | print STDERR "Cannot find 'cpanm' command\n"; | ||
| 122 | my $dialog = Gtk2::MessageDialog->new( | ||
| 123 | $window, | ||
| 124 | 'destroy-with-parent', | ||
| 125 | 'warning', | ||
| 126 | 'ok', | ||
| 127 | 'Cannot find "sudo", "cpan" or "cpanm" program', | ||
| 128 | ); | ||
| 129 | |||
| 130 | $dialog->run; | ||
| 131 | $dialog->destroy; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | 1; | ||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | =pod | ||
| 142 | |||
| 143 | =head1 NAME | ||
| 144 | |||
| 145 | App::cpang - CPAN GUI in Gtk2 | ||
| 146 | |||
| 147 | =head1 VERSION | ||
| 148 | |||
| 149 | version 0.03 | ||
| 150 | |||
| 151 | =head1 DESCRIPTION | ||
| 152 | |||
| 153 | It's about time we have a GUI for I<cpan>. Apparently we're not that into GUI, | ||
| 154 | but users are, so we need^Wshould care about it too. | ||
| 155 | |||
| 156 | This is a rough draft of a basic cpan GUI. It uses L<App::cpanminus> instead of | ||
| 157 | the basic I<cpan>. It's not pretty, but it's a start. | ||
| 158 | |||
| 159 | You are B<more than welcome> to help me work this into a beautiful GUI | ||
| 160 | application for users to use in order to search/install/test(?) modules and | ||
| 161 | applications from CPAN. | ||
| 162 | |||
| 163 | =head1 FOR USERS | ||
| 164 | |||
| 165 | If you are a user, please check L<cpang> for how to use this. | ||
| 166 | |||
| 167 | This paper describes the module behind the application. | ||
| 168 | |||
| 169 | =head1 ATTRIBUTES | ||
| 170 | |||
| 171 | These are the attributes available in C<new()>. | ||
| 172 | |||
| 173 | =head2 title | ||
| 174 | |||
| 175 | Sets the title of the main window. | ||
| 176 | |||
| 177 | use App::cpang; | ||
| 178 | |||
| 179 | my $app = App::cpang->new( title => 'MY MAIN TITLE!' ); | ||
| 180 | |||
| 181 | =head1 SUBROUTINES/METHODS | ||
| 182 | |||
| 183 | =head2 new | ||
| 184 | |||
| 185 | Surprisingly this creates a new object of type L<App::cpang>. | ||
| 186 | |||
| 187 | =head2 run | ||
| 188 | |||
| 189 | Packs everything and runs the application. | ||
| 190 | |||
| 191 | $app->run; | ||
| 192 | |||
| 193 | =head3 click($event) | ||
| 194 | |||
| 195 | Clicks on the "Install" step. This is bound to an event of the button in the | ||
| 196 | interface. | ||
| 197 | |||
| 198 | =head1 AUTHOR | ||
| 199 | |||
| 200 | Sawyer X <xsawyerx@cpan.org> | ||
| 201 | |||
| 202 | =head1 COPYRIGHT AND LICENSE | ||
| 203 | |||
| 204 | This software is copyright (c) 2010 by Sawyer X. | ||
| 205 | |||
| 206 | This is free software; you can redistribute it and/or modify it under | ||
| 207 | the same terms as the Perl 5 programming language system itself. | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | |||
| 212 | __END__ | ||