1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * anjuta-utils.h
4  * Copyright (C) Naba Kumar  <naba@gnome.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 #ifndef _ANJUTA_UTILS_H_
21 #define _ANJUTA_UTILS_H_
22 
23 #include <glib.h>
24 #include <gtk/gtk.h>
25 #include <sys/types.h>
26 #include <gio/gio.h>
27 #include <libanjuta/interfaces/ianjuta-iterable.h>
28 
29 G_BEGIN_DECLS
30 
31 gboolean	anjuta_util_copy_file					(const gchar * src,
32 						                             const gchar * dest,
33 	                           						 gboolean show_error);
34 
35 gboolean	anjuta_util_diff						(const gchar* uri,
36 							                         const gchar* text);
37 
38 void		anjuta_util_color_from_string			(const gchar * val, guint16 * r,
39 		                               				guint16 * g, guint16 * b);
40 
41 gchar*		anjuta_util_string_from_color			(guint16 r, guint16 g, guint16 b);
42 
43 GtkWidget*  anjuta_util_button_new_with_stock_image (const gchar* text,
44 													const gchar* stock_id);
45 
46 GtkWidget*  anjuta_util_dialog_add_button			(GtkDialog *dialog,
47 			                                         const gchar* text,
48 													 const gchar* stock_id,
49  													 gint response_id);
50 
51 void		anjuta_util_dialog_error				(GtkWindow *parent,
52 						                             const gchar * mesg, ...);
53 void		anjuta_util_dialog_warning				(GtkWindow *parent,
54 						                             const gchar * mesg, ...);
55 void		anjuta_util_dialog_info					(GtkWindow *parent,
56 							                         const gchar * mesg, ...);
57 void		anjuta_util_dialog_error_system			(GtkWindow* parent, gint errnum,
58 													const gchar * mesg, ... );
59 gboolean	anjuta_util_dialog_boolean_question		(GtkWindow *parent,
60 													gboolean default_to_yes,
61 													const gchar * mesg, ...);
62 gboolean	anjuta_util_dialog_input				(GtkWindow *parent,
63 					                                 const gchar *label,
64 													 const gchar *default_value,
65 					                                 gchar **value);
66 
67 gboolean	anjuta_util_install_files				(const gchar * const names);
68 gboolean	anjuta_util_package_is_installed		(const gchar * lib, gboolean show);
69 gboolean	anjuta_util_prog_is_installed			(const gchar * prog, gboolean show);
70 
71 gchar*		anjuta_util_get_a_tmp_file				(void);
72 
73 gchar*		anjuta_util_convert_to_utf8				(const gchar *str);
74 
75 gboolean	anjuta_util_jump_to_matching_brace		(IAnjutaIterable *iter,
76 			                                         gchar brace, gint limit);
77 
78 GList*		anjuta_util_parse_args_from_string		(const gchar* string);
79 
80 /***********************************************/
81 /* String integer mapping utility functions    */
82 /***********************************************/
83 typedef struct _AnjutaUtilStringMap
84 {
85 	int type;
86 	char *name;
87 } AnjutaUtilStringMap;
88 
89 int			anjuta_util_type_from_string			(AnjutaUtilStringMap *map,
90 						                             const char *str);
91 const char *anjuta_util_string_from_type			(AnjutaUtilStringMap *map,
92 			                                         int type);
93 GList *		anjuta_util_glist_from_map				(AnjutaUtilStringMap *map);
94 
95 /***********************************************/
96 /*  Functions that operate on list of strings. */
97 /***********************************************/
98 void		anjuta_util_glist_strings_free			(GList* list);
99 void		anjuta_util_glist_strings_prefix		(GList * list, const gchar *prefix);
100 void		anjuta_util_glist_strings_sufix			(GList * list, const gchar *sufix);
101 GList*		anjuta_util_glist_strings_sort			(GList * list);
102 gchar*		anjuta_util_glist_strings_join			(GList * list, gchar *delimiter);
103 
104 /**********************************************************/
105 /* Both the returned glist and the data should be g_freed */
106 /* Call g_list_strings_free() to do that.                 */
107 /**********************************************************/
108 GList*		anjuta_util_glist_from_string			(const gchar* id);
109 GList*		anjuta_util_glist_strings_dup			(GList * list);
110 
111 /* Dedup a list of paths - duplicates are removed from the tail.
112 ** Useful for deduping Recent Files and Recent Projects */
113 GList*		anjuta_util_glist_path_dedup			(GList *list);
114 
115 /* Adds the given string in the list, if it does not already exist. */
116 /* The added string will come at the top of the list */
117 /* The list will be then truncated to (length) items only */
118 GList *		anjuta_util_update_string_list			(GList *p_list,
119 					                            	const gchar *p_str,
120 													gint length);
121 
122 gboolean	anjuta_util_create_dir					(const gchar * d);
123 gchar *		anjuta_util_user_shell					(void);
124 gchar **	anjuta_util_user_terminal				(void);
125 GPid		anjuta_util_execute_shell				(const gchar *dir, const gchar *command);
126 GPid		anjuta_util_execute_terminal_shell		(const gchar *dir, const gchar *command);
127 
128 gchar*		anjuta_util_escape_quotes				(const gchar* str);
129 
130 gchar*		anjuta_util_get_real_path				(const gchar *path);
131 gchar*		anjuta_util_get_current_dir				(void);
132 GFile *		anjuta_util_file_new_for_commandline_arg(const gchar *arg);
133 
134 gchar*		anjuta_util_uri_get_dirname				(const gchar *uri);
135 gchar*		anjuta_util_replace_home_dir_with_tilde (const gchar *uri);
136 gchar*		anjuta_util_shell_expand				(const gchar *string);
137 gchar*		anjuta_util_str_middle_truncate			(const gchar *string,
138 													guint truncate_length);
139 
140 gboolean	anjuta_util_is_project_file				(const gchar *filename);
141 gboolean	anjuta_util_is_template_file			(const gchar *filename);
142 gchar*		anjuta_util_get_file_mime_type			(GFile *file);
143 gchar*		anjuta_util_get_file_info_mime_type		(GFileInfo *info);
144 gchar*		anjuta_util_get_local_path_from_uri		(const gchar *uri);
145 
146 void		anjuta_util_help_display				(GtkWidget   *parent,
147 													const gchar *doc_id,
148 													const gchar *item);
149 
150 /* XDG BaseDir specifcation functions */
151 void		anjuta_util_set_anjuta_prefix			(const gchar* name);
152 GFile*		anjuta_util_get_user_data_file			(const gchar* path, ...);
153 GFile*		anjuta_util_get_user_cache_file			(const gchar* path, ...);
154 GFile*		anjuta_util_get_user_config_file		(const gchar* path, ...);
155 gchar*		anjuta_util_get_user_data_file_path		(const gchar* path, ...);
156 gchar*		anjuta_util_get_user_cache_file_path	(const gchar* path, ...);
157 gchar*		anjuta_util_get_user_config_file_path   (const gchar* path, ...);
158 
159 /* Get the correct e-mail address */
160 gchar*		anjuta_util_get_user_mail				(void);
161 void 		anjuta_util_set_user_mail				(const gchar* id);
162 
163 /* Function for converting GFile objects to string paths
164  * Free the returned list with anjuta_util_glist_strings_free. */
165 GList *		anjuta_util_convert_gfile_list_to_path_list (GList *list);
166 GList *		anjuta_util_convert_gfile_list_to_relative_path_list (GList *list,
167 															 const gchar *parent);
168 
169 /* the returned GPtrArray should be freed with g_ptr_array_unref ().
170  * The GPtrArray is created with g_free () destroy function as parameter.
171  */
172 GPtrArray * anjuta_util_convert_string_list_to_array(GList *list);
173 
174 /* list all files in a given directory */
175 void		anjuta_util_list_all_dir_children	    (GList **children, GFile *dir);
176 
177 /* Helper functions to load a GtkBuilder file and get widgets */
178 GtkBuilder *anjuta_util_builder_new					(const gchar *filename,
179 					                                GError **error);
180 gboolean	anjuta_util_builder_get_objects			(GtkBuilder *builder,
181 				                                    const gchar *first_widget,...);
182 
183 /* Helper for drag and drop */
184 GSList *	anjuta_utils_drop_get_files				(GtkSelectionData *selection_data);
185 GPtrArray * anjuta_util_clone_string_gptrarray		(const GPtrArray* source);
186 
187 /* Temporarily copied here */
188 
189 /**
190  * ANJUTA_TYPE_BEGIN:
191  * @class_name: Name of the object
192  * @prefix: Prefix used for object methods
193  * @parent_type: The parent type of the object
194  *
195  * Create a new GObject for Anjuta. You can use ANJUTA_TYPE_ADD_INTERFACE
196  * to add interfaces to it and should close it using ANJUTA_TYPE_END
197  */
198 #define ANJUTA_TYPE_BEGIN(class_name, prefix, parent_type) \
199 GType                                                     \
200 prefix##_get_type (void)                                  \
201 {                                                         \
202   static GType type = 0;                                  \
203   if (!type)                                              \
204     {                                                     \
205         static const GTypeInfo type_info =                \
206         {                                                 \
207           sizeof (class_name##Class),                     \
208           (GBaseInitFunc) NULL,                           \
209           (GBaseFinalizeFunc) NULL,                       \
210           (GClassInitFunc) prefix##_class_init,           \
211           (GClassFinalizeFunc) NULL,                      \
212           NULL,                                           \
213           sizeof (class_name),                            \
214           0, /* n_preallocs */                            \
215           (GInstanceInitFunc) prefix##_instance_init,     \
216         };                                                \
217                                                           \
218         type = g_type_register_static (parent_type,       \
219                                        #class_name,       \
220                                        &type_info, 0);
221 
222 /**
223  * ANJUTA_TYPE_END:
224  *
225  * Ends a declaration that began with ANJUTA_TYPE_BEGIN
226  */
227 #define ANJUTA_TYPE_END                                   \
228      }                                                    \
229   return type;                                            \
230 }
231 
232 /**
233  * ANJUTA_TYPE_ADD_INTERFACE:
234  * @prefix: Prefix for the interface methods
235  * @interface_type: Type of the interface to implement
236  *
237  * Adds an interface to a declaration started with ANJUTA_TYPE_BEGIN
238  */
239 #define ANJUTA_TYPE_ADD_INTERFACE(prefix,interface_type)  \
240     {                                                     \
241         GInterfaceInfo iface_info = {                     \
242             (GInterfaceInitFunc)prefix##_iface_init,      \
243             NULL,                                         \
244             NULL                                          \
245         };                                                \
246         g_type_add_interface_static (type,                \
247                                      interface_type,      \
248                                      &iface_info);        \
249     }
250 
251 /**
252  * ANJUTA_TYPE_BOILERPLATE:
253  * @class_name: Name of the object
254  * @prefix: Prefix for the class methods
255  * @parent_type: Parent type for object
256  *
257  * Simpliefied version of ANJUTA_TYPE_BEGIN that just creates an Object
258  * without interfaces and doesn't need ANJUTA_TYPE_END
259  */
260 #define ANJUTA_TYPE_BOILERPLATE(class_name, prefix, parent_type) \
261 ANJUTA_TYPE_BEGIN(class_name, prefix, parent_type);              \
262 ANJUTA_TYPE_END
263 
264 G_END_DECLS
265 
266 #endif
267