summaryrefslogtreecommitdiff
path: root/plugins/UserInfoEx/src/ex_import/svc_ExImXML.cpp
blob: 67344160c0728d213fcd1229b7721d48a2d8d7c9 (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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
UserinfoEx plugin for Miranda IM

Copyright:
© 2006-2010 DeathAxe, Yasnovidyashii, Merlin, K. Romanov, Kreol

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 "../stdafx.h"

#define XMLCARD_VERSION	"1.1"

INT_PTR CALLBACK DlgProc_DataHistory(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
		TranslateDialogDefault(hDlg);
		{
			const ICONCTRL idIcon[] = {
				{ ICO_DLG_EXPORT,	WM_SETICON,		NULL		},
				{ ICO_DLG_EXPORT,	STM_SETIMAGE,	ICO_DLGLOGO	},
				{ ICO_BTN_EXPORT,	BM_SETIMAGE,	IDOK		},
				{ ICO_BTN_CANCEL,	BM_SETIMAGE,	IDCANCEL	}
			};
			const int numIconsToSet = g_plugin.getByte(SET_ICONS_BUTTONS, 1) ? _countof(idIcon) : 2;
			IcoLib_SetCtrlIcons(hDlg, idIcon, numIconsToSet);

			SendDlgItemMessage(hDlg, IDOK, BUTTONTRANSLATE, NULL, NULL);
			SendDlgItemMessage(hDlg, IDCANCEL, BUTTONTRANSLATE, NULL, NULL);
		}
		break;

	case WM_CTLCOLORSTATIC:
		switch (GetWindowLongPtr((HWND)lParam, GWLP_ID)) {
		case STATIC_WHITERECT:
		case ICO_DLGLOGO:
		case IDC_INFO:
			SetBkColor((HDC)wParam, RGB(255, 255, 255));
			return (INT_PTR)GetStockObject(WHITE_BRUSH);
		}
		return FALSE;

	case WM_COMMAND:
		if (HIWORD(wParam) == BN_CLICKED) {
			switch (LOWORD(wParam)) {
			case IDCANCEL:
				EndDialog(hDlg, 0);
				break;

			case IDOK:
				WORD hiWord = 0;
				if (IsDlgButtonChecked(hDlg, IDC_CHECK1))
					hiWord |= EXPORT_DATA;
				if (IsDlgButtonChecked(hDlg, IDC_CHECK2))
					hiWord |= EXPORT_HISTORY;
				EndDialog(hDlg, (INT_PTR)MAKELONG(IDOK, hiWord));
				break;
			}
		}
		break;
	}
	return FALSE;
}

/***********************************************************************************************************
 * exporting stuff
 ***********************************************************************************************************/

 /**
  * name:	Export
  * desc:	globally accessible function which does the whole export stuff.
  * params:	hContact	- handle to the contact who is to export
  *			pszFileName	- full qualified path to the xml file which is destination for the export process
  * return:	0 on success, 1 otherwise
  **/
int CFileXml::Export(lpExImParam ExImContact, LPCSTR pszFileName)
{
	DB::CEnumList Modules;

	DWORD result = (DWORD)DialogBox(g_plugin.getInst(), MAKEINTRESOURCE(IDD_EXPORT_DATAHISTORY), nullptr, DlgProc_DataHistory);
	if (LOWORD(result) != IDOK)
		return 0;

	_wExport = HIWORD(result);

	// show dialog to enable user to select modules for export
	if (!(_wExport & EXPORT_DATA) ||
		!DlgExImModules_SelectModulesToExport(ExImContact, &Modules, nullptr)) {

		FILE *xmlfile = fopen(pszFileName, "wt");
		if (!xmlfile) {
			MsgErr(nullptr, LPGENW("Can't create xml file!\n%S"), pszFileName);
			return 1;
		}

		SYSTEMTIME now;
		GetLocalTime(&now);

		// write xml header raw as it is without using the tinyxml api
		fprintf(xmlfile,
			"%c%c%c<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
			"<XMLCard ver=\"" XMLCARD_VERSION "\" ref=\"%04d-%02d-%02d %02d:%02d:%02d\">\n",
			0xefU, 0xbbU, 0xbfU, now.wYear, now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond
		);
		// remember the header's size
		LONG cbHeader = ftell(xmlfile);

		CExImContactXML vContact(this);

		// write data
		if (ExImContact->Typ == EXIM_CONTACT) {
			// export single contact
			_hContactToWorkOn = ExImContact->hContact;
			if (vContact.fromDB(ExImContact->hContact))
				vContact.Export(xmlfile, &Modules);
		}
		else {
			// other export mode
			_hContactToWorkOn = INVALID_CONTACT_ID;

			// export owner contact
			if (ExImContact->Typ == EXIM_ALL && vContact.fromDB(NULL))
				vContact.Export(xmlfile, &Modules);

			// loop for all other contact
			for (MCONTACT hContact = db_find_first(); hContact != NULL; hContact = db_find_next(hContact)) {
				switch (ExImContact->Typ) {
				case EXIM_ALL:
				case EXIM_GROUP:
					// dont export meta subcontacts by default
					if (!db_mc_isSub(hContact)) {
						if (vContact.fromDB(hContact)) {
							vContact.Export(xmlfile, &Modules);
						}
					}
					break;
				case EXIM_SUBGROUP:
					// dont export meta subcontacts by default and
					// export only contact with selectet group name
					if (!db_mc_isSub(hContact) &&
						mir_wstrncmp(ExImContact->ptszName, DB::Setting::GetTString(hContact, "CList", "Group"), mir_wstrlen(ExImContact->ptszName)) == 0) {
						if (vContact.fromDB(hContact)) {
							vContact.Export(xmlfile, &Modules);
						}
					}
					break;
				case EXIM_ACCOUNT:
					// export only contact with selectet account name
					if (!mir_strncmp(ExImContact->pszName, Proto_GetBaseAccountName(hContact), mir_strlen(ExImContact->pszName))) {
						if (vContact.fromDB(hContact)) {
							vContact.Export(xmlfile, &Modules);
						}
					}
					break;
				}
			}
		}
		// *end other export mode

		// nothing exported?
		if (cbHeader == ftell(xmlfile)) {
			fclose(xmlfile);
			DeleteFileA(pszFileName);
			return 1;
		}
		fputs("</XMLCard>\n", xmlfile);
		fclose(xmlfile);
	}
	return 0;
}

/***********************************************************************************************************
 * importing stuff
 ***********************************************************************************************************/

CFileXml::CFileXml()
{
	_numContactsTodo = 0;
	_numContactsDone = 0;
	_numSettingsTodo = 0;
	_numSettingsDone = 0;
	_numEventsTodo = 0;
	_numEventsDone = 0;
	_numEventsDuplicated = 0;
	_hContactToWorkOn = INVALID_CONTACT_ID;
	_wExport = 0;
}

/**
 * name:	ImportOwner
 * desc:	Interpretes an xmlnode as owner contact, finds a corresponding contact in database
 *			or adds a new one including all xml childnodes.
 * params:	xContact	- xmlnode representing the contact
 *			stat		- structure used to collect some statistics
 * return:	ERROR_OK on success or one other element of ImportError to tell the type of failure
 **/

int CFileXml::ImportOwner(const TiXmlElement* xContact)
{
	CExImContactXML vContact(this);
	if (vContact.LoadXmlElement(xContact) == ERROR_OK) {
		vContact.Import();
		return ERROR_OK;
	}
	return ERROR_NOT_ADDED;
}

/**
 * name:	ImportContacts
 * desc:	Parse all child nodes of an given parent node and try to import all found contacts
 * params:	xmlParent	 - xmlnode representing the parent of the list of contacts
 *			hContact	 - handle to the contact, who is the owner of the setting to import
 *			stat		 - structure used to collect some statistics
 * return:	ERROR_OK if at least one contact was successfully imported
 **/

int CFileXml::ImportContacts(const TiXmlElement *xmlParent)
{
	CExImContactXML vContact(this);

	// import contacts
	for (auto *xContact : TiXmlEnum(xmlParent)) {
		if (!mir_strcmpi(xContact->Value(), XKEY_CONTACT)) {
			// update progressbar and abort if user clicked cancel
			LPTSTR pszNick = mir_utf8decodeW(xContact->Attribute("nick"));
			// user clicked abort button
			if (_progress.UpdateContact(LPGENW("Contact: %s (%S)"), pszNick, xContact->Attribute("proto"))) {
				int result = vContact.LoadXmlElement(xContact);
				switch (result) {
				case ERROR_OK:
					// init contact class and import if matches the user desires
					if (_hContactToWorkOn == INVALID_CONTACT_ID || vContact.handle() == _hContactToWorkOn) {
						result = vContact.Import(_hContactToWorkOn != INVALID_CONTACT_ID);
						switch (result) {
						case ERROR_OK:
							_numContactsDone++;
							break;
						case ERROR_ABORTED:
							if (pszNick) mir_free(pszNick);
							return ERROR_ABORTED;
						}
					}
					break;
				case ERROR_ABORTED:
					if (pszNick) mir_free(pszNick);
					return ERROR_ABORTED;
				}
			}
			if (pszNick) mir_free(pszNick);
		}
		// import owner contact
		else if (_hContactToWorkOn == INVALID_CONTACT_ID && !mir_strcmpi(xContact->Value(), XKEY_OWNER) && vContact.LoadXmlElement(xContact) == ERROR_OK) {
			int result = vContact.Import();
			switch (result) {
			case ERROR_OK:
				_numContactsDone++;
				break;
			case ERROR_ABORTED:
				return ERROR_ABORTED;
			}
		}
	}
	return ERROR_OK;
}

/**
 * name:	CountContacts
 * desc:	Counts the number of contacts stored in the file
 * params:	xContact	- the contact, who is the owner of the keys to count
 * return:	nothing
 **/
DWORD CFileXml::CountContacts(const TiXmlElement *xmlParent)
{
	DWORD dwCount = 0;
	// count contacts in file for progress bar
	for (auto *xContact : TiXmlEnum(xmlParent))
		if (!mir_strcmpi(xContact->Name(), XKEY_CONTACT) || !mir_strcmpi(xContact->Name(), XKEY_OWNER))
			dwCount += CountContacts(xContact) + 1;

	return dwCount;
}

/**
 * name:	Import
 * desc:	Interpretes an xmlnode as owner contact, finds a corresponding contact in database
 *			or adds a new one including all xml childnodes.
 * params:	hContact	- handle to the contact, who is the owner of the setting to import
 *			pszFileName	- full qualified path to the xml file which is to import
 * return:	ERROR_OK on success or one other element of ImportError to tell the type of failure
 **/
int CFileXml::Import(MCONTACT hContact, LPCSTR pszFileName)
{
	_hContactToWorkOn = hContact;
	// load xml file
	TiXmlDocument doc;
	if (!doc.LoadFile(pszFileName)) {
		MsgErr(nullptr, LPGENW("Parser is unable to load XMLCard \"%s\"\nError: %d\nDescription: %s"),
			pszFileName, doc.ErrorID(), doc.Error());
		return 1;
	}
	// is xmlfile a XMLCard ?
	TiXmlElement *xmlCard = doc.FirstChildElement("XMLCard");
	if (xmlCard == nullptr) {
		MsgErr(nullptr, LPGENW("The selected file is no valid XMLCard"));
		return 1;
	}
	// check version
	if (mir_strcmp(xmlCard->Attribute("ver"), XMLCARD_VERSION)) {
		MsgErr(nullptr, LPGENW("The version of the XMLCard is not supported by UserInfoEx"));
		return 1;
	}

	// is owner contact to import ?
	if (_hContactToWorkOn == NULL) {
		int ret;

		// disable database safty mode to speed up the operation
		db_set_safety_mode(0);
		// import owner contact
		ret = ImportOwner(xmlCard->FirstChildElement(XKEY_OWNER));
		// as soon as possible enable safty mode again!
		db_set_safety_mode(1);

		if (!ret) {
			MsgBox(nullptr, MB_ICONINFORMATION,
				LPGENW("Complete"),
				LPGENW("Import complete"),
				LPGENW("Owner contact successfully imported."));
			return 0;
		}
		else {
			MsgErr(nullptr, LPGENW("Selected XMLCard does not contain an owner contact!"));
			return 1;
		}
	}
	else {
		// count contacts in file for progress bar
		_numContactsTodo = CountContacts(xmlCard);
		if (_numContactsTodo > 0) {
			_progress.SetContactCount(_numContactsTodo);
			// disable database safty mode to speed up the operation
			db_set_safety_mode(0);
			// import the contacts
			ImportContacts(xmlCard);
			// as soon as possible enable safty mode again!
			db_set_safety_mode(1);
		}
		// finally hide the progress dialog
		_progress.Hide();

		// show results
		MsgBox(nullptr, MB_ICONINFORMATION, LPGENW("Import complete"), LPGENW("Some basic statistics"),
			LPGENW("added contacts: %u / %u\nadded settings: %u / %u\nadded events %u / %u\nduplicated events: %u"),
			_numContactsDone, _numContactsTodo,
			_numSettingsDone, _numSettingsTodo,
			_numEventsDone, _numEventsTodo,
			_numEventsDuplicated);

	}
	return 0;
}