Make lxqt globals a public header
[lxde/liblxqt.git] / lxqtautostartentry.cpp
CommitLineData
6e6a51fe
AM
1/* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
b9223fe7 4 * LXQt - a lightweight, Qt based, desktop toolset
6e6a51fe
AM
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
739b8f7f 25#include "lxqtautostartentry.h"
e6389bfc 26#include <QFileInfo>
afd3b116 27
e6389bfc
HJYP
28#include <XdgAutoStart>
29#include <XdgDirs>
6e6a51fe 30
f05ba5af 31using namespace LXQt;
739b8f7f
AS
32
33AutostartEntry::AutostartEntry() :
6e6a51fe
AM
34 mLocalState(StateNone), mSystem(false)
35{
36}
37
739b8f7f 38AutostartEntry::AutostartEntry(const QString& name):
6e6a51fe
AM
39 mLocalState(StateNone), mSystem(false)
40{
8dd17d46 41 const QStringList& dirs = XdgDirs::autostartDirs();
5bf85d40 42 for (const QString& dir : dirs)
6e6a51fe 43 {
d7858989 44 const QString path = QString("%1/%2").arg(dir, name);
6e6a51fe
AM
45 if (QFile(path).exists())
46 {
47 mSystemFile.load(path);
48 mSystem = true;
49 break;
50 }
51 }
52
d7858989 53 const QString path = QString("%1/%2").arg(XdgDirs::autostartHome(), name);
6e6a51fe
AM
54 if (QFile(path).exists())
55 {
56 mLocalFile.load(path);
57 mLocalState = StateExists;
58 }
59}
60
739b8f7f 61void AutostartEntry::setFile(const XdgDesktopFile& file)
6e6a51fe 62{
d7858989 63 const bool local = isLocal();
6e6a51fe
AM
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
739b8f7f 78bool AutostartEntry::removeLocal()
6e6a51fe
AM
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
739b8f7f 91const XdgDesktopFile& AutostartEntry::file() const
6e6a51fe
AM
92{
93 return isLocal() ? mLocalFile : mSystemFile;
94}
95
739b8f7f 96QString AutostartEntry::name() const
6e6a51fe
AM
97{
98 return QFileInfo(file().fileName()).fileName();
99}
100
739b8f7f 101void AutostartEntry::setEnabled(bool enable)
6e6a51fe
AM
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
739b8f7f 112bool AutostartEntry::isEnabled() const
6e6a51fe 113{
b30db56a 114 return !isEmpty() && !file().value("Hidden", false).toBool();
6e6a51fe
AM
115}
116
739b8f7f 117bool AutostartEntry::commit()
6e6a51fe
AM
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}