summaryrefslogtreecommitdiff
path: root/plugins/BasicHistory/src/ExportManager.cpp
blob: 30453b10a90f25c47cf5929409c8a90e7a286f27 (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
/*
Basic History plugin
Copyright (C) 2011-2012 Krzysztof Kral

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 version 2
of the License.

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, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include "ExportManager.h"
#include "TxtExport.h"
#include "PlainHtmlExport.h"
#include "RichHtmlExport.h"
#include "BinaryExport.h"
#include "DatExport.h"
#include "Options.h"
#include "codecvt_CodePage.h"

ExportManager::ExportManager(HWND _hwnd, HCONTACT _hContact, int filter) :
	EventList(_hContact, filter),
	hwnd(_hwnd),
	oldOnTop(false)
{
}

std::wstring GetFile(const TCHAR* ext, HWND hwnd, bool open)
{
	TCHAR filter[512];
	std::locale loc;
	TCHAR extUpper[32];
	_tcscpy_s(extUpper, ext);
	extUpper[0] = std::toupper(ext[0], loc);
	mir_sntprintf(filter, SIZEOF(filter), TranslateT("%s Files (*.%s)"), extUpper, ext);
	size_t len = _tcslen(filter) + 1;
	mir_sntprintf(filter + len, 512 - len, TranslateT("*.%s"), ext);
	len += _tcslen(filter + len);
	filter[++len] = 0;
	TCHAR stzFilePath[1024];
	_tcscpy_s(stzFilePath, TranslateT("History"));
	_tcscat_s(stzFilePath, _T("."));
	_tcscat_s(stzFilePath, ext);
	len = _tcslen(stzFilePath) + 1;
	stzFilePath[len] = 0;
	OPENFILENAME ofn = {0};
	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFilter = filter;
	ofn.nFilterIndex = 1;
	ofn.lpstrFile = stzFilePath;
	ofn.lpstrTitle = open ? TranslateT("Import") : TranslateT("Export");
	ofn.nMaxFile = 1024;
	ofn.lpstrDefExt = ext;
	if (open) {
		ofn.Flags = OFN_PATHMUSTEXIST | OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
		if (GetOpenFileName(&ofn))
			return stzFilePath;
	}
	else {
		ofn.Flags = OFN_NOREADONLYRETURN | OFN_PATHMUSTEXIST | OFN_EXPLORER | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR;
		if (GetSaveFileName(&ofn))
			return stzFilePath;
	}

	return L"";
}

std::wstring ReplaceExt(const std::wstring& file, const TCHAR* ext)
{
	size_t pos = file.find(_T("<ext>"));
	if (pos < file.length()) {
		std::wstring fileName = file.substr(0, pos);
		fileName += ext;
		fileName += file.substr(pos + 5);
		return fileName;
	}

	return file;
}

bool ExportManager::Export(IExport::ExportType type)
{
	exp = NULL;
	UINT cp;
	std::wstring encoding;
	bool isBin = false;
	switch(type) {
	case IExport::Txt:
		exp = new TxtExport();
		cp = Options::instance->codepageTxt;
		encoding = Options::instance->encodingTxt;
		isFlat = true;
		break;
	case IExport::PlainHtml:
		exp = new PlainHtmlExport();
		cp = Options::instance->codepageHtml1;
		encoding = Options::instance->encodingHtml1;
		break;
	case IExport::RichHtml:
		exp = new RichHtmlExport();
		cp = Options::instance->codepageHtml2;
		encoding = Options::instance->encodingHtml2;
		break;
	case IExport::Binary:
		exp = new BinaryExport();
		cp = CP_UTF8;
		encoding = L"UTF8";
		isFlat = true;
		oldOnTop = true;
		isBin = true;
		break;
	case IExport::Dat:
		exp = new DatExport();
		cp = CP_UTF8;
		encoding = L"UTF8";
		isFlat = true;
		oldOnTop = true;
		isBin = true;
		break;
	default:
		return false;
	}

	std::wstring fileName;
	if (file.empty())
		fileName = GetFile(exp->GetExt(), hwnd, false);
	else
		fileName = ReplaceExt(file, exp->GetExt());

	if (fileName.empty())
		return false;

	std::wofstream* stream;
	if (!isBin) {
		stream = new std::wofstream (fileName.c_str());
		if (!stream->is_open())
			return false;
	
		std::locale filelocale(std::locale(), new codecvt_CodePage<wchar_t>(cp));
		stream->imbue(filelocale);
		exp->SetStream(stream);
	}
	else {
		std::ofstream* cstream = new std::ofstream (fileName.c_str(), std::ios_base::binary);
		if (!cstream->is_open())
			return false;
	
		stream = (std::wofstream*)cstream;
		exp->SetStream(stream);
	}

	exp->WriteHeader(fileName, GetFilterName(), GetMyName(), GetMyId(), GetContactName(), GetProtocolName(), GetContactId(), GetBaseProtocol(), encoding);

	RefreshEventList();

	exp->WriteFooter();
	if (!isBin) {
		stream->close();
		delete stream;
	}
	else {
		std::ofstream* cstream = (std::ofstream*)stream;
		cstream->close();
		delete cstream;
	}

	delete exp;
	return true;
}

const TCHAR* ExportManager::GetExt(IImport::ImportType type)
{
	IImport* imp = NULL;
	switch(type) {
	case IImport::Binary:
		imp = new BinaryExport();
		break;
	case IImport::Dat:
		imp = new DatExport();
		break;
	default:
		return L"";
	}
	
	const TCHAR* ext = imp->GetExt();
	delete imp;
	return ext;
}

int ExportManager::Import(IImport::ImportType type, const std::vector<HCONTACT>& contacts)
{
	IImport* imp = NULL;
	switch(type) {
	case IImport::Binary:
		imp = new BinaryExport();
		break;
	case IImport::Dat:
		imp = new DatExport();
		break;
	default:
		return -2;
	}

	std::wstring fileName;
	if (file.empty())
		return -2;
	else
	{
		fileName = ReplaceExt(file, imp->GetExt());
	}

	if (fileName.empty())
		return -2;

	std::ifstream* stream = new std::ifstream (fileName.c_str(), std::ios_base::binary);
	if (!stream->is_open())
		return -2;
	
	imp->SetStream(stream);
	int t = imp->IsContactInFile(contacts);
	stream->close();
	delete stream;
	delete imp;
	return t;
}

bool ExportManager::Import(IImport::ImportType type, std::vector<IImport::ExternalMessage>& eventList, std::wstring* err, bool* differentContact, std::vector<HCONTACT>* contacts)
{
	IImport* imp = NULL;
	switch(type) {
	case IImport::Binary:
		imp = new BinaryExport();
		break;
	case IImport::Dat:
		imp = new DatExport();
		break;
	default:
		return false;
	}
	
	std::wstring fileName;
	if (file.empty())
		file = fileName = GetFile(imp->GetExt(), hwnd, true);
	else
		fileName = ReplaceExt(file, imp->GetExt());

	std::ifstream* stream = new std::ifstream (fileName.c_str(), std::ios_base::binary);
	if (!stream->is_open())
		return false;
	
	imp->SetStream(stream);
	std::vector<HCONTACT> v;
	v.push_back(hContact);
	bool ret = true;
	int contInFile = imp->IsContactInFile(v);
	if (contInFile == -1) {
		ret = false;
		if (err != NULL)
			*err = TranslateT("File does not contain selected contact");
		
		if (contacts != NULL && differentContact != NULL) {
			contInFile = imp->IsContactInFile(*contacts);
			if (contInFile >= 0) {
				*differentContact = true;
				hContact = (*contacts)[contInFile];
			}
		}
	}
	else if (contInFile == 0 || contInFile == -3) {
		ret = imp->GetEventList(eventList);
		if (!ret && err != NULL)
			*err = TranslateT("File is corrupted");
	}
	else {
		ret = false;
		if (err != NULL)
			*err = TranslateT("File is corrupted");
	}
	stream->close();
	delete stream;
	delete imp;
	return ret;
}

void ExportManager::AddGroup(bool isMe, const std::wstring &time, const std::wstring &user, const std::wstring &eventText, int ico)
{
	if (exp == NULL)
		return;
	
	exp->WriteGroup(isMe, time, user, eventText);
	TCHAR str[MAXSELECTSTR + 8]; // for safety reason
	str[0] = 0;
	tm lastTime;
	bool isFirst = true;
	bool lastMe = false;
	EventData data;
	std::deque<EventIndex> revDeq;
	std::deque<EventIndex>& deq = eventList.back();
	if (!oldOnTop && Options::instance->messagesNewOnTop) {
		revDeq.insert(revDeq.begin(), deq.rbegin(), deq.rend());
		deq = revDeq;
	}
	for (std::deque<EventIndex>::iterator it = deq.begin(); it != deq.end(); ++it) {
		EventIndex hDbEvent = *it;
		if (GetEventData(hDbEvent, data)) {
			lastMe = data.isMe;

			TCHAR* formatDate = Options::instance->messagesShowSec ? _T("d s") : _T("d t");
			TCHAR* longFormatDate = Options::instance->messagesShowSec ? _T("d s") : _T("d t");
			if (!Options::instance->messagesShowDate) {
				if (isFirst) {
					isFirst = false;
					formatDate = Options::instance->messagesShowSec ? _T("s") : _T("t");
					time_t tt = data.timestamp;
					localtime_s(&lastTime,  &tt);
				}
				else {
					time_t tt = data.timestamp;
					tm t;
					localtime_s(&t,  &tt);
					if (lastTime.tm_yday == t.tm_yday && lastTime.tm_year == t.tm_year)
						formatDate = Options::instance->messagesShowSec ? _T("s") : _T("t");
				}
			}
				
			tmi.printTimeStamp(NULL, data.timestamp, longFormatDate, str , MAXSELECTSTR, 0);
			std::wstring longDate = str;
			tmi.printTimeStamp(NULL, data.timestamp, formatDate, str , MAXSELECTSTR, 0);
			std::wstring shortDate = str;

			std::wstring user;
			if (lastMe)
				user = myName;
			else
				user = contactName;
				
			GetEventMessage(hDbEvent, str);
			std::wstring strMessage = str;
			if (strMessage.length() + 1 >= MAXSELECTSTR)
				continue;

			if (hDbEvent.isExternal)
				GetExtEventDBei(hDbEvent);

			exp->WriteMessage(lastMe, longDate, shortDate, user, strMessage, gdbei);
		}
	}
}

void ExportManager::DeleteExportedEvents()
{
	for (size_t j = 0; j < eventList.size(); ++j)
		for (size_t i = 0; i < eventList[j].size(); ++i)
			DeleteEvent(eventList[j][i]);
}

void ExportManager::SetDeleteWithoutExportEvents(int _deltaTime, DWORD _now)
{
	exp = NULL;
	deltaTime = _deltaTime;
	now = _now;
	RefreshEventList();
}