-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehandler.cpp
197 lines (167 loc) · 4.88 KB
/
filehandler.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "FileHandler.h"
#include "configsaver.h"
void FileHandler::switchModel(int index)
{
if (index < 0 || index > LAppLive2DManager::GetInstance()->GetModelNum())
{
LAppPal::PrintLogLn("Invalid index");
}
LAppLive2DManager::GetInstance()->ChangeScene(index);
}
void FileHandler::switchModel()
{
LAppLive2DManager::GetInstance()->NextScene();
}
int FileHandler::getModelNum()
{
return LAppLive2DManager::GetInstance()->GetModelDirSize();
}
void FileHandler::addModel(QString path)
{
LAppLive2DManager::GetInstance()->AddModel(path.toLocal8Bit().data());
}
QStringList &FileHandler::getModelDirList()
{
return toQStringList(LAppLive2DManager::GetInstance()->GetModelDir());
}
void FileHandler::saveModelPath(QString &path)
{
ConfigSaver::saveModelPath(path);
}
QStringList *FileHandler::getModelPath()
{
QStringList *ret = ConfigSaver::getModelPath();
if(ret == NULL)
{
return new QStringList();
}
return ret;
}
//拷贝文件:
bool FileHandler::copyFileToPath(QString sourceDir ,QString toDir, bool coverFileIfExist)
{
toDir.replace("\\","/");
if (sourceDir == toDir){
return true;
}
if (!QFile::exists(sourceDir)){
return false;
}
QDir *createfile = new QDir;
bool exist = createfile->exists(toDir);
if (exist){
if(coverFileIfExist){
createfile->remove(toDir);
}
}//end if
if(!QFile::copy(sourceDir, toDir))
{
return false;
}
return true;
}
//拷贝文件夹:
bool FileHandler::copyDirectoryFiles(const QString &fromDir, const QString &toDir, bool coverFileIfExist)
{
QDir sourceDir(fromDir);
QDir targetDir(toDir);
if(!targetDir.exists()){ /**< 如果目标目录不存在,则进行创建 */
if(!targetDir.mkdir(targetDir.absolutePath()))
return false;
}
QFileInfoList fileInfoList = sourceDir.entryInfoList();
foreach(QFileInfo fileInfo, fileInfoList){
if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
continue;
if(fileInfo.isDir()){ /**< 当为目录时,递归的进行copy */
if(!copyDirectoryFiles(fileInfo.filePath(),
targetDir.filePath(fileInfo.fileName()),
coverFileIfExist))
return false;
}
else{ /**< 当允许覆盖操作时,将旧文件进行删除操作 */
if(coverFileIfExist && targetDir.exists(fileInfo.fileName())){
targetDir.remove(fileInfo.fileName());
}
/// 进行文件copy
if(!QFile::copy(fileInfo.filePath(),
targetDir.filePath(fileInfo.fileName()))){
return false;
}
}
}
return true;
}
// 删除文件夹
bool FileHandler::deleteFolder(const QString &folderPath)
{
if(folderPath.isEmpty() || !QDir().exists(folderPath))
{
qDebug() << QT_BACKGROUND_LOG << "delete folder: invalid path";
return false;
}
QFileInfo info(folderPath);
if(info.isFile())
{
qDebug() << QT_BACKGROUND_LOG << "delete folder: dir isn't a folder";
return false;
}
if(info.isDir())
{
QDir dir(folderPath);
dir.removeRecursively();
qDebug() << QT_BACKGROUND_LOG << "delete folder: deleted dir";
return true;
}
qDebug() << QT_BACKGROUND_LOG << "delete folder: unknown fault";
return false;
}
void FileHandler::resetModel()
{
LAppLive2DManager::GetInstance()->SetUpModel();
}
void FileHandler::saveVoiceToModel(int voiceIndex)
{
LAppLive2DManager::GetInstance()->saveCurrentModleVoiceIndex(voiceIndex);
}
int FileHandler::getModelVoiceIndex()
{
LAppModel* model = LAppLive2DManager::GetInstance()->GetModel(0);
if(model != NULL)
{
int index = model->getVoice();
qDebug() << QT_DEBUG_OUTPUT << "get voice from model. index: " << index;
return index;
}
return -1;
}
// 一些类型转换函数
// 主要方便这个文件里一些函数
// 用不用都行, 设成 public 了
//
Csm::csmString &FileHandler::toCsmString(const QString &target)
{
return *new Csm::csmString(target.toStdString().c_str());
}
Csm::csmVector<Csm::csmString> &FileHandler::toCsmStringList(const QStringList &target)
{
Csm::csmVector<Csm::csmString> *retVector = new Csm::csmVector<Csm::csmString>();
for(auto x:target)
{
retVector->PushBack(toCsmString(x));
}
return *retVector;
}
QString &FileHandler::toQString(const Csm::csmString &target)
{
return *new QString(target.GetRawString());
}
QStringList &FileHandler::toQStringList(const Csm::csmVector<Csm::csmString> &target)
{
QStringList *retList = new QStringList();
for(int i = 0; i < target.GetSize(); i++)
{
retList->push_back(toQString(target[i]));
}
return *retList;
}