From c4fea4934333faac2a6629c945333697d2d15142 Mon Sep 17 00:00:00 2001 From: "Leandro A. F. Pereira" Date: Tue, 24 Oct 2006 15:25:46 +0000 Subject: Cleanups Generate report in plain text (no UI yet) Copy information to clipboard --- hardinfo2/report.c | 329 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 274 insertions(+), 55 deletions(-) (limited to 'hardinfo2/report.c') diff --git a/hardinfo2/report.c b/hardinfo2/report.c index db2a02e3..6304aacb 100644 --- a/hardinfo2/report.c +++ b/hardinfo2/report.c @@ -25,62 +25,44 @@ static ReportDialog *report_dialog_new(GtkTreeModel *model, GtkWidget *parent); static void set_all_active(ReportDialog *rd, gboolean setting); -static void -report_html_header(ReportContext *ctx) +void +report_header(ReportContext *ctx) { - fprintf(ctx->stream, - "\n" \ - "\n" \ - "HardInfo System Report\n" \ - "\n" \ - "\n" \ - ""); + ctx->header(ctx); } -static void -report_html_footer(ReportContext *ctx) +void +report_footer(ReportContext *ctx) { - fprintf(ctx->stream, - "
"); + ctx->footer(ctx); } -static void -report_html_title(ReportContext *ctx, gchar *text) +void +report_title(ReportContext *ctx, gchar *text) { - fprintf(ctx->stream, - "%s\n", text); + ctx->title(ctx, text); } -static void -report_html_subtitle(ReportContext *ctx, gchar *text) +void +report_subtitle(ReportContext *ctx, gchar *text) { - fprintf(ctx->stream, - "%s\n", text); + ctx->subtitle(ctx, text); } -static void -report_html_subsubtitle(ReportContext *ctx, gchar *text) + +void +report_subsubtitle(ReportContext *ctx, gchar *text) { - fprintf(ctx->stream, - "%s\n", text); + ctx->subsubtitle(ctx, text); } -static void -report_html_key_value(ReportContext *ctx, gchar *key, gchar *value) +void +report_key_value(ReportContext *ctx, gchar *key, gchar *value) { - fprintf(ctx->stream, - "%s" \ - "%s\n", key, value); + ctx->keyvalue(ctx, key, value); } -static void -report_html_table(ReportContext *ctx, gchar *text) +void +report_table(ReportContext *ctx, gchar *text) { GKeyFile *key_file = g_key_file_new(); gchar **groups; @@ -103,7 +85,7 @@ report_html_table(ReportContext *ctx, gchar *text) tmpgroup = g_strdup(group); strend(group, '#'); - report_html_subsubtitle(ctx, group); + report_subsubtitle(ctx, group); for (j = 0; keys[j]; j++) { gchar *key = keys[j]; @@ -125,10 +107,10 @@ report_html_table(ReportContext *ctx, gchar *text) gchar **tmp; tmp = g_strsplit(++key, "$", 0); - report_html_key_value(ctx, tmp[1], value); + report_key_value(ctx, tmp[1], value); g_strfreev(tmp); } else { - report_html_key_value(ctx, key, value); + report_key_value(ctx, key, value); } } @@ -144,6 +126,189 @@ report_html_table(ReportContext *ctx, gchar *text) g_key_file_free(key_file); } +static void +report_html_header(ReportContext *ctx) +{ + FILE *stream = (FILE*)ctx->stream; + fprintf(stream, + "\n" \ + "\n" \ + "HardInfo System Report\n" \ + "\n" \ + "\n" \ + ""); +} + +static void +report_html_footer(ReportContext *ctx) +{ + FILE *stream = (FILE*)ctx->stream; + + fprintf(stream, + "
"); +} + +static void +report_html_title(ReportContext *ctx, gchar *text) +{ + FILE *stream = (FILE*)ctx->stream; + + fprintf(stream, + "%s\n", text); +} + +static void +report_html_subtitle(ReportContext *ctx, gchar *text) +{ + FILE *stream = (FILE*)ctx->stream; + + fprintf(stream, + "%s\n", text); +} + +static void +report_html_subsubtitle(ReportContext *ctx, gchar *text) +{ + FILE *stream = (FILE*)ctx->stream; + + fprintf(stream, + "%s\n", text); +} + +static void +report_html_key_value(ReportContext *ctx, gchar *key, gchar *value) +{ + FILE *stream = (FILE*)ctx->stream; + + fprintf(stream, + "%s" \ + "%s\n", key, value); +} + +static void +report_text_header(ReportContext *ctx) +{ + FILE *stream = (FILE*)ctx->stream; + + fprintf(stream, + "HardInfo System Report\n" \ + "----------------------\n\n"); +} + +static void +report_text_footer(ReportContext *ctx) +{ +} + +static void +report_text_title(ReportContext *ctx, gchar *text) +{ + FILE *stream = (FILE*)ctx->stream; + int i = strlen(text); + + fputs(text, stream); + fputc('\n', stream); + for (; i; i--) + fputc('*', stream); + + fputc('\n', stream); +} + +static void +report_text_subtitle(ReportContext *ctx, gchar *text) +{ + FILE *stream = (FILE*)ctx->stream; + int i = strlen(text); + + fputs(text, stream); + fputc('\n', stream); + for (; i; i--) + fputc('-', stream); + + fputc('\n', stream); +} + +static void +report_text_subsubtitle(ReportContext *ctx, gchar *text) +{ + FILE *stream = (FILE*)ctx->stream; + + fputc('-', stream); + fputs(text, stream); + fputc('-', stream); + + fputc('\n', stream); +} + +static void +report_text_key_value(ReportContext *ctx, gchar *key, gchar *value) +{ + FILE *stream = (FILE*)ctx->stream; + + fprintf(stream, "%s\t\t: %s\n", key, value); +} + +static void +report_string_header(ReportContext *ctx) +{ + if (ctx->stream) + g_free(ctx->stream); + + ctx->stream = g_strdup(""); +} + +static void +report_string_footer(ReportContext *ctx) +{ +} + +static void +report_string_title(ReportContext *ctx, gchar *text) +{ + gchar *str = (gchar*)ctx->stream; + int i = strlen(text); + + str = g_strdup_printf("%s%s\n", str, text); + for (; i; i--) + str = g_strconcat(str, "*", NULL); + + str = g_strconcat(str, "\n", NULL); + ctx->stream = str; +} + +static void +report_string_subtitle(ReportContext *ctx, gchar *text) +{ + gchar *str = (gchar*)ctx->stream; + int i = strlen(text); + + str = g_strdup_printf("%s%s\n", str, text); + for (; i; i--) + str = g_strconcat(str, "-", NULL); + + str = g_strconcat(str, "\n", NULL); + ctx->stream = str; +} + +static void +report_string_subsubtitle(ReportContext *ctx, gchar *text) +{ + ctx->stream = g_strdup_printf("%s-%s-\n", (gchar*)ctx->stream, text); +} + +static void +report_string_key_value(ReportContext *ctx, gchar *key, gchar *value) +{ + ctx->stream = g_strdup_printf("%s%s\t\t: %s\n", (gchar*)ctx->stream, key, value); +} + static void report_generate_child(ReportContext *ctx, GtkTreeIter *iter) { @@ -158,8 +323,8 @@ report_generate_child(ReportContext *ctx, GtkTreeIter *iter) ctx->entry = entry; - report_html_subtitle(ctx, entry->name); - report_html_table(ctx, entry->func(entry->number)); + report_subtitle(ctx, entry->name); + report_table(ctx, entry->func(entry->number)); } static void @@ -169,7 +334,7 @@ report_generate_children(ReportContext *ctx, GtkTreeIter *iter) gchar *name; gtk_tree_model_get(model, iter, TREE_COL_NAME, &name, -1); - report_html_title(ctx, name); + report_title(ctx, name); if (gtk_tree_model_iter_has_child(model, iter)) { gint children = gtk_tree_model_iter_n_children(model, iter); @@ -212,6 +377,63 @@ report_get_filename(void) return filename; } +ReportContext* +report_context_html_new(ReportDialog *rd, gpointer stream) +{ + ReportContext *ctx; + + ctx = g_new0(ReportContext, 1); + ctx->header = report_html_header; + ctx->footer = report_html_footer; + ctx->title = report_html_title; + ctx->subtitle = report_html_subtitle; + ctx->subsubtitle = report_html_subsubtitle; + ctx->keyvalue = report_html_key_value; + + ctx->rd = rd; + ctx->stream = stream; + + return ctx; +} + +ReportContext* +report_context_text_new(ReportDialog *rd, gpointer stream) +{ + ReportContext *ctx; + + ctx = g_new0(ReportContext, 1); + ctx->header = report_text_header; + ctx->footer = report_text_footer; + ctx->title = report_text_title; + ctx->subtitle = report_text_subtitle; + ctx->subsubtitle = report_text_subsubtitle; + ctx->keyvalue = report_text_key_value; + + ctx->rd = rd; + ctx->stream = stream; + + return ctx; +} + +ReportContext* +report_context_string_new(ReportDialog *rd, gpointer string) +{ + ReportContext *ctx; + + ctx = g_new0(ReportContext, 1); + ctx->header = report_string_header; + ctx->footer = report_string_footer; + ctx->title = report_string_title; + ctx->subtitle = report_string_subtitle; + ctx->subsubtitle = report_string_subsubtitle; + ctx->keyvalue = report_string_key_value; + + ctx->rd = rd; + ctx->stream = string; + + return ctx; +} + static gboolean report_generate(ReportDialog *rd) { @@ -221,19 +443,16 @@ report_generate(ReportDialog *rd) gchar *file; FILE *stream; - file = report_get_filename(); - if (!file) + if (!(file = report_get_filename())) return FALSE; - stream = fopen(file, "w+"); - if (!stream) + + if (!(stream = fopen(file, "w+"))) return FALSE; - model = rd->model; - ctx = g_new0(ReportContext, 1); - ctx->rd = rd; - ctx->stream = stream; + model = rd->model; + ctx = report_context_html_new(rd, stream); - report_html_header(ctx); + report_header(ctx); gtk_tree_model_get_iter_first(model, &iter); @@ -241,7 +460,7 @@ report_generate(ReportDialog *rd) report_generate_children(ctx, &iter); } while (gtk_tree_model_iter_next(model, &iter)); - report_html_footer(ctx); + report_footer(ctx); fclose(ctx->stream); g_free(ctx); -- cgit v1.2.3