File indexing completed on 2024-04-28 16:49:36

0001 /*
0002 *  Copyright 2018 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 "menu.h"
0021 
0022 // local
0023 #include "../../liblatte2/types.h"
0024 
0025 // Qt
0026 #include <QAction>
0027 #include <QDebug>
0028 #include <QFont>
0029 #include <QMenu>
0030 #include <QtDBus>
0031 #include <QTimer>
0032 
0033 // KDE
0034 #include <KActionCollection>
0035 #include <KLocalizedString>
0036 
0037 // Plasma
0038 #include <Plasma/Containment>
0039 #include <Plasma/Corona>
0040 #include <Plasma/ServiceJob>
0041 
0042 const int LAYOUTSPOS = 3;
0043 
0044 Menu::Menu(QObject *parent, const QVariantList &args)
0045     : Plasma::ContainmentActions(parent, args)
0046 {
0047     makeActions();
0048 }
0049 
0050 Menu::~Menu()
0051 {
0052     m_separator1->deleteLater();
0053     m_addWidgetsAction->deleteLater();
0054     m_configureAction->deleteLater();
0055     m_printAction->deleteLater();
0056     m_switchLayoutsMenu->deleteLater();
0057     m_layoutsAction->deleteLater();
0058 }
0059 
0060 void Menu::makeActions()
0061 {
0062     m_separator1 = new QAction(this);
0063     m_separator1->setSeparator(true);
0064 
0065     m_printAction = new QAction(QIcon::fromTheme("edit"), "Print Message...", this);
0066     connect(m_printAction, &QAction::triggered, [ = ]() {
0067         qDebug() << "Action Trigerred !!!";
0068     });
0069 
0070     m_addWidgetsAction = new QAction(QIcon::fromTheme("add"), i18n("&Add Widgets..."), this);
0071     m_addWidgetsAction->setStatusTip(i18n("Show Plasma Widget Explorer"));
0072     connect(m_addWidgetsAction, &QAction::triggered, [ = ]() {
0073         QDBusInterface iface("org.kde.plasmashell", "/PlasmaShell", "", QDBusConnection::sessionBus());
0074 
0075         if (iface.isValid()) {
0076             iface.call("toggleWidgetExplorer");
0077         }
0078     });
0079 
0080     m_configureAction = new QAction(QIcon::fromTheme("configure"), i18nc("view settings window", "View &Settings..."), this);
0081     connect(m_configureAction, &QAction::triggered, this, &Menu::requestConfiguration);
0082 
0083     connect(this->containment(), &Plasma::Containment::userConfiguringChanged, this, [&](bool configuring){
0084         m_configureAction->setVisible(!configuring);
0085         // because sometimes it's disabled unexpectedly
0086         // we should enable it
0087         m_configureAction->setEnabled(true);
0088     });
0089 
0090     m_switchLayoutsMenu = new QMenu;
0091     m_layoutsAction = m_switchLayoutsMenu->menuAction();
0092     m_layoutsAction->setText(i18n("&Layouts"));
0093     m_layoutsAction->setIcon(QIcon::fromTheme("user-identity"));
0094     m_layoutsAction->setStatusTip(i18n("Switch to another layout"));
0095 
0096     connect(m_switchLayoutsMenu, &QMenu::aboutToShow, this, &Menu::populateLayouts);
0097     connect(m_switchLayoutsMenu, &QMenu::triggered, this, &Menu::switchToLayout);
0098 }
0099 
0100 
0101 void Menu::requestConfiguration()
0102 {
0103     if (this->containment()) {
0104         emit this->containment()->configureRequested(containment());
0105     }
0106 }
0107 
0108 
0109 QList<QAction *> Menu::contextualActions()
0110 {
0111     QList<QAction *> actions;
0112     actions << m_separator1;
0113     //actions << m_printAction;
0114     actions << m_layoutsAction;
0115     actions << m_addWidgetsAction;
0116     actions << m_configureAction;
0117 
0118     m_data.clear();
0119     QDBusInterface iface("org.kde.lattedock", "/Latte", "", QDBusConnection::sessionBus());
0120 
0121     if (iface.isValid()) {
0122         iface.call("setContextMenuView", (int)containment()->id());
0123         QDBusReply<QStringList> replyData = iface.call("contextMenuData");
0124 
0125         m_data = replyData.value();
0126     }
0127 
0128     if (m_data.size() > LAYOUTSPOS + 1) {
0129         m_layoutsAction->setEnabled(true);
0130         m_layoutsAction->setVisible(true);
0131     } else {
0132         m_layoutsAction->setVisible(false);
0133     }
0134 
0135     Latte::Types::ViewType viewType{Latte::Types::DockView};
0136 
0137     if (m_data.size() >= LAYOUTSPOS + 1) {
0138         viewType = static_cast<Latte::Types::ViewType>((m_data[2]).toInt());
0139     }
0140 
0141     const QString configureActionText = (viewType == Latte::Types::DockView) ? i18nc("dock settings window", "Dock &Settings...") : i18nc("panel settings window", "Panel &Settings...");
0142     m_configureAction->setText(configureActionText);
0143 
0144     return actions;
0145 }
0146 
0147 QAction *Menu::action(const QString &name)
0148 {
0149     if (name == "add widgets") {
0150         return m_addWidgetsAction;
0151     } else if (name == "configure") {
0152         return m_configureAction;
0153     } else if (name == "layouts") {
0154         return m_layoutsAction;
0155     }
0156 
0157     return nullptr;
0158 }
0159 
0160 void Menu::populateLayouts()
0161 {
0162     m_switchLayoutsMenu->clear();
0163 
0164     if (m_data.size() > LAYOUTSPOS + 1) {
0165         //when there are more than 1 layouts present
0166         Latte::Types::LayoutsMemoryUsage memoryUsage = static_cast<Latte::Types::LayoutsMemoryUsage>((m_data[0]).toInt());
0167         QString currentName = m_data[1];
0168 
0169         for (int i = LAYOUTSPOS; i < m_data.size(); ++i) {
0170             bool isActive = m_data[i].startsWith("0") ? false : true;
0171 
0172             QString layout = m_data[i].right(m_data[i].length() - 2);
0173 
0174             QString currentText = (memoryUsage == Latte::Types::MultipleLayouts && layout == currentName) ?
0175                                   (" " + i18nc("current layout", "(Current)")) : "";
0176             QString layoutName = layout + currentText;
0177 
0178             QAction *layoutAction = new QAction(layoutName, m_switchLayoutsMenu);
0179 
0180             layoutAction->setCheckable(true);
0181 
0182             if (isActive) {
0183                 layoutAction->setChecked(true);
0184             } else {
0185                 layoutAction->setChecked(false);
0186             }
0187 
0188             layoutAction->setData(layout);
0189 
0190             if (isActive) {
0191                 QFont font = layoutAction->font();
0192                 font.setBold(true);
0193                 layoutAction->setFont(font);
0194             }
0195 
0196             m_switchLayoutsMenu->addAction(layoutAction);
0197         }
0198 
0199         m_switchLayoutsMenu->addSeparator();
0200 
0201         QAction *editLayoutsAction = new QAction(i18n("Configure..."), m_switchLayoutsMenu);
0202         editLayoutsAction->setData(QStringLiteral(" _show_latte_settings_dialog_"));
0203         m_switchLayoutsMenu->addAction(editLayoutsAction);
0204     }
0205 }
0206 
0207 void Menu::switchToLayout(QAction *action)
0208 {
0209     const QString layout = action->data().toString();
0210 
0211     if (layout == " _show_latte_settings_dialog_") {
0212         QTimer::singleShot(400, [this]() {
0213             QDBusInterface iface("org.kde.lattedock", "/Latte", "", QDBusConnection::sessionBus());
0214 
0215             if (iface.isValid()) {
0216                 iface.call("showSettingsWindow", (int)Latte::Types::LayoutPage);
0217             }
0218         });
0219     } else {
0220         QTimer::singleShot(400, [this, layout]() {
0221             QDBusInterface iface("org.kde.lattedock", "/Latte", "", QDBusConnection::sessionBus());
0222 
0223             if (iface.isValid()) {
0224                 iface.call("switchToLayout", layout);
0225             }
0226         });
0227     }
0228 }
0229 
0230 K_EXPORT_PLASMA_CONTAINMENTACTIONS_WITH_JSON(lattecontextmenu, Menu, "plasma-containmentactions-lattecontextmenu.json")
0231 
0232 #include "menu.moc"