comparison base/ResourceFinder.cpp @ 983:a8f91db36e9d

If the old and new-style user resource prefixes return different results, move across resources from old to new to prime the new path first time we look it up
author Chris Cannam
date Wed, 10 Sep 2014 09:40:45 +0100
parents f161ca450050
children 8f076d02569a
comparison
equal deleted inserted replaced
982:f161ca450050 983:a8f91db36e9d
90 #endif 90 #endif
91 91
92 return list; 92 return list;
93 } 93 }
94 94
95 QString 95 static QString
96 ResourceFinder::getUserResourcePrefix() 96 getOldStyleUserResourcePrefix()
97 { 97 {
98 #if QT_VERSION >= 0x050000
99 QString loc = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
100 return loc;
101 #else
102 #ifdef Q_OS_WIN32 98 #ifdef Q_OS_WIN32
103 // This does not work correctly for non-ASCII home directory names 99 // This is awkward and does not work correctly for non-ASCII home
100 // directory names, hence getNewStyleUserResourcePrefix() below
104 char *homedrive = getenv("HOMEDRIVE"); 101 char *homedrive = getenv("HOMEDRIVE");
105 char *homepath = getenv("HOMEPATH"); 102 char *homepath = getenv("HOMEPATH");
106 QString home; 103 QString home;
107 if (homedrive && homepath) { 104 if (homedrive && homepath) {
108 home = QString("%1%2").arg(homedrive).arg(homepath); 105 home = QString("%1%2").arg(homedrive).arg(homepath);
122 return QString("%1/.local/share/%2") 119 return QString("%1/.local/share/%2")
123 .arg(home) 120 .arg(home)
124 .arg(qApp->applicationName()); 121 .arg(qApp->applicationName());
125 #endif 122 #endif
126 #endif 123 #endif
127 #endif 124 }
125
126 static QString
127 getNewStyleUserResourcePrefix()
128 {
129 #if QT_VERSION >= 0x050000
130 // This is expected to be much more reliable than
131 // getOldStyleUserResourcePrefix(), but it returns a different
132 // directory because it includes the organisation name (which is
133 // fair enough). Hence migrateOldStyleResources() which moves
134 // across any resources found in the old-style path the first time
135 // we look for the new-style one
136 return QStandardPaths::writableLocation(QStandardPaths::DataLocation);
137 #else
138 return getOldStyleUserResourcePrefix();
139 #endif
140 }
141
142 static void
143 migrateOldStyleResources()
144 {
145 QString oldPath = getOldStyleUserResourcePrefix();
146 QString newPath = getNewStyleUserResourcePrefix();
147
148 if (oldPath != newPath &&
149 QDir(oldPath).exists() &&
150 !QDir(newPath).exists()) {
151
152 QDir d(oldPath);
153
154 if (!d.mkpath(newPath)) {
155 cerr << "WARNING: Failed to create new-style resource path \""
156 << newPath << "\" to migrate old resources to" << endl;
157 return;
158 }
159
160 QDir target(newPath);
161
162 bool success = true;
163
164 QStringList entries
165 (d.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot));
166
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;
171 } else {
172 cerr << "WARNING: Failed to move old resource \""
173 << entry << "\" from old location \""
174 << oldPath << "\" to new location \""
175 << newPath << "\"" << endl;
176 success = false;
177 }
178 }
179
180 if (success) {
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;
186 } else {
187 cerr << "NOTE: Successfully moved " << entries.size()
188 << " resource(s) from old resource "
189 << "path \"" << oldPath << "\" to new path \""
190 << newPath << "\"" << endl;
191 }
192 }
193 }
194 }
195
196 QString
197 ResourceFinder::getUserResourcePrefix()
198 {
199 migrateOldStyleResources();
200 return getNewStyleUserResourcePrefix();
128 } 201 }
129 202
130 QStringList 203 QStringList
131 ResourceFinder::getResourcePrefixList() 204 ResourceFinder::getResourcePrefixList()
132 { 205 {