diff --git a/src/dialogs/managetemplatesdialog.cpp b/src/dialogs/managetemplatesdialog.cpp index c5298b21..77d5d6ff 100644 --- a/src/dialogs/managetemplatesdialog.cpp +++ b/src/dialogs/managetemplatesdialog.cpp @@ -1,330 +1,329 @@ /***************************************************************************************** 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 "dialogs/managetemplatesdialog.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "kiledebug.h" #include "kileextensions.h" #include "kileinfo.h" #include "templates.h" #include "utilities.h" class TemplateListViewItem : public QTreeWidgetItem { public: TemplateListViewItem(QTreeWidget* parent, QTreeWidgetItem* preceding, const QString& mode, const KileTemplate::Info& info) : QTreeWidgetItem(parent, preceding), m_info(info) { setText(0, mode); setText(1, info.name); setText(2, KileInfo::documentTypeToString(info.type)); } virtual ~TemplateListViewItem() { } KileTemplate::Info getTemplateInfo() { return m_info; } protected: KileTemplate::Info m_info; }; // dialog to create a template ManageTemplatesDialog::ManageTemplatesDialog(KileTemplate::Manager *templateManager, const QUrl &sourceURL, const QString &caption, QWidget *parent, const char *name) : QDialog(parent) , m_templateManager(templateManager) , m_sourceURL(sourceURL) { setObjectName(name); setWindowTitle(caption); setModal(true); QVBoxLayout *mainLayout = new QVBoxLayout; setLayout(mainLayout); m_templateType = KileDocument::Extensions().determineDocumentType(sourceURL); QWidget *page = new QWidget(this); page->setObjectName("managetemplates_mainwidget"); mainLayout->addWidget(page); QGridLayout *topLayout = new QGridLayout(); topLayout->setContentsMargins(0, 0, 0, 0); page->setLayout(topLayout); topLayout->addWidget(new QLabel(i18n("Name:"), page), 0, 0); QString fileName = m_sourceURL.fileName(); //remove the extension int dotPos = fileName.lastIndexOf('.'); if (dotPos >= 0) { fileName = fileName.mid(0, dotPos); } m_nameEdit = new QLineEdit(fileName, page); mainLayout->addWidget(m_nameEdit); topLayout->addWidget(m_nameEdit, 0, 1); topLayout->addWidget(new QLabel(i18n("Type: %1", KileInfo::documentTypeToString(m_templateType)), page), 0, 2); topLayout->addWidget(new QLabel(i18n("Icon:"), page), 1, 0); m_iconEdit = new QLineEdit(KileUtilities::locate(QStandardPaths::AppDataLocation, "pics/type_Default.png"), page); mainLayout->addWidget(m_iconEdit); topLayout->addWidget(m_iconEdit, 1, 1); QPushButton *iconbut = new QPushButton(i18n("Select..."), page); mainLayout->addWidget(iconbut); topLayout->addWidget(iconbut, 1, 2); m_templateList = new QTreeWidget(page); mainLayout->addWidget(m_templateList); m_templateList->setSortingEnabled(false); m_templateList->setHeaderLabels(QStringList() << i18nc("marked", "M") << i18n("Existing Templates") << i18n("Document Type")); m_templateList->setAllColumnsShowFocus(true); m_templateList->setRootIsDecorated(false); populateTemplateListView(m_templateType); topLayout->addWidget(m_templateList, 2, 0, 1, 3); m_showAllTypesCheckBox = new QCheckBox(i18n("Show all the templates"), page); mainLayout->addWidget(m_showAllTypesCheckBox); m_showAllTypesCheckBox->setChecked(false); connect(m_showAllTypesCheckBox, &QCheckBox::toggled, this, &ManageTemplatesDialog::updateTemplateListView); topLayout->addWidget(m_showAllTypesCheckBox, 3, 0, 1, 2); QPushButton *clearSelectionButton = new QPushButton(page); mainLayout->addWidget(clearSelectionButton); clearSelectionButton->setIcon(QIcon::fromTheme("edit-clear-locationbar")); int buttonSize = clearSelectionButton->sizeHint().height(); clearSelectionButton->setFixedSize(buttonSize, buttonSize); clearSelectionButton->setToolTip(i18n("Clear Selection")); connect(clearSelectionButton, &QPushButton::clicked, this, &ManageTemplatesDialog::clearSelection); topLayout->addWidget(clearSelectionButton, 3, 2, Qt::AlignRight); topLayout->addWidget(new QLabel(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), 4, 0, 1, 3); QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); okButton->setDefault(true); okButton->setShortcut(Qt::CTRL | Qt::Key_Return); mainLayout->addWidget(buttonBox); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(m_templateList, &QTreeWidget::itemClicked, this, &ManageTemplatesDialog::slotSelectedTemplate); connect(iconbut, &QPushButton::clicked, this, &ManageTemplatesDialog::slotSelectIcon); connect(this, &QDialog::accepted, this, &ManageTemplatesDialog::addTemplate); } // dialog to remove a template ManageTemplatesDialog::ManageTemplatesDialog(KileTemplate::Manager *templateManager, const QString &caption, QWidget *parent, const char *name) : QDialog(parent) , m_templateManager(templateManager) , m_templateType(KileDocument::Undefined) , m_showAllTypesCheckBox(Q_NULLPTR) { setObjectName(name); setWindowTitle(caption); setModal(true); QVBoxLayout *mainLayout = new QVBoxLayout(); mainLayout->setContentsMargins(0, 0, 0, 0); setLayout(mainLayout); m_templateList = new QTreeWidget(this); m_templateList->setSortingEnabled(false); m_templateList->setHeaderLabels(QStringList() << i18nc("marked", "M") << i18n("Existing Templates") << i18n("Document Type")); m_templateList->setAllColumnsShowFocus(true); m_templateList->setRootIsDecorated(false); populateTemplateListView(KileDocument::Undefined); mainLayout->addWidget(m_templateList); mainLayout->addWidget(new QLabel(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)."), this)); QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok); okButton->setDefault(true); okButton->setShortcut(Qt::CTRL | Qt::Key_Return); mainLayout->addWidget(buttonBox); connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); connect(this, &QDialog::accepted, this, &ManageTemplatesDialog::removeTemplate); } ManageTemplatesDialog::~ManageTemplatesDialog() { } void ManageTemplatesDialog::updateTemplateListView(bool showAllTypes) { populateTemplateListView((showAllTypes ? KileDocument::Undefined : m_templateType)); } void ManageTemplatesDialog::clearSelection() { m_templateList->clearSelection(); } void ManageTemplatesDialog::populateTemplateListView(KileDocument::Type type) { m_templateManager->scanForTemplates(); KileTemplate::TemplateList templateList = m_templateManager->getTemplates(type); QString mode; QTreeWidgetItem* previousItem = Q_NULLPTR; m_templateList->clear(); for (KileTemplate::TemplateListIterator i = templateList.begin(); i != templateList.end(); ++i) { KileTemplate::Info info = *i; QFileInfo iconFileInfo(info.icon); mode = (QFileInfo(info.path).isWritable() && (!iconFileInfo.exists() || iconFileInfo.isWritable())) ? " " : "*"; if ((type == KileDocument::Undefined) || (info.type == type)) { previousItem = new TemplateListViewItem(m_templateList, previousItem, mode, info); } } m_templateList->resizeColumnToContents(0); m_templateList->resizeColumnToContents(1); } void ManageTemplatesDialog::slotSelectedTemplate(QTreeWidgetItem *item) { TemplateListViewItem *templateItem = dynamic_cast(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(); QString res = dlg->openDialog(); KIconLoader kil; if (!res.isNull()) { m_iconEdit->setText(kil.iconPath(res, -KIconLoader::SizeLarge, false)); } } void ManageTemplatesDialog::addTemplate() { QString templateName = (m_nameEdit->text()).trimmed(); if (templateName.isEmpty()) { KMessageBox::error(this, i18n("The template name that you have entered is invalid.\nPlease enter a new name.")); return; } QString icon = (m_iconEdit->text()).trimmed(); QUrl iconURL = QUrl::fromUserInput(icon); if (icon.isEmpty()) { KMessageBox::error(this, i18n("Please choose an icon first.")); return; } KIO::StatJob* statJob = KIO::stat(iconURL, KIO::StatJob::SourceSide, 0); KJobWidgets::setWindow(statJob, this); statJob->exec(); if (statJob->error()) { KMessageBox::error(this, i18n("The icon file: %1\ndoes not seem to exist. Please choose a new icon.", icon)); return; } statJob = KIO::stat(m_sourceURL, KIO::StatJob::SourceSide, 0); KJobWidgets::setWindow(statJob, this); statJob->exec(); if (statJob->error()) { KMessageBox::error(this, i18n("The file: %1\ndoes not seem to exist. Maybe you forgot to save the file?", m_sourceURL.toString())); return; } QTreeWidgetItem* item = m_templateList->currentItem(); if (!item && m_templateManager->searchForTemplate(templateName, m_templateType)) { KMessageBox::error(this, i18n("A template named \"%1\" already exists.\nPlease remove it first.", templateName)); return; } bool returnValue; if (item) { TemplateListViewItem *templateItem = dynamic_cast(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?", templateInfo.name)) == KMessageBox::No) { reject(); return; } - returnValue = m_templateManager->replace(templateInfo, m_sourceURL, templateName, iconURL); - } - else { - returnValue = m_templateManager->add(m_sourceURL, templateName, iconURL); } + + returnValue = m_templateManager->add(m_sourceURL, templateName, iconURL); + if (!returnValue) { KMessageBox::error(this, i18n("Failed to create the template.")); reject(); return; } } bool ManageTemplatesDialog::removeTemplate() { QTreeWidgetItem* item = m_templateList->currentItem(); if (!item) { KMessageBox::information(this, i18n("Please select a template that should be removed.")); return true; } TemplateListViewItem *templateItem = dynamic_cast(item); Q_ASSERT(templateItem); KileTemplate::Info templateInfo = templateItem->getTemplateInfo(); if (KMessageBox::warningYesNo(this, i18n("You are about to remove the template \"%1\"; are you sure?", templateInfo.name)) == KMessageBox::No) { return false; } if (!m_templateManager->remove(templateInfo)) { KMessageBox::error(this, i18n("The template could not be removed.")); reject(); return false; } return true; } diff --git a/src/templates.cpp b/src/templates.cpp index 1d6204f5..da432ea7 100644 --- a/src/templates.cpp +++ b/src/templates.cpp @@ -1,441 +1,373 @@ /******************************************************************************************* begin : Sat Apr 26 2003 copyright : (C) 2003 by Jeroen Wijnhout (wijnhout@science.uva.nl) 2005 by Holger Danielsson (holger.danielsson@t-online.de) 2007-2019 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 "templates.h" #include #include #include #include #include #include #include #include #include #include #include "kileinfo.h" #include "kiledebug.h" #include "utilities.h" // 2005-08-04: dani // - added script support to search existing class files // (classes: Koma, Beamer, Prosper, HA-prosper) // - sort items ('Empty Document' will always be the first entry) // 2006-30-04: tbraun // - drag and drop makes no sense here // - use the Select mode namespace KileTemplate { ////////////////////// Info ////////////////////// Info::Info() : type(KileDocument::Undefined) { } bool Info::operator==(const Info ti) const { return name==ti.name; } ////////////////////// Manager ////////////////////// Manager::Manager(KileInfo* kileInfo, QObject* parent, const char* name) : QObject(parent), m_kileInfo(kileInfo) { setObjectName(name); } Manager::~Manager() { } bool Manager::copyAppData(const QUrl &src, const QString& subdir, const QString& fileName) { //let saveLocation find and create the appropriate place to //store the templates (usually $HOME/.kde/share/apps/kile/templates) QString dir = KileUtilities::writableLocation(QStandardPaths::AppDataLocation) + '/' + subdir; QUrl targetURL = QUrl::fromUserInput(dir); targetURL = targetURL.adjusted(QUrl::StripTrailingSlash); targetURL.setPath(targetURL.path() + '/' + fileName); //if a directory is found if (!dir.isNull()) { // create dir if not existing, needed for copyjob QDir testDir(dir); if (!testDir.exists()) { testDir.mkpath(dir); } // copy file - KIO::FileCopyJob* copyJob = KIO::file_copy(src, targetURL); + if(src == targetURL) { // copying a file over itself + return true; + } + KIO::FileCopyJob* copyJob = KIO::file_copy(src, targetURL, -1, KIO::Overwrite); KJobWidgets::setWindow(copyJob, m_kileInfo->mainWindow()); return copyJob->exec(); } else { - KMessageBox::error(Q_NULLPTR, i18n("Could not find a folder to save %1 to.\nCheck whether you have a .kde folder with write permissions in your home folder.", fileName)); + KMessageBox::error(Q_NULLPTR, i18n("Could not find a folder to save %1 to.\nCheck whether you have a folder named \".kde\" with write permissions in your home folder.", fileName)); return false; } } bool Manager::removeAppData(const QString &file) { QFileInfo fileInfo(file); if(fileInfo.exists()) { KIO::SimpleJob* deleteJob = KIO::file_delete(QUrl::fromUserInput(file)); KJobWidgets::setWindow(deleteJob, m_kileInfo->mainWindow()); return deleteJob->exec(); } return true; } bool Manager::searchForTemplate(const QString& name, KileDocument::Type& type) const { for (KileTemplate::TemplateListConstIterator i = m_TemplateList.constBegin(); i != m_TemplateList.constEnd(); ++i) { KileTemplate::Info info = *i; if(info.name == name && info.type == type) { return true; } } return false; } bool Manager::add(const QUrl &templateSourceURL, const QString &name, const QUrl &icon) { KileDocument::Extensions *extensions = m_kileInfo->extensions(); KileDocument::Type type = extensions->determineDocumentType(templateSourceURL); return add(templateSourceURL, type, name, icon); } bool Manager::add(const QUrl &templateSourceURL, KileDocument::Type type, const QString &name, const QUrl &icon) { KileDocument::Extensions *extensions = m_kileInfo->extensions(); QString extension = extensions->defaultExtensionForDocumentType(type); return copyAppData(templateSourceURL, "templates", "template_" + name + extension) && copyAppData(icon, "pics", "type_" + name + extension + ".kileicon"); } bool Manager::remove(Info ti) { return removeAppData(ti.path) && removeAppData(ti.icon); } -bool Manager::replace(const KileTemplate::Info& toBeReplaced, const QUrl &newTemplateSourceURL, const QString& newName, const QUrl& newIcon) { - KileDocument::Type type = m_kileInfo->extensions()->determineDocumentType(newTemplateSourceURL); - - //start by copying the files that belong to the new template to a safe place - QString templateTempFile, iconTempFile; - - if( newTemplateSourceURL.isLocalFile() ) { - // file protocol. We do not need the network - templateTempFile = newTemplateSourceURL.toLocalFile(); - } - else { - QTemporaryFile tmpFile; - tmpFile.setAutoRemove( false ); - tmpFile.open(); - - templateTempFile = tmpFile.fileName(); - m_TempFilePath = tmpFile.fileName(); - KIO::FileCopyJob* fileCopyJob = KIO::file_copy( newTemplateSourceURL, QUrl::fromLocalFile(templateTempFile), -1, KIO::Overwrite ); - KJobWidgets::setWindow( fileCopyJob, m_kileInfo->mainWindow() ); - - if( ! fileCopyJob->exec() ) { - return false; - } - } - - if( newIcon.isLocalFile() ) { - // file protocol. We do not need the network - iconTempFile = newIcon.toLocalFile(); - } - else { - QTemporaryFile tmpFile; - tmpFile.setAutoRemove( false ); - tmpFile.open(); - - iconTempFile = tmpFile.fileName(); - m_TempFilePath = tmpFile.fileName(); - KIO::FileCopyJob* fileCopyJob = KIO::file_copy( newIcon, QUrl::fromLocalFile(iconTempFile), -1, KIO::Overwrite ); - KJobWidgets::setWindow( fileCopyJob, m_kileInfo->mainWindow() ); - - if( ! fileCopyJob->exec() ) { - if( ! templateTempFile.isEmpty() ) - QFile::remove( templateTempFile ); - return false; - } - } - - //now delete the template that should be replaced - if(!remove(toBeReplaced)) { - if( ! templateTempFile.isEmpty() ) - QFile::remove( templateTempFile ); - if( ! iconTempFile.isEmpty() ) - QFile::remove( iconTempFile ); - } - - //finally, create the new template - if(!add(QUrl::fromUserInput(templateTempFile), type, newName, QUrl::fromUserInput(iconTempFile))) { - if( ! templateTempFile.isEmpty() ) - QFile::remove( templateTempFile ); - if( ! iconTempFile.isEmpty() ) - QFile::remove( iconTempFile ); - return false; - } - - if( ! templateTempFile.isEmpty() ) - QFile::remove( templateTempFile ); - if( ! iconTempFile.isEmpty() ) - QFile::remove( iconTempFile ); - - return true; -} - void Manager::scanForTemplates() { KILE_DEBUG_MAIN << "===scanForTemplates()==================="; QStringList dirs = KileUtilities::locateAll(QStandardPaths::AppDataLocation, "templates", QStandardPaths::LocateDirectory); QDir templates; KileTemplate::Info ti; KileDocument::Extensions *extensions = m_kileInfo->extensions(); m_TemplateList.clear(); for(QStringList::iterator i = dirs.begin(); i != dirs.end(); ++i) { templates = QDir(*i, "template_*"); for (uint j = 0; j < templates.count(); ++j) { ti.path = templates.path() + '/' + templates[j]; QFileInfo fileInfo(ti.path); ti.name = fileInfo.completeBaseName().mid(9); //remove "template_", do it this way to avoid problems with user input! ti.type = extensions->determineDocumentType(QUrl::fromUserInput(ti.path)); ti.icon = KileUtilities::locate(QStandardPaths::AppDataLocation, "pics/type_" + ti.name + extensions->defaultExtensionForDocumentType(ti.type) + ".kileicon"); if (m_TemplateList.contains(ti)) { KILE_DEBUG_MAIN << "\tignoring: " << ti.path; } else { m_TemplateList.append(ti); KILE_DEBUG_MAIN << "\tadding: " << ti.name << " " << ti.path; } } } } QString Manager::defaultEmptyTemplateCaption() { return i18n("Empty File"); } QString Manager::defaultEmptyLaTeXTemplateCaption() { return i18n("Empty LaTeX File"); } QString Manager::defaultEmptyBibTeXTemplateCaption() { return i18n("Empty BibTeX File"); } TemplateList Manager::getAllTemplates() const { return m_TemplateList; } TemplateList Manager::getTemplates(KileDocument::Type type) const { if(type == KileDocument::Undefined) { return getAllTemplates(); } TemplateList toReturn; for (KileTemplate::TemplateListConstIterator i = m_TemplateList.constBegin(); i != m_TemplateList.constEnd(); ++i) { KileTemplate::Info info = *i; if(info.type == type) { toReturn.push_back(info); } } return toReturn; } } ////////////////////// TemplateItem ////////////////////// // new compare function to make the "Empty (...) Document" items appear at the beginning TemplateItem::TemplateItem(QListWidget * parent, const KileTemplate::Info& info) : QListWidgetItem(QPixmap(info.icon), info.name, parent) { m_info = info; } bool TemplateItem::operator<(const QListWidgetItem &other) const { if(text() == KileTemplate::Manager::defaultEmptyTemplateCaption()) { return true; } else if(other.text() == KileTemplate::Manager::defaultEmptyTemplateCaption()) { return false; } else { return QListWidgetItem::operator<(other); } } ////////////////////// TemplateIconView ////////////////////// TemplateIconView::TemplateIconView(QWidget *parent) : QListWidget(parent), m_templateManager(Q_NULLPTR), m_proc(Q_NULLPTR) { setViewMode(QListView::IconMode); setMovement(QListView::Static); setResizeMode(QListView::Adjust); setSelectionMode(QAbstractItemView::SingleSelection); setFlow(QListView::TopToBottom); setMinimumHeight(100); setIconSize(QSize(48, 48)); } TemplateIconView::~TemplateIconView() { } void TemplateIconView::setTemplateManager(KileTemplate::Manager *templateManager) { m_templateManager = templateManager; } void TemplateIconView::fillWithTemplates(KileDocument::Type type) { if(!m_templateManager) { return; } clear(); if(type == KileDocument::LaTeX) { searchLaTeXClassFiles(); } else { addTemplateIcons(type); } } void TemplateIconView::searchLaTeXClassFiles() { if(!m_templateManager) { return; } m_output.clear(); QString command = "kpsewhich -format=tex scrartcl.cls beamer.cls prosper.cls HA-prosper.sty"; delete m_proc; m_proc = new KProcess(this); (*m_proc) << KShell::splitArgs(command); m_proc->setOutputChannelMode(KProcess::MergedChannels); m_proc->setReadChannel(QProcess::StandardOutput); connect(m_proc, SIGNAL(readyReadStandardOutput()), this, SLOT(slotProcessOutput())); connect(m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotProcessExited(int,QProcess::ExitStatus))); connect(m_proc, SIGNAL(error(QProcess::ProcessError)), this, SLOT(slotProcessError())); KILE_DEBUG_MAIN << "=== NewFileWidget::searchClassFiles() ===================="; KILE_DEBUG_MAIN << "\texecute: " << command; m_proc->start(); } void TemplateIconView::slotProcessOutput() { QByteArray buf = m_proc->readAllStandardOutput(); m_output += QString::fromLocal8Bit(buf.data(), buf.size()); } void TemplateIconView::slotProcessError() { addTemplateIcons(KileDocument::LaTeX); emit classFileSearchFinished(); } void TemplateIconView::slotProcessExited(int /*exitCode*/, QProcess::ExitStatus exitStatus) { if(exitStatus != QProcess::NormalExit) { m_output.clear(); } addTemplateIcons(KileDocument::LaTeX); emit classFileSearchFinished(); } void TemplateIconView::addTemplateIcons(KileDocument::Type type) { if(!m_templateManager) { return; } QString emptyIcon = KileUtilities::locate(QStandardPaths::AppDataLocation, "pics/" + QString(DEFAULT_EMPTY_ICON) + ".png" ); KileTemplate::Info emptyDocumentInfo; emptyDocumentInfo.name = KileTemplate::Manager::defaultEmptyTemplateCaption(); emptyDocumentInfo.icon = emptyIcon; emptyDocumentInfo.type = type; TemplateItem *emp = new TemplateItem(this, emptyDocumentInfo); setCurrentItem(emp); if(type == KileDocument::LaTeX) { // disable non standard templates QMap map; map["Scrartcl"] = false; map["Scrbook"] = false; map["Scrreprt"] = false; map["Scrlttr2"] = false; map["Beamer"] = false; map["Prosper"] = false; map["HA-prosper"] = false; // split search results and look, which class files are present QStringList list = m_output.split('\n'); for(QStringList::Iterator it=list.begin(); it!=list.end(); ++it) { QString filename = QFileInfo(*it).fileName(); if(filename=="scrartcl.cls") { map["Scrartcl"] = true; map["Scrbook"] = true; map["Scrreprt"] = true; map["Scrlttr2"] = true; } else if(filename=="beamer.cls") { map["Beamer"] = true; } else if(filename=="prosper.cls") { map["Prosper"] = true; } else if(filename=="HA-prosper.sty") { map["HA-prosper"] = true; } } KileTemplate::TemplateList templateList = m_templateManager->getTemplates(KileDocument::LaTeX); // insert all standard templates, all user-defined templates // and those templates, which have a present class for (KileTemplate::TemplateListIterator i=templateList.begin(); i != templateList.end(); ++i) { KileTemplate::Info info = *i; QString classname = info.name; if(!map.contains(classname) || map[classname]==true) { new TemplateItem(this, info); } } } else { KileTemplate::TemplateList templateList = m_templateManager->getTemplates(type); for (KileTemplate::TemplateListIterator i=templateList.begin(); i != templateList.end(); ++i) { new TemplateItem(this, *i); } } // sort all items (item for 'Empty Document' will always be the first one) sortItems(); } diff --git a/src/templates.h b/src/templates.h index 517e4990..979ca210 100644 --- a/src/templates.h +++ b/src/templates.h @@ -1,171 +1,169 @@ /*************************************************************************************** begin : Sat Apr 26 2003 copyright : (C) 2003 by Jeroen Wijnhout (wijnhout@science.uva.nl) - 2007, 2008 by Michel Ludwig (michel.ludwig@kdemail.net) + 2007-2019 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. * * * ***************************************************************************/ #ifndef TEMPLATES_H #define TEMPLATES_H #include #include #include #include #include #include "kileconstants.h" #define DEFAULT_EMPTY_ICON "type_Empty" class KProcess; class KileInfo; namespace KileTemplate { struct Info { public: Info(); QString name; QString path; QString icon; KileDocument::Type type; bool operator==(const Info ti) const; }; typedef QList TemplateList; typedef QList::iterator TemplateListIterator; typedef QList::const_iterator TemplateListConstIterator; class Manager : public QObject { Q_OBJECT public: explicit Manager(KileInfo *info, QObject* parent = Q_NULLPTR, const char* name = NULL); virtual ~Manager(); void scanForTemplates(); /** * Get all the templates. **/ TemplateList getAllTemplates() const; /** * Get all the templates of a certain type. * * @param type The type of the templates that should be returned. You can pass "KileDocument::Undefined" to * retrieve every template. **/ TemplateList getTemplates(KileDocument::Type type) const; /** * Checks whether a template with a given name and type exists. * * @return true iff a template with the given name and type could be found **/ bool searchForTemplate(const QString& name, KileDocument::Type& type) const; - // add a template in $HOME/kile/templates/ + // Add a template in .kde/share/kile/templates/ + // This function will override any existing template. bool add(const QUrl &templateSourceURL, const QString &name, const QUrl &icon); - // remove a template from $HOME/kile/templates/ + // Remove a template from .kde/share/kile/templates/ bool remove(KileTemplate::Info ti); - // replaces a template - bool replace(const KileTemplate::Info& toBeReplaced, const QUrl &newTemplateSourceURL, const QString& newName, const QUrl& newIcon); - // these have to be methods as we cannot use i18n calls in global objects static QString defaultEmptyTemplateCaption(); static QString defaultEmptyLaTeXTemplateCaption(); static QString defaultEmptyBibTeXTemplateCaption(); protected: KileInfo* m_kileInfo; private: bool copyAppData(const QUrl &src, const QString& subdir, const QString& fileName); bool removeAppData(const QString &file); /** * Adds a new template. This method differs from the other add method in that it does not try to determine * the type of the template from the passed source URL. **/ bool add(const QUrl &templateSourceURL, KileDocument::Type type, const QString& name, const QUrl& icon); private: TemplateList m_TemplateList; QString m_TempFilePath; }; } class TemplateItem : public QListWidgetItem { public: TemplateItem( QListWidget * parent, const KileTemplate::Info & info); ~TemplateItem() {} bool operator<(const QListWidgetItem &other) const override; QString name() { return m_info.name; } QString path() { return m_info.path; } QString icon() { return m_info.icon; } KileDocument::Type type() { return m_info.type; } private: KileTemplate::Info m_info; }; class TemplateIconView : public QListWidget { Q_OBJECT public: explicit TemplateIconView(QWidget *parent = Q_NULLPTR); virtual ~TemplateIconView(); void setTemplateManager(KileTemplate::Manager *templateManager); void fillWithTemplates(KileDocument::Type type); Q_SIGNALS: void classFileSearchFinished(); protected: KileTemplate::Manager *m_templateManager; QString m_output; KProcess *m_proc; void addTemplateIcons(KileDocument::Type type); void searchLaTeXClassFiles(); protected Q_SLOTS: void slotProcessError(); void slotProcessOutput(); void slotProcessExited(int exitCode, QProcess::ExitStatus exitStatus); }; #endif