File: quicklaunchaction.cpp

package info (click to toggle)
lxqt-panel 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 15,748 kB
  • sloc: cpp: 27,548; xml: 798; makefile: 19
file content (185 lines) | stat: -rw-r--r-- 6,034 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * LXQt - a lightweight, Qt based, desktop toolset
 * https://lxqt.org
 *
 * Copyright: 2010-2011 Razor team
 * Authors:
 *   Petr Vanek <petr@scribus.info>
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include "quicklaunchaction.h"
#include <QDesktopServices>
#include <QFileIconProvider>
#include <QMimeDatabase>
#include <QMessageBox>
#include <QProcess>
#include <QUrl>
#include <XdgDesktopFile>
#include <XdgIcon>
#include <XdgMimeType>


QuickLaunchAction::QuickLaunchAction(const QString & name,
                                     const QString & exec,
                                     const QString & icon,
                                     QWidget * parent)
    : QAction(name, parent),
      m_valid(true)
{
    m_type = ActionLegacy;

    m_settingsMap[QStringLiteral("name")] = name;
    m_settingsMap[QStringLiteral("exec")] = exec;
    m_settingsMap[QStringLiteral("icon")] = icon;

    // Since the keys "desktop" and "file" have priority over the above keys
    // (see LXQtQuickLaunch::LXQtQuickLaunch), we prevent their reading
    // from another config file by setting them to empty strings here.
    m_settingsMap[QStringLiteral("desktop")] = QString();
    m_settingsMap[QStringLiteral("file")] = QString();

    if (icon == QLatin1String("") || icon.isNull())
        setIcon(XdgIcon::defaultApplicationIcon());
    else
        setIcon(QIcon(icon));

    setData(exec);
    connect(this, &QAction::triggered, this, [this] { execAction(); });
}

QuickLaunchAction::QuickLaunchAction(const XdgDesktopFile * xdg,
                                     QWidget * parent)
    : QAction(parent),
      m_valid(true)
{
    m_type = ActionXdg;

    m_settingsMap[QStringLiteral("desktop")] = xdg->fileName();

    QString title(xdg->localizedValue(QStringLiteral("Name")).toString());
    QString gn(xdg->localizedValue(QStringLiteral("GenericName")).toString());
    if (!gn.isEmpty())
        title += QLatin1String(" (") + gn + QLatin1String(")");
    setText(title);

    setIcon(xdg->icon(XdgIcon::defaultApplicationIcon()));

    setData(xdg->fileName());
    connect(this, &QAction::triggered, this, [this] { execAction(); });

    // populate the additional actions
    for (auto const & action : const_cast<const QStringList &&>(xdg->actions()))
    {
        QAction * act = new QAction{xdg->actionIcon(action), xdg->actionName(action), this};
        act->setData(action);
        connect(act, &QAction::triggered, this, [this, act] { execAction(act->data().toString()); });
        m_additionalActions.push_back(act);
    }
}

QuickLaunchAction::QuickLaunchAction(const QString & fileName, QWidget * parent)
    : QAction(parent),
      m_valid(true)
{
    m_type = ActionFile;
    setText(fileName);
    setData(fileName);

    m_settingsMap[QStringLiteral("file")] = fileName;
    // prevent reading of "desktop" from another config file
    m_settingsMap[QStringLiteral("desktop")] = QString();

    QFileInfo fi(fileName);
    if (fi.isDir())
    {
        QFileIconProvider ip;
        setIcon(ip.icon(fi));
    }
    else
    {
        QMimeDatabase db;
        XdgMimeType mi(db.mimeTypeForFile(fi));
        setIcon(mi.icon());
    }

    connect(this, &QAction::triggered, this, [this] { execAction(); });
}

void QuickLaunchAction::execAction(QString additionalAction)
{
    QString exec(data().toString());
    qDebug() << "execAction" << exec;
    switch (m_type)
    {
        case ActionLegacy:
        {
            QStringList args = QProcess::splitCommand(exec);
            QProcess::startDetached(args.takeFirst(), args);
            break;
        }
        case ActionXdg:
        {
            XdgDesktopFile xdg;
            if(xdg.load(exec))
            {
                if (additionalAction.isEmpty())
                    xdg.startDetached();
                else
                    xdg.actionActivate(additionalAction, QStringList{});
            }
            break;
        }
        case ActionFile:
            QDesktopServices::openUrl(QUrl(exec));
            break;
    }
}

void QuickLaunchAction::updateXdgAction()
{
    if (m_valid && m_type == ActionXdg)
    {
        XdgDesktopFile xdg;
        if (xdg.load(data().toString()) && xdg.isSuitable())
        {
            QString title(xdg.localizedValue(QStringLiteral("Name")).toString());
            QString gn(xdg.localizedValue(QStringLiteral("GenericName")).toString());
            if (!gn.isEmpty())
                title += QLatin1String(" (") + gn + QLatin1String(")");
            setText(title);
            setIcon(xdg.icon(XdgIcon::defaultApplicationIcon()));

            qDeleteAll (m_additionalActions);
            m_additionalActions.clear();
            for (auto const & action : const_cast<const QStringList &&>(xdg.actions()))
            {
                QAction * act = new QAction{xdg.actionIcon(action), xdg.actionName(action), this};
                act->setData(action);
                connect(act, &QAction::triggered, this, [this, act] { execAction(act->data().toString()); });
                m_additionalActions.push_back(act);
            }
        }
        else
        {
            qDebug() << "XdgDesktopFile" << data() << "is not valid or applicable";
        }
    }
}