1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2  * (c)LGPL2+
3  *
4  * LXQt - a lightweight, Qt based, desktop toolset
5  * https://lxqt.org/
6  *
7  * Copyright (C) 2012  Alec Moskvin <alecm@gmx.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13 
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18 
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  * END_COMMON_COPYRIGHT_HEADER */
24 
25 #include "autostartpage.h"
26 #include "ui_autostartpage.h"
27 
28 #include "autostartedit.h"
29 #include "autostartutils.h"
30 
31 #include <LXQt/Globals>
32 
33 #include <QMessageBox>
34 
AutoStartPage(QWidget * parent)35 AutoStartPage::AutoStartPage(QWidget* parent) :
36     QWidget(parent),
37     ui(new Ui::AutoStartPage)
38 {
39     ui->setupUi(this);
40 
41     connect(ui->addButton,    &QPushButton::clicked, this, &AutoStartPage::addButton_clicked);
42     connect(ui->editButton,   &QPushButton::clicked, this, &AutoStartPage::editButton_clicked);
43     connect(ui->deleteButton, &QPushButton::clicked, this, &AutoStartPage::deleteButton_clicked);
44 
45     restoreSettings();
46 }
47 
~AutoStartPage()48 AutoStartPage::~AutoStartPage()
49 {
50     delete ui;
51 }
52 
restoreSettings()53 void AutoStartPage::restoreSettings()
54 {
55     QAbstractItemModel* oldModel = ui->autoStartView->model();
56     mXdgAutoStartModel = new AutoStartItemModel(ui->autoStartView);
57     ui->autoStartView->setModel(mXdgAutoStartModel);
58     delete oldModel;
59     ui->autoStartView->setExpanded(mXdgAutoStartModel->index(0, 0), true);
60     ui->autoStartView->setExpanded(mXdgAutoStartModel->index(1, 0), true);
61     updateButtons();
62     connect(mXdgAutoStartModel, &AutoStartItemModel::dataChanged, this, &AutoStartPage::updateButtons);
63     connect(ui->autoStartView->selectionModel(), &QItemSelectionModel::currentChanged, this, &AutoStartPage::selectionChanged);
64 }
65 
save()66 void AutoStartPage::save()
67 {
68     bool doRestart = false;
69 
70     /*
71      * Get the previous settings
72      */
73     QMap<QString, AutostartItem> previousItems(AutostartItem::createItemMap());
74     QMutableMapIterator<QString, AutostartItem> i(previousItems);
75     while (i.hasNext()) {
76         i.next();
77         if (AutostartUtils::isLXQtModule(i.value().file()))
78             i.remove();
79     }
80 
81 
82     /*
83      * Get the settings from the Ui
84      */
85     QMap<QString, AutostartItem> currentItems = mXdgAutoStartModel->items();
86     QMutableMapIterator<QString, AutostartItem> j(currentItems);
87 
88     while (j.hasNext()) {
89         j.next();
90         if (AutostartUtils::isLXQtModule(j.value().file()))
91             j.remove();
92     }
93 
94 
95     /* Compare the settings */
96 
97     if (previousItems.count() != currentItems.count())
98     {
99         doRestart = true;
100     }
101     else
102     {
103         QMap<QString, AutostartItem>::const_iterator k = currentItems.constBegin();
104         while (k != currentItems.constEnd())
105         {
106             if (previousItems.contains(k.key()))
107             {
108                 if (k.value().file() != previousItems.value(k.key()).file())
109                 {
110                     doRestart = true;
111                     break;
112                 }
113             }
114             else
115             {
116                 doRestart = true;
117                 break;
118             }
119             ++k;
120         }
121     }
122 
123     if (doRestart)
124         emit needRestart();
125 
126     mXdgAutoStartModel->writeChanges();
127 }
128 
addButton_clicked()129 void AutoStartPage::addButton_clicked()
130 {
131     AutoStartEdit edit(QString(), QString(), false);
132     bool success = false;
133     while (!success && edit.exec() == QDialog::Accepted)
134     {
135         QModelIndex index = ui->autoStartView->selectionModel()->currentIndex();
136         XdgDesktopFile file(XdgDesktopFile::ApplicationType, edit.name(), edit.command());
137         if (edit.needTray())
138             file.setValue(QL1S("X-LXQt-Need-Tray"), true);
139         if (mXdgAutoStartModel->setEntry(index, file))
140             success = true;
141         else
142             QMessageBox::critical(this, tr("Error"), tr("File '%1' already exists!").arg(file.fileName()));
143     }
144 }
145 
editButton_clicked()146 void AutoStartPage::editButton_clicked()
147 {
148     QModelIndex index = ui->autoStartView->selectionModel()->currentIndex();
149     XdgDesktopFile file = mXdgAutoStartModel->desktopFile(index);
150     AutoStartEdit edit(file.name(), file.value(QL1S("Exec")).toString(), file.contains(QL1S("X-LXQt-Need-Tray")));
151     if (edit.exec() == QDialog::Accepted)
152     {
153         file.setLocalizedValue(QL1S("Name"), edit.name());
154         file.setValue(QL1S("Exec"), edit.command());
155         if (edit.needTray())
156             file.setValue(QL1S("X-LXQt-Need-Tray"), true);
157         else
158             file.removeEntry(QL1S("X-LXQt-Need-Tray"));
159 
160         mXdgAutoStartModel->setEntry(index, file, true);
161     }
162 }
163 
deleteButton_clicked()164 void AutoStartPage::deleteButton_clicked()
165 {
166     QModelIndex index = ui->autoStartView->selectionModel()->currentIndex();
167     mXdgAutoStartModel->removeRow(index.row(), index.parent());
168 }
169 
selectionChanged(QModelIndex curr)170 void AutoStartPage::selectionChanged(QModelIndex curr)
171 {
172     AutoStartItemModel::ActiveButtons active = mXdgAutoStartModel->activeButtons(curr);
173     ui->addButton->setEnabled(active & AutoStartItemModel::AddButton);
174     ui->editButton->setEnabled(active & AutoStartItemModel::EditButton);
175     ui->deleteButton->setEnabled(active & AutoStartItemModel::DeleteButton);
176 }
177 
updateButtons()178 void AutoStartPage::updateButtons()
179 {
180     selectionChanged(ui->autoStartView->selectionModel()->currentIndex());
181 }
182