Advertisement
hackloper775

PerlBrowser 4.5

Jan 10th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 16.67 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.5
  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.  
  31.         use Gtk3::WebKit;      # Llamamos a webkit
  32.        
  33.         # Usar icono.xpm
  34.  
  35.         my $ventana = Gtk3::Window->new('toplevel'); # Definimos ventana principal
  36.         my $icono = 'icon.xpm'; # Definimos icono
  37.         if ( -e $icono ) { # Vemos si existe el icono
  38.                 $ventana->set_icon_from_file($icono); # Si es asi,se lo colomos a la ventana
  39.         }
  40.  
  41.         # Parametros de la ventana
  42.  
  43.         my $titulo = "Simple Perl Browser"; # Titulo de la ventana
  44.         $ventana->set_default_size(800, 600); # Tamaño por defecto
  45.     $ventana->maximize(TRUE);  #  Maximizar al abrir
  46.         $ventana->set_title($titulo);  # Colocamos titulo
  47.         $ventana->set_border_width(2); # Añadimos bordes
  48.         $ventana->$unscreen(); # No fullscreen por defecto
  49.         $ventana->signal_connect(delete_event => \&salir); # Si recibe señal de X i a &salir
  50.     my $inicio; # Ventana de configuracion de inicio
  51.  
  52.         # Opciones
  53.  
  54.         my $table = Gtk3::Grid->new; # Creamos tabla con Grid
  55.  
  56.     # Opciones nulas
  57.     # Arrays multidimensionales
  58.  
  59.         my @entries = (
  60.         [ "Opciones", undef, "_Opciones" ],
  61.         ["Acerca de", 'gtk-about', "_Acerca de", '<control>B', "Acerca de", \&activate_action ],
  62.         ["Configuracion", 'gtk-properties', "_Configuracion",'<control>S', "Configuracion", \&activate_action ],
  63.         ["Salir", 'gtk-quit', "_Salir", '<control>Q', "Salir", \&activate_action ]
  64.         );
  65.  
  66.     # Opciones cambiantes
  67.     # Arrays multidimensionales
  68.  
  69.         my @toggle_entries = (                      
  70.         ["Completa",undef, "_Pantalla Completa", undef,    
  71.         "Pantalla completa", \&activate_action, FALSE ],                              
  72.         ["HideTitlebar", undef, "_Hide Titlebar", undef,
  73.         "Hide Titlebar",  \&activate_action,FALSE]
  74.         );
  75.    
  76.     # Las metemos en un grupo
  77.  
  78.         my $action_group = Gtk3::ActionGroup->new('AppWindowActions');
  79.         $action_group->add_actions( \@entries, undef );
  80.         $action_group->add_toggle_actions( \@toggle_entries, undef );
  81.  
  82.     # Creamos un UIManager y insertamos el grupo
  83.  
  84.         my $ui = Gtk3::UIManager->new();
  85.         $ui->insert_action_group( $action_group, 0 );
  86.         $ventana->add_accel_group( $ui->get_accel_group );
  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.         $entry->set_icon_from_stock( 'secondary', 'gtk-apply' );
  112.         $entry->signal_connect( 'icon-press' => \&entrada, undef );
  113.         $entrycio->signal_connect( "activate" => \&confinicio, undef );
  114.  
  115.         # Botones
  116.  
  117.         # Creamos y labels
  118.  
  119.         my $brg = Gtk3::ToolButton->new_from_stock('gtk-quit');
  120.         my $fresh = Gtk3::ToolButton->new_from_stock('gtk-refresh');
  121.         my $frenar = Gtk3::ToolButton->new_from_stock('gtk-stop');    
  122.         my $batras = Gtk3::ToolButton->new_from_stock('gtk-go-back');
  123.         my $belante = Gtk3::ToolButton->new_from_stock('gtk-go-forward');
  124.         my $vermenos = Gtk3::ToolButton->new_from_stock('gtk-zoom-in');
  125.         my $vermas = Gtk3::ToolButton->new_from_stock('gtk-zoom-out');
  126.         my $label = Gtk3::Label->new("");
  127.         my $spinner = Gtk3::Spinner->new();
  128.  
  129.     # Desviamos señales de botones a subrutinas
  130.  
  131.         $fresh->signal_connect ( "clicked" => \&recargar );    
  132.         $frenar->signal_connect ( "clicked" => \&parar );      
  133.         $batras->signal_connect( "clicked" => \&atras );
  134.         $belante->signal_connect ( "clicked" => \&adelante );
  135.         $vermenos->signal_connect ( "clicked" => \&zomin );
  136.         $vermas->signal_connect ( "clicked" => \&zomout);
  137.  
  138.         # Frenar y recargar unificados
  139.  
  140.         my $botones = Gtk3::Notebook->new(); # Creamos Notebook
  141.         $botones->set_show_tabs(FALSE);
  142.         $botones->set_show_border(FALSE); # No mostramos bordes
  143.         $botones->append_page($fresh); # Añadimos botones
  144.         $botones->append_page($frenar);
  145.        
  146.         # Unimos todo
  147.    
  148.         # Caja horizontal
  149.  
  150.         my $hbox = Gtk3::Box->new("horizontal", 0);
  151.         $hbox->pack_start($entry, TRUE,TRUE , 0);
  152.         $hbox->pack_start($botones, FALSE,FALSE , 0);
  153.         $hbox->pack_start($label, FALSE,FALSE , 5);
  154.         $hbox->pack_start($spinner, FALSE,FALSE , 5);
  155.         $hbox->pack_start($vermas, FALSE,FALSE , 0);
  156.         $hbox->pack_start($vermenos, FALSE,FALSE , 0);
  157.         $hbox->pack_start($batras, FALSE, FALSE, 0);
  158.         $hbox->pack_start($belante, FALSE, FALSE, 0);
  159.         $hbox->pack_start($table,FALSE, FALSE, 0);
  160.  
  161.         # Caja Vertical
  162.  
  163.         my $vbox = Gtk3::Box->new("vertical", 0);
  164.         $vbox->pack_start($hbox, FALSE, FALSE, 0);
  165.         $vbox->pack_start($scrolls, TRUE, TRUE,  0);
  166.  
  167.     # Añadimos cajas a ventana
  168.  
  169.         $ventana->add($vbox);
  170.        
  171.         # Lo mostramos
  172.  
  173.         $ventana->show_all();
  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. &json; # Abrimos .json
  212.  
  213. my $motorb = $confP->{'pantalla'}->{'inicio'}->[2]; # Definimos el motor de busqueda
  214.  
  215. my $uri = $entry->get_text;  # Llamamos el texto ingresado en entry
  216.  
  217. if (substr($uri,0,7) eq "http://") { # Vemos si tiene http://  
  218.         $uri = $uri; # Si es asi,no cambiamos su valor
  219.     }
  220.     elsif(substr($uri,0,5) eq "https") {  # Vemos si tiene https
  221.         $uri = $uri; # Si es asi no cambiamos si valor
  222.     }
  223.     elsif($uri =~ /\s/) { # Vemos si tiene espacios
  224.     $uri = "$motorb" . "$uri";
  225.     } # Si es asi,buscamos en el motor de busqueda
  226.     elsif($uri =~ /[.]/) { # Vemos si tiene puntos sin espacios
  227.     $uri = "http://" . $uri; # Si es asi,le añadimos http://
  228.     }
  229.     else { # Si es otra cosa,lo buscamos en el motor de busqueda
  230.     $uri = "$motorb" . "$uri";
  231. }
  232. $ver->load_uri($uri); # Cargamos el url
  233. $botones->set_current_page(1); # Cambiamos el boton de recargar
  234. }
  235.  
  236. # Aumentar disminuir ZOOM
  237.  
  238. sub zomin { $ver->zoom_in } # Aumentamos el zoom
  239.  
  240. sub zomout { $ver->zoom_out } # Disminuimos el zoom
  241.  
  242. # Progreso
  243.  
  244. sub progreso{
  245. &cargar();
  246. my $ctitulo;
  247. my $carga = $ver->get('progress'); # Llamos el progreso
  248. $spinner->start(); # Iniciamos spiner
  249.  
  250. # Creamos bucle de verificacion
  251.  
  252. for ( $ctitulo < 100 ) { # Si aun no llega a 100 la carga,entramos al bucle
  253.    my $ctitul = sprintf ("%2d%%", 100 * $carga); # Recortamos la carga
  254.    $label->set_markup("$ctitul");  # Lo mostramos en un label
  255.    if ($ctitul == 100) { # Si ya llego a 100
  256.    $botones->set_current_page(0); # Cambiamos el notebook
  257.    $spinner->stop(); # Detenemos spiner
  258.  
  259.                 }
  260.         }
  261. }
  262.  
  263. # Titulo
  264.  
  265. sub titulo {
  266. my $titul2 = $ver->get_title(); # Llamos el titulo de la web
  267. my $url_sitio = $ver->get_uri(); # Llamamos el nombre de la url
  268. if ($titul2){ # Si hay tal
  269. $ventana->set_title( $titul2 );} # Se lo ponemos de nombre a la ventana
  270. $entry->set_text("$url_sitio"); # Le ponemos el nombre de la url al entry
  271. }
  272.  
  273. # Recargar
  274.  
  275. sub recargar {
  276. &cargar();
  277. $ver->reload; # Recargamos webview
  278. }
  279.  
  280. # Detener
  281.  
  282. sub parar {
  283. $ver->stop_loading;  # Detenemos carga de webview
  284. $botones->set_current_page(0);
  285. }
  286.  
  287. # Cargar
  288.  
  289. sub cargar { $botones->set_current_page(1); } # Cambiamos notebook
  290.  
  291. # Acerca de
  292.  
  293. sub acerca_de {
  294.  
  295. # Creamos Texto
  296.  
  297. my $texto =<<EOF;
  298.  
  299. SimplePerlBrowser
  300.  
  301. Navegador Web escrito en Perl
  302.  
  303. Por: Tiempo de Tux
  304.  
  305. Version: 4.5
  306.  
  307. EOF
  308.  
  309. # Lo mostramos en dialogo
  310.  
  311. my $dialog = Gtk3::MessageDialog->new(
  312. $ventana,'destroy-with-parent', 'info', 'close', $texto);
  313. $dialog->signal_connect( response => sub { $dialog->destroy } );
  314. $dialog->show();
  315. }
  316.  
  317. # Cargar pagina en webview
  318.  
  319. sub web {
  320. $ver->load_uri( $uri ); # Cargamos url
  321. $ver->signal_connect( 'notify::progress' => \&progreso, undef ); # Mandamos señales
  322. $ver->signal_connect( 'notify::title' => \&titulo, undef ); # A subrutinas
  323. }
  324.  
  325. # Activate acion (opciones)
  326.  
  327. sub activate_action {
  328. my $action = shift;
  329. my $name = $action->get_name; # Llamos el nombre de lo cliqueado
  330.  
  331. # Con estructiras selectivas,decidimos que hacer con ellos
  332.  
  333. if ( $name eq 'Completa' ) {
  334.         &pantallacompleta;
  335. }elsif ( $name eq 'HideTitlebar'){
  336.         my $value = $ventana->get_hide_titlebar_when_maximized();
  337.     if ( $value == 0 ) {
  338.     $ventana->set_hide_titlebar_when_maximized (TRUE);
  339.     }
  340.     else{ $ventana->set_hide_titlebar_when_maximized (FALSE); }
  341.     }
  342. elsif ( $name eq 'Acerca de'){ &acerca_de(); }
  343. elsif ( $name eq 'Configuracion'){ &buscador_actual(); }
  344. elsif ( $name eq 'Salir'){ &salir(); }
  345. }
  346.  
  347. # Ventana de configurar pagina de inicio
  348.  
  349. # Checar motor de busqueda actual
  350.  
  351. sub buscador_actual {
  352.  
  353. &json; # Lo abrimos cada que vez que se necesite
  354.  
  355. my $data; # Definimos una variable que llenaremos mas adelante
  356.  
  357. my $buscador_actual = $confP->{'pantalla'}->{'inicio'}->[2]; # Checamos el motor de busqueda
  358.  
  359. # Buscamos el motor
  360.  
  361. if ( $buscador_actual =~ m/google/ ) {
  362.  
  363. $data = "Google"; # Y llenamos la variable con el motor
  364.  
  365. } elsif ( $buscador_actual =~ m/bing/) {
  366.  
  367. $data = "Bing";
  368.  
  369. } elsif ( $buscador_actual =~ m/duck/) {
  370.  
  371. $data = "Duckduckgo";
  372.  
  373. } else {
  374.  
  375. $data = "No tienes configurado JSON";
  376.  
  377. }
  378.  
  379. &inicioconf($data); # Vamos a la pantalla de configuracion y le pasamos el motor
  380.  
  381. }
  382.  
  383. sub inicioconf {
  384.  
  385.     my ($motor_actual) = @_; # Recibimos el motor
  386.  
  387.         $inicio = Gtk3::Window->new('toplevel'); # Igual que la grande
  388.  
  389.         # Ventana
  390.        
  391.         $inicio->set_default_size(280, 100);
  392.         $inicio->set_title("Pagina de inicio");
  393.         $inicio->set_resizable(FALSE);
  394.         $inicio->set_border_width(5);
  395.         $inicio->set_position("center");  
  396.         $inicio->set_decorated(FALSE);
  397.         $inicio->signal_connect (delete_event => sub {$inicio->destroy });
  398.        
  399.         # Dentro del box
  400.        
  401.         $entrycio->set_icon_from_stock( 'secondary', 'gtk-apply' );
  402.         my $config = Gtk3::RadioButton->new_with_label_from_widget(undef, "Actual: $motor_actual");
  403.         my $config0 = Gtk3::RadioButton->new_with_label_from_widget($config, "Google");
  404.         my $config1 = Gtk3::RadioButton->new_with_label_from_widget($config, "Duckduckgo");
  405.     my $config2 = Gtk3::RadioButton->new_with_label_from_widget($config, "Bing");
  406.         my $separador0 = Gtk3::Separator->new('horizontal');
  407.         my $separador1 = Gtk3::Separator->new('horizontal');
  408.         my $separador2 = Gtk3::Separator->new('horizontal');
  409.         my $labl0 = Gtk3::Label->new("Opciones");
  410.         my $labl1 = Gtk3::Label->new("http://");
  411.         my $labl2 = Gtk3::Label->new("Pagina de inicio");
  412.         my $labl3 = Gtk3::Label->new("Buscador");
  413.         my $ok = Gtk3::Button->new_with_label("Aceptar");
  414.         my $cerrar = Gtk3::Button->new_with_label("Cerrar");
  415.  
  416.     # Señales
  417.  
  418.         $entrycio->signal_connect( 'icon-press' => \&confinicio );
  419.     $config0->signal_connect (toggled => \&confnav, "Google"); #
  420.     $config1->signal_connect (toggled => \&confnav, "Duck");   # Cuando recibamos un motor vamos a
  421.     $config2->signal_connect (toggled => \&confnav, "Bing");   # confnav
  422.     $cerrar->signal_connect ( "clicked" => sub {$inicio->destroy });
  423.     $ok->signal_connect ( "clicked" => sub {$inicio->destroy });
  424.  
  425.         &seticio; # Colocamos el motor nuevo
  426.  
  427.         my $hcio = Gtk3::Box->new("horizontal", 0);
  428.         $hcio->pack_start($labl1, FALSE, FALSE, 0);
  429.         $hcio->pack_start($entrycio, FALSE, FALSE, 0);
  430.        
  431.         my $ayc = Gtk3::Box->new("horizontal", 0);
  432.         $ayc->pack_start($ok, FALSE, FALSE, 30);
  433.         $ayc->pack_start($cerrar, FALSE, FALSE, 30);
  434.        
  435.         my $vcio = Gtk3::Box->new("vertical", 2);
  436.         $vcio->pack_start($labl0, FALSE, FALSE, 0);
  437.         $vcio->pack_start($separador0, FALSE, FALSE, 0);
  438.         $vcio->pack_start($labl2, FALSE, FALSE, 0);
  439.         $vcio->pack_start($hcio, FALSE, FALSE, 2);
  440.         $vcio->pack_start($separador1, FALSE, FALSE, 5);
  441.         $vcio->pack_start($labl3, FALSE, FALSE, 5);
  442.         $vcio->pack_start($config, FALSE, FALSE, 5);
  443.         $vcio->pack_start($config0, FALSE, FALSE, 5);
  444.         $vcio->pack_start($config1, FALSE, FALSE, 5);
  445.         $vcio->pack_start($config2, FALSE, FALSE, 5);
  446.         $vcio->pack_start($separador2, FALSE, FALSE, 5);
  447.         $vcio->pack_start($ayc, FALSE, FALSE, 5);
  448.              
  449.         $inicio->add($vcio);
  450.        
  451.         # Lo mostramos
  452.  
  453.         $inicio->show_all();  
  454. }
  455.  
  456. # Menu
  457.  
  458. sub get_ui { # Creamos menu
  459.     return "<ui>
  460.        <menubar name='MenuBar'>
  461.   <menu action='Opciones'>
  462.          <menuitem action='Completa'/>
  463.          <menuitem action='HideTitlebar'/>
  464.          <menuitem action='Configuracion'/>
  465.          <menuitem action='Acerca de'/>
  466.          <menuitem action='Salir'/>
  467.   </menu>
  468. </menubar>
  469. </ui>";
  470. }
  471.  
  472. # Configuracion de pagina de inicio
  473.  
  474. sub confinicio {
  475.  
  476. my $paginicio = $entrycio->get_text; # Llamos texto ingresado
  477.  
  478. if (substr($paginicio,0,7) eq 'http://') { # Si no tiene http://
  479.    
  480.     &opensettings(0,$paginicio); # Si tiene se lo mandamos a la configuracion
  481.    
  482. }
  483.  
  484. # Si no encontro http:// pasamos a elsif
  485.  
  486. elsif ($paginicio ne "" ) { # Vemos si se ha ingresado,algo
  487.  
  488. # Si no esta vacio
  489.  
  490. &opensettings(0,"http://" . $paginicio);
  491.  
  492. # Mandamos esta configuracion a JSON ^
  493.  
  494. }
  495.  
  496. }
  497.  
  498. # Pantalla completa
  499.  
  500. sub pantallacompleta {
  501.  
  502. my $fuu = substr($unscreen,0,2); # Recortamos variable
  503.  
  504. if ($fuu eq "un") { # vemos si en conf.json esta unfull la pantalla
  505.  
  506. &opensettings(1,"fullscreen"); # si es asi lo cambiamos
  507.  
  508. }
  509. elsif ( $fuu eq "fu") { # vemos si en conf.json esta full la pantalla
  510.  
  511. &opensettings(1,"unfullscreen"); # si es asi lo cambiamos
  512.  
  513. }
  514.  
  515. else {
  516.  
  517. say "No tienes configurado\nconf.json\n"; # Si no hay archivo,o esta mal configurado
  518.  
  519. }
  520.  
  521. $ventana->$unscreen(); # Cargamos ajustes
  522.  
  523. }
  524.  
  525. # JSON
  526.  
  527. sub json {
  528.  
  529. {
  530.   local $/; # Para usar archivos locales
  531.   open my $cf, "<", "conf.json"; # Abrimos la configuracion
  532.   $json = <$cf>; # Definimos $json con el filehandle
  533.   close $cf; # Cerramos conf.json
  534. }
  535.  
  536. }
  537.  
  538. # Colocar uri de pagina de inicio en el entry de confinicio
  539.  
  540. sub seticio {
  541.  
  542. my @text = split(m/^http[:]\/\//,$iniP); # Le quitamos el http://
  543.  
  544. $entrycio->set_text($text[1]);
  545.    
  546. }
  547.  
  548. # Configurar motor de busqueda
  549.  
  550. sub confnav {
  551.    
  552. my ($widget,$buscador) = @_;
  553.  
  554. # Motores de busqueda
  555.  
  556. my @motores = (
  557.  
  558. "http://www.bing.com/search?q=",
  559.  
  560. "https://duckduckgo.com/?q=",
  561.  
  562. "http://www.google.com/search?q="
  563.  
  564. );
  565.  
  566. if ($buscador eq 'Bing') {
  567.     &opensettings(2,$motores[0]);
  568. }
  569. elsif($buscador eq 'Duck' ) {
  570.     &opensettings(2,$motores[1]);
  571. }
  572. elsif ($buscador eq 'Google' ){
  573.     &opensettings(2,$motores[2]);
  574. }
  575.  
  576. }
  577.  
  578. # Sub rutina de ajustes
  579.  
  580. sub opensettings {
  581.  
  582. my ($number,$setting) = @_; # Pasamos lo valores pasados del codigo principal a variables locales
  583.  
  584. $unscreen = $confP->{'pantalla'}->{'inicio'}->[$number] = "$setting";
  585.  
  586. # Configuramossegun las variables locales ^
  587.  
  588. open my $cfn, ">", "conf.json"; # Abrimos conf.json
  589. print $cfn encode_json($confP); # Escribimos configuracion
  590. close $cfn; # Cerramos
  591.  
  592. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement