autostart.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef AUTOSTART_H
  2. #define AUTOSTART_H
  3. #include <qglobal.h>
  4. #include <QDir>
  5. #include <QCoreApplication>
  6. #define MY_APP_NAME "NumLockManager"
  7. #define MY_APP_PATH QDir::toNativeSeparators(QCoreApplication::applicationFilePath())
  8. #ifdef Q_OS_WIN
  9. #include <QSettings>
  10. class Autostart{
  11. public:
  12. Autostart (){}
  13. ~Autostart(){delete settings;}
  14. bool status(){
  15. return !settings->value(MY_APP_NAME).toString().isEmpty();
  16. }
  17. void set(bool s){
  18. s ? settings->setValue(MY_APP_NAME, MY_APP_PATH) : settings->remove(MY_APP_NAME);
  19. settings->sync();
  20. }
  21. void switch_(){
  22. status() ? settings->remove(MY_APP_NAME) : settings->setValue(MY_APP_NAME, MY_APP_PATH);
  23. settings->sync();
  24. }
  25. private:
  26. QSettings * settings = new QSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
  27. };
  28. #endif
  29. #ifdef Q_OS_LINUX
  30. #include <QFile>
  31. #include <QStandardPaths>
  32. class Autostart{
  33. public:
  34. Autostart (){
  35. file_data = file_data.replace("$", MY_APP_PATH);
  36. }
  37. bool status(){
  38. return QFile::exists(file_path);
  39. }
  40. void set(bool s){
  41. s ? make_file() : (void)QFile::remove(file_path);
  42. }
  43. void switch_(){
  44. status() ? (void)QFile::remove(file_path) : make_file();
  45. }
  46. private:
  47. QString file_path = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)[0] + "/autostart/NumLockManager.desktop";
  48. QString file_data = QString("[Desktop Entry]\n"
  49. "Encoding=UTF-8\n"
  50. "Version=1.2\n"
  51. "Type=Application\n"
  52. "Name=NumLockManager\n"
  53. "Comment=\n"
  54. "Exec=$\n"
  55. "RunHook=0\n"
  56. "StartupNotify=false\n"
  57. "Terminal=false\n"
  58. "Hidden=false\n");
  59. void make_file(){
  60. QFile file(file_path);
  61. file.open(QIODevice::WriteOnly | QIODevice::Text);
  62. file.write(file_data.toUtf8());
  63. file.close();
  64. }
  65. };
  66. #endif
  67. #endif // AUTOSTART_H