1
0
mirror of https://github.com/KDE/latte-dock.git synced 2025-01-24 18:03:53 +03:00
latte-dock/app/layout.cpp

267 lines
5.9 KiB
C++
Raw Normal View History

/*
* Copyright 2017 Smith AR <audoban@openmailbox.org>
* Michail Vourlakos <mvourlakos@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
2018-01-07 19:33:18 +02:00
#include "layout.h"
#include <QFile>
#include <KSharedConfig>
namespace Latte {
2018-01-07 19:33:18 +02:00
Layout::Layout(QObject *parent, QString layoutFile, QString assignedName)
: QObject(parent)
{
qDebug() << "Layout file to create object: " << layoutFile << " with name: " << assignedName;
2017-07-03 21:33:27 +03:00
if (QFile(layoutFile).exists()) {
if (assignedName.isEmpty()) {
assignedName = layoutName(layoutFile);
}
KSharedConfigPtr lConfig = KSharedConfig::openConfig(layoutFile);
m_layoutGroup = KConfigGroup(lConfig, "LayoutSettings");
setFile(layoutFile);
setName(assignedName);
loadConfig();
init();
}
}
2018-01-07 19:33:18 +02:00
Layout::~Layout()
{
if (!m_layoutFile.isEmpty()) {
//saveConfig();
m_layoutGroup.sync();
}
}
2018-01-07 19:33:18 +02:00
void Layout::init()
{
2018-01-07 19:33:18 +02:00
connect(this, &Layout::activitiesChanged, this, &Layout::saveConfig);
connect(this, &Layout::versionChanged, this, &Layout::saveConfig);
connect(this, &Layout::colorChanged, this, &Layout::saveConfig);
connect(this, &Layout::showInMenuChanged, this, &Layout::saveConfig);
connect(this, &Layout::launchersChanged, this, &Layout::saveConfig);
}
2018-01-07 19:33:18 +02:00
int Layout::version() const
{
return m_version;
}
2018-01-07 19:33:18 +02:00
void Layout::setVersion(int ver)
{
if (m_version == ver) {
return;
}
m_version = ver;
emit versionChanged();
}
2018-01-07 19:33:18 +02:00
bool Layout::showInMenu() const
{
return m_showInMenu;
}
2018-01-07 19:33:18 +02:00
void Layout::setShowInMenu(bool show)
{
if (m_showInMenu == show) {
return;
}
m_showInMenu = show;
emit showInMenuChanged();
}
2018-01-07 19:33:18 +02:00
QString Layout::name() const
{
return m_layoutName;
}
2018-01-07 19:33:18 +02:00
void Layout::setName(QString name)
{
if (m_layoutName == name) {
return;
}
2017-07-03 21:33:27 +03:00
qDebug() << "Layout name:" << name;
m_layoutName = name;
emit nameChanged();
}
2018-01-07 19:33:18 +02:00
QString Layout::color() const
{
return m_color;
}
2018-01-07 19:33:18 +02:00
void Layout::setColor(QString color)
{
if (m_color == color) {
return;
}
m_color = color;
emit colorChanged();
}
2018-01-07 19:33:18 +02:00
QString Layout::file() const
{
return m_layoutFile;
}
2018-01-07 19:33:18 +02:00
void Layout::setFile(QString file)
{
if (m_layoutFile == file) {
return;
}
2017-07-03 21:33:27 +03:00
qDebug() << "Layout file:" << file;
m_layoutFile = file;
emit fileChanged();
}
2018-01-07 19:33:18 +02:00
QStringList Layout::launchers() const
{
return m_launchers;
}
2018-01-07 19:33:18 +02:00
void Layout::setLaunchers(QStringList launcherList)
{
2017-07-24 17:56:07 +03:00
if (m_launchers == launcherList)
return;
2017-07-24 17:56:07 +03:00
m_launchers = launcherList;
emit launchersChanged();
}
2018-01-07 19:33:18 +02:00
QStringList Layout::activities() const
{
return m_activities;
}
2018-01-07 19:33:18 +02:00
void Layout::setActivities(QStringList activities)
{
if (m_activities == activities) {
return;
}
m_activities = activities;
emit activitiesChanged();
}
2018-01-07 19:33:18 +02:00
bool Layout::fileIsBroken() const
{
if (m_layoutFile.isEmpty() || !QFile(m_layoutFile).exists()) {
return false;
}
KSharedConfigPtr lFile = KSharedConfig::openConfig(m_layoutFile);
KConfigGroup containmentsEntries = KConfigGroup(lFile, "Containments");
QStringList ids;
ids << containmentsEntries.groupList();
QStringList conts;
conts << ids;
QStringList applets;
foreach (auto cId, containmentsEntries.groupList()) {
auto appletsEntries = containmentsEntries.group(cId).group("Applets");
ids << appletsEntries.groupList();
applets << appletsEntries.groupList();
}
QSet<QString> idsSet = QSet<QString>::fromList(ids);
QMap<QString, int> countOfStrings;
for (int i = 0; i < ids.count(); i++) {
countOfStrings[ids[i]]++;
}
if (idsSet.count() != ids.count()) {
qDebug() << " ---- ERROR: BROKEN LAYOUT ----";
foreach (QString c, conts) {
if (applets.contains(c)) {
qDebug() << "Error: Same applet and containment id found ::: " << c;
}
}
for (int i = 0; i < ids.count(); ++i) {
for (int j = i + 1; j < ids.count(); ++j) {
if (ids[i] == ids[j]) {
qDebug() << "Error: Applets with same id ::: " << ids[i];
}
}
}
return true;
}
return false;
}
2018-01-07 19:33:18 +02:00
QString Layout::layoutName(const QString &fileName)
{
int lastSlash = fileName.lastIndexOf("/");
QString tempLayoutFile = fileName;
QString layoutName = tempLayoutFile.remove(0, lastSlash + 1);
int ext = layoutName.lastIndexOf(".layout.latte");
layoutName = layoutName.remove(ext, 13);
return layoutName;
}
2018-01-07 19:33:18 +02:00
void Layout::loadConfig()
{
m_version = m_layoutGroup.readEntry("version", 2);
m_color = m_layoutGroup.readEntry("color", QString("blue"));
m_showInMenu = m_layoutGroup.readEntry("showInMenu", false);
m_activities = m_layoutGroup.readEntry("activities", QStringList());
m_launchers = m_layoutGroup.readEntry("launchers", QStringList());
}
2018-01-07 19:33:18 +02:00
void Layout::saveConfig()
{
qDebug() << "layout is saving... for layout:" << m_layoutName;
m_layoutGroup.writeEntry("version", m_version);
m_layoutGroup.writeEntry("showInMenu", m_showInMenu);
m_layoutGroup.writeEntry("color", m_color);
m_layoutGroup.writeEntry("launchers", m_launchers);
m_layoutGroup.writeEntry("activities", m_activities);
m_layoutGroup.sync();
}
}