26 #include <QStringList> 28 #include <QCoreApplication> 30 #if QT_VERSION >= 0x050000 31 #include <QStandardPaths> 72 std::string programFiles;
73 (void)
getEnvUtf8(
"ProgramFiles", programFiles);
74 if (programFiles !=
"") {
75 list << QString(
"%1/%2/%3")
76 .arg(QString::fromStdString(programFiles))
77 .arg(qApp->organizationName())
78 .arg(qApp->applicationName());
80 list << QString(
"C:/Program Files/%1/%2")
81 .arg(qApp->organizationName())
82 .arg(qApp->applicationName());
86 list << QString(
"/Library/Application Support/%1")
87 .arg(qApp->applicationName());
89 list << QString(
"/usr/local/share/%1")
90 .arg(qApp->applicationName());
91 list << QString(
"/usr/share/%1")
92 .arg(qApp->applicationName());
105 char *homedrive = getenv(
"HOMEDRIVE");
106 char *homepath = getenv(
"HOMEPATH");
108 if (homedrive && homepath) {
109 home = QString(
"%1%2").arg(homedrive).arg(homepath);
111 home = QDir::home().absolutePath();
113 if (home ==
"")
return "";
114 return QString(
"%1/.%2").arg(home).arg(qApp->applicationName());
116 char *home = getenv(
"HOME");
117 if (!home || !home[0])
return "";
119 return QString(
"%1/Library/Application Support/%2")
121 .arg(qApp->applicationName());
123 return QString(
"%1/.local/share/%2")
125 .arg(qApp->applicationName());
133 if (qApp->applicationName() ==
"" || qApp->organizationName() ==
"") {
134 cerr <<
"ERROR: Can't use ResourceFinder before setting application and organization name" << endl;
135 throw std::logic_error(
"Can't use ResourceFinder before setting application and organization name");
138 #if QT_VERSION >= 0x050000 146 #if QT_VERSION >= 0x050400 147 return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
149 cerr <<
"WARNING: ResourceFinder::getOldStyleUserResourcePrefix: Building with older version of Qt (pre 5.4), resource location may be incompatible with future versions" << endl;
150 return QStandardPaths::writableLocation(QStandardPaths::DataLocation);
154 cerr <<
"WARNING: ResourceFinder::getOldStyleUserResourcePrefix: Building with very old version of Qt (pre 5.0?), resource location may be incompatible with future versions" << endl;
165 if (oldPath != newPath &&
166 QDir(oldPath).exists() &&
167 !QDir(newPath).exists()) {
171 if (!d.mkpath(newPath)) {
172 cerr <<
"WARNING: Failed to create new-style resource path \"" 173 << newPath <<
"\" to migrate old resources to" << endl;
177 QDir target(newPath);
182 (d.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot));
184 foreach (QString entry, entries) {
185 if (d.rename(entry, target.filePath(entry))) {
186 cerr <<
"NOTE: Successfully moved resource \"" 187 << entry <<
"\" from old resource path to new" << endl;
189 cerr <<
"WARNING: Failed to move old resource \"" 190 << entry <<
"\" from old location \"" 191 << oldPath <<
"\" to new location \"" 192 << newPath <<
"\"" << endl;
198 if (!d.rmdir(oldPath)) {
199 cerr <<
"WARNING: Failed to remove old resource path \"" 200 << oldPath <<
"\" after migrating " << entries.size()
201 <<
" resource(s) to new path \"" << newPath
202 <<
"\" (directory not empty?)" << endl;
204 cerr <<
"NOTE: Successfully moved " << entries.size()
205 <<
" resource(s) from old resource " 206 <<
"path \"" << oldPath <<
"\" to new path \"" 207 << newPath <<
"\"" << endl;
228 if (user !=
"") list << user;
246 if (resourceCat !=
"") resourceCat =
"/" + resourceCat;
248 for (QStringList::const_iterator i = prefixes.begin();
249 i != prefixes.end(); ++i) {
256 QString(
"%1%2/%3").arg(prefix).arg(resourceCat).arg(fileName);
257 if (QFileInfo(path).exists() && QFileInfo(path).isReadable()) {
273 if (resourceCat !=
"") resourceCat =
"/" + resourceCat;
275 for (QStringList::const_iterator i = prefixes.begin();
276 i != prefixes.end(); ++i) {
279 QString path = QString(
"%1%2").arg(prefix).arg(resourceCat);
280 if (QFileInfo(path).exists() &&
281 QFileInfo(path).isDir() &&
282 QFileInfo(path).isReadable()) {
294 if (dir ==
"")
return "";
296 return dir +
"/" + fileName;
305 if (user ==
"")
return "";
307 if (resourceCat !=
"") resourceCat =
"/" + resourceCat;
310 if (!userDir.exists()) {
311 if (!userDir.mkpath(user)) {
312 cerr <<
"ResourceFinder::getResourceSaveDir: ERROR: Failed to create user resource path \"" << user <<
"\"" << endl;
317 if (resourceCat !=
"") {
318 QString save = QString(
"%1%2").arg(user).arg(resourceCat);
320 if (!saveDir.exists()) {
321 if (!saveDir.mkpath(save)) {
322 cerr <<
"ResourceFinder::getResourceSaveDir: ERROR: Failed to create user resource path \"" << save <<
"\"" << endl;
339 filters << QString(
"*.%1").arg(fileExt);
341 for (QStringList::const_iterator i = prefixes.begin();
342 i != prefixes.end(); ++i) {
347 if (resourceCat !=
"") {
348 path = QString(
"%1/%2").arg(prefix).arg(resourceCat);
354 if (!dir.exists())
continue;
356 dir.setNameFilters(filters);
357 QStringList entries = dir.entryList
358 (QDir::Files | QDir::Readable, QDir::Name);
360 for (QStringList::const_iterator j = entries.begin();
361 j != entries.end(); ++j) {
362 results << QString(
"%1/%2").arg(path).arg(*j);
374 if (!path.startsWith(
':'))
return true;
379 SVDEBUG <<
"ResourceFinder::unbundleResource: File " << fileName <<
" is bundled, un-bundling it" << endl;
382 if (!file.copy(target)) {
383 cerr <<
"ResourceFinder::unbundleResource: ERROR: Failed to un-bundle resource file \"" << fileName <<
"\" to user location \"" << target <<
"\"" << endl;
388 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...
bool getEnvUtf8(std::string variable, std::string &value)
Return the value of the given environment variable by reference.
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()