1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published by
4  * the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9  * for more details.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, see <http://www.gnu.org/licenses/>.
13  *
14  *
15  * Authors:
16  *		Chris Lahey <clahey@ximian.com>
17  *
18  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
19  *
20  */
21 
22 #include "evolution-config.h"
23 
24 #include "e-cell-date.h"
25 
26 #include <sys/time.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include <string.h>
30 
31 #include <glib/gi18n.h>
32 
33 #include "e-datetime-format.h"
34 #include "e-unicode.h"
35 
G_DEFINE_TYPE(ECellDate,e_cell_date,E_TYPE_CELL_TEXT)36 G_DEFINE_TYPE (ECellDate, e_cell_date, E_TYPE_CELL_TEXT)
37 
38 static gchar *
39 ecd_get_text (ECellText *cell,
40               ETableModel *model,
41               gint col,
42               gint row)
43 {
44 	gint64 *pdate = e_table_model_value_at (model, col, row);
45 	gchar *res;
46 
47 	if (!pdate || *pdate == 0) {
48 		e_table_model_free_value (model, col, pdate);
49 		return g_strdup (_("?"));
50 	}
51 
52 	res = e_cell_date_value_to_text (E_CELL_DATE (cell), *pdate, FALSE);
53 
54 	e_table_model_free_value (model, col, pdate);
55 
56 	return res;
57 }
58 
59 static void
ecd_free_text(ECellText * cell,ETableModel * model,gint col,gchar * text)60 ecd_free_text (ECellText *cell,
61 	       ETableModel *model,
62 	       gint col,
63                gchar *text)
64 {
65 	g_free (text);
66 }
67 
68 static void
e_cell_date_class_init(ECellDateClass * class)69 e_cell_date_class_init (ECellDateClass *class)
70 {
71 	ECellTextClass *ectc = E_CELL_TEXT_CLASS (class);
72 
73 	ectc->get_text = ecd_get_text;
74 	ectc->free_text = ecd_free_text;
75 }
76 
77 static void
e_cell_date_init(ECellDate * ecd)78 e_cell_date_init (ECellDate *ecd)
79 {
80 	g_object_set (ecd, "use-tabular-numbers", TRUE, NULL);
81 }
82 
83 /**
84  * e_cell_date_new:
85  * @fontname: font to be used to render on the screen
86  * @justify: Justification of the string in the cell.
87  *
88  * Creates a new ECell renderer that can be used to render dates that
89  * that come from the model.  The value returned from the model is
90  * interpreted as being a time_t.
91  *
92  * The ECellDate object support a large set of properties that can be
93  * configured through the Gtk argument system and allows the user to have
94  * a finer control of the way the string is displayed.  The arguments supported
95  * allow the control of strikeout, bold, color and a date filter.
96  *
97  * The arguments "strikeout_column", "underline_column", "bold_column"
98  * and "color_column" set and return an integer that points to a
99  * column in the model that controls these settings.  So controlling
100  * the way things are rendered is achieved by having special columns
101  * in the model that will be used to flag whether the date should be
102  * rendered with strikeout, underline, or bolded.  In the case of the
103  * "color_column" argument, the column in the model is expected to
104  * have a string that can be parsed by gdk_color_parse().
105  *
106  * Returns: an ECell object that can be used to render dates.
107  */
108 ECell *
e_cell_date_new(const gchar * fontname,GtkJustification justify)109 e_cell_date_new (const gchar *fontname,
110                  GtkJustification justify)
111 {
112 	ECellDate *ecd = g_object_new (E_TYPE_CELL_DATE, NULL);
113 
114 	e_cell_text_construct (E_CELL_TEXT (ecd), fontname, justify);
115 
116 	return (ECell *) ecd;
117 }
118 
119 void
e_cell_date_set_format_component(ECellDate * ecd,const gchar * fmt_component)120 e_cell_date_set_format_component (ECellDate *ecd,
121                                   const gchar *fmt_component)
122 {
123 	g_return_if_fail (ecd != NULL);
124 
125 	g_object_set_data_full (
126 		G_OBJECT (ecd), "fmt-component",
127 		g_strdup (fmt_component), g_free);
128 }
129 
130 gchar *
e_cell_date_value_to_text(ECellDate * ecd,gint64 value,gboolean date_only)131 e_cell_date_value_to_text (ECellDate *ecd,
132 			   gint64 value,
133 			   gboolean date_only)
134 {
135 	const gchar *fmt_component, *fmt_part = NULL;
136 
137 	if (value == 0)
138 		return g_strdup (_("?"));
139 
140 	fmt_component = g_object_get_data ((GObject *) ecd, "fmt-component");
141 	if (!fmt_component || !*fmt_component)
142 		fmt_component = "Default";
143 	else
144 		fmt_part = "table";
145 
146 	return e_datetime_format_format (fmt_component, fmt_part,
147 		date_only ? DTFormatKindDate : DTFormatKindDateTime, (time_t) value);
148 }
149 
150 gchar *
e_cell_date_tm_to_text(ECellDate * ecd,struct tm * tm_time,gboolean date_only)151 e_cell_date_tm_to_text (ECellDate *ecd,
152 			struct tm *tm_time,
153 			gboolean date_only)
154 {
155 	const gchar *fmt_component, *fmt_part = NULL;
156 
157 	if (!tm_time)
158 		return g_strdup (_("?"));
159 
160 	fmt_component = g_object_get_data ((GObject *) ecd, "fmt-component");
161 	if (!fmt_component || !*fmt_component)
162 		fmt_component = "Default";
163 	else
164 		fmt_part = "table";
165 
166 	return e_datetime_format_format_tm (fmt_component, fmt_part,
167 		date_only ? DTFormatKindDate : DTFormatKindDateTime, tm_time);
168 }
169