1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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);
}
|