#include "systemController.h" #include #include #include #include #include #include #include #include #include #ifdef Q_OS_ANDROID #include "platforms/android/android_controller.h" #endif #ifdef Q_OS_IOS #include "platforms/ios/ios_controller.h" #include #endif SystemController::SystemController(const std::shared_ptr &settings, QObject *parent) : QObject(parent), m_settings(settings) { } void SystemController::saveFile(const QString &fileName, const QString &data) { #if defined Q_OS_ANDROID AndroidController::instance()->saveFile(fileName, data); return; #endif #ifdef Q_OS_IOS QUrl fileUrl = QDir::tempPath() + "/" + fileName; QFile file(fileUrl.toString()); #else QFile file(fileName); #endif // todo check if save successful file.open(QIODevice::WriteOnly); file.write(data.toUtf8()); file.close(); #ifdef Q_OS_IOS QStringList filesToSend; filesToSend.append(fileUrl.toString()); // todo check if save successful IosController::Instance()->shareText(filesToSend); return; #else QFileInfo fi(fileName); #ifdef Q_OS_MAC const auto url = "file://" + fi.absoluteDir().absolutePath(); #else const auto url = fi.absoluteDir().absolutePath(); #endif QDesktopServices::openUrl(url); #endif } bool SystemController::readFile(const QString &fileName, QByteArray &data) { #ifdef Q_OS_ANDROID int fd = AndroidController::instance()->getFd(fileName); if (fd == -1) return false; QFile file; if(!file.open(fd, QIODevice::ReadOnly)) return false; data = file.readAll(); AndroidController::instance()->closeFd(); #else QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) return false; data = file.readAll(); #endif return true; } bool SystemController::readFile(const QString &fileName, QString &data) { QByteArray byteArray; if(!readFile(fileName, byteArray)) return false; data = byteArray; return true; } QString SystemController::getFileName(const QString &acceptLabel, const QString &nameFilter, const QString &selectedFile, const bool isSaveMode, const QString &defaultSuffix) { QString fileName; #ifdef Q_OS_ANDROID Q_ASSERT(!isSaveMode); return AndroidController::instance()->openFile(nameFilter); #endif #ifdef Q_OS_IOS fileName = IosController::Instance()->openFile(); if (fileName.isEmpty()) { return fileName; } CFURLRef url = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, CFStringCreateWithCharacters(0, reinterpret_cast(fileName.unicode()), fileName.length()), kCFURLPOSIXPathStyle, 0); if (!CFURLStartAccessingSecurityScopedResource(url)) { qDebug() << "Could not access path " << QUrl::fromLocalFile(fileName).toString(); } return fileName; #endif QObject *mainFileDialog = m_qmlRoot->findChild("mainFileDialog").parent(); if (!mainFileDialog) { return ""; } mainFileDialog->setProperty("acceptLabel", QVariant::fromValue(acceptLabel)); mainFileDialog->setProperty("nameFilters", QVariant::fromValue(QStringList(nameFilter))); mainFileDialog->setProperty("defaultSuffix", QVariant::fromValue(defaultSuffix)); mainFileDialog->setProperty("isSaveMode", QVariant::fromValue(isSaveMode)); if (!selectedFile.isEmpty()) { mainFileDialog->setProperty("selectedFile", QVariant::fromValue(QUrl(selectedFile))); } QMetaObject::invokeMethod(mainFileDialog, "open"); bool isFileDialogAccepted = false; QEventLoop wait; QObject::connect(this, &SystemController::fileDialogClosed, [&wait, &isFileDialogAccepted](const bool isAccepted) { isFileDialogAccepted = isAccepted; wait.quit(); }); wait.exec(); QObject::disconnect(this, &SystemController::fileDialogClosed, nullptr, nullptr); if (!isFileDialogAccepted) { return ""; } fileName = mainFileDialog->property("selectedFile").toString(); return QUrl(fileName).toLocalFile(); } void SystemController::setQmlRoot(QObject *qmlRoot) { m_qmlRoot = qmlRoot; } bool SystemController::isAuthenticated() { #ifdef Q_OS_ANDROID return AndroidController::instance()->requestAuthentication(); #else return true; #endif } void SystemController::sendTouch(float x, float y) { #ifdef Q_OS_ANDROID AndroidController::instance()->sendTouch(x, y); #endif }