File indexing completed on 2024-04-21 05:30:48

0001 /*
0002     SPDX-FileCopyrightText: 2019 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "abstractlayout.h"
0007 
0008 // local
0009 #include "../data/layoutdata.h"
0010 
0011 // Qt
0012 #include <QDir>
0013 #include <QDebug>
0014 #include <QFile>
0015 #include <QLatin1String>
0016 
0017 // KDE
0018 #include <KSharedConfig>
0019 
0020 namespace Latte {
0021 namespace Layout {
0022 
0023 AbstractLayout::AbstractLayout(QObject *parent, QString layoutFile, QString assignedName)
0024     : QObject(parent)
0025 {
0026     qDebug() << "Layout file to create object: " << layoutFile << " with name: " << assignedName;
0027 
0028     if (QFile(layoutFile).exists()) {
0029         if (assignedName.isEmpty()) {
0030             assignedName =  layoutName(layoutFile);
0031         }
0032 
0033         //!this order is important because setFile initializes also the m_layoutGroup
0034         setFile(layoutFile);
0035         setName(assignedName);
0036         loadConfig();
0037         init();
0038 
0039         m_loadedCorrectly = true;
0040     }
0041 }
0042 
0043 AbstractLayout::~AbstractLayout()
0044 {   
0045 }
0046 
0047 void AbstractLayout::init()
0048 {
0049     connect(this, &AbstractLayout::backgroundStyleChanged, this, &AbstractLayout::backgroundChanged);
0050     connect(this, &AbstractLayout::backgroundStyleChanged, this, &AbstractLayout::textColorChanged);
0051     connect(this, &AbstractLayout::customBackgroundChanged, this, &AbstractLayout::backgroundChanged);
0052     connect(this, &AbstractLayout::customTextColorChanged, this, &AbstractLayout::textColorChanged);
0053     connect(this, &AbstractLayout::colorChanged, this, &AbstractLayout::backgroundChanged);
0054     connect(this, &AbstractLayout::colorChanged, this, &AbstractLayout::textColorChanged);
0055 
0056     connect(this, &AbstractLayout::customBackgroundChanged, this, &AbstractLayout::saveConfig);
0057     connect(this, &AbstractLayout::customTextColorChanged, this, &AbstractLayout::saveConfig);
0058     connect(this, &AbstractLayout::colorChanged, this, &AbstractLayout::saveConfig);
0059 
0060     connect(this, &AbstractLayout::iconChanged, this, &AbstractLayout::saveConfig);
0061     connect(this, &AbstractLayout::lastUsedActivityChanged, this, &AbstractLayout::saveConfig);
0062     connect(this, &AbstractLayout::launchersChanged, this, &AbstractLayout::saveConfig);
0063     connect(this, &AbstractLayout::preferredForShortcutsTouchedChanged, this, &AbstractLayout::saveConfig);
0064     connect(this, &AbstractLayout::popUpMarginChanged, this, &AbstractLayout::saveConfig);
0065     connect(this, &AbstractLayout::schemeFileChanged, this, &AbstractLayout::saveConfig);
0066     connect(this, &AbstractLayout::versionChanged, this, &AbstractLayout::saveConfig);
0067 }
0068 
0069 int AbstractLayout::version() const
0070 {
0071     return m_version;
0072 }
0073 
0074 void AbstractLayout::setVersion(int ver)
0075 {
0076     if (m_version == ver) {
0077         return;
0078     }
0079 
0080     m_version = ver;
0081 
0082     emit versionChanged();
0083 }
0084 
0085 
0086 bool AbstractLayout::preferredForShortcutsTouched() const
0087 {
0088     return m_preferredForShortcutsTouched;
0089 }
0090 
0091 void AbstractLayout::setPreferredForShortcutsTouched(bool touched)
0092 {
0093     if (m_preferredForShortcutsTouched == touched) {
0094         return;
0095     }
0096 
0097     m_preferredForShortcutsTouched = touched;
0098     emit preferredForShortcutsTouchedChanged();
0099 }
0100 
0101 int AbstractLayout::popUpMargin() const
0102 {
0103     return m_popUpMargin;
0104 }
0105 
0106 void AbstractLayout::setPopUpMargin(const int &margin)
0107 {
0108     if (m_popUpMargin == margin) {
0109         return;
0110     }
0111 
0112     m_popUpMargin = margin;
0113     emit popUpMarginChanged();
0114 }
0115 
0116 QString AbstractLayout::background() const
0117 {
0118     if (m_backgroundStyle == ColorBackgroundStyle) {
0119         return m_color;
0120     } else {
0121         return m_customBackground;
0122     }
0123 }
0124 
0125 QString AbstractLayout::schemeFile() const
0126 {
0127     return m_schemeFile;
0128 }
0129 
0130 void AbstractLayout::setSchemeFile(const QString &file)
0131 {
0132     if (m_schemeFile == file) {
0133         return;
0134     }
0135 
0136     m_schemeFile = file;
0137     emit schemeFileChanged();
0138 }
0139 
0140 QString AbstractLayout::textColor() const
0141 {
0142     if (m_backgroundStyle == ColorBackgroundStyle) {
0143         return predefinedTextColor();
0144     } else {
0145         return m_customTextColor;
0146     }
0147 }
0148 
0149 BackgroundStyle AbstractLayout::backgroundStyle() const
0150 {
0151     return m_backgroundStyle;
0152 }
0153 
0154 void AbstractLayout::setBackgroundStyle(const BackgroundStyle &style)
0155 {
0156     if (m_backgroundStyle == style) {
0157         return;
0158     }
0159 
0160     m_backgroundStyle = style;
0161     emit backgroundStyleChanged();
0162 }
0163 
0164 
0165 QString AbstractLayout::customBackground() const
0166 {
0167     return m_customBackground;
0168 }
0169 
0170 void AbstractLayout::setCustomBackground(const QString &background)
0171 {
0172     if (m_customBackground == background) {
0173         return;
0174     }
0175 
0176     m_customBackground = background;
0177 
0178     emit customBackgroundChanged();
0179 }
0180 
0181 QString AbstractLayout::file() const
0182 {
0183     return m_layoutFile;
0184 }
0185 
0186 void AbstractLayout::setFile(QString file)
0187 {
0188     if (m_layoutFile == file) {
0189         return;
0190     }
0191 
0192     qDebug() << "Layout file:" << file;
0193 
0194     m_layoutFile = file;
0195 
0196     KSharedConfigPtr filePtr = KSharedConfig::openConfig(m_layoutFile);
0197     m_layoutGroup = KConfigGroup(filePtr, "LayoutSettings");
0198 
0199     emit fileChanged();
0200 }
0201 
0202 QString AbstractLayout::name() const
0203 {
0204     return m_layoutName;
0205 }
0206 
0207 void AbstractLayout::setName(QString name)
0208 {
0209     if (m_layoutName == name) {
0210         return;
0211     }
0212 
0213     qDebug() << "Layout name:" << name;
0214 
0215     m_layoutName = name;
0216 
0217     emit nameChanged();
0218 }
0219 
0220 QString AbstractLayout::color() const
0221 {
0222     return m_color;
0223 }
0224 
0225 void AbstractLayout::setColor(QString color)
0226 {
0227     if (m_color == color) {
0228         return;
0229     }
0230 
0231     m_color = color;
0232     emit colorChanged();
0233 }
0234 
0235 QString AbstractLayout::icon() const
0236 {
0237     return m_icon;
0238 }
0239 
0240 void AbstractLayout::setIcon(const QString &icon)
0241 {
0242     if (m_icon == icon) {
0243         return;
0244     }
0245 
0246     m_icon = icon;
0247     emit iconChanged();
0248 }
0249 
0250 QString AbstractLayout::lastUsedActivity() const
0251 {
0252     return m_lastUsedActivity;
0253 }
0254 
0255 void AbstractLayout::clearLastUsedActivity()
0256 {
0257     m_lastUsedActivity = "";
0258     emit lastUsedActivityChanged();
0259 }
0260 
0261 QString AbstractLayout::defaultCustomTextColor()
0262 {
0263     return "#3C1C00";
0264 }
0265 
0266 QString AbstractLayout::defaultCustomBackground()
0267 {
0268     return "defaultcustom";
0269 }
0270 
0271 QString AbstractLayout::defaultTextColor(const QString &color)
0272 {
0273     //! the user is in default layout theme
0274     if (color == QLatin1String("blue")) {
0275         return "#D7E3FF";
0276     } else if (color == QLatin1String("brown")) {
0277         return "#F1DECB";
0278     } else if (color == QLatin1String("darkgrey")) {
0279         return "#ECECEC";
0280     } else if (color == QLatin1String("gold")) {
0281         return "#7C3636";
0282     } else if (color == QLatin1String("green")) {
0283         return "#4D7549";
0284     } else if (color == QLatin1String("lightskyblue")) {
0285         return "#0C2A43";
0286     } else if (color == QLatin1String("orange")) {
0287         return "#6F3902";
0288     } else if (color == QLatin1String("pink")) {
0289         return "#743C46";
0290     } else if (color == QLatin1String("purple")) {
0291         return "#ECD9FF";
0292     }  else if (color == QLatin1String("red")) {
0293         return "#F3E4E4";
0294     }  else if (color == QLatin1String("wheat")) {
0295         return "#6A4E25";
0296     }  else {
0297         return "#FCFCFC";
0298     }
0299 }
0300 
0301 QString AbstractLayout::predefinedTextColor() const
0302 {
0303     return AbstractLayout::defaultTextColor(m_color);
0304 }
0305 
0306 QString AbstractLayout::customTextColor() const
0307 {
0308     return m_customTextColor;
0309 }
0310 
0311 void AbstractLayout::setCustomTextColor(const QString &customColor)
0312 {
0313     if (m_customTextColor == customColor) {
0314         return;
0315     }
0316 
0317     m_customTextColor = customColor;
0318     emit customTextColorChanged();
0319 }
0320 
0321 QStringList AbstractLayout::launchers() const
0322 {
0323     return m_launchers;
0324 }
0325 
0326 void AbstractLayout::setLaunchers(QStringList launcherList)
0327 {
0328     if (m_launchers == launcherList)
0329         return;
0330 
0331     m_launchers = launcherList;
0332 
0333     emit launchersChanged();
0334 }
0335 
0336 Type AbstractLayout::type() const
0337 {
0338     return Type::Abstract;
0339 }
0340 
0341 QList<Plasma::Types::Location> combinedFreeEdges(const QList<Plasma::Types::Location> &edges1, const QList<Plasma::Types::Location> &edges2)
0342 {
0343     QList<Plasma::Types::Location> validFreeEdges;
0344 
0345     for (int i=0; i<edges1.count(); ++i) {
0346         if (edges2.contains(edges1[i])) {
0347             validFreeEdges << edges1[i];
0348         }
0349     }
0350 
0351     return validFreeEdges;
0352 }
0353 
0354 
0355 QString AbstractLayout::layoutName(const QString &fileName)
0356 {
0357     int lastSlash = fileName.lastIndexOf("/");
0358     QString tempLayoutFile = fileName;
0359     QString layoutName = tempLayoutFile.remove(0, lastSlash + 1);
0360 
0361     int ext = layoutName.lastIndexOf(".layout.latte");
0362     layoutName = layoutName.remove(ext, 13);
0363 
0364     return layoutName;
0365 }
0366 
0367 void AbstractLayout::syncSettings()
0368 {
0369     if (QFile(file()).exists()) {
0370         m_layoutGroup.sync();
0371     }
0372 }
0373 
0374 void AbstractLayout::loadConfig()
0375 {
0376     m_version = m_layoutGroup.readEntry("version", 2);
0377     m_launchers = m_layoutGroup.readEntry("launchers", QStringList());
0378     m_lastUsedActivity = m_layoutGroup.readEntry("lastUsedActivity", QString());
0379     m_preferredForShortcutsTouched = m_layoutGroup.readEntry("preferredForShortcutsTouched", false);
0380     m_popUpMargin = m_layoutGroup.readEntry("popUpMargin", -1);
0381 
0382     m_color = m_layoutGroup.readEntry("color", QString("blue"));
0383     m_backgroundStyle = static_cast<BackgroundStyle>(m_layoutGroup.readEntry("backgroundStyle", (int)ColorBackgroundStyle));
0384 
0385     m_schemeFile = m_layoutGroup.readEntry("schemeFile", QString(Data::Layout::DEFAULTSCHEMEFILE));
0386 
0387     if (m_schemeFile.startsWith("~")) {
0388         m_schemeFile.remove(0, 1);
0389         m_schemeFile = QDir::homePath() + m_schemeFile;
0390     }
0391 
0392     m_schemeFile = m_schemeFile.isEmpty() || !QFileInfo(m_schemeFile).exists() ? Data::Layout::DEFAULTSCHEMEFILE : m_schemeFile;
0393 
0394     QString deprecatedTextColor = m_layoutGroup.readEntry("textColor", QString());
0395     QString deprecatedBackground = m_layoutGroup.readEntry("background", QString());
0396 
0397     if (deprecatedBackground.startsWith("/")) {
0398         m_customBackground = deprecatedBackground;
0399         m_customTextColor = deprecatedTextColor;
0400         setBackgroundStyle(PatternBackgroundStyle);
0401 
0402         m_layoutGroup.writeEntry("background", QString());
0403         m_layoutGroup.writeEntry("textColor", QString());
0404 
0405         saveConfig();
0406     } else {
0407         m_customBackground = m_layoutGroup.readEntry("customBackground", QString());
0408         m_customTextColor = m_layoutGroup.readEntry("customTextColor", QString());
0409     }
0410 
0411     m_icon = m_layoutGroup.readEntry("icon", QString());
0412 }
0413 
0414 void AbstractLayout::saveConfig()
0415 {
0416     qDebug() << "abstract layout is saving... for layout:" << m_layoutName;
0417     m_layoutGroup.writeEntry("version", m_version);
0418     m_layoutGroup.writeEntry("color", m_color);
0419     m_layoutGroup.writeEntry("launchers", m_launchers);
0420     m_layoutGroup.writeEntry("backgroundStyle", (int)m_backgroundStyle);
0421     m_layoutGroup.writeEntry("customBackground", m_customBackground);
0422     m_layoutGroup.writeEntry("customTextColor", m_customTextColor);
0423     m_layoutGroup.writeEntry("icon", m_icon);
0424     m_layoutGroup.writeEntry("lastUsedActivity", m_lastUsedActivity);
0425     m_layoutGroup.writeEntry("popUpMargin", m_popUpMargin);
0426     m_layoutGroup.writeEntry("preferredForShortcutsTouched", m_preferredForShortcutsTouched);
0427 
0428     QString scmfile = m_schemeFile;
0429 
0430     if (scmfile.startsWith(QDir::homePath())) {
0431         scmfile.remove(0, QDir::homePath().size());
0432         scmfile = "~" + scmfile;
0433     }
0434     m_layoutGroup.writeEntry("schemeFile", scmfile == Data::Layout::DEFAULTSCHEMEFILE ? "" : scmfile);
0435 
0436     m_layoutGroup.sync();
0437 }
0438 
0439 }
0440 }