Converted README to markdown
[rox-filer.git] / ROX-Filer / src / log.c
blob34b21a43b98d832fa6ad0e2ffda76c2aacf6590d
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2007, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* logging.c - the action logger */
22 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <gtk/gtk.h>
29 #include "global.h"
31 #include "log.h"
32 #include "main.h"
33 #include "gui_support.h"
35 static GtkTreeStore *log;
37 /* The columns in the log list store */
38 #define TIMESTAMP 0
39 #define DIRECTORY 1
40 #define MESSAGE 2
42 /* Static prototypes */
43 static void log_dialog_response(GtkDialog *dialog, gint resp_id,
44 gpointer udata);
46 /****************************************************************
47 * EXTERNAL INTERFACE *
48 ****************************************************************/
50 void log_init()
52 log = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
54 log_info_paths(_("ROX-Filer started"), NULL, NULL);
56 #if 0
57 GList *paths = NULL;
59 paths = g_list_prepend(paths, "/first");
60 paths = g_list_prepend(paths, "/second");
62 log_info_paths("Test path", NULL, "/the/path");
63 log_info_paths("Test paths", paths, NULL);
64 log_info_paths("Test both", paths, "/other/path");
65 log_info_paths("Test one", paths->next, "/other/path");
66 log_info_paths("Test promote", paths->next, NULL);
67 #endif
70 /* Record a message in the log.
71 * paths is the list of items being processed, if any
72 * path is a single path (if only one is being processed)
73 * paths and path may both be given (e.g. for copying or moving)
75 void log_info_paths(const gchar *message, GList *paths, const gchar *path)
77 GtkTreeIter iter;
78 char timestamp[32];
79 time_t t;
80 struct tm *now;
81 char *actual_message = NULL;
82 int n_paths;
84 if (!message)
85 message = "(no log message!)";
87 t = time(NULL);
88 now = localtime(&t);
90 if (now == NULL || !strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M", now))
92 g_warning("Failed to generate timestamp!");
93 strcpy(timestamp, "ERROR");
96 gtk_tree_store_append(log, &iter, NULL);
98 n_paths = g_list_length(paths);
100 if (path == NULL && n_paths == 1)
102 /* Promote the single item to the main path */
103 path = paths->data;
104 paths = NULL;
105 n_paths = 0;
108 if (n_paths == 1)
109 actual_message = g_strdup_printf(_("%s '%s'"), message, g_basename((char *) paths->data));
110 else if (n_paths > 1)
111 actual_message = g_strdup_printf(_("%s on %d items"), message, n_paths);
113 gtk_tree_store_set(log, &iter,
114 TIMESTAMP, timestamp,
115 DIRECTORY, path,
116 MESSAGE, actual_message ? actual_message : message,
117 -1);
119 while (paths)
121 GtkTreeIter child_iter;
122 gtk_tree_store_append(log, &child_iter, &iter);
123 gtk_tree_store_set(log, &child_iter,
124 MESSAGE, _("Item"),
125 DIRECTORY, paths->data,
126 -1);
127 paths = paths->next;
130 g_free(actual_message);
133 /* Open the log window. */
134 void log_show_window()
136 GtkWidget *dialog;
137 GtkBuilder *builder;
138 GtkTreeView *tv;
139 GtkTreeViewColumn *column;
140 GtkCellRenderer *renderer;
141 gchar *ids[] = {"Log viewer", NULL};
143 builder = get_gtk_builder(ids);
145 tv = GTK_TREE_VIEW(gtk_builder_get_object(builder, "log_list"));
146 gtk_tree_view_set_model(tv, GTK_TREE_MODEL(log));
148 renderer = gtk_cell_renderer_text_new();
150 column = gtk_tree_view_column_new_with_attributes(_("Time"), renderer,
151 "text", TIMESTAMP,
152 NULL);
153 gtk_tree_view_append_column(tv, column);
155 column = gtk_tree_view_column_new_with_attributes(_("Action"), renderer,
156 "text", MESSAGE,
157 NULL);
158 gtk_tree_view_column_set_resizable(column, TRUE);
159 gtk_tree_view_append_column(tv, column);
161 column = gtk_tree_view_column_new_with_attributes(_("Path"), renderer,
162 "text", DIRECTORY,
163 NULL);
164 gtk_tree_view_append_column(tv, column);
166 dialog = GTK_WIDGET(gtk_builder_get_object(builder, "Log viewer"));
167 g_signal_connect(G_OBJECT(dialog),
168 "response", (GCallback) log_dialog_response, NULL);
170 gtk_widget_show(dialog);
171 g_object_unref(builder);
174 /****************************************************************
175 * INTERNAL FUNCTIONS *
176 ****************************************************************/
177 static void log_dialog_response(GtkDialog *dialog, gint resp_id,
178 gpointer udata)
180 /* Only response we should get is CLOSE */
181 gtk_widget_hide(GTK_WIDGET(dialog));
182 gtk_widget_destroy(GTK_WIDGET(dialog));