File: gpehelp.c

package info (click to toggle)
libgpewidget 0.117-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,628 kB
  • sloc: sh: 9,164; ansic: 6,416; makefile: 163
file content (163 lines) | stat: -rw-r--r-- 4,296 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) Florian Boor <florian.boor@kernelconcepts.de>
 *
 * Rewrite for integration of new GPE help system:
 * Copyright (C) Philippe De Swert <philippedeswert@scarlet.be>
 *
 * Dedicated to Rammstein - Live in Berlin - Disk 1 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA. 
 */

#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk/gdk.h>
#include <glib.h>


/* helper function definitions*/
char *gpe_check_for_help (const char *appname);	/* can be used as a quick help check when starting an app */
static GKeyFile *load_help_key_file (void);

/** 
 * gpe_show_help:
 * @appname: Name of the application to show help about.
 * @topic: String describing help topic.
 *
 *  This function provides a generic interface for displaying
 *  full text online help. It is intended to be independent from
 *  file format and location.
 *  Return value is FALSE if help is found and displayed, TRUE
 *  if an error occurs.
 *	 
 *  Returns: TRUE if an error occured, FALSE otherwise.
 */
gboolean
gpe_show_help (const char *appname, const char *topic)
{
  gchar *helpfile;
  gchar *helpadress;
  gchar *apps = "gpe-helpviewer gpe-mini-browser dillo minimo";
  gchar **app, **applist;
  const gchar *command=NULL;
  gboolean spawn = FALSE;

  /* lookup help file */
  helpfile = gpe_check_for_help (appname);
  if (helpfile == NULL)
    return TRUE;

  /* check if the file is readable */
  if (access (helpfile, R_OK))
    {
#if defined DEBUG
      printf ("helpfile %s, not readable\n", helpfile);
#endif
      return TRUE;
    }

  /* construct the complete help address */
  if ((topic) && strlen (topic))
    helpadress = g_strdup_printf ("%s#%s", helpfile, topic);
  else
    helpadress = helpfile;

  /* make array of strings to be able to check for the different aplications */
  app = g_strsplit (apps, " ", 0);
  applist = app;

  while (*app != NULL)
    {
      command = g_strdup_printf ("%s %s", *app, helpadress);
#if defined DEBUG
      printf ("command = %s\n", command);
#endif
      spawn = g_spawn_command_line_async (command, NULL);
      if (spawn)
         return FALSE;
      app++;
    }
  if (!spawn)
    {
      g_free ((char *)command);
      g_free (helpfile);
      g_strfreev (applist); 
      if (helpadress != helpfile)
        g_free (helpadress); 
      return TRUE;
    }
   /* should never get here */
   return TRUE;
}

/** 
 * gpe_check_for_help:
 * @appname: Name of the application.
 *
 * Checks for the existance of any installed help. Returns NULL if no help, or
 * if the help for that particular application is not installed.
 * If the help is found it returns the filename to that help.
 *
 * Returns: Filename if help is installed.
 */
char *
gpe_check_for_help (const char *appname)
{
  gchar *file;
  GKeyFile *info;

  info = load_help_key_file ();
  if (info == NULL)
    {
      return NULL;
    }
  else
    {
      file = g_key_file_get_value (info, "Help", appname, NULL);
      g_free (info);
      return file;
    }

}

/*
 * Loads the keyfile in memory and returns a pointer to it.
 * This will be NULL if no help is installed or the helpviewer
 * configuration file is not there 
*/
static GKeyFile *
load_help_key_file (void)
{
  GKeyFile *config;
  gboolean loaded;
  const char *filename = "/etc/gpe/gpe-help.conf";

  config = g_key_file_new ();
  loaded =
    g_key_file_load_from_file (config, filename, G_KEY_FILE_NONE, NULL);
  if (loaded)
    return config;
  else
    return NULL;
}