1/*
2 * LegacyClonk
3 *
4 * Copyright (c) RedWolf Design
5 * Copyright (c) 2017-2021, The LegacyClonk Team and contributors
6 *
7 * Distributed under the terms of the ISC license; see accompanying file
8 * "COPYING" for details.
9 *
10 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11 * See accompanying file "TRADEMARK" for details.
12 *
13 * To redistribute this file separately, substitute the full license texts
14 * for the above references.
15 */
16
17#include <Standard.h>
18
19#ifdef WITH_GLIB
20#include <glib.h>
21#endif
22
23#ifdef _WIN32
24#include "C4Windows.h"
25#include <shellapi.h>
26#endif
27
28#ifdef __APPLE__
29#include <cstdlib>
30#include <string>
31#endif
32
33// open a weblink in an external browser
34bool OpenURL(const char *szURL)
35{
36#ifdef _WIN32
37 if (reinterpret_cast<intptr_t>(ShellExecuteA(nullptr, "open", szURL, nullptr, nullptr, SW_SHOW)) > 32)
38 return true;
39#endif
40#ifdef WITH_GLIB
41 const char *argv[][3] =
42 {
43 { "xdg-open", szURL, nullptr },
44 { "sensible-browser", szURL, nullptr },
45 { "firefox", szURL, nullptr },
46 { "mozilla", szURL, nullptr },
47 { "konqueror", szURL, nullptr },
48 { "epiphany", szURL, nullptr },
49 { nullptr, nullptr, nullptr }
50 };
51 for (int i = 0; argv[i][0]; ++i)
52 {
53 GError *error = nullptr;
54 if (g_spawn_async(working_directory: g_get_home_dir(), argv: const_cast<gchar **>(argv[i]), envp: nullptr, flags: G_SPAWN_SEARCH_PATH, child_setup: nullptr, user_data: nullptr, child_pid: nullptr, error: &error))
55 return true;
56 else fprintf(stderr, format: "%s\n", error->message);
57 }
58#endif
59#ifdef __APPLE__
60 std::string command = std::string("open ") + '"' + szURL + '"';
61 std::system(command.c_str());
62 return true;
63#endif
64 // operating system not supported, or all opening method(s) failed
65 return false;
66}
67