autostartpane.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "autostartpane.h"
  2. #include "../dialogs/addautostartentrydialog.h"
  3. void AutostartPane::setPaneContents() {
  4. mContentsWidget = new QWidget();
  5. QVBoxLayout* contentsLayout = new QVBoxLayout(mContentsWidget);
  6. mContentsWidget->setObjectName("innerPane");
  7. // Adding widgets
  8. QLabel* entriesLabel = new QLabel("Autostart Entries");
  9. entriesLabel->setFont(mTitleFont);
  10. contentsLayout->addWidget(entriesLabel);
  11. QHBoxLayout* autostartLayout = new QHBoxLayout();
  12. mAutostartListWidget = new QListWidget();
  13. mAutostartListWidget->setFont(mFont);
  14. QString style = QString("QListView::item:selected { "
  15. "background-color: %1; color: #ffffff; "
  16. "};").arg(getConfigValue("accent").toString());
  17. mAutostartListWidget->setStyleSheet(style);
  18. autostartLayout->addWidget(mAutostartListWidget);
  19. QVBoxLayout* buttonsLayout = new QVBoxLayout();
  20. QPushButton* addButton = new QPushButton("+");
  21. addButton->setFont(mFont);
  22. buttonsLayout->addWidget(addButton);
  23. QPushButton* removeButton = new QPushButton("-");
  24. removeButton->setFont(mFont);
  25. buttonsLayout->addWidget(removeButton);
  26. buttonsLayout->addSpacerItem(new QSpacerItem(0, 0,
  27. QSizePolicy::Ignored,
  28. QSizePolicy::MinimumExpanding));
  29. autostartLayout->addLayout(buttonsLayout);
  30. contentsLayout->addLayout(autostartLayout);
  31. // Set current settings
  32. QString path = QString("%1/.config/autostart").arg(QDir::homePath());
  33. QDir autostartDir(path);
  34. if (autostartDir.exists()) {
  35. QStringList autostartEntries = autostartDir.entryList(QDir::Files | QDir::NoDotAndDotDot);
  36. foreach (QString entry, autostartEntries) {
  37. QString name, iconName;
  38. QSettings desktopFileReader(autostartDir.absoluteFilePath(entry),
  39. QSettings::IniFormat);
  40. desktopFileReader.sync();
  41. desktopFileReader.beginGroup("Desktop Entry");
  42. name = desktopFileReader.value("Name").toString();
  43. iconName = desktopFileReader.value("Icon").toString();
  44. desktopFileReader.endGroup();
  45. QListWidgetItem* item = new QListWidgetItem(name);
  46. if (QIcon::hasThemeIcon(iconName)) {
  47. item->setIcon(QIcon::fromTheme(iconName));
  48. }
  49. else if (QFile::exists(iconName)) {
  50. item->setIcon(QIcon(iconName));
  51. }
  52. else {
  53. item->setIcon(QIcon::fromTheme("dialog-question"));
  54. }
  55. mAutostartListWidget->addItem(item);
  56. mEntryByItem[item] = autostartDir.absoluteFilePath(entry);
  57. }
  58. }
  59. else {
  60. autostartDir.mkpath(path);
  61. }
  62. // Making connections
  63. connect(addButton, &QPushButton::clicked, this, [this]() {
  64. auto addAutostartEntryDialog = new AddAutostartEntryDialog(mCfgObj, this);
  65. addAutostartEntryDialog->setPaneContents();
  66. addAutostartEntryDialog->show();
  67. });
  68. connect(removeButton, &QPushButton::clicked, this, [this]() {
  69. if (!mAutostartListWidget->selectedItems().isEmpty()) {
  70. QListWidgetItem* item = mAutostartListWidget->selectedItems()[0];
  71. QFile::remove(mEntryByItem[item]);
  72. mEntryByItem.remove(item);
  73. delete item;
  74. }
  75. });
  76. finalizeUI();
  77. }
  78. void AutostartPane::addEntry(QString entry) {
  79. QString name, iconName;
  80. QSettings desktopFileReader(entry, QSettings::IniFormat);
  81. desktopFileReader.sync();
  82. desktopFileReader.beginGroup("Desktop Entry");
  83. name = desktopFileReader.value("Name").toString();
  84. iconName = desktopFileReader.value("Icon").toString();
  85. desktopFileReader.endGroup();
  86. QListWidgetItem* item = new QListWidgetItem(name);
  87. if (QIcon::hasThemeIcon(iconName)) {
  88. item->setIcon(QIcon::fromTheme(iconName));
  89. }
  90. else if (QFile::exists(iconName)) {
  91. item->setIcon(QIcon(iconName));
  92. }
  93. else {
  94. item->setIcon(QIcon::fromTheme("dialog-question"));
  95. }
  96. mAutostartListWidget->addItem(item);
  97. QString filename = entry.split('/').last();
  98. QString path = QString("%1/.config/autostart/%2").arg(QDir::homePath(), filename);
  99. mEntryByItem[item] = path;
  100. if (QFile::exists(path)) {
  101. QFile::remove(path);
  102. }
  103. QFile::copy(entry, path);
  104. }
  105. AutostartPane::AutostartPane(QJsonObject* cfgObj) : Pane(nullptr,
  106. cfgObj,
  107. tr("Autostart"),
  108. "cs-general") {
  109. }