2011-04-04 02:01:37 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Vincent Sanders <vince@simtec.co.uk>
|
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* NetSurf 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; version 2 of the License.
|
|
|
|
*
|
|
|
|
* NetSurf 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-08-11 01:22:59 +04:00
|
|
|
#include "utils/config.h"
|
|
|
|
|
2014-10-16 12:51:53 +04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
2011-04-04 02:01:37 +04:00
|
|
|
#include <windows.h>
|
2016-10-19 13:12:19 +03:00
|
|
|
#include <shlobj.h>
|
|
|
|
#include <shlwapi.h>
|
2016-04-19 13:21:06 +03:00
|
|
|
#include <io.h>
|
2011-04-04 02:01:37 +04:00
|
|
|
|
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "utils/log.h"
|
2013-02-22 18:34:23 +04:00
|
|
|
#include "utils/messages.h"
|
2011-04-04 02:01:37 +04:00
|
|
|
#include "utils/filepath.h"
|
2014-05-27 03:08:04 +04:00
|
|
|
#include "utils/file.h"
|
2014-07-06 22:36:12 +04:00
|
|
|
#include "utils/nsurl.h"
|
2014-10-16 12:51:53 +04:00
|
|
|
#include "utils/nsoption.h"
|
2016-10-20 01:31:49 +03:00
|
|
|
#include "netsurf/url_db.h"
|
|
|
|
#include "netsurf/cookie_db.h"
|
2016-05-30 19:32:57 +03:00
|
|
|
#include "netsurf/browser_window.h"
|
2016-05-30 13:29:39 +03:00
|
|
|
#include "netsurf/fetch.h"
|
2016-05-30 13:23:32 +03:00
|
|
|
#include "netsurf/misc.h"
|
2016-05-30 13:51:10 +03:00
|
|
|
#include "netsurf/netsurf.h"
|
2011-04-04 02:01:37 +04:00
|
|
|
|
|
|
|
#include "windows/findfile.h"
|
2015-06-30 23:10:13 +03:00
|
|
|
#include "windows/file.h"
|
2011-04-04 02:01:37 +04:00
|
|
|
#include "windows/drawable.h"
|
2014-01-16 19:00:43 +04:00
|
|
|
#include "windows/download.h"
|
2014-10-16 12:51:53 +04:00
|
|
|
#include "windows/localhistory.h"
|
2015-03-13 02:48:53 +03:00
|
|
|
#include "windows/window.h"
|
2015-03-13 17:32:06 +03:00
|
|
|
#include "windows/schedule.h"
|
|
|
|
#include "windows/font.h"
|
|
|
|
#include "windows/filetype.h"
|
|
|
|
#include "windows/pointers.h"
|
2015-04-16 00:40:36 +03:00
|
|
|
#include "windows/bitmap.h"
|
2014-10-16 12:51:53 +04:00
|
|
|
#include "windows/gui.h"
|
2011-04-04 02:01:37 +04:00
|
|
|
|
|
|
|
static char **respaths; /** resource search path vector. */
|
|
|
|
|
2016-10-19 13:12:19 +03:00
|
|
|
char *nsw32_config_home; /* exported global defined in windows/gui.h */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the path to the config directory.
|
|
|
|
*
|
|
|
|
* This ought to use SHGetKnownFolderPath(FOLDERID_RoamingAppData) and
|
|
|
|
* PathCcpAppend() but uses depricated API because that is what mingw
|
|
|
|
* supports.
|
|
|
|
*
|
|
|
|
* @param config_home_out Path to configuration directory.
|
|
|
|
* @return NSERROR_OK on sucess and \a config_home_out updated else error code.
|
|
|
|
*/
|
|
|
|
static nserror get_config_home(char **config_home_out)
|
|
|
|
{
|
|
|
|
TCHAR adPath[MAX_PATH]; /* appdata path */
|
2016-10-20 01:31:49 +03:00
|
|
|
char nsdir[] = "NetSurf";
|
2016-10-19 13:12:19 +03:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
hres = SHGetFolderPath(NULL,
|
|
|
|
CSIDL_APPDATA | CSIDL_FLAG_CREATE,
|
|
|
|
NULL,
|
|
|
|
SHGFP_TYPE_CURRENT,
|
|
|
|
adPath);
|
|
|
|
if (hres != S_OK) {
|
|
|
|
return NSERROR_INVALID;
|
|
|
|
}
|
|
|
|
|
2016-10-20 01:31:49 +03:00
|
|
|
if (PathAppend(adPath, nsdir) == false) {
|
2016-10-19 13:12:19 +03:00
|
|
|
return NSERROR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ensure netsurf directory exists */
|
|
|
|
if (CreateDirectory(adPath, NULL) == 0) {
|
|
|
|
DWORD dw;
|
|
|
|
dw = GetLastError();
|
|
|
|
if (dw != ERROR_ALREADY_EXISTS) {
|
|
|
|
return NSERROR_NOT_DIRECTORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*config_home_out = strdup(adPath);
|
|
|
|
|
2016-10-20 01:31:49 +03:00
|
|
|
LOG("using config path \"%s\"", *config_home_out);
|
|
|
|
|
2016-10-19 13:12:19 +03:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2011-04-04 02:01:37 +04:00
|
|
|
|
2014-10-26 15:42:53 +03:00
|
|
|
/**
|
|
|
|
* Cause an abnormal program termination.
|
|
|
|
*
|
|
|
|
* \note This never returns and is intended to terminate without any cleanup.
|
|
|
|
*
|
|
|
|
* \param error The message to display to the user.
|
|
|
|
*/
|
|
|
|
static void die(const char *error)
|
|
|
|
{
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-04-26 01:11:55 +03:00
|
|
|
|
2014-01-15 03:31:54 +04:00
|
|
|
static nsurl *gui_get_resource_url(const char *path)
|
2011-04-04 02:01:37 +04:00
|
|
|
{
|
|
|
|
char buf[PATH_MAX];
|
2011-10-05 12:32:52 +04:00
|
|
|
nsurl *url = NULL;
|
|
|
|
|
2014-05-27 02:43:36 +04:00
|
|
|
netsurf_path_to_nsurl(filepath_sfind(respaths, buf, path), &url);
|
2011-10-05 12:32:52 +04:00
|
|
|
|
|
|
|
return url;
|
2011-04-04 02:01:37 +04:00
|
|
|
}
|
|
|
|
|
2014-10-12 14:08:01 +04:00
|
|
|
/**
|
2013-05-23 14:50:57 +04:00
|
|
|
* Ensures output logging stream is available
|
2011-04-04 02:01:37 +04:00
|
|
|
*/
|
2013-05-23 14:50:57 +04:00
|
|
|
static bool nslog_ensure(FILE *fptr)
|
2011-04-04 02:01:37 +04:00
|
|
|
{
|
|
|
|
/* mwindows compile flag normally invalidates standard io unless
|
|
|
|
* already redirected
|
|
|
|
*/
|
|
|
|
if (_get_osfhandle(fileno(fptr)) == -1) {
|
|
|
|
AllocConsole();
|
|
|
|
freopen("CONOUT$", "w", fptr);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:19:06 +04:00
|
|
|
/**
|
2014-10-12 14:08:01 +04:00
|
|
|
* Set option defaults for windows frontend
|
2013-05-28 18:19:06 +04:00
|
|
|
*
|
|
|
|
* @param defaults The option table to update.
|
|
|
|
* @return error status.
|
|
|
|
*/
|
|
|
|
static nserror set_defaults(struct nsoption_s *defaults)
|
2012-10-17 17:20:18 +04:00
|
|
|
{
|
|
|
|
/* Set defaults for absent option strings */
|
|
|
|
|
2016-03-20 13:24:18 +03:00
|
|
|
/* locate CA bundle and set as default, cannot rely on curl
|
|
|
|
* compiled in default on windows.
|
|
|
|
*/
|
|
|
|
DWORD res_len;
|
|
|
|
DWORD buf_tchar_size = PATH_MAX + 1;
|
|
|
|
DWORD buf_bytes_size = sizeof(TCHAR) * buf_tchar_size;
|
|
|
|
char *ptr = NULL;
|
|
|
|
char *buf;
|
2016-10-20 01:31:49 +03:00
|
|
|
char *fname;
|
|
|
|
HRESULT hres;
|
|
|
|
char dldir[] = "Downloads";
|
2016-03-20 13:24:18 +03:00
|
|
|
|
|
|
|
buf = malloc(buf_bytes_size);
|
|
|
|
if (buf== NULL) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
buf[0] = '\0';
|
|
|
|
|
2016-10-20 01:31:49 +03:00
|
|
|
/* locate certificate bundle */
|
2016-03-20 13:24:18 +03:00
|
|
|
res_len = SearchPathA(NULL,
|
|
|
|
"ca-bundle.crt",
|
|
|
|
NULL,
|
|
|
|
buf_tchar_size,
|
|
|
|
buf,
|
|
|
|
&ptr);
|
|
|
|
if (res_len > 0) {
|
|
|
|
nsoption_setnull_charp(ca_bundle, strdup(buf));
|
|
|
|
}
|
2016-10-20 01:31:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* download directory default
|
|
|
|
*
|
|
|
|
* unfortunately SHGetKnownFolderPath(FOLDERID_Downloads) is
|
|
|
|
* not available so use the obsolete method of user prodile
|
|
|
|
* with downloads suffixed
|
|
|
|
*/
|
|
|
|
buf[0] = '\0';
|
|
|
|
|
|
|
|
hres = SHGetFolderPath(NULL,
|
|
|
|
CSIDL_PROFILE | CSIDL_FLAG_CREATE,
|
|
|
|
NULL,
|
|
|
|
SHGFP_TYPE_CURRENT,
|
|
|
|
buf);
|
|
|
|
if (hres == S_OK) {
|
|
|
|
if (PathAppend(buf, dldir)) {
|
|
|
|
nsoption_setnull_charp(downloads_directory,
|
|
|
|
strdup(buf));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:24:18 +03:00
|
|
|
free(buf);
|
|
|
|
|
2012-10-17 17:20:18 +04:00
|
|
|
/* ensure homepage option has a default */
|
|
|
|
nsoption_setnull_charp(homepage_url, strdup(NETSURF_HOMEPAGE));
|
2013-05-28 18:19:06 +04:00
|
|
|
|
2016-10-20 01:31:49 +03:00
|
|
|
/* cookie file default */
|
|
|
|
fname = NULL;
|
|
|
|
netsurf_mkpath(&fname, NULL, 2, nsw32_config_home, "Cookies");
|
|
|
|
if (fname != NULL) {
|
|
|
|
nsoption_setnull_charp(cookie_file, fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cookie jar default */
|
|
|
|
fname = NULL;
|
|
|
|
netsurf_mkpath(&fname, NULL, 2, nsw32_config_home, "Cookies");
|
|
|
|
if (fname != NULL) {
|
|
|
|
nsoption_setnull_charp(cookie_jar, fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* url database default */
|
|
|
|
fname = NULL;
|
|
|
|
netsurf_mkpath(&fname, NULL, 2, nsw32_config_home, "URLs");
|
|
|
|
if (fname != NULL) {
|
|
|
|
nsoption_setnull_charp(url_file, fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bookmark database default */
|
|
|
|
fname = NULL;
|
|
|
|
netsurf_mkpath(&fname, NULL, 2, nsw32_config_home, "Hotlist");
|
|
|
|
if (fname != NULL) {
|
|
|
|
nsoption_setnull_charp(hotlist_path, fname);
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:19:06 +04:00
|
|
|
return NSERROR_OK;
|
2012-10-17 17:20:18 +04:00
|
|
|
}
|
|
|
|
|
2014-01-11 18:20:30 +04:00
|
|
|
|
2016-10-19 13:12:19 +03:00
|
|
|
/**
|
|
|
|
* Initialise user options location and contents
|
|
|
|
*/
|
|
|
|
static nserror nsw32_option_init(int *pargc, char** argv)
|
|
|
|
{
|
|
|
|
nserror ret;
|
|
|
|
char *choices = NULL;
|
|
|
|
|
|
|
|
/* user options setup */
|
|
|
|
ret = nsoption_init(set_defaults, &nsoptions, &nsoptions_default);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attempt to load the user choices */
|
|
|
|
ret = netsurf_mkpath(&choices, NULL, 2, nsw32_config_home, "Choices");
|
|
|
|
if (ret == NSERROR_OK) {
|
|
|
|
nsoption_read(choices, nsoptions);
|
|
|
|
free(choices);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* overide loaded options with those from commandline */
|
|
|
|
nsoption_commandline(pargc, argv, nsoptions);
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-16 00:50:28 +03:00
|
|
|
static struct gui_misc_table win32_misc_table = {
|
2015-03-13 17:32:06 +03:00
|
|
|
.schedule = win32_schedule,
|
2016-04-28 00:41:59 +03:00
|
|
|
.warning = win32_warning,
|
2015-03-13 17:32:06 +03:00
|
|
|
};
|
|
|
|
|
2011-04-04 02:01:37 +04:00
|
|
|
/**
|
2015-03-15 03:00:45 +03:00
|
|
|
* Entry point from windows
|
2011-04-04 02:01:37 +04:00
|
|
|
**/
|
|
|
|
int WINAPI
|
|
|
|
WinMain(HINSTANCE hInstance, HINSTANCE hLastInstance, LPSTR lpcli, int ncmd)
|
|
|
|
{
|
|
|
|
char **argv = NULL;
|
|
|
|
int argc = 0, argctemp = 0;
|
|
|
|
size_t len;
|
|
|
|
LPWSTR *argvw;
|
|
|
|
char *messages;
|
|
|
|
nserror ret;
|
2013-02-14 22:21:11 +04:00
|
|
|
const char *addr;
|
|
|
|
nsurl *url;
|
2014-05-10 13:22:10 +04:00
|
|
|
struct netsurf_table win32_table = {
|
2016-04-16 00:50:28 +03:00
|
|
|
.misc = &win32_misc_table,
|
2014-01-15 23:37:05 +04:00
|
|
|
.window = win32_window_table,
|
|
|
|
.clipboard = win32_clipboard_table,
|
|
|
|
.download = win32_download_table,
|
2014-01-23 03:19:57 +04:00
|
|
|
.fetch = win32_fetch_table,
|
2014-05-07 19:14:18 +04:00
|
|
|
.file = win32_file_table,
|
2014-02-02 02:17:36 +04:00
|
|
|
.utf8 = win32_utf8_table,
|
2015-04-16 00:40:36 +03:00
|
|
|
.bitmap = win32_bitmap_table,
|
2016-04-24 19:59:03 +03:00
|
|
|
.layout = win32_layout_table,
|
2014-01-15 23:37:05 +04:00
|
|
|
};
|
2014-01-23 04:09:59 +04:00
|
|
|
win32_fetch_table->get_resource_url = gui_get_resource_url;
|
2011-04-04 02:01:37 +04:00
|
|
|
|
2014-05-10 13:22:10 +04:00
|
|
|
ret = netsurf_register(&win32_table);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
die("NetSurf operation table registration failed");
|
|
|
|
}
|
|
|
|
|
2016-10-30 15:58:43 +03:00
|
|
|
/* Save the application-instance handle. */
|
|
|
|
hinst = hInstance;
|
|
|
|
|
2016-10-19 13:12:19 +03:00
|
|
|
setbuf(stderr, NULL);
|
|
|
|
|
|
|
|
/* Construct a unix style argc/argv */
|
2011-04-04 02:01:37 +04:00
|
|
|
if (SLEN(lpcli) > 0) {
|
|
|
|
argvw = CommandLineToArgvW(GetCommandLineW(), &argc);
|
|
|
|
}
|
|
|
|
|
|
|
|
argv = malloc(sizeof(char *) * argc);
|
|
|
|
while (argctemp < argc) {
|
|
|
|
len = wcstombs(NULL, argvw[argctemp], 0) + 1;
|
|
|
|
if (len > 0) {
|
|
|
|
argv[argctemp] = malloc(len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv[argctemp] != NULL) {
|
|
|
|
wcstombs(argv[argctemp], argvw[argctemp], len);
|
|
|
|
/* alter windows-style forward slash flags to
|
|
|
|
* hyphen flags.
|
|
|
|
*/
|
|
|
|
if (argv[argctemp][0] == '/')
|
|
|
|
argv[argctemp][0] = '-';
|
|
|
|
}
|
|
|
|
argctemp++;
|
|
|
|
}
|
|
|
|
|
2013-05-23 14:50:57 +04:00
|
|
|
/* initialise logging - not fatal if it fails but not much we
|
|
|
|
* can do about it
|
|
|
|
*/
|
|
|
|
nslog_init(nslog_ensure, &argc, argv);
|
|
|
|
|
2016-10-19 13:12:19 +03:00
|
|
|
/* Locate the correct user configuration directory path */
|
|
|
|
ret = get_config_home(&nsw32_config_home);
|
2013-05-23 14:50:57 +04:00
|
|
|
if (ret != NSERROR_OK) {
|
2016-10-19 13:12:19 +03:00
|
|
|
LOG("Unable to locate a configuration directory.");
|
|
|
|
nsw32_config_home = NULL;
|
2013-05-23 14:50:57 +04:00
|
|
|
}
|
2016-10-19 13:12:19 +03:00
|
|
|
|
|
|
|
/* Initialise user options */
|
|
|
|
ret = nsw32_option_init(&argc, argv);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
LOG("Options failed to initialise (%s)\n",
|
|
|
|
messages_get_errorcode(ret));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
respaths = nsws_init_resource("${APPDATA}\\NetSurf:${HOME}\\.netsurf:${NETSURFRES}:${PROGRAMFILES}\\NetSurf\\NetSurf\\:"NETSURF_WINDOWS_RESPATH);
|
2011-04-04 02:01:37 +04:00
|
|
|
|
2015-06-19 18:29:42 +03:00
|
|
|
/* message init */
|
2013-05-28 18:19:06 +04:00
|
|
|
messages = filepath_find(respaths, "messages");
|
2015-06-19 18:29:42 +03:00
|
|
|
messages_add_from_file(messages);
|
2011-04-04 02:01:37 +04:00
|
|
|
free(messages);
|
2015-06-19 18:29:42 +03:00
|
|
|
|
|
|
|
/* common initialisation */
|
|
|
|
ret = netsurf_init(NULL);
|
2013-05-28 18:19:06 +04:00
|
|
|
if (ret != NSERROR_OK) {
|
2015-05-28 18:08:46 +03:00
|
|
|
LOG("NetSurf failed to initialise");
|
2013-05-28 18:19:06 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2011-04-04 02:01:37 +04:00
|
|
|
|
2016-10-20 01:31:49 +03:00
|
|
|
urldb_load(nsoption_charp(url_file));
|
|
|
|
urldb_load_cookies(nsoption_charp(cookie_file));
|
|
|
|
|
2011-04-04 02:01:37 +04:00
|
|
|
ret = nsws_create_main_class(hInstance);
|
|
|
|
ret = nsws_create_drawable_class(hInstance);
|
|
|
|
ret = nsws_create_localhistory_class(hInstance);
|
|
|
|
|
2012-03-22 13:34:34 +04:00
|
|
|
nsoption_set_bool(target_blank, false);
|
2011-04-04 02:01:37 +04:00
|
|
|
|
|
|
|
nsws_window_init_pointers(hInstance);
|
|
|
|
|
|
|
|
/* If there is a url specified on the command line use it */
|
2012-03-22 13:34:34 +04:00
|
|
|
if (argc > 1) {
|
2011-04-04 02:01:37 +04:00
|
|
|
addr = argv[1];
|
2013-02-14 22:21:11 +04:00
|
|
|
} else if (nsoption_charp(homepage_url) != NULL) {
|
2012-03-22 13:34:34 +04:00
|
|
|
addr = nsoption_charp(homepage_url);
|
2013-02-14 22:21:11 +04:00
|
|
|
} else {
|
|
|
|
addr = NETSURF_HOMEPAGE;
|
2012-03-22 13:34:34 +04:00
|
|
|
}
|
2011-04-04 02:01:37 +04:00
|
|
|
|
2015-05-28 18:08:46 +03:00
|
|
|
LOG("calling browser_window_create");
|
2011-04-04 02:01:37 +04:00
|
|
|
|
2014-05-10 13:22:10 +04:00
|
|
|
ret = nsurl_create(addr, &url);
|
|
|
|
if (ret == NSERROR_OK) {
|
|
|
|
ret = browser_window_create(BW_CREATE_HISTORY,
|
2013-02-14 22:21:11 +04:00
|
|
|
url,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL);
|
|
|
|
nsurl_unref(url);
|
|
|
|
|
|
|
|
}
|
2014-05-10 13:22:10 +04:00
|
|
|
if (ret != NSERROR_OK) {
|
2016-04-28 00:41:59 +03:00
|
|
|
win32_warning(messages_get_errorcode(ret), 0);
|
2013-02-14 22:21:11 +04:00
|
|
|
} else {
|
2014-10-12 14:08:01 +04:00
|
|
|
win32_run();
|
2013-02-14 22:21:11 +04:00
|
|
|
}
|
2011-04-04 02:01:37 +04:00
|
|
|
|
2016-10-20 01:31:49 +03:00
|
|
|
urldb_save_cookies(nsoption_charp(cookie_jar));
|
|
|
|
urldb_save(nsoption_charp(url_file));
|
|
|
|
|
2011-04-04 02:01:37 +04:00
|
|
|
netsurf_exit();
|
|
|
|
|
2016-10-19 13:12:19 +03:00
|
|
|
/* finalise options */
|
|
|
|
nsoption_finalise(nsoptions, nsoptions_default);
|
2011-04-04 02:01:37 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|