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