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: (transfer full): 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: (transfer full): A newly allocated strings list. The first argument
1127 * is the terminal program name. The following are the arguments needed to
1128 * execute 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:
1341 * @string: arguments
1343 * Parse arguments from a string to a GList.
1345 * Returns: (element-type utf8) (transfer full): A newly allocated GList of
1349 anjuta_util_parse_args_from_string (const gchar
* string
)
1353 gboolean is_quote
= FALSE
;
1354 gchar
* buffer
= g_new0(gchar
, strlen(string
) + 1);
1371 /* The current char was escaped */
1374 } else if (*s
== '\\') {
1375 /* Current char is an escape */
1377 } else if (is_quote
&& *s
== quote
) {
1378 /* Current char ends a quotation */
1380 if (!isspace(*(s
+1)) && (*(s
+1) != '\0')) {
1381 /* If there is no space after the quotation or it is not
1382 the end of the string */
1383 g_warning ("Parse error while parsing program arguments");
1385 } else if ((*s
== '\"' || *s
== '\'')) {
1387 /* Current char starts a quotation */
1391 /* Just a quote char inside quote */
1394 } else if (is_quote
){
1395 /* Any other char inside quote */
1397 } else if (isspace(*s
)) {
1398 /* Any white space outside quote */
1400 buffer
[idx
++] = '\0';
1401 args
= g_list_append (args
, g_strdup (buffer
));
1410 /* There are chars in the buffer. Flush as the last arg */
1411 buffer
[idx
++] = '\0';
1412 args
= g_list_append (args
, g_strdup (buffer
));
1416 g_warning ("Unclosed quotation encountered at the end of parsing");
1423 anjuta_util_escape_quotes(const gchar
* str
)
1427 const gchar
*s
= str
;
1429 g_return_val_if_fail(str
, NULL
);
1432 /* We are assuming there will be less than 2048 chars to escape */
1433 max_size
= strlen(str
) + 2048;
1434 buffer
= g_new (gchar
, max_size
);
1440 if (*s
== '\"' || *s
== '\'' || *s
== '\\')
1441 buffer
[idx
++] = '\\';
1449 /* Diff the text contained in uri with text. Return true if files
1450 differ, FALSE if they are identical.*/
1452 gboolean
anjuta_util_diff(const gchar
* uri
, const gchar
* text
)
1455 GFileInfo
*file_info
;
1457 gchar
* file_text
= NULL
;
1460 file
= g_file_new_for_uri (uri
);
1461 file_info
= g_file_query_info (file
,
1462 G_FILE_ATTRIBUTE_STANDARD_SIZE
,
1463 G_FILE_QUERY_INFO_NONE
,
1467 if (file_info
== NULL
)
1469 g_object_unref (file
);
1473 size
= g_file_info_get_attribute_uint64(file_info
,
1474 G_FILE_ATTRIBUTE_STANDARD_SIZE
);
1475 g_object_unref (file_info
);
1477 if (size
== 0 && text
== NULL
)
1479 g_object_unref (file
);
1482 else if (size
== 0 || text
== NULL
)
1484 g_object_unref (file
);
1488 if (!g_file_load_contents(file
,
1495 g_object_unref (file
);
1498 g_object_unref (file
);
1500 if (bytes_read
!= size
)
1506 /* according to g_file_load_contents's documentation
1507 * file_text is guaranteed to end with \0.
1509 if (strcmp (file_text
, text
) == 0)
1520 * anjuta_util_is_project_file:
1521 * @filename: the file name
1523 * Return %TRUE if the file is an anjuta project file. It is implemented by
1524 * checking only the file extension. So it does not check the existence
1525 * of the file. But it is working on an URI if it does not containt a
1528 * Returns: %TRUE if the file is a project file, else %FALSE
1531 anjuta_util_is_project_file (const gchar
*filename
)
1533 gsize len
= strlen (filename
);
1534 return ((len
> 8) && (strcmp (filename
+ len
- 7, ".anjuta") == 0));
1538 * anjuta_util_is_template_file:
1539 * @filename: the file name
1541 * Return %TRUE if the file is an template project file. It is implemented by
1542 * checking only the file extension. So it does not check the existence
1543 * of the file. But it is working on an URI if it does not containt a
1546 * Returns: %TRUE if the file is a template file, else %FALSE
1549 anjuta_util_is_template_file (const gchar
*filename
)
1551 gsize len
= strlen (filename
);
1552 return ((len
> 9) && (strcmp (filename
+ len
- 8, ".wiz.tgz") == 0));
1556 * anjuta_util_get_file_info_mine_type:
1557 * @info: the file information object
1559 * Return the mime type corresponding to a file infor object.
1561 * Returns: (transfer full) (allow-none): The mime type as a newly allocated
1562 * string that must be freed with g_free() or %NULL if the mime type cannot be found.
1565 anjuta_util_get_file_info_mime_type (GFileInfo
*info
)
1567 gchar
*mime_type
= NULL
;
1568 const gchar
*extension
;
1571 g_return_val_if_fail (info
!= NULL
, NULL
);
1574 /* If Anjuta is not installed in system gnome prefix, the mime types
1575 * may not have been correctly registed. In that case, we use the
1576 * following mime detection
1578 name
= g_file_info_get_name (info
);
1579 extension
= strrchr(name
, '.');
1580 if ((extension
!= NULL
) && (extension
!= name
))
1582 const static struct {gchar
*extension
; gchar
*type
;} anjuta_types
[] = {
1583 {"anjuta", "application/x-anjuta"},
1584 {"prj", "application/x-anjuta-old"},
1588 for (i
= 0; anjuta_types
[i
].extension
!= NULL
; i
++)
1590 if (strcmp(extension
+ 1, anjuta_types
[i
].extension
) == 0)
1592 mime_type
= g_strdup (anjuta_types
[i
].type
);
1598 /* Use mime database if it is not an Anjuta type */
1599 if (mime_type
== NULL
)
1601 mime_type
= g_content_type_get_mime_type (g_file_info_get_content_type(info
));
1608 * anjuta_util_get_file_mine_type:
1611 * Check if a file exists and return its mime type.
1613 * Returns: (transfer full) (allow-none): %NULL if the corresponding file doesn't
1614 * exist or the mime type as a newly allocated string that must be freed with
1618 anjuta_util_get_file_mime_type (GFile
*file
)
1621 gchar
*mime_type
= NULL
;
1623 g_return_val_if_fail (file
!= NULL
, NULL
);
1625 /* Get file information, check that the file exist at the same time */
1626 info
= g_file_query_info (file
,
1627 G_FILE_ATTRIBUTE_STANDARD_NAME
","
1628 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
,
1629 G_FILE_QUERY_INFO_NONE
,
1635 mime_type
= anjuta_util_get_file_info_mime_type (info
);
1636 g_object_unref (info
);
1643 anjuta_util_get_local_path_from_uri (const gchar
*uri
)
1648 file
= g_file_new_for_uri (uri
);
1649 local_path
= g_file_get_path (file
);
1650 g_object_unref (file
);
1655 #ifdef EMULATE_FORKPTY
1658 static int ptym_open (char *pts_name
);
1659 static int ptys_open (int fdm
, char * pts_name
);
1662 login_tty(int ttyfd
)
1674 /* First disconnect from the old controlling tty. */
1676 fd
= open("/dev/tty", O_RDWR
|O_NOCTTY
);
1679 ioctl(fd
, TIOCNOTTY
, NULL
);
1683 //syslog(LOG_WARNING, "NO CTTY");
1684 #endif /* TIOCNOTTY */
1686 /* Verify that we are successfully disconnected from the controlling tty. */
1687 fd
= open("/dev/tty", O_RDWR
|O_NOCTTY
);
1690 //syslog(LOG_WARNING, "Failed to disconnect from controlling tty.");
1694 /* Make it our controlling tty. */
1696 ioctl(ttyfd
, TIOCSCTTY
, NULL
);
1697 #endif /* TIOCSCTTY */
1699 fdname
= ttyname (ttyfd
);
1700 fd
= open(fdname
, O_RDWR
);
1702 ;//syslog(LOG_WARNING, "open %s: %s", fdname, strerror(errno));
1706 /* Verify that we now have a controlling tty. */
1707 fd
= open("/dev/tty", O_WRONLY
);
1710 //syslog(LOG_WARNING, "open /dev/tty: %s", strerror(errno));
1715 #if defined(HAVE_VHANGUP) && !defined(HAVE_REVOKE)
1717 RETSIGTYPE (*sig
)();
1718 sig
= signal(SIGHUP
, SIG_IGN
);
1720 signal(SIGHUP
, sig
);
1723 fd
= open(fdname
, O_RDWR
);
1726 //syslog(LOG_ERR, "can't reopen ctty %s: %s", fdname, strerror(errno));
1748 openpty(int *amaster
, int *aslave
, char *name
, struct termios
*termp
,
1749 struct winsize
*winp
)
1752 *amaster
= ptym_open(line
);
1755 *aslave
= ptys_open(*amaster
, line
);
1763 #define TCSAFLUSH TCSETAF
1766 (void) tcsetattr(*aslave
, TCSAFLUSH
, termp
);
1769 (void) ioctl(*aslave
, TIOCSWINSZ
, (char *)winp
);
1775 ptym_open(char * pts_name
)
1781 strcpy(pts_name
, "/dev/ptmx");
1782 fdm
= open(pts_name
, O_RDWR
);
1785 if (grantpt(fdm
) < 0) { /* grant access to slave */
1789 if (unlockpt(fdm
) < 0) { /* clear slave's lock flag */
1794 if (ptr
== NULL
) { /* get slave's name */
1798 strcpy(pts_name
, ptr
); /* return name of slave */
1799 return fdm
; /* return fd of master */
1803 strcpy(pts_name
, "/dev/ptyXY");
1804 /* array index: 012345689 (for references in following code) */
1805 for (ptr1
= "pqrstuvwxyzPQRST"; *ptr1
!= 0; ptr1
++) {
1806 pts_name
[8] = *ptr1
;
1807 for (ptr2
= "0123456789abcdef"; *ptr2
!= 0; ptr2
++) {
1808 pts_name
[9] = *ptr2
;
1809 /* try to open master */
1810 fdm
= open(pts_name
, O_RDWR
);
1812 if (errno
== ENOENT
) /* different from EIO */
1813 return -1; /* out of pty devices */
1815 continue; /* try next pty device */
1817 pts_name
[5] = 't'; /* chage "pty" to "tty" */
1818 return fdm
; /* got it, return fd of master */
1821 return -1; /* out of pty devices */
1826 ptys_open(int fdm
, char * pts_name
)
1830 /* following should allocate controlling terminal */
1831 fds
= open(pts_name
, O_RDWR
);
1836 if (ioctl(fds
, I_PUSH
, "ptem") < 0) {
1841 if (ioctl(fds
, I_PUSH
, "ldterm") < 0) {
1846 if (ioctl(fds
, I_PUSH
, "ttcompat") < 0) {
1852 if (ioctl(fdm
, I_PUSH
, "pckt") < 0) {
1858 if (ioctl(fdm
, I_SRDOPT
, RMSGN
|RPROTDAT
) < 0) {
1867 struct group
*grptr
;
1869 grptr
= getgrnam("tty");
1871 gid
= grptr
->gr_gid
;
1873 gid
= -1; /* group tty is not in the group file */
1874 /* following two functions don't work unless we're root */
1875 chown(pts_name
, getuid(), gid
);
1876 chmod(pts_name
, S_IRUSR
| S_IWUSR
| S_IWGRP
);
1877 fds
= open(pts_name
, O_RDWR
);
1887 forkpty(int *amaster
, char *name
, struct termios
*termp
, struct winsize
*winp
)
1889 int master
, slave
, pid
;
1891 if (openpty(&master
, &slave
, name
, termp
, winp
) == -1)
1893 switch (pid
= fork()) {
1912 int scandir(const char *dir
, struct dirent
***namelist
,
1913 int (*select
)(const struct dirent
*),
1914 int (*compar
)(const struct dirent
**, const struct dirent
**))
1917 struct dirent
*entry
;
1921 if ((d
=opendir(dir
)) == NULL
)
1925 while ((entry
=readdir(d
)) != NULL
)
1927 if (select
== NULL
|| (select
!= NULL
&& (*select
)(entry
)))
1929 *namelist
=(struct dirent
**)realloc((void *)(*namelist
),
1930 (size_t)((i
+1)*sizeof(struct dirent
*)));
1931 if (*namelist
== NULL
) return(-1);
1932 entrysize
=sizeof(struct dirent
)-sizeof(entry
->d_name
)+strlen(entry
->d_name
)+1;
1933 (*namelist
)[i
]=(struct dirent
*)malloc(entrysize
);
1934 if ((*namelist
)[i
] == NULL
) return(-1);
1935 memcpy((*namelist
)[i
], entry
, entrysize
);
1939 if (closedir(d
)) return(-1);
1940 if (i
== 0) return(-1);
1942 qsort((void *)(*namelist
), (size_t)i
, sizeof(struct dirent
*), compar
);
1947 #endif /* EMULATE_FORKPTY */
1950 anjuta_util_help_display (GtkWidget
*parent
,
1951 const gchar
*doc_id
,
1954 GError
*error
= NULL
;
1958 command
= g_strdup_printf ("yelp help:%s%s%s",
1960 item
== NULL
? "" : "/",
1961 item
== NULL
? "" : item
);
1963 if (!g_spawn_command_line_async (command
, &error
) &&
1966 g_warning ("Error executing help application: %s",
1968 g_error_free (error
);
1974 /* The following functions are taken from gedit */
1976 /* Note that this function replace home dir with ~ */
1978 anjuta_util_uri_get_dirname (const gchar
*uri
)
1983 // CHECK: does it work with uri chaining? - Paolo
1984 str
= g_path_get_dirname (uri
);
1985 g_return_val_if_fail (str
!= NULL
, ".");
1987 if ((strlen (str
) == 1) && (*str
== '.'))
1994 res
= anjuta_util_replace_home_dir_with_tilde (str
);
2002 anjuta_util_replace_home_dir_with_tilde (const gchar
*uri
)
2007 g_return_val_if_fail (uri
!= NULL
, NULL
);
2009 /* Note that g_get_home_dir returns a const string */
2010 tmp
= (gchar
*)g_get_home_dir ();
2013 return g_strdup (uri
);
2015 home
= g_filename_to_utf8 (tmp
, -1, NULL
, NULL
, NULL
);
2017 return g_strdup (uri
);
2019 if (strcmp (uri
, home
) == 0)
2023 return g_strdup ("~");
2027 home
= g_strdup_printf ("%s/", tmp
);
2030 if (g_str_has_prefix (uri
, home
))
2034 res
= g_strdup_printf ("~/%s", uri
+ strlen (home
));
2043 return g_strdup (uri
);
2047 * anjuta_util_shell_expand:
2048 * @string: input string
2050 * Expand environment variables $(var_name) and tilde (~) in the input string.
2052 * Returns: (transfer full): a newly-allocated string that must be freed with g_free().
2055 anjuta_util_shell_expand (const gchar
*string
)
2059 if (string
== NULL
) return NULL
;
2061 expand
= g_string_sized_new (strlen (string
));
2063 for (; *string
!= '\0'; string
++)
2069 /* Variable expansion */
2074 while (isalnum (*end
) || (*end
== '_')) end
++;
2075 var_name_len
= end
- string
- 1;
2076 if (var_name_len
> 0)
2080 g_string_append_len (expand
, string
+ 1, var_name_len
);
2081 value
= g_getenv (expand
->str
+ expand
->len
- var_name_len
);
2082 g_string_truncate (expand
, expand
->len
- var_name_len
);
2083 g_string_append (expand
, value
);
2091 /* User home directory expansion */
2092 if (isspace(string
[1]) || (string
[1] == G_DIR_SEPARATOR
) || (string
[1] == '\0'))
2094 g_string_append (expand
, g_get_home_dir());
2102 g_string_append_c (expand
, *string
);
2105 return g_string_free (expand
, FALSE
);
2109 anjuta_util_str_middle_truncate (const gchar
*string
,
2110 guint truncate_length
)
2115 guint num_left_chars
;
2117 guint delimiter_length
;
2118 const gchar
*delimiter
= "\342\200\246";
2120 g_return_val_if_fail (string
!= NULL
, NULL
);
2122 length
= strlen (string
);
2124 g_return_val_if_fail (g_utf8_validate (string
, length
, NULL
), NULL
);
2126 /* It doesnt make sense to truncate strings to less than
2127 * the size of the delimiter plus 2 characters (one on each
2130 delimiter_length
= g_utf8_strlen (delimiter
, -1);
2131 if (truncate_length
< (delimiter_length
+ 2)) {
2132 return g_strdup (string
);
2135 n_chars
= g_utf8_strlen (string
, length
);
2137 /* Make sure the string is not already small enough. */
2138 if (n_chars
<= truncate_length
) {
2139 return g_strdup (string
);
2142 /* Find the 'middle' where the truncation will occur. */
2143 num_left_chars
= (truncate_length
- delimiter_length
) / 2;
2144 right_offset
= n_chars
- truncate_length
+ num_left_chars
+ delimiter_length
;
2146 truncated
= g_string_new_len (string
,
2147 g_utf8_offset_to_pointer (string
, num_left_chars
) - string
);
2148 g_string_append (truncated
, delimiter
);
2149 g_string_append (truncated
, g_utf8_offset_to_pointer (string
, right_offset
));
2151 return g_string_free (truncated
, FALSE
);
2155 * Functions to implement XDG Base Directory Specification
2156 * http://standards.freedesktop.org/basedir-spec/latest/index.html
2157 * Use this to save any config/cache/data files
2162 anjuta_util_construct_pathv (const gchar
* str
, va_list str_list
)
2165 const gchar
* tmp_str
;
2168 str_arr
= g_ptr_array_new();
2169 g_ptr_array_add (str_arr
, (gpointer
) str
);
2171 /* Extract elements from va_list */
2174 while ((tmp_str
= va_arg (str_list
, const gchar
*)) != NULL
)
2176 g_ptr_array_add (str_arr
, (gpointer
)tmp_str
);
2181 /* Terminate the list */
2182 g_ptr_array_add (str_arr
, NULL
);
2184 path
= g_build_filenamev ((gchar
**)str_arr
->pdata
);
2185 g_ptr_array_free (str_arr
, TRUE
);
2191 anjuta_util_get_user_cache_filev (const gchar
* path
, va_list list
)
2193 gchar
*uri_str
, *base_path
, *dir
;
2195 const gchar anjuta_prefix
[] = "anjuta";
2196 base_path
= g_build_filename (g_get_user_cache_dir(), anjuta_prefix
, path
, NULL
);
2198 uri_str
= anjuta_util_construct_pathv (base_path
, list
);
2201 uri
= g_file_new_for_path (uri_str
);
2202 dir
= g_path_get_dirname (uri_str
);
2204 if (!anjuta_util_create_dir (dir
)) return NULL
;
2210 anjuta_util_get_user_cache_file (const gchar
* path
, ...)
2213 va_start (list
, path
);
2214 return anjuta_util_get_user_cache_filev (path
, list
);
2218 anjuta_util_get_user_config_filev (const gchar
* path
, va_list list
)
2220 gchar
*uri_str
, *base_path
, *dir
;
2222 const gchar anjuta_prefix
[] = "anjuta";
2223 base_path
= g_build_filename (g_get_user_config_dir(), anjuta_prefix
, path
, NULL
);
2225 uri_str
= anjuta_util_construct_pathv (base_path
, list
);
2228 uri
= g_file_new_for_path (uri_str
);
2229 dir
= g_path_get_dirname (uri_str
);
2231 if (!anjuta_util_create_dir (dir
)) return NULL
;
2237 anjuta_util_get_user_config_file (const gchar
* path
, ...)
2240 va_start (list
, path
);
2241 return anjuta_util_get_user_config_filev (path
, list
);
2245 anjuta_util_get_user_data_filev (const gchar
* path
, va_list list
)
2247 gchar
*uri_str
, *base_path
, *dir
;
2249 const gchar anjuta_prefix
[] = "anjuta";
2250 base_path
= g_build_filename (g_get_user_data_dir(), anjuta_prefix
, path
, NULL
);
2252 uri_str
= anjuta_util_construct_pathv (base_path
, list
);
2255 uri
= g_file_new_for_path (uri_str
);
2256 dir
= g_path_get_dirname (uri_str
);
2258 if (!anjuta_util_create_dir (dir
)) return NULL
;
2264 anjuta_util_get_user_data_file (const gchar
* path
, ...)
2267 va_start (list
, path
);
2268 return anjuta_util_get_user_data_filev (path
, list
);
2272 anjuta_util_get_user_cache_file_path (const gchar
* path
, ...)
2277 va_start (list
, path
);
2278 file
= anjuta_util_get_user_cache_filev (path
, list
);
2279 file_path
= g_file_get_path (file
);
2280 g_object_unref (file
);
2286 anjuta_util_get_user_config_file_path (const gchar
* path
, ...)
2291 va_start (list
, path
);
2292 file
= anjuta_util_get_user_config_filev (path
, list
);
2293 file_path
= g_file_get_path (file
);
2294 g_object_unref (file
);
2300 anjuta_util_get_user_data_file_path (const gchar
* path
, ...)
2305 va_start (list
, path
);
2306 file
= anjuta_util_get_user_data_filev (path
, list
);
2307 file_path
= g_file_get_path (file
);
2308 g_object_unref (file
);
2314 anjuta_util_convert_gfile_list_to_path_list (GList
*list
)
2317 GList
*current_file
;
2322 for (current_file
= list
; current_file
!= NULL
; current_file
= g_list_next (current_file
))
2324 path
= g_file_get_path (current_file
->data
);
2326 /* Ignore files with invalid paths */
2328 path_list
= g_list_append (path_list
, path
);
2335 anjuta_util_convert_gfile_list_to_relative_path_list (GList
*list
,
2336 const gchar
*parent
)
2340 GList
*current_file
;
2343 parent_file
= g_file_new_for_path (parent
);
2348 for (current_file
= list
; current_file
!= NULL
; current_file
= g_list_next (current_file
))
2350 path
= g_file_get_relative_path (parent_file
, current_file
->data
);
2352 /* Ignore files with invalid paths */
2354 path_list
= g_list_append (path_list
, path
);
2357 g_object_unref (parent_file
);
2365 * anjuta_util_builder_new:
2366 * @filename: Builder file name to open
2367 * @error: Optional error object, if %NULL display a dialog if the file is missing
2369 * Create a new GtkBuilder object and load the file in it. Display an error
2370 * if the file is missing. Use a dialog if error is %NULL, just a warning
2371 * if the error can be reported.
2373 * Returns: The new GtkBuilder object
2376 anjuta_util_builder_new (const gchar
*filename
, GError
**error
)
2378 GtkBuilder
*bxml
= gtk_builder_new ();
2381 /* Load glade file */
2382 if (!gtk_builder_add_from_file (bxml
, filename
, &err
))
2384 g_object_unref (bxml
);
2387 /* Display the error to the user if it cannot be reported to the caller */
2390 anjuta_util_dialog_error (NULL
, _("Unable to load user interface file: %s"), err
->message
);
2394 g_warning ("Couldn't load builder file: %s", err
->message
);
2396 g_propagate_error (error
, err
);
2399 /* Tag the builder object with the filename to allow better error message
2400 * with the following function */
2403 g_object_set_data_full (G_OBJECT (bxml
), "filename", g_strdup (filename
), g_free
);
2410 * anjuta_util_builder_get_objects:
2411 * @builder: Builder object
2412 * @first_widget: Name of first widget to get
2413 * ...: Address to store the first widget pointer, followed optionally by
2414 * more name/pointer pairs, followed by %NULL
2416 * Create a new GtkBuilder object and load the file in it. Display an error
2417 * if the file is missing. Use a dialog if error is %NULL, just a warning
2418 * if the error can be reported.
2420 * Returns: %TRUE is everything works as expected.
2423 anjuta_util_builder_get_objects (GtkBuilder
*builder
, const gchar
*first_widget
,...)
2427 GObject
**object_ptr
;
2428 gboolean missing
= FALSE
;
2430 va_start (args
, first_widget
);
2432 for (name
= first_widget
; name
; name
= va_arg (args
, char *))
2434 object_ptr
= va_arg (args
, void *);
2435 *object_ptr
= gtk_builder_get_object (builder
, name
);
2437 /* Object not found, display a warning */
2440 const gchar
*filename
= (const gchar
*)g_object_get_data (G_OBJECT (builder
), "filename");
2443 g_warning ("Missing widget '%s' in file %s", name
, filename
);
2447 g_warning("Missing widget '%s'", name
);
2458 * anjuta_utils_drop_get_files:
2459 * @selection_data: the #GtkSelectionData from drag_data_received
2461 * Create a list of valid uri's from a uri-list drop.
2463 * Return value: (element-type GFile*): a list of GFiles
2466 anjuta_utils_drop_get_files (GtkSelectionData
*selection_data
)
2470 GSList
* files
= NULL
;
2472 uris
= g_uri_list_extract_uris ((gchar
*) gtk_selection_data_get_data (selection_data
));
2474 for (i
= 0; uris
[i
] != NULL
; i
++)
2476 GFile
* file
= g_file_new_for_uri (uris
[i
]);
2477 files
= g_slist_append(files
, file
);
2484 * anjuta_util_get_user_mail:
2486 * Returns: The e-mail Address of the logged-in user. The resulting string
2487 * must be free'd after use.
2490 anjuta_util_get_user_mail()
2492 /* FIXME: Use libfolks or something like it to query the mail address */
2493 return g_strconcat(g_get_user_name (), "@", g_get_host_name (), NULL
);
2497 * anjuta_utils_clone_string_gptrarray:
2498 * @source: The source GPtrArray containing items representing strings
2500 * Clones the contents of source GPtrArray into a new allocated GPtrArray.
2502 * Return a new allocated GPtrArray with strings g_strdup (), %NULL on error.
2503 * The returned array has set g_free as GDestroyNotity function, so that user
2504 * should only care to g_ptr_array_unref () without freeing the strings.
2507 anjuta_util_clone_string_gptrarray (const GPtrArray
* source
)
2512 g_return_val_if_fail (source
!= NULL
, NULL
);
2514 dest
= g_ptr_array_sized_new (source
->len
);
2515 g_ptr_array_set_free_func (dest
, g_free
);
2517 for (i
= 0; i
< source
->len
; i
++)
2519 g_ptr_array_add (dest
, g_strdup (g_ptr_array_index (source
, i
)));
2526 anjuta_util_list_all_dir_children (GList
**children
, GFile
*dir
)
2528 GFileEnumerator
*list
;
2530 list
= g_file_enumerate_children (dir
,
2531 G_FILE_ATTRIBUTE_STANDARD_NAME
,
2532 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
2540 while ((info
= g_file_enumerator_next_file (list
, NULL
, NULL
)) != NULL
)
2545 name
= g_file_info_get_name (info
);
2546 file
= g_file_get_child (dir
, name
);
2547 g_object_unref (info
);
2549 if (g_file_query_file_type (file
, G_FILE_QUERY_INFO_NONE
, NULL
) == G_FILE_TYPE_DIRECTORY
)
2551 anjuta_util_list_all_dir_children (children
, file
);
2552 g_object_unref (file
);
2556 *children
= g_list_prepend (*children
, file
);
2559 g_file_enumerator_close (list
, NULL
, NULL
);
2560 g_object_unref (list
);
2565 anjuta_util_convert_string_list_to_array (GList
*list
)
2570 g_return_val_if_fail (list
!= NULL
, NULL
);
2572 res
= g_ptr_array_new_with_free_func (g_free
);
2575 while (node
!= NULL
)
2577 g_ptr_array_add (res
, g_strdup (node
->data
));
2579 node
= g_list_next (node
);