aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_webdev/serviceHelper.cpp
blob: 4f0723e2af773c9082acfa093e49de9b55009988 (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "main.h"
#include "./serviceHelper.h"
#include "./wasabi.h"
#include "./serviceHost.h"

#include <ifc_omservice.h>
#include <storageIni.h>
#include <ifc_omserviceeditor.h>
#include <ifc_omserviceeditor.h>
#include <ifc_mlnavigationhelper.h>

#include <strsafe.h>

HRESULT ServiceHelper_QueryStorage(ifc_omstorage **storage)
{
	if (NULL == storage) return E_POINTER;
	
	if (NULL == OMSERVICEMNGR)
	{
		*storage = NULL;
		return E_UNEXPECTED;
	}

	return OMSERVICEMNGR->QueryStorage(&SUID_OmStorageIni, storage);
}

HRESULT ServiceHelper_Create(UINT serviceId, LPCWSTR pszName, LPCWSTR pszIcon, LPCWSTR pszUrl, UINT flags, BOOL fSave, ifc_omservice **serviceOut)
{
	if (NULL == serviceOut) 
		return E_POINTER;

	*serviceOut = NULL;

	if (NULL == OMSERVICEMNGR)
		return E_UNEXPECTED;
	
	WebDevServiceHost *serviceHost;
	if (FAILED(WebDevServiceHost::GetCachedInstance(&serviceHost)))
		serviceHost = NULL;

	HRESULT hr = OMSERVICEMNGR->CreateService(serviceId, serviceHost, serviceOut);
	if (SUCCEEDED(hr))
	{
		ifc_omserviceeditor *editor;
		if (SUCCEEDED((*serviceOut)->QueryInterface(IFC_OmServiceEditor, (void**)&editor)))
		{
			WCHAR szBuffer[4096] = {0};
			editor->BeginUpdate();
			if (NULL != pszName && IS_INTRESOURCE(pszName))
			{
				if (NULL != WASABI_API_LNGSTRINGW_BUF((INT)(INT_PTR)pszName, szBuffer, ARRAYSIZE(szBuffer)))
					editor->SetName(szBuffer, FALSE);
			}
			else
				editor->SetName(pszName, FALSE);
			
			if (NULL != pszIcon && IS_INTRESOURCE(pszIcon))
			{
				if (SUCCEEDED(Plugin_MakeResourcePath(szBuffer, ARRAYSIZE(szBuffer), RT_RCDATA, pszIcon, RESPATH_COMPACT)))
					editor->SetIcon(szBuffer, FALSE);
			}
			else
				editor->SetIcon(pszIcon, FALSE);
			
			if (NULL != pszUrl && IS_INTRESOURCE(pszUrl))
			{
				if (SUCCEEDED(Plugin_MakeResourcePath(szBuffer, ARRAYSIZE(szBuffer), RT_HTML, pszUrl, RESPATH_TARGETIE | RESPATH_COMPACT)))
					editor->SetUrl(szBuffer, FALSE);
			}
			else
				editor->SetUrl(pszUrl, FALSE);
			
			editor->SetFlags(flags, 0xFFFFFFFF);

			if (FALSE != fSave)
			{
				hr = ServiceHelper_Save(*serviceOut);
				if (FAILED(hr))
				{
					(*serviceOut)->Release();
					*serviceOut = NULL;
				}
			}
			else
			{
				editor->SetModified(0, (UINT)-1);
			}
			editor->EndUpdate();
			editor->Release();
		}
	}

	if (NULL != serviceHost)
		serviceHost->Release();

	return hr;
}

HRESULT ServiceHelper_Save(ifc_omservice *service)
{
	if (NULL == service) 
		return E_INVALIDARG;

	if (NULL == OMSERVICEMNGR)
		return E_UNEXPECTED;
	
	HRESULT hr;
	ifc_omstorage *storage;
	hr = ServiceHelper_QueryStorage(&storage);
	if (SUCCEEDED(hr))
	{		
		hr = storage->Save(&service, 1, ifc_omstorage::saveModifiedOnly | ifc_omstorage::saveClearModified, NULL);
		storage->Release();
	}
	return hr;
}

HRESULT ServiceHelper_Delete(ifc_omservice *service)
{
	if (NULL == service) 
		return E_INVALIDARG;

	if (NULL == OMSERVICEMNGR)
		return E_UNEXPECTED;

	ifc_omstorage *storage;
	HRESULT hr = ServiceHelper_QueryStorage(&storage);
	if (SUCCEEDED(hr))
	{
		hr = storage->Delete(&service, 1, NULL);
		storage->Release();
	}
	return hr;
}

HRESULT ServiceHelper_Reload(ifc_omservice *service)
{
	if (NULL == service) 
		return E_INVALIDARG;

	if (NULL == OMSERVICEMNGR)
		return E_UNEXPECTED;

	ifc_omstorage *storage;
	HRESULT hr = ServiceHelper_QueryStorage(&storage);
	if (SUCCEEDED(hr))
	{
		hr = storage->Reload(&service, 1, NULL);
		storage->Release();
	}

	return hr;
}

HRESULT ServiceHelper_UpdateIcon(ifc_omserviceeditor *editor, LPCWSTR pszImage)
{
	WCHAR szBuffer[8192];
	szBuffer[0] = L'\0';

	if (NULL == editor) 
		return E_INVALIDARG;

	ifc_omservice *service;
	if (SUCCEEDED(editor->QueryInterface(IFC_OmService, (void**)&service)))
	{
		if (FAILED(service->GetIcon(szBuffer, ARRAYSIZE(szBuffer))))
			szBuffer[0] = L'\0';
		service->Release();
	}

	HRESULT hr = editor->SetIcon(pszImage, FALSE);
	if (FAILED(hr) || S_FALSE == hr) return hr;

	if (L'\0' != szBuffer)
	{
		ifc_mlnavigationhelper *navHelper;
		if (SUCCEEDED(OMUTILITY->GetMlNavigationHelper(Plugin_GetLibrary(), &navHelper)))
		{		
			navHelper->ReleaseIndex(szBuffer);
			navHelper->Release();
		}
	}

	return S_OK;
}

HRESULT ServiceHelper_IsSpecial(ifc_omservice *service)
{
	if (NULL == service)
		return E_INVALIDARG;

	UINT serviceFlags;
	HRESULT hr = service->GetFlags(&serviceFlags);
	if (SUCCEEDED(hr))
	{
		hr = (0 != ((WDSVCF_ROOT | WDSVCF_SPECIAL) & serviceFlags)) ? S_OK : S_FALSE;
	}
	
	return hr;
}

HRESULT ServiceHelper_IsPreAuthorized(ifc_omservice *service)
{
	if (NULL == service)
		return E_INVALIDARG;

	UINT serviceFlags;
	HRESULT hr = service->GetFlags(&serviceFlags);
	if (SUCCEEDED(hr))
	{
		hr = (0 != (WDSVCF_PREAUTHORIZED & serviceFlags)) ? S_OK : S_FALSE;
	}

	return hr;
}

HRESULT ServiceHelper_RegisterPreAuthorized(ifc_omservice *service)
{
	if (NULL == AGAVE_API_JSAPI2_SECURITY)
		return E_UNEXPECTED;
					
	HRESULT hr;
	WCHAR szBuffer[64];
	hr = StringCchPrintf(szBuffer, ARRAYSIZE(szBuffer), L"%u", service->GetId());
	if (SUCCEEDED(hr))
	{
		UINT flags;
		bool bypassEnabled = (SUCCEEDED(service->GetFlags(&flags)) && 0 != (WDSVCF_PREAUTHORIZED & flags));
		AGAVE_API_JSAPI2_SECURITY->SetBypass(szBuffer, bypassEnabled);
	}
	return hr;
}