?? sec-sessionmanagement.html
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <title> Session Management </title> <meta name="GENERATOR" content= "Modular DocBook HTML Stylesheet Version 1.45"> <link rel="HOME" title="GTK+ / Gnome Application Development" href="ggad.html"> <link rel="UP" title="Gnome Application Basics" href= "cha-startup.html"> <link rel="PREVIOUS" title="Saving Configuration Information" href="z79.html"> <link rel="NEXT" title="The Main Window: GnomeApp" href= "cha-main.html"> </head> <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink= "#840084" alink="#0000FF"> <div class="NAVHEADER"> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <th colspan="4" align="center"> <font color="#000000" size="2">GTK+ / Gnome Application Development</font> </th> </tr> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="z79.html"><font color="#0000ff" size="2"><b> <<< Previous</b></font></a> </td> <td width="25%" colspan="2" bgcolor="#ffffff" align= "center"> <font color="#0000ff" size="2"><b><a href="ggad.html"> <font color="#0000ff" size="2"><b> Home</b></font></a></b></font> </td> <td width="25%" bgcolor="#ffffff" align="right"> <a href="cha-main.html"><font color="#0000ff" size="2"> <b>Next >>></b></font></a> </td> </tr> </table> </div> <div class="SECT1"> <h1 class="SECT1"> <a name="SEC-SESSIONMANAGEMENT">Session Management</a> </h1> <p> The term <i class="FIRSTTERM">session</i> refers to a snapshot of the state of a user's desktop: what applications are open, where their windows are located, what windows each application has open, what size those windows are, what documents are open, current cursor position, and so on. Users should be able to save their session before logging out, and have it automatically restored as closely as possible next time they log in. For this to work, applications must cooperate by having the ability to record and restore those aspects of their state not controlled by the window manager. </p> <p> A special program called the <i class="FIRSTTERM">session manager</i> notifies applications when they should save their state. The Gnome desktop environment comes with a session manager called <tt class="APPLICATION"> gnome-session</tt>, but Gnome uses the X session management specification, which is several years old. CDE uses the same specification, and at press time, KDE was planning to adopt it as well; an application that implements session management via the Gnome interfaces should work on any session-managed desktop. Gnome does implement some extensions to the basic specification (notably, startup "priorities") but these should not break other session managers and will likely be implemented in KDE as well. </p> <p> It's worthwhile to read the session management documentation that comes with X; it's a good introduction to what's going on "behind the scenes." The Gnome libraries also come with a useful document, called <tt class= "FILENAME">session-management.txt</tt>; have a look at it, and the heavily-commented <tt class="FILENAME"> gnome-client.h</tt> header file, for additional details not covered in this section. </p> <div class="SECT2"> <h2 class="SECT2"> <a name="Z86">Using the <span class="STRUCTNAME"> GnomeClient</span> Object</a> </h2> <p> Gnome shields you from the raw session management interface that comes with X. This is done via a <span class="STRUCTNAME">GtkObject</span> called <span class= "STRUCTNAME">GnomeClient</span>. <span class= "STRUCTNAME">GnomeClient</span> represents your application's connection to the session manager. </p> <p> Gnome manages most of the details of session management. For most applications, you only have to respond to two requests. </p> <ul> <li> <p> When a session is saved, the session manager will ask each client to save enough information to restore its state the next time the user logs in. Your application should save as much interesting state as possible: the current open documents, cursor position, command histories, and so on. Applications should <i class="EMPHASIS">not</i> save their current window geometries; the window manager is responsible for that. </p> </li> <li> <p> Sometimes the session manager will ask your client to shut down and exit (typically when the user logs out). When you receive this request you should do whatever is necessary to exit the application. </p> </li> </ul> <p> When the session manager requests action from your application, a the <span class="STRUCTNAME"> GnomeClient</span> object emits an appropriate signal. The two important signals are <span class="SYMBOL"> "save_yourself"</span> and <span class="SYMBOL"> "die"</span>. <span class="SYMBOL">"save_yourself"</span> is emitted when an application should save its state, and <span class="SYMBOL">"die"</span> is emitted when an application should exit. A <span class="SYMBOL"> "save_yourself"</span> callback is fairly complex and has quite a few arguments; a <span class="SYMBOL"> "die"</span> callback is trivial. </p> <p> GnomeHello obtains a pointer to the <span class= "STRUCTNAME">GnomeClient</span> object and connects to its signals as follows: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> client = gnome_master_client (); gtk_signal_connect (GTK_OBJECT (client), "save_yourself", GTK_SIGNAL_FUNC (save_session), argv[0]); gtk_signal_connect (GTK_OBJECT (client), "die", GTK_SIGNAL_FUNC (session_die), NULL); </pre> </td> </tr> </table> <p> <span class="STRUCTNAME">argv[0]</span> will be used in the <span class="SYMBOL">"save_yourself"</span> callback. </p> <p> First, here's the <span class="SYMBOL">"die"</span> callback from GnomeHello: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> static voidsession_die(GnomeClient* client, gpointer client_data){ gtk_main_quit ();} </pre> </td> </tr> </table> <p> Straightforward; the application just exits. </p> <p> Now the <span class="SYMBOL">"save_yourself"</span> callback: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> static gintsave_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint is_shutdown, GnomeInteractStyle interact_style, gint is_fast, gpointer client_data){ gchar** argv; guint argc; /* allocate 0-filled, so it will be NULL-terminated */ argv = g_malloc0(sizeof(gchar*)*4); argc = 1; argv[0] = client_data; if (message) { argv[1] = "--message"; argv[2] = message; argc = 3; } gnome_client_set_clone_command (client, argc, argv); gnome_client_set_restart_command (client, argc, argv); return TRUE;} </pre> </td> </tr> </table> <p> This is a bit more complex. A <span class="SYMBOL"> "save_yourself"</span> must tell the session manager how to restart and "clone" (create a new instance of) the application. The restarted application should remember as much state as possible; in GnomeHello's case, it will remember the message being displayed. The simplest way to store application state is to generate a command line, as GnomeHello does. It's also possible to ask <span class= "STRUCTNAME">GnomeClient</span> for a prefix to be used with the gnome-config API; you can then save information to a per-session configuration file. Applications with significant state will need to use this method. </p> </div> </div> <div class="NAVFOOTER"> <br> <br> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="z79.html"><font color="#0000ff" size="2"><b> <<< Previous</b></font></a> </td> <td width="25%" colspan="2" bgcolor="#ffffff" align= "center"> <font color="#0000ff" size="2"><b><a href="ggad.html"> <font color="#0000ff" size="2"><b> Home</b></font></a></b></font> </td> <td width="25%" bgcolor="#ffffff" align="right"> <a href="cha-main.html"><font color="#0000ff" size="2"> <b>Next >>></b></font></a> </td> </tr> <tr> <td colspan="2" align="left"> <font color="#000000" size="2"><b>Saving Configuration Information</b></font> </td> <td colspan="2" align="right"> <font color="#000000" size="2"><b>The Main Window: <tt class="CLASSNAME">GnomeApp</tt></b></font> </td> </tr> </table> </div> </body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -