subnormalconfig.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "subnormalconfig.h"
  2. #include "ui_subnormalconfig.h"
  3. #include <QFile>
  4. #include <QFileInfo>
  5. #include <QDebug>
  6. #include <QTextStream>
  7. #include <QFileDevice>
  8. #include <QTextEdit>
  9. #include <QMessageBox>
  10. #include <QTemporaryDir>
  11. #include "BootGlobal.h"
  12. static const QString LAUNCH_DESKTOP_CONFIG_FILENAME = "spexplorerauto.desktop";
  13. #ifdef WITH_QT
  14. static const QString LAUNCH_DESKTOP_AUTOSTAT_DIR = "/etc/xdg/autostart/";
  15. #else
  16. static const QString LAUNCH_DESKTOP_AUTOSTAT_DIR = "D:/";
  17. #endif
  18. SubNormalConfig::SubNormalConfig(QWidget *parent) :
  19. QWidget(parent),
  20. ui(new Ui::SubNormalConfig)
  21. {
  22. ui->setupUi(this);
  23. //connect(ui->autoLaunchAtBoot, SIGNAL(clicked()), this, SLOT(updateAutoState()));
  24. ui->autoLaunchAtBoot->setChecked(checkAutoStartState());
  25. }
  26. SubNormalConfig::~SubNormalConfig()
  27. {
  28. delete ui;
  29. }
  30. bool SubNormalConfig::setAutoStart(bool enable)
  31. {
  32. bool result = true;
  33. QString appName = LAUNCH_DESKTOP_CONFIG_FILENAME;
  34. QString destAppFullPath = LAUNCH_DESKTOP_AUTOSTAT_DIR + appName;
  35. if(enable && !autoRunFileExisted())
  36. {
  37. result = ExtractResFile(this, ":/res/spexplorerauto.desktop", destAppFullPath);
  38. if (result) {
  39. result = enableNopasswdAsSudo(true);
  40. if (!result) {
  41. QFile file(destAppFullPath);
  42. file.remove();
  43. }
  44. }
  45. }
  46. else if(!enable && autoRunFileExisted())
  47. {
  48. QFile file(destAppFullPath);
  49. result = file.remove();
  50. if(!result) {
  51. QString content = QString("删除文件 %1 失败: %2").arg(LAUNCH_DESKTOP_CONFIG_FILENAME).arg(file.errorString());
  52. QMessageBox::critical(this, "错误", content, QMessageBox::Yes);
  53. result = false;
  54. }
  55. }
  56. return result;
  57. }
  58. bool SubNormalConfig::autoRunFileExisted() const
  59. {
  60. QString appName = LAUNCH_DESKTOP_CONFIG_FILENAME;
  61. QString destAppFullPath = LAUNCH_DESKTOP_AUTOSTAT_DIR + appName;
  62. QFileInfo fileInfo(destAppFullPath);
  63. if(fileInfo.exists() && fileInfo.isFile()) {
  64. return true;
  65. }
  66. return false;
  67. }
  68. bool SubNormalConfig::enableNopasswdAsSudo(bool enable /*= true*/)
  69. {
  70. bool result(true);
  71. QString loginUser = getUserName();
  72. if (loginUser.isEmpty()) {
  73. QMessageBox::critical(this, "错误", "获取用户名失败!", QMessageBox::Yes);
  74. return false;
  75. }
  76. QTemporaryDir dir;
  77. if (!dir.isValid()) {
  78. QMessageBox::critical(this, "错误", QString("创建临时文件夹失败: %1").arg(dir.errorString()), QMessageBox::Yes);
  79. return false;
  80. }
  81. QString storeFilePath = dir.filePath("set_nopass_priviledge.sh");
  82. result = ExtractResFile(this, ":/res/set_nopass_priviledge.sh", storeFilePath);
  83. if (!result) {
  84. return false;
  85. }
  86. QString programs = QString("%1 %2").arg(storeFilePath).arg(loginUser);
  87. result = false;
  88. auto resultStr = ExecuteShellScripts(this, programs);
  89. if (resultStr.count() > 0) {
  90. const int exitCode = resultStr[0].toInt();
  91. if (exitCode == 1 || exitCode == 0) {
  92. result = true;
  93. }
  94. }
  95. return result;
  96. }
  97. bool SubNormalConfig::checkAutoStartState() const
  98. {
  99. return autoRunFileExisted();
  100. }
  101. void SubNormalConfig::updateAutoState()
  102. {
  103. }
  104. void SubNormalConfig::on_autoLaunchAtBoot_clicked(bool checked)
  105. {
  106. qDebug() << "Enter " << __FUNCTION__ << endl;
  107. bool result = setAutoStart(checked);
  108. if(!result) {
  109. ui->autoLaunchAtBoot->setChecked(!checked);
  110. }
  111. }