summary.c 6.09 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>
Michael Meeks's avatar
Michael Meeks committed
10
#include <time.h>
11 12 13 14 15
#include <config.h>
#include <ctype.h>
#include <glib.h>
#include "summary.h"

Michael Meeks's avatar
Michael Meeks committed
16 17 18 19 20 21
/*
 *  NOTE:
 *  These strings are related to the fields in src/dialogs/summary.glade
 *  The field names are summary_item_name[i] prefixed with "glade_".
 *
 */
22
gchar *summary_item_name[] = {
Michael Meeks's avatar
Michael Meeks committed
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
	"codepage",
	"title",
	"subject",
	"author",
	"keywords",
	"comments",
	"template",
	"last_author",
	"revision_number",
	"last_printed",
	"created",
	"last_saved",
	"page_count",
	"word_count",
	"character_count",
	"application",
	"security",
	"category",
	"presentation_format",
	"number_of_bytes",
	"number_of_lines",
	"number_of_paragraphs",
	"number_of_slides",
	"number_of_pages_with_notes",
	"number_of_hidden_slides",
	"number_of_sound_or_video_clips",
	"thumbnail_scaling",
	"manager",
	"company",
	"links_uptodate"
53 54 55 56 57 58 59 60 61 62 63 64 65
};

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 *
66
summary_item_new_int (const gchar *name, gint i)
67 68 69 70 71 72
{
	SummaryItem *sit = summary_item_new (name, SUMMARY_INT);
	sit->v.i = i;
	return sit;
}

Michael Meeks's avatar
Michael Meeks committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
SummaryItem *
summary_item_new_boolean (const gchar *name, gboolean i)
{
	SummaryItem *sit = summary_item_new (name, SUMMARY_BOOLEAN);
	sit->v.boolean = i;
	return sit;
}

SummaryItem *
summary_item_new_short (const gchar *name, gshort i)
{
	SummaryItem *sit = summary_item_new (name, SUMMARY_SHORT);
	sit->v.short_i = i;
	return sit;
}

89
SummaryItem *
90
summary_item_new_time (const gchar *name, GTimeVal t)
91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
	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;
}

105 106 107
char *
summary_item_as_text (const SummaryItem *sit)
{
Michael Meeks's avatar
Michael Meeks committed
108 109 110
	char   *ch_time;
	time_t  time;

111 112 113 114 115 116 117 118 119
	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;
Michael Meeks's avatar
Michael Meeks committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

	case SUMMARY_BOOLEAN:
		if      (sit->v.boolean == 0)
			return "False";
			
		else if (sit->v.boolean == 1)
			return "True";
			
		else
			return "Unrecognized boolean value";
		break;

	case SUMMARY_SHORT:
		return g_strdup_printf ("%d", sit->v.short_i);
		break;

136 137
	case SUMMARY_INT:
		return g_strdup_printf ("%d", sit->v.i);
Michael Meeks's avatar
Michael Meeks committed
138 139 140 141 142 143 144 145 146
		break;

	case SUMMARY_TIME:
		time = (time_t)sit->v.time.tv_sec;
		ch_time = ctime(&time);
		ch_time[strlen(ch_time)-1] = '\0';
		return g_strdup (ch_time);
		break;

147 148 149 150 151
	default:
		return g_strdup ("Unhandled type");
	}
}

152 153 154 155 156
void
summary_item_free (SummaryItem *sit)
{
	g_return_if_fail (sit);

157
	switch (sit->type) {
158 159 160 161
	case SUMMARY_STRING:
		g_free (sit->v.txt);
		sit->v.txt = NULL;
		break;
Michael Meeks's avatar
Michael Meeks committed
162 163 164

	case SUMMARY_BOOLEAN:
	case SUMMARY_SHORT:
165 166 167 168 169 170 171
	case SUMMARY_INT:
	case SUMMARY_TIME:
		break;
	default:
		g_warning ("unknown / unimplemented summary type");
		break;
	}
Morten Welinder's avatar
Morten Welinder committed
172
	g_free (sit->name);
173 174 175
	g_free (sit);
}

Michael Meeks's avatar
Michael Meeks committed
176
static void
177 178
summary_item_dump (SummaryItem *sit)
{
179
	char *txt;
180 181 182 183
	g_return_if_fail (sit);
	g_return_if_fail (sit->name);

	printf (" '%s' = ", sit->name);
184 185 186 187
	
	txt = summary_item_as_text (sit);
	printf (" %s\n", txt);
	g_free (txt);
188 189 190 191 192
}

static gint
g_str_case_equal (gconstpointer v, gconstpointer v2)
{
193
	  return g_strcasecmp ((const gchar*) v, (const gchar*)v2) == 0;
194 195 196 197 198 199
}

/* a char* hash function from ASU */
static guint
g_str_case_hash (gconstpointer v)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
200 201 202 203 204 205 206 207 208 209 210 211 212
	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 */;
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
}


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
238 239
	SummaryItem *tsit;

240 241 242 243 244
	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
245 246 247 248 249
	if ((tsit = summary_info_get (sin, sit->name))) {
		g_hash_table_remove (sin->names, sit->name);
		summary_item_free (tsit);
	}
	
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
	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
287
	g_free (sin);
288 289
}

Michael Meeks's avatar
Michael Meeks committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
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;
}

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
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");
}