aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_online/importFile.cpp
blob: d186306ddfcf54096325bdd13f36c4316c62e49d (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "main.h"
#include "./import.h"
#include "./api__ml_online.h"
#include "./resource.h"
#include "./serviceHost.h"
#include "./serviceHelper.h"
#include "./navigation.h"

#include <ifc_omstorage.h>
#include <ifc_omfilestorage.h>
#include <ifc_omstorageenum.h>
#include <ifc_omservice.h>
#include <ifc_omserviceenum.h>

#include <strsafe.h>

static HRESULT ImportFile_GetEnumerator(ifc_omstorageenumerator **enumerator)
{
	if (NULL == OMSERVICEMNGR) return E_UNEXPECTED;
	return OMSERVICEMNGR->EnumStorage(&STID_OmFileStorage, ifc_omstorage::capPublic | ifc_omstorage::capLoad, enumerator);
}

HRESULT ImportService_GetFileSupported()
{
	if (NULL == OMSERVICEMNGR) 
		return E_UNEXPECTED;

	ifc_omstorageenumerator *enumerator;
	HRESULT hr = ImportFile_GetEnumerator(&enumerator);
	
	if (SUCCEEDED(hr))
	{		
		ifc_omstorage *storage;
		if(S_OK == enumerator->Next(1, &storage, NULL))
		{
			storage->Release();
			hr = S_OK;
		}
		else
		{
			hr = S_FALSE;
		}

		enumerator->Release();
	}
	return hr;
}

static HRESULT ImportFile_GetFilter(LPWSTR pszBuffer, UINT cchBufferMax, DWORD *defaultIndex)
{
	if (NULL != defaultIndex) 
		*defaultIndex = 0;

	if (NULL == pszBuffer) 
		return E_POINTER;

	HRESULT hr;
	WCHAR szName[128] = {0}, szList[512] = {0};

	LPWSTR cursor = pszBuffer;
	size_t remaining = cchBufferMax;

	LPWSTR listC = szList;
	size_t listR = ARRAYSIZE(szList);
	
	DWORD counter = 0;

	szList[0] = L'\0';
	pszBuffer[0] = L'\0';

	WASABI_API_LNGSTRINGW_BUF(IDS_FILEFILTER_ALL, szName, ARRAYSIZE(szName));
	hr = Plugin_AppendFileFilter(cursor, remaining, szName, L"*.*", &cursor, &remaining, TRUE);
	if (FAILED(hr)) return hr;
	counter++;

	ifc_omstorageenumerator *enumerator;
	hr = ImportFile_GetEnumerator(&enumerator);
	if (SUCCEEDED(hr))
	{		
		ifc_omstorage *storage;
		ifc_omfilestorage *fileStorage;
		while(S_OK == enumerator->Next(1, &storage, NULL))
		{
			if (SUCCEEDED(storage->QueryInterface(IFC_OmFileStorage, (void**)&fileStorage)))
			{
				WCHAR szFilter[64] = {0};
				if (SUCCEEDED(fileStorage->GetFilter(szFilter, ARRAYSIZE(szFilter))) && 
					L'\0' != szFilter[0] &&
					SUCCEEDED(storage->GetDescription(szName, ARRAYSIZE(szName))))
				{
					hr = Plugin_AppendFileFilter(cursor, remaining, szName, szFilter, &cursor, &remaining, TRUE);
					if (FAILED(hr)) break;

					counter++;
			
					if (listC == szList || SUCCEEDED(StringCchCopyEx(listC, listR, L";", &listC, &listR, 0)))
						StringCchCopyEx(listC, listR, szFilter, &listC, &listR, 0);
				}
				fileStorage->Release();
			}
			storage->Release();
		}
		enumerator->Release();
	}

	if (SUCCEEDED(hr) && L'\0' != szList[0])
	{
		WASABI_API_LNGSTRINGW_BUF(IDS_FILEFILTER_ALLKNOWN, szName, ARRAYSIZE(szName));
		hr = Plugin_AppendFileFilter(cursor, remaining, szName, szList, &cursor, &remaining, TRUE);
		if (FAILED(hr)) return hr;
		
		counter++;

		if (NULL != defaultIndex) 
			*defaultIndex = counter;
	}

	return hr;
}

static HRESULT ImportFile_ProcessFile(HWND hOwner, ifc_omstorageenumerator *enumerator,
									  ifc_omservicehost *serviceHost, ifc_omstorage *serviceStorage,
									  LPCWSTR pszFile, ULONG *converted)
{
	ifc_omstorage *storage;
	enumerator->Reset();

	Navigation	*navigation;
	if (FAILED(Plugin_GetNavigation(&navigation)))
		return E_FAIL;

	ULONG loaded(0), saved(0);
	while(S_OK == enumerator->Next(1, &storage, NULL))
	{
		ifc_omserviceenum *serviceEnum;
		HRESULT hr = storage->Load(pszFile, serviceHost, &serviceEnum);
		if(SUCCEEDED(hr))
		{
			ifc_omservice *service;
			while(S_OK == serviceEnum->Next(1, &service, NULL))
			{
				loaded++;
				if (SUCCEEDED(service->SetAddress(NULL)))
				{
					service->UpdateFlags(SVCF_SUBSCRIBED | SVCF_PREAUTHORIZED);

					ULONG savedOk;
					if (SUCCEEDED(serviceStorage->Save(&service, 1, ifc_omstorage::saveClearModified, &savedOk)))
					{
						navigation->CreateItem(service, 1);
						saved += savedOk;
					}
				}
				service->Release();
			}
			serviceEnum->Release();
			break;
		}
		else if (OMSTORAGE_E_UNKNOWN_FORMAT != hr)
		{
			break;
		}

		storage->Release();
	}

	if (NULL != converted) 
		*converted = saved;

	navigation->Release();

	return S_OK;
}

static HRESULT ImportFile_ProcessList(HWND hOwner, LPCWSTR pszList)
{
	if (NULL == pszList) 
		return E_INVALIDARG;
	
	LPCWSTR base, block, c;
	base = pszList;
	c = base;
	block = NULL;
	ULONG converted;

	ifc_omstorageenumerator *enumerator;
	HRESULT hr = ImportFile_GetEnumerator(&enumerator);
	if (FAILED(hr)) return hr;

	ServiceHost *serviceHost;
	hr = ServiceHost::GetCachedInstance(&serviceHost);
	if (SUCCEEDED(hr))
	{
		ifc_omstorage *serviceStorage;
		hr = ServiceHelper_QueryStorage(&serviceStorage);
		if (SUCCEEDED(hr))
		{
			ULONG scanned(0);
			while(L'\0' != *c)
			{
				block = c;
				while (L'\0' != *c) c++;
				if (c != block && block != base)
				{
					WCHAR szBuffer[MAX_PATH * 2] = {0};
					scanned++;
					if (SUCCEEDED(StringCchPrintf(szBuffer, ARRAYSIZE(szBuffer), L"%s\\%s", base, block)) && 
						SUCCEEDED(ImportFile_ProcessFile(hOwner, enumerator, serviceHost, serviceStorage, szBuffer, &converted)))
					{
					}
				}
				c++;
			}

			if (pszList == block && c != pszList && 
				SUCCEEDED(ImportFile_ProcessFile(hOwner, enumerator, serviceHost, serviceStorage, pszList, &converted)))
			{
			}
			serviceStorage->Release();
		}
		serviceHost->Release();
	}

	enumerator->Release();
	return hr;
}

HRESULT ImportService_FromFile(HWND hOwner)
{
	if (NULL == OMSERVICEMNGR) 
		return E_UNEXPECTED;

	OPENFILENAME of = {0};
	of.lStructSize = sizeof(of);

	WCHAR szFilter[1024] = {0};
	HRESULT hr = ImportFile_GetFilter(szFilter, ARRAYSIZE(szFilter), &of.nFilterIndex);
	if (FAILED(hr)) return hr;

	UINT cchResultMax = 16384;
	LPWSTR pszResult = Plugin_MallocString(cchResultMax);
	if (NULL == pszResult) return E_OUTOFMEMORY;
	*pszResult = L'\0';
		
	of.hwndOwner = hOwner;
	of.lpstrFilter = szFilter;
	of.lpstrFile = pszResult;
	of.nMaxFile = cchResultMax;
	of.lpstrInitialDir = WASABI_API_APP->path_getUserSettingsPath();
	of.lpstrTitle = WASABI_API_LNGSTRINGW(IDS_IMPORT_FILES);
	of.Flags = OFN_ENABLESIZING | OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_ALLOWMULTISELECT;
	
	if (0 == GetOpenFileName(&of))
	{
		INT err = CommDlgExtendedError();
		hr = (0 == err) ? S_FALSE : E_FAIL;
	}
	else
	{
		hr = ImportFile_ProcessList(hOwner, pszResult);
	}

	Plugin_FreeString(pszResult);
	return hr;
}