1 /*
2  * %kadu copyright begin%
3  * Copyright 2012, 2013 Bartosz Brachaczek (b.brachaczek@gmail.com)
4  * Copyright 2011, 2013, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
5  * %kadu copyright end%
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "filedesc.h"
22 
23 #include "filedesc-status-changer.h"
24 
25 #include "configuration/configuration.h"
26 #include "configuration/deprecated-configuration-api.h"
27 #include "core/core.h"
28 #include "misc/paths-provider.h"
29 
30 #include <QtCore/QFile>
31 #include <QtCore/QTextStream>
32 #include <QtCore/QTimer>
33 
FileDescription(QObject * parent)34 FileDescription::FileDescription(QObject *parent) :
35 		QObject(parent)
36 {
37 	m_timer = new QTimer{this};
38 	m_timer->setSingleShot(false);
39 	m_timer->setInterval(500);
40 	connect(m_timer, SIGNAL(timeout()), this, SLOT(checkTitle()));
41 	m_timer->start();
42 }
43 
~FileDescription()44 FileDescription::~FileDescription()
45 {
46 	m_timer->stop();
47 }
48 
setConfiguration(Configuration * configuration)49 void FileDescription::setConfiguration(Configuration *configuration)
50 {
51 	m_configuration = configuration;
52 }
53 
setFileDescStatusChanger(FileDescStatusChanger * fileDescStatusChanger)54 void FileDescription::setFileDescStatusChanger(FileDescStatusChanger *fileDescStatusChanger)
55 {
56 	m_fileDescStatusChanger = fileDescStatusChanger;
57 }
58 
setPathsProvider(PathsProvider * pathsProvider)59 void FileDescription::setPathsProvider(PathsProvider *pathsProvider)
60 {
61 	m_pathsProvider = pathsProvider;
62 }
63 
init()64 void FileDescription::init()
65 {
66 	createDefaultConfiguration();
67 	configurationUpdated();
68 }
69 
configurationUpdated()70 void FileDescription::configurationUpdated()
71 {
72 	m_file = m_configuration->deprecatedApi()->readEntry("FileDesc", "file", m_pathsProvider->profilePath() + QStringLiteral("description.txt"));
73 	m_forceDesc = m_configuration->deprecatedApi()->readBoolEntry("FileDesc", "forceDescr", true);
74 	m_allowOther = m_configuration->deprecatedApi()->readBoolEntry("FileDesc", "allowOther", true);
75 
76 	checkTitle();
77 }
78 
checkTitle()79 void FileDescription::checkTitle()
80 {
81 	QFile file{m_file};
82 
83 	if (!file.exists())
84 		return;
85 
86 	if (!file.open(QIODevice::ReadOnly))
87 		return;
88 
89 	QString description;
90 	QTextStream stream(&file);
91 	if (!stream.atEnd())
92 		description = stream.readLine();
93 	file.close();
94 
95 	m_fileDescStatusChanger->setTitle(description);
96 }
97 
createDefaultConfiguration()98 void FileDescription::createDefaultConfiguration()
99 {
100 	m_configuration->deprecatedApi()->addVariable("FileDesc", "file", m_pathsProvider->profilePath() + QStringLiteral("description.txt"));
101 	m_configuration->deprecatedApi()->addVariable("FileDesc", "forceDescr", true);
102 	m_configuration->deprecatedApi()->addVariable("FileDesc", "allowOther", true);
103 }
104 
105 #include "moc_filedesc.cpp"
106