/* * Copyright 2020 Michail Vourlakos * * This file is part of Latte-Dock * * Latte-Dock 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. * * Latte-Dock 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, see . */ #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; 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; } //! it is used in order to provide translations for system templates void Manager::exposeTranslatedTemplateNames() { i18nc("default layout template name", "Default"); i18nc("empty layout template name", "Empty"); } } }