Updated Russian translation
[lxde/liblxqt.git] / lxqtautostartentry.cpp
1 /* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - 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 <QFileInfo>
27
28 #include <XdgAutoStart>
29 #include <XdgDirs>
30
31 using namespace LXQt;
32
33 AutostartEntry::AutostartEntry() :
34 mLocalState(StateNone), mSystem(false)
35 {
36 }
37
38 AutostartEntry::AutostartEntry(const QString& name):
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
60 void AutostartEntry::setFile(const XdgDesktopFile& file)
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
77 bool AutostartEntry::removeLocal()
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
90 const XdgDesktopFile& AutostartEntry::file() const
91 {
92 return isLocal() ? mLocalFile : mSystemFile;
93 }
94
95 QString AutostartEntry::name() const
96 {
97 return QFileInfo(file().fileName()).fileName();
98 }
99
100 void AutostartEntry::setEnabled(bool enable)
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
111 bool AutostartEntry::isEnabled() const
112 {
113 return !isEmpty() && !file().value("Hidden", false).toBool();
114 }
115
116 bool AutostartEntry::commit()
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 }