diff --git a/installdeb.cpp b/installdeb.cpp index 8e9be07..2e9b07e 100644 --- a/installdeb.cpp +++ b/installdeb.cpp @@ -15,6 +15,10 @@ void InstallDEB::AddCommand(QString command){ this->commandList.append(command); } +void InstallDEB::SetCommandAfterRootRun(QString command){ + commandAfterRootRun = command; +} + void InstallDEB::RunCommand(bool withRoot){ this->terminal->setEnabled(true); this->runStatus = true; @@ -24,6 +28,7 @@ void InstallDEB::RunCommand(bool withRoot){ for(int i = 0; i < this->commandList.size(); i++){ commandCode += this->commandList.at(i) + "\n"; } + commandCode += "rm -rfv '" + bashPath + "'"; QFile file(bashPath); file.open(QFile::WriteOnly); @@ -35,21 +40,35 @@ void InstallDEB::RunCommand(bool withRoot){ system(("chmod +x '" + bashPath + "'").toUtf8()); // 赋予运行权限 this->terminal->setColorScheme("DarkPastels"); if(withRoot){ - this->terminal->setShellProgram("/usr/bin/pkexec"); - this->terminal->setArgs(QStringList() << "/usr/bin/bash" << bashPath); + QString bashMainPath = "/tmp/deepin-wine-runner-aptss-installer-root-" + QString::number(QDateTime::currentMSecsSinceEpoch()) + ".sh"; + QString commandCode = "#!/bin/bash\n"\ + "pkexec env DISPLAY=$DISPLAY bash '" + bashPath + "'\n" + + this->commandAfterRootRun + + "\nrm -rfv '" + bashMainPath + "'\n" + "rm -rfv '" + bashPath + "'"; + + QFile file(bashMainPath); + file.open(QFile::WriteOnly); + if(!file.isWritable()){ + throw "Can't write the file!"; + } + file.write(commandCode.toUtf8()); + file.close(); + system(("chmod +x '" + bashMainPath + "'").toUtf8()); // 赋予运行权限 + this->terminal->setShellProgram("/usr/bin/bash"); + this->terminal->setArgs(QStringList() << bashMainPath); } else{ this->terminal->setShellProgram("/usr/bin/bash"); this->terminal->setArgs(QStringList() << bashPath); } //this->terminal->setAutoClose(1); - this->terminal->setAutoFillBackground(1); - QObject::connect(this->terminal, &QTermWidget::finished, this->mainWindow, [this](){ - //QMessageBox::information(this->mainWindow, "A", "B"); + //this->terminal->setAutoFillBackground(1); + /*QObject::connect(this->terminal, &QTermWidget::finished, this->mainWindow, [this](){ MessageBox *message = new MessageBox(); message->information("提示", "应用安装完成"); this->mainWindow->sendMessage(QIcon(":/Icon/MessageBox/dialog-information.svg"), "应用安装完成"); - }); + });*/ this->terminal->startShellProgram(); } diff --git a/installdeb.h b/installdeb.h index b2250d2..675d15b 100644 --- a/installdeb.h +++ b/installdeb.h @@ -12,9 +12,11 @@ public: InstallDEB(QTermWidget *terminal, DMainWindow *mainWindow = NULL); void AddCommand(QString command); void RunCommand(bool withRoot=false); + void SetCommandAfterRootRun(QString command); QStringList commandList; private: QTermWidget *terminal; + QString commandAfterRootRun; DMainWindow *mainWindow = NULL; bool runStatus; }; diff --git a/main.cpp b/main.cpp index c65206b..a171d05 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ #include <DMessageBox> #include <iostream> #include <DApplicationSettings> +#include "messagebox.h" DWIDGET_USE_NAMESPACE using namespace std; @@ -28,15 +29,36 @@ int main(int argc, char *argv[]) a.setApplicationDescription("Wine运行器是一个能让Linux用户更加方便地运行Windows应用的程序。原版的 Wine 只能使用命令操作,且安装过程较为繁琐,对小白不友好。于是该运行器为了解决该痛点,内置了对Wine图形化的支持、Wine 安装器、微型应用商店、各种Wine工具、自制的Wine程序打包器、运行库安装工具等。"); a.setApplicationVersion("3.6.1"); a.setProductIcon(QIcon(":/Icon/deepin-wine-runner.svg")); - a.setProductName("aptss 安装器"); + a.setProductName("aptss 应用安装器"); a.setApplicationHomePage("https://gitee.com/gfdgd-xi/deep-wine-runner"); //DApplication::setApplicationHomePage("https://gitee.com/gfdgd-xi/deep-wine-runner"); DApplication::setOrganizationName("gfdgd_xi"); DApplication::setApplicationName("deepin-wine-runner-aptss-installer"); + if(argc > 0){ + // 用于显示对话框 + qDebug() << argv[1]; + if(QString(argv[1]) == "--messagebox-information"){ + if(argc < 4){ + qDebug() << "参数有误!"; + return 1; + } + MessageBox().information(argv[2], argv[3]); + return a.exec(); + } + if(QString(argv[1]) == "--messagebox-error"){ + if(argc < 4){ + qDebug() << "参数有误!"; + return 1; + } + MessageBox().critical(argv[2], argv[3]); + return a.exec(); + } + } if(system("which aptss")){ - DMessageBox::information(NULL, "错误", "无法检测到 aptss\n请确保您已安装星火应用商店并更新至最新版本"); + MessageBox().critical("错误", "无法检测到 aptss\n请确保您已安装星火应用商店并更新至最新版本"); + a.exec(); return 1; } diff --git a/mainwindow.cpp b/mainwindow.cpp index b64f38f..68d8996 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -8,6 +8,7 @@ #include <DTitlebar> #include <QDesktopServices> #include <DApplication> +#include <DFileDialog> MainWindow::MainWindow(QWidget *parent) : DMainWindow(parent) @@ -16,7 +17,7 @@ MainWindow::MainWindow(QWidget *parent) ui->setupUi(this); // 自定义标题栏 DTitlebar *bar = this->titlebar(); - bar->setTitle("应用安装器"); + bar->setTitle("aptss 应用安装器"); bar->setIcon(QIcon(":/Icon/deepin-wine-runner.svg")); bar->setBackgroundTransparent(true); QMenu *openProgramWebsite = bar->menu()->addMenu("项目地址"); @@ -61,10 +62,25 @@ void MainWindow::on_installPath_clicked() return; } InstallDEB *deb = new InstallDEB(terminal, this); + deb->AddCommand("set -e"); deb->AddCommand("aptss update"); deb->AddCommand("aptss install \"" + ui->debPath->text() + "\" -y"); + deb->SetCommandAfterRootRun("if [[ $? != 0 ]]; then \n"\ + " '" + QCoreApplication::applicationFilePath() + "' --messagebox-error 错误 安装失败! > /dev/null 2>&1 \n"\ + "else \n" + " '" + QCoreApplication::applicationFilePath() + "' --messagebox-information 提示 安装完成! > /dev/null 2>&1 \n"\ + "fi"); deb->RunCommand(1); } + +void MainWindow::on_browserButton_clicked() +{ + QString filePath = DFileDialog::getOpenFileName(this, "选择 deb 包", QDir::homePath(), "deb 包(*.deb);;所有文件(*.*)"); + if(filePath != ""){ + ui->debPath->setText(filePath); + } +} + diff --git a/mainwindow.h b/mainwindow.h index c3bbcb5..951ef2f 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -19,6 +19,8 @@ public: private slots: void on_installPath_clicked(); + void on_browserButton_clicked(); + private: QTermWidget *terminal = new QTermWidget(0); Ui::MainWindow *ui; diff --git a/mainwindow.ui b/mainwindow.ui index ad139b2..abc0b6d 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -11,7 +11,7 @@ </rect> </property> <property name="windowTitle"> - <string>MainWindow</string> + <string>aptss 应用安装器</string> </property> <property name="windowIcon"> <iconset resource="Resources.qrc"> diff --git a/messagebox.cpp b/messagebox.cpp index 8b4eb9a..97d779c 100644 --- a/messagebox.cpp +++ b/messagebox.cpp @@ -3,6 +3,7 @@ #include <DLabel> #include <DPushButton> #include <DTitlebar> +#include <QScreen> MessageBox::MessageBox(DWidget *parent){ @@ -37,6 +38,25 @@ void MessageBox::ShowMessageBox(QString iconPath, QString title, QString text){ messageBox->resize(messageBox->frameSize().width(), messageBox->frameSize().height()); //// 根据窗口信息获取中点 + if(this->parent == NULL){ + /// 如果没有传入窗口 + // 获取主屏幕信息 + int screenWidth = QGuiApplication::primaryScreen()->geometry().width(); + int screenHeight = QGuiApplication::primaryScreen()->geometry().height(); + // 获取对话框信息 + int messageBoxWidth = messageBox->frameSize().width(); + int messageBoxHeight = messageBox->frameSize().height(); + // 计算坐标 + int x = (screenWidth / 2) - (messageBoxWidth / 2); + int y = (screenHeight / 2) - (messageBoxHeight / 2); + messageBox->move(x, y); + qDebug() << screenWidth; + qDebug() << screenHeight; + qDebug() << messageBoxWidth; + qDebug() << messageBoxHeight; + qDebug() << x << y; + return; + } // 获取窗口信息 int parentWindowX = this->parent->frameGeometry().x(); int parentWindowY = this->parent->frameGeometry().y(); @@ -44,7 +64,7 @@ void MessageBox::ShowMessageBox(QString iconPath, QString title, QString text){ int parentWindowHeight = this->parent->frameGeometry().height(); int messageBoxWidth = messageBox->frameSize().width(); int messageBoxHeight = messageBox->frameSize().height(); - // 计算 X 坐标 + // 计算坐标 int x = parentWindowX + ((parentWindowWidth / 2) - (messageBoxWidth / 2)); int y = parentWindowY + ((parentWindowHeight / 2) - (messageBoxHeight / 2)); messageBox->move(x, y);