summaryrefslogtreecommitdiff
path: root/src/yelp-info.c
blob: 4e02bbcfd91145c851e2371bda8c582536e8fd5b (plain)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2002 Red Hat Inc.
 * Copyright (C) 2000 Sun Microsystems, Inc. 
 * Copyright (C) 2001 Eazel, Inc.
 * Copyright (C) 2002 Mikael Hallendal <micke@codefactory.se>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 *
 * This code was based on the hyperbola code in nautilus.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <libgnome/gnome-i18n.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>

#include "yelp-section.h"
#include "yelp-util.h"
#include "yelp-info.h"


static void
yelp_info_read_info_dir (const char *basedir, GSList **info_list)
{
	DIR           *dirh;
	struct dirent *dent;
	YelpSection   *section;
	YelpURI       *uri;

	dirh = opendir (basedir);
	if (!dirh) {
		return;
	}

	while ((dent = readdir (dirh))) {
		char *ctmp = NULL;
		char uribuf[128], titlebuf[128];

		if (dent->d_name[0] == '.') {
			continue;
		}

		do {
			if (ctmp) {
				*ctmp = '\0';
			}
			
			ctmp = strrchr (dent->d_name, '.');
		} while (ctmp && strcmp (ctmp, ".info"));

		if (!ctmp) {
			continue;
		}

		*ctmp = '\0';

		strcpy (titlebuf, dent->d_name);
		strcat (titlebuf, " (info)");

		g_snprintf (uribuf, sizeof (uribuf), "info:%s", dent->d_name);

		uri = yelp_uri_new (uribuf);

		section = yelp_section_new (YELP_SECTION_DOCUMENT,
					    titlebuf, uri);

		yelp_uri_unref (uri);
		
		*info_list = g_slist_prepend (*info_list, section);
						      
	}

	closedir (dirh);
}

gboolean
yelp_info_init (GNode *tree, GList **index)
{
	GNode        *root;
	struct stat   stat_dir1;
	struct stat   stat_dir2;
	GSList       *info_list = NULL;
	GSList       *node;
	gchar       **infopathes;
	gchar        *infopath;
	gint          i;
	
	infopath = g_strdup (g_getenv ("INFOPATH"));
	
	if (infopath) {
		g_strstrip (infopath);
		infopathes = g_strsplit (infopath, ":", -1);
		g_free (infopath);
		
		for (i = 0; infopathes[i]; i++) {
			yelp_info_read_info_dir (infopathes[i], &info_list);
		}
	} else {
		stat ("/usr/info", &stat_dir1);
		stat ("/usr/share/info", &stat_dir2);
	
		yelp_info_read_info_dir ("/usr/info", &info_list);
	
		if (stat_dir1.st_ino != stat_dir2.st_ino) {
			yelp_info_read_info_dir  ("/usr/share/info", &info_list);
		}
	}
	
	if (g_slist_length (info_list) <= 0) {
		return FALSE;
	}

	root = g_node_append_data (tree,
				   yelp_section_new (YELP_SECTION_CATEGORY,
						     "info", NULL));

	for (node = info_list; node; node = node->next) {
		g_node_append_data (root, node->data);

		*index = g_list_prepend (*index, node->data);
	}

	g_slist_free (info_list);

	return TRUE;
}