summaryrefslogtreecommitdiff
path: root/app/wlib/gtklib/opendocument.c
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff-webhosting.net>2020-08-24 21:26:53 +0200
committerJörg Frings-Fürst <debian@jff-webhosting.net>2020-08-24 21:26:53 +0200
commitdf247efec654e512242e4f4f1b0212034f9e01fe (patch)
tree25c02e16957f3aa613af30c140fd8e8a3d52fda6 /app/wlib/gtklib/opendocument.c
parentd0b6a8a4ec298024f14f704f9e40a6f9d324ccf3 (diff)
parenta5ade52caa489cf0a713e0f02b764000d203140e (diff)
Merge branch 'release/debian/1%5.2.0Beta2.1-1' into masterHEADdebian/1%5.2.0Beta2.1-1master
Diffstat (limited to 'app/wlib/gtklib/opendocument.c')
-rw-r--r--app/wlib/gtklib/opendocument.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/app/wlib/gtklib/opendocument.c b/app/wlib/gtklib/opendocument.c
new file mode 100644
index 0000000..c03f9cb
--- /dev/null
+++ b/app/wlib/gtklib/opendocument.c
@@ -0,0 +1,117 @@
+/** \file opendocument.c
+ * open a document using the systems default application for that doc
+ */
+
+/* XTrkCad - Model Railroad CAD
+ * Copyright (C) 2018 Martin Fischer
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+#include "gtkint.h"
+#include "i18n.h"
+
+#include "dynstring.h"
+
+#if defined (_WIN32)
+
+#define DEFAULTOPENCOMMAND "start"
+
+#endif
+
+#if defined(__APPLE__) && defined(__MACH__)
+
+#define DEFAULTOPENCOMMAND "open"
+
+#else
+
+#define DEFAULTOPENCOMMAND "xdg-open"
+
+#endif
+
+/**
+ * Extend the PATH variable in the environment to include XTrackCAD's
+ * script directory.
+ *
+ * \return pointer to old path
+ */
+
+static char *
+ExtendPath(void)
+{
+ char *path = strdup(getenv("PATH"));
+ DynString newPath;
+ DynStringMalloc(&newPath, 16);
+
+ // append XTrackCAD's directory to the path as a fallback
+ DynStringCatCStrs(&newPath,
+ path,
+ ":",
+ wGetAppLibDir(),
+ NULL);
+
+ setenv("PATH",
+ DynStringToCStr(&newPath),
+ TRUE);
+
+ DynStringFree(&newPath);
+
+ return (path);
+}
+
+/**
+ * Invoke the system's default application to open a file. First the
+ * system's standard xdg-open command is attempted. If that is not available, the
+ * version included with the XTrackCAD installation is executed.
+ *
+ * \param topic IN URI of document
+ */
+
+unsigned wOpenFileExternal(char * filename)
+{
+ int rc;
+ DynString commandLine;
+ char *currentPath;
+
+ assert(filename != NULL);
+ assert(strlen(filename));
+
+ currentPath = ExtendPath();
+
+ DynStringMalloc(&commandLine, 16);
+ DynStringCatCStrs(&commandLine,
+ DEFAULTOPENCOMMAND,
+ " \"",
+ filename,
+ "\"",
+ NULL);
+
+ // the command should be found via the PATH
+ rc = system(DynStringToCStr(&commandLine));
+
+ // restore the PATH
+ setenv("PATH",
+ currentPath,
+ TRUE);
+
+ free(currentPath);
+ DynStringFree(&commandLine);
+
+ return(rc==0);
+}