11#include " Plugin.hpp"
22#include < QSettings>
3- #include < QIcon>
4- #include < QFileInfo>
3+ #include < QFile>
54#include < QDir>
5+ #include < QImageReader>
66#include " Settings.hpp"
77#include " ../common/defs.h"
88
@@ -23,42 +23,62 @@ Plugin::Plugin(QString name, QString path, QObject *parent) :
2323 _pathPlugin = path;
2424 QDir pluginPath (_pathPlugin);
2525
26- QString fileName = path+" /" +name+" .ini" ;
27- QSettings settings ( fileName, QSettings::IniFormat );
28- settings.beginGroup (" Main" );
29- this ->_name = settings.value ( " Name" , " Error" ).toString ();
30- if (settings.contains (kOsSpecificExecuteKey )) {
31- this ->_exec = settings.value ( kOsSpecificExecuteKey , " " ).toString ();
32- } else {
33- this ->_exec = settings.value ( " Execute" , " " ).toString ();
26+ const QString fileName (path+" /" +name+" .ini" );
27+ if (!QFile::exists (fileName)) {
28+ qWarning () << Q_FUNC_INFO << name << fileName << " does not exist, generating defaults, make sure to edit the file!" ;
29+ if (!QFile::copy (" :/plugin-template.ini" , fileName))
30+ qWarning () << Q_FUNC_INFO << name << " failed to generate" << fileName;
31+ else if (!QFile::setPermissions (fileName, QFile::permissions (fileName) | QFileDevice::WriteOwner))
32+ qWarning () << Q_FUNC_INFO << name << " could not set write permissions to" << fileName;
3433 }
35- this ->_guid = settings.value ( " Guid" , " " ).toString ();
36- this ->_author = settings.value ( " Author" , " " ).toString ();
37- this ->_description = settings.value ( " Description" , " " ).toString ();
38- this ->_version = settings.value ( " Version" , " " ).toString ();
39- this ->_icon = pluginPath.absoluteFilePath (settings.value ( " Icon" , " " ).toString ());
34+ QSettings settings (fileName, QSettings::IniFormat);
35+ settings.beginGroup (" Main" );
36+ _name = settings.value (" Name" , " " ).toString ();
37+ if (_name.isEmpty ())
38+ _name = name;
39+
40+ if (settings.contains (kOsSpecificExecuteKey ))
41+ _arguments = settings.value (kOsSpecificExecuteKey , " " ).toString ().split (' ' );
42+ else
43+ _arguments = settings.value (" Execute" , " " ).toString ().split (' ' );
44+
45+ if (!_arguments.isEmpty ())
46+ _exec = _arguments.takeFirst ();
47+
48+ if (_exec.isEmpty ())
49+ qWarning () << Q_FUNC_INFO << name << " no executable, check" << fileName;
50+
51+ _guid = settings.value (" Guid" , " " ).toString ();
52+ _author = settings.value (" Author" , " " ).toString ();
53+ _description = settings.value (" Description" , " " ).toString ();
54+ _version = settings.value (" Version" , " " ).toString ();
55+
56+ const QString iconName = settings.value (" Icon" , " " ).toString ();
57+ const QString iconPath = pluginPath.absoluteFilePath (iconName);
58+ if (!iconName.isEmpty () && QFile::exists (iconPath) && !QImageReader::imageFormat (iconPath).isEmpty ())
59+ _icon = QIcon (iconPath);
60+ if (_icon.isNull ())
61+ _icon = QIcon (" :/icons/plugins.png" );
62+
4063 settings.endGroup ();
4164
4265 process = new QProcess (this );
43-
4466}
4567
4668Plugin::~Plugin ()
4769{
4870 Stop ();
4971}
5072
51- QString Plugin::Name () const
52- {
73+ QString Plugin::Name () const {
5374 return _name;
5475}
5576
56- QString Plugin::Guid () const
57- {
77+ QString Plugin::Guid () const {
5878 return _guid;
5979}
6080
61- QString Plugin::Author () const {
81+ QString Plugin::Author () const {
6282 return _author;
6383}
6484
@@ -71,54 +91,56 @@ QString Plugin::Version() const {
7191}
7292
7393
74- QIcon Plugin::Icon () const {
75- QFileInfo f (_icon);
76- if (f.exists ())
77- return QIcon (_icon);
78- return QIcon (" :/icons/plugins.png" );
94+ QIcon Plugin::Icon () const {
95+ return _icon;
7996}
8097
8198
8299int Plugin::getPriority () const {
83- QString key = this -> _name +" /Priority" ;
100+ const QString key = _name+" /Priority" ;
84101 return Settings::valueMain (key).toInt ();
85102}
86103
87104void Plugin::setPriority (int priority) {
88- QString key = this -> _name +" /Priority" ;
105+ const QString key = _name+" /Priority" ;
89106 Settings::setValueMain (key,priority);
90107}
91108
92109bool Plugin::isEnabled () const {
93- QString key = this -> _name +" /Enable" ;
110+ const QString key = _name+" /Enable" ;
94111 return Settings::valueMain (key).toBool ();
95112}
96113
97- void Plugin::setEnabled (bool enable){
98- DEBUG_LOW_LEVEL << Q_FUNC_INFO << enable;
99- QString key = this -> _name +" /Enable" ;
114+ void Plugin::setEnabled (bool enable) {
115+ DEBUG_LOW_LEVEL << Q_FUNC_INFO << _name << enable;
116+ const QString key = _name+" /Enable" ;
100117 Settings::setValueMain (key,enable);
101- if (!enable) this -> Stop ();
102- if (enable) this -> Start ();
118+ if (!enable) Stop ();
119+ if (enable) Start ();
103120}
104121
105122
106123void Plugin::Start ()
107124{
108- DEBUG_LOW_LEVEL << Q_FUNC_INFO << _exec;
109-
110- // QStringList arguments;
111- // arguments << "-style" << "fusion";
125+ DEBUG_LOW_LEVEL << Q_FUNC_INFO << _name << " starting" << _exec << _arguments;
112126
113127 QDir dir (_pathPlugin);
114128 QDir::setCurrent (dir.absolutePath ());
115129
116130 process->disconnect ();
117- connect (process, SIGNAL (stateChanged (QProcess::ProcessState)), this , SIGNAL (stateChanged (QProcess::ProcessState)));
131+
132+ connect (process, SIGNAL (stateChanged (QProcess::ProcessState)), this , SLOT (stateChanged (QProcess::ProcessState)));
133+ connect (process, SIGNAL (errorOccurred (QProcess::ProcessError)), this , SLOT (errorOccurred (QProcess::ProcessError)));
134+
135+ connect (process, SIGNAL (started ()), this , SLOT (started ()));
136+ connect (process, SIGNAL (finished (int , QProcess::ExitStatus)), this , SLOT (finished (int , QProcess::ExitStatus)));
137+
138+ connect (process, SIGNAL (readyReadStandardError ()), this , SLOT (readyReadStandardError ()));
139+ connect (process, SIGNAL (readyReadStandardOutput ()), this , SLOT (readyReadStandardOutput ()));
118140
119141 process->setEnvironment (QProcess::systemEnvironment ());
120- // process->setProcessChannelMode(QProcess::ForwardedChannels);
121142 process->setProgram (_exec);
143+ process->setArguments (_arguments);
122144 process->start ();
123145}
124146
@@ -132,3 +154,34 @@ QProcess::ProcessState Plugin::state() const
132154 return process->state ();
133155}
134156
157+ // QProcess slots
158+ void Plugin::stateChanged (QProcess::ProcessState newState)
159+ {
160+ DEBUG_LOW_LEVEL << Q_FUNC_INFO << _name << newState;
161+ emit pluginStateChanged (newState);
162+ }
163+
164+ void Plugin::errorOccurred (QProcess::ProcessError error)
165+ {
166+ qWarning () << Q_FUNC_INFO << _name << error << _exec << _arguments;
167+ }
168+
169+ void Plugin::started ()
170+ {
171+ DEBUG_LOW_LEVEL << Q_FUNC_INFO << _name;
172+ }
173+
174+ void Plugin::finished (int exitCode, QProcess::ExitStatus exitStatus)
175+ {
176+ DEBUG_LOW_LEVEL << Q_FUNC_INFO << _name << " exitCode=" << exitCode << exitStatus;
177+ }
178+
179+ void Plugin::readyReadStandardError ()
180+ {
181+ qWarning () << Q_FUNC_INFO << _name << process->readAllStandardError ();
182+ }
183+
184+ void Plugin::readyReadStandardOutput ()
185+ {
186+ DEBUG_LOW_LEVEL << Q_FUNC_INFO << _name << process->readAllStandardOutput ();
187+ }
0 commit comments