summaryrefslogtreecommitdiff
path: root/map65/runtime_paths.cpp
blob: eabf229620c04ef4017240d40c91d9a5757cf8a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "runtime_paths.h"

#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
#include <QDebug>
#include <QStringList>
#include <QSettings>
#include <QVariant>

namespace
{
  QStringList runtimeSourceCandidates(QString const& appDir, QString const& installedDataDir,
                                      QString const& fileName)
  {
    QDir app {appDir};
    QDir cwd {QDir::currentPath()};

    return {
      QDir {installedDataDir}.absoluteFilePath(fileName),
      app.absoluteFilePath(fileName),
      app.absoluteFilePath("resources/" + fileName),
      app.absoluteFilePath("../" + fileName),
      app.absoluteFilePath("../map65/resources/" + fileName),
      cwd.absoluteFilePath(fileName),
      cwd.absoluteFilePath("map65/resources/" + fileName)
    };
  }
}

QString writableMap65DataDir()
{
  QString dataDir = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
  if (dataDir.isEmpty()) {
    dataDir = QDir::home().absoluteFilePath(".map65");
  }

  if (!QDir{}.mkpath(dataDir)) {
    qWarning() << "Unable to create MAP65 data directory:" << dataDir;
  }

  QDir dir {dataDir};
  if (!dir.mkpath("save")) {
    qWarning() << "Unable to create MAP65 save directory:" << dir.absoluteFilePath("save");
  }
  return dataDir;
}

QString map65SettingsFile(QString const& appDir, QString const& dataDir)
{
  QString settingsFile = QDir {dataDir}.absoluteFilePath("map65.ini");
  QString legacySettingsFile = QDir {appDir}.absoluteFilePath("map65.ini");
  if (!QFile::exists(settingsFile) && QFile::exists(legacySettingsFile)) {
    if (QFile::copy(legacySettingsFile, settingsFile)) {
      QFile::setPermissions(settingsFile, QFile::ReadOwner | QFile::WriteOwner
                            | QFile::ReadGroup | QFile::ReadOther);
    } else {
      qWarning() << "Unable to migrate MAP65 settings from" << legacySettingsFile
                 << "to" << settingsFile;
    }
  }
  return settingsFile;
}

int readFSam96000(QSettings const& settings, int defaultValue)
{
  QString text = settings.value("FSam96000", QString::number(defaultValue)).toString();
  if (text.compare("true", Qt::CaseInsensitive) == 0) return 1;    // legacy bool: true  == 96000
  if (text.compare("false", Qt::CaseInsensitive) == 0) return 0;   // legacy bool: false == 95238
  bool ok = false;
  int value = text.toInt(&ok);
  if (ok && value >= 0 && value <= 2) return value;                // current int: 0/1/2
  return defaultValue;
}

QString map65RuntimeFile(QString const& dataDir, QString const& fileName)
{
  return QDir {dataDir}.absoluteFilePath(fileName);
}

QString map65RuntimeSourceFile(QString const& appDir, QString const& installedDataDir,
                               QString const& writableDataDir, QString const& fileName)
{
  QString const writablePath = map65RuntimeFile(writableDataDir, fileName);
  QString const writableCanonicalPath = QFileInfo {writablePath}.canonicalFilePath();
  auto const candidates = runtimeSourceCandidates(appDir, installedDataDir, fileName);
  for (auto const& sourcePath : candidates) {
    QFileInfo sourceInfo {sourcePath};
    if (!sourceInfo.exists() || !sourceInfo.isFile() || sourceInfo.size() == 0) {
      continue;
    }
    if (sourceInfo.absoluteFilePath() == writablePath
        || (!writableCanonicalPath.isEmpty()
            && sourceInfo.canonicalFilePath() == writableCanonicalPath)) {
      continue;
    }
    return sourceInfo.absoluteFilePath();
  }

  return candidates.front();
}

QString ensureMap65RuntimeFile(QString const& appDir, QString const& installedDataDir,
                               QString const& writableDataDir, QString const& fileName)
{
  QString const writablePath = map65RuntimeFile(writableDataDir, fileName);
  QFileInfo writableInfo {writablePath};
  if (writableInfo.exists() && writableInfo.size() > 0) {
    return writablePath;
  }

  QString const sourcePath = map65RuntimeSourceFile(appDir, installedDataDir,
                                                    writableDataDir, fileName);
  QFileInfo sourceInfo {sourcePath};
  if (sourceInfo.exists() && sourceInfo.isFile() && sourceInfo.size() > 0) {
    if (writableInfo.exists()) {
      QFile::remove(writablePath);
    }

    if (QFile::copy(sourceInfo.absoluteFilePath(), writablePath)) {
      QFile::setPermissions(writablePath, QFile::ReadOwner | QFile::WriteOwner
                            | QFile::ReadGroup | QFile::ReadOther);
      return writablePath;
    }

    qWarning() << "Unable to migrate MAP65 runtime file from" << sourcePath
               << "to" << writablePath;
  }

  return writablePath;
}