@@ -40,7 +40,7 @@ void PrintOptionHelp() {
40
40
" \n "
41
41
" Options:\n "
42
42
" --help Show this help message.\n "
43
- " --logfile Write log to ~/. ssr/log-DATE_TIME.txt instead of stdout.\n "
43
+ " --logfile Write log to $XDG_DATA_HOME/ ssr/log-DATE_TIME.txt instead of stdout.\n "
44
44
" --statsfile[=FILE] Write recording statistics to FILE. If FILE is omitted,\n "
45
45
" /dev/shm/simplescreenrecorder-stats-PID is used. It will\n "
46
46
" be updated continuously and deleted when the recording\n "
@@ -164,12 +164,52 @@ int main(int argc, char* argv[]) {
164
164
}
165
165
}
166
166
167
+ // migrate data from ~/.ssr to follow XDG directory spec
168
+ {
169
+ QString oldDir (QDir::homePath () + " /.ssr" );
170
+ if (QDir (oldDir).exists ()) {
171
+ QDir r;
172
+ // migrate config files
173
+ int num_conf_files = 3 ;
174
+ const char * conf_files[num_conf_files] {
175
+ " input-profiles" , " output-profiles" , " settings.conf"
176
+ };
177
+ for (int i = 0 ; i < num_conf_files; i++) {
178
+ QString oldFile (oldDir + " /" + conf_files[i]);
179
+ QString newFile (GetApplicationConfigDir () + " /" + conf_files[i]);
180
+ if (QFile (newFile).exists ()) {
181
+ continue ;
182
+ }
183
+ if (!r.rename (oldFile, newFile)) {
184
+ Logger::LogError (" [main] " + Logger::tr (" Error: Couldn't migrate config files from ~/.ssr to new XDG-compliant location!" ));
185
+ throw 0 ;
186
+ }
187
+ }
188
+ // migrate the rest of the files to data directory
189
+ QDir qOldDir (oldDir);
190
+ for (QFileInfo fi : qOldDir.entryInfoList ()) {
191
+ QString newFile (GetApplicationDataDir () + " /" + fi.fileName ());
192
+ if (QFile (newFile).exists ()) {
193
+ continue ;
194
+ }
195
+ if (!r.rename (fi.filePath (), newFile)) {
196
+ Logger::LogError (" [main] " + Logger::tr (" Error: Couldn't migrate data files from ~/.ssr to new XDG-compliant location!" ));
197
+ throw 0 ;
198
+ }
199
+ }
200
+ if (system (qPrintable (" rm -rf ~/.ssr" )) != 0 ) { // TODO: If SSR ever switches to Qt5, this must be replaced by QDir::removeRecursively()
201
+ Logger::LogError (" [main] " + Logger::tr (" Error: Couldn't delete ~/.ssr directory after data migration attempt!" ));
202
+ throw 0 ;
203
+ }
204
+ }
205
+ }
206
+
167
207
// redirect stdout and stderr to a log file
168
208
if (g_option_logfile) {
169
209
170
210
// delete logs from versions < 0.2.3 (should be removed at some point in the future)
171
211
{
172
- QDir dir (GetApplicationUserDir ());
212
+ QDir dir (GetApplicationDataDir ());
173
213
dir.setFilter (QDir::Files | QDir::NoDotAndDotDot);
174
214
dir.setNameFilters (QStringList (" log-*.txt" ));
175
215
for (QFileInfo fileinfo : dir.entryInfoList ()) {
@@ -179,7 +219,7 @@ int main(int argc, char* argv[]) {
179
219
180
220
// delete old logs
181
221
QDateTime now = QDateTime::currentDateTime ();
182
- QDir dir (GetApplicationUserDir (" logs" ));
222
+ QDir dir (GetApplicationDataDir (" logs" ));
183
223
dir.setFilter (QDir::Files | QDir::NoDotAndDotDot);
184
224
dir.setNameFilters (QStringList (" log-*.txt" ));
185
225
for (QFileInfo fileinfo : dir.entryInfoList ()) {
@@ -237,12 +277,26 @@ QString GetApplicationSystemDir(const QString& subdir) {
237
277
return dir;
238
278
}
239
279
240
- QString GetApplicationUserDir (const QString& subdir) {
241
- QString dir = QDir::homePath () + " /.ssr" ;
280
+ QString GetApplicationDataDir (const QString& subdir) {
281
+ return GetAppXDGDir (subdir, " XDG_DATA_HOME" , " /.local/share" );
282
+ }
283
+
284
+ QString GetApplicationConfigDir (const QString& subdir) {
285
+ return GetAppXDGDir (subdir, " XDG_CONFIG_HOME" , " /.config" );
286
+ }
287
+
288
+ QString GetAppXDGDir (const QString& subdir, const char * xdg_env_name, const char * default_xdg_home_subdir) {
289
+ QString dir;
290
+ QByteArray env = qgetenv (xdg_env_name);
291
+ if (env.count () == 0 || !QDir (QString (env)).exists ()) {
292
+ dir = QDir::homePath () + default_xdg_home_subdir + " /ssr" ;
293
+ } else {
294
+ dir = QString (env) + " /ssr" ;
295
+ }
242
296
if (!subdir.isEmpty ())
243
297
dir += " /" + subdir;
244
298
if (!QDir::root ().mkpath (dir)) {
245
- Logger::LogError (" [GetApplicationUserDir ] " + Logger::tr (" Error: Can't create .ssr directory!" ));
299
+ Logger::LogError (" [GetAppXDGDir ] " + Logger::tr (" Error: Can't create '%1' directory!" ). arg (dir ));
246
300
throw 0 ;
247
301
}
248
302
return dir;
0 commit comments