Make lxqt globals a public header
[lxde/liblxqt.git] / lxqtautostartentry.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - a lightweight, Qt based, desktop toolset
5 * http://razor-qt.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 "lxqtautostartentry.h"
26 #include <QFileInfo>
27
28 #include <XdgAutoStart>
29 #include <XdgDirs>
30
31 using namespace LXQt;
32
33 AutostartEntry::AutostartEntry() :
34 mLocalState(StateNone), mSystem(false)
35 {
36 }
37
38 AutostartEntry::AutostartEntry(const QString& name):
39 mLocalState(StateNone), mSystem(false)
40 {
41 const QStringList& dirs = XdgDirs::autostartDirs();
42 for (const QString& dir : dirs)
43 {
44 const QString path = QString("%1/%2").arg(dir, name);
45 if (QFile(path).exists())
46 {
47 mSystemFile.load(path);
48 mSystem = true;
49 break;
50 }
51 }
52
53 const QString path = QString("%1/%2").arg(XdgDirs::autostartHome(), name);
54 if (QFile(path).exists())
55 {
56 mLocalFile.load(path);
57 mLocalState = StateExists;
58 }
59 }
60
61 void AutostartEntry::setFile(const XdgDesktopFile& file)
62 {
63 const bool local = isLocal();
64 if (mSystem && local && file == mSystemFile)
65 {
66 removeLocal();
67 }
68 else
69 {
70 if (local)
71 mLocalState = StateModified;
72 else
73 mLocalState = StateTransient;
74 mLocalFile = file;
75 }
76 }
77
78 bool AutostartEntry::removeLocal()
79 {
80 if (!isLocal())
81 return false;
82
83 if (mLocalState == StateTransient)
84 mLocalState = StateNone;
85 else
86 mLocalState = StateDeleted;
87
88 return !mSystem;
89 }
90
91 const XdgDesktopFile& AutostartEntry::file() const
92 {
93 return isLocal() ? mLocalFile : mSystemFile;
94 }
95
96 QString AutostartEntry::name() const
97 {
98 return QFileInfo(file().fileName()).fileName();
99 }
100
101 void AutostartEntry::setEnabled(bool enable)
102 {
103 XdgDesktopFile f = file();
104 if (enable)
105 f.removeEntry("Hidden");
106 else
107 f.setValue("Hidden", true);
108
109 setFile(f);
110 }
111
112 bool AutostartEntry::isEnabled() const
113 {
114 return !isEmpty() && !file().value("Hidden", false).toBool();
115 }
116
117 bool AutostartEntry::commit()
118 {
119 if (mLocalState == StateDeleted)
120 {
121 mLocalState = StateNone;
122 return QFile::remove(mLocalFile.fileName());
123 }
124 else if (mLocalState == StateModified || mLocalState == StateTransient)
125 {
126 mLocalState = StateExists;
127 return mLocalFile.save(XdgAutoStart::localPath(mLocalFile));
128 }
129 return true;
130 }