add functionality for ExportViewTemplate

work/spdx
Michail Vourlakos 4 years ago
parent eec63e5f13
commit 26af7e4091

@ -612,6 +612,75 @@ bool Storage::exportTemplate(const QString &originFile, const QString &destinati
return true;
}
bool Storage::exportTemplate(const Layout::GenericLayout *layout, Plasma::Containment *containment, const QString &destinationFile, const Data::AppletsTable &approvedApplets)
{
if (!layout || !containment || destinationFile.isEmpty()) {
return false;
}
if (QFile(destinationFile).exists()) {
QFile::remove(destinationFile);
}
KSharedConfigPtr destFilePtr = KSharedConfig::openConfig(destinationFile);
KConfigGroup copied_conts = KConfigGroup(destFilePtr, "Containments");
KConfigGroup copied_c1 = KConfigGroup(&copied_conts, QString::number(containment->id()));
containment->config().copyTo(&copied_c1);
//!investigate if there are subcontainments in the containment to copy also
//! subId, subAppletId
QHash<uint, QString> subInfo;
auto applets = containment->config().group("Applets");
for (const auto &applet : applets.groupList()) {
int tSubId = subContainmentId(applets.group(applet));
//! It is a subcontainment !!!
if (isValid(tSubId)) {
subInfo[tSubId] = applet;
qDebug() << "subcontainment with id "<< tSubId << " was found in the containment... ::: " << containment->id();
}
}
if (subInfo.count() > 0) {
for(const auto subId : subInfo.keys()) {
Plasma::Containment *subcontainment{nullptr};
for (const auto containment : layout->corona()->containments()) {
if (containment->id() == subId) {
subcontainment = containment;
break;
}
}
if (subcontainment) {
KConfigGroup copied_sub = KConfigGroup(&copied_conts, QString::number(subcontainment->id()));
subcontainment->config().copyTo(&copied_sub);
}
}
}
//! end of subcontainments specific code
//! clear applets that are not approved
for (const auto &cId : copied_conts.groupList()) {
auto applets = copied_conts.group(cId).group("Applets");
for (const auto &aId: applets.groupList()) {
QString pluginId = applets.group(aId).readEntry("plugin", "");
if (!approvedApplets.containsId(pluginId) && !isSubContainment(applets.group(aId))) {
//!remove all configuration for that applet
for (const auto &configId: applets.group(aId).groupList()) {
applets.group(aId).group(configId).deleteGroup();
}
}
}
}
return true;
}
ViewDelayedCreationData Storage::copyView(const Layout::GenericLayout *layout, Plasma::Containment *containment)
{
if (!containment || !layout->corona()) {

@ -86,6 +86,7 @@ public:
ViewDelayedCreationData newView(const Layout::GenericLayout *destination, const QString &templateFile);
bool exportTemplate(const QString &originFile, const QString &destinationFile, const Data::AppletsTable &approvedApplets);
bool exportTemplate(const Layout::GenericLayout *layout, Plasma::Containment *containment, const QString &destinationFile, const Data::AppletsTable &approvedApplets);
/// STATIC
//! Check if an applet config group is valid or belongs to removed applet

@ -40,7 +40,7 @@ ExportTemplateDialog::ExportTemplateDialog(SettingsDialog *parent, const QString
m_corona = parent->corona();
init();
initExtractButton(i18n("Export your selected layout as template"));
initExportButton(i18n("Export your selected layout as template"));
//! we must create handlers after creating/adjusting the ui
m_handler = new Handler::ExportTemplateHandler(this, layoutName, layoutId);
connect(m_handler, &Handler::ExportTemplateHandler::dataChanged, this, &ExportTemplateDialog::onDataChanged);
@ -53,7 +53,7 @@ ExportTemplateDialog::ExportTemplateDialog(Latte::View *view)
m_corona = qobject_cast<Latte::Corona *>(view->corona());
init();
initExtractButton(i18n("Export your selected view as template"));
initExportButton(i18n("Export your selected view as template"));
//! we must create handlers after creating/adjusting the ui
m_handler = new Handler::ExportTemplateHandler(this, view);
}
@ -91,7 +91,7 @@ void ExportTemplateDialog::initButtons()
this, &ExportTemplateDialog::onCancel);
}
void ExportTemplateDialog::initExtractButton(const QString &tooltip)
void ExportTemplateDialog::initExportButton(const QString &tooltip)
{
m_exportButton = new QPushButton(this);
m_exportButton->setText(i18nc("export template", "Export"));

@ -75,7 +75,7 @@ private slots:
void onReset();
void initButtons();
void initExtractButton(const QString &tooltip);
void initExportButton(const QString &tooltip);
private:
void init();

@ -21,6 +21,7 @@
#include "exporttemplatehandler.h"
// local
#include <coretypes.h>
#include "ui_exporttemplatedialog.h"
#include "exporttemplatedialog.h"
#include "appletsmodel.h"
@ -71,6 +72,10 @@ ExportTemplateHandler::ExportTemplateHandler(Dialog::ExportTemplateDialog *paren
ExportTemplateHandler::ExportTemplateHandler(Dialog::ExportTemplateDialog *parentDialog, Latte::View *view)
: ExportTemplateHandler(parentDialog)
{
QString name = (view->type() == Latte::Types::PanelView ? i18n("Panel") : i18n("Dock"));
loadViewApplets(view);
o_filepath = parentDialog->corona()->templatesManager()->proposedTemplateAbsolutePath(name + ".view.latte");
setFilepath(o_filepath);
}
ExportTemplateHandler::~ExportTemplateHandler()
@ -95,7 +100,7 @@ void ExportTemplateHandler::init()
m_appletsProxyModel->setSourceModel(m_appletsModel);
m_appletsProxyModel->setSortRole(Model::Applets::SORTINGROLE);
m_appletsProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
// m_appletsProxyModel->sort(Model::Applets::NAMECOLUMN, Qt::AscendingOrder);
// m_appletsProxyModel->sort(Model::Applets::NAMECOLUMN, Qt::AscendingOrder);
m_ui->appletsTable->setModel(m_appletsProxyModel);
@ -131,6 +136,7 @@ void ExportTemplateHandler::loadLayoutApplets(const QString &layoutName, const Q
void ExportTemplateHandler::loadViewApplets(Latte::View *view)
{
m_originView = view;
Data::AppletsTable c_data = Latte::Layouts::Storage::self()->plugins(view->layout(), view->containment()->id());
m_appletsModel->setData(c_data);
m_parentDialog->setWindowTitle(i18n("Export View Template"));
@ -175,6 +181,14 @@ void ExportTemplateHandler::chooseFileDialog()
} else {
setFilepath(file);
}
} else {
if (!file.endsWith(".view.latte")) {
QString selected = file;
selected = selected.replace(QDir::homePath(), "~");
showInlineMessage(i18n("<i>%0</i> does not end with <i>.view.latte</i> extension. Selected file <b>rejected</b>.").arg(selected), KMessageWidget::Error, true);
} else {
setFilepath(file);
}
}
});
@ -190,32 +204,38 @@ void ExportTemplateHandler::onExport()
true);
};
bool result{false};
if (!m_originLayoutFilePath.isEmpty()) {
bool result = m_parentDialog->corona()->templatesManager()->exportTemplate(m_originLayoutFilePath,
c_filepath,
m_appletsModel->selectedApplets());
if (result) {
QAction *openUrlAction = new QAction(i18n("Open Location..."), this);
openUrlAction->setData(c_filepath);
QList<QAction *> actions;
actions << openUrlAction;
connect(openUrlAction, &QAction::triggered, this, [&, openUrlAction]() {
QString file = openUrlAction->data().toString();
if (!file.isEmpty()) {
KIO::highlightInFileManager({file});
}
});
showInlineMessage(i18nc("settings:template export success","Template <b>%0</b> export succeeded...").arg(QFileInfo(c_filepath).baseName()),
KMessageWidget::Information,
false,
actions);
} else {
showExportTemplateError(QFileInfo(c_filepath).baseName());
}
result = m_parentDialog->corona()->templatesManager()->exportTemplate(m_originLayoutFilePath,
c_filepath,
m_appletsModel->selectedApplets());
} else if (m_originView){
result = m_parentDialog->corona()->templatesManager()->exportTemplate(m_originView,
c_filepath,
m_appletsModel->selectedApplets());
}
if (result) {
QAction *openUrlAction = new QAction(i18n("Open Location..."), this);
openUrlAction->setData(c_filepath);
QList<QAction *> actions;
actions << openUrlAction;
connect(openUrlAction, &QAction::triggered, this, [&, openUrlAction]() {
QString file = openUrlAction->data().toString();
if (!file.isEmpty()) {
KIO::highlightInFileManager({file});
}
});
showInlineMessage(i18nc("settings:template export success","Template <b>%0</b> export succeeded...").arg(QFileInfo(c_filepath).baseName()),
KMessageWidget::Information,
false,
actions);
} else {
showExportTemplateError(QFileInfo(c_filepath).baseName());
}
}

@ -103,6 +103,7 @@ private:
QString o_filepath;
QString m_originLayoutFilePath;
Latte::View *m_originView{nullptr};
Dialog::ExportTemplateDialog *m_parentDialog{nullptr};
Ui::ExportTemplateDialog *m_ui{nullptr};

@ -25,6 +25,7 @@
#include "../layouts/importer.h"
#include "../layouts/storage.h"
#include "../tools/commontools.h"
#include "../view/view.h"
// Qt
#include <QDir>
@ -178,6 +179,11 @@ bool Manager::exportTemplate(const QString &originFile, const QString &destinati
return Latte::Layouts::Storage::self()->exportTemplate(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")) {

@ -36,6 +36,7 @@
namespace Latte {
class Corona;
class View;
}
namespace Latte {
@ -59,6 +60,7 @@ public:
bool viewTemplateExists(const QString &templateName) const;
bool exportTemplate(const QString &originFile, const QString &destinationFile, const Data::AppletsTable &approvedApplets);
bool exportTemplate(const Latte::View *view, const QString &destinationFile, const Data::AppletsTable &approvedApplets);
Data::Layout layoutTemplateForName(const QString &layoutName);

@ -39,6 +39,7 @@
#include "../plasma/extended/theme.h"
#include "../screenpool.h"
#include "../settings/universalsettings.h"
#include "../settings/exporttemplatedialog/exporttemplatedialog.h"
#include "../shortcuts/globalshortcuts.h"
#include "../shortcuts/shortcutstracker.h"
@ -453,6 +454,12 @@ void View::copyView()
m_layout->copyView(containment());
}
void View::exportTemplate()
{
Latte::Settings::Dialog::ExportTemplateDialog *exportDlg = new Latte::Settings::Dialog::ExportTemplateDialog(this);
exportDlg->show();
}
void View::removeView()
{
if (m_layout && m_layout->viewsCount() > 1) {

@ -269,6 +269,7 @@ public:
public slots:
Q_INVOKABLE void copyView();
Q_INVOKABLE void exportTemplate();
Q_INVOKABLE void removeView();
Q_INVOKABLE void moveToLayout(QString layoutName);

@ -544,6 +544,8 @@ Loader {
if (item.actionId === "new:") {
latteView.layout.newView(item.templateId);
} else if (item.actionId === "export:") {
latteView.exportTemplate();
} else if (item.actionId === "copy:") {
latteView.copyView();
} else if (item.actionId === "move:") {

Loading…
Cancel
Save