mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 00:16:51 +08:00
Ref T286, utility functions for copy caches/settings
This commit is contained in:
106
src/blackmisc/cachesettingsutils.cpp
Normal file
106
src/blackmisc/cachesettingsutils.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/* Copyright (C) 2018
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "cachesettingsutils.h"
|
||||
#include "settingscache.h"
|
||||
#include "datacache.h"
|
||||
#include <QRegularExpression>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
bool CCacheSettingsUtils::isSetting(const QString &fileName)
|
||||
{
|
||||
if (!fileName.contains(CSettingsCache::relativeFilePath())) { return false; }
|
||||
return fileName.contains(binSettings(), Qt::CaseInsensitive);
|
||||
}
|
||||
|
||||
bool CCacheSettingsUtils::isCache(const QString &fileName)
|
||||
{
|
||||
if (!fileName.contains(CDataCache::relativeFilePath())) { return false; }
|
||||
return fileName.contains(binData(), Qt::CaseInsensitive);
|
||||
}
|
||||
|
||||
const QString &CCacheSettingsUtils::binSettings()
|
||||
{
|
||||
static const QString s("bin/settings/");
|
||||
return s;
|
||||
}
|
||||
|
||||
const QString &CCacheSettingsUtils::binData()
|
||||
{
|
||||
static const QString s("bin/data/");
|
||||
return s;
|
||||
}
|
||||
|
||||
QString CCacheSettingsUtils::relativeSettingsPath(const QString &fileName)
|
||||
{
|
||||
if (!isSetting(fileName)) { return QString(); }
|
||||
const int index = fileName.lastIndexOf(binSettings());
|
||||
if (index < 0) { return QString(); }
|
||||
return fileName.mid(index);
|
||||
}
|
||||
|
||||
QString CCacheSettingsUtils::relativeCachePath(const QString &fileName)
|
||||
{
|
||||
if (!isCache(fileName)) { return QString(); }
|
||||
const int index = fileName.lastIndexOf(binData());
|
||||
if (index < 0) { return QString(); }
|
||||
return fileName.mid(index);
|
||||
}
|
||||
|
||||
QString CCacheSettingsUtils::otherVersionSettingsFileName(const CApplicationInfo &info, const QString &mySettingFile)
|
||||
{
|
||||
const QString relativeMySetting = relativeSettingsPath(mySettingFile);
|
||||
return otherVersionFileName(info, relativeMySetting);
|
||||
}
|
||||
|
||||
QString CCacheSettingsUtils::otherVersionCacheFileName(const CApplicationInfo &info, const QString &myCacheFile)
|
||||
{
|
||||
const QString relativeMyCache = relativeCachePath(myCacheFile);
|
||||
return otherVersionFileName(info, relativeMyCache);
|
||||
}
|
||||
|
||||
bool CCacheSettingsUtils::hasOtherVersionSettingsFile(const CApplicationInfo &info, const QString &mySettingFile)
|
||||
{
|
||||
return !otherVersionSettingsFileName(info, mySettingFile).isEmpty();
|
||||
}
|
||||
|
||||
bool CCacheSettingsUtils::hasOtherVersionCacheFile(const CApplicationInfo &info, const QString &myCacheFile)
|
||||
{
|
||||
return !otherVersionCacheFileName(info, myCacheFile).isEmpty();
|
||||
}
|
||||
|
||||
QString CCacheSettingsUtils::otherVersionFileName(const CApplicationInfo &info, const QString &relativeFileName)
|
||||
{
|
||||
thread_local const QRegularExpression re("bin$");
|
||||
if (relativeFileName.isEmpty()) { return ""; }
|
||||
QString otherFile = info.getApplicationDataDirectory();
|
||||
otherFile.replace(re, relativeFileName);
|
||||
const QFileInfo fi(otherFile);
|
||||
if (!fi.isFile()) { return ""; }
|
||||
if (fi.exists()) { return fi.absoluteFilePath(); }
|
||||
return "";
|
||||
}
|
||||
|
||||
QString CCacheSettingsUtils::otherVersionSettingsFileContent(const CApplicationInfo &info, const QString &mySettingFile)
|
||||
{
|
||||
const QString file = otherVersionSettingsFileName(info, mySettingFile);
|
||||
if (file.isEmpty()) { return ""; }
|
||||
const QString jsonStr = CFileUtils::readFileToString(file);
|
||||
return jsonStr;
|
||||
}
|
||||
|
||||
QString CCacheSettingsUtils::otherVersionCacheFileContent(const CApplicationInfo &info, const QString &myCacheFile)
|
||||
{
|
||||
const QString file = otherVersionCacheFileName(info, myCacheFile);
|
||||
if (file.isEmpty()) { return ""; }
|
||||
const QString jsonStr = CFileUtils::readFileToString(file);
|
||||
return jsonStr;
|
||||
}
|
||||
} // namespace
|
||||
66
src/blackmisc/cachesettingsutils.h
Normal file
66
src/blackmisc/cachesettingsutils.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/* Copyright (C) 2018
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKMISC_CACHESETTINGSUTILS_H
|
||||
#define BLACKMISC_CACHESETTINGSUTILS_H
|
||||
|
||||
#include "blackmisc/applicationinfo.h"
|
||||
#include "blackmiscexport.h"
|
||||
#include <QString>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
//! Utils for copying cache and settings content
|
||||
class BLACKMISC_EXPORT CCacheSettingsUtils
|
||||
{
|
||||
public:
|
||||
//! Setting?
|
||||
static bool isSetting(const QString &fileName);
|
||||
|
||||
//! Cache?
|
||||
static bool isCache(const QString &fileName);
|
||||
|
||||
//! Path
|
||||
static const QString &binSettings();
|
||||
|
||||
//! Path
|
||||
static const QString &binData();
|
||||
|
||||
//! Relative setting path
|
||||
static QString relativeSettingsPath(const QString &fileName);
|
||||
|
||||
//! Relative cache path
|
||||
static QString relativeCachePath(const QString &fileName);
|
||||
|
||||
//! File name for cache/setting
|
||||
static QString otherVersionFileName(const BlackMisc::CApplicationInfo &info, const QString &relativeFileName);
|
||||
|
||||
//! Create other version's setting file from "my settings file"
|
||||
static QString otherVersionSettingsFileName(const BlackMisc::CApplicationInfo &info, const QString &mySettingFile);
|
||||
|
||||
//! Create other version's cache file from "my cache file"
|
||||
static QString otherVersionCacheFileName(const BlackMisc::CApplicationInfo &info, const QString &myCacheFile);
|
||||
|
||||
//! Has the settings file for the given other version?
|
||||
static bool hasOtherVersionSettingsFile(const BlackMisc::CApplicationInfo &info, const QString &mySettingFile);
|
||||
|
||||
//! Has the cache file for the given other version?
|
||||
static bool hasOtherVersionCacheFile(const BlackMisc::CApplicationInfo &info, const QString &myCacheFile);
|
||||
|
||||
//! Setting JSON object as string
|
||||
static QString otherVersionSettingsFileContent(const BlackMisc::CApplicationInfo &info, const QString &mySettingFile);
|
||||
|
||||
//! Cache JSON object as string
|
||||
static QString otherVersionCacheFileContent(const BlackMisc::CApplicationInfo &info, const QString &myCacheFile);
|
||||
};
|
||||
} // ns
|
||||
|
||||
#endif // guard
|
||||
Reference in New Issue
Block a user