summaryrefslogtreecommitdiffstats
path: root/src/modules/theme/themefunctions.cpp
blob: 654f30bf6be74a540a97534e8764eb58518cb858 (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//=============================================================================
//
//   File : themefunctions.cpp
//   Created on Wed 03 Jan 2007 03:14:07 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC Client distribution
//   Copyright (C) 2007 Szymon Stefanek <pragma at kvirc dot net>
//
//   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 opinion) 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. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#include "themefunctions.h"

#include "kvi_packagefile.h"
#include "kvi_locale.h"
#include "kvi_msgbox.h"
#include "kvi_app.h"
#include "kvi_htmldialog.h"
#include "kvi_iconmanager.h"
#include "kvi_miscutils.h"
#include "kvi_sourcesdate.h"
#include "kvi_theme.h"
#include "kvi_frame.h"
#define KviTalMimeSourceFactory TQMimeSourceFactory
#include <tqmime.h>

namespace KviThemeFunctions
{

	static bool notAValidThemePackage(TQString &szError)
	{
		KviTQString::sprintf(szError,__tr2qs_ctx("The selected file does not seem to be a valid KVIrc theme package","theme"));
		return false;
	}

	bool installThemePackage(const TQString &szThemePackageFileName,TQString &szError,TQWidget * pDialogParent)
	{
		KviPointerHashTable<TQString,TQString> * pInfoFields;
		TQString * pValue;
		bool bInstall;
		TQPixmap pix;
		TQByteArray * pByteArray;
		KviHtmlDialogData hd;

		const char * check_fields[] = { "Name", "Version", "Author", "Description", "Date", "Application" };
	
		// check if it is a valid theme file
		KviPackageReader r;
		if(!r.readHeader(szThemePackageFileName))
		{
			TQString szErr = r.lastError();
			KviTQString::sprintf(szError,__tr2qs_ctx("The selected file does not seem to be a valid KVIrc package: %Q","theme"),&szErr);
			return false;
		}
	
		pInfoFields = r.stringInfoFields();
	
		pValue = pInfoFields->find("PackageType");
		if(!pValue)return notAValidThemePackage(szError);
		if(!KviTQString::equalCI(*pValue,"ThemePack"))return notAValidThemePackage(szError);
		pValue = pInfoFields->find("ThemePackVersion");
		if(!pValue)return notAValidThemePackage(szError);
		if(!KviTQString::equalCI(*pValue,"1"))return notAValidThemePackage(szError);
		
		// make sure the default fields exist
		for(int i=0;i<6;i++)
		{
			pValue = pInfoFields->find(check_fields[i]);
			if(!pValue)return notAValidThemePackage(szError);
		}
	
		pValue = pInfoFields->find("ThemeCount");
		if(!pValue)return notAValidThemePackage(szError);
		bool bOk;
		int iThemeCount = pValue->toInt(&bOk);
		if(!bOk)return notAValidThemePackage(szError);
		if(iThemeCount < 1)return notAValidThemePackage(szError);
	
		// ok.. it should be really valid at this point
		
		// load its picture
		pByteArray = r.binaryInfoFields()->find("Image");
		if(pByteArray)
			pix.loadFromData(*pByteArray,0,0);
		
		if(pix.isNull())
		{
			// load the default icon
			pix = *(g_pIconManager->getBigIcon(KVI_BIGICON_THEME));
		}

		TQString szPackageName;
		TQString szPackageVersion;
		TQString szPackageAuthor;
		TQString szPackageDescription;
		TQString szPackageDate;
		TQString szPackageThemeEngineVersion;
		TQString szPackageApplication;

		TQString szAuthor = __tr2qs_ctx("Author","theme");
		TQString szCreatedAt = __tr2qs_ctx("Created at","theme");
		TQString szCreatedOn = __tr2qs_ctx("Created with","theme");

		r.getStringInfoField("Name",szPackageName);
		r.getStringInfoField("Version",szPackageVersion);
		r.getStringInfoField("Author",szPackageAuthor);
		r.getStringInfoField("Description",szPackageDescription);
		r.getStringInfoField("Application",szPackageApplication);
		r.getStringInfoField("Date",szPackageDate);

		TQString szWarnings;
		TQString szDetails = "<html><body bgcolor=\"#ffffff\">";
		TQString szTmp;

		int iIdx = 0;
		int iValidThemeCount = iThemeCount;
		
		while(iIdx < iThemeCount)
		{
			bool bValid = true;
		
			TQString szThemeName;
			TQString szThemeVersion;
			TQString szThemeDescription;
			TQString szThemeDate;
			TQString szThemeSubdirectory;
			TQString szThemeAuthor;
			TQString szThemeThemeEngineVersion;
			TQString szThemeApplication;

			KviTQString::sprintf(szTmp,"Theme%dName",iIdx);
			r.getStringInfoField(szTmp,szThemeName);
			KviTQString::sprintf(szTmp,"Theme%dVersion",iIdx);
			r.getStringInfoField(szTmp,szThemeVersion);
			KviTQString::sprintf(szTmp,"Theme%dApplication",iIdx);
			r.getStringInfoField(szTmp,szThemeApplication);
			KviTQString::sprintf(szTmp,"Theme%dDescription",iIdx);
			r.getStringInfoField(szTmp,szThemeDescription);
			KviTQString::sprintf(szTmp,"Theme%dDate",iIdx);
			r.getStringInfoField(szTmp,szThemeDate);
			KviTQString::sprintf(szTmp,"Theme%dSubdirectory",iIdx);
			r.getStringInfoField(szTmp,szThemeSubdirectory);
			KviTQString::sprintf(szTmp,"Theme%dAuthor",iIdx);
			r.getStringInfoField(szTmp,szThemeAuthor);
			KviTQString::sprintf(szTmp,"Theme%dThemeEngineVersion",iIdx);
			r.getStringInfoField(szTmp,szThemeThemeEngineVersion);
			KviTQString::sprintf(szTmp,"Theme%dScreenshot",iIdx);
			TQPixmap pixScreenshot;
			pByteArray = r.binaryInfoFields()->find(szTmp);
			if(pByteArray)
				pixScreenshot.loadFromData(*pByteArray,0,0);

			if(szThemeName.isEmpty() || szThemeVersion.isEmpty() || szThemeSubdirectory.isEmpty() || szThemeThemeEngineVersion.isEmpty())
				bValid = false;
			if(KviMiscUtils::compareVersions(szThemeThemeEngineVersion,KVI_CURRENT_THEME_ENGINE_VERSION) < 0)
				bValid = false;

			TQString szDetailsBuffer;

			getThemeHtmlDescription(
				szDetailsBuffer,
				szThemeName,
				szThemeVersion,
				szThemeDescription,
				szThemeSubdirectory,
				szThemeApplication,
				szThemeAuthor,
				szThemeDate,
				szThemeThemeEngineVersion,
				pixScreenshot,
				iIdx
			);

			if(iIdx > 0)
				szDetails += "<hr>";

			szDetails += szDetailsBuffer;

			if(!bValid)
			{
				szDetails += "<p><center><font color=\"#ff0000\"><b>";
				szDetails += __tr2qs_ctx("Warning: The theme might be incompatible with this version of KVIrc","theme");
				szDetails += "</b></font></center></p>";
				iValidThemeCount--;
			}

			iIdx++;
		}

		szDetails += "<br><p><center><a href=\"theme_dialog_main\">";
		szDetails +=  __tr2qs_ctx("Go Back to Package Data","theme");
		szDetails += "</a></center></p>";
		szDetails += "</body></html>";

		if(iValidThemeCount < iThemeCount)
		{
			szWarnings += "<p><center><font color=\"#ff0000\"><b>";
			szWarnings += __tr2qs_ctx("Warning: Some of the theme contained in this package might be either corrupted or incompatible with this version of KVIrc","theme");
			szWarnings += "</b></font></center></p>";
		}

		TQString szShowDetails = __tr2qs_ctx("Show Details","theme");

		KviTQString::sprintf(hd.szHtmlText,
			"<html bgcolor=\"#ffffff\">" \
				"<body bgcolor=\"#ffffff\">" \
					"<p><center>" \
						"<h2>%Q %Q</h2>" \
					"</center></p>" \
					"<p><center>" \
						"<img src=\"theme_dialog_pack_image\">" \
					"</center></p>" \
					"<p><center>" \
						"<i>%Q</i>" \
					"</center></p>" \
					"<p><center>" \
						"%Q: <b>%Q</b><br>" \
						"%Q: <b>%Q</b><br>" \
					"</center></p>" \
					"<p><center>" \
						"<font color=\"#808080\">" \
							"%Q: %Q<br>" \
						"</font>" \
					"</center></p>" \
					"%Q" \
					"<br>" \
					"<p><center>" \
						"<a href=\"theme_dialog_details\">%Q</a>" \
					"</center></p>" \
				"</body>" \
			"</html>",
			&szPackageName,
			&szPackageVersion,
			&szPackageDescription,
			&szAuthor,
			&szPackageAuthor,
			&szCreatedAt,
			&szPackageDate,
			&szCreatedOn,
			&szPackageApplication,
			&szWarnings,
			&szShowDetails
		);

		

		KviTalMimeSourceFactory::defaultFactory()->setPixmap("theme_dialog_pack_image",pix);
		KviTalMimeSourceFactory::defaultFactory()->setText("theme_dialog_details",szDetails);
		KviTalMimeSourceFactory::defaultFactory()->setText("theme_dialog_main",hd.szHtmlText);
	
		TQString beginCenter = "<center>";
		TQString endCenter = "</center>";

		hd.szCaption = __tr2qs_ctx("Install Theme Pack - KVIrc","theme");
		hd.szUpperLabelText = beginCenter + __tr2qs_ctx("You're about to install the following theme package","theme") + endCenter;
		hd.szLowerLabelText = beginCenter + __tr2qs_ctx("Do you want to proceed with the installation ?","theme") + endCenter;
		hd.szButton1Text = __tr2qs_ctx("Do Not Install","theme");
		hd.szButton2Text = __tr2qs_ctx("Yes, Proceed","theme");
		hd.iDefaultButton = 2;
		hd.iCancelButton = 1;
		hd.pixIcon = *(g_pIconManager->getSmallIcon(KVI_SMALLICON_THEME));
		hd.iMinimumWidth = 350;
		hd.iMinimumHeight = 420;
		hd.iFlags = KviHtmlDialogData::ForceMinimumSize;

		bInstall = KviHtmlDialog::display(pDialogParent,&hd) == 2;
		
		if(bInstall)
		{
			TQString szUnpackPath;
			g_pApp->getLocalKvircDirectory(szUnpackPath,KviApp::Themes);
			if(!r.unpack(szThemePackageFileName,szUnpackPath))
			{
				TQString szErr2 = r.lastError();
				KviTQString::sprintf(szError,__tr2qs_ctx("Failed to unpack the selected file: %Q","theme"),&szErr2);
				return true;
			}
		}

		return true;
	}


	void getThemeHtmlDescription(
		TQString &szBuffer,
		const TQString &szThemeName,
		const TQString &szThemeVersion,
		const TQString &szThemeDescription,
		const TQString &szThemeSubdirectory,
		const TQString &szThemeApplication,
		const TQString &szThemeAuthor,
		const TQString &szThemeDate,
		const TQString &szThemeThemeEngineVersion,
		const TQPixmap &pixScreenshot,
		int iUniqueIndexInDocument
	)
	{
		TQString szAuthor = __tr2qs_ctx("Author","theme");
		TQString szCreatedAt = __tr2qs_ctx("Created at","theme");
		TQString szCreatedOn = __tr2qs_ctx("Created with","theme");
		TQString szThemeEngineVersion = __tr2qs_ctx("Theme Engine Version","theme");
		TQString szSubdirectory = __tr2qs_ctx("Subdirectory","theme");

		TQString szScreenshot;
		if(!pixScreenshot.isNull())
		{
			KviTQString::sprintf(szScreenshot,"<p><center><img src=\"theme_shot%d\"></center></p>",iUniqueIndexInDocument);
			TQString szTmp;
			KviTQString::sprintf(szTmp,"theme_shot%d",iUniqueIndexInDocument);
			KviTalMimeSourceFactory::defaultFactory()->setPixmap(szTmp,pixScreenshot);
		} else {
			szScreenshot = "";
		}

		KviTQString::sprintf(
			szBuffer,
			"<p><center>" \
				"<h2>%Q %Q</h2>" \
			"</center></p>" \
			"%Q" \
			"<p><center>" \
				"<i>%Q</i>" \
			"</center></p>" \
			"<p><center>" \
				"%Q: <b>%Q</b><br>" \
				"%Q: <b>%Q</b><br>" \
			"</center></p>" \
			"<p><center>" \
				"<font color=\"#808080\">" \
					"%Q: %Q<br>" \
					"%Q: %Q<br>" \
					"%Q: %Q<br>" \
				"</font>" \
			"</center></p>",
			&szThemeName,
			&szThemeVersion,
			&szScreenshot,
			&szThemeDescription,
			&szAuthor,
			&szThemeAuthor,
			&szCreatedAt,
			&szThemeDate,
			&szCreatedOn,
			&szThemeApplication,
			&szThemeEngineVersion,
			&szThemeThemeEngineVersion,
			&szSubdirectory,
			&szThemeSubdirectory
		);
	}

	bool makeKVIrcScreenshot(const TQString &szSavePngFilePath,bool bMaximizeFrame)
	{
		if(bMaximizeFrame)
		{
			if(g_pFrame->isMaximized())
				bMaximizeFrame = false;
		}
	
		if(bMaximizeFrame)
			g_pFrame->showMaximized();
		
		TQPixmap pix = TQPixmap::grabWidget(g_pFrame);
		bool bResult = true;
		
		if(pix.isNull())
			bResult = false;
		else {
			if(!pix.save(szSavePngFilePath,"PNG",100))
				bResult = false;
		}

		if(bMaximizeFrame)
			g_pFrame->showNormal();
		return bResult;
	}
};