diff --git a/Resource.qrc b/Resource.qrc index 3bbe664..22bce9f 100644 --- a/Resource.qrc +++ b/Resource.qrc @@ -10,5 +10,6 @@ <file>icon/dialog-question.svg</file> <file>icon/dialog-warning.svg</file> <file>icon/icon.svg</file> + <file>shell/kernel-installer-remove-template.sh</file> </qresource> </RCC> diff --git a/kernelinformation.cpp b/kernelinformation.cpp index aa81212..254efce 100644 --- a/kernelinformation.cpp +++ b/kernelinformation.cpp @@ -109,3 +109,9 @@ QString KernelInformation::localKernelName() const process.close(); return result; } + +bool KernelInformation::get_installedAlready(int value) const +{ + QString pkgName = this->get_pkgName(value).at(0); + return QFile::exists("/var/lib/dpkg/info/" + pkgName + ".list"); +} diff --git a/kernelinformation.h b/kernelinformation.h index 27ffd0e..1a9c11c 100644 --- a/kernelinformation.h +++ b/kernelinformation.h @@ -14,6 +14,8 @@ #include <QProcess> +#include <QFile> + class KernelInformation : public QObject { Q_OBJECT @@ -31,6 +33,7 @@ public: QStringList get_pkgName(int value) const; QStringList get_system(int value) const; QStringList get_arch(int value) const; + bool get_installedAlready(int value) const; QString localKernelName() const; diff --git a/kernelinstaller.cpp b/kernelinstaller.cpp index db332ed..21d4a7e 100644 --- a/kernelinstaller.cpp +++ b/kernelinstaller.cpp @@ -10,17 +10,25 @@ #define MAX_TMP_NUM 1024 #define MIN_TMP_NUM 0 -KernelInstaller::KernelInstaller(QStringList kernelList, QWidget *parent) : +KernelInstaller::KernelInstaller(Option option, QStringList kernelList, QWidget *parent) : QMainWindow(parent), ui(new Ui::KernelInstaller) { ui->setupUi(this); + runOption = option; // 修改提示文本 QString kernel = ""; foreach (QString name, kernelList) { kernel += name + " "; } - ui->m_status->setText(tr("Try to install ") + kernel); + + switch(runOption) { + case Option::Install: + ui->m_status->setText(tr("Try to install ") + kernel); + case Option::Remove: + ui->m_status->setText(tr("Try to remove ") + kernel); + } + this->kernelList = kernelList; terminal = new QTermWidget(0); @@ -63,7 +71,15 @@ QString KernelInstaller::BuildKernelInstallerBash(QStringList kernelList, QStrin foreach (QString name, kernelList) { kernel += name + " "; } - QFile file(":/shell/kernel-installer-template.sh"); + QString filePath = ":/shell/kernel-installer-template.sh"; + switch(runOption) { + case Option::Install: + filePath = ":/shell/kernel-installer-template.sh"; + case Option::Remove: + filePath = ":/shell/kernel-installer-remove-template.sh"; + } + + QFile file(filePath); file.open(QFile::ReadOnly); QString data = file.readAll(); data = data.replace("${KernelList}", kernel); diff --git a/kernelinstaller.h b/kernelinstaller.h index e577003..1816927 100644 --- a/kernelinstaller.h +++ b/kernelinstaller.h @@ -14,13 +14,20 @@ class KernelInstaller : public QMainWindow Q_OBJECT public: - explicit KernelInstaller(QStringList kernelList, QWidget *parent = nullptr); + enum Option { + Install, + Remove + }; + + explicit KernelInstaller(Option option, QStringList kernelList, QWidget *parent = nullptr); ~KernelInstaller(); + signals: void InstallFinished(int status); private: + Option runOption; Ui::KernelInstaller *ui; QTermWidget *terminal; QStringList kernelList; diff --git a/mainwindow.cpp b/mainwindow.cpp index 9da3606..16167d2 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -31,12 +31,13 @@ void MainWindow::RefreshKernelListView(KernelInformation *info) // 更新列表 int count = info->get_count(); QStandardItemModel *model = new QStandardItemModel(); - model->setHorizontalHeaderLabels(QStringList() << tr("ID") << tr("Kernel Name") << tr("Author") << tr("Arch")); + model->setHorizontalHeaderLabels(QStringList() << tr("ID") << tr("Kernel Name") << tr("Author") << tr("Arch") << tr("Installed")); for(int i = 0; i < count; i++) { model->setItem(i, 0, new QStandardItem(QString::number(i))); model->setItem(i, 1, new QStandardItem(info->get_name(i))); model->setItem(i, 2, new QStandardItem(info->get_author(i))); model->setItem(i, 3, new QStandardItem(info->get_arch(i).at(0))); + model->setItem(i, 4, new QStandardItem((QStringList() << "" << "Y").at(info->get_installedAlready(i)))); } ui->m_kernelShow->setModel(model); } @@ -66,7 +67,7 @@ void MainWindow::on_m_installButton_clicked() QModelIndex index = ui->m_kernelShow->model()->index(row, 0); int id = ui->m_kernelShow->model()->data(index).toUInt(); // 获取选中行 - KernelInstaller *installer = new KernelInstaller(kernelInformation->get_pkgName(id)); + KernelInstaller *installer = new KernelInstaller(KernelInstaller::Option::Install, kernelInformation->get_pkgName(id)); installer->show(); } @@ -95,3 +96,21 @@ void MainWindow::on_actionGithub_triggered() QDesktopServices::openUrl(QUrl("https://github.com/GXDE-OS/gxde-kernel-manager")); } + +void MainWindow::on_m_removeButton_clicked() +{ + QModelIndex list = ui->m_kernelShow->selectionModel()->currentIndex(); + int row = list.row(); + if(row <= 0) { + // 未选中任何内容 + QMessageBox::critical(this, tr("Error"), tr("Nothing to choose")); + return; + } + // 获取 ID + QModelIndex index = ui->m_kernelShow->model()->index(row, 0); + int id = ui->m_kernelShow->model()->data(index).toUInt(); + // 获取选中行 + KernelInstaller *installer = new KernelInstaller(KernelInstaller::Option::Remove, kernelInformation->get_pkgName(id)); + installer->show(); +} + diff --git a/mainwindow.h b/mainwindow.h index d81a6fb..c7dd8b1 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -31,6 +31,8 @@ private slots: void on_actionGithub_triggered(); + void on_m_removeButton_clicked(); + private: Ui::MainWindow *ui; KernelInformation *kernelInformation; diff --git a/mainwindow.ui b/mainwindow.ui index 90ee0b8..2097b24 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -71,6 +71,13 @@ </property> </widget> </item> + <item> + <widget class="QPushButton" name="m_removeButton"> + <property name="text"> + <string>Remove</string> + </property> + </widget> + </item> </layout> </item> </layout> diff --git a/shell/kernel-installer-remove-template.sh b/shell/kernel-installer-remove-template.sh new file mode 100644 index 0000000..946444d --- /dev/null +++ b/shell/kernel-installer-remove-template.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -e +rm /tmp/gxde-kernel-manager-installer-status -f +apt purge ${KernelList} -y +rm -f "${kernelInstallerShellTempPath}" \ No newline at end of file