⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 z79.html

📁 GTK+_ Gnome Application Development
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>  <head>    <title>      Saving Configuration Information    </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="Argument Parsing with popt" href=     "z77.html">    <link rel="NEXT" title="Session Management" href=     "sec-sessionmanagement.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="z77.html"><font color="#0000ff" size="2"><b>            &lt;&lt;&lt; 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="sec-sessionmanagement.html"><font color=             "#0000ff" size="2"><b>Next &gt;&gt;&gt;</b></font></a>          </td>        </tr>      </table>    </div>    <div class="SECT1">      <h1 class="SECT1">        <a name="Z79">Saving Configuration Information</a>      </h1>      <p>        <tt class="APPLICATION">libgnome</tt> comes with the        ability to store simple key-value pairs in plain text        configuration files. Convenience routines are provided for        numeric and boolean types which transparently convert to        and from a text representation of each type. The standard        location for Gnome configuration files is <tt class=         "FILENAME">~/.gnome</tt>, and the library will use that        location by default. However, the library can be used with        any file. There are also variants of each function which        save to <tt class="FILENAME">~/.gnome_private</tt>, a        directory with user permissions only. The basic functions        to store and retrieve data are listed in <a href=         "z79.html#STARTUP-GNOMECONFIGGET">Figure 4 in the section        called <i>Reading Stored Config Data</i></a> and <a href=         "z79.html#STARTUP-GNOMECONFIGSET">Figure 5 in the section        called <i>Storing Data In Configuration Files</i></a>. This        module of <tt class="APPLICATION">libgnome</tt> is often        referred to as <tt class="APPLICATION">gnome-config</tt>.        Don't confuse this usage of "gnome-config" with the <tt        class="APPLICATION">gnome-config</tt> script that reports        the compile and link flags for Gnome programs.      </p>      <p>        The <tt class="APPLICATION">gnome-config</tt> functions        work with a <i class="FIRSTTERM">path</i>. A path has three        components:      </p>      <ul>        <li>          <p>            The <i class="FIRSTTERM">filename</i> to use,            underneath the <tt class="FILENAME">~/.gnome</tt> or            <tt class="FILENAME">~/.gnome_private</tt> directory.            By convention this is the name of your application.            &#13;          </p>        </li>        <li>          <p>            A <i class="FIRSTTERM">section</i>---a logical            subcategory of related configuration information.&#13;          </p>        </li>        <li>          <p>            A <i class="FIRSTTERM">key</i>---the key half of a            key-value pair. The key is actually associated with a            piece of configuration data.&#13;          </p>        </li>      </ul>      <p>        A path is passed to Gnome as a string, with the form <tt        class="APPLICATION">"/filename/section/key"</tt>. If you        want to use a filename which is <i class="EMPHASIS">not</i>        in the standard Gnome directories, you can bracket the        entire path with the <tt class="APPLICATION">'='</tt>        character and it will be interpreted as absolute. You can        even use this as a simple datafile format (it is used for        the <tt class="APPLICATION">.desktop</tt> files programs        install in order to appear on the Gnome panel menu).        However, XML (perhaps using the <tt class="APPLICATION">        gnome-xml</tt> package) is almost certainly a better choice        for that. XML may also be a better choice for storing some        kinds of configuration information; the primary advantage        of the <tt class="APPLICATION">libgnome</tt> configuration        library is its simplicity.      </p>      <p>        <tt class="APPLICATION">gnome-config</tt> has a long        history; it was first written for the WINE Windows emulator        project, then used in the GNU Midnight Commander file        manager, and finally migrated into the Gnome libraries. The        plan is to replace <tt class="APPLICATION">        gnome-config</tt> with something more powerful in the next        version of Gnome; we want to support per-host        configuration, backends such as LDAP, and other features.        However, the <tt class="APPLICATION">gnome-config</tt> API        will almost certainly be supported even if the underlying        engine changes dramatically.      </p>      <div class="SECT2">        <h2 class="SECT2">          <a name="Z80">Reading Stored Config Data</a>        </h2>        <p>          Retrieving data from files is simple. You simply call a          function to retrieve the value for a given key. The          value-retrieving functions (shown in <a href=           "z79.html#STARTUP-GNOMECONFIGGET">Figure 4</a>) accept a          path as their argument. For example, you might ask          whether the user wants to see a dialog box:        </p>        <table border="0" bgcolor="#E0E0E0" width="100%">          <tr>            <td><pre class="PROGRAMLISTING">&#13;  gboolean show_dialog;  show_dialog =     gnome_config_get_bool("/myapp/General/dialog"); &#13;</pre>            </td>          </tr>        </table>        <p>          If the config file doesn't exist yet, or there is no key          matching the path you provide, these functions return 0,          <span class="STRUCTNAME">FALSE</span>, or <span class=           "STRUCTNAME">NULL</span>. The functions that return a          string return allocated memory; you should <tt class=           "FUNCTION">g_free()</tt> the returned string. The string          vector functions return an allocated vector full of          allocated strings (<tt class="FUNCTION">g_strfreev()</tt>          is the easiest way to free this vector).        </p>        <p>          You can specify a default value to be returned if the key          does not exist; to do so, append an <span class=           "STRUCTNAME">"=value"</span> to the path. For example:        </p>        <table border="0" bgcolor="#E0E0E0" width="100%">          <tr>            <td><pre class="PROGRAMLISTING">&#13;  gboolean show_dialog;  show_dialog =     gnome_config_get_bool("/myapp/General/dialog=true"); &#13;</pre>            </td>          </tr>        </table>        <p>          Each function has a <span class="STRUCTNAME">          with_default</span> variant; these tell you whether the          return value was taken from a config file or from the          default you specified. For example:        </p>        <table border="0" bgcolor="#E0E0E0" width="100%">          <tr>            <td><pre class="PROGRAMLISTING">&#13;  gboolean show_dialog;  gboolean used_default;  show_dialog =     gnome_config_get_bool_with_default("/myapp/General/dialog=true",                                        &amp;used_default);   if (used_default)    printf("Default value used for show_dialog\n");&#13;</pre>            </td>          </tr>        </table>        <p>          <tt class="FUNCTION">gnome_config_push_prefix()</tt> and          <tt class="FUNCTION">gnome_config_pop_prefix()</tt> (in          <a href="z79.html#STARTUP-GNOMECONFIGMISC">Figure 7 in          the section called <i>Other Config File          Operations</i></a>) can be used to avoid specifying the          entire path each time. For example:        </p>        <table border="0" bgcolor="#E0E0E0" width="100%">          <tr>            <td><pre class="PROGRAMLISTING">&#13;  gboolean show_dialog;  gnome_config_push_prefix("/myapp/General/");  show_dialog =     gnome_config_get_bool("dialog=true");   gnome_config_pop_prefix();&#13;</pre>            </td>          </tr>        </table>        <p>          These functions also work when saving values.        </p>        <p>          The configuration functions with <span class=          "STRUCTNAME">private</span> in their name use a <tt          class="FILENAME">.gnome_private</tt> directory with          restricted permissions, as discussed above. The <span          class="STRUCTNAME">translated_string</span> functions          qualify the provided key with the name of the current          locale; these are used when Gnome reads <tt class=           "APPLICATION">.desktop</tt> files (see <a href=           "z72.html#SEC-.DESKTOP">the section called <i><tt class=           "APPLICATION">.desktop</tt> Entries</i> in the chapter          called <i>Creating Your Source Tree</i></a>) and are          probably not useful to applications.        </p>        <div class="FIGURE">          <a name="STARTUP-GNOMECONFIGGET"></a>          <div class="FUNCSYNOPSIS">            <a name="STARTUP-GNOMECONFIGGET.SYNOPSIS"></a>            <table border="0" bgcolor="#E0E0E0" width="100%">              <tr>                <td><pre class="FUNCSYNOPSISINFO">#include &lt;libgnome/gnome-config.h&gt;</pre>                </td>              </tr>            </table>            <p>              <code><code class="FUNCDEF">gchar* <tt class=              "FUNCTION">gnome_config_get_string</tt></code>(const              gchar* <tt class="PARAMETER"><i>              path</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">gchar* <tt class=              "FUNCTION">              gnome_config_get_translated_string</tt></code>(const              gchar* <tt class="PARAMETER"><i>              path</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">gint <tt class=              "FUNCTION">gnome_config_get_int</tt></code>(const              gchar* <tt class="PARAMETER"><i>              path</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">gdouble <tt class=               "FUNCTION">gnome_config_get_float</tt></code>(const              gchar* <tt class="PARAMETER"><i>              path</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">gboolean <tt class=               "FUNCTION">gnome_config_get_bool</tt></code>(const              gchar* <tt class="PARAMETER"><i>              path</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">void <tt class=              "FUNCTION">gnome_config_get_vector</tt></code>(const              gchar* <tt class="PARAMETER"><i>path</i></tt>, gint*              <tt class="PARAMETER"><i>argcp</i></tt>, gchar*** <tt              class="PARAMETER"><i>argvp</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">gchar* <tt class=              "FUNCTION">              gnome_config_private_get_string</tt></code>(const              gchar* <tt class="PARAMETER"><i>              path</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">gchar* <tt class=              "FUNCTION">              gnome_config_private_get_translated_string</tt></code>(const              gchar* <tt class="PARAMETER"><i>              path</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">gint <tt class=              "FUNCTION">              gnome_config_private_get_int</tt></code>(const gchar*              <tt class="PARAMETER"><i>path</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">gdouble <tt class=               "FUNCTION">              gnome_config_private_get_float</tt></code>(const              gchar* <tt class="PARAMETER"><i>              path</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">gboolean <tt class=               "FUNCTION">              gnome_config_private_get_bool</tt></code>(const              gchar* <tt class="PARAMETER"><i>              path</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">void <tt class=              "FUNCTION">              gnome_config_private_get_vector</tt></code>(const              gchar* <tt class="PARAMETER"><i>path</i></tt>, gint*              <tt class="PARAMETER"><i>argcp</i></tt>, gchar*** <tt              class="PARAMETER"><i>argvp</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">gchar* <tt class=              "FUNCTION">              gnome_config_get_string_with_default</tt></code>(const              gchar* <tt class="PARAMETER"><i>path</i></tt>,              gboolean* <tt class="PARAMETER"><i>              was_default</i></tt>);</code>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -