123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #include "subnormalconfig.h"
- #include "ui_subnormalconfig.h"
- #include <QFile>
- #include <QFileInfo>
- #include <QDebug>
- #include <QTextStream>
- #include <QFileDevice>
- #include <QTextEdit>
- #include <QMessageBox>
- #include <QTemporaryDir>
- #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);
- }
- }
|