summary.c 4.71 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * summary.c:  Summary Information management
 *
 * Author:
 *    Michael Meeks (michael@imaginator.com)
 *
 * (C) 1999 Michael Meeks
 */
Michael Meeks's avatar
Michael Meeks committed
9
#include <stdio.h>
10 11 12 13 14 15 16 17 18
#include <config.h>
#include <ctype.h>
#include <glib.h>
#include "summary.h"

gchar *summary_item_name[] = {
	"Title",
	"Subject",
	"Author",
Michael Meeks's avatar
Michael Meeks committed
19 20 21 22
	"Keywords",
	"Comments",
	"Template",
	"Last Author",
Michael Meeks's avatar
Michael Meeks committed
23 24
	"Manager",
	"Category",
Michael Meeks's avatar
Michael Meeks committed
25
	"Application"
26 27 28 29 30 31 32 33 34 35 36 37 38
};

static SummaryItem *
summary_item_new (const gchar *name, SummaryItemType t)
{
	SummaryItem *sit = g_new (SummaryItem, 1);
	sit->name = g_strdup (name);
	sit->type = t;
	return sit;
}


SummaryItem *
39
summary_item_new_int (const gchar *name, gint i)
40 41 42 43 44 45 46
{
	SummaryItem *sit = summary_item_new (name, SUMMARY_INT);
	sit->v.i = i;
	return sit;
}

SummaryItem *
47
summary_item_new_time (const gchar *name, GTimeVal t)
48 49 50 51 52 53 54 55 56 57 58 59 60 61
{
	SummaryItem *sit = summary_item_new (name, SUMMARY_TIME);
	sit->v.time = t;
	return sit;
}

SummaryItem *
summary_item_new_string (const gchar *name, const gchar *string)
{
	SummaryItem *sit = summary_item_new (name, SUMMARY_STRING);
	sit->v.txt = g_strdup (string);
	return sit;
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
char *
summary_item_as_text (const SummaryItem *sit)
{
	g_return_val_if_fail (sit != NULL, NULL);
	
	switch (sit->type) {
	case SUMMARY_STRING:
		if (sit->v.txt)
			return g_strdup (sit->v.txt);
		else
			return g_strdup ("Internal Error");
		break;
	case SUMMARY_INT:
		return g_strdup_printf ("%d", sit->v.i);
	default:
		return g_strdup ("Unhandled type");
	}
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
void
summary_item_free (SummaryItem *sit)
{
	g_return_if_fail (sit);

	switch (sit->type)
	{
	case SUMMARY_STRING:
		g_free (sit->v.txt);
		sit->v.txt = NULL;
		break;
	case SUMMARY_INT:
	case SUMMARY_TIME:
		break;
	default:
		g_warning ("unknown / unimplemented summary type");
		break;
	}
Morten Welinder's avatar
Morten Welinder committed
99
	g_free (sit->name);
100 101 102
	g_free (sit);
}

Michael Meeks's avatar
Michael Meeks committed
103
static void
104 105
summary_item_dump (SummaryItem *sit)
{
106
	char *txt;
107 108 109 110
	g_return_if_fail (sit);
	g_return_if_fail (sit->name);

	printf (" '%s' = ", sit->name);
111 112 113 114
	
	txt = summary_item_as_text (sit);
	printf (" %s\n", txt);
	g_free (txt);
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
}

static gint
g_str_case_equal (gconstpointer v, gconstpointer v2)
{
  return g_strcasecmp ((const gchar*) v, (const gchar*)v2) == 0;
}

/* a char* hash function from ASU */
static guint
g_str_case_hash (gconstpointer v)
{
  const char *s = (char*)v;
  const char *p;
  guint h=0, g;

  for(p = s; *p != '\0'; p += 1) {
    h = ( h << 4 ) + tolower (*p);
    if ( ( g = h & 0xf0000000 ) ) {
      h = h ^ (g >> 24);
      h = h ^ g;
    }
  }

  return h /* % M */;
}


SummaryInfo *
summary_info_new (void)
{
	SummaryInfo *sin = g_new (SummaryInfo, 1);
	sin->names = g_hash_table_new (g_str_case_hash,
				       g_str_case_equal);
	return sin;
}

SummaryItem *
summary_info_get (SummaryInfo *sin, char *name)
{
	g_return_val_if_fail (sin != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);
	g_return_val_if_fail (sin->names != NULL, NULL);

	return g_hash_table_lookup (sin->names, name);
}

void
summary_info_add (SummaryInfo *sin, SummaryItem *sit)
{
Michael Meeks's avatar
Michael Meeks committed
165 166
	SummaryItem *tsit;

167 168 169 170 171
	g_return_if_fail (sin != NULL);
	g_return_if_fail (sit != NULL);
	g_return_if_fail (sit->name != NULL);
	g_return_if_fail (sin->names != NULL);

Michael Meeks's avatar
Michael Meeks committed
172 173 174 175 176
	if ((tsit = summary_info_get (sin, sit->name))) {
		g_hash_table_remove (sin->names, sit->name);
		summary_item_free (tsit);
	}
	
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
	g_hash_table_insert (sin->names, sit->name, sit);
}

void
summary_info_default (SummaryInfo *sin)
{
	SummaryItem *sit;

	g_return_if_fail (sin != NULL);

	sit = summary_item_new_string (summary_item_name [SUMMARY_I_AUTHOR],
				       g_get_real_name ());
	summary_info_add (sin, sit);

	sit = summary_item_new_string (summary_item_name [SUMMARY_I_APP],
				       g_get_prgname ());
	summary_info_add (sin, sit);
}

static void
free_item (gchar *key, SummaryItem *item, void *dummy)
{
	g_return_if_fail (item);

	summary_item_free (item);
}

void
summary_info_free (SummaryInfo *sin)
{
	g_return_if_fail (sin != NULL);
	g_return_if_fail (sin->names != NULL);

	g_hash_table_foreach (sin->names, (GHFunc)free_item,
			      NULL);
	g_hash_table_destroy (sin->names);
	sin->names = NULL;
Morten Welinder's avatar
Morten Welinder committed
214
	g_free (sin);
215 216
}

Michael Meeks's avatar
Michael Meeks committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
static void
append_item (gchar *key, SummaryItem *item, GList **l)
{
	*l = g_list_append (*l, item);
}

GList *
summary_info_as_list (SummaryInfo *sin)
{
	GList *l = NULL;

	g_return_val_if_fail (sin != NULL, NULL);
	g_return_val_if_fail (sin->names != NULL, NULL);

	g_hash_table_foreach (sin->names, (GHFunc)append_item,
			      &l);

	return l;
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static void
dump_item (gchar *key, SummaryItem *item, void *dummy)
{
	summary_item_dump (item);
}

void
summary_info_dump (SummaryInfo *sin)
{
	g_return_if_fail (sin != NULL);
	g_return_if_fail (sin->names != NULL);

	printf ("summary information ...\n");

	g_hash_table_foreach (sin->names, (GHFunc)dump_item,
			      NULL);

	printf ("... end of summary information\n");
}