-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk9shell.cpp
208 lines (183 loc) · 5.02 KB
/
k9shell.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
// Copyright (c) 2021-2023, K9spud LLC.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "k9shell.h"
#include <QTemporaryFile>
#include <QTextStream>
#include <QDebug>
#include <QApplication>
#include <QFileInfo>
K9Shell* shell = nullptr;
K9Shell::K9Shell(QObject *parent) : QProcess(parent)
{
findBackend();
findTransport();
doas = "echo"; // fall back to echoing commands if sudo and doas are missing from the system
QFileInfo fi;
fi.setFile("/usr/bin/doas");
if(fi.exists())
{
doas = fi.canonicalFilePath();
return;
}
fi.setFile("/usr/bin/sudo");
if(fi.exists())
{
doas = fi.canonicalFilePath();
}
}
void K9Shell::findBackend()
{
QFileInfo fi;
fi.setFile(qApp->applicationDirPath() + "/appswipebackend");
if(fi.exists())
{
backend = fi.canonicalFilePath();
return;
}
fi.setFile("/usr/bin/appswipebackend");
if(fi.exists())
{
backend = fi.canonicalFilePath();
return;
}
fi.setFile("/usr/local/bin/appswipebackend");
if(fi.exists())
{
backend = fi.canonicalFilePath();
return;
}
qDebug() << "Couldn't find appswipebackend binary!";
}
void K9Shell::findTransport()
{
QFileInfo fi;
fi.setFile(qApp->applicationDirPath() + "/appswipetransport");
if(fi.exists())
{
transport = fi.canonicalFilePath();
return;
}
fi.setFile("/usr/bin/appswipetransport");
if(fi.exists())
{
transport = fi.canonicalFilePath();
return;
}
fi.setFile("/usr/local/bin/appswipetransport");
if(fi.exists())
{
transport = fi.canonicalFilePath();
return;
}
qDebug() << "Couldn't find appswipetransport binary!";
}
void K9Shell::findBzip2()
{
QFileInfo fi;
fi.setFile("/bin/bzip2");
if(fi.exists())
{
bzip2 = fi.canonicalFilePath();
return;
}
fi.setFile("/usr/bin/bzip2");
if(fi.exists())
{
bzip2 = fi.canonicalFilePath();
return;
}
qDebug() << "Couldn't find bzip2 binary!";
}
void K9Shell::externalBrowser(QString url)
{
QStringList args;
args << "open" << url;
startDetached("/usr/bin/gio", args);
}
void K9Shell::externalTerm(QString cmd)
{
externalTerm(cmd, cmd);
}
void K9Shell::externalTerm(QString script, QString title, bool waitSuccessful)
{
QTemporaryFile* tmp = new QTemporaryFile();
if(tmp->open() == false)
{
qDebug() << "Couldn't open temporary file.";
delete tmp;
return;
}
QTextStream out(tmp);
QString escaped;
QStringList cmds = script.split("\n");
QString cmd;
for(int i = 0; i < cmds.count(); i++)
{
cmd = cmds.at(i);
escaped = cmd;
if(escaped.contains("\""))
{
escaped.replace("\"", "\\\"");
}
if(cmd.contains("RET_CODE=$?") == false &&
cmd.contains("qlop -Hp") == false &&
cmd.contains(backend) == false)
{
out << "echo \"" << escaped << "\"\n";
}
out << cmd << "\n";
}
if(cmd.endsWith("| less") == false)
{
if(script.contains("RET_CODE=$?") == false)
{
out << "RET_CODE=$?\n";
}
if(waitSuccessful == false)
{
out << "if [[ $RET_CODE -eq 0 ]];\n";
out << "then\n";
out << " echo \"Success!\"\n";
out << "else\n";
out << " echo \"Press 'q' to close (exit code: $RET_CODE)\"\n";
out << " while true; do\n";
out << " read -n1 -rs\n";
out << " [[ $REPLY == 'q' || $REPLY == 'Q' ]] && break\n";
out << " done\n";
out << "fi\n";
}
else
{
out << "echo \"Press 'q' to close (exit code: $RET_CODE)\"\n";
out << "while true; do\n";
out << " read -n1 -rs\n";
out << " [[ $REPLY == 'q' || $REPLY == 'Q' ]] && break\n";
out << "done\n";
}
}
out << QString("rm -f \"%1\"").arg(tmp->fileName());
out.flush();
tmp->close();
QStringList term;
term << "-T" << title << "-e" << "/bin/bash" << tmp->fileName();
startDetached("/usr/bin/lxterminal", term);
}
void K9Shell::externalFileManager(QString url)
{
QStringList args;
args << "open" << url;
startDetached("/usr/bin/gio", args);
}