26 #include <glib/gi18n.h> 28 #include "dialog-doclink-utils.h" 30 #include "dialog-utils.h" 43 static QofLogModule log_module = GNC_MOD_GUI;
48 convert_uri_to_abs_path (
const gchar *path_head,
const gchar *uri,
49 gchar *uri_scheme, gboolean return_uri)
51 gchar *ret_value = NULL;
61 ret_value = g_strdup (file_path);
67 if (g_strcmp0 (uri_scheme,
"file") == 0)
70 ret_value = g_strdup (uri);
78 gnc_doclink_get_unescape_uri (
const gchar *path_head,
const gchar *uri, gchar *uri_scheme)
80 gchar *display_str = NULL;
85 gchar *file_path = convert_uri_to_abs_path (path_head, uri, uri_scheme, FALSE);
88 display_str = g_uri_unescape_string (file_path, NULL);
90 display_str = g_uri_unescape_string (uri, NULL);
94 #ifdef G_OS_WIN32 // make path look like a traditional windows path 95 g_strdelimit (display_str,
"/",
'\\');
98 DEBUG(
"Return display string is '%s'", display_str);
103 gnc_doclink_get_use_uri (
const gchar *path_head,
const gchar *uri, gchar *uri_scheme)
105 gchar *use_str = NULL;
110 gchar *file_path = convert_uri_to_abs_path (path_head, uri, uri_scheme, TRUE);
113 use_str = g_strdup (file_path);
115 use_str = g_strdup (uri);
119 DEBUG(
"Return use string is '%s'", use_str);
124 gnc_doclink_get_unescaped_just_uri (
const gchar *uri)
126 gchar *path_head = gnc_doclink_get_path_head ();
128 gchar *ret_uri = gnc_doclink_get_unescape_uri (path_head, uri, uri_scheme);
136 gnc_doclink_convert_trans_link_uri (gpointer trans, gboolean book_ro)
139 const gchar *part = NULL;
144 if (g_str_has_prefix (uri,
"file:") && !g_str_has_prefix (uri,
"file://"))
150 if (g_str_has_prefix (uri,
"file:/"))
151 part = uri + strlen (
"file:/");
152 else if (g_str_has_prefix (uri,
"file:"))
153 part = uri + strlen (
"file:");
158 return g_strdup (part);
160 return g_strdup (uri);
166 doclink_get_path_head_and_set (gboolean *path_head_set)
168 gchar *ret_path = NULL;
170 *path_head_set = FALSE;
172 if (path_head && *path_head)
174 *path_head_set = TRUE;
175 ret_path = g_strdup (path_head);
179 const gchar *doc = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
187 if (!g_str_has_suffix (ret_path,
"/"))
189 gchar *folder_with_slash = g_strconcat (ret_path,
"/", NULL);
191 ret_path = g_strdup (folder_with_slash);
192 g_free (folder_with_slash);
197 PINFO (
"Failed to save preference at %s, %s with %s",
198 GNC_PREFS_GROUP_GENERAL, GNC_DOC_LINK_PATH_HEAD, ret_path);
206 gnc_doclink_get_path_head (
void)
208 gboolean path_head_set = FALSE;
210 return doclink_get_path_head_and_set (&path_head_set);
214 gnc_doclink_set_path_head_label (GtkWidget *path_head_label,
const gchar *incoming_path_head,
const gchar *prefix)
216 gboolean path_head_set = FALSE;
217 gchar *path_head = NULL;
219 gchar *path_head_str;
220 gchar *path_head_text;
222 if (incoming_path_head)
224 path_head = g_strdup (incoming_path_head);
225 path_head_set = TRUE;
228 path_head = doclink_get_path_head_and_set (&path_head_set);
231 path_head_str = gnc_doclink_get_unescape_uri (NULL, path_head, scheme);
236 if (g_file_test (path_head_str, G_FILE_TEST_IS_DIR))
237 path_head_text = g_strdup_printf (
"%s '%s'", _(
"Path head for files is,"), path_head_str);
239 path_head_text = g_strdup_printf (
"%s '%s'", _(
"Path head does not exist,"), path_head_str);
242 path_head_text = g_strdup_printf (_(
"Path head not set, using '%s' for relative paths"), path_head_str);
246 gchar *tmp = g_strdup (path_head_text);
247 g_free (path_head_text);
249 path_head_text = g_strdup_printf (
"%s %s", prefix, tmp);
254 gtk_label_set_text (GTK_LABEL(path_head_label), path_head_text);
257 gnc_widget_style_context_add_class (GTK_WIDGET(path_head_label),
"gnc-class-highlight");
260 g_free (path_head_str);
261 g_free (path_head_text);
269 const gchar *old_path_head_uri;
271 const gchar *new_path_head_uri;
277 update_invoice_uri (
QofInstance* data, gpointer user_data)
280 GncInvoice *invoice = GNC_INVOICE(data);
281 const gchar* uri = gncInvoiceGetDocLink (invoice);
285 gboolean rel = FALSE;
292 if (rel && doclink_update->change_old)
294 gchar *new_uri = gnc_doclink_get_use_uri (doclink_update->old_path_head_uri, uri, scheme);
295 gncInvoiceSetDocLink (invoice, new_uri);
301 if (!rel && doclink_update->change_new && g_str_has_prefix (uri, doclink_update->new_path_head_uri))
304 const gchar *part = uri + strlen (doclink_update->new_path_head_uri);
305 gchar *new_uri = g_strdup (part);
307 gncInvoiceSetDocLink (invoice, new_uri);
314 update_trans_uri (
QofInstance* data, gpointer user_data)
317 Transaction *trans = GNC_TRANSACTION(data);
321 uri = gnc_doclink_convert_trans_link_uri (trans, doclink_update->book_ro);
325 gboolean rel = FALSE;
332 if (rel && doclink_update->change_old)
334 gchar *new_uri = gnc_doclink_get_use_uri (doclink_update->old_path_head_uri, uri, scheme);
344 if (!rel && doclink_update->change_new && g_str_has_prefix (uri, doclink_update->new_path_head_uri))
347 const gchar *part = uri + strlen (doclink_update->new_path_head_uri);
348 gchar *new_uri = g_strdup (part);
360 change_relative_and_absolute_uri_paths (
const gchar *old_path_head_uri, gboolean change_old,
361 const gchar *new_path_head_uri, gboolean change_new)
363 QofBook *book = gnc_get_current_book();
373 doclink_update->old_path_head_uri = old_path_head_uri;
374 doclink_update->new_path_head_uri = new_path_head_uri;
375 doclink_update->change_old = change_old;
376 doclink_update->change_new = change_new;
377 doclink_update->book_ro = book_ro;
381 update_trans_uri, doclink_update);
385 update_invoice_uri, doclink_update);
387 g_free (doclink_update);
391 gnc_doclink_pref_path_head_changed (GtkWindow *parent,
const gchar *old_path_head_uri)
395 GtkWidget *use_old_path_head, *use_new_path_head;
396 GtkWidget *old_head_label, *new_head_label;
398 gchar *new_path_head_uri = gnc_doclink_get_path_head ();
400 if (g_strcmp0 (old_path_head_uri, new_path_head_uri) == 0)
402 g_free (new_path_head_uri);
407 builder = gtk_builder_new();
408 gnc_builder_add_from_file (builder,
"dialog-doclink.glade",
"link_path_head_changed_dialog");
409 dialog = GTK_WIDGET(gtk_builder_get_object (builder,
"link_path_head_changed_dialog"));
412 gtk_window_set_transient_for (GTK_WINDOW(dialog), GTK_WINDOW(parent));
415 gtk_widget_set_name (GTK_WIDGET(dialog),
"gnc-id-doclink-change");
416 gnc_widget_style_context_add_class (GTK_WIDGET(dialog),
"gnc-class-doclink");
418 old_head_label = GTK_WIDGET(gtk_builder_get_object (builder,
"existing_path_head"));
419 new_head_label = GTK_WIDGET(gtk_builder_get_object (builder,
"new_path_head"));
421 use_old_path_head = GTK_WIDGET(gtk_builder_get_object (builder,
"use_old_path_head"));
422 use_new_path_head = GTK_WIDGET(gtk_builder_get_object (builder,
"use_new_path_head"));
425 gnc_doclink_set_path_head_label (old_head_label, old_path_head_uri, _(
"Existing"));
426 gnc_doclink_set_path_head_label (new_head_label, new_path_head_uri, _(
"New"));
428 gtk_widget_show (dialog);
429 g_object_unref (G_OBJECT(builder));
432 result = gtk_dialog_run (GTK_DIALOG(dialog));
433 if (result == GTK_RESPONSE_OK)
435 gboolean use_old = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(use_old_path_head));
436 gboolean use_new = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(use_new_path_head));
438 if (use_old || use_new)
439 change_relative_and_absolute_uri_paths (old_path_head_uri, use_old,
440 new_path_head_uri, use_new);
442 g_free (new_path_head_uri);
443 gtk_widget_destroy (dialog);
gchar * gnc_uri_get_scheme(const gchar *uri)
Extracts the scheme from a uri.
gchar * gnc_prefs_get_string(const gchar *group, const gchar *pref_name)
Get a string value from the preferences backend.
gchar * gnc_file_path_absolute(const gchar *prefix, const gchar *relative)
Given a prefix and a relative path, return the absolute path.
utility functions for the GnuCash UI
#define PINFO(format, args...)
Print an informational note.
const char * xaccTransGetReadOnly(Transaction *trans)
Returns a non-NULL value if this Transaction was marked as read-only with some specific "reason" text...
#define DEBUG(format, args...)
Print a debugging message.
gchar * gnc_uri_get_path(const gchar *uri)
Extracts the path part from a uri.
gboolean gnc_prefs_set_string(const gchar *group, const gchar *pref_name, const gchar *value)
Store a string into the preferences backend.
const gchar * gnc_userdata_dir(void)
Ensure that the user's configuration directory exists and is minimally populated. ...
const char * xaccTransGetDocLink(const Transaction *trans)
Gets the transaction Document Link.
Account handling public routines.
Gnome specific utility functions.
Generic api to store and retrieve preferences.
Business Invoice Interface.
gboolean qof_book_is_readonly(const QofBook *book)
Return whether the book is read only.
void xaccTransSetDocLink(Transaction *trans, const char *doclink)
Sets the transaction Document Link.
Utility functions for convert uri in separate components and back.
QofCollection * qof_book_get_collection(const QofBook *book, QofIdType entity_type)
Return The table of entities of the given type.
File path resolution utility functions.
API for Transactions and Splits (journal entries)
gchar * gnc_uri_create_uri(const gchar *scheme, const gchar *hostname, gint32 port, const gchar *username, const gchar *password, const gchar *path)
Composes a normalized uri starting from its separate components.