aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/addon/PackAddonDialog.cpp
blob: 4b86e1fd983892d2808f509e2cf3d64c7c45346e (about) (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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//============================================================================
//
//   File : PackAddonDialog.cpp
//   Creation date : Sat 03 May 2008 01:40:44 by Elvio Basello
//
//   This file is part of the KVIrc IRC Client distribution
//   Copyright (C) 2008-2009 Elvio Basello <hellvis69 at netsons dot org>
//
//   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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//============================================================================

#include "PackAddonDialog.h"
#include "AddonFunctions.h"

#include "KviLocale.h"
#include "KviFileUtils.h"
#include "KviApplication.h"
#include "KviIconManager.h"
#include "KviPackageWriter.h"
#include "kvi_fileextensions.h"
#include "kvi_sourcesdate.h"
#include "KviOptions.h"

#include <QTextEdit>
#include <QLineEdit>
#include <QLabel>
#include <QMessageBox>
#include <QDir>
#include <QDirIterator>

PackAddonDialog::PackAddonDialog(QWidget * pParent)
    : QWizard(pParent)
{
	setMinimumSize(400, 350);
	setObjectName("addon_package_wizard");
	setOption(QWizard::IndependentPages, true);
	setWindowTitle(__tr2qs_ctx("Create Addon Package - KVIrc", "addon"));

	QPixmap * pSide = g_pIconManager->getBigIcon("kvi_setup_label.png");
	QPixmap * pLogo = g_pIconManager->getBigIcon("kvi_bigicon_addons.png");

#if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
#if(_WIN32_WINNT >= 0x0600)
	// We are on a Windows Vista / Seven
	// It will fallback to XP if alpha compositing is disabled
	setWizardStyle(QWizard::AeroStyle);
#else
	// Windows XP
	setWizardStyle(QWizard::ModernStyle);
	setPixmap(QWizard::WatermarkPixmap, *pSide);
	setPixmap(QWizard::LogoPixmap, *pLogo);
#endif
#elif defined(COMPILE_ON_MAC)
	setWizardStyle(QWizard::MacStyle);
	setPixmap(QWizard::BackgroundPixmap, *pSide);
#else
	// All other systems
	setWizardStyle(QWizard::ClassicStyle);
	setPixmap(QWizard::WatermarkPixmap, *pSide);
	setPixmap(QWizard::LogoPixmap, *pLogo);
#endif

	// Add a default property for file selectors
	setDefaultProperty("KviFileSelector", "tmpFile", SIGNAL(selectionChanged(const QString &)));

	// Welcome page
	QWizardPage * pPage = new QWizardPage(this);
	QVBoxLayout * pLayout = new QVBoxLayout(pPage);

	pPage->setLayout(pLayout);
	pPage->setTitle(__tr2qs_ctx("Welcome", "addon"));

	QLabel * pLabel = new QLabel(pPage);
	pLabel->setWordWrap(true);
	pLabel->setText(__tr2qs_ctx("This procedure allows you to export the selected addon to a single package.<br>It is useful when you want to distribute your addon to the public.", "addon"));
	pLayout->addWidget(pLabel);

	pLabel = new QLabel(pPage);
	pLabel->setWordWrap(true);
	QString szText;
	szText += __tr2qs_ctx("You will be asked to provide some information like the package name, the version, a description and so on.", "addon");
	szText += "<br><br>";
	szText += __tr2qs_ctx("Hit the \"Next\" button to begin.", "addon");
	pLabel->setText(szText);
	pLayout->addWidget(pLabel);

	pPage->setMinimumWidth(350);

	addPage(pPage);

	// Packager information
	m_pPackAddonInfoPackageWidget = new PackAddonInfoPackageWidget(this);
	addPage(m_pPackAddonInfoPackageWidget);

	// File selection
	m_pPackAddonFileSelectionWidget = new PackAddonFileSelectionWidget(this);
	addPage(m_pPackAddonFileSelectionWidget);

	// Save selection
	m_pPackAddonSaveSelectionWidget = new PackAddonSaveSelectionWidget(this);
	addPage(m_pPackAddonSaveSelectionWidget);

	// Summary info
	m_pPackAddonSummaryInfoWidget = new PackAddonSummaryInfoWidget(this);
	addPage(m_pPackAddonSummaryInfoWidget);
}

PackAddonDialog::~PackAddonDialog()
    = default;

void PackAddonDialog::accept()
{
	if(!packAddon())
		return;
	QWizard::accept();
}

bool PackAddonDialog::packAddon()
{
	// Get data from registered fields
	AddonInfo info;
	info.szAuthor = field("packageAuthor").toString();
	info.szName = field("packageName").toString();
	info.szVersion = field("packageVersion").toString();
	info.szDescription = field("packageDescription").toString();
	info.szMinVersion = field("packageMinVersion").toString();
	info.szImage = field("packageImage").toString();
	info.szDirPath = field("packageDirPath").toString();
	info.szSavePath = field("packageSavePath").toString();

	if(info.szSavePath.isEmpty())
	{
		info.szSavePath = QDir::homePath();
		KviQString::ensureLastCharIs(info.szSavePath, QChar(KVI_PATH_SEPARATOR_CHAR));
		info.szSavePath += m_szName;
		info.szSavePath += "-";
		info.szSavePath += m_szVersion;
		info.szSavePath += KVI_FILEEXTENSION_ADDONPACKAGE;
	}

	if(QFile::exists(info.szSavePath))
	{
		if(QMessageBox::question(
		       this,
		       __tr2qs_ctx("Exporting Addon Confirmation - KVIrc", "addon"),
		       __tr2qs_ctx("File %1 already exists. Do you want to overwrite it?", "addon").arg(info.szSavePath))
		    == QMessageBox::No)
			return false;
	}

	// Raise the files summary dialog
	m_pPackAddonSummaryFilesWidget = new PackAddonSummaryFilesWidget(this);
	m_pPackAddonSummaryFilesWidget->setPath(info.szDirPath);
	if(m_pPackAddonSummaryFilesWidget->exec() == QDialog::Rejected)
	{
		delete m_pPackAddonSummaryFilesWidget;
		return false;
	}

	QString szError;

	if(!AddonFunctions::pack(info, szError))
	{
		QMessageBox::critical(this,
		    __tr2qs_ctx("Addon Packaging - KVIrc", "addon"), szError);
		return false;
	}

	QMessageBox::information(this, __tr2qs_ctx("Exporting Addon Completed - KVIrc", "addon"), __tr2qs_ctx("The package was saved successfully in %1", "addon").arg(info.szSavePath));

	return true;
}

PackAddonInfoPackageWidget::PackAddonInfoPackageWidget(PackAddonDialog * pParent)
    : QWizardPage(pParent)
{
	setObjectName("addon_package_info_page");
	setTitle(__tr2qs_ctx("Package Information", "addon"));
	setSubTitle(__tr2qs_ctx("Here you need to provide information about you (the packager) and a short description of the package you're creating.", "addon"));

	QGridLayout * pLayout = new QGridLayout(this);

	QLabel * pLabel = new QLabel(this);
	pLabel->setText(__tr2qs_ctx("Package &author:", "addon"));
	pLayout->addWidget(pLabel, 0, 0);

	m_pPackageAuthorEdit = new QLineEdit(this);
	pLabel->setBuddy(m_pPackageAuthorEdit);
	pLayout->addWidget(m_pPackageAuthorEdit, 0, 1);

	pLabel = new QLabel(this);
	pLabel->setText(__tr2qs_ctx("Package &name:", "addon"));
	pLayout->addWidget(pLabel, 1, 0);

	m_pPackageNameEdit = new QLineEdit(this);
	m_pPackageNameEdit->setPlaceholderText(__tr2qs_ctx("No spaces allowed", "addon"));
	pLabel->setBuddy(m_pPackageNameEdit);
	pLayout->addWidget(m_pPackageNameEdit, 1, 1);

	pLabel = new QLabel(this);
	pLabel->setText(__tr2qs_ctx("Package &version:", "addon"));
	pLayout->addWidget(pLabel, 2, 0);

	m_pPackageVersionEdit = new QLineEdit(this);
	m_pPackageVersionEdit->setPlaceholderText("1.0");
	pLabel->setBuddy(m_pPackageVersionEdit);
	pLayout->addWidget(m_pPackageVersionEdit, 2, 1);

	pLabel = new QLabel(this);
	pLabel->setText(__tr2qs_ctx("Package &description:", "addon"));
	pLayout->addWidget(pLabel, 3, 0);

	m_pPackageDescriptionEdit = new QLineEdit(this);
	m_pPackageDescriptionEdit->setText(QString());
	pLabel->setBuddy(m_pPackageDescriptionEdit);
	pLayout->addWidget(m_pPackageDescriptionEdit, 3, 1);

	pLabel = new QLabel(this);
	pLabel->setText(__tr2qs_ctx("Minimum &KVIrc version:", "addon"));
	pLayout->addWidget(pLabel, 4, 0);

	m_pPackageMinVersionEdit = new QLineEdit(this);
	m_pPackageMinVersionEdit->setText(KVI_VERSION);
	pLabel->setBuddy(m_pPackageMinVersionEdit);
	pLayout->addWidget(m_pPackageMinVersionEdit, 4, 1);

	// Store data in the fields
	registerField("packageAuthor*", m_pPackageAuthorEdit);
	registerField("packageName*", m_pPackageNameEdit);
	registerField("packageVersion*", m_pPackageVersionEdit);
	registerField("packageDescription*", m_pPackageDescriptionEdit);
	registerField("packageMinVersion", m_pPackageMinVersionEdit);
}

PackAddonInfoPackageWidget::~PackAddonInfoPackageWidget()
    = default;

PackAddonFileSelectionWidget::PackAddonFileSelectionWidget(PackAddonDialog * pParent)
    : QWizardPage(pParent)
{
	setObjectName("addon_package_file_page");
	setTitle(__tr2qs_ctx("Package Files", "addon"));
	setSubTitle(__tr2qs_ctx("Here you need to select the directory where the addon files to be packed are located.", "addon"));

	QVBoxLayout * pLayout = new QVBoxLayout(this);

	// Select source directory
	m_pDirPathSelector = new KviDirectorySelector(this, __tr2qs_ctx("Select the source directory:", "addon"), &m_szDirPath, true, KviFileSelector::VerticalLayout);
	pLayout->addWidget(m_pDirPathSelector);

	// Select addon icon
	m_pPackageImageEdit = new KviFileSelector(this, __tr2qs_ctx("Select the image file:", "addon"), &m_szPackageImage, true, KviFileSelector::VerticalLayout, KVI_FILTER_IMAGE);
	pLayout->addWidget(m_pPackageImageEdit);

	// Store data in the fields
	registerField("packageDirPath*", m_pDirPathSelector);
	registerField("packageImage*", m_pPackageImageEdit);
}

PackAddonFileSelectionWidget::~PackAddonFileSelectionWidget()
    = default;

PackAddonSaveSelectionWidget::PackAddonSaveSelectionWidget(PackAddonDialog * pParent)
    : QWizardPage(pParent)
{
	setObjectName("addon_package_save_page");
	setTitle(__tr2qs_ctx("Save Package", "addon"));
	setSubTitle(__tr2qs_ctx("Here you need to provide the path where to save the created addon package", "addon"));

	QVBoxLayout * pLayout = new QVBoxLayout(this);

	// Select save path
	m_pSavePathSelector = new KviFileSelector(
	    this,
	    __tr2qs_ctx("Select save path:", "addon"),
	    &m_szFilePath,
	    true,
	    KviFileSelector::ChooseSaveFileName | KviFileSelector::VerticalLayout,
	    KVI_FILTER_ADDON);
	pLayout->addWidget(m_pSavePathSelector);

	// Store data in the fields
	registerField("packageSavePath*", m_pSavePathSelector);
}

PackAddonSaveSelectionWidget::~PackAddonSaveSelectionWidget()
    = default;

void PackAddonSaveSelectionWidget::initializePage()
{
	// Get data from registered fields
	QString szName = field("packageName").toString();
	QString szVersion = field("packageVersion").toString();

	// Create addon name
	QString szSavePath = QDir::homePath();
	KviQString::ensureLastCharIs(szSavePath, QChar(KVI_PATH_SEPARATOR_CHAR));
	szSavePath += szName;
	szSavePath += "-";
	szSavePath += szVersion;
	szSavePath += KVI_FILEEXTENSION_ADDONPACKAGE;
	m_pSavePathSelector->setTmpFile(szSavePath);
}

PackAddonSummaryInfoWidget::PackAddonSummaryInfoWidget(PackAddonDialog * pParent)
    : QWizardPage(pParent)
{
	setObjectName("addon_package_summary_info_page");
	setTitle(__tr2qs_ctx("Final Information", "addon"));
	setSubTitle(__tr2qs_ctx("Here are the details you provided. If this information is correct, hit the \"Finish\" button to complete the packaging operations.", "addon"));

	QVBoxLayout * pLayout = new QVBoxLayout(this);
	m_pLabelInfo = new QLabel(this);
	pLayout->addWidget(m_pLabelInfo);
}

PackAddonSummaryInfoWidget::~PackAddonSummaryInfoWidget()
    = default;

void PackAddonSummaryInfoWidget::initializePage()
{
	// Get data from registered fields
	QString szAuthor = field("packageAuthor").toString();
	QString szName = field("packageName").toString();
	QString szVersion = field("packageVersion").toString();
	QString szDescription = field("packageDescription").toString();
	QString szMinVersion = field("packageMinVersion").toString();
	QString szImage = field("packageImage").toString();
	QString szDirPath = field("packageDirPath").toString();
	QString szSavePath = field("packageSavePath").toString();

	QString szText = __tr2qs_ctx("This is what I will check for:", "addon");
	szText += "<br><br><b>";
	szText += __tr2qs_ctx("Package author", "addon");
	szText += ":</b> ";
	szText += szAuthor;
	szText += "<br><b>";
	szText += __tr2qs_ctx("Package name", "addon");
	szText += ":</b> ";
	szText += szName;
	szText += "<br><b>";
	szText += __tr2qs_ctx("Package version", "addon");
	szText += ":</b> ";
	szText += szVersion;
	szText += "<br><b>";
	szText += __tr2qs_ctx("Package description", "addon");
	szText += ":</b> ";
	szText += szDescription;
	szText += "<br><b>";
	szText += __tr2qs_ctx("Minimum KVIrc version", "addon");
	szText += ":</b> ";
	szText += szMinVersion;
	szText += "<br><b>";
	szText += __tr2qs_ctx("Image file", "addon");
	szText += ":</b> ";
	szText += szImage;
	szText += "<br><b>";
	szText += __tr2qs_ctx("Source directory", "addon");
	szText += ":</b> ";
	szText += szDirPath;
	szText += "<br><b>";
	szText += __tr2qs_ctx("Save path", "addon");
	szText += ":</b> ";
	szText += szSavePath;

	m_pLabelInfo->setText(szText);
}

PackAddonSummaryFilesWidget::PackAddonSummaryFilesWidget(PackAddonDialog * pParent)
    : QDialog(pParent)
{
	setObjectName("addon_package_summary_file_dialog");
	setWindowTitle(__tr2qs_ctx("File Summary Review - KVIrc", "addon"));
	setWindowModality(Qt::WindowModal);
	setModal(true);

	QVBoxLayout * pLayout = new QVBoxLayout(this);

	QLabel * pLabel = new QLabel(this);
	pLabel->setText(__tr2qs_ctx("Here are the files found in the directories you provided.\nIf the files listed below are correct, hit the \"Finish\" button to complete the packaging operation.", "addon"));
	pLayout->addWidget(pLabel);

	m_pFiles = new QTextEdit(this);
	m_pFiles->setReadOnly(true);
	pLayout->addWidget(m_pFiles);

	KviTalHBox * pBox = new KviTalHBox(this);
	QPushButton * pCancel = new QPushButton(pBox);
	pCancel->setText(__tr2qs_ctx("Cancel", "addon"));
	connect(pCancel, SIGNAL(clicked()), this, SLOT(reject()));

	QPushButton * pAccept = new QPushButton(pBox);
	pAccept->setText(__tr2qs_ctx("Finish", "addon"));
	connect(pAccept, SIGNAL(clicked()), this, SLOT(accept()));
	pLayout->addWidget(pBox);
}

PackAddonSummaryFilesWidget::~PackAddonSummaryFilesWidget()
    = default;

void PackAddonSummaryFilesWidget::accept()
{
	QDialog::accept();
}

void PackAddonSummaryFilesWidget::reject()
{
	QDialog::reject();
}

void PackAddonSummaryFilesWidget::showEvent(QShowEvent *)
{
	QStringList list;

	QDirIterator it(m_szPath, QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);

	// Iterate through the addon dir and its subdirs
	while(it.hasNext())
	{
		QString szStr = it.next();
		if(!szStr.contains("/."))
			list.append(szStr);
	}

	// QDirIterator does not support sorting, so do it manually
	list.sort();

	m_pFiles->setPlainText(list.join("\n"));
}