move at-exit messages about pool utilization to DEBUG_TRACE
[ardour.git] / libs / pbd / openuri.cc
1 /*
2  * Copyright (C) 2010-2015 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2012 Todd Naugle <toddn@harrisonconsoles.com>
4  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #ifdef WAF_BUILD
22 #include "libpbd-config.h"
23 #endif
24
25 #include <boost/scoped_ptr.hpp>
26 #include <string>
27 #include <glibmm/spawn.h>
28
29 #include "pbd/epa.h"
30 #include "pbd/openuri.h"
31
32 #ifdef __APPLE__
33 #include <curl/curl.h>
34         extern bool cocoa_open_url (const char*);
35 #endif
36
37 #ifdef PLATFORM_WINDOWS
38 # include <windows.h>
39 # include <shellapi.h>
40 #else
41 # include <sys/types.h>
42 # include <unistd.h>
43 #endif
44
45 bool
46 PBD::open_uri (const char* uri)
47 {
48 #ifdef PLATFORM_WINDOWS
49         gunichar2* wuri = g_utf8_to_utf16 (uri, -1, NULL, NULL, NULL);
50         ShellExecuteW(NULL, L"open", (LPCWSTR)wuri, NULL, NULL, SW_SHOWNORMAL);
51         g_free (wuri);
52         return true;
53 #elif __APPLE__
54         return cocoa_open_url (uri);
55 #else
56         EnvironmentalProtectionAgency* global_epa = EnvironmentalProtectionAgency::get_global_epa ();
57         boost::scoped_ptr<EnvironmentalProtectionAgency> current_epa;
58
59         /* revert all environment settings back to whatever they were when ardour started
60          */
61
62         if (global_epa) {
63                 current_epa.reset (new EnvironmentalProtectionAgency(true)); /* will restore settings when we leave scope */
64                 global_epa->restore ();
65         }
66
67         std::string s(uri);
68         while (s.find("\\") != std::string::npos)
69                 s.replace(s.find("\\"), 1, "\\\\");
70         while (s.find("\"") != std::string::npos)
71                 s.replace(s.find("\\"), 1, "\\\"");
72
73 #ifdef NO_VFORK
74         std::string command = "xdg-open ";
75         command += '"' + s + '"';
76         command += " &";
77         (void) system (command.c_str());
78 #else
79         if (::vfork () == 0) {
80                 ::execlp ("xdg-open", "xdg-open", s.c_str(), (char*)NULL);
81                 exit (EXIT_SUCCESS);
82         }
83 #endif
84
85 #endif /* not PLATFORM_WINDOWS and not __APPLE__ */
86         return true;
87 }
88
89 bool
90 PBD::open_uri (const std::string& uri)
91 {
92         return open_uri (uri.c_str());
93 }
94
95 bool
96 PBD::open_folder (const std::string& d)
97 {
98 #ifdef __APPLE__
99         CURL *curl = curl_easy_init ();
100         bool rv = false;
101         if (curl) {
102                 char * e = curl_easy_escape (curl, d.c_str(), d.size());
103                 std::string url = "file:///" + std::string(e);
104                 rv = PBD::open_uri (url);
105                 curl_free (e);
106         }
107         return rv;
108 #else
109         return PBD::open_uri (d);
110 #endif
111 }