[go: up one dir, main page]

Menu

[r89]: / tags / 0.4.0 / Utils.cpp  Maximize  Restore  History

Download this file

312 lines (253 with data), 9.8 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
 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Midi Layer
Copyright (C) 2012-2013, António José Soares Maia <maia.ajs@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; If not, see <http://www.gnu.org/licenses/>.
*/
#include "Utils.h"
//QAtomicPointer<Utils> Utils::m_Utils;
Utils * Utils::m_Utils;
QMutex Utils::mutex;
Utils* Utils::get() {
if( !m_Utils )
{
mutex.lock();
if( !m_Utils )
m_Utils = new Utils;
mutex.unlock();
}
return m_Utils;
}
void Utils::Drop() {
mutex.lock();
delete m_Utils;
m_Utils = 0;
mutex.unlock();
}
void Utils::setMainWindowReady(bool ready) {
this->mainWindowReady = ready;
}
bool Utils::isMainWindowReady() {
return this->mainWindowReady;
}
QString Utils::getCloneName(QString name, QStringList existingNames) {
if (existingNames.indexOf(name,0)==-1)
return name;
QString radical = "";
// Verify if 'name' is a clone
QRegExp regexp("(.*)\\s\\((\\d+)\\)");
regexp.indexIn(name);
QStringList captureList = regexp.capturedTexts();
int maxIndex=1;
if (captureList.at(0).length()>0) {
radical = captureList.at(1);
maxIndex = captureList.at(2).toInt(0,10);
if (maxIndex<1)
maxIndex=1;
} else {
radical=name;
}
while (existingNames.contains( QString("%1 (%2)").arg(radical).arg(++maxIndex)));
if (ISLOGGABLE_TRACE) LOG_TRACE(QString ("getCloneName result: %1 (%2)").arg(radical).arg(maxIndex));
return QString ("%1 (%2)").arg(radical).arg(maxIndex);
}
bool Utils::copyDir(const QString source, const QString destination, const bool override) {
QDir directory(source);
bool error = false;
if (!directory.exists()) {
if (ISLOGGABLE_INFO) LOG_ERROR("copyDir: source directory ("+ source +") does not exist");
return false;
}
QStringList dirs = directory.entryList(QDir::AllDirs | QDir::Hidden | QDir::NoDotAndDotDot);
QStringList files = directory.entryList(QDir::Files | QDir::Hidden);
QList<QString>::iterator d,f;
QDir temp(destination);
temp.mkpath(temp.path());
for (d = dirs.begin(); d != dirs.end(); ++d) {
if ((*d) == "." || (*d) == "..") {
continue;
}
if (!QFileInfo(directory.path() + "/" + (*d)).isDir()) {
continue;
}
QDir temp(destination + "/" + (*d));
temp.mkpath(temp.path());
if (!copyDir(directory.path() + "/" + (*d), destination + "/" + (*d), override)) {
error = true;
}
}
for (f = files.begin(); f != files.end(); ++f) {
QFile tempFile(directory.path() + "/" + (*f));
if (QFileInfo(directory.path() + "/" + (*f)).isDir()) {
continue;
}
QFile destFile(destination + "/" + directory.relativeFilePath(tempFile.fileName()));
if (destFile.exists() && override) {
destFile.remove();
}
if (!tempFile.copy(destination + "/" + directory.relativeFilePath(tempFile.fileName()))) {
if (ISLOGGABLE_ERROR) LOG_ERROR("copyDir: could not copy file "+ tempFile.fileName() + " to " +destination + "/" + directory.relativeFilePath(tempFile.fileName()));
error = true;
}
}
return !error;
}
/*!
Delete a directory along with all of its contents.
\param dirName Path of directory to remove.
\return true on success; false on error.
*/
bool Utils::removeDir(const QString &dirName)
{
bool result = true;
QDir dir(dirName);
if (dir.exists(dirName)) {
Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
if (info.isDir()) {
result = removeDir(info.absoluteFilePath());
}
else {
result = QFile::remove(info.absoluteFilePath());
}
if (!result) {
return result;
}
}
result = dir.rmdir(dirName);
}
return result;
}
QStringList Utils::getBanksOnDir(QString baseDir) {
QStringList dirList;
QDir banksDir = QDir( baseDir );
QStringList bankDirNameFilter;
bankDirNameFilter.append( QString("*") );
QStringList dirsOnFolder = banksDir.entryList(bankDirNameFilter, QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable );
// Find a dirs with bank file definitions
for (int i=0; i<dirsOnFolder.count(); i++) {
QString completeBankFileName = baseDir + "/" + dirsOnFolder.at(i) + "/" + dirsOnFolder.at(i) + APP_DATA_BANK_EXTENSION;
QFile file( completeBankFileName );
if ( file.exists() ) {
dirList << dirsOnFolder.at(i);
}
}
return dirList;
}
QStringList Utils::searchBanksOnDir(QString baseDir) {
QStringList dirList;
// Scan available preset files
QDir banksDir = QDir( baseDir );
QStringList bankDirNameFilter;
bankDirNameFilter.append( QString("*") );
QStringList dirsOnFolder = banksDir.entryList(bankDirNameFilter, QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable );
// Find a dirs with bank file definitions
for (int i=0; i<dirsOnFolder.count(); i++) {
bool canAddBank = false;
QString completeBankFileName = baseDir + "/" + dirsOnFolder.at(i) + "/" + dirsOnFolder.at(i) + APP_DATA_BANK_EXTENSION;
QFile file( completeBankFileName );
if ( file.exists() ) {
canAddBank = true;
}
else {
QDir presetsDir = QDir( baseDir + "/" + dirsOnFolder.at(i) );
QStringList presetsDirNameFilter;
presetsDirNameFilter.append( QString("*") );
QStringList presetsOnFolder = presetsDir.entryList(presetsDirNameFilter, QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable );
for (int j=0; j<presetsOnFolder.count(); j++) {
QString completePresetFileName = baseDir + "/" + dirsOnFolder.at(i) + "/" + presetsOnFolder.at(j)+ "/" + presetsOnFolder.at(j) + APP_DATA_PRESET_EXTENSION;
QFile file( completePresetFileName );
if ( file.exists() ) {
canAddBank = true;
break;
} else {
QDir elementsDir = QDir( baseDir + "/" + dirsOnFolder.at(i) + "/" + presetsOnFolder.at(j) );
QStringList elementsDirNameFilter;
elementsDirNameFilter.append( QString("*%1").arg(APP_DATA_ELEMENT_EXTENSION) );
QStringList elementsOnFolder = elementsDir.entryList(elementsDirNameFilter, QDir::Files | QDir::NoDotAndDotDot | QDir::Readable );
if (elementsOnFolder.count()>0) {
canAddBank = true;
}
}
}
}
if (canAddBank) {
dirList << dirsOnFolder.at(i);
}
}
return dirList;
}
void Utils::setPortableMode(bool portableMode) {
this->portableMode = portableMode;
}
bool Utils::isPortableMode(void) {
return this->portableMode;
}
void Utils::setDataDir(QString dataDir) {
this->dataDir = dataDir;
}
QString Utils::getDataDir(void) {
return this->dataDir;
}
void Utils::setPreferencesDir(QString preferencesDir) {
this->preferencesDir = preferencesDir;
}
QString Utils::getPreferencesDir(void) {
return this->preferencesDir;
}
void Utils::configDisplayFont(QFont * font) {
font->setFamily(QString::fromUtf8("LCDDotPT"));
font->setPointSize(12);
font->setBold(false);
font->setWeight(50);
font->setKerning(true);
font->setStyleStrategy(QFont::PreferDefault);
}
void Utils::configBigSpinValueFont(QFont * font) {
font->setFamily(QString::fromUtf8("LED Digital 7"));
font->setPointSize(18);
font->setBold(false);
font->setWeight(50);
font->setKerning(true);
font->setStyleStrategy(QFont::PreferDefault);
}
void Utils::configSpinValueFont(QFont * font) {
font->setFamily(QString::fromUtf8("LED Digital 7"));
font->setPointSize(14);
font->setBold(false);
font->setWeight(50);
font->setKerning(true);
font->setStyleStrategy(QFont::PreferDefault);
}
QRegExp Utils::getNameRegexValidator() {
QRegExp nameValidator("^(([a-zA-Z]:|\\\\)\\\\)?(((\\.)|(\\.\\.)|([^\\\\/:\\*\\?""\\|<>\\. ](([^\\\\/:\\*\\?""\\|<>\\. ])|([^\\\\/:\\*\\?""\\|<>]*[^\\\\/:\\*\\?""\\|<>\\. ]))?))\\\\)*[^\\\\/:\\*\\?""\\|<>\\. ](([^\\\\/:\\*\\?""\\|<>\\. ])|([^\\\\/:\\*\\?""\\|<>]*[^\\\\/:\\*\\?""\\|<>\\. ]))?$");
return nameValidator;
}
bool Utils::renameFile(QString oldName, QString newName) {
if ( !QFile::rename(oldName, newName) ) {
if (ISLOGGABLE_ERROR) LOG_ERROR("Can not rename file '"+ oldName +"' to '"+ newName+ "'");
return false;
}
return true;
}
#ifdef QT_DEBUG
QString Utils::toString(QStringList stringList) {
QString string = QString("");
bool first=true;
for (int i=0; i<stringList.count(); i++) {
if (first)
first=false;
else
string.append(", ");
string.append(stringList.at(i));
}
return "["+string+"]";
}
#endif