2020-01-17 17:23:40 +08:00
|
|
|
/* BEGIN_COMMON_COPYRIGHT_HEADER
|
|
|
|
|
* (c)LGPL2+
|
|
|
|
|
*
|
|
|
|
|
* Copyright: 2010-2011 Razor team
|
|
|
|
|
* Authors:
|
|
|
|
|
* Petr Vanek <petr@scribus.info>
|
|
|
|
|
*
|
2020-01-19 10:27:16 +08:00
|
|
|
* Copyright: 2019 Tianjin KYLIN Information Technology Co., Ltd. *
|
2020-01-17 17:23:40 +08:00
|
|
|
*
|
|
|
|
|
* 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 */
|
|
|
|
|
|
2020-06-28 17:22:09 +08:00
|
|
|
#include <gio/gdesktopappinfo.h>
|
2020-01-17 17:23:40 +08:00
|
|
|
#include "quicklaunchaction.h"
|
|
|
|
|
#include <QDesktopServices>
|
|
|
|
|
#include <QFileIconProvider>
|
|
|
|
|
#include <QMimeDatabase>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QUrl>
|
|
|
|
|
#include <XdgDesktopFile>
|
|
|
|
|
#include <XdgIcon>
|
|
|
|
|
#include <XdgMimeType>
|
2020-10-19 09:54:56 +08:00
|
|
|
#include "ukuiquicklaunch.h"
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <XdgDesktopFile>
|
|
|
|
|
#include <QFileInfo>
|
2020-01-17 17:23:40 +08:00
|
|
|
|
2020-06-28 17:22:09 +08:00
|
|
|
/*传入参数为三个字段*/
|
2020-01-17 17:23:40 +08:00
|
|
|
QuickLaunchAction::QuickLaunchAction(const QString & name,
|
|
|
|
|
const QString & exec,
|
|
|
|
|
const QString & icon,
|
|
|
|
|
QWidget * parent)
|
|
|
|
|
: QAction(name, parent),
|
|
|
|
|
m_valid(true)
|
|
|
|
|
{
|
|
|
|
|
m_type = ActionLegacy;
|
|
|
|
|
|
|
|
|
|
m_settingsMap["name"] = name;
|
|
|
|
|
m_settingsMap["exec"] = exec;
|
|
|
|
|
m_settingsMap["icon"] = icon;
|
|
|
|
|
|
|
|
|
|
if (icon == "" || icon.isNull())
|
|
|
|
|
setIcon(XdgIcon::defaultApplicationIcon());
|
|
|
|
|
else
|
|
|
|
|
setIcon(QIcon(icon));
|
|
|
|
|
|
|
|
|
|
setData(exec);
|
|
|
|
|
connect(this, &QAction::triggered, this, [this] { execAction(); });
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 17:22:09 +08:00
|
|
|
/*用xdg的方式解析*/
|
2020-01-17 17:23:40 +08:00
|
|
|
QuickLaunchAction::QuickLaunchAction(const XdgDesktopFile * xdg,
|
|
|
|
|
QWidget * parent)
|
|
|
|
|
: QAction(parent),
|
|
|
|
|
m_valid(true)
|
|
|
|
|
{
|
|
|
|
|
m_type = ActionXdg;
|
|
|
|
|
|
|
|
|
|
m_settingsMap["desktop"] = xdg->fileName();
|
|
|
|
|
|
2020-10-16 15:03:18 +08:00
|
|
|
QString title(xdg->localizedValue("Name").toString());
|
2020-12-28 16:13:50 +08:00
|
|
|
QIcon icon=QIcon::fromTheme(xdg->localizedValue("Icon").toString());
|
|
|
|
|
//add special path search /use/share/pixmaps
|
|
|
|
|
if (icon.isNull())
|
|
|
|
|
{
|
|
|
|
|
QString path = QString("/usr/share/pixmaps/%1.%2").arg(xdg->localizedValue("Icon").toString()).arg("png");
|
|
|
|
|
QString path_svg = QString("/usr/share/pixmaps/%1.%2").arg(xdg->localizedValue("Icon").toString()).arg("svg");
|
|
|
|
|
//qDebug() << "createDesktopFileThumbnail path:" <<path;
|
|
|
|
|
if(QFile::exists(path)){
|
|
|
|
|
icon=QIcon(path);
|
|
|
|
|
}
|
|
|
|
|
else if(QFile::exists(path_svg)){
|
|
|
|
|
icon=QIcon(path_svg);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-07 09:07:58 +08:00
|
|
|
if (icon.isNull())
|
|
|
|
|
icon = xdg->icon();
|
2020-01-17 17:23:40 +08:00
|
|
|
setText(title);
|
|
|
|
|
|
2020-12-28 16:13:50 +08:00
|
|
|
setIcon(icon);
|
2020-01-17 17:23:40 +08:00
|
|
|
|
|
|
|
|
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, act] { execAction(act->data().toString()); });
|
|
|
|
|
m_addtitionalActions.push_back(act);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QuickLaunchAction::QuickLaunchAction(const QString & fileName, QWidget * parent)
|
|
|
|
|
: QAction(parent),
|
|
|
|
|
m_valid(true)
|
|
|
|
|
{
|
|
|
|
|
m_type = ActionFile;
|
|
|
|
|
setText(fileName);
|
|
|
|
|
setData(fileName);
|
|
|
|
|
|
|
|
|
|
m_settingsMap["file"] = fileName;
|
|
|
|
|
|
|
|
|
|
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(); });
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 17:22:09 +08:00
|
|
|
/*解析Exec字段*/
|
2020-01-17 17:23:40 +08:00
|
|
|
void QuickLaunchAction::execAction(QString additionalAction)
|
|
|
|
|
{
|
2020-10-19 09:54:56 +08:00
|
|
|
UKUIQuickLaunch *uqk = qobject_cast<UKUIQuickLaunch*>(parent());
|
2020-01-17 17:23:40 +08:00
|
|
|
QString exec(data().toString());
|
2020-10-19 13:40:10 +08:00
|
|
|
bool showQMessage = false;
|
2020-01-17 17:23:40 +08:00
|
|
|
switch (m_type)
|
|
|
|
|
{
|
2020-10-19 09:54:56 +08:00
|
|
|
case ActionLegacy:
|
2020-10-19 13:40:10 +08:00
|
|
|
if (!QProcess::startDetached(exec))
|
|
|
|
|
showQMessage =true;
|
2020-10-19 09:54:56 +08:00
|
|
|
break;
|
|
|
|
|
case ActionXdg: {
|
|
|
|
|
XdgDesktopFile xdg;
|
|
|
|
|
if(xdg.load(exec))
|
|
|
|
|
{
|
2021-01-15 14:45:14 +08:00
|
|
|
if (additionalAction.isEmpty()){
|
2021-01-09 17:04:30 +08:00
|
|
|
GDesktopAppInfo * appinfo=g_desktop_app_info_new_from_filename(xdg.fileName().toStdString().data());
|
|
|
|
|
if (!g_app_info_launch_uris(G_APP_INFO(appinfo),nullptr, nullptr, nullptr))
|
2020-10-19 13:40:10 +08:00
|
|
|
showQMessage =true;
|
2020-10-19 09:54:56 +08:00
|
|
|
g_object_unref(appinfo);
|
2021-01-15 14:40:11 +08:00
|
|
|
} else {
|
|
|
|
|
if (!xdg.actionActivate(additionalAction, QStringList{}))
|
|
|
|
|
showQMessage =true;
|
|
|
|
|
}
|
2020-12-24 14:32:50 +08:00
|
|
|
#if 0
|
2020-10-19 09:54:56 +08:00
|
|
|
} else {
|
|
|
|
|
//xdg 的方式实现点击打开应用,可正确读取转义的字符
|
2021-01-15 14:40:11 +08:00
|
|
|
if (additionalAction.isEmpty()){
|
2020-11-17 17:54:16 +08:00
|
|
|
if (!xdg.startDetached())
|
2020-10-19 13:40:10 +08:00
|
|
|
showQMessage =true;
|
2020-10-19 09:54:56 +08:00
|
|
|
} else {
|
2020-10-19 13:40:10 +08:00
|
|
|
if (!xdg.actionActivate(additionalAction, QStringList{}))
|
|
|
|
|
showQMessage =true;
|
2020-10-19 09:54:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-12-24 14:32:50 +08:00
|
|
|
#endif
|
2020-10-19 13:40:10 +08:00
|
|
|
} else
|
|
|
|
|
showQMessage =true;
|
|
|
|
|
}
|
2020-10-19 09:54:56 +08:00
|
|
|
break;
|
|
|
|
|
case ActionFile:
|
|
|
|
|
QFileInfo fileinfo(exec);
|
2020-12-24 10:56:48 +08:00
|
|
|
QString openfile = exec;
|
|
|
|
|
if (fileinfo.isSymLink()) {
|
|
|
|
|
openfile = fileinfo.symLinkTarget();
|
|
|
|
|
}
|
2020-10-19 09:54:56 +08:00
|
|
|
if (fileinfo.exists()) {
|
2020-12-24 10:56:48 +08:00
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(openfile));
|
2020-10-19 09:54:56 +08:00
|
|
|
} else {
|
2020-10-19 13:40:10 +08:00
|
|
|
showQMessage =true;
|
2020-09-24 15:39:00 +08:00
|
|
|
}
|
2020-10-19 09:54:56 +08:00
|
|
|
break;
|
2020-01-17 17:23:40 +08:00
|
|
|
}
|
2020-10-19 13:40:10 +08:00
|
|
|
if (showQMessage) {
|
|
|
|
|
qWarning() << "XdgDesktopFile" << exec << "is not valid";
|
|
|
|
|
QMessageBox::information(uqk, tr("Error Path"),
|
|
|
|
|
tr("File/URL cannot be opened cause invalid path.")
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-01-17 17:23:40 +08:00
|
|
|
}
|