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