-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitManager.cpp
More file actions
95 lines (77 loc) · 2.52 KB
/
initManager.cpp
File metadata and controls
95 lines (77 loc) · 2.52 KB
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
//initManager.cpp
#include "initManager.h"
#include <QStandardPaths>
#include <QDir>
#include <QApplication>
//declare static member variables outside of class to allocate memory correctly
QString CInitManager::m_dataPath = "";
QString CInitManager::m_scriptsPath = "";
CInitManager::CInitManager()
{
}
CInitManager::~CInitManager()
{
}
bool CInitManager::resolvePaths()
{
m_dataPath = getSubfolderPath( "data" );
m_scriptsPath = getSubfolderPath( "scripts" );
if (m_dataPath.isEmpty() || m_scriptsPath.isEmpty())
{
return false;
}
return true;
}
QString CInitManager::getSubfolderPath(const QString& subfolderName)
{
//Get location of application executable
QDir baseDir(QCoreApplication::applicationDirPath());
//See if the subfolder exists off the absolute path
if (baseDir.exists(subfolderName))
{
return baseDir.absoluteFilePath(subfolderName) + "/";
}
//Move up two folders and try again
if (baseDir.cdUp() && baseDir.cdUp())
{
if (baseDir.exists(subfolderName))
{
return baseDir.absoluteFilePath(subfolderName) + "/";
}
}
//Not found so return empty string
return QString();
}
bool CInitManager::copyDatabaseToAppLocal(QString& filename)
{
//Identify and create the appdata local folder for AtMap to hold the version of the database for the current user
QString localPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
QDir localDir(localPath);
// Create local path in case it did not exist
QDir().mkpath(localPath);
//Only proceed if the database is not already in appdata local
QString nameDb = "land.db";
QFile qf( localPath + "/" + nameDb);
filename = qf.fileName();
if (qf.exists() )
return true;
//Get the database file
QFile sourceFile( DATA_PATH + "land.db" );
//Check it exists
if (!sourceFile.exists() )
{
filename = "";
return false;
}
//Do the copy from source to app data local
if (!sourceFile.copy(qf.fileName() ))
{
qCritical() << "File Error: Found source database but copy to appdata/local failed." << sourceFile.errorString();
filename = "";
return false;
}
// Ensure the user has full permissions to write to their local copy
qf.setPermissions(QFile::WriteOwner | QFile::ReadOwner | QFile::WriteUser | QFile::ReadUser);
qDebug() << "Success: Database copied from" << sourceFile.fileName() << "to" << filename;
return true;
}