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
, const gchar
*mesg
, ...)
390 GtkWindow
*real_parent
;
392 va_start (args
, mesg
);
393 message
= g_strdup_vprintf (mesg
, args
);
396 if (parent
&& GTK_IS_WINDOW (parent
))
398 real_parent
= parent
;
405 dialog
= gtk_message_dialog_new (real_parent
,
406 GTK_DIALOG_DESTROY_WITH_PARENT
,
407 GTK_MESSAGE_QUESTION
,
408 GTK_BUTTONS_YES_NO
, "%s", message
);
410 ret
= gtk_dialog_run (GTK_DIALOG (dialog
));
411 gtk_widget_destroy (dialog
);
414 return (ret
== GTK_RESPONSE_YES
);
418 anjuta_util_dialog_input (GtkWindow
*parent
, const gchar
*prompt
,
419 const gchar
*default_value
, gchar
**return_value
)
421 GtkWidget
*dialog
, *label
, *frame
, *entry
, *dialog_vbox
, *vbox
;
424 GtkWindow
*real_parent
;
426 if (parent
&& GTK_IS_WINDOW (parent
))
428 real_parent
= parent
;
435 dialog
= gtk_dialog_new_with_buttons (prompt
, real_parent
,
436 GTK_DIALOG_DESTROY_WITH_PARENT
,
437 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
438 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
440 gtk_dialog_set_default_response (GTK_DIALOG (dialog
), GTK_RESPONSE_OK
);
441 dialog_vbox
= gtk_dialog_get_content_area (GTK_DIALOG (dialog
));
442 gtk_window_set_default_size (GTK_WINDOW (dialog
), 400, -1);
443 gtk_widget_show (dialog_vbox
);
445 markup
= g_strconcat ("<b>", prompt
, "</b>", NULL
);
446 label
= gtk_label_new (NULL
);
447 gtk_label_set_markup (GTK_LABEL (label
), markup
);
448 gtk_widget_show (label
);
451 frame
= gtk_frame_new (NULL
);
452 gtk_frame_set_label_widget (GTK_FRAME (frame
), label
);
453 gtk_frame_set_shadow_type (GTK_FRAME (frame
), GTK_SHADOW_NONE
);
454 gtk_container_set_border_width (GTK_CONTAINER (frame
), 10);
455 gtk_widget_show (frame
);
456 gtk_box_pack_start (GTK_BOX (dialog_vbox
), frame
, FALSE
, FALSE
, 0);
458 vbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 0);
459 gtk_widget_show (vbox
);
460 gtk_container_set_border_width (GTK_CONTAINER (vbox
), 10);
461 gtk_container_add (GTK_CONTAINER (frame
), vbox
);
463 entry
= gtk_entry_new ();
464 gtk_widget_show (entry
);
465 gtk_entry_set_activates_default (GTK_ENTRY (entry
), TRUE
);
466 gtk_box_pack_start (GTK_BOX (vbox
), entry
, FALSE
, FALSE
, 0);
468 gtk_entry_set_text (GTK_ENTRY (entry
), default_value
);
470 res
= gtk_dialog_run (GTK_DIALOG (dialog
));
472 if (gtk_entry_get_text (GTK_ENTRY (entry
)) &&
473 strlen (gtk_entry_get_text (GTK_ENTRY (entry
))) > 0)
475 *return_value
= g_strdup (gtk_entry_get_text (GTK_ENTRY (entry
)));
479 *return_value
= NULL
;
481 gtk_widget_destroy (dialog
);
482 return (res
== GTK_RESPONSE_OK
);
486 on_install_files_done (GObject
*proxy
, GAsyncResult
*result
,
489 GError
*error
= NULL
;
490 g_dbus_proxy_call_finish ((GDBusProxy
*) proxy
, result
, &error
);
494 Only dbus error is handled. Rest of the errors are from packagekit
495 which have already been notified to user by packagekit.
497 if (error
->domain
== G_DBUS_ERROR
)
499 const gchar
*error_message
= NULL
;
501 /* Service error which implies packagekit is missing */
502 if (error
->code
== G_DBUS_ERROR_SERVICE_UNKNOWN
)
504 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.");
506 /* General dbus error implies failure to call dbus method */
507 else if (error
->code
!= G_DBUS_ERROR_NO_REPLY
)
509 error_message
= error
->message
;
512 anjuta_util_dialog_error (NULL
,
513 _("Installation failed: %s"),
516 g_error_free (error
);
521 anjuta_util_install_files (const gchar
* const names
)
523 GDBusConnection
* connection
;
531 connection
= g_bus_get_sync (G_BUS_TYPE_SESSION
, NULL
, NULL
);
535 proxy
= g_dbus_proxy_new_sync (connection
,
536 G_DBUS_PROXY_FLAGS_NONE
,
538 "org.freedesktop.PackageKit",
539 "/org/freedesktop/PackageKit",
540 "org.freedesktop.PackageKit.Modify",
546 pkgv
= g_strsplit (names
, ", ", 0);
547 g_dbus_proxy_call (proxy
, "InstallProvideFiles",
548 g_variant_new ("(u^ass)",
552 G_DBUS_CALL_FLAGS_NONE
,
555 on_install_files_done
,
562 anjuta_util_package_is_installed (const gchar
* package
, gboolean show
)
564 gboolean installed
= FALSE
;
569 if ((pid
= fork()) == 0)
570 execlp ("pkg-config", "pkg-config", "--exists", package
, NULL
);
572 waitpid (pid
, &status
, 0);
573 exit_status
= WEXITSTATUS (status
);
574 installed
= (exit_status
== 0) ? TRUE
: FALSE
;
580 anjuta_util_dialog_error (NULL
,
581 _("The \"%s\" package is not installed.\n"
582 "Please install it."), package
);
589 anjuta_util_prog_is_installed (const gchar
* prog
, gboolean show
)
591 gchar
* prog_path
= g_find_program_in_path (prog
);
599 anjuta_util_dialog_error (NULL
, _("The \"%s\" utility is not installed.\n"
600 "Please install it."), prog
);
606 anjuta_util_get_a_tmp_file (void)
608 static gint count
= 0;
612 tmpdir
= g_get_tmp_dir ();
614 g_strdup_printf ("%s/anjuta_%d.%d", tmpdir
, count
++, getpid ());
618 /* GList of strings operations */
620 anjuta_util_glist_from_string (const gchar
*string
)
622 gchar
*str
, *temp
, buff
[256];
624 gchar
*word_start
, *word_end
;
627 temp
= g_strdup (string
);
637 /* Remove leading spaces */
638 while (isspace (*str
) && *str
!= '\0')
643 /* Find start and end of word */
645 while (!isspace (*str
) && *str
!= '\0')
649 /* Copy the word into the buffer */
650 for (ptr
= word_start
, i
= 0; ptr
< word_end
; ptr
++, i
++)
654 list
= g_list_append (list
, g_strdup (buff
));
663 /* Prefix the strings */
665 anjuta_util_glist_strings_prefix (GList
* list
, const gchar
*prefix
)
670 g_return_if_fail (prefix
!= NULL
);
675 node
->data
= g_strconcat (prefix
, tmp
, NULL
);
676 if (tmp
) g_free (tmp
);
677 node
= g_list_next (node
);
681 /* Suffix the strings */
683 anjuta_util_glist_strings_sufix (GList
* list
, const gchar
*sufix
)
688 g_return_if_fail (sufix
!= NULL
);
693 node
->data
= g_strconcat (tmp
, sufix
, NULL
);
694 if (tmp
) g_free (tmp
);
695 node
= g_list_next (node
);
699 /* Duplicate list of strings */
701 anjuta_util_glist_strings_dup (GList
* list
)
711 new_list
= g_list_append (new_list
, g_strdup(node
->data
));
713 new_list
= g_list_append (new_list
, NULL
);
714 node
= g_list_next (node
);
719 /* Join list of strings using the given delimiter */
721 anjuta_util_glist_strings_join (GList
* list
, gchar
*delimiter
)
724 gboolean first
= TRUE
;
727 joined
= g_string_new (NULL
);
734 g_string_append (joined
, delimiter
);
737 g_string_append (joined
, node
->data
);
739 node
= g_list_next (node
);
742 return g_string_free (joined
, FALSE
);
744 g_string_free (joined
, TRUE
);
749 anjuta_util_get_real_path (const gchar
*path
)
755 gchar buf
[PATH_MAX
+1];
757 result
= realpath (path
, buf
);
760 *(buf
+ PATH_MAX
) = '\0'; /* ensure a terminator */
761 return g_strdup (buf
);
765 /* the string returned by realpath should be cleaned with
766 free(), not g_free() */
767 buf
= realpath (path
, NULL
);
770 result
= g_strdup (buf
);
780 * anjuta_util_get_current_dir:
782 * Get current working directory, unlike g_get_current_dir, keeps symbolic links
785 * Returns: The current working directory.
788 anjuta_util_get_current_dir (void)
792 pwd
= g_getenv ("PWD");
795 return g_strdup (pwd
);
799 return g_get_current_dir ();
804 is_valid_scheme_character (char c
)
806 return g_ascii_isalnum (c
) || c
== '+' || c
== '-' || c
== '.';
809 /* Following RFC 2396, valid schemes are built like:
810 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
813 has_valid_scheme (const char *uri
)
819 if (!g_ascii_isalpha (*p
))
824 } while (is_valid_scheme_character (*p
));
830 * anjuta_util_file_new_for_commandline_arg:
832 * @arg: URI or relative or absolute file path
834 * Create a new file corresponding to arg, unlike g_file_new_for_commandline_arg,
835 * keeps symbolic links in path name.
837 * Returns: A new GFile object
840 anjuta_util_file_new_for_commandline_arg (const gchar
*arg
)
846 g_return_val_if_fail (arg
!= NULL
, NULL
);
848 if (g_path_is_absolute (arg
))
849 return g_file_new_for_path (arg
);
851 if (has_valid_scheme (arg
))
852 return g_file_new_for_uri (arg
);
854 current_dir
= anjuta_util_get_current_dir ();
855 filename
= g_build_filename (current_dir
, arg
, NULL
);
856 g_free (current_dir
);
858 file
= g_file_new_for_path (filename
);
865 /* Dedup a list of paths - duplicates are removed from the tail.
866 ** Useful for deduping Recent Files and Recent Projects */
868 anjuta_util_glist_path_dedup(GList
*list
)
870 GList
*nlist
= NULL
, *tmp
, *tmp1
;
873 for (tmp
= list
; tmp
; tmp
= g_list_next(tmp
))
875 path
= anjuta_util_get_real_path ((const gchar
*) tmp
->data
);
878 if (stat (path
, &s
) != 0)
884 for (tmp1
= nlist
; tmp1
; tmp1
= g_list_next(tmp1
))
886 if (0 == strcmp((const char *) tmp1
->data
, path
))
894 nlist
= g_list_prepend(nlist
, path
);
898 anjuta_util_glist_strings_free(list
);
899 nlist
= g_list_reverse(nlist
);
904 sort_node (gchar
* a
, gchar
*b
)
906 if ( !a
&& !b
) return 0;
907 else if (!a
) return -1;
908 else if (!b
) return 1;
909 return strcmp (a
, b
);
912 /* Sort the list alphabatically */
914 anjuta_util_glist_strings_sort (GList
* list
)
916 return g_list_sort(list
, (GCompareFunc
)sort_node
);
919 /* Free the strings and GList */
921 anjuta_util_glist_strings_free (GList
* list
)
923 g_list_foreach (list
, (GFunc
) g_free
, NULL
);
928 anjuta_util_type_from_string (AnjutaUtilStringMap
*map
, const char *str
)
932 while (-1 != map
[i
].type
)
934 if (0 == strcmp(map
[i
].name
, str
))
942 anjuta_util_string_from_type (AnjutaUtilStringMap
*map
, int type
)
945 while (-1 != map
[i
].type
)
947 if (map
[i
].type
== type
)
955 anjuta_util_glist_from_map (AnjutaUtilStringMap
*map
)
957 GList
*out_list
= NULL
;
959 while (-1 != map
[i
].type
)
961 out_list
= g_list_append(out_list
, map
[i
].name
);
969 anjuta_util_update_string_list (GList
*p_list
, const gchar
*p_str
, gint length
)
975 for (i
= 0; i
< g_list_length (p_list
); i
++)
977 str
= (gchar
*) g_list_nth_data (p_list
, i
);
980 if (strcmp (p_str
, str
) == 0)
982 p_list
= g_list_remove (p_list
, str
);
983 p_list
= g_list_prepend (p_list
, str
);
987 p_list
= g_list_prepend (p_list
, g_strdup (p_str
));
988 while (g_list_length (p_list
) > length
)
990 str
= g_list_nth_data (p_list
, g_list_length (p_list
) - 1);
991 p_list
= g_list_remove (p_list
, str
);
998 anjuta_util_create_dir (const gchar
* path
)
1000 GFile
*dir
= g_file_new_for_path (path
);
1004 if (g_file_query_exists (dir
, NULL
))
1006 GFileInfo
*info
= g_file_query_info (dir
,
1007 G_FILE_ATTRIBUTE_STANDARD_TYPE
,
1008 G_FILE_QUERY_INFO_NONE
,
1010 if (g_file_info_get_file_type (info
) != G_FILE_TYPE_DIRECTORY
)
1012 g_message ("Warning: %s is a file. \n "
1013 "It is trying to be treated as a directory.",g_file_get_path (dir
));
1014 g_object_unref (dir
);
1017 g_object_unref (info
);
1021 parent
= g_path_get_dirname (path
);
1022 if (anjuta_util_create_dir (parent
))
1025 if (!g_file_make_directory (dir
, NULL
, &err
))
1027 g_warning ("Error directory:\n %s", err
->message
);
1028 g_object_unref (dir
);
1035 g_object_unref (dir
);
1039 g_object_unref (dir
);
1045 * anjuta_util_user_shell:
1047 * Retrieves the user's preferred shell.
1049 * Returns: A newly allocated string that is the path to the shell.
1051 /* copied from deprecated gnome_util_user_shell in libgnome */
1053 anjuta_util_user_shell (void)
1059 const gchar shells
[][14] = {
1060 /* Note that on some systems shells can also
1061 * be installed in /usr/bin */
1062 "/bin/bash", "/usr/bin/bash",
1063 "/bin/zsh", "/usr/bin/zsh",
1064 "/bin/tcsh", "/usr/bin/tcsh",
1065 "/bin/ksh", "/usr/bin/ksh",
1066 "/bin/csh", "/bin/sh"
1069 if (geteuid () == getuid () &&
1070 getegid () == getgid ()) {
1071 /* only in non-setuid */
1072 if ((shell
= g_getenv ("SHELL"))){
1073 if (access (shell
, X_OK
) == 0) {
1074 return g_strdup (shell
);
1078 pw
= getpwuid(getuid());
1079 if (pw
&& pw
->pw_shell
) {
1080 if (access (pw
->pw_shell
, X_OK
) == 0) {
1081 return g_strdup (pw
->pw_shell
);
1085 for (i
= 0; i
!= G_N_ELEMENTS (shells
); i
++) {
1086 if (access (shells
[i
], X_OK
) == 0) {
1087 return g_strdup (shells
[i
]);
1091 /* If /bin/sh doesn't exist, your system is truly broken. */
1094 /* Placate compiler. */
1097 /* g_find_program_in_path() always looks also in the Windows
1098 * and System32 directories, so it should always find either cmd.exe
1101 gchar
*retval
= g_find_program_in_path ("cmd.exe");
1104 retval
= g_find_program_in_path ("command.com");
1106 g_assert (retval
!= NULL
);
1113 * anjuta_util_user_terminal:
1115 * Retrieves the user's preferred terminal.
1117 * Returns: A newly allocated strings list. The first argument is the terminal
1118 * program name. The following are the arguments needed to execute
1119 * a command. The list has to be freed with g_strfreev
1121 /* copied from deprecated gnome_execute_terminal in libgnome */
1123 anjuta_util_user_terminal (void)
1125 /* FIXME: GSettings */
1127 GConfClient
*client
;
1128 gchar
*terminal
= NULL
;
1129 gchar
**argv
= NULL
;
1130 static const gchar
*terms
[] = {
1142 client
= gconf_client_get_default ();
1143 terminal
= gconf_client_get_string (client
, "/desktop/gnome/applications/terminal/exec", NULL
);
1144 g_object_unref (client
);
1148 gchar
*command_line
;
1151 exec_flag
= gconf_client_get_string (client
, "/desktop/gnome/applications/terminal/exec_arg", NULL
);
1152 command_line
= g_strconcat (terminal
, " ", exec_flag
, NULL
);
1154 g_shell_parse_argv (command_line
, NULL
, &argv
, NULL
);
1162 /* Search for common ones */
1163 for (term
= terms
; *term
!= NULL
; term
++)
1165 terminal
= g_find_program_in_path (*term
);
1166 if (terminal
!= NULL
) break;
1170 g_warning (_("Cannot find a terminal; using "
1171 "xterm, even if it may not work"));
1172 terminal
= g_strdup ("xterm");
1174 argv
= g_new0 (char *, 3);
1176 /* Note that gnome-terminal takes -x and
1177 * as -e in gnome-terminal is broken we use that. */
1178 argv
[1] = g_strdup (term
== &terms
[2] ? "-x" : "-e");
1182 g_warning ("anjuta_util_user_terminal: Not implemented");
1188 anjuta_util_execute_shell (const gchar
*dir
, const gchar
*command
)
1193 g_return_val_if_fail (command
!= NULL
, -1);
1195 shell
= anjuta_util_user_shell ();
1201 anjuta_util_create_dir (dir
);
1204 execlp (shell
, shell
, "-c", command
, NULL
);
1205 g_warning (_("Cannot execute command: %s (using shell %s)\n"), command
, shell
);
1209 g_warning (_("Cannot execute command: %s (using shell %s)\n"), command
, shell
);
1212 // Anjuta will take care of child exit automatically.
1217 anjuta_util_execute_terminal_shell (const gchar
*dir
, const gchar
*command
)
1223 g_return_val_if_fail (command
!= NULL
, -1);
1225 shell
= anjuta_util_user_shell ();
1226 term_argv
= anjuta_util_user_terminal ();
1232 anjuta_util_create_dir (dir
);
1235 execlp (term_argv
[0], term_argv
[0], term_argv
[1], shell
, "-c", command
, NULL
);
1236 g_warning (_("Cannot execute command: %s (using shell %s)\n"), command
, shell
);
1240 g_warning (_("Cannot execute command: %s (using shell %s)\n"), command
, shell
);
1242 g_strfreev (term_argv
);
1244 // Anjuta will take care of child exit automatically.
1249 anjuta_util_convert_to_utf8 (const gchar
*str
)
1251 GError
*error
= NULL
;
1252 gchar
*utf8_msg_string
= NULL
;
1254 g_return_val_if_fail (str
!= NULL
, NULL
);
1255 g_return_val_if_fail (strlen (str
) > 0, NULL
);
1257 if (g_utf8_validate(str
, -1, NULL
))
1259 utf8_msg_string
= g_strdup (str
);
1263 gsize rbytes
, wbytes
;
1264 utf8_msg_string
= g_locale_to_utf8 (str
, -1, &rbytes
, &wbytes
, &error
);
1265 if (error
!= NULL
) {
1266 g_warning ("g_locale_to_utf8 failed: %s\n", error
->message
);
1267 g_error_free (error
);
1268 /* g_free (utf8_msg_string);
1272 return utf8_msg_string
;
1275 #define LEFT_BRACE(ch) (ch == ')'? '(' : (ch == '}'? '{' : (ch == ']'? '[' : ch)))
1278 anjuta_util_jump_to_matching_brace (IAnjutaIterable
*iter
, gchar brace
, gint limit
)
1280 gchar point_ch
= brace
;
1281 gint cur_iteration
= 0;
1282 gboolean use_limit
= (limit
> 0);
1283 GString
*braces_stack
= g_string_new ("");
1285 g_return_val_if_fail (point_ch
== ')' || point_ch
== ']' ||
1286 point_ch
== '}', FALSE
);
1288 /* DEBUG_PRINT ("%s", "Matching brace being"); */
1290 g_string_prepend_c (braces_stack
, point_ch
);
1292 while (ianjuta_iterable_previous (iter
, NULL
))
1296 if (use_limit
&& cur_iteration
> limit
)
1299 /* Skip comments and strings */
1300 IAnjutaEditorAttribute attrib
=
1301 ianjuta_editor_cell_get_attribute (IANJUTA_EDITOR_CELL (iter
), NULL
);
1302 if (attrib
== IANJUTA_EDITOR_COMMENT
|| attrib
== IANJUTA_EDITOR_STRING
)
1305 /* DEBUG_PRINT ("%s", "point ch = %c", point_ch); */
1306 point_ch
= ianjuta_editor_cell_get_char (IANJUTA_EDITOR_CELL (iter
), 0,
1308 if (point_ch
== ')' || point_ch
== ']' || point_ch
== '}')
1311 g_string_prepend_c (braces_stack
, point_ch
);
1314 if (point_ch
== LEFT_BRACE (braces_stack
->str
[0]))
1317 g_string_erase (braces_stack
, 0, 1);
1319 /* Bail out if there is no more in stack */
1320 if (braces_stack
->str
[0] == '\0')
1322 /* DEBUG_PRINT ("%s", "Matching brace end -- found"); */
1326 /* DEBUG_PRINT ("%s", "Matching brace end -- not found"); */
1331 anjuta_util_parse_args_from_string (const gchar
* string
)
1335 gboolean is_quote
= FALSE
;
1336 gchar
* buffer
= g_new0(gchar
, strlen(string
) + 1);
1353 /* The current char was escaped */
1356 } else if (*s
== '\\') {
1357 /* Current char is an escape */
1359 } else if (is_quote
&& *s
== quote
) {
1360 /* Current char ends a quotation */
1362 if (!isspace(*(s
+1)) && (*(s
+1) != '\0')) {
1363 /* If there is no space after the quotation or it is not
1364 the end of the string */
1365 g_warning ("Parse error while parsing program arguments");
1367 } else if ((*s
== '\"' || *s
== '\'')) {
1369 /* Current char starts a quotation */
1373 /* Just a quote char inside quote */
1376 } else if (is_quote
){
1377 /* Any other char inside quote */
1379 } else if (isspace(*s
)) {
1380 /* Any white space outside quote */
1382 buffer
[idx
++] = '\0';
1383 args
= g_list_append (args
, g_strdup (buffer
));
1392 /* There are chars in the buffer. Flush as the last arg */
1393 buffer
[idx
++] = '\0';
1394 args
= g_list_append (args
, g_strdup (buffer
));
1398 g_warning ("Unclosed quotation encountered at the end of parsing");
1405 anjuta_util_escape_quotes(const gchar
* str
)
1409 const gchar
*s
= str
;
1411 g_return_val_if_fail(str
, NULL
);
1414 /* We are assuming there will be less than 2048 chars to escape */
1415 max_size
= strlen(str
) + 2048;
1416 buffer
= g_new (gchar
, max_size
);
1422 if (*s
== '\"' || *s
== '\'' || *s
== '\\')
1423 buffer
[idx
++] = '\\';
1431 /* Diff the text contained in uri with text. Return true if files
1432 differ, FALSE if they are identical.*/
1434 gboolean
anjuta_util_diff(const gchar
* uri
, const gchar
* text
)
1437 GFileInfo
*file_info
;
1439 gchar
* file_text
= NULL
;
1442 file
= g_file_new_for_uri (uri
);
1443 file_info
= g_file_query_info (file
,
1444 G_FILE_ATTRIBUTE_STANDARD_SIZE
,
1445 G_FILE_QUERY_INFO_NONE
,
1449 if (file_info
== NULL
)
1451 g_object_unref (file
);
1455 size
= g_file_info_get_attribute_uint64(file_info
,
1456 G_FILE_ATTRIBUTE_STANDARD_SIZE
);
1457 g_object_unref (file_info
);
1459 if (size
== 0 && text
== NULL
)
1461 g_object_unref (file
);
1464 else if (size
== 0 || text
== NULL
)
1466 g_object_unref (file
);
1470 if (!g_file_load_contents(file
,
1477 g_object_unref (file
);
1480 g_object_unref (file
);
1482 if (bytes_read
!= size
)
1488 /* according to g_file_load_contents's documentation
1489 * file_text is guaranteed to end with \0.
1491 if (strcmp (file_text
, text
) == 0)
1502 * anjuta_util_is_project_file:
1503 * @filename: the file name
1505 * Return TRUE if the file is an anjuta project file. It is implemented by
1506 * checking only the file extension. So it does not check the existence
1507 * of the file. But it is working on an URI if it does not containt a
1510 * Returns: TRUE if the file is a project file, else FALSE
1513 anjuta_util_is_project_file (const gchar
*filename
)
1515 gsize len
= strlen (filename
);
1516 return ((len
> 8) && (strcmp (filename
+ len
- 7, ".anjuta") == 0));
1520 * anjuta_util_is_template_file:
1521 * @filename: the file name
1523 * Return TRUE if the file is an template 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 template file, else FALSE
1531 anjuta_util_is_template_file (const gchar
*filename
)
1533 gsize len
= strlen (filename
);
1534 return ((len
> 9) && (strcmp (filename
+ len
- 8, ".wiz.tgz") == 0));
1538 * anjuta_util_get_file_mine_type:
1541 * Check if a file exists and return its mime type.
1543 * Returns: NULL if the corresponding file doesn't exist or the mime type as a newly
1544 * allocated string that must be freed with g_free().
1547 anjuta_util_get_file_mime_type (GFile
*file
)
1550 gchar
*mime_type
= NULL
;
1552 g_return_val_if_fail (file
!= NULL
, NULL
);
1554 /* Get file information, check that the file exist at the same time */
1555 info
= g_file_query_info (file
,
1556 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
,
1557 G_FILE_QUERY_INFO_NONE
,
1563 const gchar
*extension
;
1566 /* If Anjuta is not installed in system gnome prefix, the mime types
1567 * may not have been correctly registed. In that case, we use the
1568 * following mime detection
1570 name
= g_file_get_basename (file
);
1571 extension
= strrchr(name
, '.');
1572 if (extension
!= NULL
)
1574 const static struct {gchar
*extension
; gchar
*type
;} anjuta_types
[] = {
1575 {"anjuta", "application/x-anjuta"},
1576 {"prj", "application/x-anjuta-old"},
1580 for (i
= 0; anjuta_types
[i
].extension
!= NULL
; i
++)
1582 if (strcmp(extension
+ 1, anjuta_types
[i
].extension
) == 0)
1584 mime_type
= g_strdup (anjuta_types
[i
].type
);
1591 /* Use mime database if it is not an Anjuta type */
1592 if (mime_type
== NULL
)
1594 mime_type
= g_content_type_get_mime_type (g_file_info_get_content_type(info
));
1597 g_object_unref (info
);
1604 anjuta_util_get_local_path_from_uri (const gchar
*uri
)
1609 file
= g_file_new_for_uri (uri
);
1610 local_path
= g_file_get_path (file
);
1611 g_object_unref (file
);
1616 #ifdef EMULATE_FORKPTY
1619 static int ptym_open (char *pts_name
);
1620 static int ptys_open (int fdm
, char * pts_name
);
1623 login_tty(int ttyfd
)
1635 /* First disconnect from the old controlling tty. */
1637 fd
= open("/dev/tty", O_RDWR
|O_NOCTTY
);
1640 ioctl(fd
, TIOCNOTTY
, NULL
);
1644 //syslog(LOG_WARNING, "NO CTTY");
1645 #endif /* TIOCNOTTY */
1647 /* Verify that we are successfully disconnected from the controlling tty. */
1648 fd
= open("/dev/tty", O_RDWR
|O_NOCTTY
);
1651 //syslog(LOG_WARNING, "Failed to disconnect from controlling tty.");
1655 /* Make it our controlling tty. */
1657 ioctl(ttyfd
, TIOCSCTTY
, NULL
);
1658 #endif /* TIOCSCTTY */
1660 fdname
= ttyname (ttyfd
);
1661 fd
= open(fdname
, O_RDWR
);
1663 ;//syslog(LOG_WARNING, "open %s: %s", fdname, strerror(errno));
1667 /* Verify that we now have a controlling tty. */
1668 fd
= open("/dev/tty", O_WRONLY
);
1671 //syslog(LOG_WARNING, "open /dev/tty: %s", strerror(errno));
1676 #if defined(HAVE_VHANGUP) && !defined(HAVE_REVOKE)
1678 RETSIGTYPE (*sig
)();
1679 sig
= signal(SIGHUP
, SIG_IGN
);
1681 signal(SIGHUP
, sig
);
1684 fd
= open(fdname
, O_RDWR
);
1687 //syslog(LOG_ERR, "can't reopen ctty %s: %s", fdname, strerror(errno));
1709 openpty(int *amaster
, int *aslave
, char *name
, struct termios
*termp
,
1710 struct winsize
*winp
)
1713 *amaster
= ptym_open(line
);
1716 *aslave
= ptys_open(*amaster
, line
);
1724 #define TCSAFLUSH TCSETAF
1727 (void) tcsetattr(*aslave
, TCSAFLUSH
, termp
);
1730 (void) ioctl(*aslave
, TIOCSWINSZ
, (char *)winp
);
1736 ptym_open(char * pts_name
)
1742 strcpy(pts_name
, "/dev/ptmx");
1743 fdm
= open(pts_name
, O_RDWR
);
1746 if (grantpt(fdm
) < 0) { /* grant access to slave */
1750 if (unlockpt(fdm
) < 0) { /* clear slave's lock flag */
1755 if (ptr
== NULL
) { /* get slave's name */
1759 strcpy(pts_name
, ptr
); /* return name of slave */
1760 return fdm
; /* return fd of master */
1764 strcpy(pts_name
, "/dev/ptyXY");
1765 /* array index: 012345689 (for references in following code) */
1766 for (ptr1
= "pqrstuvwxyzPQRST"; *ptr1
!= 0; ptr1
++) {
1767 pts_name
[8] = *ptr1
;
1768 for (ptr2
= "0123456789abcdef"; *ptr2
!= 0; ptr2
++) {
1769 pts_name
[9] = *ptr2
;
1770 /* try to open master */
1771 fdm
= open(pts_name
, O_RDWR
);
1773 if (errno
== ENOENT
) /* different from EIO */
1774 return -1; /* out of pty devices */
1776 continue; /* try next pty device */
1778 pts_name
[5] = 't'; /* chage "pty" to "tty" */
1779 return fdm
; /* got it, return fd of master */
1782 return -1; /* out of pty devices */
1787 ptys_open(int fdm
, char * pts_name
)
1791 /* following should allocate controlling terminal */
1792 fds
= open(pts_name
, O_RDWR
);
1797 if (ioctl(fds
, I_PUSH
, "ptem") < 0) {
1802 if (ioctl(fds
, I_PUSH
, "ldterm") < 0) {
1807 if (ioctl(fds
, I_PUSH
, "ttcompat") < 0) {
1813 if (ioctl(fdm
, I_PUSH
, "pckt") < 0) {
1819 if (ioctl(fdm
, I_SRDOPT
, RMSGN
|RPROTDAT
) < 0) {
1828 struct group
*grptr
;
1830 grptr
= getgrnam("tty");
1832 gid
= grptr
->gr_gid
;
1834 gid
= -1; /* group tty is not in the group file */
1835 /* following two functions don't work unless we're root */
1836 chown(pts_name
, getuid(), gid
);
1837 chmod(pts_name
, S_IRUSR
| S_IWUSR
| S_IWGRP
);
1838 fds
= open(pts_name
, O_RDWR
);
1848 forkpty(int *amaster
, char *name
, struct termios
*termp
, struct winsize
*winp
)
1850 int master
, slave
, pid
;
1852 if (openpty(&master
, &slave
, name
, termp
, winp
) == -1)
1854 switch (pid
= fork()) {
1873 int scandir(const char *dir
, struct dirent
***namelist
,
1874 int (*select
)(const struct dirent
*),
1875 int (*compar
)(const struct dirent
**, const struct dirent
**))
1878 struct dirent
*entry
;
1882 if ((d
=opendir(dir
)) == NULL
)
1886 while ((entry
=readdir(d
)) != NULL
)
1888 if (select
== NULL
|| (select
!= NULL
&& (*select
)(entry
)))
1890 *namelist
=(struct dirent
**)realloc((void *)(*namelist
),
1891 (size_t)((i
+1)*sizeof(struct dirent
*)));
1892 if (*namelist
== NULL
) return(-1);
1893 entrysize
=sizeof(struct dirent
)-sizeof(entry
->d_name
)+strlen(entry
->d_name
)+1;
1894 (*namelist
)[i
]=(struct dirent
*)malloc(entrysize
);
1895 if ((*namelist
)[i
] == NULL
) return(-1);
1896 memcpy((*namelist
)[i
], entry
, entrysize
);
1900 if (closedir(d
)) return(-1);
1901 if (i
== 0) return(-1);
1903 qsort((void *)(*namelist
), (size_t)i
, sizeof(struct dirent
*), compar
);
1908 #endif /* EMULATE_FORKPTY */
1911 anjuta_util_help_display (GtkWidget
*parent
,
1912 const gchar
*doc_id
,
1915 GError
*error
= NULL
;
1919 command
= g_strdup_printf ("yelp help:%s%s%s",
1921 item
== NULL
? "" : "/",
1922 item
== NULL
? "" : item
);
1924 if (!g_spawn_command_line_async (command
, &error
) &&
1927 g_warning ("Error executing help application: %s",
1929 g_error_free (error
);
1935 /* The following functions are taken from gedit */
1937 /* Note that this function replace home dir with ~ */
1939 anjuta_util_uri_get_dirname (const gchar
*uri
)
1944 // CHECK: does it work with uri chaining? - Paolo
1945 str
= g_path_get_dirname (uri
);
1946 g_return_val_if_fail (str
!= NULL
, ".");
1948 if ((strlen (str
) == 1) && (*str
== '.'))
1955 res
= anjuta_util_replace_home_dir_with_tilde (str
);
1963 anjuta_util_replace_home_dir_with_tilde (const gchar
*uri
)
1968 g_return_val_if_fail (uri
!= NULL
, NULL
);
1970 /* Note that g_get_home_dir returns a const string */
1971 tmp
= (gchar
*)g_get_home_dir ();
1974 return g_strdup (uri
);
1976 home
= g_filename_to_utf8 (tmp
, -1, NULL
, NULL
, NULL
);
1978 return g_strdup (uri
);
1980 if (strcmp (uri
, home
) == 0)
1984 return g_strdup ("~");
1988 home
= g_strdup_printf ("%s/", tmp
);
1991 if (g_str_has_prefix (uri
, home
))
1995 res
= g_strdup_printf ("~/%s", uri
+ strlen (home
));
2004 return g_strdup (uri
);
2008 * anjuta_util_shell_expand:
2009 * @string: input string
2011 * Expand environment variables $(var_name) and tilde (~) in the input string.
2013 * Returns: a newly-allocated string that must be freed with g_free().
2016 anjuta_util_shell_expand (const gchar
*string
)
2020 if (string
== NULL
) return NULL
;
2022 expand
= g_string_sized_new (strlen (string
));
2024 for (; *string
!= '\0'; string
++)
2030 /* Variable expansion */
2035 while (isalnum (*end
) || (*end
== '_')) end
++;
2036 var_name_len
= end
- string
- 1;
2037 if (var_name_len
> 0)
2041 g_string_append_len (expand
, string
+ 1, var_name_len
);
2042 value
= g_getenv (expand
->str
+ expand
->len
- var_name_len
);
2043 g_string_truncate (expand
, expand
->len
- var_name_len
);
2044 g_string_append (expand
, value
);
2052 /* User home directory expansion */
2053 if (isspace(string
[1]) || (string
[1] == G_DIR_SEPARATOR
) || (string
[1] == '\0'))
2055 g_string_append (expand
, g_get_home_dir());
2063 g_string_append_c (expand
, *string
);
2066 return g_string_free (expand
, FALSE
);
2070 anjuta_util_str_middle_truncate (const gchar
*string
,
2071 guint truncate_length
)
2076 guint num_left_chars
;
2078 guint delimiter_length
;
2079 const gchar
*delimiter
= "\342\200\246";
2081 g_return_val_if_fail (string
!= NULL
, NULL
);
2083 length
= strlen (string
);
2085 g_return_val_if_fail (g_utf8_validate (string
, length
, NULL
), NULL
);
2087 /* It doesnt make sense to truncate strings to less than
2088 * the size of the delimiter plus 2 characters (one on each
2091 delimiter_length
= g_utf8_strlen (delimiter
, -1);
2092 if (truncate_length
< (delimiter_length
+ 2)) {
2093 return g_strdup (string
);
2096 n_chars
= g_utf8_strlen (string
, length
);
2098 /* Make sure the string is not already small enough. */
2099 if (n_chars
<= truncate_length
) {
2100 return g_strdup (string
);
2103 /* Find the 'middle' where the truncation will occur. */
2104 num_left_chars
= (truncate_length
- delimiter_length
) / 2;
2105 right_offset
= n_chars
- truncate_length
+ num_left_chars
+ delimiter_length
;
2107 truncated
= g_string_new_len (string
,
2108 g_utf8_offset_to_pointer (string
, num_left_chars
) - string
);
2109 g_string_append (truncated
, delimiter
);
2110 g_string_append (truncated
, g_utf8_offset_to_pointer (string
, right_offset
));
2112 return g_string_free (truncated
, FALSE
);
2116 * Functions to implement XDG Base Directory Specification
2117 * http://standards.freedesktop.org/basedir-spec/latest/index.html
2118 * Use this to save any config/cache/data files
2123 anjuta_util_construct_pathv (const gchar
* str
, va_list str_list
)
2126 const gchar
* tmp_str
;
2129 str_arr
= g_ptr_array_new();
2130 g_ptr_array_add (str_arr
, (gpointer
) str
);
2132 /* Extract elements from va_list */
2135 while ((tmp_str
= va_arg (str_list
, const gchar
*)) != NULL
)
2137 g_ptr_array_add (str_arr
, (gpointer
)tmp_str
);
2142 /* Terminate the list */
2143 g_ptr_array_add (str_arr
, NULL
);
2145 path
= g_build_filenamev ((gchar
**)str_arr
->pdata
);
2146 g_ptr_array_free (str_arr
, TRUE
);
2152 anjuta_util_get_user_cache_filev (const gchar
* path
, va_list list
)
2154 gchar
*uri_str
, *base_path
, *dir
;
2156 const gchar anjuta_prefix
[] = "anjuta";
2157 base_path
= g_build_filename (g_get_user_cache_dir(), anjuta_prefix
, path
, NULL
);
2159 uri_str
= anjuta_util_construct_pathv (base_path
, list
);
2162 uri
= g_file_new_for_path (uri_str
);
2163 dir
= g_path_get_dirname (uri_str
);
2165 if (!anjuta_util_create_dir (dir
)) return NULL
;
2171 anjuta_util_get_user_cache_file (const gchar
* path
, ...)
2174 va_start (list
, path
);
2175 return anjuta_util_get_user_cache_filev (path
, list
);
2179 anjuta_util_get_user_config_filev (const gchar
* path
, va_list list
)
2181 gchar
*uri_str
, *base_path
, *dir
;
2183 const gchar anjuta_prefix
[] = "anjuta";
2184 base_path
= g_build_filename (g_get_user_config_dir(), anjuta_prefix
, path
, NULL
);
2186 uri_str
= anjuta_util_construct_pathv (base_path
, list
);
2189 uri
= g_file_new_for_path (uri_str
);
2190 dir
= g_path_get_dirname (uri_str
);
2192 if (!anjuta_util_create_dir (dir
)) return NULL
;
2198 anjuta_util_get_user_config_file (const gchar
* path
, ...)
2201 va_start (list
, path
);
2202 return anjuta_util_get_user_config_filev (path
, list
);
2206 anjuta_util_get_user_data_filev (const gchar
* path
, va_list list
)
2208 gchar
*uri_str
, *base_path
, *dir
;
2210 const gchar anjuta_prefix
[] = "anjuta";
2211 base_path
= g_build_filename (g_get_user_data_dir(), anjuta_prefix
, path
, NULL
);
2213 uri_str
= anjuta_util_construct_pathv (base_path
, list
);
2216 uri
= g_file_new_for_path (uri_str
);
2217 dir
= g_path_get_dirname (uri_str
);
2219 if (!anjuta_util_create_dir (dir
)) return NULL
;
2225 anjuta_util_get_user_data_file (const gchar
* path
, ...)
2228 va_start (list
, path
);
2229 return anjuta_util_get_user_data_filev (path
, list
);
2233 anjuta_util_get_user_cache_file_path (const gchar
* path
, ...)
2238 va_start (list
, path
);
2239 file
= anjuta_util_get_user_cache_filev (path
, list
);
2240 file_path
= g_file_get_path (file
);
2241 g_object_unref (file
);
2247 anjuta_util_get_user_config_file_path (const gchar
* path
, ...)
2252 va_start (list
, path
);
2253 file
= anjuta_util_get_user_config_filev (path
, list
);
2254 file_path
= g_file_get_path (file
);
2255 g_object_unref (file
);
2261 anjuta_util_get_user_data_file_path (const gchar
* path
, ...)
2266 va_start (list
, path
);
2267 file
= anjuta_util_get_user_data_filev (path
, list
);
2268 file_path
= g_file_get_path (file
);
2269 g_object_unref (file
);
2275 anjuta_util_convert_gfile_list_to_path_list (GList
*list
)
2278 GList
*current_file
;
2283 for (current_file
= list
; current_file
!= NULL
; current_file
= g_list_next (current_file
))
2285 path
= g_file_get_path (current_file
->data
);
2287 /* Ignore files with invalid paths */
2289 path_list
= g_list_append (path_list
, path
);
2296 anjuta_util_convert_gfile_list_to_relative_path_list (GList
*list
,
2297 const gchar
*parent
)
2301 GList
*current_file
;
2304 parent_file
= g_file_new_for_path (parent
);
2309 for (current_file
= list
; current_file
!= NULL
; current_file
= g_list_next (current_file
))
2311 path
= g_file_get_relative_path (parent_file
, current_file
->data
);
2313 /* Ignore files with invalid paths */
2315 path_list
= g_list_append (path_list
, path
);
2318 g_object_unref (parent_file
);
2326 * anjuta_util_builder_new:
2327 * @filename: Builder file name to open
2328 * @error: Optional error object, if NULL display a dialog if the file is missing
2330 * Create a new GtkBuilder object and load the file in it. Display an error
2331 * if the file is missing. Use a dialog if error is NULL, just a warning
2332 * if the error can be reported.
2334 * Returns: The new GtkBuilder object
2337 anjuta_util_builder_new (const gchar
*filename
, GError
**error
)
2339 GtkBuilder
*bxml
= gtk_builder_new ();
2342 /* Load glade file */
2343 if (!gtk_builder_add_from_file (bxml
, filename
, &err
))
2345 g_object_unref (bxml
);
2348 /* Display the error to the user if it cannot be reported to the caller */
2351 anjuta_util_dialog_error (NULL
, _("Unable to load user interface file: %s"), err
->message
);
2355 g_warning ("Couldn't load builder file: %s", err
->message
);
2357 g_propagate_error (error
, err
);
2360 /* Tag the builder object with the filename to allow better error message
2361 * with the following function */
2364 g_object_set_data_full (G_OBJECT (bxml
), "filename", g_strdup (filename
), g_free
);
2371 * anjuta_util_builder_get_objects:
2372 * @builder: Builder object
2373 * @first_widget: Name of first widget to get
2374 * ...: Address to store the first widget pointer, followed optionally by
2375 * more name/pointer pairs, followed by NULL
2377 * Create a new GtkBuilder object and load the file in it. Display an error
2378 * if the file is missing. Use a dialog if error is NULL, just a warning
2379 * if the error can be reported.
2381 * Returns: TRUE is everything works as expected.
2384 anjuta_util_builder_get_objects (GtkBuilder
*builder
, const gchar
*first_widget
,...)
2388 GObject
**object_ptr
;
2389 gboolean missing
= FALSE
;
2391 va_start (args
, first_widget
);
2393 for (name
= first_widget
; name
; name
= va_arg (args
, char *))
2395 object_ptr
= va_arg (args
, void *);
2396 *object_ptr
= gtk_builder_get_object (builder
, name
);
2398 /* Object not found, display a warning */
2401 const gchar
*filename
= (const gchar
*)g_object_get_data (G_OBJECT (builder
), "filename");
2404 g_warning ("Missing widget '%s' in file %s", name
, filename
);
2408 g_warning("Missing widget '%s'", name
);
2419 * anjuta_utils_drop_get_files:
2420 * @selection_data: the #GtkSelectionData from drag_data_received
2422 * Create a list of valid uri's from a uri-list drop.
2424 * Return value: (element-type GFile*): a list of GFiles
2427 anjuta_utils_drop_get_files (GtkSelectionData
*selection_data
)
2431 GSList
* files
= NULL
;
2433 uris
= g_uri_list_extract_uris ((gchar
*) gtk_selection_data_get_data (selection_data
));
2435 for (i
= 0; uris
[i
] != NULL
; i
++)
2437 GFile
* file
= g_file_new_for_uri (uris
[i
]);
2438 files
= g_slist_append(files
, file
);
2445 * anjuta_util_get_user_mail:
2447 * Returns: The e-mail Address of the logged-in user. The resulting string
2448 * must be free'd after use.
2451 anjuta_util_get_user_mail()
2453 /* FIXME: Use libfolks or something like it to query the mail address */
2454 return g_strconcat(g_get_user_name (), "@", g_get_host_name (), NULL
);
2458 * anjuta_utils_clone_string_gptrarray:
2459 * @source: The source GPtrArray containing items representing strings
2461 * Clones the contents of source GPtrArray into a new allocated GPtrArray.
2463 * Return a new allocated GPtrArray with strings g_strdup (), NULL on error.
2464 * The returned array has set g_free as GDestroyNotity function, so that user
2465 * should only care to g_ptr_array_unref () without freeing the strings.
2468 anjuta_util_clone_string_gptrarray (const GPtrArray
* source
)
2473 g_return_val_if_fail (source
!= NULL
, NULL
);
2475 dest
= g_ptr_array_sized_new (source
->len
);
2476 g_ptr_array_set_free_func (dest
, g_free
);
2478 for (i
= 0; i
< source
->len
; i
++)
2480 g_ptr_array_add (dest
, g_strdup (g_ptr_array_index (source
, i
)));
2487 anjuta_util_list_all_dir_children (GList
**children
, GFile
*dir
)
2489 GFileEnumerator
*list
;
2491 list
= g_file_enumerate_children (dir
,
2492 G_FILE_ATTRIBUTE_STANDARD_NAME
,
2493 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
2501 while ((info
= g_file_enumerator_next_file (list
, NULL
, NULL
)) != NULL
)
2506 name
= g_file_info_get_name (info
);
2507 file
= g_file_get_child (dir
, name
);
2508 g_object_unref (info
);
2510 if (g_file_query_file_type (file
, G_FILE_QUERY_INFO_NONE
, NULL
) == G_FILE_TYPE_DIRECTORY
)
2512 anjuta_util_list_all_dir_children (children
, file
);
2513 g_object_unref (file
);
2517 *children
= g_list_prepend (*children
, file
);
2520 g_file_enumerator_close (list
, NULL
, NULL
);
2521 g_object_unref (list
);
2526 anjuta_util_convert_string_list_to_array (GList
*list
)
2531 g_return_val_if_fail (list
!= NULL
, NULL
);
2533 res
= g_ptr_array_new_with_free_func (g_free
);
2536 while (node
!= NULL
)
2538 g_ptr_array_add (res
, g_strdup (node
->data
));
2540 node
= g_list_next (node
);