Files
ukui-panel/plugin-quicklaunch/quicklaunchaction.cpp

197 lines
6.2 KiB
C++
Raw Permalink Normal View History

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 */
#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>
#include "ukuiquicklaunch.h"
#include <QMessageBox>
#include <XdgDesktopFile>
#include <QFileInfo>
2020-01-17 17:23:40 +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(); });
}
/*用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();
QString title(xdg->localizedValue("Name").toString());
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);
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(); });
}
/*解析Exec字段*/
2020-01-17 17:23:40 +08:00
void QuickLaunchAction::execAction(QString additionalAction)
{
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)
{
case ActionLegacy:
2020-10-19 13:40:10 +08:00
if (!QProcess::startDetached(exec))
showQMessage =true;
break;
case ActionXdg: {
XdgDesktopFile xdg;
if(xdg.load(exec))
{
if (additionalAction.isEmpty()){
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;
g_object_unref(appinfo);
} else {
if (!xdg.actionActivate(additionalAction, QStringList{}))
showQMessage =true;
}
#if 0
} else {
//xdg 的方式实现点击打开应用,可正确读取转义的字符
if (additionalAction.isEmpty()){
if (!xdg.startDetached())
2020-10-19 13:40:10 +08:00
showQMessage =true;
} else {
2020-10-19 13:40:10 +08:00
if (!xdg.actionActivate(additionalAction, QStringList{}))
showQMessage =true;
}
}
#endif
2020-10-19 13:40:10 +08:00
} else
showQMessage =true;
}
break;
case ActionFile:
QFileInfo fileinfo(exec);
QString openfile = exec;
if (fileinfo.isSymLink()) {
openfile = fileinfo.symLinkTarget();
}
if (fileinfo.exists()) {
QDesktopServices::openUrl(QUrl::fromLocalFile(openfile));
} else {
2020-10-19 13:40:10 +08:00
showQMessage =true;
2020-09-24 15:39:00 +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
}