#include "subnormalconfig.h" #include "ui_subnormalconfig.h" #include #include #include #include #include #include #include #include #include "BootGlobal.h" static const QString LAUNCH_DESKTOP_CONFIG_FILENAME = "spexplorerauto.desktop"; #ifdef WITH_QT static const QString LAUNCH_DESKTOP_AUTOSTAT_DIR = "/etc/xdg/autostart/"; #else static const QString LAUNCH_DESKTOP_AUTOSTAT_DIR = "D:/"; #endif SubNormalConfig::SubNormalConfig(QWidget *parent) : QWidget(parent), ui(new Ui::SubNormalConfig) { ui->setupUi(this); //connect(ui->autoLaunchAtBoot, SIGNAL(clicked()), this, SLOT(updateAutoState())); ui->autoLaunchAtBoot->setChecked(checkAutoStartState()); } SubNormalConfig::~SubNormalConfig() { delete ui; } bool SubNormalConfig::setAutoStart(bool enable) { bool result = true; QString appName = LAUNCH_DESKTOP_CONFIG_FILENAME; QString destAppFullPath = LAUNCH_DESKTOP_AUTOSTAT_DIR + appName; if(enable && !autoRunFileExisted()) { result = ExtractResFile(this, ":/res/spexplorerauto.desktop", destAppFullPath); if (result) { result = enableNopasswdAsSudo(true); if (!result) { QFile file(destAppFullPath); file.remove(); } } } else if(!enable && autoRunFileExisted()) { QFile file(destAppFullPath); result = file.remove(); if(!result) { QString content = QString("删除文件 %1 失败: %2").arg(LAUNCH_DESKTOP_CONFIG_FILENAME).arg(file.errorString()); QMessageBox::critical(this, "错误", content, QMessageBox::Yes); result = false; } } return result; } bool SubNormalConfig::autoRunFileExisted() const { QString appName = LAUNCH_DESKTOP_CONFIG_FILENAME; QString destAppFullPath = LAUNCH_DESKTOP_AUTOSTAT_DIR + appName; QFileInfo fileInfo(destAppFullPath); if(fileInfo.exists() && fileInfo.isFile()) { return true; } return false; } bool SubNormalConfig::enableNopasswdAsSudo(bool enable /*= true*/) { bool result(true); QString loginUser = getUserName(); if (loginUser.isEmpty()) { QMessageBox::critical(this, "错误", "获取用户名失败!", QMessageBox::Yes); return false; } QTemporaryDir dir; if (!dir.isValid()) { QMessageBox::critical(this, "错误", QString("创建临时文件夹失败: %1").arg(dir.errorString()), QMessageBox::Yes); return false; } QString storeFilePath = dir.filePath("set_nopass_priviledge.sh"); result = ExtractResFile(this, ":/res/set_nopass_priviledge.sh", storeFilePath); if (!result) { return false; } QString programs = QString("%1 %2").arg(storeFilePath).arg(loginUser); result = false; auto resultStr = ExecuteShellScripts(this, programs); if (resultStr.count() > 0) { const int exitCode = resultStr[0].toInt(); if (exitCode == 1 || exitCode == 0) { result = true; } } return result; } bool SubNormalConfig::checkAutoStartState() const { return autoRunFileExisted(); } void SubNormalConfig::updateAutoState() { } void SubNormalConfig::on_autoLaunchAtBoot_clicked(bool checked) { qDebug() << "Enter " << __FUNCTION__ << endl; bool result = setAutoStart(checked); if(!result) { ui->autoLaunchAtBoot->setChecked(!checked); } }