1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Naba Kumar <naba@gnome.org>
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.
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.
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
22 * SECTION:anjuta-utils
24 * @short_description: Utility functions
26 * @stability: Unstable
27 * @include: libanjuta/anjuta-utils.h
40 #include <sys/types.h>
41 #include <sys/fcntl.h>
42 #include <sys/termios.h>
53 #include <glib/gi18n.h>
55 #include <glib/gstdio.h>
58 #include <libanjuta/anjuta-utils.h>
59 #include <libanjuta/anjuta-debug.h>
60 #include <libanjuta/interfaces/ianjuta-editor-cell.h>
62 #define FILE_BUFFER_SIZE 1024
65 anjuta_util_from_file_to_file (GInputStream
*istream
,
66 GOutputStream
*ostream
)
70 gchar buffer
[FILE_BUFFER_SIZE
];
72 while (bytes
!= 0 && bytes
!= -1)
74 bytes
= g_input_stream_read (istream
, buffer
,
80 g_output_stream_write (ostream
, buffer
,
89 g_warning ("%s", error
->message
);
94 if (!g_output_stream_close (ostream
, NULL
, &error
))
96 g_warning ("%s", error
->message
);
100 if (!g_input_stream_close (istream
, NULL
, &error
))
102 g_warning ("%s", error
->message
);
103 g_error_free (error
);
108 * anjuta_util_copy_file:
109 * @src: the file where copy
110 * @dest: the path to copy the @src
111 * @show_error: TRUE to show a dialog error
113 * Copies @src to @dest and shows a dialog error in case is needed.
115 * Returns: TRUE if there was an error copying the file.
118 anjuta_util_copy_file (const gchar
* src
, const gchar
* dest
, gboolean show_error
)
120 GFile
*src_file
, *dest_file
;
121 GFileInputStream
*istream
;
122 GFileOutputStream
*ostream
;
123 GError
*error
= NULL
;
124 gboolean toret
= FALSE
;
126 src_file
= g_file_new_for_path (src
);
127 dest_file
= g_file_new_for_path (dest
);
129 istream
= g_file_read (src_file
, NULL
, &error
);
133 ostream
= g_file_create (dest_file
, G_FILE_CREATE_NONE
,
138 anjuta_util_from_file_to_file (G_INPUT_STREAM (istream
), G_OUTPUT_STREAM (ostream
));
143 anjuta_util_dialog_error_system (NULL
, error
->code
,
146 g_warning ("%s", error
->message
);
151 g_object_unref (src_file
);
152 g_object_unref (dest_file
);
158 anjuta_util_color_from_string (const gchar
* val
, guint16
* r
, guint16
* g
, guint16
* b
)
161 if (gdk_color_parse(val
, &color
))
170 anjuta_util_string_from_color (guint16 r
, guint16 g
, guint16 b
)
172 return g_strdup_printf("#%02x%02x%02x", r
>> 8, g
>> 8, b
>> 8);
176 anjuta_util_button_new_with_stock_image (const gchar
* text
,
177 const gchar
* stock_id
)
187 button
= gtk_button_new ();
189 child
= gtk_bin_get_child (GTK_BIN (button
));
191 gtk_container_remove (GTK_CONTAINER (button
), child
);
193 if (gtk_stock_lookup (stock_id
, &item
))
195 label
= gtk_label_new_with_mnemonic (text
);
197 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), GTK_WIDGET (button
));
199 image
= gtk_image_new_from_stock (stock_id
, GTK_ICON_SIZE_BUTTON
);
200 hbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 2);
202 align
= gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
204 gtk_box_pack_start (GTK_BOX (hbox
), image
, FALSE
, FALSE
, 0);
205 gtk_box_pack_end (GTK_BOX (hbox
), label
, FALSE
, FALSE
, 0);
207 gtk_container_add (GTK_CONTAINER (button
), align
);
208 gtk_container_add (GTK_CONTAINER (align
), hbox
);
209 gtk_widget_show_all (align
);
214 label
= gtk_label_new_with_mnemonic (text
);
215 gtk_label_set_mnemonic_widget (GTK_LABEL (label
), GTK_WIDGET (button
));
217 gtk_misc_set_alignment (GTK_MISC (label
), 0.5, 0.5);
219 gtk_widget_show (label
);
220 gtk_container_add (GTK_CONTAINER (button
), label
);
226 anjuta_util_dialog_add_button (GtkDialog
*dialog
, const gchar
* text
,
227 const gchar
* stock_id
, gint response_id
)
231 g_return_val_if_fail (GTK_IS_DIALOG (dialog
), NULL
);
232 g_return_val_if_fail (text
!= NULL
, NULL
);
233 g_return_val_if_fail (stock_id
!= NULL
, NULL
);
235 button
= anjuta_util_button_new_with_stock_image (text
, stock_id
);
236 g_return_val_if_fail (button
!= NULL
, NULL
);
238 gtk_widget_set_can_default (button
, TRUE
);
240 gtk_widget_show (button
);
242 gtk_dialog_add_action_widget (dialog
, button
, response_id
);
248 anjuta_util_dialog_error (GtkWindow
*parent
, const gchar
*mesg
, ...)
253 GtkWindow
*real_parent
;
255 va_start (args
, mesg
);
256 message
= g_strdup_vprintf (mesg
, args
);
259 if (parent
&& GTK_IS_WINDOW (parent
))
261 real_parent
= parent
;
268 // Dialog to be HIG compliant
269 dialog
= gtk_message_dialog_new (real_parent
,
270 GTK_DIALOG_DESTROY_WITH_PARENT
,
272 GTK_BUTTONS_CLOSE
, "%s", message
);
273 g_signal_connect (G_OBJECT (dialog
), "response",
274 G_CALLBACK (gtk_widget_destroy
), NULL
);
275 gtk_widget_show (dialog
);
280 anjuta_util_dialog_warning (GtkWindow
*parent
, const gchar
* mesg
, ...)
285 GtkWindow
*real_parent
;
287 va_start (args
, mesg
);
288 message
= g_strdup_vprintf (mesg
, args
);
291 if (parent
&& GTK_IS_WINDOW (parent
))
293 real_parent
= parent
;
300 // Dialog to be HIG compliant
301 dialog
= gtk_message_dialog_new (real_parent
,
302 GTK_DIALOG_DESTROY_WITH_PARENT
,
304 GTK_BUTTONS_CLOSE
, "%s", message
);
305 g_signal_connect (G_OBJECT (dialog
), "response",
306 G_CALLBACK (gtk_widget_destroy
), NULL
);
307 gtk_widget_show (dialog
);
312 anjuta_util_dialog_info (GtkWindow
*parent
, const gchar
* mesg
, ...)
317 GtkWindow
*real_parent
;
319 va_start (args
, mesg
);
320 message
= g_strdup_vprintf (mesg
, args
);
323 if (parent
&& GTK_IS_WINDOW (parent
))
325 real_parent
= parent
;
331 // Dialog to be HIG compliant
332 dialog
= gtk_message_dialog_new (real_parent
,
333 GTK_DIALOG_DESTROY_WITH_PARENT
,
335 GTK_BUTTONS_CLOSE
, "%s", message
);
336 g_signal_connect (G_OBJECT (dialog
), "response",
337 G_CALLBACK (gtk_widget_destroy
), NULL
);
338 gtk_widget_show (dialog
);
343 anjuta_util_dialog_error_system (GtkWindow
* parent
, gint errnum
,
344 const gchar
* mesg
, ... )
350 GtkWindow
*real_parent
;
352 va_start (args
, mesg
);
353 message
= g_strdup_vprintf (mesg
, args
);
357 /* Avoid space in translated string */
358 tot_mesg
= g_strconcat (message
, "\n", _("System:"), " ",
359 g_strerror(errnum
), NULL
);
364 if (parent
&& GTK_IS_WINDOW (parent
))
366 real_parent
= parent
;
372 // Dialog to be HIG compliant
373 dialog
= gtk_message_dialog_new (real_parent
,
374 GTK_DIALOG_DESTROY_WITH_PARENT
,
376 GTK_BUTTONS_CLOSE
, "%s", tot_mesg
);
377 g_signal_connect (G_OBJECT (dialog
), "response",
378 G_CALLBACK (gtk_widget_destroy
), NULL
);
379 gtk_widget_show (dialog
);
384 anjuta_util_dialog_boolean_question (GtkWindow
*parent
, gboolean default_to_yes
,
385 const gchar
*mesg
, ...)
391 GtkWindow
*real_parent
;
393 va_start (args
, mesg
);
394 message
= g_strdup_vprintf (mesg
, args
);
397 if (parent
&& GTK_IS_WINDOW (parent
))
399 real_parent
= parent
;
406 dialog
= gtk_message_dialog_new (real_parent
,
407 GTK_DIALOG_DESTROY_WITH_PARENT
,
408 GTK_MESSAGE_QUESTION
,
409 GTK_BUTTONS_YES_NO
, "%s", message
);
411 gtk_dialog_set_default_response (GTK_DIALOG (dialog
), GTK_RESPONSE_YES
);
413 ret
= gtk_dialog_run (GTK_DIALOG (dialog
));
414 gtk_widget_destroy (dialog
);
417 return (ret
== GTK_RESPONSE_YES
);
421 anjuta_util_dialog_input (GtkWindow
*parent
, const gchar
*prompt
,
422 const gchar
*default_value
, gchar
**return_value
)
424 GtkWidget
*dialog
, *label
, *frame
, *entry
, *dialog_vbox
, *vbox
;
427 GtkWindow
*real_parent
;
429 if (parent
&& GTK_IS_WINDOW (parent
))
431 real_parent
= parent
;
438 dialog
= gtk_dialog_new_with_buttons (prompt
, real_parent
,
439 GTK_DIALOG_DESTROY_WITH_PARENT
,
440 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
441 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
443 gtk_dialog_set_default_response (GTK_DIALOG (dialog
), GTK_RESPONSE_OK
);
444 dialog_vbox
= gtk_dialog_get_content_area (GTK_DIALOG (dialog
));
445 gtk_window_set_default_size (GTK_WINDOW (dialog
), 400, -1);
446 gtk_widget_show (dialog_vbox
);
448 markup
= g_strconcat ("<b>", prompt
, "</b>", NULL
);
449 label
= gtk_label_new (NULL
);
450 gtk_label_set_markup (GTK_LABEL (label
), markup
);
451 gtk_widget_show (label
);
454 frame
= gtk_frame_new (NULL
);
455 gtk_frame_set_label_widget (GTK_FRAME (frame
), label
);
456 gtk_frame_set_shadow_type (GTK_FRAME (frame
), GTK_SHADOW_NONE
);
457 gtk_container_set_border_width (GTK_CONTAINER (frame
), 10);
458 gtk_widget_show (frame
);
459 gtk_box_pack_start (GTK_BOX (dialog_vbox
), frame
, FALSE
, FALSE
, 0);
461 vbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 0);
462 gtk_widget_show (vbox
);
463 gtk_container_set_border_width (GTK_CONTAINER (vbox
), 10);
464 gtk_container_add (GTK_CONTAINER (frame
), vbox
);
466 entry
= gtk_entry_new ();
467 gtk_widget_show (entry
);
468 gtk_entry_set_activates_default (GTK_ENTRY (entry
), TRUE
);
469 gtk_box_pack_start (GTK_BOX (vbox
), entry
, FALSE
, FALSE
, 0);
471 gtk_entry_set_text (GTK_ENTRY (entry
), default_value
);
473 res
= gtk_dialog_run (GTK_DIALOG (dialog
));
475 if (gtk_entry_get_text (GTK_ENTRY (entry
)) &&
476 strlen (gtk_entry_get_text (GTK_ENTRY (entry
))) > 0)
478 *return_value
= g_strdup (gtk_entry_get_text (GTK_ENTRY (entry
)));
482 *return_value
= NULL
;
484 gtk_widget_destroy (dialog
);
485 return (res
== GTK_RESPONSE_OK
);
489 on_install_files_done (GObject
*proxy
, GAsyncResult
*result
,
492 GError
*error
= NULL
;
493 g_dbus_proxy_call_finish ((GDBusProxy
*) proxy
, result
, &error
);
497 Only dbus error is handled. Rest of the errors are from packagekit
498 which have already been notified to user by packagekit.
500 if (error
->domain
== G_DBUS_ERROR
)
502 const gchar
*error_message
= NULL
;
504 /* Service error which implies packagekit is missing */
505 if (error
->code
== G_DBUS_ERROR_SERVICE_UNKNOWN
)
507 error_message
= _("You do not seem to have PackageKit installed. PackageKit is required for installing missing packages. Please install \"packagekit-gnome\" package from your distribution, or install the missing packages manually.");
509 /* General dbus error implies failure to call dbus method */
510 else if (error
->code
!= G_DBUS_ERROR_NO_REPLY
)
512 error_message
= error
->message
;
515 anjuta_util_dialog_error (NULL
,
516 _("Installation failed: %s"),
519 g_error_free (error
);
524 anjuta_util_install_files (const gchar
* const names
)
526 GDBusConnection
* connection
;
534 connection
= g_bus_get_sync (G_BUS_TYPE_SESSION
, NULL
, NULL
);
538 proxy
= g_dbus_proxy_new_sync (connection
,
539 G_DBUS_PROXY_FLAGS_NONE
,
541 "org.freedesktop.PackageKit",
542 "/org/freedesktop/PackageKit",
543 "org.freedesktop.PackageKit.Modify",
549 pkgv
= g_strsplit (names
, ", ", 0);
550 g_dbus_proxy_call (proxy
, "InstallProvideFiles",
551 g_variant_new ("(u^ass)",
555 G_DBUS_CALL_FLAGS_NONE
,
558 on_install_files_done
,
565 anjuta_util_package_is_installed (const gchar
* package
, gboolean show
)
567 const gchar
* const argv
[] = { "pkg-config", "--exists", package
, NULL
};
571 if (!g_spawn_sync (NULL
, (gchar
**)argv
, NULL
, G_SPAWN_SEARCH_PATH
, NULL
,
572 NULL
, NULL
, NULL
, &exit_status
, &err
))
576 anjuta_util_dialog_error (NULL
,
577 _("Failed to run \"%s\". "
578 "The returned error was: \"%s\"."),
579 "pkg-config --exists", err
->message
);
585 if (WIFEXITED (exit_status
) && WEXITSTATUS (exit_status
) == 0)
590 anjuta_util_dialog_error (NULL
,
591 _("The \"%s\" package is not installed.\n"
592 "Please install it."), package
);
599 anjuta_util_prog_is_installed (const gchar
* prog
, gboolean show
)
601 gchar
* prog_path
= g_find_program_in_path (prog
);
609 anjuta_util_dialog_error (NULL
, _("The \"%s\" utility is not installed.\n"
610 "Please install it."), prog
);
616 anjuta_util_get_a_tmp_file (void)
618 static gint count
= 0;
622 tmpdir
= g_get_tmp_dir ();
624 g_strdup_printf ("%s/anjuta_%d.%d", tmpdir
, count
++, getpid ());
628 /* GList of strings operations */
630 anjuta_util_glist_from_string (const gchar
*string
)
632 gchar
*str
, *temp
, buff
[256];
634 gchar
*word_start
, *word_end
;
637 temp
= g_strdup (string
);
647 /* Remove leading spaces */
648 while (isspace (*str
) && *str
!= '\0')
653 /* Find start and end of word */
655 while (!isspace (*str
) && *str
!= '\0')
659 /* Copy the word into the buffer */
660 for (ptr
= word_start
, i
= 0; ptr
< word_end
; ptr
++, i
++)
664 list
= g_list_append (list
, g_strdup (buff
));
673 /* Prefix the strings */
675 anjuta_util_glist_strings_prefix (GList
* list
, const gchar
*prefix
)
680 g_return_if_fail (prefix
!= NULL
);
685 node
->data
= g_strconcat (prefix
, tmp
, NULL
);
686 if (tmp
) g_free (tmp
);
687 node
= g_list_next (node
);
691 /* Suffix the strings */
693 anjuta_util_glist_strings_sufix (GList
* list
, const gchar
*sufix
)
698 g_return_if_fail (sufix
!= NULL
);
703 node
->data
= g_strconcat (tmp
, sufix
, NULL
);
704 if (tmp
) g_free (tmp
);
705 node
= g_list_next (node
);
709 /* Duplicate list of strings */
711 anjuta_util_glist_strings_dup (GList
* list
)
721 new_list
= g_list_append (new_list
, g_strdup(node
->data
));
723 new_list
= g_list_append (new_list
, NULL
);
724 node
= g_list_next (node
);
729 /* Join list of strings using the given delimiter */
731 anjuta_util_glist_strings_join (GList
* list
, gchar
*delimiter
)
734 gboolean first
= TRUE
;
737 joined
= g_string_new (NULL
);
744 g_string_append (joined
, delimiter
);
747 g_string_append (joined
, node
->data
);
749 node
= g_list_next (node
);
752 return g_string_free (joined
, FALSE
);
754 g_string_free (joined
, TRUE
);
759 anjuta_util_get_real_path (const gchar
*path
)
765 gchar buf
[PATH_MAX
+1];
767 result
= realpath (path
, buf
);
770 *(buf
+ PATH_MAX
) = '\0'; /* ensure a terminator */
771 return g_strdup (buf
);
775 /* the string returned by realpath should be cleaned with
776 free(), not g_free() */
777 buf
= realpath (path
, NULL
);
780 result
= g_strdup (buf
);
790 * anjuta_util_get_current_dir:
792 * Get current working directory, unlike g_get_current_dir, keeps symbolic links
795 * Returns: The current working directory.
798 anjuta_util_get_current_dir (void)
802 pwd
= g_getenv ("PWD");
805 return g_strdup (pwd
);
809 return g_get_current_dir ();
814 is_valid_scheme_character (char c
)
816 return g_ascii_isalnum (c
) || c
== '+' || c
== '-' || c
== '.';
819 /* Following RFC 2396, valid schemes are built like:
820 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
823 has_valid_scheme (const char *uri
)
829 if (!g_ascii_isalpha (*p
))
834 } while (is_valid_scheme_character (*p
));
840 * anjuta_util_file_new_for_commandline_arg:
841 * @arg: URI or relative or absolute file path
843 * Create a new file corresponding to arg, unlike g_file_new_for_commandline_arg,
844 * keeps symbolic links in path name.
846 * Returns: A new GFile object
849 anjuta_util_file_new_for_commandline_arg (const gchar
*arg
)
855 g_return_val_if_fail (arg
!= NULL
, NULL
);
857 if (g_path_is_absolute (arg
))
858 return g_file_new_for_path (arg
);
860 if (has_valid_scheme (arg
))
861 return g_file_new_for_uri (arg
);
863 current_dir
= anjuta_util_get_current_dir ();
864 filename
= g_build_filename (current_dir
, arg
, NULL
);
865 g_free (current_dir
);
867 file
= g_file_new_for_path (filename
);
874 /* Dedup a list of paths - duplicates are removed from the tail.
875 ** Useful for deduping Recent Files and Recent Projects */
877 anjuta_util_glist_path_dedup(GList
*list
)
879 GList
*nlist
= NULL
, *tmp
, *tmp1
;
882 for (tmp
= list
; tmp
; tmp
= g_list_next(tmp
))
884 path
= anjuta_util_get_real_path ((const gchar
*) tmp
->data
);
887 if (stat (path
, &s
) != 0)
893 for (tmp1
= nlist
; tmp1
; tmp1
= g_list_next(tmp1
))
895 if (0 == strcmp((const char *) tmp1
->data
, path
))
903 nlist
= g_list_prepend(nlist
, path
);
907 anjuta_util_glist_strings_free(list
);
908 nlist
= g_list_reverse(nlist
);
913 sort_node (gchar
* a
, gchar
*b
)
915 if ( !a
&& !b
) return 0;
916 else if (!a
) return -1;
917 else if (!b
) return 1;
918 return strcmp (a
, b
);
921 /* Sort the list alphabatically */
923 anjuta_util_glist_strings_sort (GList
* list
)
925 return g_list_sort(list
, (GCompareFunc
)sort_node
);
928 /* Free the strings and GList */
930 anjuta_util_glist_strings_free (GList
* list
)
932 g_list_foreach (list
, (GFunc
) g_free
, NULL
);
937 anjuta_util_type_from_string (AnjutaUtilStringMap
*map
, const char *str
)
941 while (-1 != map
[i
].type
)
943 if (0 == strcmp(map
[i
].name
, str
))
951 anjuta_util_string_from_type (AnjutaUtilStringMap
*map
, int type
)
954 while (-1 != map
[i
].type
)
956 if (map
[i
].type
== type
)
964 anjuta_util_glist_from_map (AnjutaUtilStringMap
*map
)
966 GList
*out_list
= NULL
;
968 while (-1 != map
[i
].type
)
970 out_list
= g_list_append(out_list
, map
[i
].name
);
978 anjuta_util_update_string_list (GList
*p_list
, const gchar
*p_str
, gint length
)
984 for (i
= 0; i
< g_list_length (p_list
); i
++)
986 str
= (gchar
*) g_list_nth_data (p_list
, i
);
989 if (strcmp (p_str
, str
) == 0)
991 p_list
= g_list_remove (p_list
, str
);
992 p_list
= g_list_prepend (p_list
, str
);
996 p_list
= g_list_prepend (p_list
, g_strdup (p_str
));
997 while (g_list_length (p_list
) > length
)
999 str
= g_list_nth_data (p_list
, g_list_length (p_list
) - 1);
1000 p_list
= g_list_remove (p_list
, str
);
1007 anjuta_util_create_dir (const gchar
* path
)
1009 GFile
*dir
= g_file_new_for_path (path
);
1013 if (g_file_query_exists (dir
, NULL
))
1015 GFileInfo
*info
= g_file_query_info (dir
,
1016 G_FILE_ATTRIBUTE_STANDARD_TYPE
,
1017 G_FILE_QUERY_INFO_NONE
,
1019 if (g_file_info_get_file_type (info
) != G_FILE_TYPE_DIRECTORY
)
1021 g_message ("Warning: %s is a file. \n "
1022 "It is trying to be treated as a directory.",g_file_get_path (dir
));
1023 g_object_unref (dir
);
1026 g_object_unref (info
);
1030 parent
= g_path_get_dirname (path
);
1031 if (anjuta_util_create_dir (parent
))
1034 if (!g_file_make_directory (dir
, NULL
, &err
))
1036 g_warning ("Error directory:\n %s", err
->message
);
1037 g_object_unref (dir
);
1044 g_object_unref (dir
);
1048 g_object_unref (dir
);
1054 * anjuta_util_user_shell:
1056 * Retrieves the user's preferred shell.
1058 * Returns: A newly allocated string that is the path to the shell.
1060 /* copied from deprecated gnome_util_user_shell in libgnome */
1062 anjuta_util_user_shell (void)
1068 const gchar shells
[][14] = {
1069 /* Note that on some systems shells can also
1070 * be installed in /usr/bin */
1071 "/bin/bash", "/usr/bin/bash",
1072 "/bin/zsh", "/usr/bin/zsh",
1073 "/bin/tcsh", "/usr/bin/tcsh",
1074 "/bin/ksh", "/usr/bin/ksh",
1075 "/bin/csh", "/bin/sh"
1078 if (geteuid () == getuid () &&
1079 getegid () == getgid ()) {
1080 /* only in non-setuid */
1081 if ((shell
= g_getenv ("SHELL"))){
1082 if (access (shell
, X_OK
) == 0) {
1083 return g_strdup (shell
);
1087 pw
= getpwuid(getuid());
1088 if (pw
&& pw
->pw_shell
) {
1089 if (access (pw
->pw_shell
, X_OK
) == 0) {
1090 return g_strdup (pw
->pw_shell
);
1094 for (i
= 0; i
!= G_N_ELEMENTS (shells
); i
++) {
1095 if (access (shells
[i
], X_OK
) == 0) {
1096 return g_strdup (shells
[i
]);
1100 /* If /bin/sh doesn't exist, your system is truly broken. */
1103 /* Placate compiler. */
1106 /* g_find_program_in_path() always looks also in the Windows
1107 * and System32 directories, so it should always find either cmd.exe
1110 gchar
*retval
= g_find_program_in_path ("cmd.exe");
1113 retval
= g_find_program_in_path ("command.com");
1115 g_assert (retval
!= NULL
);
1122 * anjuta_util_user_terminal:
1124 * Retrieves the user's preferred terminal.
1126 * Returns: A newly allocated strings list. The first argument is the terminal
1127 * program name. The following are the arguments needed to execute
1128 * a command. The list has to be freed with g_strfreev
1130 /* copied from deprecated gnome_execute_terminal in libgnome */
1132 anjuta_util_user_terminal (void)
1134 /* FIXME: GSettings */
1136 GConfClient
*client
;
1137 gchar
*terminal
= NULL
;
1138 gchar
**argv
= NULL
;
1139 static const gchar
*terms
[] = {
1151 client
= gconf_client_get_default ();
1152 terminal
= gconf_client_get_string (client
, "/desktop/gnome/applications/terminal/exec", NULL
);
1153 g_object_unref (client
);
1157 gchar
*command_line
;
1160 exec_flag
= gconf_client_get_string (client
, "/desktop/gnome/applications/terminal/exec_arg", NULL
);
1161 command_line
= g_strconcat (terminal
, " ", exec_flag
, NULL
);
1163 g_shell_parse_argv (command_line
, NULL
, &argv
, NULL
);
1171 /* Search for common ones */
1172 for (term
= terms
; *term
!= NULL
; term
++)
1174 terminal
= g_find_program_in_path (*term
);
1175 if (terminal
!= NULL
) break;
1179 g_warning (_("Cannot find a terminal; using "
1180 "xterm, even if it may not work"));
1181 terminal
= g_strdup ("xterm");
1183 argv
= g_new0 (char *, 3);
1185 /* Note that gnome-terminal takes -x and
1186 * as -e in gnome-terminal is broken we use that. */
1187 argv
[1] = g_strdup (term
== &terms
[2] ? "-x" : "-e");
1191 g_warning ("anjuta_util_user_terminal: Not implemented");
1197 anjuta_util_execute_shell (const gchar
*dir
, const gchar
*command
)
1202 g_return_val_if_fail (command
!= NULL
, -1);
1204 shell
= anjuta_util_user_shell ();
1210 anjuta_util_create_dir (dir
);
1213 execlp (shell
, shell
, "-c", command
, NULL
);
1214 g_warning (_("Cannot execute command: %s (using shell %s)\n"), command
, shell
);
1218 g_warning (_("Cannot execute command: %s (using shell %s)\n"), command
, shell
);
1221 // Anjuta will take care of child exit automatically.
1226 anjuta_util_execute_terminal_shell (const gchar
*dir
, const gchar
*command
)
1232 g_return_val_if_fail (command
!= NULL
, -1);
1234 shell
= anjuta_util_user_shell ();
1235 term_argv
= anjuta_util_user_terminal ();
1241 anjuta_util_create_dir (dir
);
1244 execlp (term_argv
[0], term_argv
[0], term_argv
[1], shell
, "-c", command
, NULL
);
1245 g_warning (_("Cannot execute command: %s (using shell %s)\n"), command
, shell
);
1249 g_warning (_("Cannot execute command: %s (using shell %s)\n"), command
, shell
);
1251 g_strfreev (term_argv
);
1253 // Anjuta will take care of child exit automatically.
1258 anjuta_util_convert_to_utf8 (const gchar
*str
)
1260 GError
*error
= NULL
;
1261 gchar
*utf8_msg_string
= NULL
;
1263 g_return_val_if_fail (str
!= NULL
, NULL
);
1264 g_return_val_if_fail (strlen (str
) > 0, NULL
);
1266 if (g_utf8_validate(str
, -1, NULL
))
1268 utf8_msg_string
= g_strdup (str
);
1272 gsize rbytes
, wbytes
;
1273 utf8_msg_string
= g_locale_to_utf8 (str
, -1, &rbytes
, &wbytes
, &error
);
1274 if (error
!= NULL
) {
1275 g_warning ("g_locale_to_utf8 failed: %s\n", error
->message
);
1276 g_error_free (error
);
1277 /* g_free (utf8_msg_string);
1281 return utf8_msg_string
;
1284 #define LEFT_BRACE(ch) (ch == ')'? '(' : (ch == '}'? '{' : (ch == ']'? '[' : ch)))
1287 anjuta_util_jump_to_matching_brace (IAnjutaIterable
*iter
, gchar brace
, gint limit
)
1289 gchar point_ch
= brace
;
1290 gint cur_iteration
= 0;
1291 gboolean use_limit
= (limit
> 0);
1292 GString
*braces_stack
= g_string_new ("");
1294 g_return_val_if_fail (point_ch
== ')' || point_ch
== ']' ||
1295 point_ch
== '}', FALSE
);
1297 /* DEBUG_PRINT ("%s", "Matching brace being"); */
1299 g_string_prepend_c (braces_stack
, point_ch
);
1301 while (ianjuta_iterable_previous (iter
, NULL
))
1305 if (use_limit
&& cur_iteration
> limit
)
1308 /* Skip comments and strings */
1309 IAnjutaEditorAttribute attrib
=
1310 ianjuta_editor_cell_get_attribute (IANJUTA_EDITOR_CELL (iter
), NULL
);
1311 if (attrib
== IANJUTA_EDITOR_COMMENT
|| attrib
== IANJUTA_EDITOR_STRING
)
1314 /* DEBUG_PRINT ("%s", "point ch = %c", point_ch); */
1315 point_ch
= ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter
), 0,
1317 if (point_ch
== ')' || point_ch
== ']' || point_ch
== '}')
1320 g_string_prepend_c (braces_stack
, point_ch
);
1323 if (point_ch
== LEFT_BRACE (braces_stack
->str
[0]))
1326 g_string_erase (braces_stack
, 0, 1);
1328 /* Bail out if there is no more in stack */
1329 if (braces_stack
->str
[0] == '\0')
1331 /* DEBUG_PRINT ("%s", "Matching brace end -- found"); */
1335 /* DEBUG_PRINT ("%s", "Matching brace end -- not found"); */
1340 anjuta_util_parse_args_from_string (const gchar
* string
)
1344 gboolean is_quote
= FALSE
;
1345 gchar
* buffer
= g_new0(gchar
, strlen(string
) + 1);
1362 /* The current char was escaped */
1365 } else if (*s
== '\\') {
1366 /* Current char is an escape */
1368 } else if (is_quote
&& *s
== quote
) {
1369 /* Current char ends a quotation */
1371 if (!isspace(*(s
+1)) && (*(s
+1) != '\0')) {
1372 /* If there is no space after the quotation or it is not
1373 the end of the string */
1374 g_warning ("Parse error while parsing program arguments");
1376 } else if ((*s
== '\"' || *s
== '\'')) {
1378 /* Current char starts a quotation */
1382 /* Just a quote char inside quote */
1385 } else if (is_quote
){
1386 /* Any other char inside quote */
1388 } else if (isspace(*s
)) {
1389 /* Any white space outside quote */
1391 buffer
[idx
++] = '\0';
1392 args
= g_list_append (args
, g_strdup (buffer
));
1401 /* There are chars in the buffer. Flush as the last arg */
1402 buffer
[idx
++] = '\0';
1403 args
= g_list_append (args
, g_strdup (buffer
));
1407 g_warning ("Unclosed quotation encountered at the end of parsing");
1414 anjuta_util_escape_quotes(const gchar
* str
)
1418 const gchar
*s
= str
;
1420 g_return_val_if_fail(str
, NULL
);
1423 /* We are assuming there will be less than 2048 chars to escape */
1424 max_size
= strlen(str
) + 2048;
1425 buffer
= g_new (gchar
, max_size
);
1431 if (*s
== '\"' || *s
== '\'' || *s
== '\\')
1432 buffer
[idx
++] = '\\';
1440 /* Diff the text contained in uri with text. Return true if files
1441 differ, FALSE if they are identical.*/
1443 gboolean
anjuta_util_diff(const gchar
* uri
, const gchar
* text
)
1446 GFileInfo
*file_info
;
1448 gchar
* file_text
= NULL
;
1451 file
= g_file_new_for_uri (uri
);
1452 file_info
= g_file_query_info (file
,
1453 G_FILE_ATTRIBUTE_STANDARD_SIZE
,
1454 G_FILE_QUERY_INFO_NONE
,
1458 if (file_info
== NULL
)
1460 g_object_unref (file
);
1464 size
= g_file_info_get_attribute_uint64(file_info
,
1465 G_FILE_ATTRIBUTE_STANDARD_SIZE
);
1466 g_object_unref (file_info
);
1468 if (size
== 0 && text
== NULL
)
1470 g_object_unref (file
);
1473 else if (size
== 0 || text
== NULL
)
1475 g_object_unref (file
);
1479 if (!g_file_load_contents(file
,
1486 g_object_unref (file
);
1489 g_object_unref (file
);
1491 if (bytes_read
!= size
)
1497 /* according to g_file_load_contents's documentation
1498 * file_text is guaranteed to end with \0.
1500 if (strcmp (file_text
, text
) == 0)
1511 * anjuta_util_is_project_file:
1512 * @filename: the file name
1514 * Return TRUE if the file is an anjuta project file. It is implemented by
1515 * checking only the file extension. So it does not check the existence
1516 * of the file. But it is working on an URI if it does not containt a
1519 * Returns: TRUE if the file is a project file, else FALSE
1522 anjuta_util_is_project_file (const gchar
*filename
)
1524 gsize len
= strlen (filename
);
1525 return ((len
> 8) && (strcmp (filename
+ len
- 7, ".anjuta") == 0));
1529 * anjuta_util_is_template_file:
1530 * @filename: the file name
1532 * Return TRUE if the file is an template project file. It is implemented by
1533 * checking only the file extension. So it does not check the existence
1534 * of the file. But it is working on an URI if it does not containt a
1537 * Returns: TRUE if the file is a template file, else FALSE
1540 anjuta_util_is_template_file (const gchar
*filename
)
1542 gsize len
= strlen (filename
);
1543 return ((len
> 9) && (strcmp (filename
+ len
- 8, ".wiz.tgz") == 0));
1547 * anjuta_util_get_file_info_mine_type:
1548 * @info: the file information object
1550 * Return the mime type corresponding to a file infor object.
1552 * Returns: The mime type as a newly allocated string that must be freed with
1553 * g_free() or %NULL if the mime type cannot be found.
1556 anjuta_util_get_file_info_mime_type (GFileInfo
*info
)
1558 gchar
*mime_type
= NULL
;
1559 const gchar
*extension
;
1562 g_return_val_if_fail (info
!= NULL
, NULL
);
1565 /* If Anjuta is not installed in system gnome prefix, the mime types
1566 * may not have been correctly registed. In that case, we use the
1567 * following mime detection
1569 name
= g_file_info_get_name (info
);
1570 extension
= strrchr(name
, '.');
1571 if ((extension
!= NULL
) && (extension
!= name
))
1573 const static struct {gchar
*extension
; gchar
*type
;} anjuta_types
[] = {
1574 {"anjuta", "application/x-anjuta"},
1575 {"prj", "application/x-anjuta-old"},
1579 for (i
= 0; anjuta_types
[i
].extension
!= NULL
; i
++)
1581 if (strcmp(extension
+ 1, anjuta_types
[i
].extension
) == 0)
1583 mime_type
= g_strdup (anjuta_types
[i
].type
);
1589 /* Use mime database if it is not an Anjuta type */
1590 if (mime_type
== NULL
)
1592 mime_type
= g_content_type_get_mime_type (g_file_info_get_content_type(info
));
1599 * anjuta_util_get_file_mine_type:
1602 * Check if a file exists and return its mime type.
1604 * Returns: NULL if the corresponding file doesn't exist or the mime type as a newly
1605 * allocated string that must be freed with g_free().
1608 anjuta_util_get_file_mime_type (GFile
*file
)
1611 gchar
*mime_type
= NULL
;
1613 g_return_val_if_fail (file
!= NULL
, NULL
);
1615 /* Get file information, check that the file exist at the same time */
1616 info
= g_file_query_info (file
,
1617 G_FILE_ATTRIBUTE_STANDARD_NAME
","
1618 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
,
1619 G_FILE_QUERY_INFO_NONE
,
1625 mime_type
= anjuta_util_get_file_info_mime_type (info
);
1626 g_object_unref (info
);
1633 anjuta_util_get_local_path_from_uri (const gchar
*uri
)
1638 file
= g_file_new_for_uri (uri
);
1639 local_path
= g_file_get_path (file
);
1640 g_object_unref (file
);
1645 #ifdef EMULATE_FORKPTY
1648 static int ptym_open (char *pts_name
);
1649 static int ptys_open (int fdm
, char * pts_name
);
1652 login_tty(int ttyfd
)
1664 /* First disconnect from the old controlling tty. */
1666 fd
= open("/dev/tty", O_RDWR
|O_NOCTTY
);
1669 ioctl(fd
, TIOCNOTTY
, NULL
);
1673 //syslog(LOG_WARNING, "NO CTTY");
1674 #endif /* TIOCNOTTY */
1676 /* Verify that we are successfully disconnected from the controlling tty. */
1677 fd
= open("/dev/tty", O_RDWR
|O_NOCTTY
);
1680 //syslog(LOG_WARNING, "Failed to disconnect from controlling tty.");
1684 /* Make it our controlling tty. */
1686 ioctl(ttyfd
, TIOCSCTTY
, NULL
);
1687 #endif /* TIOCSCTTY */
1689 fdname
= ttyname (ttyfd
);
1690 fd
= open(fdname
, O_RDWR
);
1692 ;//syslog(LOG_WARNING, "open %s: %s", fdname, strerror(errno));
1696 /* Verify that we now have a controlling tty. */
1697 fd
= open("/dev/tty", O_WRONLY
);
1700 //syslog(LOG_WARNING, "open /dev/tty: %s", strerror(errno));
1705 #if defined(HAVE_VHANGUP) && !defined(HAVE_REVOKE)
1707 RETSIGTYPE (*sig
)();
1708 sig
= signal(SIGHUP
, SIG_IGN
);
1710 signal(SIGHUP
, sig
);
1713 fd
= open(fdname
, O_RDWR
);
1716 //syslog(LOG_ERR, "can't reopen ctty %s: %s", fdname, strerror(errno));
1738 openpty(int *amaster
, int *aslave
, char *name
, struct termios
*termp
,
1739 struct winsize
*winp
)
1742 *amaster
= ptym_open(line
);
1745 *aslave
= ptys_open(*amaster
, line
);
1753 #define TCSAFLUSH TCSETAF
1756 (void) tcsetattr(*aslave
, TCSAFLUSH
, termp
);
1759 (void) ioctl(*aslave
, TIOCSWINSZ
, (char *)winp
);
1765 ptym_open(char * pts_name
)
1771 strcpy(pts_name
, "/dev/ptmx");
1772 fdm
= open(pts_name
, O_RDWR
);
1775 if (grantpt(fdm
) < 0) { /* grant access to slave */
1779 if (unlockpt(fdm
) < 0) { /* clear slave's lock flag */
1784 if (ptr
== NULL
) { /* get slave's name */
1788 strcpy(pts_name
, ptr
); /* return name of slave */
1789 return fdm
; /* return fd of master */
1793 strcpy(pts_name
, "/dev/ptyXY");
1794 /* array index: 012345689 (for references in following code) */
1795 for (ptr1
= "pqrstuvwxyzPQRST"; *ptr1
!= 0; ptr1
++) {
1796 pts_name
[8] = *ptr1
;
1797 for (ptr2
= "0123456789abcdef"; *ptr2
!= 0; ptr2
++) {
1798 pts_name
[9] = *ptr2
;
1799 /* try to open master */
1800 fdm
= open(pts_name
, O_RDWR
);
1802 if (errno
== ENOENT
) /* different from EIO */
1803 return -1; /* out of pty devices */
1805 continue; /* try next pty device */
1807 pts_name
[5] = 't'; /* chage "pty" to "tty" */
1808 return fdm
; /* got it, return fd of master */
1811 return -1; /* out of pty devices */
1816 ptys_open(int fdm
, char * pts_name
)
1820 /* following should allocate controlling terminal */
1821 fds
= open(pts_name
, O_RDWR
);
1826 if (ioctl(fds
, I_PUSH
, "ptem") < 0) {
1831 if (ioctl(fds
, I_PUSH
, "ldterm") < 0) {
1836 if (ioctl(fds
, I_PUSH
, "ttcompat") < 0) {
1842 if (ioctl(fdm
, I_PUSH
, "pckt") < 0) {
1848 if (ioctl(fdm
, I_SRDOPT
, RMSGN
|RPROTDAT
) < 0) {
1857 struct group
*grptr
;
1859 grptr
= getgrnam("tty");
1861 gid
= grptr
->gr_gid
;
1863 gid
= -1; /* group tty is not in the group file */
1864 /* following two functions don't work unless we're root */
1865 chown(pts_name
, getuid(), gid
);
1866 chmod(pts_name
, S_IRUSR
| S_IWUSR
| S_IWGRP
);
1867 fds
= open(pts_name
, O_RDWR
);
1877 forkpty(int *amaster
, char *name
, struct termios
*termp
, struct winsize
*winp
)
1879 int master
, slave
, pid
;
1881 if (openpty(&master
, &slave
, name
, termp
, winp
) == -1)
1883 switch (pid
= fork()) {
1902 int scandir(const char *dir
, struct dirent
***namelist
,
1903 int (*select
)(const struct dirent
*),
1904 int (*compar
)(const struct dirent
**, const struct dirent
**))
1907 struct dirent
*entry
;
1911 if ((d
=opendir(dir
)) == NULL
)
1915 while ((entry
=readdir(d
)) != NULL
)
1917 if (select
== NULL
|| (select
!= NULL
&& (*select
)(entry
)))
1919 *namelist
=(struct dirent
**)realloc((void *)(*namelist
),
1920 (size_t)((i
+1)*sizeof(struct dirent
*)));
1921 if (*namelist
== NULL
) return(-1);
1922 entrysize
=sizeof(struct dirent
)-sizeof(entry
->d_name
)+strlen(entry
->d_name
)+1;
1923 (*namelist
)[i
]=(struct dirent
*)malloc(entrysize
);
1924 if ((*namelist
)[i
] == NULL
) return(-1);
1925 memcpy((*namelist
)[i
], entry
, entrysize
);
1929 if (closedir(d
)) return(-1);
1930 if (i
== 0) return(-1);
1932 qsort((void *)(*namelist
), (size_t)i
, sizeof(struct dirent
*), compar
);
1937 #endif /* EMULATE_FORKPTY */
1940 anjuta_util_help_display (GtkWidget
*parent
,
1941 const gchar
*doc_id
,
1944 GError
*error
= NULL
;
1948 command
= g_strdup_printf ("yelp help:%s%s%s",
1950 item
== NULL
? "" : "/",
1951 item
== NULL
? "" : item
);
1953 if (!g_spawn_command_line_async (command
, &error
) &&
1956 g_warning ("Error executing help application: %s",
1958 g_error_free (error
);
1964 /* The following functions are taken from gedit */
1966 /* Note that this function replace home dir with ~ */
1968 anjuta_util_uri_get_dirname (const gchar
*uri
)
1973 // CHECK: does it work with uri chaining? - Paolo
1974 str
= g_path_get_dirname (uri
);
1975 g_return_val_if_fail (str
!= NULL
, ".");
1977 if ((strlen (str
) == 1) && (*str
== '.'))
1984 res
= anjuta_util_replace_home_dir_with_tilde (str
);
1992 anjuta_util_replace_home_dir_with_tilde (const gchar
*uri
)
1997 g_return_val_if_fail (uri
!= NULL
, NULL
);
1999 /* Note that g_get_home_dir returns a const string */
2000 tmp
= (gchar
*)g_get_home_dir ();
2003 return g_strdup (uri
);
2005 home
= g_filename_to_utf8 (tmp
, -1, NULL
, NULL
, NULL
);
2007 return g_strdup (uri
);
2009 if (strcmp (uri
, home
) == 0)
2013 return g_strdup ("~");
2017 home
= g_strdup_printf ("%s/", tmp
);
2020 if (g_str_has_prefix (uri
, home
))
2024 res
= g_strdup_printf ("~/%s", uri
+ strlen (home
));
2033 return g_strdup (uri
);
2037 * anjuta_util_shell_expand:
2038 * @string: input string
2040 * Expand environment variables $(var_name) and tilde (~) in the input string.
2042 * Returns: a newly-allocated string that must be freed with g_free().
2045 anjuta_util_shell_expand (const gchar
*string
)
2049 if (string
== NULL
) return NULL
;
2051 expand
= g_string_sized_new (strlen (string
));
2053 for (; *string
!= '\0'; string
++)
2059 /* Variable expansion */
2064 while (isalnum (*end
) || (*end
== '_')) end
++;
2065 var_name_len
= end
- string
- 1;
2066 if (var_name_len
> 0)
2070 g_string_append_len (expand
, string
+ 1, var_name_len
);
2071 value
= g_getenv (expand
->str
+ expand
->len
- var_name_len
);
2072 g_string_truncate (expand
, expand
->len
- var_name_len
);
2073 g_string_append (expand
, value
);
2081 /* User home directory expansion */
2082 if (isspace(string
[1]) || (string
[1] == G_DIR_SEPARATOR
) || (string
[1] == '\0'))
2084 g_string_append (expand
, g_get_home_dir());
2092 g_string_append_c (expand
, *string
);
2095 return g_string_free (expand
, FALSE
);
2099 anjuta_util_str_middle_truncate (const gchar
*string
,
2100 guint truncate_length
)
2105 guint num_left_chars
;
2107 guint delimiter_length
;
2108 const gchar
*delimiter
= "\342\200\246";
2110 g_return_val_if_fail (string
!= NULL
, NULL
);
2112 length
= strlen (string
);
2114 g_return_val_if_fail (g_utf8_validate (string
, length
, NULL
), NULL
);
2116 /* It doesnt make sense to truncate strings to less than
2117 * the size of the delimiter plus 2 characters (one on each
2120 delimiter_length
= g_utf8_strlen (delimiter
, -1);
2121 if (truncate_length
< (delimiter_length
+ 2)) {
2122 return g_strdup (string
);
2125 n_chars
= g_utf8_strlen (string
, length
);
2127 /* Make sure the string is not already small enough. */
2128 if (n_chars
<= truncate_length
) {
2129 return g_strdup (string
);
2132 /* Find the 'middle' where the truncation will occur. */
2133 num_left_chars
= (truncate_length
- delimiter_length
) / 2;
2134 right_offset
= n_chars
- truncate_length
+ num_left_chars
+ delimiter_length
;
2136 truncated
= g_string_new_len (string
,
2137 g_utf8_offset_to_pointer (string
, num_left_chars
) - string
);
2138 g_string_append (truncated
, delimiter
);
2139 g_string_append (truncated
, g_utf8_offset_to_pointer (string
, right_offset
));
2141 return g_string_free (truncated
, FALSE
);
2145 * Functions to implement XDG Base Directory Specification
2146 * http://standards.freedesktop.org/basedir-spec/latest/index.html
2147 * Use this to save any config/cache/data files
2152 anjuta_util_construct_pathv (const gchar
* str
, va_list str_list
)
2155 const gchar
* tmp_str
;
2158 str_arr
= g_ptr_array_new();
2159 g_ptr_array_add (str_arr
, (gpointer
) str
);
2161 /* Extract elements from va_list */
2164 while ((tmp_str
= va_arg (str_list
, const gchar
*)) != NULL
)
2166 g_ptr_array_add (str_arr
, (gpointer
)tmp_str
);
2171 /* Terminate the list */
2172 g_ptr_array_add (str_arr
, NULL
);
2174 path
= g_build_filenamev ((gchar
**)str_arr
->pdata
);
2175 g_ptr_array_free (str_arr
, TRUE
);
2181 anjuta_util_get_user_cache_filev (const gchar
* path
, va_list list
)
2183 gchar
*uri_str
, *base_path
, *dir
;
2185 const gchar anjuta_prefix
[] = "anjuta";
2186 base_path
= g_build_filename (g_get_user_cache_dir(), anjuta_prefix
, path
, NULL
);
2188 uri_str
= anjuta_util_construct_pathv (base_path
, list
);
2191 uri
= g_file_new_for_path (uri_str
);
2192 dir
= g_path_get_dirname (uri_str
);
2194 if (!anjuta_util_create_dir (dir
)) return NULL
;
2200 anjuta_util_get_user_cache_file (const gchar
* path
, ...)
2203 va_start (list
, path
);
2204 return anjuta_util_get_user_cache_filev (path
, list
);
2208 anjuta_util_get_user_config_filev (const gchar
* path
, va_list list
)
2210 gchar
*uri_str
, *base_path
, *dir
;
2212 const gchar anjuta_prefix
[] = "anjuta";
2213 base_path
= g_build_filename (g_get_user_config_dir(), anjuta_prefix
, path
, NULL
);
2215 uri_str
= anjuta_util_construct_pathv (base_path
, list
);
2218 uri
= g_file_new_for_path (uri_str
);
2219 dir
= g_path_get_dirname (uri_str
);
2221 if (!anjuta_util_create_dir (dir
)) return NULL
;
2227 anjuta_util_get_user_config_file (const gchar
* path
, ...)
2230 va_start (list
, path
);
2231 return anjuta_util_get_user_config_filev (path
, list
);
2235 anjuta_util_get_user_data_filev (const gchar
* path
, va_list list
)
2237 gchar
*uri_str
, *base_path
, *dir
;
2239 const gchar anjuta_prefix
[] = "anjuta";
2240 base_path
= g_build_filename (g_get_user_data_dir(), anjuta_prefix
, path
, NULL
);
2242 uri_str
= anjuta_util_construct_pathv (base_path
, list
);
2245 uri
= g_file_new_for_path (uri_str
);
2246 dir
= g_path_get_dirname (uri_str
);
2248 if (!anjuta_util_create_dir (dir
)) return NULL
;
2254 anjuta_util_get_user_data_file (const gchar
* path
, ...)
2257 va_start (list
, path
);
2258 return anjuta_util_get_user_data_filev (path
, list
);
2262 anjuta_util_get_user_cache_file_path (const gchar
* path
, ...)
2267 va_start (list
, path
);
2268 file
= anjuta_util_get_user_cache_filev (path
, list
);
2269 file_path
= g_file_get_path (file
);
2270 g_object_unref (file
);
2276 anjuta_util_get_user_config_file_path (const gchar
* path
, ...)
2281 va_start (list
, path
);
2282 file
= anjuta_util_get_user_config_filev (path
, list
);
2283 file_path
= g_file_get_path (file
);
2284 g_object_unref (file
);
2290 anjuta_util_get_user_data_file_path (const gchar
* path
, ...)
2295 va_start (list
, path
);
2296 file
= anjuta_util_get_user_data_filev (path
, list
);
2297 file_path
= g_file_get_path (file
);
2298 g_object_unref (file
);
2304 anjuta_util_convert_gfile_list_to_path_list (GList
*list
)
2307 GList
*current_file
;
2312 for (current_file
= list
; current_file
!= NULL
; current_file
= g_list_next (current_file
))
2314 path
= g_file_get_path (current_file
->data
);
2316 /* Ignore files with invalid paths */
2318 path_list
= g_list_append (path_list
, path
);
2325 anjuta_util_convert_gfile_list_to_relative_path_list (GList
*list
,
2326 const gchar
*parent
)
2330 GList
*current_file
;
2333 parent_file
= g_file_new_for_path (parent
);
2338 for (current_file
= list
; current_file
!= NULL
; current_file
= g_list_next (current_file
))
2340 path
= g_file_get_relative_path (parent_file
, current_file
->data
);
2342 /* Ignore files with invalid paths */
2344 path_list
= g_list_append (path_list
, path
);
2347 g_object_unref (parent_file
);
2355 * anjuta_util_builder_new:
2356 * @filename: Builder file name to open
2357 * @error: Optional error object, if NULL display a dialog if the file is missing
2359 * Create a new GtkBuilder object and load the file in it. Display an error
2360 * if the file is missing. Use a dialog if error is NULL, just a warning
2361 * if the error can be reported.
2363 * Returns: The new GtkBuilder object
2366 anjuta_util_builder_new (const gchar
*filename
, GError
**error
)
2368 GtkBuilder
*bxml
= gtk_builder_new ();
2371 /* Load glade file */
2372 if (!gtk_builder_add_from_file (bxml
, filename
, &err
))
2374 g_object_unref (bxml
);
2377 /* Display the error to the user if it cannot be reported to the caller */
2380 anjuta_util_dialog_error (NULL
, _("Unable to load user interface file: %s"), err
->message
);
2384 g_warning ("Couldn't load builder file: %s", err
->message
);
2386 g_propagate_error (error
, err
);
2389 /* Tag the builder object with the filename to allow better error message
2390 * with the following function */
2393 g_object_set_data_full (G_OBJECT (bxml
), "filename", g_strdup (filename
), g_free
);
2400 * anjuta_util_builder_get_objects:
2401 * @builder: Builder object
2402 * @first_widget: Name of first widget to get
2403 * ...: Address to store the first widget pointer, followed optionally by
2404 * more name/pointer pairs, followed by NULL
2406 * Create a new GtkBuilder object and load the file in it. Display an error
2407 * if the file is missing. Use a dialog if error is NULL, just a warning
2408 * if the error can be reported.
2410 * Returns: TRUE is everything works as expected.
2413 anjuta_util_builder_get_objects (GtkBuilder
*builder
, const gchar
*first_widget
,...)
2417 GObject
**object_ptr
;
2418 gboolean missing
= FALSE
;
2420 va_start (args
, first_widget
);
2422 for (name
= first_widget
; name
; name
= va_arg (args
, char *))
2424 object_ptr
= va_arg (args
, void *);
2425 *object_ptr
= gtk_builder_get_object (builder
, name
);
2427 /* Object not found, display a warning */
2430 const gchar
*filename
= (const gchar
*)g_object_get_data (G_OBJECT (builder
), "filename");
2433 g_warning ("Missing widget '%s' in file %s", name
, filename
);
2437 g_warning("Missing widget '%s'", name
);
2448 * anjuta_utils_drop_get_files:
2449 * @selection_data: the #GtkSelectionData from drag_data_received
2451 * Create a list of valid uri's from a uri-list drop.
2453 * Return value: (element-type GFile*): a list of GFiles
2456 anjuta_utils_drop_get_files (GtkSelectionData
*selection_data
)
2460 GSList
* files
= NULL
;
2462 uris
= g_uri_list_extract_uris ((gchar
*) gtk_selection_data_get_data (selection_data
));
2464 for (i
= 0; uris
[i
] != NULL
; i
++)
2466 GFile
* file
= g_file_new_for_uri (uris
[i
]);
2467 files
= g_slist_append(files
, file
);
2474 * anjuta_util_get_user_mail:
2476 * Returns: The e-mail Address of the logged-in user. The resulting string
2477 * must be free'd after use.
2480 anjuta_util_get_user_mail()
2482 /* FIXME: Use libfolks or something like it to query the mail address */
2483 return g_strconcat(g_get_user_name (), "@", g_get_host_name (), NULL
);
2487 * anjuta_utils_clone_string_gptrarray:
2488 * @source: The source GPtrArray containing items representing strings
2490 * Clones the contents of source GPtrArray into a new allocated GPtrArray.
2492 * Return a new allocated GPtrArray with strings g_strdup (), NULL on error.
2493 * The returned array has set g_free as GDestroyNotity function, so that user
2494 * should only care to g_ptr_array_unref () without freeing the strings.
2497 anjuta_util_clone_string_gptrarray (const GPtrArray
* source
)
2502 g_return_val_if_fail (source
!= NULL
, NULL
);
2504 dest
= g_ptr_array_sized_new (source
->len
);
2505 g_ptr_array_set_free_func (dest
, g_free
);
2507 for (i
= 0; i
< source
->len
; i
++)
2509 g_ptr_array_add (dest
, g_strdup (g_ptr_array_index (source
, i
)));
2516 anjuta_util_list_all_dir_children (GList
**children
, GFile
*dir
)
2518 GFileEnumerator
*list
;
2520 list
= g_file_enumerate_children (dir
,
2521 G_FILE_ATTRIBUTE_STANDARD_NAME
,
2522 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
2530 while ((info
= g_file_enumerator_next_file (list
, NULL
, NULL
)) != NULL
)
2535 name
= g_file_info_get_name (info
);
2536 file
= g_file_get_child (dir
, name
);
2537 g_object_unref (info
);
2539 if (g_file_query_file_type (file
, G_FILE_QUERY_INFO_NONE
, NULL
) == G_FILE_TYPE_DIRECTORY
)
2541 anjuta_util_list_all_dir_children (children
, file
);
2542 g_object_unref (file
);
2546 *children
= g_list_prepend (*children
, file
);
2549 g_file_enumerator_close (list
, NULL
, NULL
);
2550 g_object_unref (list
);
2555 anjuta_util_convert_string_list_to_array (GList
*list
)
2560 g_return_val_if_fail (list
!= NULL
, NULL
);
2562 res
= g_ptr_array_new_with_free_func (g_free
);
2565 while (node
!= NULL
)
2567 g_ptr_array_add (res
, g_strdup (node
->data
));
2569 node
= g_list_next (node
);