2013-07-23 01:58:48 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Cocos2D-X Qt 5 Platform
|
|
|
|
Copyright (C) 2013 Jolla Ltd.
|
|
|
|
Contact: Thomas Perl <thomas.perl@jollamobile.com>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "platform/CCPlatformMacros.h"
|
|
|
|
#include "platform/CCCommon.h"
|
|
|
|
#include "ccTypes.h"
|
|
|
|
#include "ccMacros.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
class CC_DLL FileUtilsQt5 : public FileUtils
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-26 16:55:46 +08:00
|
|
|
FileUtilsQt5();
|
2013-07-23 01:58:48 +08:00
|
|
|
|
|
|
|
/* override funtions */
|
|
|
|
virtual bool init();
|
|
|
|
virtual std::string getWritablePath();
|
|
|
|
virtual bool isFileExist(const std::string& strFilePath);
|
|
|
|
};
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
FileUtils *
|
|
|
|
FileUtils::getInstance()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
if (s_sharedFileUtils == NULL)
|
|
|
|
{
|
2013-07-26 16:55:46 +08:00
|
|
|
s_sharedFileUtils = new FileUtilsQt5();
|
2013-07-23 01:58:48 +08:00
|
|
|
s_sharedFileUtils->init();
|
|
|
|
}
|
|
|
|
return s_sharedFileUtils;
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
FileUtilsQt5::FileUtilsQt5()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-07-26 16:55:46 +08:00
|
|
|
FileUtilsQt5::init()
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
// Determine directory of the application executable
|
|
|
|
QDir app_dir = QDir("/proc/self/exe").canonicalPath();
|
|
|
|
app_dir.cdUp();
|
|
|
|
|
|
|
|
// Resources should be placed alongside the binary (same directory)
|
|
|
|
QString path = app_dir.path() + "/Resources/";
|
2013-07-26 16:55:46 +08:00
|
|
|
_defaultResRootPath = path.toStdString();
|
2013-07-23 01:58:48 +08:00
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
return FileUtils::init();
|
2013-07-23 01:58:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2013-09-07 13:52:23 +08:00
|
|
|
FileUtilsQt5::getWritablePath() const
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
QDir dir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
|
|
|
|
|
|
|
|
// Create directory if it does not exist yet
|
|
|
|
if (!dir.exists()) {
|
|
|
|
dir.mkpath(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir.path().toStdString();
|
|
|
|
}
|
|
|
|
|
2013-09-07 13:52:23 +08:00
|
|
|
bool FileUtilsQt5::isFileExist(const std::string& strFilePath) const
|
2013-07-23 01:58:48 +08:00
|
|
|
{
|
|
|
|
QString filePath = QString::fromStdString(strFilePath);
|
|
|
|
|
|
|
|
// Try filename without any path first
|
|
|
|
if (QFile(filePath).exists()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:55:46 +08:00
|
|
|
// If not found, look for file in _defaultResRootPath
|
|
|
|
QString defaultResRootPath = QString::fromStdString(_defaultResRootPath);
|
2013-07-23 01:58:48 +08:00
|
|
|
return QFile(QDir(defaultResRootPath).filePath(filePath)).exists();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|