hyperbola-nav-index.c 88.3 KB
Newer Older
1
#include <libnautilus/libnautilus.h>
2
#include <libnautilus-private/nautilus-bonobo-extensions.h>
3
#include <gnome.h>
4
5
#include "hyperbola-filefmt.h"
#include <gtk/gtk.h>
Elliot Lee's avatar
Elliot Lee committed
6
#include <string.h>
7
#include <limits.h>
8
#include <libxml/parser.h>
9
#include <dirent.h>
Elliot Lee's avatar
Elliot Lee committed
10
#include <ctype.h>
11
12
#include "hyperbola-nav.h"

13
14
15
16
17
18
19
#ifdef ENABLE_SCROLLKEEPER_SUPPORT
#include <gtk/gtkhseparator.h>


typedef enum { PRIMARY, SECONDARY, TERTIARY, SEE, SEEALSO, NONE } ItemType;

#else
20
typedef struct {
21
        NautilusView *view_frame;
22

23
        GtkWidget *clist, *ent;
24

25
        GTree *all_items;
Elliot Lee's avatar
Elliot Lee committed
26

27
        gint8 notify_count;
28
29
} HyperbolaNavigationIndex;

30
31
typedef enum { PRIMARY, SECONDARY, SEE, SEEALSO, NONE, T_LAST =

32
33
                NONE } ItemType;
#endif
34

Elliot Lee's avatar
Elliot Lee committed
35
typedef struct {
36
	char *text, *uri; 
37
	GTree *subitems;
38
39
40
41
42
43
44
45
        gboolean shown:1;
#ifdef ENABLE_SCROLLKEEPER_SUPPORT
	GTree *ref_subitems;
	GTree *duplicates;
	ItemType type;
	gboolean xref;
	char *index_id;
#else
46
	ItemType type:6;
47
48
        gboolean xref:1;
#endif
Elliot Lee's avatar
Elliot Lee committed
49
50
} IndexItem;

51
typedef struct {
52
53
54
55
	HyperbolaNavigationIndex *hni;
	char *words[50];
	int nwords;
	gboolean did_select;
56
57
58
59
#ifdef ENABLE_SCROLLKEEPER_SUPPORT
	char *indexid;
	int current_rownum;
#endif
60
61
} CListCreationInfo;

Elliot Lee's avatar
Elliot Lee committed
62
typedef struct {
63
64
65
	CListCreationInfo *cci;
	int indent, matches;
	int super_matches[50];
66
67
68
69
#ifdef ENABLE_SCROLLKEEPER_SUPPORT
	int full_matches;
	gboolean highlight;
#endif
Elliot Lee's avatar
Elliot Lee committed
70
71
} MoreCListInfo;

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#ifdef ENABLE_SCROLLKEEPER_SUPPORT

typedef struct {
	HyperbolaNavigationIndex *hni;
	HyperbolaIndexSelection index_type;
} IndexFiles;

typedef struct {
	IndexFiles *idx_files;
} MoreIndexFiles;
#endif

typedef struct {
	HyperbolaNavigationIndex *idx;
	GString *sub_text;
	ItemType sub_type;
	char *stinfo[NONE];
	int in_term;
#ifdef ENABLE_SCROLLKEEPER_SUPPORT
	char *doc_uri, *indexfile, *separator;
	char *index_ids[NONE];
	GSList *idx_refs[NONE];
	GTree *parent_tree;
	GTree *index_items;
	int in_indexitem;
#else
	char *idx_ref, *appname, *filename;
#endif
} SAXParseInfo;

#ifdef ENABLE_SCROLLKEEPER_SUPPORT
static int select_handler_id;
static void
make_index_select_page(GtkWidget *button, HyperbolaNavigationIndex *hni);
static void
create_index_select_page(HyperbolaNavigationIndex *hni);
static GSList *
hash_table_get_keys (GHashTable *hash_table);
static void
clear_index_hashtable (HyperbolaNavigationIndex *hni); 
static void
hyperbola_navigation_index_create_clist (HyperbolaNavigationIndex * hni);
static void
hyperbola_navigation_index_OK_button(GtkWidget *button, HyperbolaNavigationIndex *hni);
static void
hyperbola_navigation_index_select_all_button(GtkWidget *button, HyperbolaNavigationIndex *hni);
static void
hyperbola_navigation_index_select_none_button(GtkWidget *button, HyperbolaNavigationIndex *hni);
static void
hyperbola_navigation_index_cancel_button(GtkWidget *button, HyperbolaNavigationIndex *hni);
static void
redraw_index_display_page(HyperbolaNavigationIndex *hni);
static gboolean 
index_ctree_populate_subnode (gpointer key, gpointer value, gpointer user_data);
static void 
index_ctree_populate(HyperbolaNavigationIndex *hni);
static void 
process_index_term(SAXParseInfo *spi, ItemType it);
static void 
hyperbola_navigation_index_show_selected_terms (HyperbolaNavigationIndex *hni);
static void
hyperbola_navigation_index_parse_index_files (HyperbolaNavigationIndex *hni, 
						HyperbolaIndexSelection index_type);
static void
hyperbola_navigation_index_find_contents_selected_index_files(HyperbolaNavigationIndex *hni);
static void
hyperbola_navigation_index_tree_select_row (GtkCTree * ctree,
			   								GtkCTreeNode * node,
											gint column,
											HyperbolaNavigationIndex * hni);
static void
hyperbola_navigation_index_tree_unselect_row (GtkCTree * ctree, 
										GtkCTreeNode * node,
										gint column,
										HyperbolaNavigationIndex * hni);

/*
 * hyperbola_navigation_index_show_button
 * @GtkWidget *button : the button pointer
 * @HyperbolaNavigationIndex *hni: the pointer to the HyperbolaNavigationIndex
 * activated when the "Show" button is clicked on index_display_page.
 * it displays the specified index terms for the specified documents.
 */

static void 
hyperbola_navigation_index_show_button(GtkWidget *button, HyperbolaNavigationIndex *hni)
{

      /*
       * determines which document collection we are dealing with by 
       * the value of hni->index_type.
       * If hni->show_all_terms is true
       */
      switch(hni->index_type){
		case CONTENTS_SELECTED_INDEX_FILES:
			if(hni->show_all_terms){
                       		hyperbola_navigation_index_create_clist(hni);
			} else{
				hyperbola_navigation_index_show_selected_terms
				(hni);
			}
                       	break;
                case INDEX_SELECTED_INDEX_FILES:
			if(hni->show_all_terms){
                       		hyperbola_navigation_index_create_clist(hni);
			} else{
				hyperbola_navigation_index_show_selected_terms
				(hni);
			}
                       	break;
               	case ALL_INDEX_FILES:
			if(hni->show_all_terms){
                       		hyperbola_navigation_index_create_clist(hni);
			} else{
				hyperbola_navigation_index_show_selected_terms
				(hni);
			}
                       	break;
        };

}

/*
 * hyperbola_navigation_index_OK_button
 * @GtkWidget *button : pointer to the OK button on the index_select_page 
 * @HyperbolaNavigationIndex *hni
 * Reads the index files for the selected documents.
 * If the "Specific documents" radio button is on, then new index list is
 * created. 
 * Changes display to index_display_page.
 */
static void 
hyperbola_navigation_index_OK_button(GtkWidget *button, HyperbolaNavigationIndex *hni)
{
	hyperbola_navigation_index_parse_index_files(hni,
						    INDEX_SELECTED_INDEX_FILES);
	if(hni->index_type == INDEX_SELECTED_INDEX_FILES){
		hyperbola_navigation_index_create_clist(hni);
	}
	redraw_index_display_page(hni);
}

/*
 * hyperbola_navigation_index_select_all_button
 * @GtkWidget *button : pointer to the "Select All" button on the index_select_page 
 * @HyperbolaNavigationIndex *hni
 * Selects all the rows in the index_select_page display.
 * Finds all the index files (from
 * hni->index_contents->doc_tree->all_index_files), and adds each file to 
 * hni->index_selected_index_files (if not already present).
 */
static void 
hyperbola_navigation_index_select_all_button(GtkWidget *button, HyperbolaNavigationIndex *hni)
{ 
	GSList *keys = NULL, *tmp = NULL;
	char *index_file;
	
	gtk_clist_select_all(GTK_CLIST(hni->index_contents->ctree));
	keys = hash_table_get_keys(hni->index_contents->doc_tree->all_index_files);
	tmp = keys;

	while (tmp != NULL){
		if(!g_hash_table_lookup(hni->index_selected_index_files,
					          (char *)tmp->data)){
			index_file = g_strdup(g_hash_table_lookup(hni->index_contents->doc_tree->all_index_files,
					          (char *)tmp->data));
			g_hash_table_insert(hni->index_selected_index_files,
						g_strdup((char *)tmp->data), index_file);
		}
		tmp = g_slist_next(tmp);
	}
}

/*
 * hyperbola_navigation_index_select_none_button
 * @GtkWidget *button : pointer to the "Select None" button on the index_select_page 
 * @HyperbolaNavigationIndex *hni
 * Unselects all the rows in the index_select_page display.
 * Clears all the entries in hni->index_selected_index_files.
 */
static void 
hyperbola_navigation_index_select_none_button(GtkWidget *button, HyperbolaNavigationIndex *hni)
{
	gtk_clist_unselect_all(GTK_CLIST(hni->index_contents->ctree));
	clear_index_hashtable(hni);
}

/*
 * clear_index_hashtable
 * @HyperbolaNavigationIndex *hni
 * Finds all the keys to the hni->index_selected_index_files.
 * Uses these keys to lookup and deleted all entries, leaving
 * hni->index_selected_index_files empty.
 */
static void
clear_index_hashtable(HyperbolaNavigationIndex *hni)
{
	GSList *keys = NULL, *tmp = NULL;
	char *doc_uri, *value;
	keys = hash_table_get_keys(hni->index_selected_index_files);
	tmp = keys;

	while (tmp != NULL){
		doc_uri = (char *)tmp->data;
		value = g_hash_table_lookup(hni->index_selected_index_files,
						doc_uri);
		if(value != NULL){
			g_hash_table_remove(hni->index_selected_index_files,
						doc_uri);
			g_free(value);
		}
		g_free(doc_uri);
		tmp = g_slist_next(tmp);
	}
}
/*
 * hyperbola_navigation_index_cancel_button
 * @GtkWidget *button : pointer to the "Cancel" button on the index_select_page 
 * @HyperbolaNavigationIndex *hni
 * Unselects all rows on the index_select_page display.
 * Clears all entries in hni->index_select_index_page
 * Displays the index_display_page
 */

static void 
hyperbola_navigation_index_cancel_button(GtkWidget *button, HyperbolaNavigationIndex *hni)
{
	gtk_clist_unselect_all(GTK_CLIST(hni->index_contents->ctree));
	clear_index_hashtable(hni);
	redraw_index_display_page(hni);
}
#endif 

Elliot Lee's avatar
Elliot Lee committed
305
static gboolean
306
text_matches (const char *text, const char *check_for)
Elliot Lee's avatar
Elliot Lee committed
307
{
308
309
	const char *ctmp;
	int check_len;
Elliot Lee's avatar
Elliot Lee committed
310

311
312
	if (!check_for || !*check_for)
		return FALSE;
Elliot Lee's avatar
Elliot Lee committed
313

314
315
	ctmp = text;
	check_len = strlen (check_for);
Elliot Lee's avatar
Elliot Lee committed
316

317
	while (*ctmp) {
Darin Adler's avatar
Darin Adler committed
318
319
		if (g_ascii_tolower (*ctmp) == g_ascii_tolower (*check_for)) {
			if (!g_ascii_strncasecmp (ctmp, check_for, check_len))
320
321
				return TRUE;
		}
Elliot Lee's avatar
Elliot Lee committed
322

323
324
		ctmp++;
	}
Elliot Lee's avatar
Elliot Lee committed
325

326
327
328
329
	if (strstr (text, check_for)) {
		static volatile int barrier;
		while (barrier);
	}
Elliot Lee's avatar
Elliot Lee committed
330

331
	return FALSE;
Elliot Lee's avatar
Elliot Lee committed
332
333
}

334
335
336
337
338
339
340
341
342
343
344
#ifdef ENABLE_SCROLLKEEPER_SUPPORT
/*
 * hyperbola_navigation_index_show_duplicate_index_item
 * @const char *key: the key to the item in the subitems tree
 * @IndexItem *ii: the value of the item in the subitems tree
 * @ MoreCListInfo *mci: data required for processing
 * This function is called from hyperbola_navigation_index_show_index_item when
 * using g_tree_traverse to traverse an entry's subitems.
 * This function will display the subitem in the index terms display
 * Returns 0 (indicating to continue traversing tree)
 */
345
static gint
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
hyperbola_navigation_index_show_duplicate_index_item(const char *key, 
													IndexItem *ii,
													MoreCListInfo *mci)
{
	int rownum;
	char *rowtext, *tmp_text=NULL, *output_text=NULL;
	CListCreationInfo *cci=mci->cci;
	HyperbolaNavigationIndex *hni = cci->hni;

	tmp_text=g_strdup(ii->text);
	output_text = strtok(tmp_text, "#");
	if (output_text == NULL)
		output_text = g_strdup(ii->text);
	
	rowtext = g_strdup_printf ("%*s%s", (mci->indent - 1) * 5, "", output_text);
	rownum = gtk_clist_append (GTK_CLIST (hni->clist), &rowtext);
	gtk_clist_set_row_data (GTK_CLIST (hni->clist), rownum, ii);

	return 0;

}
/*
 * hyperbola_navigation_index_show_reference_item
 * @const char *key: the key to the item in the index tree
 * @IndexItem *ii: the value of the item in the index tree
 * @ MoreCListInfo *mci: data required for processing
 * This function is called from hyperbola_navigation_index_clist_select_row
 * when the user  clicks on a see/seealso reference. It is the traversal 
 * function used when traversing the relevant index tree.
 * This function will shift the display to the target of the see/seealso
 * reference.
 * Returns 1 when target row found or 0 to continue traversing tree
 */

static gint
hyperbola_navigation_index_show_reference_item(const char *key, IndexItem *ii,
                                        MoreCListInfo *mci)
383
{
384
385
	CListCreationInfo *cci=mci->cci;
	int rownum;
386
387
	HyperbolaNavigationIndex *hni = cci->hni;
	MoreCListInfo sub_mci = *mci;
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
	
	//printf("ii->index_id is %s, cci->indexid is %s\n", ii->index_id, cci->indexid);
	if(ii->index_id && !ii->xref && !strcmp(ii->index_id, cci->indexid)){
		rownum = gtk_clist_find_row_from_data(GTK_CLIST(hni->clist),
							(gpointer)ii);
		gtk_clist_select_row(GTK_CLIST(hni->clist), rownum, 0);
		gtk_clist_unselect_row(GTK_CLIST(hni->clist), cci->current_rownum, 0);
		gtk_clist_moveto(GTK_CLIST(hni->clist), rownum, 0, 0.5, 0.0);
		return 1;
	} else {
		if (ii->subitems)
			g_tree_traverse (ii->subitems, (GTraverseFunc)
							hyperbola_navigation_index_show_reference_item,
							G_IN_ORDER, &sub_mci);
	}
	return 0;
404
405


406
}
407

408
409
410
411
412
413
414
415
416
417
418
/*
 * hyperbola_navigation_index_show_index_item
 * @const char *key: the key to the item in the index tree
 * @IndexItem *ii: the value of the item in the index tree
 * @ MoreCListInfo *mci: data required for processing
 * This function is first called from
 * hyperbola_navigation_index_create_clist(), and then recursively
 * from here as traverse subtrees.
 * It creates index entries for the index display (hni->clist).
 * Returns 0 to indicate should continue traversing tree.
 */
419

420
421
422
423
424
425
426
427
428
429
static gint
hyperbola_navigation_index_show_index_item(const char *key, IndexItem *ii,
					MoreCListInfo *mci)
{
	int rownum;
	char *rowtext;
	CListCreationInfo *cci=mci->cci;
	HyperbolaNavigationIndex *hni = cci->hni;
	MoreCListInfo sub_mci = *mci;
	sub_mci.indent++;
Elliot Lee's avatar
Elliot Lee committed
430

431
432
433
434
	
	rowtext = g_strdup_printf ("%*s%s", mci->indent * 5, "", ii->text);
	rownum = gtk_clist_append (GTK_CLIST (hni->clist), &rowtext);
	hni->index_terms_found = TRUE;
435
	gtk_clist_set_row_data (GTK_CLIST (hni->clist), rownum, ii);
436
437
438
439
440
441
442
443
444
445
446
447
448
449
				
	if (ii->duplicates)
		g_tree_traverse (ii->duplicates, (GTraverseFunc)
				 hyperbola_navigation_index_show_duplicate_index_item,
				 G_IN_ORDER, &sub_mci);
	if (ii->ref_subitems)
		g_tree_traverse (ii->ref_subitems, (GTraverseFunc)
				 hyperbola_navigation_index_show_index_item,
				 G_IN_ORDER, &sub_mci);
	if (ii->subitems)
		g_tree_traverse (ii->subitems, (GTraverseFunc)
				 hyperbola_navigation_index_show_index_item,
				 G_IN_ORDER, &sub_mci);
	return 0;
450
451


452
}
453

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* hyperbola_navigation_index_create_clist
 * @HyperbolaNavigationIndex *hni
 * Function traverses relevant tree (according to value of hni->index_type),
 * and populates the index display (hni->clist) with these tree entries. 
 * If no entries are found (ie hni->index_terms_found remains FALSE, then a
 * suitable message is displayed in the hni->clist.
 */

static void
hyperbola_navigation_index_create_clist (HyperbolaNavigationIndex * hni) 
{
	CListCreationInfo cci;
	MoreCListInfo mci;
	GtkWidget *index_items = NULL;

	memset (&cci, 0, sizeof (cci));
	memset (&mci, 0, sizeof (mci));

	cci.hni = hni;
	mci.cci = &cci;
	mci.indent = 0;

	switch(hni->index_type){
		case CONTENTS_SELECTED_INDEX_FILES:
			index_items = (GtkWidget *) hni->contents_selected_index_items;
			break;
		case INDEX_SELECTED_INDEX_FILES:
			index_items = (GtkWidget *) hni->index_selected_index_items;
			break;
		case ALL_INDEX_FILES:
			index_items = (GtkWidget *) hni->all_index_items;
			break;
	};


	/* Set hni->index_terms_found to FALSE. If an index term is
	 * entered into the clist then this is set to TRUE.
	 */
	hni->index_terms_found = FALSE;
		
	gtk_clist_freeze (GTK_CLIST (hni->clist));
	gtk_clist_clear (GTK_CLIST (hni->clist));
	gtk_widget_set_sensitive(GTK_WIDGET(hni->clist), TRUE);
	g_tree_traverse ((GTree *)index_items,
				(GTraverseFunc) hyperbola_navigation_index_show_index_item,
                         G_IN_ORDER, &mci);
	/* if hni->index_terms_found is FALSE, then no entries for clist, so
	 * enter the following message there.
	*/
	if (hni->index_terms_found == FALSE){
		
		const char *message[] = { _("No index terms found for this selection")};
		gtk_clist_insert (GTK_CLIST (hni->clist), 0, (char **)message);
		gtk_clist_set_selectable(GTK_CLIST(hni->clist), 0, FALSE);
		/* We dont want to be able to select this row , however
		 * gtk_clist_set_selectable on its own , would not work on a 
		 * double-click so using this function instead. 
		 */ 
		gtk_widget_set_sensitive(GTK_WIDGET(hni->clist), FALSE);
513
	}
514
515
516
	
	gtk_clist_thaw (GTK_CLIST (hni->clist));
}
Elliot Lee's avatar
Elliot Lee committed
517

518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
 * hyperbola_navigation_index_show_selected_duplicate_item
 * @const char *key: the key to the item in the index tree
 * @IndexItem *ii: the value of the item in the index tree
 * @ MoreCListInfo *mci: data required for processing
 * This function is a traversal function called from
 * hyperbola_navigation_index_show_selected_item().
 * If an entry has a duplicates subtree (ie if ii->duplicates),
 * then this duplicates subtree is traversed and its entries are
 * displayed.
 * Returns 0 to indicate continued traversal of tree
 */
static gint
hyperbola_navigation_index_show_selected_duplicate_item(const char *key, IndexItem * ii,
                                      MoreCListInfo * mci)
{
        int rownum;
        char *rowtext, *tmp_text = NULL, *output_text = NULL;
        CListCreationInfo *cci = mci->cci;
        HyperbolaNavigationIndex *hni = cci->hni;
        MoreCListInfo dup_mci = *mci;
        GdkColor c;

	tmp_text = g_strdup(ii->text);
        output_text = strtok(tmp_text, "#");
        if (output_text == NULL)
                output_text = g_strdup(ii->text);

        rowtext = g_strdup_printf ("%*s%s", (mci->indent - 1) * 5,
                                   "", output_text);
        rownum = gtk_clist_append (GTK_CLIST (hni->clist), &rowtext);
        gtk_clist_set_row_data (GTK_CLIST (hni->clist), rownum, ii);
	  if (dup_mci.highlight){
        	/* highlight this row as a match */

        	c.red = c.green = 65535;
        	c.blue = 20000;
        	gdk_color_alloc (gdk_rgb_get_cmap (), &c);
        	gtk_clist_set_background (GTK_CLIST (hni->clist), rownum, &c);

        	if (!cci->did_select) {
       		         cci->did_select = TRUE;
                	gtk_clist_select_row (GTK_CLIST (hni->clist), rownum, 0);
        	}
	}
        /* Easiest way of showing the parents of matching items*/
        if (!dup_mci.matches && dup_mci.full_matches < cci->nwords)
                gtk_clist_remove (GTK_CLIST (hni->clist), rownum);
Elliot Lee's avatar
Elliot Lee committed
566

567
568
        return 0;
}
Elliot Lee's avatar
Elliot Lee committed
569

570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
 * hyperbola_navigation_index_show_selected_item
 * @const char *key: the key to the item in the index tree
 * @IndexItem *ii: the value of the item in the index tree
 * @ MoreCListInfo *mci: data required for processing
 * This function is the traversal function called from
 * hyperbola_navigation_index_show_selected_terms(), to
 * traverse the selected tree, and show only those index terms
 * that match text entered in the GtkEntry box.
 * Returns 0 to indicate continued traversal of tree
 */
static gint
hyperbola_navigation_index_show_selected_item (const char *key, IndexItem * ii,
                                      MoreCListInfo * mci)
{
        int rownum, i;
        char *rowtext;
        CListCreationInfo *cci = mci->cci;
        HyperbolaNavigationIndex *hni = cci->hni;
        MoreCListInfo sub_mci = *mci;
        MoreCListInfo dup_mci = sub_mci;
        int add_matches;        /* Whether this item itself contributed to a match */


        /* Three types of display:
         * shown - when it is part of a match, or is a parent of a matched item
         * shown + colored - when it has all the stuff needed for a match, but no more
         * hidden - when it is totally irrelevant
         */
        sub_mci.indent++;
        sub_mci.matches = 0;

        for (i = sub_mci.full_matches = add_matches = 0; i < cci->nwords; i++) {
                gboolean this_matches;

                this_matches = text_matches (ii->text, cci->words[i]);
                if (this_matches) {
                        add_matches++;
                        sub_mci.super_matches[i] = 1;

                }
                if (mci->super_matches[i] || this_matches)
                        sub_mci.full_matches++;
        }

        rowtext = g_strdup_printf ("%*s%s", mci->indent * 5,
                    	"", ii->text);
        rownum = gtk_clist_append (GTK_CLIST (hni->clist), &rowtext);
        gtk_clist_set_row_data (GTK_CLIST (hni->clist), rownum, ii);

        if (cci->nwords && sub_mci.full_matches >= cci->nwords && add_matches) { 
	        /* highlight this row as a match */
                GdkColor c;

                c.red = c.green = 65535;
                c.blue = 20000;
                gdk_color_alloc (gdk_rgb_get_cmap (), &c);
                gtk_clist_set_background (GTK_CLIST (hni->clist), rownum, &c);

                if (!cci->did_select) {
                        cci->did_select = TRUE;
                        gtk_clist_select_row (GTK_CLIST (hni->clist), rownum,
                                              0);
                }
		dup_mci.highlight = TRUE; /* if highlighting this term also need to 
					   * highlight duplicates 
					   */

        }
	if (ii->duplicates){
               	dup_mci.indent = sub_mci.indent;
		dup_mci.matches = sub_mci.matches;
		dup_mci.full_matches = sub_mci.full_matches;
               	g_tree_traverse (ii->duplicates,
                       	         (GTraverseFunc)
                               	 hyperbola_navigation_index_show_selected_duplicate_item,
                               	 G_IN_ORDER, &dup_mci);
	}
	dup_mci.highlight = FALSE;
649

650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
        if (ii->subitems)
                g_tree_traverse (ii->subitems,
                                 (GTraverseFunc)
                                 hyperbola_navigation_index_show_selected_item,
                                 G_IN_ORDER, &sub_mci);

        /* Easiest way of showing the parents of matching items*/
        if (!sub_mci.matches && sub_mci.full_matches < cci->nwords)
                gtk_clist_remove (GTK_CLIST (hni->clist), rownum);

        if (sub_mci.matches || sub_mci.full_matches >= cci->nwords)
                mci->matches++;

        return 0;
}

#else

static gint
hyperbola_navigation_index_show_item (const char *key, IndexItem * ii,
                                      MoreCListInfo * mci)
{
        int rownum, i;
        char rowtext[512], *textptr, *see_start, *see_end;
        CListCreationInfo *cci = mci->cci;
        HyperbolaNavigationIndex *hni = cci->hni;
        MoreCListInfo sub_mci = *mci;
        int my_matches;         /* Whether this item is part of a match */
        int add_matches;        /* Whether this item itself contributed to a match */

        /* Three types of display:
         * shown - when it is part of a match, or is a parent of a matched item
         * shown + colored - when it has all the stuff needed for a match, but no more
         * hidden - when it is totally irrelevant
         */
        sub_mci.indent++;
        sub_mci.matches = 0;

        /* We ignore secondary terms that were displayed on the toplevel
         * using the "secondary, primary" form - looks nicer when searching */
        if (ii->xref && cci->nwords > 0)
                return 0;

        for (i = my_matches = add_matches = 0; i < cci->nwords; i++) {
                gboolean this_matches;

                this_matches = text_matches (ii->text, cci->words[i]);
                if (this_matches) {
                        add_matches++;
                        sub_mci.super_matches[i] = 1;

                }
                if (mci->super_matches[i] || this_matches)
                        my_matches++;
        }

        switch (ii->type) {
        default:
                see_start = see_end = "";
                break;
        case SEE:
                see_start = _("see ");
                see_end = ")";
                break;
        case SEEALSO:
                see_start = _("see also ");
                see_end = ")";
                break;
        }

        g_snprintf (rowtext, sizeof (rowtext), "%*s%s%s%s", mci->indent * 3,
                    "", see_start, ii->text, see_end);
        textptr = rowtext;
        rownum = gtk_clist_append (GTK_CLIST (hni->clist), &textptr);
        gtk_clist_set_row_data (GTK_CLIST (hni->clist), rownum, ii);

        if (cci->nwords && my_matches >= cci->nwords && add_matches) {  /* highlight this row as a match */
                GdkColor c;

                c.red = c.green = 65535;
                c.blue = 20000;
                gdk_color_alloc (gdk_rgb_get_cmap (), &c);
                gtk_clist_set_background (GTK_CLIST (hni->clist), rownum, &c);

                if (!cci->did_select) {
                        cci->did_select = TRUE;
                        gtk_clist_select_row (GTK_CLIST (hni->clist), rownum,
                                              0);
                }
        }

        if (ii->subitems)
                g_tree_traverse (ii->subitems,
                                 (GTraverseFunc)
                                 hyperbola_navigation_index_show_item,
                                 G_IN_ORDER, &sub_mci);

        /* Easiest way of showing the parents of matching items */
        if (!sub_mci.matches && my_matches < cci->nwords)
                gtk_clist_remove (GTK_CLIST (hni->clist), rownum);

        if (sub_mci.matches || my_matches >= cci->nwords)
                mci->matches++;

        return 0;
755
}
756
#endif
757

758
759
760
761
762
763
764
765
#ifdef ENABLE_SCROLLKEEPER_SUPPORT
/*
 * hyperbola_navigation_index_show_selected_terms
 * @HyperbolaNavigationIndex *hni
 * User has indicated that only those index terms matching the 
 * text entered in the GtkEntry box should be displayed.
 * The relevant tree is traversed and matching terms are displayed.
 */
Elliot Lee's avatar
Elliot Lee committed
766
static void
767
hyperbola_navigation_index_show_selected_terms (HyperbolaNavigationIndex *hni)
Elliot Lee's avatar
Elliot Lee committed
768
{
769
770
771
772
773
	CListCreationInfo cci;
	MoreCListInfo mci;
	char *stxt, *tmp_stxt;
	char *ctmp = NULL;
	int tmp_len;
774
775
776
777
778
779
780
781
782
783
784
785
786
787
	GtkWidget *index_items = NULL;

	switch (hni->index_type) {
		case ALL_INDEX_FILES:
			index_items = (GtkWidget *)hni->all_index_items;
			break;
		case CONTENTS_SELECTED_INDEX_FILES:
			index_items = (GtkWidget *)hni->contents_selected_index_items;
			break;
		case INDEX_SELECTED_INDEX_FILES:
			index_items = (GtkWidget *)hni->index_selected_index_items;
			break;
		}

788
789
790
791
792
793
794
795
796
797
798
799
800

	stxt = gtk_entry_get_text (GTK_ENTRY (hni->ent));

	memset (&cci, 0, sizeof (cci));
	memset (&mci, 0, sizeof (mci));

	cci.hni = hni;

	tmp_len = strlen (stxt) + 1;
	tmp_stxt = alloca (tmp_len);
	memcpy (tmp_stxt, stxt, tmp_len);
	ctmp = strtok (tmp_stxt, ", \t");
	cci.nwords = 0;
801

802
803
804
805
806
	if (ctmp) {
		do {
			cci.words[cci.nwords] = ctmp;
			cci.nwords++;
		}
807
808
		while ((ctmp = strtok (NULL, ", \t")) && (size_t) cci.nwords <
			sizeof (cci.words) / sizeof (cci.words[0]));
Elliot Lee's avatar
Elliot Lee committed
809
810
	}

811
812
813
	cci.did_select = FALSE;
	mci.cci = &cci;
	mci.indent = 0;
Elliot Lee's avatar
Elliot Lee committed
814

815
816
	gtk_clist_freeze (GTK_CLIST (hni->clist));
	gtk_clist_clear (GTK_CLIST (hni->clist));
817
818
819
820
821
	gtk_widget_set_sensitive(GTK_WIDGET(hni->clist), TRUE);
	
	g_tree_traverse ((GTree *)index_items,
		(GTraverseFunc) hyperbola_navigation_index_show_selected_item,
		G_IN_ORDER, &mci);
Elliot Lee's avatar
Elliot Lee committed
822

823
824
	if (!mci.matches && cci.nwords) {
		const char *nomatches[] = { _("No matches.") };
Elliot Lee's avatar
Elliot Lee committed
825

826
827
828
		gtk_clist_insert (GTK_CLIST (hni->clist),0, (char **) nomatches);
		gtk_clist_set_selectable(GTK_CLIST(hni->clist), 0, FALSE);
		gtk_widget_set_sensitive(GTK_WIDGET(hni->clist), FALSE);
829
	}
Elliot Lee's avatar
Elliot Lee committed
830

831
	gtk_clist_thaw (GTK_CLIST (hni->clist));
Elliot Lee's avatar
Elliot Lee committed
832
833
}

834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
#endif

#ifndef  ENABLE_SCROLLKEEPER_SUPPORT
static void
hyperbola_navigation_index_update_clist (HyperbolaNavigationIndex * hni)
{
        CListCreationInfo cci;
        MoreCListInfo mci;
        char *stxt, *tmp_stxt;
        char *ctmp = NULL;
        int tmp_len;

        stxt = gtk_entry_get_text (GTK_ENTRY (hni->ent));

        memset (&cci, 0, sizeof (cci));
        memset (&mci, 0, sizeof (mci));

        cci.hni = hni;

        tmp_len = strlen (stxt) + 1;
        tmp_stxt = alloca (tmp_len);
        memcpy (tmp_stxt, stxt, tmp_len);
        ctmp = strtok (tmp_stxt, ", \t");
        cci.nwords = 0;
        if (ctmp) {
                do {
                        cci.words[cci.nwords] = ctmp;
                        g_print ("Word %d is %s\n", cci.nwords, ctmp);
                        cci.nwords++;
                }
                while ((ctmp = strtok (NULL, ", \t")) &&
                       (size_t) cci.nwords <
                       sizeof (cci.words) / sizeof (cci.words[0]));
        }

        cci.did_select = FALSE;
        mci.cci = &cci;
        mci.indent = 0;

        gtk_clist_freeze (GTK_CLIST (hni->clist));
        gtk_clist_clear (GTK_CLIST (hni->clist));

        g_tree_traverse (hni->all_items,
                         (GTraverseFunc) hyperbola_navigation_index_show_item,
                         G_IN_ORDER, &mci);

        if (!mci.matches && cci.nwords) {
                int rownum;
                const char *nomatches[] = { _("No matches.") };

                rownum =
                        gtk_clist_append (GTK_CLIST (hni->clist),
                                          (char **) nomatches);
                gtk_clist_set_selectable (GTK_CLIST (hni->clist), rownum,
                                          FALSE);
        }

        gtk_clist_thaw (GTK_CLIST (hni->clist));
}
#endif

Elliot Lee's avatar
Elliot Lee committed
895
static void
896
hyperbola_navigation_index_ent_changed (GtkWidget * ent,
897
                                        HyperbolaNavigationIndex * hni)
Elliot Lee's avatar
Elliot Lee committed
898
{
899
900
901
#ifndef ENABLE_SCROLLKEEPER_SUPPORT
        hyperbola_navigation_index_update_clist (hni);
#endif
Elliot Lee's avatar
Elliot Lee committed
902
903
}

904
905
906
907
908
909
910
911
/*
 * hyperbola_navigation_index_ent_activate
 * @GtkWidget *ent : GtkEntry box where user enters text of index
 *                   terms to be displayed
 * @HyperbolaNavigationIndex *hni
 * When the user presses return within the GtkEntry box,
 * the index display (hni->clist) is recreated to show matching terms only
 */
Elliot Lee's avatar
Elliot Lee committed
912
static void
913
hyperbola_navigation_index_ent_activate (GtkWidget * ent,
914
                                         HyperbolaNavigationIndex * hni)
Elliot Lee's avatar
Elliot Lee committed
915
{
916
917
918
919
#ifdef ENABLE_SCROLLKEEPER_SUPPORT
	hyperbola_navigation_index_show_selected_terms (hni);

#endif
Elliot Lee's avatar
Elliot Lee committed
920
921
}

922

Elliot Lee's avatar
Elliot Lee committed
923
static void
924
start_document (SAXParseInfo * spi)
Elliot Lee's avatar
Elliot Lee committed
925
{
926
}
Elliot Lee's avatar
Elliot Lee committed
927

928
929
930
931
static void
end_document (SAXParseInfo * spi)
{
}
Elliot Lee's avatar
Elliot Lee committed
932

933
934
935
936
937
938
static void
characters (SAXParseInfo * spi, const gchar * chars, int len)
{
#ifdef ENABLE_SCROLLKEEPER_SUPPORT
	if (spi->sub_type == NONE ||
		!spi->in_term) 
939
		return;
940
941
942
943
#else		
	if (spi->sub_type == NONE)
		return;
#endif
Elliot Lee's avatar
Elliot Lee committed
944

945
	g_string_sprintfa (spi->sub_text, "%.*s", len, chars);
Elliot Lee's avatar
Elliot Lee committed
946
947
}

948
949
950
951
952
953
954
955
956
/* Removes all duplicate spaces */
static void
despace (GString * s)
{
        char *ctmp, *ctmp_s = NULL;
        int i;

        g_assert (s->len == (int) strlen (s->str));
        for (ctmp = s->str, i = s->len; *ctmp; ctmp++, i--) {
Darin Adler's avatar
Darin Adler committed
957
                if (g_ascii_isspace (*ctmp)) {
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
                        if (*ctmp != ' ')
                                *ctmp = ' ';
                        if (!ctmp_s)
                                ctmp_s = ctmp;
                } else if (ctmp_s) {
                        if ((ctmp - ctmp_s) > 1) {
                                memmove (ctmp_s + 1, ctmp, i + 1);
                                ctmp = ctmp_s + 2;
                                i--;
                                if (i != (int) strlen (ctmp))
                                        g_error
                                                ("i (%d) != strlen(ctmp) (%ld)",
                                                 i, (long) strlen (ctmp));
                        }
                        ctmp_s = NULL;
                }
        }
        s->len = strlen (s->str);
}
977

978
979

static xmlEntityPtr
980
get_entity (SAXParseInfo * spi, const gchar * name)
981
982
{
#ifdef ERROR_OUTPUT
983
        g_print ("in getEntity:%s\n", name);
984
985
#endif

986
        return xmlGetPredefinedEntity (name);
987
988
}

989
#ifndef ENABLE_SCROLLKEEPER_SUPPORT
990
static void
991
start_element (SAXParseInfo * spi, const gchar * name, const xmlChar ** attrs)
992
{
Darin Adler's avatar
Darin Adler committed
993
        if (!g_ascii_strcasecmp (name, "indexterm")) {
994
995
996
                int i;

                for (i = 0; attrs[i]; i++) {
Darin Adler's avatar
Darin Adler committed
997
                        if (!g_ascii_strcasecmp (attrs[i], "id")) {
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
                                i++;
                                break;
                        }
                }

                g_return_if_fail (attrs[i]);

                spi->idx_ref = g_strdup (attrs[i]);
                spi->in_term++;
                g_string_assign (spi->sub_text, "");
                for (i = PRIMARY; i < NONE; i++)
                        spi->stinfo[i] = NULL;
                spi->sub_type = NONE;

                return;
        }
        if (!spi->in_term || spi->sub_type != NONE)
                return;

Darin Adler's avatar
Darin Adler committed
1017
        if (!g_ascii_strcasecmp (name, "primary")) {
1018
                spi->sub_type = PRIMARY;
Darin Adler's avatar
Darin Adler committed
1019
        } else if (!g_ascii_strcasecmp (name, "secondary")) {
1020
                spi->sub_type = SECONDARY;
Darin Adler's avatar
Darin Adler committed
1021
        } else if (!g_ascii_strcasecmp (name, "seealso")) {
1022
                spi->sub_type = SEEALSO;
Darin Adler's avatar
Darin Adler committed
1023
        } else if (!g_ascii_strcasecmp (name, "see")) {
1024
1025
1026
1027
1028
1029
                spi->sub_type = SEE;
        } else
                spi->sub_type = NONE;

        g_return_if_fail (spi->sub_type == NONE ||
                          !spi->stinfo[spi->sub_type]);
1030
1031
1032
}

static void
1033
end_element (SAXParseInfo * spi, const gchar * name)
1034
{
1035
1036
        int this_type;

Darin Adler's avatar
Darin Adler committed
1037
        if (!g_ascii_strcasecmp (name, "indexterm")) {
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
                int i;
                IndexItem *parent_ii = NULL, *ii = NULL;
                ItemType it;
                GTree *parent_tree = NULL;

                /* Algorithm:
                 * If this is a see (also), we have to list it on the top level, so make the node.
                 * If this is a secondary, we have to first find/make the primary.
                 */

                spi->in_term--;

                g_return_if_fail (spi->stinfo[PRIMARY]);

                it = PRIMARY;
                if (spi->stinfo[SECONDARY] || spi->stinfo[SEE] ||
                    spi->stinfo[SEEALSO]) {
                        parent_ii =
                                g_tree_lookup (spi->idx->all_items,
                                               spi->stinfo[PRIMARY]);

                        if (!parent_ii) {
                                parent_ii = g_new0 (IndexItem, 1);
                                parent_ii->type = PRIMARY;
                                parent_ii->text =
                                        g_strdup (spi->stinfo[PRIMARY]);
                                g_tree_insert (spi->idx->all_items,
                                               parent_ii->text, parent_ii);
                        }

                        if (!parent_ii->subitems)
                                parent_ii->subitems =
                                        g_tree_new ((GCompareFunc)
Darin Adler's avatar
Darin Adler committed
1071
                                                    eel_strcoll);
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
                        parent_tree = parent_ii->subitems;

                        if (spi->stinfo[SECONDARY])
                                it = SECONDARY;
                        else if (spi->stinfo[SEE])
                                it = SEE;
                        else if (spi->stinfo[SEEALSO])
                                it = SEEALSO;

                        if ((it == SECONDARY) &&
                            (spi->stinfo[SEE] || spi->stinfo[SEEALSO])) {
                                /* Make a second layer */
                                ii =
                                        g_tree_lookup (parent_tree,
                                                       spi->stinfo[PRIMARY]);

                                if (!ii) {
                                        ii = g_new0 (IndexItem, 1);
                                        ii->type = SECONDARY;
                                        ii->text =
                                                g_strdup (spi->
                                                          stinfo[SECONDARY]);
                                        g_tree_insert (parent_tree, ii->text,
                                                       ii);
                                }

                                if (!ii->subitems)
                                        ii->subitems =
                                                g_tree_new ((GCompareFunc)
Darin Adler's avatar
Darin Adler committed
1101
                                                            eel_strcoll);
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
                                parent_ii = ii;
                                parent_tree = parent_ii->subitems;

                                it = spi->stinfo[SEE] ? SEE : SEEALSO;
                        }
                } else {
                        parent_tree = spi->idx->all_items;
                }

                g_assert (parent_tree != spi->idx->all_items ||
                          it == PRIMARY);

                ii = g_tree_lookup (parent_tree, spi->stinfo[it]);
                if (!ii) {
                        ii = g_new0 (IndexItem, 1);
                        ii->text = g_strdup (spi->stinfo[it]);
                        g_tree_insert (parent_tree, ii->text, ii);
                        ii->type = it;
                }
                if (!ii->uri)
                        ii->uri =
                                g_strdup_printf ("help:%s/%s/%s",
                                                 spi->appname, spi->filename,
                                                 spi->idx_ref);
                g_assert (ii->type == it);

                if (spi->stinfo[SECONDARY]) {
                        /* Also insert a top-level node that gives info on this secondary node */

                        char buf[512];
                        char *txt;

                        g_snprintf (buf, sizeof (buf), "%s, %s",
                                    spi->stinfo[SECONDARY],
                                    spi->stinfo[PRIMARY]);
                        if (spi->stinfo[SEE] || spi->stinfo[SEEALSO]) {
                                if (spi->stinfo[SEE]) {
                                        it = SEE;
                                        txt = spi->stinfo[SEE];
                                        strcat (buf, _(" (see \""));
                                } else {
                                        txt = spi->stinfo[SEEALSO];
                                        strcat (buf, _(" (see also \""));
                                }

                                strcat (buf, txt);
                                strcat (buf, ")");
                        }
                        parent_tree = spi->idx->all_items;

                        ii = g_tree_lookup (parent_tree, spi->stinfo[it]);
                        if (!ii) {
                                ii = g_new0 (IndexItem, 1);
                                ii->text = g_strdup (buf);
                                g_tree_insert (parent_tree, ii->text, ii);
                                ii->type = PRIMARY;
                                ii->xref = TRUE;
                        }

                        if (!ii->uri)
                                ii->uri =
                                        g_strdup_printf ("help:%s/%s/%s",
                                                         spi->appname,
                                                         spi->filename,
                                                         spi->idx_ref);
                }

                for (i = PRIMARY; i < NONE; i++) {
                        g_free (spi->stinfo[i]);
                        spi->stinfo[i] = NULL;
                }
                g_free (spi->idx_ref);
                spi->idx_ref = NULL;

                return;
        }

        if (!spi->in_term || spi->sub_type == NONE)
                return;

Darin Adler's avatar
Darin Adler committed
1182
        if (!g_ascii_strcasecmp (name, "primary")) {
1183
                this_type = PRIMARY;
Darin Adler's avatar
Darin Adler committed
1184
        } else if (!g_ascii_strcasecmp (name, "secondary")) {
1185
                this_type = SECONDARY;
Darin Adler's avatar
Darin Adler committed
1186
        } else if (!g_ascii_strcasecmp (name, "seealso")) {
1187
                this_type = SEEALSO;
Darin Adler's avatar
Darin Adler committed
1188
        } else if (!g_ascii_strcasecmp (name, "see")) {
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
                this_type = SEE;
        } else
                this_type = NONE;

        g_return_if_fail (this_type != NONE && !spi->stinfo[this_type] &&
                          this_type == (int) spi->sub_type);

        if (spi->sub_text->len) {
                despace (spi->sub_text);
                spi->stinfo[this_type] = g_strdup (spi->sub_text->str);
                g_assert (spi->stinfo[this_type]);
        }
        g_message ("Set \"%s\" for %s (%d)", spi->stinfo[this_type], name,
                   this_type);
        g_string_assign (spi->sub_text, "");
        spi->sub_type = NONE;
1205
1206
}

1207
1208
1209
1210
1211
#endif



#ifdef ENABLE_SCROLLKEEPER_SUPPORT
1212
static void
1213
start_element (SAXParseInfo * spi, const gchar * name, const xmlChar ** attrs)
1214
{
Darin Adler's avatar
Darin Adler committed
1215
	if (!g_ascii_strcasecmp (name, "indexitem") &&
1216
				spi->sub_type==NONE) {
1217
		int i;
1218
1219
1220
1221
1222
1223
1224
1225
1226
		for (i = PRIMARY; i < NONE; i++){
			spi->idx_refs[i] = NULL;
			spi->stinfo[i] = NULL;
		}
		spi->sub_type = PRIMARY;
		spi->in_indexitem = 1;
		spi->parent_tree = spi->index_items;
		return;
	}
1227

Darin Adler's avatar
Darin Adler committed
1228
	if (!g_ascii_strcasecmp (name, "indexitem") &&
1229
1230
1231
1232
1233
1234
1235
				spi->sub_type==PRIMARY) {
		process_index_term (spi, PRIMARY);
		spi->sub_type = SECONDARY;
		spi->in_indexitem++;
		return;
	}

Darin Adler's avatar
Darin Adler committed
1236
	if (!g_ascii_strcasecmp (name, "indexitem") &&
1237
1238
1239
1240
				spi->sub_type==SECONDARY) {
		if(spi->in_indexitem > 1){
			process_index_term (spi, SECONDARY);
			spi->sub_type = TERTIARY;
1241
		}
1242
1243
1244
		spi->in_indexitem++;
		return;
	}
1245

Darin Adler's avatar
Darin Adler committed
1246
	if (!g_ascii_strcasecmp (name, "indexitem") &&
1247
1248
1249
1250
1251
1252
				spi->sub_type==TERTIARY) {
		if(spi->in_indexitem < 2)
			spi->sub_type = SECONDARY;
		spi->in_indexitem++;
		return;
	}
1253

Darin Adler's avatar
Darin Adler committed
1254
	if (!g_ascii_strcasecmp (name, "title")){
1255
1256
1257
1258
1259
		spi->in_term++;
		g_string_assign (spi->sub_text, "");
		return;
	}

1260
	
Darin Adler's avatar
Darin Adler committed
1261
	if (!g_ascii_strcasecmp (name, "link")) {
1262
1263
		int i, j;
		for (i = 0; attrs[i]; i++) {
Darin Adler's avatar
Darin Adler committed
1264
			if (!g_ascii_strcasecmp (attrs[i], "linkid")){
1265
1266
1267
1268
1269
1270
1271
				i++;
				break;
			}
		}
		g_return_if_fail(attrs[i]);
		spi->idx_refs[spi->sub_type] = g_slist_prepend(spi->idx_refs[spi->sub_type],
							g_strdup(attrs[i])); 
1272

1273
		for(j=i+1; attrs[j]; j++){
Darin Adler's avatar
Darin Adler committed
1274
			if (!g_ascii_strcasecmp (attrs[j], "indexid")){
1275
1276
1277
1278
1279
1280
1281
				j++;
				break;
			}
		}
		if(attrs[j] && strlen(attrs[j]) > 1){
			spi->index_ids[spi->sub_type] = g_strdup(attrs[j]); 
		}
1282
		return;
1283
	}
1284

Darin Adler's avatar
Darin Adler committed
1285
	if (!g_ascii_strcasecmp (name, "see")) {
1286
1287
		int i;
		for (i = 0; attrs[i]; i++) {
Darin Adler's avatar
Darin Adler committed
1288
			if (!g_ascii_strcasecmp (attrs[i], "indexid")){
1289
1290
				i++;
				break;
1291
1292
			}
		}
1293
1294
1295
1296
1297
		g_return_if_fail(attrs[i]);
		spi->index_ids[SEE] = g_strdup(attrs[i]); 
		spi->in_term++;
		g_string_assign (spi->sub_text, "");
		return;
Elliot Lee's avatar
Elliot Lee committed
1298
1299
	}

1300

Darin Adler's avatar
Darin Adler committed
1301
	if (!g_ascii_strcasecmp (name, "seealso")) {
1302
		int i;
1303
		for (i = 0; attrs[i]; i++) {
Darin Adler's avatar
Darin Adler committed
1304
			if (!g_ascii_strcasecmp (attrs[i], "indexid")){
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
				i++;
				break;
			}
		}
		g_return_if_fail(attrs[i]);
		spi->index_ids[SEEALSO] = g_strdup(attrs[i]); 
		spi->in_term++;
		g_string_assign (spi->sub_text, "");
		return;
	}
1315
1316


Darin Adler's avatar
Darin Adler committed
1317
	if (!g_ascii_strcasecmp (name, "indexdoc")){
1318
1319
1320
1321
		spi->sub_type = NONE;
		return;
	}
					
1322

1323
1324
	if (!spi->in_term || spi->sub_type != NONE)
		return;
1325

1326
1327
1328
	g_return_if_fail (spi->sub_type == NONE ||
			  !spi->stinfo[spi->sub_type]);
}
1329

1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
static void 
process_index_term(SAXParseInfo *spi, ItemType it)
{ 
	GSList *idx_refs_list = NULL;
	GTree *parent_tree = NULL;
	char *tmp_uri = NULL, *tmp_text = NULL; 
	IndexItem *ii = NULL, *parent_ii = NULL, *duplicate_ii = NULL;

	if (spi->sub_type == SECONDARY){
		parent_ii = g_tree_lookup(spi->index_items,
						spi->stinfo[PRIMARY]);
		if (!parent_ii->subitems)
			parent_ii->subitems = 
				g_tree_new((GCompareFunc)
Darin Adler's avatar
Darin Adler committed
1344
						eel_strcoll);
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
		spi->parent_tree = parent_ii->subitems;
	}
	ii =
		g_tree_lookup (spi->parent_tree,
			       		spi->stinfo[it]);

	if (!ii) {
		ii = g_new0 (IndexItem, 1);
		ii->type = it;
		ii->text = g_strdup(spi->stinfo[it]);
		if(spi->index_ids[it])
			ii->index_id = g_strdup(spi->index_ids[it]);
		g_tree_insert (spi->parent_tree,
			       		ii->text, ii);
	}
1360

1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
	if(spi->idx_refs[it]) {
		idx_refs_list = spi->idx_refs[it];
		while (idx_refs_list != NULL) {
			if(!ii->uri){	
				tmp_uri=g_strdup_printf("%s%s%s",
							spi->doc_uri,
							spi->separator,
							(char *)idx_refs_list->data);
				ii->uri=tmp_uri;
			} else {
				tmp_text=g_strdup_printf("%s#%s",
							 spi->stinfo[it],
						         (char *)idx_refs_list->data);
				if (!ii->duplicates)
                               		ii->duplicates =
                                       			g_tree_new ((GCompareFunc)
Darin Adler's avatar
Darin Adler committed
1377
                                               		eel_strcoll);
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393

				duplicate_ii =
						g_tree_lookup (ii->duplicates,
			       					tmp_text);
				if(!duplicate_ii){
					duplicate_ii = g_new0 (IndexItem, 1);
                        		duplicate_ii->type = it;
                        		duplicate_ii->text =tmp_text;
                        		g_tree_insert (ii->duplicates,
                                    		        duplicate_ii->text, duplicate_ii);
					tmp_uri=g_strdup_printf("%s%s%s",
								spi->doc_uri,
								spi->separator,
								(char *)idx_refs_list->data);
					duplicate_ii->uri=tmp_uri;
				}
1394
			}
1395
			idx_refs_list=g_slist_next(idx_refs_list);
1396
		}
1397
1398
1399
1400
1401
	}
	if(spi->sub_type == SECONDARY){
		if (!ii->subitems)
			ii->subitems = 
				g_tree_new((GCompareFunc)
Darin Adler's avatar
Darin Adler committed
1402
						eel_strcoll);
1403
1404
		spi->parent_tree = ii->subitems;
	}
1405

1406
1407
1408
1409
1410
	if (spi->stinfo[SEE]) {
                char *buf;
		if (!ii->ref_subitems)
                          ii->ref_subitems =
                                        g_tree_new ((GCompareFunc)
Darin Adler's avatar
Darin Adler committed
1411
                                                        eel_strcoll);
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
		parent_tree = ii->ref_subitems;
                buf = g_strdup_printf ("%s%s%s",
                                        _("(see: "), spi->stinfo[SEE], ")");
                ii = g_tree_lookup (parent_tree, buf);
                if (!ii) {
                         ii = g_new0 (IndexItem, 1);
                         ii->text = g_strdup (buf);
                         g_tree_insert (parent_tree, ii->text, ii);
                         ii->type = SEE;
                         ii->xref=TRUE;
                         if(spi->index_ids[SEE])
                                ii->index_id=g_strdup(spi->index_ids[SEE]);
                }
	}
	if (spi->stinfo[SEEALSO]) {
                char *buf;
		if (!ii->ref_subitems)
                          ii->ref_subitems =
                                        g_tree_new ((GCompareFunc)
Darin Adler's avatar
Darin Adler committed
1431
                                                        eel_strcoll);
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
		parent_tree = ii->ref_subitems;
                buf = g_strdup_printf ("%s%s%s",
                                        _("(see also: "), spi->stinfo[SEEALSO], ")");
                ii = g_tree_lookup (parent_tree, buf);
                if (!ii) {
                         ii = g_new0 (IndexItem, 1);
                         ii->text = g_strdup (buf);
                         g_tree_insert (parent_tree, ii->text, ii);
                         ii->type = SEEALSO;
                         ii->xref=TRUE;
                         if(spi->index_ids[SEEALSO])
                                ii->index_id=g_strdup(spi->index_ids[SEEALSO]);
                }
	}
	if(it != PRIMARY){
		int i;
		for (i = it; i < NONE; i++) {
                       g_free (spi->stinfo[i]);
                       spi->stinfo[i] = NULL;
                	g_free (spi->index_ids[spi->sub_type]);
                	spi->index_ids[spi->sub_type] = NULL;
			g_slist_free(spi->idx_refs[i]);
			spi->idx_refs[i] = NULL;
                }
	}
1457
1458


1459
1460
1461
1462
1463
1464
1465
}
static void
end_element (SAXParseInfo * spi, const gchar * name)
{
	int this_type;/*, i;*/
	ItemType it;

Darin Adler's avatar
Darin Adler committed
1466
	if (!g_ascii_strcasecmp (name, "indexitem")) {
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478

		spi->in_indexitem--;
		it = spi->sub_type;

		if (spi->in_indexitem > 0 && spi->stinfo[it]){
			process_index_term(spi, it);
		} else if(spi->in_indexitem == 0){
			spi->sub_type = NONE;
			g_free (spi->stinfo[PRIMARY]);
			spi->stinfo[PRIMARY] = NULL;
			g_free (spi->index_ids[PRIMARY]);
			spi->index_ids[PRIMARY] = NULL;
1479
		}
1480
1481
1482
1483
1484
				
			
		return;
	}
       
1485

Darin Adler's avatar
Darin Adler committed
1486
	if (!g_ascii_strcasecmp (name, "indexdoc")){
1487
		spi->sub_type = NONE;
1488
		return;
1489
1490
	}

1491
	if (spi->sub_type == NONE)
1492
1493
		return;

Darin Adler's avatar
Darin Adler committed
1494
	if (!g_ascii_strcasecmp(name, "link")){
1495
1496
			this_type = spi->sub_type;
			return;
Darin Adler's avatar
Darin Adler committed
1497
	} else if (!g_ascii_strcasecmp (name, "title")) {
1498
1499
		this_type=spi->sub_type;
		spi->in_term--;
Darin Adler's avatar
Darin Adler committed
1500
	} else if (!g_ascii_strcasecmp (name, "seealso")) {
1501
		this_type = SEEALSO;
1502
		spi->in_term--;
Darin Adler's avatar
Darin Adler committed
1503
	} else if (!g_ascii_strcasecmp (name, "see")) {
1504
		this_type = SEE;
1505
		spi->in_term--;
1506
1507
1508
1509
1510
1511
1512
1513
	} else
		this_type = NONE;


	if (spi->sub_text->len) {
		despace (spi->sub_text);
		spi->stinfo[this_type] = g_strdup (spi->sub_text->str);
		g_assert (spi->stinfo[this_type]);
1514
	}
1515
1516
	/*g_message ("Set \"%s\" for %s (%d)", spi->stinfo[this_type], name,
		   this_type);*/
1517
	g_string_assign (spi->sub_text, "");
1518
}
1519
#endif
1520
1521

static void
1522
handle_error (SAXParseInfo * spi, const char *msg, ...)
1523
{
1524
        va_list args;
1525

1526
        va_start (args, msg);
1527
#ifdef ERROR_OUTPUT
1528
        g_logv ("XML", G_LOG_LEVEL_CRITICAL, msg, args);
1529
#endif
1530
        va_end (args);
1531
1532
1533
}

static void
1534
handle_fatal_error (SAXParseInfo * spi, const char *msg, ...)
1535
{
1536
        va_list args;
1537

1538
        va_start (args, msg);
1539
#ifdef ERROR_OUTPUT
1540
        g_logv ("XML", G_LOG_LEVEL_ERROR, msg, args);
1541
#endif
1542
        va_end (args);</