-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.cpp
308 lines (286 loc) · 10.7 KB
/
widget.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "widget.h"
#include "ui_widget.h"
#include <QIcon>
#include <QPixmap>
#include <QImage>
#include <QPainter>
#include <QtSvg/QSvgRenderer>
//#include <DProgressBar>
#include <QFileDialog>
#include <QFont>
#include <QProcess>
#include <QFileInfo>
#include <QtConcurrent> //并发
#include <QDebug>
#include <QDir>
#include <QSettings> //读取desktop
#include <fstream>
#include <QDragEnterEvent>
#include <QRegExp>
//DWIDGET_USE_NAMESPACE
using namespace std;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
step=1;
ui->setupUi(this);
ui->label->setPixmap(QIcon::fromTheme("package-x-generic").pixmap(135,135));
ui->label->setScaledContents(true);
ui->progressBar->hide();
ui->progressBar->setRange(0,100);
QFont font_tip;
ui->label_2->setStyleSheet("color:#8AA1B4");
ui->label_2->setText("将AppImage软件包拖入以打开");
ui->label_2->setWordWrap(true);
setAcceptDrops(true);
ui->widget_5->hide();
QRegExp regx("[0-9]+[0-9a-zA-Z.-+~]{29}"); //使用正则表达式规范输入一定程度上防止用户非法输入
QValidator *validator = new QRegExpValidator(regx, ui->lineEdit_2 );
ui->lineEdit_2->setValidator( validator );
ui->pushButton_2->hide();
ui->checkBox->hide();
qDebug()<<ui->checkBox->isChecked();
ui->widget_4->hide();
}
Widget::~Widget()
{
delete ui;
}
void Widget::unpack()
{
system("rm -r /tmp/squashfs-root");
setAcceptDrops(false);
QDir::setCurrent("/tmp/");
QProcess process;
process.start("cd /tmp");
process.waitForFinished();
system("chmod +x "+filePath.toUtf8());
process.start(filePath.toUtf8() + " --appimage-extract");
process.waitForFinished();
//开始读取图标
QProcess lspng;
lspng.start("ls /tmp/squashfs-root/");
lspng.waitForFinished();
QString output=lspng.readAllStandardOutput();
QStringList pngname = output.split("\n");
for (int i = 0;i<pngname.size();i++) {
if(pngname[i].right(4)==".png" || pngname[i].right(4)==".svg"){
pngname[0]=pngname[i];
}
}
qDebug()<<pngname[0];
file_icon=pngname[0];
if(pngname[0].right(3)=="png"){
ui->label->setPixmap(QPixmap::fromImage(QImage("/tmp/squashfs-root/"+pngname[0])));
}else {
QString svgPath = "/tmp/squashfs-root/"+pngname[0].toUtf8();
QSvgRenderer* svgRender = new QSvgRenderer();
svgRender->load(svgPath);
QPixmap* pixmap = new QPixmap(128,128);
pixmap->fill(Qt::transparent);//设置背景透明
QPainter p(pixmap);
svgRender->render(&p);
ui->label->setPixmap(*pixmap);
ui->label->setAlignment(Qt::AlignHCenter);
}
//图标文件设置结束
//开始读取软件名
QProcess lsdesktop;
lspng.start("ls /tmp/squashfs-root/");
lspng.waitForFinished();
QString output_desktop=lspng.readAllStandardOutput();
QStringList desktop_name = output.split("\n");
for (int i = 0;i<desktop_name.size();i++) {
if(desktop_name[i].right(8)==".desktop"){
desktop_name[0]=desktop_name[i];
}
}
QSettings desktopname ("/tmp/squashfs-root/"+desktop_name[0],QSettings::IniFormat);
ctrl_Version=desktopname.value( "Desktop Entry/Version" ).toString();
ctrl_Description=desktopname.value( "Desktop Entry/Comment" ).toString();
QFileInfo desktop_file("/tmp/squashfs-root/"+ desktop_name[0]) ;//获得desktop文件名作为包名,不包含后缀
ctrl_Package=desktop_file.fileName().left(desktop_file.fileName().length()-8);
//软件名读取结束
ui->widget_5->show();
ui->lineEdit->setText(ctrl_Package);
ui->pushButton->setText("开始打包");
ui->checkBox->show();
ui->pushButton->setEnabled(true);
step=2;
}
void Widget::pack()
{
ui->progressBar->setValue(0);
ui->progressBar->show();
ui->widget_4->show();
setAcceptDrops(false);
system("rm -r /tmp/packdeb");
ui->label_2->setText("正在创建目录结构");
system("mkdir /tmp/packdeb");
system("mkdir /tmp/packdeb/DEBIAN");
system("touch /tmp/packdeb/DEBIAN/control");
system("mkdir /tmp/packdeb/opt");
system("mkdir /tmp/packdeb/opt/durapps");
system("mkdir /tmp/packdeb/opt/durapps/a2d-packages");
system("mkdir /tmp/packdeb/opt/durapps/a2d-packages/"+ctrl_Package.toUtf8());
system("mkdir /tmp/packdeb/usr");
system("mkdir /tmp/packdeb/usr/share");
system("mkdir /tmp/packdeb/usr/share/applications");
system("mkdir /tmp/packdeb/usr/share/icons/");
ui->progressBar->setValue(10);
ui->label_2->setText("正在复制文件");
if(ui->checkBox->isChecked()){
system("cp -r /tmp/squashfs-root/* /tmp/packdeb/opt/durapps/a2d-packages/"+ctrl_Package.toUtf8());
}else {
system("cp "+filePath.toUtf8()+" /tmp/packdeb/opt/durapps/a2d-packages/"+ctrl_Package.toUtf8());//部署可执行文件
}
ui->progressBar->setValue(20);
//生成快捷方式
QFile redesktop("/tmp/squashfs-root/"+ctrl_Package+".desktop");
fstream newdesktop;
newdesktop.open("/tmp/packdeb/usr/share/applications/"+ctrl_Package.toUtf8()+".desktop",ios::out);
redesktop.open(QIODevice::ReadOnly|QIODevice::Text);
QTextStream txtInput(&redesktop);
QString lineStr;
while (!txtInput.atEnd())
{
lineStr = txtInput.readLine(); //读取数据
if(lineStr.left(4)=="Exec"){
if(ui->checkBox->isChecked()){
lineStr="Exec=/opt/durapps/a2d-packages/"+ctrl_Package+"/AppRun --no-sandbox";
}else {
lineStr="Exec=/opt/durapps/a2d-packages/"+ctrl_Package+"/"+imageName+" --no-sandbox";//关联快捷
}
}
if(lineStr.left(4)=="Icon"){
lineStr="Icon=/opt/durapps/a2d-packages/"+ctrl_Package+"/"+file_icon;
}
newdesktop<<lineStr.toStdString()<<endl;
}
redesktop.close();
newdesktop.close();
ui->progressBar->setValue(30);
// system("cp -r /tmp/squashfs-root/usr/share/icons/* /tmp/packdeb/usr/share/icons");//复制图标
system("cp /tmp/squashfs-root/"+file_icon.toUtf8()+" /tmp/packdeb/opt/durapps/a2d-packages/"+ctrl_Package.toUtf8());
qDebug()<<"cp /tmp/squashfs-root/"+file_icon.toUtf8()+" /tmp/packdeb/opt/durapps/a2d-packages/"+ctrl_Package.toUtf8();
ui->progressBar->setValue(40);
ui->label_2->setText("写入控制文件");
fstream out_control;
out_control.open("/tmp/packdeb/DEBIAN/control",ios::out);
out_control<<"Package: "<<ctrl_Package.toStdString()<<endl;
out_control<<"Version: "<<ctrl_Version.toStdString()<<endl;
out_control<<"Architecture: "<<ctrl_Architecture.toStdString()<<endl;
out_control<<"Description: "<<ctrl_Description.toStdString()<<endl;
out_control<<"Depends: \n";
out_control<<"Maintainer: spark-store\n";
out_control.close();
ui->progressBar->setValue(80);
update();
ui->label_2->setText("正在打包,可能需要较长时间");
if(ui->checkBox->isChecked()){
system("dpkg -b /tmp/packdeb "+dirPath.toUtf8()+"/"+ctrl_Package.toUtf8()+"_"+ctrl_Version.toUtf8()+"_a2d_amd64.deb");
}else {
system("fakeroot dpkg -b /tmp/packdeb "+dirPath.toUtf8()+"/"+ctrl_Package.toUtf8()+"_"+ctrl_Version.toUtf8()+"_a2d_amd64.deb");
}
ui->progressBar->setValue(100);
update();
ui->progressBar->hide();
ui->widget_4->hide();
ui->label_2->setText("打包完成,请到原目录查看");
ui->pushButton->setText("打开文件夹");
ui->pushButton->setEnabled(true);
ui->widget_5->hide();
step=-2;
fstream deepinInstaller;
deepinInstaller.open("/usr/bin/deepin-deb-installer",ios::in);
if(deepinInstaller){
ui->pushButton_2->show();
}
}
void Widget::dragEnterEvent(QDragEnterEvent * event)
{
event->acceptProposedAction();
}
void Widget::dropEvent(QDropEvent *event)
{
QString fileurl=event->mimeData()->urls().at(0).toString();
qDebug()<<fileurl;
if(fileurl.right(9)==".AppImage" || fileurl.right(9)==".appimage" || fileurl.right(9)==".APPIMAGE"){
qDebug()<<fileurl.length();
filePath=fileurl.right(fileurl.length()-7);
QFileInfo fileInfo(filePath);
filePath =fileInfo.path()+"/"+fileInfo.fileName();
dirPath = fileInfo.absolutePath();
imageName = fileInfo.fileName();
ui->label_2->setText(filePath);
ui->pushButton->setEnabled(false);
ui->pushButton->setText("正在解析软件包");
QtConcurrent::run([=](){
unpack();
});
}
}
void Widget::on_pushButton_clicked()
{
if(step==-1){
QApplication::quit();
}
if(step==1){ //打开文件
filePath = QFileDialog::getOpenFileName(nullptr,("打开文件"),QDir::homePath(),
("AppImage(*.appImage);;所有文件(*.*)"));
if(filePath!=""){
QFileInfo fileInfo(filePath);
dirPath = fileInfo.absolutePath();
imageName = fileInfo.fileName();
ui->label_2->setText(filePath);
ui->pushButton->setEnabled(false);
ui->pushButton->setText("正在解析软件包");
QtConcurrent::run([=](){ //并发防止无响应
unpack();
});
}
}
if(step==2){ //开始打包
ui->checkBox->hide();
fstream fakerootIsHere;
fakerootIsHere.open("/usr/bin/fakeroot",ios::in);
if(fakerootIsHere){
if(ui->lineEdit->text()!="" && ui->lineEdit_2->text()!="" && ui->lineEdit_2->text().length()>=2){
ctrl_Package=ui->lineEdit->text();
ctrl_Version=ui->lineEdit_2->text();
ctrl_Architecture=ui->comboBox->currentText();
ui->pushButton->setEnabled(false);
ui->pushButton->setText("请稍等");
ui->lineEdit->setEnabled(false);
ui->lineEdit_2->setEnabled(false);
ui->comboBox->setEnabled(false);
QtConcurrent::run([=](){ //并发防止无响应
pack();
});
}else {
system("notify-send \"请检查是否将包信息补充完整\"");
}
}else {
system("notify-send \"你需要安装fakeroot来继续执行下面的操作\"");
}
}
if(step==-2){ //打开文件夹
system("dde-file-manager "+dirPath.toUtf8()+"&");
QApplication::quit();
}
}
void Widget::on_pushButton_2_clicked()
{
system("deepin-deb-installer "+dirPath.toUtf8()+"/"+ctrl_Package.toUtf8()+"_a2d.deb&");
QApplication::quit();
}
void Widget::on_checkBox_clicked(bool checked)
{
qDebug()<<checked;
if(checked){
ui->label_2->setText("解包后打包会使软件打开速度变快,\n但是部分软件可能因此无法运行。");
}else {
ui->label_2->setText(filePath);
}
}