Skip to content

Commit 6f70e08

Browse files
Bob-ITjralls
authored andcommitted
Add check for path being relative.
If the preference "assoc-head" is set and file path is a sub directory then only this part is saved to the transaction making the full path portable. Paths outside of "assoc-head" will be saved in full. There is also a test to make sure the URI passed to the open call has a valid scheme otherwise the windows version will crash.
1 parent 47953c9 commit 6f70e08

File tree

1 file changed

+66
-11
lines changed

1 file changed

+66
-11
lines changed

src/gnome/gnc-split-reg.c

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,9 @@ static void
902902
gsr_default_associate_handler_file (GNCSplitReg *gsr, Transaction *trans, gboolean have_uri)
903903
{
904904
GtkWidget *dialog;
905-
gint response;
905+
gint response;
906+
gboolean valid_path_head = FALSE;
907+
gchar *path_head = gnc_prefs_get_string (GNC_PREFS_GROUP_GENERAL, "assoc-head");
906908

907909
dialog = gtk_file_chooser_dialog_new (_("Associate File with Transaction"),
908910
GTK_WINDOW(gsr->window),
@@ -914,20 +916,34 @@ gsr_default_associate_handler_file (GNCSplitReg *gsr, Transaction *trans, gboole
914916

915917
gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER(dialog), FALSE);
916918

919+
if ((path_head != NULL) && (g_strcmp0 (path_head, "") != 0)) // not default entry
920+
valid_path_head = TRUE;
921+
917922
if (have_uri)
918923
{
919-
GtkWidget *extra_widget;
920-
gchar *uri_label = g_strconcat (_("Existing Association is "), xaccTransGetAssociation (trans), NULL);
924+
gchar *new_uri;
925+
gchar *uri_label;
926+
gchar *filename;
921927

922-
extra_widget = gtk_label_new (uri_label);
928+
const gchar *uri = xaccTransGetAssociation (trans);
923929

924-
gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER(dialog), extra_widget);
930+
if (valid_path_head && g_str_has_prefix (uri,"file:/") && !g_str_has_prefix (uri,"file://"))
931+
{
932+
const gchar *part = uri + strlen ("file:");
933+
new_uri = g_strconcat (path_head, part, NULL);
934+
}
935+
else
936+
new_uri = g_strdup (uri);
925937

926-
gtk_file_chooser_set_uri (GTK_FILE_CHOOSER(dialog), xaccTransGetAssociation (trans));
938+
filename = g_uri_unescape_string (new_uri, NULL);
939+
uri_label = g_strconcat (_("Existing Association is "), filename, NULL);
940+
gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER(dialog), gtk_label_new (uri_label));
941+
gtk_file_chooser_set_uri (GTK_FILE_CHOOSER(dialog), new_uri);
927942

928943
g_free (uri_label);
944+
g_free (new_uri);
945+
g_free (filename);
929946
}
930-
931947
response = gtk_dialog_run (GTK_DIALOG (dialog));
932948

933949
if (response == GTK_RESPONSE_REJECT)
@@ -936,10 +952,22 @@ gsr_default_associate_handler_file (GNCSplitReg *gsr, Transaction *trans, gboole
936952
if (response == GTK_RESPONSE_ACCEPT)
937953
{
938954
gchar *dialog_uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
939-
DEBUG("File URI: %s\n", dialog_uri);
940-
xaccTransSetAssociation (trans, dialog_uri);
955+
956+
PINFO("Dialog File URI: %s\n", dialog_uri);
957+
958+
if (valid_path_head && g_str_has_prefix (dialog_uri, path_head))
959+
{
960+
gchar *part = dialog_uri + strlen (path_head);
961+
gchar *new_uri = g_strconcat ("file:", part, NULL);
962+
xaccTransSetAssociation (trans, new_uri);
963+
g_free (new_uri);
964+
}
965+
else
966+
xaccTransSetAssociation (trans, dialog_uri);
967+
941968
g_free (dialog_uri);
942969
}
970+
g_free (path_head);
943971
gtk_widget_destroy (dialog);
944972
}
945973

@@ -984,9 +1012,9 @@ gsr_default_associate_handler_location (GNCSplitReg *gsr, Transaction *trans, gb
9841012
// set the default response
9851013
gtk_dialog_set_default_response (GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
9861014

987-
// run the dialog
9881015
gtk_widget_show_all (dialog);
9891016

1017+
// run the dialog
9901018
response = gtk_dialog_run (GTK_DIALOG (dialog));
9911019

9921020
if (response == GTK_RESPONSE_REJECT)
@@ -1062,6 +1090,8 @@ gsr_default_execassociated_handler (GNCSplitReg *gsr, gpointer data)
10621090
Split *split = gnc_split_register_get_current_split (reg);
10631091
GtkWidget *dialog;
10641092
const char *uri;
1093+
const char *run_uri;
1094+
gchar *uri_scheme;
10651095

10661096
/* get the current split based on cursor position */
10671097
if (split == NULL)
@@ -1082,11 +1112,36 @@ gsr_default_execassociated_handler (GNCSplitReg *gsr, gpointer data)
10821112
#endif
10831113

10841114
uri = xaccTransGetAssociation (trans);
1115+
10851116
if (g_strcmp0 (uri, "") == 0 && g_strcmp0 (uri, NULL) == 0)
10861117
gnc_error_dialog (NULL, "%s", _("This transaction is not associated with a URI."));
10871118
else
1088-
gnc_launch_assoc (uri);
1119+
{
1120+
if (g_str_has_prefix (uri,"file:/") && !g_str_has_prefix (uri,"file://")) // Check for relative path
1121+
{
1122+
gchar *path_head = gnc_prefs_get_string (GNC_PREFS_GROUP_GENERAL, "assoc-head");
1123+
1124+
if ((path_head != NULL) && (g_strcmp0 (path_head, "") != 0)) // not default entry
1125+
{
1126+
const gchar *part = uri + strlen ("file:");
1127+
run_uri = g_strconcat (path_head, part, NULL);
1128+
}
1129+
else
1130+
run_uri = g_strdup (uri);
1131+
}
1132+
else
1133+
run_uri = g_strdup (uri);
10891134

1135+
uri_scheme = g_uri_parse_scheme (run_uri);
1136+
1137+
if (uri_scheme != NULL) // make sure we have a scheme entry
1138+
{
1139+
gnc_launch_assoc (run_uri);
1140+
g_free (uri_scheme);
1141+
}
1142+
else
1143+
gnc_error_dialog (NULL, "%s", _("This transaction is not associated with a valid URI."));
1144+
}
10901145
return;
10911146
}
10921147

0 commit comments

Comments
 (0)