summaryrefslogtreecommitdiff
path: root/src/modules/utils/openurl.cpp
blob: 72268a502a22403de0839b06995beb3fd5a9716c (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
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (c) 2012-14 Miranda NG project (http://miranda-ng.org),
Copyright (c) 2000-12 Miranda IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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 "..\..\core\commonheaders.h"
#include <ctype.h>

struct TOpenUrlInfo
{
	TOpenUrlInfo(TCHAR *_url, int _bNew) :
		szUrl(_url),
		newWindow(_bNew)
	{}

	ptrT szUrl;
	int newWindow;
};

static void OpenURLThread(void *arg)
{
	TOpenUrlInfo *hUrlInfo = (TOpenUrlInfo*)arg;

	// wack a protocol on it
	CMString tszUrl;
	if ((isalpha(hUrlInfo->szUrl[0]) && hUrlInfo->szUrl[1] == ':') || hUrlInfo->szUrl[0] == '\\')
		tszUrl.Format(_T("file:///%s"), hUrlInfo->szUrl);
	else {
		int i;
		for (i = 0; _istalpha(hUrlInfo->szUrl[i]); i++);
		if (hUrlInfo->szUrl[i] == ':')
			tszUrl = hUrlInfo->szUrl;
		else if (!_tcsnicmp(hUrlInfo->szUrl, _T("ftp."), 4))
			tszUrl.Format(_T("ftp://%s"), hUrlInfo->szUrl);
		else
			tszUrl.Format(_T("http://%s"), hUrlInfo->szUrl);
	}

	// check user defined browser for opening urls
	ptrT tszBrowser(db_get_tsa(NULL, "Miranda", "OpenUrlBrowser"));
	if (tszBrowser)
		ShellExecute(NULL, _T("open"), tszBrowser, tszUrl, NULL, (hUrlInfo->newWindow) ? SW_NORMAL : SW_SHOWDEFAULT);
	else
		ShellExecute(NULL, _T("open"), tszUrl, NULL, NULL, (hUrlInfo->newWindow) ? SW_NORMAL : SW_SHOWDEFAULT);

	delete hUrlInfo;
}

static INT_PTR OpenURL(WPARAM wParam, LPARAM lParam)
{
	if (lParam == 0)
		return 1;

	TOpenUrlInfo *hUrlInfo = new TOpenUrlInfo((wParam & OUF_UNICODE) ? mir_wstrdup((WCHAR*)lParam) : mir_a2t((char*)lParam), wParam & OUF_NEWWINDOW);
	forkthread(OpenURLThread, 0, hUrlInfo);
	return 0;
}

int InitOpenUrl(void)
{
	CreateServiceFunction(MS_UTILS_OPENURL, OpenURL);
	return 0;
}