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 #if !defined (__E_UTIL_H_INSIDE__) && !defined (LIBEUTIL_COMPILATION)
23 #error "Only <e-util/e-util.h> should be included directly."
24 #endif
25 
26 #ifndef E_MISC_UTILS_H
27 #define E_MISC_UTILS_H
28 
29 #include <sys/types.h>
30 #include <gtk/gtk.h>
31 #include <limits.h>
32 
33 #include <libedataserver/libedataserver.h>
34 
35 #include <e-util/e-marshal.h>
36 #include <e-util/e-util-enums.h>
37 
38 G_BEGIN_DECLS
39 
40 typedef enum {
41 	E_FOCUS_NONE,
42 	E_FOCUS_CURRENT,
43 	E_FOCUS_START,
44 	E_FOCUS_END
45 } EFocus;
46 
47 typedef enum {
48 	E_RESTORE_WINDOW_SIZE = 1 << 0,
49 	E_RESTORE_WINDOW_POSITION = 1 << 1
50 } ERestoreWindowFlags;
51 
52 typedef void	(*EForeachFunc)			(gint model_row,
53 						 gpointer closure);
54 
55 const gchar *	e_get_accels_filename		(void);
56 void		e_show_uri			(GtkWindow *parent,
57 						 const gchar *uri);
58 void		e_display_help			(GtkWindow *parent,
59 						 const gchar *link_id);
60 void		e_restore_window		(GtkWindow *window,
61 						 const gchar *settings_path,
62 						 ERestoreWindowFlags flags);
63 GtkAction *	e_lookup_action			(GtkUIManager *ui_manager,
64 						 const gchar *action_name);
65 GtkActionGroup *e_lookup_action_group		(GtkUIManager *ui_manager,
66 						 const gchar *group_name);
67 gint		e_action_compare_by_label	(GtkAction *action1,
68 						 GtkAction *action2);
69 void		e_action_group_remove_all_actions
70 						(GtkActionGroup *action_group);
71 GtkRadioAction *e_radio_action_get_current_action
72 						(GtkRadioAction *radio_action);
73 void		e_action_group_add_actions_localized
74 						(GtkActionGroup *action_group,
75 						 const gchar *translation_domain,
76 						 const GtkActionEntry *entries,
77 						 guint n_entries,
78 						 gpointer user_data);
79 GtkWidget *	e_builder_get_widget		(GtkBuilder *builder,
80 						 const gchar *widget_name);
81 void		e_load_ui_builder_definition	(GtkBuilder *builder,
82 						 const gchar *basename);
83 guint		e_load_ui_manager_definition	(GtkUIManager *ui_manager,
84 						 const gchar *basename);
85 void		e_categories_add_change_hook	(GHookFunc func,
86 						 gpointer object);
87 
88 /* String to/from double conversion functions */
89 gdouble		e_flexible_strtod		(const gchar *nptr,
90 						 gchar **endptr);
91 
92 /* 29 bytes should enough for all possible values that
93  * g_ascii_dtostr can produce with the %.17g format.
94  * Then add 10 for good measure */
95 #define E_ASCII_DTOSTR_BUF_SIZE (DBL_DIG + 12 + 10)
96 gchar *		e_ascii_dtostr			(gchar *buffer,
97 						 gint buf_len,
98 						 const gchar *format,
99 						 gdouble d);
100 
101 gchar *		e_str_without_underscores	(const gchar *string);
102 GString *	e_str_replace_string		(const gchar *text,
103 						 const gchar *find,
104 						 const gchar *replace);
105 gint		e_str_compare			(gconstpointer x,
106 						 gconstpointer y);
107 gint		e_str_case_compare		(gconstpointer x,
108 						 gconstpointer y);
109 gint		e_collate_compare		(gconstpointer x,
110 						 gconstpointer y);
111 gint		e_int_compare                   (gconstpointer x,
112 						 gconstpointer y);
113 guint32		e_color_to_value		(const GdkColor *color);
114 
115 guint32		e_rgba_to_value			(const GdkRGBA *rgba);
116 
117 void		e_rgba_to_color			(const GdkRGBA *rgba,
118 						 GdkColor *color);
119 
120 void		e_utils_get_theme_color		(GtkWidget *widget,
121 						 const gchar *color_names,
122 						 const gchar *fallback_color_ident,
123 						 GdkRGBA *rgba);
124 
125 void		e_utils_get_theme_color_color	(GtkWidget *widget,
126 						 const gchar *color_names,
127 						 const gchar *fallback_color_ident,
128 						 GdkColor *color);
129 
130 #define E_UTILS_LIGHTNESS_MULT	1.3
131 #define E_UTILS_DARKNESS_MULT	0.7
132 #define E_UTILS_DEFAULT_THEME_BG_COLOR				"#AAAAAA"
133 #define E_UTILS_DEFAULT_THEME_BASE_COLOR			"#FFFFFF"
134 #define E_UTILS_DEFAULT_THEME_FG_COLOR				"#000000"
135 #define E_UTILS_DEFAULT_THEME_TEXT_COLOR			"#000000"
136 #define E_UTILS_DEFAULT_THEME_SELECTED_BG_COLOR			"#729fcf"
137 #define E_UTILS_DEFAULT_THEME_SELECTED_FG_COLOR			"#000000"
138 #define E_UTILS_DEFAULT_THEME_UNFOCUSED_SELECTED_BG_COLOR	"#808080"
139 #define E_UTILS_DEFAULT_THEME_UNFOCUSED_SELECTED_FG_COLOR	"#000000"
140 
141 void		e_utils_shade_color		(const GdkRGBA *a,
142 						 GdkRGBA *b,
143 						 gdouble mult);
144 
145 GdkRGBA		e_utils_get_text_color_for_background
146 						(const GdkRGBA *bg_rgba);
147 
148 /* This only makes a filename safe for usage as a filename.
149  * It still may have shell meta-characters in it. */
150 gchar *		e_format_number			(gint number);
151 
152 typedef gint	(*ESortCompareFunc)		(gconstpointer first,
153 						 gconstpointer second,
154 						 gpointer closure);
155 
156 void		e_bsearch			(gconstpointer key,
157 						 gconstpointer base,
158 						 gsize nmemb,
159 						 gsize size,
160 						 ESortCompareFunc compare,
161 						 gpointer closure,
162 						 gsize *start,
163 						 gsize *end);
164 
165 gsize		e_strftime_fix_am_pm		(gchar *str,
166 						 gsize max,
167 						 const gchar *fmt,
168 						 const struct tm *tm);
169 gsize		e_utf8_strftime_fix_am_pm	(gchar *str,
170 						 gsize max,
171 						 const gchar *fmt,
172 						 const struct tm *tm);
173 gsize		e_utf8_strftime_match_lc_messages
174 						(gchar *string,
175 						 gsize max,
176 						 const gchar *fmt,
177 						 const struct tm *tm);
178 const gchar *	e_get_month_name		(GDateMonth month,
179 						 gboolean abbreviated);
180 const gchar *	e_get_weekday_name		(GDateWeekday weekday,
181 						 gboolean abbreviated);
182 GDateWeekday	e_weekday_get_next		(GDateWeekday weekday);
183 GDateWeekday	e_weekday_get_prev		(GDateWeekday weekday);
184 GDateWeekday	e_weekday_add_days		(GDateWeekday weekday,
185 						 guint n_days);
186 GDateWeekday	e_weekday_subtract_days		(GDateWeekday weekday,
187 						 guint n_days);
188 guint		e_weekday_get_days_between	(GDateWeekday weekday1,
189 						 GDateWeekday weekday2);
190 gint		e_weekday_to_tm_wday		(GDateWeekday weekday);
191 GDateWeekday	e_weekday_from_tm_wday		(gint tm_wday);
192 
193 gboolean	e_file_lock_create		(void);
194 void		e_file_lock_destroy		(void);
195 gboolean	e_file_lock_exists		(void);
196 GPid		e_file_lock_get_pid		(void);
197 
198 gchar *		e_util_guess_mime_type		(const gchar *filename,
199                                                  gboolean localfile);
200 
201 GSList *	e_util_get_category_filter_options
202 						(void);
203 GList *		e_util_dup_searchable_categories (void);
204 
205 gboolean	e_util_get_open_source_job_info	(const gchar *extension_name,
206 						 const gchar *source_display_name,
207 						 gchar **description,
208 						 gchar **alert_ident,
209 						 gchar **alert_arg_0);
210 struct _EAlertSinkThreadJobData;
211 void		e_util_propagate_open_source_job_error
212 						(struct _EAlertSinkThreadJobData *job_data,
213 						 const gchar *extension_name,
214 						 GError *local_error,
215 						 GError **error);
216 struct _EClientCache;
217 EClient *	e_util_open_client_sync		(struct _EAlertSinkThreadJobData *job_data,
218 						 struct _EClientCache *client_cache,
219 						 const gchar *extension_name,
220 						 ESource *source,
221 						 guint32 wait_for_connected_seconds,
222 						 GCancellable *cancellable,
223 						 GError **error);
224 
225 /* Useful GBinding transform functions */
226 gboolean	e_binding_transform_color_to_string
227 						(GBinding *binding,
228 						 const GValue *source_value,
229 						 GValue *target_value,
230 						 gpointer not_used);
231 gboolean	e_binding_transform_string_to_color
232 						(GBinding *binding,
233 						 const GValue *source_value,
234 						 GValue *target_value,
235 						 gpointer not_used);
236 gboolean	e_binding_transform_source_to_uid
237 						(GBinding *binding,
238 						 const GValue *source_value,
239 						 GValue *target_value,
240 						 ESourceRegistry *registry);
241 gboolean	e_binding_transform_uid_to_source
242 						(GBinding *binding,
243 						 const GValue *source_value,
244 						 GValue *target_value,
245 						 ESourceRegistry *registry);
246 
247 gboolean	e_binding_transform_text_non_null
248 						(GBinding *binding,
249 						 const GValue *source_value,
250 						 GValue *target_value,
251 						 gpointer user_data);
252 
253 GBinding *	e_binding_bind_object_text_property
254 						(gpointer source,
255 						 const gchar *source_property,
256 						 gpointer target,
257 						 const gchar *target_property,
258 						 GBindingFlags flags);
259 
260 gulong		e_signal_connect_notify		(gpointer instance,
261 						 const gchar *notify_name,
262 						 GCallback c_handler,
263 						 gpointer user_data);
264 gulong		e_signal_connect_notify_after	(gpointer instance,
265 						 const gchar *notify_name,
266 						 GCallback c_handler,
267 						 gpointer user_data);
268 gulong		e_signal_connect_notify_swapped	(gpointer instance,
269 						 const gchar *notify_name,
270 						 GCallback c_handler,
271 						 gpointer user_data);
272 gulong		e_signal_connect_notify_object	(gpointer instance,
273 						 const gchar *notify_name,
274 						 GCallback c_handler,
275 						 gpointer gobject,
276 						 GConnectFlags connect_flags);
277 void		e_signal_disconnect_notify_handler
278 						(gpointer instance,
279 						 gulong *handler_id);
280 
281 GSettings *	e_util_ref_settings		(const gchar *schema_id);
282 void		e_util_cleanup_settings		(void);
283 gboolean	e_util_prompt_user		(GtkWindow *parent,
284 						 const gchar *settings_schema,
285 						 const gchar *promptkey,
286 						 const gchar *tag,
287 						 ...);
288 void		e_util_run_simple_async_result_in_thread
289 						(GSimpleAsyncResult *simple,
290 						 GSimpleAsyncThreadFunc func,
291 						 GCancellable *cancellable);
292 gboolean	e_util_is_running_gnome		(void);
293 gboolean	e_util_is_running_flatpak	(void);
294 void		e_util_set_entry_issue_hint	(GtkWidget *entry,
295 						 const gchar *hint);
296 
297 guint		e_util_normalize_font_size	(GtkWidget *widget,
298 						 gdouble font_size);
299 void		e_util_init_main_thread		(GThread *thread);
300 gboolean	e_util_is_main_thread		(GThread *thread);
301 gchar *		e_util_save_image_from_clipboard
302 						(GtkClipboard *clipboard);
303 gboolean	e_util_check_gtk_bindings_in_key_press_event_cb
304 						(GtkWidget *widget,
305 						 GdkEvent *event);
306 void		e_util_save_file_chooser_folder	(GtkFileChooser *file_chooser);
307 void		e_util_load_file_chooser_folder	(GtkFileChooser *file_chooser);
308 gboolean	e_util_get_webkit_developer_mode_enabled
309 						(void);
310 gchar *		e_util_next_uri_from_uri_list	(guchar **uri_list,
311 						 gint *len,
312 						 gint *list_len);
313 void		e_util_resize_window_for_screen	(GtkWindow *window,
314 						 gint window_width,
315 						 gint window_height,
316 						 const GSList *children); /* GtkWidget * */
317 gboolean	e_util_query_ldap_root_dse_sync	(const gchar *host,
318 						 guint16 port,
319 						 gchar ***out_root_dse,
320 						 GCancellable *cancellable,
321 						 GError **error);
322 gchar *		e_util_get_uri_tooltip		(const gchar *uri);
323 gchar *		e_util_get_language_name	(const gchar *language_tag);
324 gboolean	e_util_get_language_info	(const gchar *language_tag,
325 						 gchar **out_language_name,
326 						 gchar **out_country_name);
327 void		e_misc_util_free_global_memory	(void);
328 gboolean	e_util_can_preview_filename	(const gchar *filename);
329 void		e_util_markup_append_escaped	(GString *buffer,
330 						 const gchar *format,
331 						 ...) G_GNUC_PRINTF (2, 3);
332 
333 typedef struct _ESupportedLocales {
334 	const gchar *code;	/* like 'en' */
335 	const gchar *locale;	/* like 'en_US' */
336 } ESupportedLocales;
337 
338 void		e_util_enum_supported_locales	(void);
339 const ESupportedLocales *
340 		e_util_get_supported_locales	(void);
341 
342 void		e_util_ensure_scrolled_window_height
343 						(GtkScrolledWindow *scrolled_window);
344 void		e_util_make_safe_filename	(gchar *filename);
345 
346 G_END_DECLS
347 
348 #endif /* E_MISC_UTILS_H */
349