-
Notifications
You must be signed in to change notification settings - Fork 430
/
actionhandler.cpp
174 lines (142 loc) · 5.08 KB
/
actionhandler.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
// SPDX-License-Identifier: GPL-3.0-or-later
#include "actionhandler.h"
#include "common/appconfig.h"
#include "common/action.h"
#include "common/actiontablemodel.h"
#include "common/common.h"
#include "common/contenttype.h"
#include "common/display.h"
#include "common/log.h"
#include "common/mimetypes.h"
#include "common/textdata.h"
#include "gui/actionhandlerdialog.h"
#include "gui/icons.h"
#include "gui/notification.h"
#include "gui/notificationdaemon.h"
#include "gui/clipboardbrowser.h"
#include "gui/mainwindow.h"
#include "item/serialize.h"
#include <QDialog>
#include <cmath>
namespace {
QString actionDescription(const Action &action)
{
const auto name = action.name();
if ( !name.isEmpty() )
return QStringLiteral("Command \"%1\"").arg(name);
return action.commandLine();
}
} // namespace
ActionHandler::ActionHandler(NotificationDaemon *notificationDaemon, QObject *parent)
: QObject(parent)
, m_notificationDaemon(notificationDaemon)
, m_actionModel(new ActionTableModel(parent))
{
}
void ActionHandler::setMaxRowCount(uint rows)
{
m_actionModel->setMaxRowCount(rows);
}
void ActionHandler::showProcessManagerDialog(QWidget *parent)
{
auto dialog = new ActionHandlerDialog(this, m_actionModel, parent);
dialog->setAttribute(Qt::WA_DeleteOnClose, true);
dialog->show();
}
void ActionHandler::addFinishedAction(const QString &name)
{
m_actionModel->actionFinished(name);
}
QVariantMap ActionHandler::actionData(int id) const
{
const auto action = m_actions.value(id);
return action ? action->data() : QVariantMap();
}
void ActionHandler::setActionData(int id, const QVariantMap &data)
{
const auto action = m_actions.value(id);
if (action)
action->setData(data);
}
void ActionHandler::internalAction(Action *action)
{
this->action(action);
if ( m_actions.contains(action->id()) )
m_internalActions.insert(action->id());
}
bool ActionHandler::isInternalActionId(int id) const
{
return m_internalActions.contains(id);
}
void ActionHandler::action(Action *action)
{
action->setParent(this);
connect( action, &Action::actionStarted,
m_actionModel, &ActionTableModel::actionStarted );
connect( action, &Action::actionFinished,
this, &ActionHandler::closeAction );
const int id = m_actionModel->actionAboutToStart(action);
action->setId(id);
m_actions.insert(id, action);
COPYQ_LOG( QStringLiteral("Executing: %1").arg(actionDescription(*action)) );
action->start();
}
void ActionHandler::terminateAction(int id)
{
Action *action = m_actions.value(id);
if (action)
action->terminate();
}
void ActionHandler::closeAction(Action *action)
{
m_actions.remove(action->id());
m_internalActions.remove(action->id());
if ( action->actionFailed() ) {
const auto msg = tr("Error: %1").arg(action->errorString());
showActionErrors(action, msg, IconCircleExclamation);
#ifdef Q_OS_WIN
// FIXME: Ignore specific exit code for clipboard monitor on Windows when logging out.
} else if ( action->exitCode() == 1073807364 ) {
COPYQ_LOG( QStringLiteral("Exit code %1 (on logout?) with command: %2")
.arg(action->exitCode())
.arg(action->commandLine()) );
#endif
} else if ( action->exitCode() != 0 ) {
const auto msg = tr("Exit code: %1").arg(action->exitCode());
showActionErrors(action, msg, IconCircleXmark);
}
m_actionModel->actionFinished(action);
Q_ASSERT(runningActionCount() >= 0);
action->deleteLater();
}
void ActionHandler::showActionErrors(Action *action, const QString &message, ushort icon)
{
m_actionModel->actionFailed(action, message);
QtPrivate::QHashCombine hash;
const auto notificationId = QString::number( hash(hash(0, action->commandLine()), message) );
if ( m_notificationDaemon->findNotification(notificationId) )
return;
auto msg = message;
if ( !action->errorOutput().isEmpty() )
msg.append( "\n" + action->errorOutput() );
const int maxWidthPoints =
AppConfig().option<Config::notification_maximum_width>();
const QString command = action->commandLine()
.replace(QLatin1String("copyq eval --"), QLatin1String("copyq:"));
const QString name = action->name().isEmpty()
? QString(command).replace('\n', QLatin1String(" "))
: action->name();
const QString format = tr("Command %1").arg(quoteString("%1"));
const QString title = elideText(name, QFont(), format, pointsToPixels(maxWidthPoints));
// Print command with line numbers.
int lineNumber = 0;
const auto lines = command.split("\n");
const auto lineNumberWidth = static_cast<int>(std::log10(lines.size())) + 1;
for (const auto &line : lines)
msg.append(QStringLiteral("\n%1. %2").arg(++lineNumber, lineNumberWidth).arg(line));
log(QStringLiteral("%1\n%2").arg(title, msg));
auto notification = m_notificationDaemon->createNotification(notificationId);
notification->setTitle(title);
notification->setMessage(msg, Qt::PlainText);
notification->setIcon(icon);
}