summaryrefslogtreecommitdiffstats
path: root/src/kile/managetemplatesdialog.cpp
blob: d73a435bbf58084b55fe2271447fa010961ccafc (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
/*****************************************************************************************
    begin                : Sun Apr 27 2003
    copyright            : (C) 2003 by Jeroen Wijnhout (wijnhout@science.uva.nl)
                               2007 by Michel Ludwig (michel.ludwig@kdemail.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 option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "managetemplatesdialog.h"

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqtooltip.h>

#include <kapp.h>
#include <klocale.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <kpushbutton.h>
#include <kicondialog.h>
#include <kmessagebox.h>
#include "kiledebug.h"
#include <kio/netaccess.h>

#include "kileextensions.h"
#include "kileinfo.h"
#include "templates.h"

class TemplateListViewItem : public TQListViewItem {
	public:
		TemplateListViewItem(TQListView* listView, TQListViewItem* previousItem, const TQString& mode, const KileTemplate::Info& info) : TQListViewItem(listView, previousItem, mode, info.name, KileInfo::documentTypeToString(info.type)), m_info(info) {
		}

		virtual ~TemplateListViewItem() {
		}

		KileTemplate::Info getTemplateInfo() {
			return m_info;
		}

	protected:
		KileTemplate::Info m_info;
};

// dialog to create a template
ManageTemplatesDialog::ManageTemplatesDialog(KileTemplate::Manager *templateManager, const KURL& sourceURL, const TQString &caption, TQWidget *tqparent, const char *name ) : KDialogBase(tqparent,name,true,caption,KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true), m_templateManager(templateManager), m_sourceURL(sourceURL) {

	m_templateType = KileDocument::Extensions().determineDocumentType(sourceURL);

	TQWidget *page = new TQWidget(this , "managetemplates_mainwidget");
	setMainWidget(page);
	TQVBoxLayout *topLayout = new TQVBoxLayout(page, 0, spacingHint());

	TQHBoxLayout *nameLayout = new TQHBoxLayout(topLayout, spacingHint());
	nameLayout->addWidget(new TQLabel(i18n("Name:"),page));

	TQString fileName = m_sourceURL.fileName();
	//remove the extension
	int dotPos = fileName.tqfindRev('.');
	if(dotPos >= 0) {
		fileName = fileName.mid(0, dotPos);
	}
	m_nameEdit = new KLineEdit(fileName, page);
	nameLayout->addWidget(m_nameEdit);

	nameLayout->addWidget(new TQLabel(i18n("Type: %1").tqarg(KileInfo::documentTypeToString(m_templateType)), page));

	TQHBoxLayout *iconLayout = new TQHBoxLayout(topLayout, spacingHint());
	iconLayout->addWidget(new TQLabel(i18n("Icon:"), page));

	m_iconEdit = new KLineEdit(KGlobal::dirs()->findResource("appdata", "pics/type_Default.png"), page);
	iconLayout->addWidget(m_iconEdit);

	KPushButton *iconbut = new KPushButton(i18n("Select..."),page);
	iconLayout->addWidget(iconbut);

	m_templateList = new KListView(page);
	m_templateList->setSorting(-1);
	m_templateList->addColumn(i18n("marked", "M"));
	m_templateList->addColumn(i18n("Existing Templates"));
	m_templateList->addColumn(i18n("Document Type"));
	m_templateList->setColumnWidthMode(0, TQListView::Manual);
	m_templateList->setFullWidth(true);
	m_templateList->setAllColumnsShowFocus(true);

	populateTemplateListView(m_templateType);

	topLayout->addWidget(m_templateList);

	TQHBoxLayout *controlLayout = new TQHBoxLayout(topLayout, spacingHint());
	m_showAllTypesCheckBox = new TQCheckBox(i18n("Show all the templates"), page);
	m_showAllTypesCheckBox->setChecked(false);
	connect(m_showAllTypesCheckBox, TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(updateTemplateListView(bool)));
	controlLayout->addWidget(m_showAllTypesCheckBox);

	controlLayout->addStretch();

	KPushButton *clearSelectionButton = new KPushButton(page);
	clearSelectionButton->setPixmap(SmallIcon("clear_left.png"));
	TQToolTip::add(clearSelectionButton, i18n("Clear Selection"));
	connect(clearSelectionButton, TQT_SIGNAL(clicked()),this, TQT_SLOT(clearSelection()));
	controlLayout->addWidget(clearSelectionButton);

	topLayout->addWidget( new TQLabel(i18n("Select an existing template if you want to overwrite it with your new template.\nNote that you cannot overwrite templates marked with an asterisk:\nif you do select such a template, a new template with the same name\nwill be created in a location you have write access to."),page));

	connect(m_templateList, TQT_SIGNAL(selectionChanged(TQListViewItem*)), this, TQT_SLOT(slotSelectedTemplate(TQListViewItem*)));
	connect(iconbut, TQT_SIGNAL(clicked()),this, TQT_SLOT(slotSelectIcon()));
	connect(this, TQT_SIGNAL(aboutToClose()), this, TQT_SLOT(addTemplate()));
}

// dialog to remove a template
ManageTemplatesDialog::ManageTemplatesDialog(KileTemplate::Manager *templateManager, const TQString &caption, TQWidget *tqparent, const char *name ) : KDialogBase(tqparent,name,true,caption,KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true), m_templateManager(templateManager), m_templateType(KileDocument::Undefined), m_showAllTypesCheckBox(NULL)
{
	TQWidget *page = new TQWidget(this, "managetemplates_mainwidget");
	setMainWidget(page);
	TQVBoxLayout *topLayout = new TQVBoxLayout(page, 0, spacingHint());

	m_templateList = new KListView(page);
	m_templateList->setSorting(-1);
	m_templateList->addColumn(i18n("marked", "M"));
	m_templateList->addColumn(i18n("Existing Templates"));
	m_templateList->addColumn(i18n("Document Type"));
	m_templateList->setColumnWidthMode(0, TQListView::Manual);
	m_templateList->setFullWidth(true);
	m_templateList->setAllColumnsShowFocus(true);

	populateTemplateListView(KileDocument::Undefined);

	topLayout->addWidget(m_templateList);
	topLayout->addWidget( new TQLabel(i18n("Please select the template that you want to remove.\nNote that you cannot delete templates marked with an asterisk (for which you lack the necessary deletion permissions)."),page));

	connect(this, TQT_SIGNAL(aboutToClose()), this, TQT_SLOT(removeTemplate()));
}

ManageTemplatesDialog::~ManageTemplatesDialog(){
}

void ManageTemplatesDialog::updateTemplateListView(bool showAllTypes) {
	populateTemplateListView((showAllTypes ? KileDocument::Undefined : m_templateType));
}

void ManageTemplatesDialog::clearSelection() {
	m_templateList->clearSelection();
}

void ManageTemplatesDialog::slotOk() {
	emit aboutToClose();
}

void ManageTemplatesDialog::populateTemplateListView(KileDocument::Type type) {
	m_templateManager->scanForTemplates();
	KileTemplate::TemplateList templateList = m_templateManager->getTemplates(type);
	TQString mode;
	TQListViewItem* previousItem = NULL;

	m_templateList->clear();
	for (KileTemplate::TemplateListIterator i = templateList.begin(); i != templateList.end(); ++i)
	{
		KileTemplate::Info info = *i;
		TQFileInfo iconFileInfo(info.icon);
		mode = (TQFileInfo(info.path).isWritable() && (!iconFileInfo.exists() || iconFileInfo.isWritable())) ? " " : "*";
		if((type == KileDocument::Undefined) || (info.type == type)) { 
			previousItem = new TemplateListViewItem(m_templateList, previousItem, mode, info);
		}
	}
}

void ManageTemplatesDialog::slotSelectedTemplate(TQListViewItem *item) {
	TemplateListViewItem *templateItem = dynamic_cast<TemplateListViewItem*>(item);
	if(templateItem) {
		KileTemplate::Info info = templateItem->getTemplateInfo();
		m_nameEdit->setText(info.name);
		m_iconEdit->setText(info.icon);
	}
}

void ManageTemplatesDialog::slotSelectIcon() {
	KIconDialog *dlg = new KIconDialog();
	TQString res = dlg->openDialog();
	KIconLoader kil;

	if (!res.isNull() ) {
		m_iconEdit->setText(kil.iconPath(res,-KIcon::SizeLarge, false));
	}
}

void ManageTemplatesDialog::addTemplate() {

	TQString templateName = (m_nameEdit->text()).stripWhiteSpace();

	if(templateName.isEmpty()) {
		KMessageBox::error(this, i18n("Sorry, but the template name that you have entered is invalid.\nPlease enter a new name."));
		return;
	}

	TQString icon = (m_iconEdit->text()).stripWhiteSpace();
	KURL iconURL = KURL::fromPathOrURL(icon);

	if (icon.isEmpty()) {
		KMessageBox::error(this, i18n("Please choose an icon first."));
		return;
	}

	if (!KIO::NetAccess::exists(iconURL, true, kapp->mainWidget())) {
		KMessageBox::error(this, i18n("Sorry, but the icon file: %1\ndoes not seem to exist. Please choose a new icon.").tqarg(icon));
		return;
	}

	if (!KIO::NetAccess::exists(m_sourceURL, true, kapp->mainWidget())) {
		KMessageBox::error(this, i18n("Sorry, but the file: %1\ndoes not seem to exist. Maybe you forgot to save the file?").tqarg(m_sourceURL.prettyURL()));
		return;
	}

	TQListViewItem* item = m_templateList->selectedItem();

	if(!item && m_templateManager->searchForTemplate(templateName, m_templateType)) {
		KMessageBox::error(this, i18n("Sorry, but a template named \"%1\" already exists.\nPlease remove it first.").tqarg(templateName));
		return;
	}

	bool returnValue;
	if(item) {
		TemplateListViewItem *templateItem = dynamic_cast<TemplateListViewItem*>(item);
		Q_ASSERT(templateItem);
		KileTemplate::Info templateInfo = templateItem->getTemplateInfo();
		if (KMessageBox::warningYesNo(this, i18n("You are about to replace the template \"%1\"; are you sure?").tqarg(templateInfo.name)) == KMessageBox::No) {
			reject();
			return;
		}
		returnValue = m_templateManager->tqreplace(templateInfo, m_sourceURL, templateName, iconURL);
	}
	else {
		returnValue = m_templateManager->add(m_sourceURL, templateName, iconURL);
	}
	if (!returnValue) {
		KMessageBox::error(this, i18n("Failed to create the template."));
		reject();
		return;
	}
	accept();
}

bool ManageTemplatesDialog::removeTemplate()
{
	TQListViewItem* item = m_templateList->selectedItem();
	if(!item) {
		KMessageBox::information(this, i18n("Please select a template that should be removed."));
		return true;
	}

	TemplateListViewItem *templateItem = dynamic_cast<TemplateListViewItem*>(item);
	Q_ASSERT(templateItem);
	
	KileTemplate::Info templateInfo = templateItem->getTemplateInfo();

	if (!(KIO::NetAccess::exists(KURL::fromPathOrURL(templateInfo.path), false, kapp->mainWidget()) && (KIO::NetAccess::exists(KURL::fromPathOrURL(templateInfo.icon), false, kapp->mainWidget()) || !TQFileInfo(templateInfo.icon).exists()))) {
		KMessageBox::error(this, i18n("Sorry, but you do not have the necessary permissions to remove the selected template."));
		return false;
	}

	if (KMessageBox::warningYesNo(this, i18n("You are about to remove the template \"%1\"; are you sure?").tqarg(templateInfo.name)) == KMessageBox::No) {
		return false;
	}

	if (!m_templateManager->remove(templateInfo))
	{
		KMessageBox::error(this, i18n("Sorry, but the template could not be removed."));
		reject();
		return false;
	}
	accept();
	return true;
}

#include "managetemplatesdialog.moc"