/* SPDX-FileCopyrightText: 2020 Michail Vourlakos SPDX-License-Identifier: GPL-2.0-or-later */ #include "templatesmanager.h" // local #include "../layout/abstractlayout.h" #include "../layout/centrallayout.h" #include "../layouts/importer.h" #include "../layouts/manager.h" #include "../layouts/storage.h" #include "../tools/commontools.h" #include "../view/view.h" // Qt #include // KDE #include #include namespace Latte { namespace Templates { Manager::Manager(Latte::Corona *corona) : QObject(corona), m_corona(corona) { KDirWatch::self()->addDir(Latte::configPath() + "/latte/templates", KDirWatch::WatchFiles); connect(KDirWatch::self(), &KDirWatch::created, this, &Manager::onCustomTemplatesCountChanged); connect(KDirWatch::self(), &KDirWatch::deleted, this, &Manager::onCustomTemplatesCountChanged); connect(KDirWatch::self(), &KDirWatch::dirty, this, &Manager::onCustomTemplatesCountChanged); } Manager::~Manager() { } void Manager::init() { connect(this, &Manager::viewTemplatesChanged, m_corona->layoutsManager(), &Latte::Layouts::Manager::viewTemplatesChanged); initLayoutTemplates(); initViewTemplates(); } void Manager::initLayoutTemplates() { m_layoutTemplates.clear(); initLayoutTemplates(m_corona->kPackage().filePath("templates")); initLayoutTemplates(Latte::configPath() + "/latte/templates"); emit layoutTemplatesChanged(); } void Manager::initViewTemplates() { m_viewTemplates.clear(); initViewTemplates(m_corona->kPackage().filePath("templates")); initViewTemplates(Latte::configPath() + "/latte/templates"); emit viewTemplatesChanged(); } void Manager::initLayoutTemplates(const QString &path) { QDir templatesDir(path); QStringList filter; filter.append(QString("*.layout.latte")); QStringList templates = templatesDir.entryList(filter, QDir::Files | QDir::Hidden | QDir::NoSymLinks); for (int i=0; ikPackage().filePath("templates") == path); QDir templatesDir(path); QStringList filter; filter.append(QString("*.view.latte")); QStringList templates = templatesDir.entryList(filter, QDir::Files | QDir::Hidden | QDir::NoSymLinks); for (int i=0; iexportTemplate(originFile, destinationFile, approvedApplets); } bool Manager::exportTemplate(const Latte::View *view, const QString &destinationFile, const Data::AppletsTable &approvedApplets) { return Latte::Layouts::Storage::self()->exportTemplate(view->layout(), view->containment(), destinationFile, approvedApplets); } void Manager::onCustomTemplatesCountChanged(const QString &file) { if (file.startsWith(Latte::configPath() + "/latte/templates")) { if (file.endsWith(".layout.latte")) { initLayoutTemplates(); } else if (file.endsWith(".view.latte")) { initViewTemplates(); } } } void Manager::importSystemLayouts() { for (int i=0; i 0) { name = name.left(pos_); } int i = 2; QString namePart = name; while (hasLayoutTemplate(name)) { name = namePart + " - " + QString::number(i); i++; } return name; } QString Manager::uniqueViewTemplateName(QString name) const { int pos_ = name.lastIndexOf(QRegExp(QString(" - [0-9]+"))); if (hasViewTemplate(name) && pos_ > 0) { name = name.left(pos_); } int i = 2; QString namePart = name; while (hasViewTemplate(name)) { name = namePart + " - " + QString::number(i); i++; } return name; } QString Manager::templateName(const QString &filePath) { int lastSlash = filePath.lastIndexOf("/"); QString tempFilePath = filePath; QString templatename = tempFilePath.remove(0, lastSlash + 1); QString extension(".layout.latte"); int ext = templatename.lastIndexOf(extension); if (ext>0) { templatename = templatename.remove(ext, extension.size()); } else { extension = ".view.latte"; ext = templatename.lastIndexOf(extension); templatename = templatename.remove(ext,extension.size()); } return templatename; } //! it is used in order to provide translations for system templates void Manager::exposeTranslatedTemplateNames() { //! layout templates default names i18nc("default layout template name", "Default"); i18nc("empty layout template name", "Empty"); //! dock/panel templates default names i18nc("view template name", "Default Dock"); i18nc("view template name", "Default Panel"); i18nc("view template name", "Empty Panel"); } } }