File indexing completed on 2024-04-14 15:37:11

0001 /*
0002 *  Copyright 2019  Michail Vourlakos <mvourlakos@gmail.com>
0003 *
0004 *  This file is part of Latte-Dock
0005 *
0006 *  Latte-Dock is free software; you can redistribute it and/or
0007 *  modify it under the terms of the GNU General Public License as
0008 *  published by the Free Software Foundation; either version 2 of
0009 *  the License, or (at your option) any later version.
0010 *
0011 *  Latte-Dock is distributed in the hope that it will be useful,
0012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 *  GNU General Public License for more details.
0015 *
0016 *  You should have received a copy of the GNU General Public License
0017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #include "abstractlayout.h"
0021 
0022 // Qt
0023 #include <QDir>
0024 #include <QDebug>
0025 #include <QFile>
0026 
0027 // KDE
0028 #include <KSharedConfig>
0029 
0030 namespace Latte {
0031 namespace Layout {
0032 
0033 const QString AbstractLayout::MultipleLayoutsName = ".multiple-layouts_hidden";
0034 
0035 AbstractLayout::AbstractLayout(QObject *parent, QString layoutFile, QString assignedName)
0036     : QObject(parent)
0037 {
0038     qDebug() << "Layout file to create object: " << layoutFile << " with name: " << assignedName;
0039 
0040     if (QFile(layoutFile).exists()) {
0041         if (assignedName.isEmpty()) {
0042             assignedName =  layoutName(layoutFile);
0043         }
0044 
0045         //!this order is important because setFile initializes also the m_layoutGroup
0046         setFile(layoutFile);
0047         setName(assignedName);
0048         loadConfig();
0049         init();
0050 
0051         m_loadedCorrectly = true;
0052     }
0053 }
0054 
0055 AbstractLayout::~AbstractLayout()
0056 {
0057 }
0058 
0059 void AbstractLayout::init()
0060 {
0061     connect(this, &AbstractLayout::backgroundChanged, this, &AbstractLayout::saveConfig);
0062     connect(this, &AbstractLayout::colorChanged, this, &AbstractLayout::textColorChanged);
0063     connect(this, &AbstractLayout::lastUsedActivityChanged, this, &AbstractLayout::saveConfig);
0064     connect(this, &AbstractLayout::launchersChanged, this, &AbstractLayout::saveConfig);
0065     connect(this, &AbstractLayout::preferredForShortcutsTouchedChanged, this, &AbstractLayout::saveConfig);
0066     connect(this, &AbstractLayout::textColorChanged, this, &AbstractLayout::saveConfig);
0067     connect(this, &AbstractLayout::versionChanged, this, &AbstractLayout::saveConfig);
0068 }
0069 
0070 int AbstractLayout::version() const
0071 {
0072     return m_version;
0073 }
0074 
0075 void AbstractLayout::setVersion(int ver)
0076 {
0077     if (m_version == ver) {
0078         return;
0079     }
0080 
0081     m_version = ver;
0082 
0083     emit versionChanged();
0084 }
0085 
0086 
0087 bool AbstractLayout::preferredForShortcutsTouched() const
0088 {
0089     return m_preferredForShortcutsTouched;
0090 }
0091 
0092 void AbstractLayout::setPreferredForShortcutsTouched(bool touched)
0093 {
0094     if (m_preferredForShortcutsTouched == touched) {
0095         return;
0096     }
0097 
0098     m_preferredForShortcutsTouched = touched;
0099     emit preferredForShortcutsTouchedChanged();
0100 }
0101 
0102 QString AbstractLayout::background() const
0103 {
0104     return m_background;
0105 }
0106 
0107 void AbstractLayout::setBackground(QString path)
0108 {
0109     if (path == m_background) {
0110         return;
0111     }
0112 
0113     if (!path.isEmpty() && !QFileInfo(path).exists()) {
0114         return;
0115     }
0116 
0117     m_background = path;
0118 
0119     //! initialize the text color also
0120     if (path.isEmpty()) {
0121         setTextColor(QString());
0122     }
0123 
0124     emit backgroundChanged();
0125 }
0126 
0127 
0128 QString AbstractLayout::file() const
0129 {
0130     return m_layoutFile;
0131 }
0132 
0133 void AbstractLayout::setFile(QString file)
0134 {
0135     if (m_layoutFile == file) {
0136         return;
0137     }
0138 
0139     qDebug() << "Layout file:" << file;
0140 
0141     m_layoutFile = file;
0142 
0143     KSharedConfigPtr filePtr = KSharedConfig::openConfig(m_layoutFile);
0144     m_layoutGroup = KConfigGroup(filePtr, "LayoutSettings");
0145 
0146     emit fileChanged();
0147 }
0148 
0149 QString AbstractLayout::name() const
0150 {
0151     return m_layoutName;
0152 }
0153 
0154 void AbstractLayout::setName(QString name)
0155 {
0156     if (m_layoutName == name) {
0157         return;
0158     }
0159 
0160     qDebug() << "Layout name:" << name;
0161 
0162     m_layoutName = name;
0163 
0164     emit nameChanged();
0165 }
0166 
0167 QString AbstractLayout::color() const
0168 {
0169     return m_color;
0170 }
0171 
0172 void AbstractLayout::setColor(QString color)
0173 {
0174     if (m_color == color) {
0175         return;
0176     }
0177 
0178     m_color = color;
0179     emit colorChanged();
0180 }
0181 
0182 QString AbstractLayout::lastUsedActivity()
0183 {
0184     return m_lastUsedActivity;
0185 }
0186 
0187 void AbstractLayout::clearLastUsedActivity()
0188 {
0189     m_lastUsedActivity = "";
0190     emit lastUsedActivityChanged();
0191 }
0192 
0193 QString AbstractLayout::textColor() const
0194 {
0195     //! the user is in default layout theme
0196     if (m_background.isEmpty()) {
0197         if (m_color == "blue") {
0198             return "#D7E3FF";
0199         } else if (m_color == "brown") {
0200             return "#F1DECB";
0201         } else if (m_color == "darkgrey") {
0202             return "#ECECEC";
0203         } else if (m_color == "gold") {
0204             return "#7C3636";
0205         } else if (m_color == "green") {
0206             return "#4D7549";
0207         } else if (m_color == "lightskyblue") {
0208             return "#0C2A43";
0209         } else if (m_color == "orange") {
0210             return "#6F3902";
0211         } else if (m_color == "pink") {
0212             return "#743C46";
0213         } else if (m_color == "purple") {
0214             return "#ECD9FF";
0215         }  else if (m_color == "red") {
0216             return "#F3E4E4";
0217         }  else if (m_color == "wheat") {
0218             return "#6A4E25";
0219         }  else {
0220             return "#FCFCFC";
0221         }
0222     }
0223 
0224     return "#" + m_textColor;
0225 }
0226 
0227 void AbstractLayout::setTextColor(QString color)
0228 {
0229     //! remove # if someone is trying to set it this way
0230     if (color.startsWith("#")) {
0231         color.remove(0, 1);
0232     }
0233 
0234     if (m_textColor == color) {
0235         return;
0236     }
0237 
0238     m_textColor = color;
0239     emit textColorChanged();
0240 }
0241 
0242 QStringList AbstractLayout::launchers() const
0243 {
0244     return m_launchers;
0245 }
0246 
0247 void AbstractLayout::setLaunchers(QStringList launcherList)
0248 {
0249     if (m_launchers == launcherList)
0250         return;
0251 
0252     m_launchers = launcherList;
0253 
0254     emit launchersChanged();
0255 }
0256 
0257 Type AbstractLayout::type() const
0258 {
0259     return Type::Abstract;
0260 }
0261 
0262 QList<Plasma::Types::Location> combinedFreeEdges(const QList<Plasma::Types::Location> &edges1, const QList<Plasma::Types::Location> &edges2)
0263 {
0264     QList<Plasma::Types::Location> validFreeEdges;
0265 
0266     for (int i=0; i<edges1.count(); ++i) {
0267         if (edges2.contains(edges1[i])) {
0268             validFreeEdges << edges1[i];
0269         }
0270     }
0271 
0272     return validFreeEdges;
0273 }
0274 
0275 
0276 QString AbstractLayout::layoutName(const QString &fileName)
0277 {
0278     int lastSlash = fileName.lastIndexOf("/");
0279     QString tempLayoutFile = fileName;
0280     QString layoutName = tempLayoutFile.remove(0, lastSlash + 1);
0281 
0282     int ext = layoutName.lastIndexOf(".layout.latte");
0283     layoutName = layoutName.remove(ext, 13);
0284 
0285     return layoutName;
0286 }
0287 
0288 void AbstractLayout::loadConfig()
0289 {
0290     m_version = m_layoutGroup.readEntry("version", 2);
0291     m_color = m_layoutGroup.readEntry("color", QString("blue"));
0292     m_textColor = m_layoutGroup.readEntry("textColor", QString("fcfcfc"));
0293     m_launchers = m_layoutGroup.readEntry("launchers", QStringList());
0294     m_lastUsedActivity = m_layoutGroup.readEntry("lastUsedActivity", QString());
0295     m_preferredForShortcutsTouched = m_layoutGroup.readEntry("preferredForShortcutsTouched", false);
0296 
0297     QString back = m_layoutGroup.readEntry("background", "");
0298 
0299     if (!back.isEmpty()) {
0300         if (QFileInfo(back).exists()) {
0301             m_background = back;
0302         } else {
0303             m_layoutGroup.writeEntry("background", QString());
0304         }
0305     }
0306 
0307   //  emit activitiesChanged();*/
0308 }
0309 
0310 void AbstractLayout::saveConfig()
0311 {
0312     qDebug() << "abstract layout is saving... for layout:" << m_layoutName;
0313     m_layoutGroup.writeEntry("version", m_version);
0314     m_layoutGroup.writeEntry("color", m_color);
0315     m_layoutGroup.writeEntry("launchers", m_launchers);
0316     m_layoutGroup.writeEntry("background", m_background);
0317     m_layoutGroup.writeEntry("lastUsedActivity", m_lastUsedActivity);
0318     m_layoutGroup.writeEntry("textColor", m_textColor);
0319     m_layoutGroup.writeEntry("preferredForShortcutsTouched", m_preferredForShortcutsTouched);
0320 
0321     m_layoutGroup.sync();
0322 }
0323 
0324 }
0325 }