Settings: unify duplicated code
[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{
41 foreach (const QString& dir, XdgDirs::autostartDirs())
42 {
43 QString path = QString("%1/%2").arg(dir, name);
44 if (QFile(path).exists())
45 {
46 mSystemFile.load(path);
47 mSystem = true;
48 break;
49 }
50 }
51
52 QString path = QString("%1/%2").arg(XdgDirs::autostartHome(), name);
53 if (QFile(path).exists())
54 {
55 mLocalFile.load(path);
56 mLocalState = StateExists;
57 }
58}
59
739b8f7f 60void AutostartEntry::setFile(const XdgDesktopFile& file)
6e6a51fe
AM
61{
62 bool local = isLocal();
63 if (mSystem && local && file == mSystemFile)
64 {
65 removeLocal();
66 }
67 else
68 {
69 if (local)
70 mLocalState = StateModified;
71 else
72 mLocalState = StateTransient;
73 mLocalFile = file;
74 }
75}
76
739b8f7f 77bool AutostartEntry::removeLocal()
6e6a51fe
AM
78{
79 if (!isLocal())
80 return false;
81
82 if (mLocalState == StateTransient)
83 mLocalState = StateNone;
84 else
85 mLocalState = StateDeleted;
86
87 return !mSystem;
88}
89
739b8f7f 90const XdgDesktopFile& AutostartEntry::file() const
6e6a51fe
AM
91{
92 return isLocal() ? mLocalFile : mSystemFile;
93}
94
739b8f7f 95QString AutostartEntry::name() const
6e6a51fe
AM
96{
97 return QFileInfo(file().fileName()).fileName();
98}
99
739b8f7f 100void AutostartEntry::setEnabled(bool enable)
6e6a51fe
AM
101{
102 XdgDesktopFile f = file();
103 if (enable)
104 f.removeEntry("Hidden");
105 else
106 f.setValue("Hidden", true);
107
108 setFile(f);
109}
110
739b8f7f 111bool AutostartEntry::isEnabled() const
6e6a51fe 112{
b30db56a 113 return !isEmpty() && !file().value("Hidden", false).toBool();
6e6a51fe
AM
114}
115
739b8f7f 116bool AutostartEntry::commit()
6e6a51fe
AM
117{
118 if (mLocalState == StateDeleted)
119 {
120 mLocalState = StateNone;
121 return QFile::remove(mLocalFile.fileName());
122 }
123 else if (mLocalState == StateModified || mLocalState == StateTransient)
124 {
125 mLocalState = StateExists;
126 return mLocalFile.save(XdgAutoStart::localPath(mLocalFile));
127 }
128 return true;
129}