-
Notifications
You must be signed in to change notification settings - Fork 133
/
lxqtcustomcommand.cpp
239 lines (202 loc) · 7.2 KB
/
lxqtcustomcommand.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2021 LXQt team
*
* 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 "lxqtcustomcommand.h"
#include "custombutton.h"
#include "lxqtcustomcommandconfiguration.h"
#include <QProcess>
#include <QTimer>
#include <QVBoxLayout>
#include <XdgIcon>
#include <LXQt/Globals>
#include <QDebug>
LXQtCustomCommand::LXQtCustomCommand(const ILXQtPanelPluginStartupInfo &startupInfo):
QObject(),
ILXQtPanelPlugin(startupInfo),
mProcess(new QProcess(this)),
mTimer(new QTimer(this)),
mDelayedRunTimer(new QTimer(this)),
mFirstRun(true),
mOutput(QString()),
mAutoRotate(true),
mExpandToRows(false)
{
mButton = new CustomButton(this);
mButton->setObjectName(QLatin1String("CustomButton"));
mFont = mButton->font().toString();
mTimer->setSingleShot(true);
mDelayedRunTimer->setSingleShot(true);
mDelayedRunTimer->setInterval(500);
connect(mButton, &CustomButton::clicked, this, &LXQtCustomCommand::handleClick);
connect(mButton, &CustomButton::wheelScrolled, this, &LXQtCustomCommand::handleWheelScrolled);
connect(mTimer, &QTimer::timeout, this, &LXQtCustomCommand::runCommand);
connect(mDelayedRunTimer, &QTimer::timeout, this, &LXQtCustomCommand::runCommand);
connect(mProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &LXQtCustomCommand::handleFinished);
settingsChanged();
}
LXQtCustomCommand::~LXQtCustomCommand()
{
// Ensure process is closed before exiting and avoids warning from QProcess.
mProcess->close();
delete mButton;
}
QWidget *LXQtCustomCommand::widget()
{
return mButton;
}
void LXQtCustomCommand::realign()
{
mButton->updateButton();
}
QDialog *LXQtCustomCommand::configureDialog()
{
if (!mConfigDialog)
mConfigDialog = new LXQtCustomCommandConfiguration(settings());
return mConfigDialog;
}
void LXQtCustomCommand::settingsChanged()
{
bool shouldRun = false;
bool oldExpandToRows = mExpandToRows;
QString oldFont = mFont;
QString oldCommand = mCommand;
bool oldRunWithBash = mRunWithBash;
bool oldRepeat = mRepeat;
int oldRepeatTimer = mRepeatTimer;
QString oldIcon = mIcon;
int oldMaxWidth = mMaxWidth;
mAutoRotate = settings()->value(QStringLiteral("autoRotate"), true).toBool();
mExpandToRows = settings()->value(QStringLiteral("expandToRows")).toBool();
mFont = settings()->value(QStringLiteral("font"), mButton->font().toString()).toString();
mCommand = settings()->value(QStringLiteral("command"), QStringLiteral("echo Configure...")).toString();
mRunWithBash = settings()->value(QStringLiteral("runWithBash"), true).toBool();
mRepeat = settings()->value(QStringLiteral("repeat"), true).toBool();
mRepeatTimer = settings()->value(QStringLiteral("repeatTimer"), 5).toInt();
mRepeatTimer = qMax(1, mRepeatTimer);
mIcon = settings()->value(QStringLiteral("icon"), QString()).toString();
mText = settings()->value(QStringLiteral("text"), QStringLiteral("%1")).toString();
mMaxWidth = settings()->value(QStringLiteral("maxWidth"), 200).toInt();
mClick = settings()->value(QStringLiteral("click"), QString()).toString();
mWheelUp = settings()->value(QStringLiteral("wheelUp"), QString()).toString();
mWheelDown = settings()->value(QStringLiteral("wheelDown"), QString()).toString();
if (oldFont != mFont) {
QFont newFont;
newFont.fromString(mFont);
if (mFirstRun) {
QTimer::singleShot(0, mButton, [this, newFont] {
mButton->setFont(newFont);
updateButton();
});
}
else {
mButton->setFont(newFont);
}
}
if (oldCommand != mCommand || oldRunWithBash != mRunWithBash || oldRepeat != mRepeat)
shouldRun = true;
if (oldRepeatTimer != mRepeatTimer)
mTimer->setInterval(mRepeatTimer * 1000);
if (oldIcon != mIcon)
mButton->setIcon(XdgIcon::fromTheme(mIcon, QIcon(mIcon)));
if (oldMaxWidth != mMaxWidth)
mButton->setMaxWidth(mMaxWidth);
if (oldExpandToRows != mExpandToRows)
pluginFlagsChanged();
mButton->setAutoRotation(mAutoRotate);
if (mFirstRun) {
mFirstRun = false;
runCommand();
}
// Delay timer for running command, avoids multiple calls on settings change while typing command or clicking "Reset"
else if (shouldRun)
mDelayedRunTimer->start();
}
void LXQtCustomCommand::handleClick()
{
if (!mClick.isEmpty())
runDetached(mClick);
}
void LXQtCustomCommand::handleFinished(int exitCode, QProcess::ExitStatus /*exitStatus*/)
{
if (exitCode == 0) {
mOutput = QString::fromUtf8(mProcess->readAllStandardOutput());
if (mOutput.endsWith(QStringLiteral("\n")))
mOutput.chop(1);
}
else
mOutput = tr("Error");
updateButton();
if (mRepeat)
mTimer->start();
}
void LXQtCustomCommand::updateButton() {
QString newText = mText;
if (newText.contains(QStringLiteral("%1")))
newText = newText.arg(mOutput);
if (mButton->icon().isNull())
mButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
else
mButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
mButton->setText(newText);
mButton->updateButton();
}
void LXQtCustomCommand::handleWheelScrolled(int yDelta)
{
if (yDelta > 0 && !mWheelUp.isEmpty())
runDetached(mWheelUp);
else if (yDelta < 0 && !mWheelDown.isEmpty())
runDetached(mWheelDown);
}
void LXQtCustomCommand::runCommand()
{
if (mCommand.isEmpty())
return;
if (mProcess->state() != QProcess::NotRunning) {
mDelayedRunTimer->start();
return;
}
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
QStringList args;
if (mRunWithBash)
args << QStringLiteral("bash") << QStringLiteral("-c") << mCommand;
else
args = QProcess::splitCommand(mCommand);
mProcess->start(args.takeFirst(), args);
#else
QString exec;
if (mRunWithBash)
exec = QString(QStringLiteral("bash -c \"%1\"")).arg(mCommand);
else
exec = mCommand;
mProcess->start(exec);
#endif
}
void LXQtCustomCommand::runDetached(QString command)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
QStringList args = QProcess::splitCommand(command);
QProcess::startDetached(args.takeFirst(), args);
#else
QProcess::startDetached(command);
#endif
}