RazorAutostartEntry => LxQt::AutostartEntry
[lxde/liblxqt.git] / lxqtautostartentry.cpp
CommitLineData
6e6a51fe
AM
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
739b8f7f 25#include "lxqtautostartentry.h"
6e6a51fe
AM
26#include <qtxdg/xdgautostart.h>
27#include <qtxdg/xdgdirs.h>
28#include <QtCore/QFileInfo>
29
739b8f7f
AS
30using namespace LxQt;
31
32AutostartEntry::AutostartEntry() :
6e6a51fe
AM
33 mLocalState(StateNone), mSystem(false)
34{
35}
36
739b8f7f 37AutostartEntry::AutostartEntry(const QString& name):
6e6a51fe
AM
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
739b8f7f 59void AutostartEntry::setFile(const XdgDesktopFile& file)
6e6a51fe
AM
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
739b8f7f 76bool AutostartEntry::removeLocal()
6e6a51fe
AM
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
739b8f7f 89const XdgDesktopFile& AutostartEntry::file() const
6e6a51fe
AM
90{
91 return isLocal() ? mLocalFile : mSystemFile;
92}
93
739b8f7f 94QString AutostartEntry::name() const
6e6a51fe
AM
95{
96 return QFileInfo(file().fileName()).fileName();
97}
98
739b8f7f 99void AutostartEntry::setEnabled(bool enable)
6e6a51fe
AM
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
739b8f7f 110bool AutostartEntry::isEnabled() const
6e6a51fe 111{
b30db56a 112 return !isEmpty() && !file().value("Hidden", false).toBool();
6e6a51fe
AM
113}
114
739b8f7f 115bool AutostartEntry::commit()
6e6a51fe
AM
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}