annotate base/ResourceFinder.cpp @ 982:f161ca450050

Use QStandardPaths::writableLocation(DataLocation) instead of synthesising path ourselves (hopefully fixing encoding difficulties on Windows)
author Chris Cannam
date Tue, 09 Sep 2014 20:37:07 +0100
parents 9a422d610058
children a8f91db36e9d
rev   line source
Chris@679 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@679 2
Chris@679 3 /*
Chris@679 4 Sonic Visualiser
Chris@679 5 An audio file viewer and annotation editor.
Chris@679 6 Centre for Digital Music, Queen Mary, University of London.
Chris@679 7
Chris@679 8 This program is free software; you can redistribute it and/or
Chris@679 9 modify it under the terms of the GNU General Public License as
Chris@679 10 published by the Free Software Foundation; either version 2 of the
Chris@679 11 License, or (at your option) any later version. See the file
Chris@679 12 COPYING included with this distribution for more information.
Chris@679 13 */
Chris@679 14
Chris@679 15 /*
Chris@679 16 This is a modified version of a source file from the
Chris@679 17 Rosegarden MIDI and audio sequencer and notation editor.
Chris@679 18 This file copyright 2005-2011 Chris Cannam and the Rosegarden
Chris@679 19 development team.
Chris@679 20 */
Chris@679 21
Chris@679 22 #include "ResourceFinder.h"
Chris@679 23
Chris@679 24 #include <QDir>
Chris@679 25 #include <QFileInfo>
Chris@679 26 #include <QStringList>
Chris@679 27 #include <QProcess>
Chris@679 28 #include <QCoreApplication>
Chris@679 29
Chris@982 30 #if QT_VERSION >= 0x050000
Chris@982 31 #include <QStandardPaths>
Chris@982 32 #endif
Chris@982 33
Chris@679 34 #include <cstdlib>
Chris@679 35 #include <iostream>
Chris@679 36
Chris@679 37 /**
Chris@679 38 Resource files may be found in three places:
Chris@679 39
Chris@679 40 * Bundled into the application as Qt4 resources. These may be
Chris@679 41 opened using Qt classes such as QFile, with "fake" file paths
Chris@679 42 starting with a colon. For example ":icons/fileopen.png".
Chris@679 43
Chris@679 44 * Installed with the package, or in the user's equivalent home
Chris@679 45 directory location. For example,
Chris@679 46
Chris@679 47 - on Linux, in /usr/share/<appname> or /usr/local/share/<appname>
Chris@679 48 - on Linux, in $HOME/.local/share/<appname>
Chris@679 49
Chris@679 50 - on OS/X, in /Library/Application Support/<appname>
Chris@679 51 - on OS/X, in $HOME/Library/Application Support/<appname>
Chris@679 52
Chris@679 53 - on Windows, in %ProgramFiles%/<company>/<appname>
Chris@680 54 - on Windows, in (where?) something from http://msdn.microsoft.com/en-us/library/dd378457%28v=vs.85%29.aspx ?
Chris@679 55
Chris@679 56 These locations are searched in reverse order (user-installed
Chris@679 57 copies take priority over system-installed copies take priority
Chris@679 58 over bundled copies). Also, /usr/local takes priority over /usr.
Chris@679 59 */
Chris@679 60
Chris@679 61 QStringList
Chris@679 62 ResourceFinder::getSystemResourcePrefixList()
Chris@679 63 {
Chris@679 64 // returned in order of priority
Chris@679 65
Chris@679 66 QStringList list;
Chris@679 67
Chris@679 68 #ifdef Q_OS_WIN32
Chris@679 69 char *programFiles = getenv("ProgramFiles");
Chris@679 70 if (programFiles && programFiles[0]) {
Chris@679 71 list << QString("%1/%2/%3")
Chris@679 72 .arg(programFiles)
Chris@679 73 .arg(qApp->organizationName())
Chris@679 74 .arg(qApp->applicationName());
Chris@679 75 } else {
Chris@679 76 list << QString("C:/Program Files/%1/%2")
Chris@679 77 .arg(qApp->organizationName())
Chris@679 78 .arg(qApp->applicationName());
Chris@679 79 }
Chris@679 80 #else
Chris@679 81 #ifdef Q_OS_MAC
Chris@733 82 list << QString("/Library/Application Support/%1")
Chris@679 83 .arg(qApp->applicationName());
Chris@679 84 #else
Chris@679 85 list << QString("/usr/local/share/%1")
Chris@679 86 .arg(qApp->applicationName());
Chris@679 87 list << QString("/usr/share/%1")
Chris@679 88 .arg(qApp->applicationName());
Chris@679 89 #endif
Chris@679 90 #endif
Chris@679 91
Chris@679 92 return list;
Chris@679 93 }
Chris@679 94
Chris@679 95 QString
Chris@679 96 ResourceFinder::getUserResourcePrefix()
Chris@679 97 {
Chris@982 98 #if QT_VERSION >= 0x050000
Chris@982 99 QString loc = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
Chris@982 100 return loc;
Chris@982 101 #else
Chris@680 102 #ifdef Q_OS_WIN32
Chris@982 103 // This does not work correctly for non-ASCII home directory names
Chris@680 104 char *homedrive = getenv("HOMEDRIVE");
Chris@680 105 char *homepath = getenv("HOMEPATH");
Chris@680 106 QString home;
Chris@680 107 if (homedrive && homepath) {
Chris@680 108 home = QString("%1%2").arg(homedrive).arg(homepath);
Chris@680 109 } else {
Chris@680 110 home = QDir::home().absolutePath();
Chris@680 111 }
Chris@680 112 if (home == "") return "";
Chris@708 113 return QString("%1/.%2").arg(home).arg(qApp->applicationName()); //!!! wrong
Chris@680 114 #else
Chris@679 115 char *home = getenv("HOME");
Chris@679 116 if (!home || !home[0]) return "";
Chris@679 117 #ifdef Q_OS_MAC
Chris@733 118 return QString("%1/Library/Application Support/%2")
Chris@679 119 .arg(home)
Chris@679 120 .arg(qApp->applicationName());
Chris@679 121 #else
Chris@679 122 return QString("%1/.local/share/%2")
Chris@679 123 .arg(home)
Chris@679 124 .arg(qApp->applicationName());
Chris@679 125 #endif
Chris@982 126 #endif
Chris@982 127 #endif
Chris@679 128 }
Chris@679 129
Chris@679 130 QStringList
Chris@679 131 ResourceFinder::getResourcePrefixList()
Chris@679 132 {
Chris@679 133 // returned in order of priority
Chris@679 134
Chris@679 135 QStringList list;
Chris@679 136
Chris@679 137 QString user = getUserResourcePrefix();
Chris@679 138 if (user != "") list << user;
Chris@679 139
Chris@679 140 list << getSystemResourcePrefixList();
Chris@679 141
Chris@679 142 list << ":"; // bundled resource location
Chris@679 143
Chris@679 144 return list;
Chris@679 145 }
Chris@679 146
Chris@679 147 QString
Chris@679 148 ResourceFinder::getResourcePath(QString resourceCat, QString fileName)
Chris@679 149 {
Chris@679 150 // We don't simply call getResourceDir here, because that returns
Chris@679 151 // only the "installed file" location. We also want to search the
Chris@679 152 // bundled resources and user-saved files.
Chris@679 153
Chris@679 154 QStringList prefixes = getResourcePrefixList();
Chris@679 155
Chris@679 156 if (resourceCat != "") resourceCat = "/" + resourceCat;
Chris@679 157
Chris@679 158 for (QStringList::const_iterator i = prefixes.begin();
Chris@679 159 i != prefixes.end(); ++i) {
Chris@679 160
Chris@679 161 QString prefix = *i;
Chris@679 162
Chris@690 163 SVDEBUG << "ResourceFinder::getResourcePath: Looking up file \"" << fileName << "\" for category \"" << resourceCat << "\" in prefix \"" << prefix << "\"" << endl;
Chris@679 164
Chris@679 165 QString path =
Chris@679 166 QString("%1%2/%3").arg(prefix).arg(resourceCat).arg(fileName);
Chris@679 167 if (QFileInfo(path).exists() && QFileInfo(path).isReadable()) {
Chris@843 168 cerr << "Found it!" << endl;
Chris@679 169 return path;
Chris@679 170 }
Chris@679 171 }
Chris@679 172
Chris@679 173 return "";
Chris@679 174 }
Chris@679 175
Chris@679 176 QString
Chris@679 177 ResourceFinder::getResourceDir(QString resourceCat)
Chris@679 178 {
Chris@679 179 // Returns only the "installed file" location
Chris@679 180
Chris@679 181 QStringList prefixes = getSystemResourcePrefixList();
Chris@679 182
Chris@679 183 if (resourceCat != "") resourceCat = "/" + resourceCat;
Chris@679 184
Chris@679 185 for (QStringList::const_iterator i = prefixes.begin();
Chris@679 186 i != prefixes.end(); ++i) {
Chris@679 187
Chris@679 188 QString prefix = *i;
Chris@679 189 QString path = QString("%1%2").arg(prefix).arg(resourceCat);
Chris@679 190 if (QFileInfo(path).exists() &&
Chris@679 191 QFileInfo(path).isDir() &&
Chris@679 192 QFileInfo(path).isReadable()) {
Chris@679 193 return path;
Chris@679 194 }
Chris@679 195 }
Chris@679 196
Chris@679 197 return "";
Chris@679 198 }
Chris@679 199
Chris@679 200 QString
Chris@679 201 ResourceFinder::getResourceSavePath(QString resourceCat, QString fileName)
Chris@679 202 {
Chris@679 203 QString dir = getResourceSaveDir(resourceCat);
Chris@679 204 if (dir == "") return "";
Chris@679 205
Chris@679 206 return dir + "/" + fileName;
Chris@679 207 }
Chris@679 208
Chris@679 209 QString
Chris@679 210 ResourceFinder::getResourceSaveDir(QString resourceCat)
Chris@679 211 {
Chris@679 212 // Returns the "user" location
Chris@679 213
Chris@679 214 QString user = getUserResourcePrefix();
Chris@679 215 if (user == "") return "";
Chris@679 216
Chris@679 217 if (resourceCat != "") resourceCat = "/" + resourceCat;
Chris@679 218
Chris@679 219 QDir userDir(user);
Chris@679 220 if (!userDir.exists()) {
Chris@679 221 if (!userDir.mkpath(user)) {
Chris@843 222 cerr << "ResourceFinder::getResourceSaveDir: ERROR: Failed to create user resource path \"" << user << "\"" << endl;
Chris@679 223 return "";
Chris@679 224 }
Chris@679 225 }
Chris@679 226
Chris@679 227 if (resourceCat != "") {
Chris@679 228 QString save = QString("%1%2").arg(user).arg(resourceCat);
Chris@679 229 QDir saveDir(save);
Chris@679 230 if (!saveDir.exists()) {
Chris@959 231 if (!saveDir.mkpath(save)) {
Chris@843 232 cerr << "ResourceFinder::getResourceSaveDir: ERROR: Failed to create user resource path \"" << save << "\"" << endl;
Chris@679 233 return "";
Chris@679 234 }
Chris@679 235 }
Chris@679 236 return save;
Chris@679 237 } else {
Chris@679 238 return user;
Chris@679 239 }
Chris@679 240 }
Chris@679 241
Chris@679 242 QStringList
Chris@679 243 ResourceFinder::getResourceFiles(QString resourceCat, QString fileExt)
Chris@679 244 {
Chris@679 245 QStringList results;
Chris@679 246 QStringList prefixes = getResourcePrefixList();
Chris@679 247
Chris@679 248 QStringList filters;
Chris@679 249 filters << QString("*.%1").arg(fileExt);
Chris@679 250
Chris@679 251 for (QStringList::const_iterator i = prefixes.begin();
Chris@679 252 i != prefixes.end(); ++i) {
Chris@679 253
Chris@679 254 QString prefix = *i;
Chris@679 255 QString path;
Chris@679 256
Chris@679 257 if (resourceCat != "") {
Chris@679 258 path = QString("%1/%2").arg(prefix).arg(resourceCat);
Chris@679 259 } else {
Chris@679 260 path = prefix;
Chris@679 261 }
Chris@679 262
Chris@679 263 QDir dir(path);
Chris@679 264 if (!dir.exists()) continue;
Chris@679 265
Chris@679 266 dir.setNameFilters(filters);
Chris@679 267 QStringList entries = dir.entryList
Chris@679 268 (QDir::Files | QDir::Readable, QDir::Name);
Chris@679 269
Chris@679 270 for (QStringList::const_iterator j = entries.begin();
Chris@679 271 j != entries.end(); ++j) {
Chris@679 272 results << QString("%1/%2").arg(path).arg(*j);
Chris@679 273 }
Chris@679 274 }
Chris@679 275
Chris@679 276 return results;
Chris@679 277 }
Chris@679 278
Chris@679 279 bool
Chris@679 280 ResourceFinder::unbundleResource(QString resourceCat, QString fileName)
Chris@679 281 {
Chris@679 282 QString path = getResourcePath(resourceCat, fileName);
Chris@679 283
Chris@679 284 if (!path.startsWith(':')) return true;
Chris@679 285
Chris@679 286 // This is the lowest-priority alternative path for this
Chris@679 287 // resource, so we know that there must be no installed copy.
Chris@679 288 // Install one to the user location.
Chris@690 289 SVDEBUG << "ResourceFinder::unbundleResource: File " << fileName << " is bundled, un-bundling it" << endl;
Chris@679 290 QString target = getResourceSavePath(resourceCat, fileName);
Chris@679 291 QFile file(path);
Chris@679 292 if (!file.copy(target)) {
Chris@843 293 cerr << "ResourceFinder::unbundleResource: ERROR: Failed to un-bundle resource file \"" << fileName << "\" to user location \"" << target << "\"" << endl;
Chris@679 294 return false;
Chris@679 295 }
Chris@679 296
Chris@679 297 QFile chmod(target);
Chris@679 298 chmod.setPermissions(QFile::ReadOwner |
Chris@679 299 QFile::ReadUser | /* for potential platform-independence */
Chris@679 300 QFile::ReadGroup |
Chris@679 301 QFile::ReadOther |
Chris@679 302 QFile::WriteOwner|
Chris@679 303 QFile::WriteUser); /* for potential platform-independence */
Chris@679 304
Chris@679 305 return true;
Chris@679 306 }
Chris@679 307