26 #include <QStringList> 28 #include <QCoreApplication> 30 #if QT_VERSION >= 0x050000 31 #include <QStandardPaths> 69 char *programFiles = getenv(
"ProgramFiles");
70 if (programFiles && programFiles[0]) {
71 list << QString(
"%1/%2/%3")
73 .arg(qApp->organizationName())
74 .arg(qApp->applicationName());
76 list << QString(
"C:/Program Files/%1/%2")
77 .arg(qApp->organizationName())
78 .arg(qApp->applicationName());
82 list << QString(
"/Library/Application Support/%1")
83 .arg(qApp->applicationName());
85 list << QString(
"/usr/local/share/%1")
86 .arg(qApp->applicationName());
87 list << QString(
"/usr/share/%1")
88 .arg(qApp->applicationName());
101 char *homedrive = getenv(
"HOMEDRIVE");
102 char *homepath = getenv(
"HOMEPATH");
104 if (homedrive && homepath) {
105 home = QString(
"%1%2").arg(homedrive).arg(homepath);
107 home = QDir::home().absolutePath();
109 if (home ==
"")
return "";
110 return QString(
"%1/.%2").arg(home).arg(qApp->applicationName());
112 char *home = getenv(
"HOME");
113 if (!home || !home[0])
return "";
115 return QString(
"%1/Library/Application Support/%2")
117 .arg(qApp->applicationName());
119 return QString(
"%1/.local/share/%2")
121 .arg(qApp->applicationName());
129 #if QT_VERSION >= 0x050000 136 return QStandardPaths::writableLocation(QStandardPaths::DataLocation);
148 if (oldPath != newPath &&
149 QDir(oldPath).exists() &&
150 !QDir(newPath).exists()) {
154 if (!d.mkpath(newPath)) {
155 cerr <<
"WARNING: Failed to create new-style resource path \"" 156 << newPath <<
"\" to migrate old resources to" << endl;
160 QDir target(newPath);
165 (d.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot));
167 foreach (QString entry, entries) {
168 if (d.rename(entry, target.filePath(entry))) {
169 cerr <<
"NOTE: Successfully moved resource \"" 170 << entry <<
"\" from old resource path to new" << endl;
172 cerr <<
"WARNING: Failed to move old resource \"" 173 << entry <<
"\" from old location \"" 174 << oldPath <<
"\" to new location \"" 175 << newPath <<
"\"" << endl;
181 if (!d.rmdir(oldPath)) {
182 cerr <<
"WARNING: Failed to remove old resource path \"" 183 << oldPath <<
"\" after migrating " << entries.size()
184 <<
" resource(s) to new path \"" << newPath
185 <<
"\" (directory not empty?)" << endl;
187 cerr <<
"NOTE: Successfully moved " << entries.size()
188 <<
" resource(s) from old resource " 189 <<
"path \"" << oldPath <<
"\" to new path \"" 190 << newPath <<
"\"" << endl;
211 if (user !=
"") list << user;
229 if (resourceCat !=
"") resourceCat =
"/" + resourceCat;
231 for (QStringList::const_iterator i = prefixes.begin();
232 i != prefixes.end(); ++i) {
236 SVDEBUG <<
"ResourceFinder::getResourcePath: Looking up file \"" << fileName <<
"\" for category \"" << resourceCat <<
"\" in prefix \"" <<
prefix <<
"\"" << endl;
239 QString(
"%1%2/%3").arg(
prefix).arg(resourceCat).arg(fileName);
240 if (QFileInfo(path).exists() && QFileInfo(path).isReadable()) {
241 cerr <<
"Found it!" << endl;
256 if (resourceCat !=
"") resourceCat =
"/" + resourceCat;
258 for (QStringList::const_iterator i = prefixes.begin();
259 i != prefixes.end(); ++i) {
262 QString path = QString(
"%1%2").arg(
prefix).arg(resourceCat);
263 if (QFileInfo(path).exists() &&
264 QFileInfo(path).isDir() &&
265 QFileInfo(path).isReadable()) {
277 if (dir ==
"")
return "";
279 return dir +
"/" + fileName;
288 if (user ==
"")
return "";
290 if (resourceCat !=
"") resourceCat =
"/" + resourceCat;
293 if (!userDir.exists()) {
294 if (!userDir.mkpath(user)) {
295 cerr <<
"ResourceFinder::getResourceSaveDir: ERROR: Failed to create user resource path \"" << user <<
"\"" << endl;
300 if (resourceCat !=
"") {
301 QString save = QString(
"%1%2").arg(user).arg(resourceCat);
303 if (!saveDir.exists()) {
304 if (!saveDir.mkpath(save)) {
305 cerr <<
"ResourceFinder::getResourceSaveDir: ERROR: Failed to create user resource path \"" << save <<
"\"" << endl;
322 filters << QString(
"*.%1").arg(fileExt);
324 for (QStringList::const_iterator i = prefixes.begin();
325 i != prefixes.end(); ++i) {
330 if (resourceCat !=
"") {
331 path = QString(
"%1/%2").arg(
prefix).arg(resourceCat);
337 if (!dir.exists())
continue;
339 dir.setNameFilters(filters);
340 QStringList entries = dir.entryList
341 (QDir::Files | QDir::Readable, QDir::Name);
343 for (QStringList::const_iterator j = entries.begin();
344 j != entries.end(); ++j) {
345 results << QString(
"%1/%2").arg(path).arg(*j);
357 if (!path.startsWith(
':'))
return true;
362 SVDEBUG <<
"ResourceFinder::unbundleResource: File " << fileName <<
" is bundled, un-bundling it" << endl;
365 if (!file.copy(target)) {
366 cerr <<
"ResourceFinder::unbundleResource: ERROR: Failed to un-bundle resource file \"" << fileName <<
"\" to user location \"" << target <<
"\"" << endl;
371 chmod.setPermissions(QFile::ReadOwner |
QString getResourceSaveDir(QString resourceCat)
Return the true file path for the location in which resource files in the given resource category sho...
QStringList getResourcePrefixList()
Return all root paths for resource installations for this application, in the order in which they wil...
QString getResourcePath(QString resourceCat, QString fileName)
Return the location (as a true file path, or a Qt4 ":"-prefixed resource path) of the file best match...
QStringList getResourceFiles(QString resourceCat, QString fileExt)
Return a list of full file paths for files with the given file extension, found in the given resource...
static QString getOldStyleUserResourcePrefix()
QStringList getSystemResourcePrefixList()
Return the root paths for systemwide resource installations for this application.
static QString getNewStyleUserResourcePrefix()
QString getUserResourcePrefix()
Return the root path for user-specific resource installation for this application (i....
bool unbundleResource(QString resourceCat, QString fileName)
If the named resource file in the given resource category is available only as a bundled resource,...
QString getResourceSavePath(QString resourceCat, QString fileName)
Return the true file path for the location in which the named resource file in the given resource cat...
QString getResourceDir(QString resourceCat)
Return the true file path for installed resource files in the given resource category.
static void migrateOldStyleResources()