Commit a761ac13 authored by Laurent Montel's avatar Laurent Montel 😁

Continue to implement external script

parent fd8b5736
......@@ -43,7 +43,6 @@ endmacro ()
add_messageviewer_viewerplugin_class_unittest(viewerpluginmanagertest.cpp)
add_messageviewer_viewerplugin_class_unittest(viewerplugintoolmanagertest.cpp)
add_messageviewer_viewerplugin_widget_unittest(todoedittest.cpp "../createtodoplugin/todoedit.cpp")
add_messageviewer_viewerplugin_widget_unittest(noteedittest.cpp "../createnoteplugin/noteedit.cpp")
set_tests_properties(messageviewerplugins-todoedittest PROPERTIES ENVIRONMENT "LC_ALL=C")
......
......@@ -14,4 +14,7 @@ target_link_libraries(messageviewer_externalscriptplugin
)
install(TARGETS messageviewer_externalscriptplugin DESTINATION ${PLUGIN_INSTALL_DIR}/messageviewer)
if (KDEPIMADDONS_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
file( GLOB externalScriptDesktopFiles "*.desktop" )
install( FILES ${externalScriptDesktopFiles} DESTINATION "${KDE_INSTALL_DATADIR}/messageviewerplugins/" )
[Desktop Entry]
Name=Search Subject on Web
Executable=firefox
CommandLine=%s
Description=Search subject on web
......@@ -23,9 +23,12 @@
#include <QIcon>
#include <QAction>
#include <QDebug>
#include <QStandardPaths>
#include <QProcess>
#include <KActionCollection>
#include <KLocalizedString>
#include <KMessageBox>
using namespace MessageViewer;
......@@ -63,7 +66,20 @@ void ViewerPluginExternalscriptInterface::setText(const QString &text)
void ViewerPluginExternalscriptInterface::execute()
{
if (mCurrentInfo.count() == 2) {
//TODO
const QString newCommandLine = adaptArguments(mCurrentInfo.at(0));
const QString executable = mCurrentInfo.at(1);
const QString executablePath = QStandardPaths::findExecutable(executable);
if (executablePath.isEmpty()) {
KMessageBox::error(0, i18n("\'%1\' not found", executable), i18n("Executable not found."));
} else {
QProcess proc;
qDebug()<<" executablePath"<<executablePath<<" newCommandLine"<<newCommandLine;
const QStringList splitArguments = newCommandLine.split(QLatin1Char(' '));
qDebug()<<" splitArguments"<<splitArguments;
if (!proc.startDetached(executablePath, splitArguments)) {
KMessageBox::error(0, i18n("Impossible to start executable"));
}
}
} else {
qCDebug(EXTERNALSCRIPTPLUGIN_LOG) << "Problem with number of arguments " << mCurrentInfo.count();
}
......@@ -93,8 +109,7 @@ void ViewerPluginExternalscriptInterface::createAction(KActionCollection *ac)
}
ac->addAction(QStringLiteral("externalscript_%1").arg(info.name()), act);
QStringList actionInfo;
const QString newCommandLine = adaptArguments(info.commandLine());
actionInfo.append(newCommandLine);
actionInfo.append(info.commandLine());
actionInfo.append(info.executable());
act->setData(actionInfo);
......
......@@ -18,6 +18,8 @@
#include "viewerpluginexternalscriptmanager.h"
#include "viewerpluginexternalscriptsloadjob.h"
#include <QStandardPaths>
class ViewerPluginExternalScriptManagerInstancePrivate
{
public:
......@@ -55,8 +57,8 @@ ViewerPluginExternalScriptManager *ViewerPluginExternalScriptManager::self()
void ViewerPluginExternalScriptManager::readExternalScriptInfo()
{
ViewerPluginExternalScriptsLoadJob job;
//TODO
job.setExternalScriptsDirectory(QString());
const QStringList lst = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("messageviewerplugins/"), QStandardPaths::LocateDirectory);
job.setExternalScriptsDirectories(lst);
job.start();
mScriptInfos = job.scriptInfos();
}
......
......@@ -37,43 +37,49 @@ ViewerPluginExternalScriptsLoadJob::~ViewerPluginExternalScriptsLoadJob()
void ViewerPluginExternalScriptsLoadJob::start()
{
mScriptInfos.clear();
if (mDirectory.isEmpty()) {
if (mDirectories.isEmpty()) {
qCDebug(EXTERNALSCRIPTPLUGIN_LOG) << "External script directory not defined";
} else {
QDir dir(mDirectory);
if (dir.exists()) {
const QDir::Filters filters = QDir::Files | QDir::Hidden | QDir::NoSymLinks;
const QFileInfoList list = dir.entryInfoList(QStringList() << QStringLiteral("*.desktop"), filters);
const int listSize(list.size());
for (int i = 0; i < listSize; ++i) {
KConfig config(list.at(i).filePath());
qCDebug(EXTERNALSCRIPTPLUGIN_LOG) << "load file " << list.at(i).filePath();
KConfigGroup group(&config, QStringLiteral("Desktop Entry"));
if (group.isValid()) {
ViewerPluginExternalScriptInfo info;
info.setName(group.readEntry("Name", QString()));
info.setExecutable(group.readEntry("Executable", QString()));
info.setCommandLine(group.readEntry("CommandLine", QString()));
info.setDescription(group.readEntry("Description", QString()));
if (info.isValid()) {
mScriptInfos.append(info);
Q_FOREACH(const QString &directory, mDirectories) {
QDir dir(directory);
if (dir.exists()) {
const QDir::Filters filters = QDir::Files | QDir::Hidden | QDir::NoSymLinks;
const QFileInfoList list = dir.entryInfoList(QStringList() << QStringLiteral("*.desktop"), filters);
const int listSize(list.size());
QStringList scriptNames;
for (int i = 0; i < listSize; ++i) {
KConfig config(list.at(i).filePath());
qCDebug(EXTERNALSCRIPTPLUGIN_LOG) << "load file " << list.at(i).filePath();
KConfigGroup group(&config, QStringLiteral("Desktop Entry"));
if (group.isValid()) {
ViewerPluginExternalScriptInfo info;
const QString name = group.readEntry("Name", QString());
info.setName(name);
info.setExecutable(group.readEntry("Executable", QString()));
info.setCommandLine(group.readEntry("CommandLine", QString()));
info.setDescription(group.readEntry("Description", QString()));
if (info.isValid() && !scriptNames.contains(name)) {
mScriptInfos.append(info);
scriptNames.append(name);
}
}
}
} else {
qCDebug(EXTERNALSCRIPTPLUGIN_LOG) << "External script directory doesn't exist " << mDirectories;
}
} else {
qCDebug(EXTERNALSCRIPTPLUGIN_LOG) << "External script directory doesn't exist " << mDirectory;
}
}
}
void ViewerPluginExternalScriptsLoadJob::setExternalScriptsDirectory(const QString &dir)
void ViewerPluginExternalScriptsLoadJob::setExternalScriptsDirectories(const QStringList &dir)
{
mDirectory = dir;
mDirectories = dir;
}
QString ViewerPluginExternalScriptsLoadJob::externalScriptsDirectory() const
QStringList ViewerPluginExternalScriptsLoadJob::externalScriptsDirectories() const
{
return mDirectory;
return mDirectories;
}
QVector<ViewerPluginExternalScriptInfo> ViewerPluginExternalScriptsLoadJob::scriptInfos() const
......
......@@ -32,13 +32,13 @@ public:
~ViewerPluginExternalScriptsLoadJob();
void start();
void setExternalScriptsDirectory(const QString &dir);
QString externalScriptsDirectory() const;
void setExternalScriptsDirectories(const QStringList &dir);
QStringList externalScriptsDirectories() const;
QVector<ViewerPluginExternalScriptInfo> scriptInfos() const;
private:
QVector<ViewerPluginExternalScriptInfo> mScriptInfos;
QString mDirectory;
QStringList mDirectories;
};
#endif // VIEWERPLUGINEXTERNALSCRIPTSLOADJOB_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment