summaryrefslogtreecommitdiff
path: root/ResourcesManager.cpp
blob: 008bf50aa6d076bd834ec3647822c48f8430dfca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/** \file ResourcesManager.cpp
\brief Define the class to manage and load the resources linked with the themes
\author alpha_one_x86
\version 0.3
\date 2010
\licence GPL3, see the file COPYING */ 

#include <QDir>
#include <QFile>
#include <QApplication>
#include <QFSFileEngine>
#include <QFileInfo>


#include "ResourcesManager.h"

/// \brief Create the manager and load the defaults variables
ResourcesManager::ResourcesManager()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
	//load the internal path
	searchPath<<QString(":/");
	//load the user path but only if exists and writable
	//load the ultracopier path
	#ifdef ULTRACOPIER_VERSION_PORTABLE
		#ifdef ULTRACOPIER_VERSION_PORTABLEAPPS
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Information,"Ultracopier is compiled with the flag: ULTRACOPIER_VERSION_PORTABLEAPPS");
			//load the data folder path
			QDir dir(QApplication::applicationDirPath());
			dir.cdUp();
			dir.cdUp();
			dir.cd("Data");
			searchPath<<ResourcesManager::AddSlashIfNeeded(dir.absolutePath());
			writablePath=ResourcesManager::AddSlashIfNeeded(dir.absolutePath());
		#else
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Information,"Ultracopier is compiled with the flag: ULTRACOPIER_VERSION_PORTABLE");
			//load the ultracopier path
			QDir dir(QApplication::applicationDirPath());
			dir.cd("Data");
			searchPath<<ResourcesManager::AddSlashIfNeeded(dir.absolutePath());
			writablePath=ResourcesManager::AddSlashIfNeeded(dir.absolutePath());
		#endif
	#else
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Information,"Ultracopier is compiled as user privacy mode");
		#ifdef Q_OS_WIN32
			#define EXTRA_HOME_PATH "\\ultracopier\\"
		#else
			#define EXTRA_HOME_PATH "/.config/Ultracopier/"
		#endif
		#ifdef Q_OS_LINUX
			QDir linuxDir("/usr/share/ultracopier/");
			if(linuxDir.exists())
				searchPath<<"/usr/share/ultracopier/";
		#endif
		//load the user path but only if exists and writable
		QDir dir(QFSFileEngine::homePath()+EXTRA_HOME_PATH);
		if(dir.exists())
		{
			writablePath=ResourcesManager::AddSlashIfNeeded(dir.absolutePath());
			searchPath<<ResourcesManager::AddSlashIfNeeded(dir.absolutePath());
		} //if not exists try to create it
		else if(dir.mkpath(dir.absolutePath()))
		{
			//if created, then have write permissions
			writablePath=ResourcesManager::AddSlashIfNeeded(dir.absolutePath());
			searchPath<<ResourcesManager::AddSlashIfNeeded(dir.absolutePath());
		}
		//load the ultracopier path
		searchPath<<ResourcesManager::AddSlashIfNeeded(QApplication::applicationDirPath());
	#endif
	searchPath.removeDuplicates();
	#ifdef ULTRACOPIER_DEBUG
	index=0;
	loop_size=searchPath.size();
	while(index<loop_size) //look at each val
	{
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Information,"searchPath.at("+QString::number(index)+"): "+searchPath.at(index));
		index++;
	}
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Information,"writablePath: "+writablePath);
	#endif // ULTRACOPIER_DEBUG
}

/// \brief Destroy the resource manager
ResourcesManager::~ResourcesManager()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"start");
}

/// \brief Get folder presence and the path
QString ResourcesManager::getFolderReadPath(const QString &path)
{
	index=0;
	loop_size=searchPath.size();
	while(index<loop_size) //look at each val
	{
		QDir dir(searchPath.at(index)+path);
		if(dir.exists()) // if the path have been found, then return the full path
			return ResourcesManager::AddSlashIfNeeded(dir.absolutePath());
		index++;
	}
	return "";
}

/// \brief Get folder presence, the path and check in the folder and sub-folder the file presence
QString ResourcesManager::getFolderReadPathMultiple(const QString &path,const QStringList &fileToCheck)
{
	index=0;
	loop_size=searchPath.size();
	while(index<loop_size) //look at each val
	{
		QDir dir(searchPath.at(index)+path);
		if(checkFolderContent(dir.absolutePath(),fileToCheck))
			return dir.absolutePath()+QDir::separator();
		index++;
	}
	return "";
}

bool ResourcesManager::checkFolderContent(const QString &path,const QStringList &fileToCheck)
{
	QDir dir(path);
	if(dir.exists()) // if the path have been found, then return the full path
	{
		bool allFileToCheckIsFound=true;
		index=0;
		loop_size=fileToCheck.size();
		QString partialPath=ResourcesManager::AddSlashIfNeeded(dir.absolutePath());
		while(index<loop_size) //look at each val
		{
			if(!QFile::exists(partialPath+fileToCheck.at(index))) //if a file have been not found, consider the folder as not suitable
			{
				allFileToCheckIsFound=false;
				break;
			}
			index++;
		}
		if(allFileToCheckIsFound==true) // if all file into have been found then return this path
			return true;
	}
	return false;
}

/// \brief add / or \ in function of the platform at the end of path if both / and \ are not found
QString ResourcesManager::AddSlashIfNeeded(const QString &path)
{
	if(path.contains(QRegExp("[/\\\\]$")))
		return path;
	else
		return path+QDir::separator();
}

/// \brief get the writable path
QString ResourcesManager::getWritablePath()
{
	return writablePath;
}

/// \brief disable the writable path, if ultracopier is unable to write into
bool ResourcesManager::disableWritablePath()
{
	bool returnVal=true;
	if(writablePath=="")
		returnVal=false;
	else
		writablePath="";
	return returnVal;
}

/// \brief get the read path
QStringList ResourcesManager::getReadPath()
{
	return searchPath;
}

/// \brief remove folder
bool ResourcesManager::removeFolder(const QString &dir)
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"folder to remove: "+dir);
	bool errorFound=false;
	QDir currentDir(dir);
	QFileInfoList files = currentDir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
	int index=0;
	int loop_size=files.size();
	while(index<loop_size)
	{
		if(files.at(index).isFile())
		{
			QFile file(files.at(index).absoluteFilePath());
			if(!file.remove())
			{
				errorFound=true;
				ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Critical,"remove file failed: "+file.errorString());
			}
			else
				ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"file removed: "+file.fileName());
		}
		else if(files.at(index).isDir())
			removeFolder(files.at(index).absoluteFilePath());
		else
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"unknow file type for: "+files.at(index).absoluteFilePath());
		index++;
	}
	if(!currentDir.rmpath(dir))
	{
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Critical,"remove path failed, check right and if is empty: "+dir);
		errorFound=true;
	}
	return !errorFound;
}