Italian translation update.
[rox-filer.git] / ROX-Filer / src / log.c
blob83611052ac0d348f830aaa46df5e0d05974778fe
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>
28 #include <glade/glade.h>
30 #include "global.h"
32 #include "log.h"
33 #include "main.h"
34 #include "gui_support.h"
36 static GtkTreeStore *log;
38 /* The columns in the log list store */
39 #define TIMESTAMP 0
40 #define DIRECTORY 1
41 #define MESSAGE 2
43 /* Static prototypes */
44 static void log_dialog_response(GtkDialog *dialog, gint resp_id,
45 gpointer udata);
47 /****************************************************************
48 * EXTERNAL INTERFACE *
49 ****************************************************************/
51 void log_init()
53 log = gtk_tree_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
55 log_info_paths(_("ROX-Filer started"), NULL, NULL);
57 #if 0
58 GList *paths = NULL;
60 paths = g_list_prepend(paths, "/first");
61 paths = g_list_prepend(paths, "/second");
63 log_info_paths("Test path", NULL, "/the/path");
64 log_info_paths("Test paths", paths, NULL);
65 log_info_paths("Test both", paths, "/other/path");
66 log_info_paths("Test one", paths->next, "/other/path");
67 log_info_paths("Test promote", paths->next, NULL);
68 #endif
71 /* Record a message in the log.
72 * paths is the list of items being processed, if any
73 * path is a single path (if only one is being processed)
74 * paths and path may both be given (e.g. for copying or moving)
76 void log_info_paths(const gchar *message, GList *paths, const gchar *path)
78 GtkTreeIter iter;
79 char timestamp[32];
80 time_t t;
81 struct tm *now;
82 char *actual_message = NULL;
83 int n_paths;
85 if (!message)
86 message = "(no log message!)";
88 t = time(NULL);
89 now = localtime(&t);
91 if (now == NULL || !strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M", now))
93 g_warning("Failed to generate timestamp!");
94 strcpy(timestamp, "ERROR");
97 gtk_tree_store_append(log, &iter, NULL);
99 n_paths = g_list_length(paths);
101 if (path == NULL && n_paths == 1)
103 /* Promote the single item to the main path */
104 path = paths->data;
105 paths = NULL;
106 n_paths = 0;
109 if (n_paths == 1)
110 actual_message = g_strdup_printf(_("%s '%s'"), message, g_basename((char *) paths->data));
111 else if (n_paths > 1)
112 actual_message = g_strdup_printf(_("%s on %d items"), message, n_paths);
114 gtk_tree_store_set(log, &iter,
115 TIMESTAMP, timestamp,
116 DIRECTORY, path,
117 MESSAGE, actual_message ? actual_message : message,
118 -1);
120 while (paths)
122 GtkTreeIter child_iter;
123 gtk_tree_store_append(log, &child_iter, &iter);
124 gtk_tree_store_set(log, &child_iter,
125 MESSAGE, _("Item"),
126 DIRECTORY, paths->data,
127 -1);
128 paths = paths->next;
131 g_free(actual_message);
134 /* Open the log window. */
135 void log_show_window()
137 GladeXML *glade;
138 GtkTreeView *tv;
139 GtkTreeViewColumn *column;
140 GtkCellRenderer *renderer;
142 glade = get_glade_xml("Log viewer");
144 tv = GTK_TREE_VIEW(glade_xml_get_widget(glade, "log_list"));
145 gtk_tree_view_set_model(tv, GTK_TREE_MODEL(log));
147 renderer = gtk_cell_renderer_text_new();
149 column = gtk_tree_view_column_new_with_attributes("Time", renderer,
150 "text", TIMESTAMP,
151 NULL);
152 gtk_tree_view_append_column(tv, column);
154 column = gtk_tree_view_column_new_with_attributes("Action", renderer,
155 "text", MESSAGE,
156 NULL);
157 gtk_tree_view_column_set_resizable(column, TRUE);
158 gtk_tree_view_append_column(tv, column);
160 column = gtk_tree_view_column_new_with_attributes("Path", renderer,
161 "text", DIRECTORY,
162 NULL);
163 gtk_tree_view_append_column(tv, column);
165 g_signal_connect(G_OBJECT(glade_xml_get_widget(glade, "Log viewer")),
166 "response", (GCallback) log_dialog_response, NULL);
169 /****************************************************************
170 * INTERNAL FUNCTIONS *
171 ****************************************************************/
172 static void log_dialog_response(GtkDialog *dialog, gint resp_id,
173 gpointer udata)
175 /* Only response we should get is CLOSE */
176 gtk_widget_hide(GTK_WIDGET(dialog));
177 gtk_widget_destroy(GTK_WIDGET(dialog));