home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / libgnome2-perl / examples / session-management.pl < prev   
Encoding:
Perl Script  |  2003-12-10  |  3.6 KB  |  138 lines

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Gnome2;
  4.  
  5. # $Header: /cvsroot/gtk2-perl/gtk2-perl-xs/Gnome2/examples/session-management.pl,v 1.1 2003/12/10 21:13:17 kaffeetisch Exp $
  6.  
  7. my $application = Gnome2::Program -> init("Test", "0.1", "libgnomeui");
  8. my $client = Gnome2::Client -> master();
  9.  
  10. ###############################################################################
  11.  
  12. $client -> signal_connect(die => sub {
  13.   # No time to save anything, just die.
  14.   Gtk2 -> main_quit();
  15. });
  16.  
  17. $client -> signal_connect(save_yourself => sub {
  18.   my ($client,
  19.       $phase,
  20.       $save_style,
  21.       $shutting_down,
  22.       $interact_style,
  23.       $fast) = @_;
  24.  
  25.   if ($fast) { # We're in a hurry, so don't do anything that takes ages.
  26.     unless (save_session_quickly()) {
  27.       error("Saving session failed.") if ($interact_style & "error");
  28.       return 0;
  29.     }
  30.   }
  31.   else { # We've plenty of time.
  32.     unless (save_session()) {
  33.       if ($interact_style & "any") {
  34.         question("Couldn't save session.  Do you want me to " .
  35.                  "delete all your personal files in response?",
  36.                  sub { delete_all_personal_files(); });
  37.       }
  38.       elsif ($interact_style & "error") {
  39.         error("Saving session failed.");
  40.       }
  41.  
  42.       return 0;
  43.     }
  44.   }
  45.  
  46.   return 1;
  47. });
  48.  
  49. ###############################################################################
  50.  
  51. my $app = Gnome2::App -> new("test", "Test");
  52. my $box = Gtk2::VBox -> new(0, 0);
  53.  
  54. my $button_die = Gtk2::Button -> new("_Die");
  55. my $button_save = Gtk2::Button -> new("_Save");
  56. my $button_save_quickly = Gtk2::Button -> new("Save quickly");
  57.  
  58. # Normally, those events are fired by the session manager when the user logs
  59. # out or kills the application via the session UI.  We emulate them here.
  60. $button_die -> signal_connect(clicked => sub {
  61.   $client -> signal_emit("die");
  62. });
  63.  
  64. $button_save -> signal_connect(clicked => sub {
  65.   $client -> request_save("local", 0, "any", 0, 0);
  66. });
  67.  
  68. $button_save_quickly -> signal_connect(clicked => sub {
  69.   $client -> request_save("local", 0, "errors", 1, 0);
  70. });
  71.  
  72. $box -> pack_start($button_die, 0, 0, 0);
  73. $box -> pack_start($button_save, 0, 0, 0);
  74. $box -> pack_start($button_save_quickly, 0, 0, 0);
  75.  
  76. $app -> set_contents($box);
  77. $app -> show_all();
  78.  
  79. $app -> signal_connect(destroy => sub {
  80.   Gtk2 -> main_quit();
  81. });
  82.  
  83. Gtk2 -> main();
  84.  
  85. ###############################################################################
  86.  
  87. sub delete_all_personal_files {
  88.   $| = 1;
  89.   print "Deleting all personal files ...";
  90.   select(undef, undef, undef, 0.25);
  91.   print " done.\n";
  92. }
  93.  
  94. sub error {
  95.   my ($label) = @_;
  96.  
  97.   my $dialog = Gtk2::MessageDialog -> new($app,
  98.                                           [qw(modal destroy-with-parent)],
  99.                                           "error",
  100.                                           "ok",
  101.                                           $label);
  102.  
  103.   $dialog -> signal_connect(response => sub {
  104.     my ($dialog, $response) = @_;
  105.     $dialog -> hide();
  106.   });
  107.  
  108.   $client -> save_error_dialog($dialog);
  109. }
  110.  
  111. sub question {
  112.   my ($label, $callback) = @_;
  113.  
  114.   my $dialog = Gtk2::MessageDialog -> new($app,
  115.                                           [qw(modal destroy-with-parent)],
  116.                                           "question",
  117.                                           "yes-no",
  118.                                           $label);
  119.  
  120.   $dialog -> signal_connect(response => sub {
  121.     my ($dialog, $response) = @_;
  122.     $callback -> () if ($response eq "yes");
  123.     $dialog -> hide();
  124.   });
  125.  
  126.   $client -> save_any_dialog($dialog);
  127. }
  128.  
  129. sub save_session {
  130.   select(undef, undef, undef, 0.5);
  131.   return int(rand(2));
  132. }
  133.  
  134. sub save_session_quickly {
  135.   select(undef, undef, undef, 0.1);
  136.   return int(rand(2));
  137. }
  138.