summary.c 5.62 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
#include <config.h>
#include <ctype.h>
#include <glib.h>
#include "summary.h"
Jody Goldberg's avatar
Jody Goldberg committed
15
#include "gutils.h"
16

Michael Meeks's avatar
Michael Meeks committed
17 18 19 20 21 22
/*
 *  NOTE:
 *  These strings are related to the fields in src/dialogs/summary.glade
 *  The field names are summary_item_name[i] prefixed with "glade_".
 *
 */
23
gchar *summary_item_name[] = {
Michael Meeks's avatar
Michael Meeks committed
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
	"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"
54 55 56 57 58 59 60 61 62 63 64 65 66
};

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

Michael Meeks's avatar
Michael Meeks committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
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;
}

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

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

112
	g_return_val_if_fail (sit != NULL, NULL);
Jody Goldberg's avatar
Jody Goldberg committed
113

114 115 116 117
	switch (sit->type) {
	case SUMMARY_STRING:
		if (sit->v.txt)
			return g_strdup (sit->v.txt);
Jody Goldberg's avatar
Jody Goldberg committed
118
		return g_strdup ("Internal Error");
Michael Meeks's avatar
Michael Meeks committed
119 120

	case SUMMARY_BOOLEAN:
Jody Goldberg's avatar
Jody Goldberg committed
121 122 123 124 125 126 127
		if (sit->v.boolean == 0)
			return g_strdup ("False");

		if (sit->v.boolean == 1)
			return g_strdup ("True");

		return "Unrecognized boolean value";
Michael Meeks's avatar
Michael Meeks committed
128 129 130 131

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

132 133
	case SUMMARY_INT:
		return g_strdup_printf ("%d", sit->v.i);
Michael Meeks's avatar
Michael Meeks committed
134 135 136 137 138 139 140

	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);

141 142 143 144 145
	default:
		return g_strdup ("Unhandled type");
	}
}

146 147 148 149 150
void
summary_item_free (SummaryItem *sit)
{
	g_return_if_fail (sit);

151
	switch (sit->type) {
152 153 154 155
	case SUMMARY_STRING:
		g_free (sit->v.txt);
		sit->v.txt = NULL;
		break;
Michael Meeks's avatar
Michael Meeks committed
156 157 158

	case SUMMARY_BOOLEAN:
	case SUMMARY_SHORT:
159 160 161 162 163 164 165
	case SUMMARY_INT:
	case SUMMARY_TIME:
		break;
	default:
		g_warning ("unknown / unimplemented summary type");
		break;
	}
Morten Welinder's avatar
Morten Welinder committed
166
	g_free (sit->name);
167 168 169
	g_free (sit);
}

Michael Meeks's avatar
Michael Meeks committed
170
static void
171 172
summary_item_dump (SummaryItem *sit)
{
173
	char *txt;
174 175 176 177
	g_return_if_fail (sit);
	g_return_if_fail (sit->name);

	printf (" '%s' = ", sit->name);
Jody Goldberg's avatar
Jody Goldberg committed
178

179 180 181
	txt = summary_item_as_text (sit);
	printf (" %s\n", txt);
	g_free (txt);
182 183 184 185 186 187
}

SummaryInfo *
summary_info_new (void)
{
	SummaryInfo *sin = g_new (SummaryInfo, 1);
Jody Goldberg's avatar
Jody Goldberg committed
188 189
	sin->names = g_hash_table_new (&gnumeric_strcase_hash,
				       &gnumeric_strcase_equal);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	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
206 207
	SummaryItem *tsit;

208 209 210 211 212
	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
213 214 215 216
	if ((tsit = summary_info_get (sin, sit->name))) {
		g_hash_table_remove (sin->names, sit->name);
		summary_item_free (tsit);
	}
Jody Goldberg's avatar
Jody Goldberg committed
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
	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
255
	g_free (sin);
256 257
}

Michael Meeks's avatar
Michael Meeks committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
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;
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
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");
}