summaryrefslogtreecommitdiff
path: root/openurl.c
blob: 3acb51b5d7655780be971ae8e17b4e83f5796b4b (plain)
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
/*
 * Copyright (C) 2002-2004 Joern Thyssen <[email protected]>
 * Copyright (C) 2003-2017 the AUTHORS
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 *
 * $Id: openurl.c,v 1.31 2017/09/10 11:57:41 plm Exp $
 */

#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include "backgammon.h"
#include "openurl.h"
#if defined(_WIN32)
#include <windows.h>
#include <shellapi.h>
#else
#include <string.h>
#endif                          /* _WIN32 */
static gchar *web_browser = NULL;

extern const gchar *
get_web_browser(void)
{
    const gchar *pch;
#if defined(_WIN32)
    if (!web_browser || !*web_browser)
        return ("");
    else
#else
    if (web_browser && *web_browser)
#endif
        return web_browser;
    if ((pch = g_getenv("BROWSER")) == NULL) {
#if defined(__APPLE__)
        pch = "open";
#else
        pch = OPEN_URL_PROG;
#endif
    }
    return pch;
}


extern char *
set_web_browser(const char *sz)
{
    g_free(web_browser);
    web_browser = g_strdup(sz ? sz : "");
    return web_browser;
}

extern void
OpenURL(const char *szURL)
{
    const gchar *browser = get_web_browser();
    gchar *commandString;
    GError *error = NULL;
    if (!(browser) || !(*browser)) {
#if defined(_WIN32)
        ptrdiff_t win_error;
        gchar *url = g_filename_to_uri(szURL, NULL, NULL);
        win_error = (ptrdiff_t) ShellExecute(NULL, TEXT("open"), url ? url : szURL, NULL, ".\\", SW_SHOWNORMAL);
        if (win_error >= 0 && win_error < 33)
            outputerrf(_("Failed to perform default action on " "%s. Error code was %d"), url, (int)win_error);
        g_free(url);
        return;
#endif
    }
    commandString = g_strdup_printf("'%s' '%s'", browser, szURL);
    if (!g_spawn_command_line_async(commandString, &error)) {
        outputerrf(_("Browser couldn't open file (%s): %s\n"), commandString, error->message);
        g_error_free(error);
    }
    return;
}