Advertisement
hackloper775

PerlBrowser 4.1.1

Jan 6th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 12.83 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. #
  4. # Instalar Gtk3 y Gtk3::WebKit por CPAN,para funcionar correctamente
  5. # Programa de prueba para Tiempo de Tux <http://www.itimetux.com>
  6. # Por Demianx864
  7. # Eres libre de estudiar modificar y añadir nuevas cosas a el programa,que te diviertas :D
  8. # Simple PerlBrowser 4.1.1
  9. # Gracias al modulo WebKit <http://webkitgtk.org/>
  10. #
  11.  
  12. use strict;
  13. use feature ':5.14';
  14. use Glib qw/TRUE FALSE/;
  15. use Gtk3 -init;
  16. binmode STDOUT, ":utf8";
  17. use utf8;
  18. use JSON;
  19.  
  20. my $json; # Definimos variable json
  21.  
  22. &json; # Llamamos rutina json
  23.  
  24. my $confP = decode_json($json); # Definimos variable que llama json
  25.    
  26.     # Variables que definen ajustes
  27.    
  28.         my $iniP = $confP->{'pantalla'}->{'inicio'}->[0];
  29.         my $unscreen = $confP->{'pantalla'}->{'inicio'}->[1]; # Arrays
  30.         my $sview = $confP->{'pantalla'}->{'inicio'}->[2];
  31.  
  32.         use Gtk3::WebKit;      # Llamamos a webkit
  33.        
  34.         # Usar logo.xpm
  35.  
  36.         my $ventana = Gtk3::Window->new('toplevel'); # Definimos ventana principal
  37.         my $icono = 'icon.xpm'; # Definimos icono
  38.         if ( -e $icono ) { # Vemos si existe el icono
  39.                 $ventana->set_icon_from_file($icono); # Si es asi,se lo colomos a la ventana
  40.         }
  41.  
  42.         # Parametros de la ventana
  43.  
  44.         my $titulo = "Simple Perl Browser"; # Titulo de la ventana
  45.         $ventana->set_default_size(800, 600); # Tamaño por defecto
  46.     $ventana->maximize(TRUE);  #  Maximizar al abrir
  47.         $ventana->set_title($titulo);  # Colocamos titulo
  48.         $ventana->set_border_width(5); # Añadimos bordes
  49.         $ventana->$unscreen(); # No fullscreen por defecto
  50.         $ventana->signal_connect(delete_event => \&salir); # Si recibe señal de X i a &salir
  51.     my $inicio; # Ventana de configuracion de inicio
  52.  
  53.         # Opciones
  54.  
  55.         my $table = Gtk3::Grid->new; # Creamos tabla con Grid
  56.  
  57.     # Opciones nulas
  58.     # Arrays multidimensionales
  59.  
  60.         my @entries = (
  61.         [ "Opciones", undef, "_Opciones" ],
  62.         ["Acerca de", undef, "_Acerca de", undef, "Acerca de", \&activate_action ],
  63.         ["Inicio", undef, "_Inicio", undef, "Inicio", \&activate_action ],
  64.         ["Salir", undef, "_Salir", undef, "Salir", \&activate_action ]
  65.         );
  66.  
  67.     # Opciones cambiantes
  68.     # Arrays multidimensionales
  69.  
  70.         my @toggle_entries = (                      
  71.         ["Completa",undef, "_Pantalla Completa", undef,    
  72.         "Pantalla completa", \&activate_action, FALSE ],                              
  73.         ["HideTitlebar", undef, "_Hide Titlebar", undef,
  74.         "Hide Titlebar",  \&activate_action,FALSE]
  75.         );
  76.    
  77.     # Las metemos en un grupo
  78.  
  79.         my $action_group = Gtk3::ActionGroup->new('AppWindowActions');
  80.         $action_group->add_actions( \@entries, undef );
  81.         $action_group->add_toggle_actions( \@toggle_entries, undef );
  82.  
  83.     # Creamos un UIManager y insertamos el grupo
  84.  
  85.         my $ui = Gtk3::UIManager->new();
  86.         $ui->insert_action_group( $action_group, 0 );
  87.         my $ui_info = get_ui();
  88.         $ui->add_ui_from_string( $ui_info, length($ui_info) );
  89.         $table->attach( $ui->get_widget('/MenuBar'), 0, 0, 1, 1 );
  90.  
  91.         # Webkit
  92.  
  93.         my $uri = shift @ARGV || $iniP ; # Definimos url de variable $iniP
  94.         my $ver = Gtk3::WebKit::WebView->new(); # Creamos WebView
  95.  
  96.         # Cargamos url
  97.        
  98.         &web();
  99.        
  100.         # Añadimos url a scrolls
  101.  
  102.         my $scrolls = Gtk3::ScrolledWindow->new(); # Creamos scrolled window
  103.         $scrolls->set_policy ('automatic', 'automatic');        
  104.         $scrolls->add($ver); # Añadimos Webview
  105.        
  106.         # Entrada
  107.  
  108.         my $entry = Gtk3::Entry->new;  # Entrada de url
  109.         $entry->signal_connect( "activate" => \&entrada, undef ); # Desviamos entrada a &entrada
  110.         my $entrycio = Gtk3::Entry->new; # Entrada de pagina de inicio
  111.         $entrycio->signal_connect( "activate" => \&confinicio, undef );
  112.  
  113.         # Botones
  114.  
  115.     # Creamos y labels
  116.  
  117.         my $brg = Gtk3::ToolButton->new_from_stock('gtk-quit');
  118.         my $go = Gtk3::ToolButton->new_from_stock('gtk-find');
  119.         my $fresh = Gtk3::ToolButton->new_from_stock('gtk-refresh');
  120.         my $frenar = Gtk3::ToolButton->new_from_stock('gtk-stop');    
  121.         my $batras = Gtk3::ToolButton->new_from_stock('gtk-go-back');
  122.         my $belante = Gtk3::ToolButton->new_from_stock('gtk-go-forward');
  123.         my $vermenos = Gtk3::ToolButton->new_from_stock('gtk-zoom-in');
  124.         my $vermas = Gtk3::ToolButton->new_from_stock('gtk-zoom-out');
  125.         my $label = Gtk3::Label->new("");
  126.  
  127.     # Desviamos señales de botones a subrutinas
  128.  
  129.         $go->signal_connect ( "clicked" => \&entrada, undef );
  130.         $fresh->signal_connect ( "clicked" => \&recargar, undef );    
  131.         $frenar->signal_connect ( "clicked" => \&parar );      
  132.         $batras->signal_connect( "clicked" => \&atras );
  133.         $belante->signal_connect ( "clicked" => \&adelante );
  134.         $vermenos->signal_connect ( "clicked" => \&zomin );
  135.         $vermas->signal_connect ( "clicked" => \&zomout);
  136.  
  137.         # Frenar y recargar unificados
  138.  
  139.         my $botones = Gtk3::Notebook->new(); # Creamos Notebook
  140.         $botones->set_show_tabs(FALSE);
  141.         $botones->set_show_border(FALSE); # No mostramos bordes
  142.         $botones->append_page($fresh); # Añadimos botones
  143.         $botones->append_page($frenar);
  144.  
  145.         # Unimos todo
  146.        
  147.         # Caja horizontal
  148.  
  149.         my $hbox = Gtk3::Box->new("horizontal", 0);
  150.         $hbox->pack_start($entry, TRUE,TRUE , 0);
  151.         $hbox->pack_start($go, FALSE,FALSE , 0);
  152.         $hbox->pack_start($label, FALSE,FALSE , 0);
  153.         $hbox->pack_start($botones, FALSE,FALSE , 0);
  154.         $hbox->pack_start($vermas, FALSE,FALSE , 0);
  155.         $hbox->pack_start($vermenos, FALSE,FALSE , 0);
  156.         $hbox->pack_start($batras, FALSE, FALSE, 0);
  157.         $hbox->pack_start($belante, FALSE, FALSE, 0);
  158.         $hbox->pack_start($table,FALSE, FALSE, 0);
  159.  
  160.         # Caja Vertical
  161.  
  162.         my $vbox = Gtk3::Box->new("vertical", 0);
  163.         $vbox->pack_start($hbox, FALSE, FALSE, 0);
  164.         $vbox->pack_start($scrolls, TRUE, TRUE,  0);
  165.  
  166.     # Añadimos cajas a ventana
  167.  
  168.         $ventana->add($vbox);
  169.        
  170.         # Lo mostramos
  171.  
  172.         $ventana->show_all();
  173.      
  174.        
  175. Gtk3->main();  
  176.  
  177. ### Funciones :) ###
  178.  
  179. # Salir
  180.  
  181. sub salir {
  182. Gtk3->main_quit; # Cerramos ventana
  183. &opensettings(1,"unfullscreen"); # Cambiamos valores de conf.json
  184. }
  185.  
  186. # Adelante
  187.  
  188. sub adelante{
  189.  
  190. $ver->go_forward; # Vamos para adelante
  191.  
  192. &cargar(); # Cambiamos Notebook
  193.  
  194. }
  195.  
  196. # Atras
  197.  
  198. sub atras{
  199.  
  200. $ver->go_back; # Vamos para atras
  201.  
  202. &cargar();
  203.  
  204. }
  205.  
  206. # Entrada URL
  207.  
  208. sub entrada{
  209.  
  210. &cargar();
  211. my $uri = $entry->get_text;  # Llamamos el texto ingresado en entry
  212.  
  213.         if (substr($uri,0,7) eq "http://") { # Vemos si tiene http://  
  214.         $uri = $uri; # Si es asi,no cambiamos su valor
  215.         }
  216.     elsif(substr($uri,0,5) eq "https") {  # Vemos si tiene https
  217.         $uri = $uri; # Si es así no cambiamos su valor
  218.         }
  219.     elsif($uri =~ /\s/) { # Vemos si tiene espacios
  220.     $uri = "http://www.google.com/search?q=" . $uri; # Si es asi,buscamos en google lo ingresado
  221.     }
  222.     elsif($uri =~ /[.]/) { # Vemos si tiene puntos sin espacios
  223.     $uri = "http://" . $uri; # Si es asi,le añadimos http://
  224.     }
  225.         else { # Si es otra cosa,lo buscamos en google
  226.     $uri = "http://www.google.com/search?q=" . $uri;
  227.     }
  228.         $ver->load_uri($uri); # Cargamos el url
  229.  
  230.         $botones->set_current_page(1); # Cambiamos el boton de recargar
  231. }
  232.  
  233. # Aumentar disminuir ZOOM
  234.  
  235. sub zomin { $ver->zoom_in } # Aumentamos el zoom
  236.  
  237. sub zomout { $ver->zoom_out } # Disminuimos el zoom
  238.  
  239. # Progreso
  240.  
  241. sub progreso{
  242. &cargar();
  243. my $ctitulo;
  244. my $carga = $ver->get('progress'); # Llamos el progreso
  245. my $url_sitio = $ver->get_uri(); # Llamamos el nombre de la url
  246. $entry->set_text("$url_sitio"); # Le ponemos el nombre de la url al entry
  247.  
  248. # Creamos bucle de verificacion
  249.  
  250. for ( $ctitulo < 100 ) { # Si aun no llega a 100 la carga,entramos al bucle
  251.    my $ctitul = sprintf ("%2d%%", 100 * $carga); # Recortamos la carga
  252.    $ctitulo = chop($ctitul); # Recortamos mas
  253.    $label->set_markup("$ctitul%");  # Lo mostramos en un label
  254.    if ($ctitul == 100) { # Si ya llego a 100
  255.    $botones->set_current_page(0); # Cambiamos el notebook
  256.                 }
  257.         }
  258. }
  259.  
  260. # Titulo
  261.  
  262. sub titulo {
  263. my $titul2 = $ver->get_title(); # Llamos el titulo de la web
  264. if ($titul2){ # Si hay tal
  265. $ventana->set_title( $titul2 );} # Se lo ponemos de nombre a la ventana
  266. }
  267.  
  268. # Recargar
  269.  
  270. sub recargar {
  271. &cargar();
  272. $ver->reload; # Recargamos webview
  273. }
  274.  
  275. # Detener
  276.  
  277. sub parar {
  278. $ver->stop_loading;  # Detenemos carga de webview
  279. $botones->set_current_page(0);
  280. }
  281.  
  282. # Cargar
  283.  
  284. sub cargar { $botones->set_current_page(1); } # Cambiamos notebook
  285.  
  286. # Acerca de
  287.  
  288. sub acerca_de {
  289.  
  290. # Creamos Texto
  291.  
  292. my $texto =<<EOF;
  293.  
  294. SimplePerlBrowser
  295.  
  296. Navegador Web escrito en Perl
  297.  
  298. Por: Tiempo de Tux
  299.  
  300. Version: 4.1
  301.  
  302. EOF
  303.  
  304. # Lo mostramos en dialogo
  305.  
  306. my $dialog = Gtk3::MessageDialog->new(
  307. $ventana,'destroy-with-parent', 'info', 'close', $texto);
  308. $dialog->signal_connect( response => sub { $dialog->destroy } );
  309. $dialog->show();
  310. }
  311.  
  312. # Cargar pagina en webview
  313.  
  314. sub web {
  315. $ver->load_uri( $uri ); # Cargamos url
  316. $ver->signal_connect( 'notify::progress' => \&progreso, undef ); # Mandamos señales
  317. $ver->signal_connect( 'notify::title' => \&titulo, undef ); # A subrutinas
  318. }
  319.  
  320. # Activate acion (opciones)
  321.  
  322. sub activate_action {
  323. my $action = shift;
  324. my $name = $action->get_name; # Llamos el nombre de lo cliqueado
  325.  
  326. # Con estructiras selectivas,decidimos que hacer con ellos
  327.  
  328. if ( $name eq 'Completa' ) {
  329.         &pantallacompleta;
  330. }elsif ( $name eq 'HideTitlebar'){
  331.         my $value = $ventana->get_hide_titlebar_when_maximized();
  332. if ( $value == 0 ) { $ventana->set_hide_titlebar_when_maximized (TRUE);}
  333. else{ $ventana->set_hide_titlebar_when_maximized (FALSE); }
  334. }
  335. elsif ( $name eq 'Acerca de'){ &acerca_de(); }
  336. elsif ( $name eq 'Inicio'){ &inicioconf(); }
  337. elsif ( $name eq 'Salir'){ &salir(); }
  338. }
  339.  
  340. # Ventana de configurar pagina de inicio
  341.  
  342. sub inicioconf {
  343.         $inicio = Gtk3::Window->new('toplevel'); # Igual que la grande
  344.         $inicio->set_default_size(270, 100);
  345.         $inicio->set_title("Pagina de inicio");
  346.         $inicio->set_resizable(FALSE);
  347.         $inicio->set_border_width(5);
  348.         $inicio->set_position("center");  
  349.         $inicio->signal_connect (delete_event => sub {$inicio->destroy });
  350.         my $acinicio = Gtk3::ToolButton->new_from_stock('gtk-apply');
  351.         $acinicio->signal_connect ( "clicked" => \&confinicio, undef );
  352.         my $labl = Gtk3::Label->new("http://");
  353.         my $vcio = Gtk3::Box->new("horizontal", 0);
  354.         $vcio->pack_start($labl, FALSE, FALSE,   0);
  355.         $vcio->pack_start($entrycio, FALSE, FALSE, 0);
  356.         $vcio->pack_start($acinicio, FALSE, FALSE, 0);
  357.         $inicio->add($vcio);
  358.        
  359.         # Lo mostramos
  360.  
  361.         $inicio->show_all();  
  362. }
  363.  
  364. # Menu
  365.  
  366. sub get_ui { # Creamos menu
  367.     return "<ui>
  368.        <menubar name='MenuBar'>
  369.   <menu action='Opciones'>
  370.          <menuitem action='Completa'/>
  371.          <menuitem action='HideTitlebar'/>
  372.          <menuitem action='Inicio'/>
  373.          <menuitem action='Acerca de'/>
  374.          <menuitem action='Salir'/>
  375.   </menu>
  376. </menubar>
  377. </ui>";
  378. }
  379.  
  380. # Configuracion de pagina de inicio
  381.  
  382. sub confinicio {
  383.  
  384. my $paginicio = $entrycio->get_text; # Llamos texto ingresado
  385.  
  386. if ($paginicio ne "" ) { # Vemos si se ha ingresado,algo
  387.  
  388. # Si no esta vacio
  389.  
  390. &opensettings(0,"http://" . $paginicio);
  391.  
  392. # Mandamos esta configuracion a JSON ^
  393.  
  394. }
  395.  
  396. $inicio->destroy; # Destrimos ventana
  397. }
  398.  
  399. # Pantalla completa
  400.  
  401. sub pantallacompleta {
  402.  
  403. my $fuu = substr($unscreen,0,2); # Recortamos variable
  404.  
  405. if ($fuu eq "un") { # vemos si en conf.json esta unfull la pantalla
  406.  
  407. &opensettings(1,"fullscreen"); # si es asi lo cambiamos
  408.  
  409. }
  410. elsif ( $fuu eq "fu") { # vemos si en conf.json esta full la pantalla
  411.  
  412. &opensettings(1,"unfullscreen"); # si es asi lo cambiamos
  413.  
  414. }
  415.  
  416. else {
  417.  
  418. say "No tienes configurado\nconf.json\n"; # Si no hay archivo,o esta mal configurado
  419.  
  420. }
  421.  
  422. $ventana->$unscreen(); # Cargamos ajustes
  423.  
  424. }
  425.  
  426. # JSON
  427.  
  428. sub json {
  429.  
  430. {
  431.   local $/; # Para usar archivos locales
  432.   open my $cf, "<", "conf.json"; # Abrimos la configuracion
  433.   $json = <$cf>; # Definimos $json con el filehandle
  434.   close $cf; # Cerramos conf.json
  435. }
  436.  
  437. }
  438.  
  439. # Sub rutina de ajustes
  440.  
  441. sub opensettings {
  442.  
  443. my ($number,$setting) = @_; # Pasamos lo valores pasados del código principal
  444.  
  445. # Y otras sub rutinas a variables locales
  446.  
  447. $unscreen = $confP->{'pantalla'}->{'inicio'}->[$number] = "$setting";
  448.  
  449. # Configuramos según las variables locales ^
  450.  
  451. open my $cfn, ">", "conf.json"; # Abrimos conf.json
  452. print $cfn encode_json($confP); # Escribimos configuracion
  453. close $cfn; # Cerramos
  454.  
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement