Skip to content

Commit

Permalink
reworking of getting the path to the file when saving/opening files
Browse files Browse the repository at this point in the history
  • Loading branch information
Nethius committed Sep 7, 2023
1 parent e1fa24c commit b5dd48a
Show file tree
Hide file tree
Showing 21 changed files with 167 additions and 243 deletions.
79 changes: 0 additions & 79 deletions client/fileUtilites.cpp

This file was deleted.

16 changes: 0 additions & 16 deletions client/fileUtilites.h

This file was deleted.

7 changes: 4 additions & 3 deletions client/platforms/ios/MobileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
#include <QObject>
#include <QStringList>

class MobileUtils : public QObject {
class MobileUtils : public QObject
{
Q_OBJECT

public:
MobileUtils() = delete;

public slots:
static void shareText(const QStringList& filesToSend);

static void shareText(const QStringList &filesToSend);
static void openFile();
};

#endif // MOBILEUTILS_H
28 changes: 28 additions & 0 deletions client/platforms/ios/MobileUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,31 @@
}
}

@interface MyFilePickerDelegate : NSObject <UIDocumentPickerDelegate>
@end

@implementation MyFilePickerDelegate

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
for (NSURL *url in urls) {
NSString *filePath = [url path];

NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSString *fileContent = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
NSLog(@"Содержимое файла: %@", fileContent);
}
}

@end

void MobileUtils::openFile() {
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.item"] inMode:UIDocumentPickerModeOpen];

MyFilePickerDelegate *filePickerDelegate = [[MyFilePickerDelegate alloc] init];
documentPicker.delegate = filePickerDelegate;

UIViewController *qtController = getViewController();
if (!qtController) return;

[qtController presentViewController:documentPicker animated:YES completion:nil];
}
6 changes: 3 additions & 3 deletions client/ui/controllers/exportController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "configurators/openvpn_configurator.h"
#include "configurators/wireguard_configurator.h"
#include "core/errorstrings.h"
#include "fileUtilites.h"
#include "systemController.h"
#ifdef Q_OS_ANDROID
#include "platforms/android/androidutils.h"
#endif
Expand Down Expand Up @@ -200,9 +200,9 @@ QList<QString> ExportController::getQrCodes()
return m_qrCodes;
}

void ExportController::saveFile(const QString &fileName)
void ExportController::exportConfig(const QString &fileName)
{
FileUtilites::saveFile(fileName, m_config);
SystemController::saveFile(fileName, m_config);
}

QList<QString> ExportController::generateQrCodeImageSeries(const QByteArray &data)
Expand Down
4 changes: 3 additions & 1 deletion client/ui/controllers/exportController.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ public slots:
QString getConfig();
QList<QString> getQrCodes();

void saveFile(const QString &fileName);
void exportConfig(const QString &fileName);

signals:
void generateConfig(int type);
void exportErrorOccurred(const QString &errorMessage);

void exportConfigChanged();

void saveFile(const QString &fileName, const QString &data);

private:
QList<QString> generateQrCodeImageSeries(const QByteArray &data);
QString svgToBase64(const QString &image);
Expand Down
7 changes: 1 addition & 6 deletions client/ui/controllers/importController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#ifdef Q_OS_IOS
#include <CoreFoundation/CoreFoundation.h>
#endif
#include "fileUtilites.h"

namespace
{
Expand Down Expand Up @@ -88,11 +87,7 @@ ImportController::ImportController(const QSharedPointer<ServersModel> &serversMo

void ImportController::extractConfigFromFile(const QString &fileName)
{
QQuickItem *obj = findChild<QQuickItem *>("saveFileDialog");

QUrl url(fileName);
QString path = url.toLocalFile();
QFile file(path);
QFile file(fileName);

if (file.open(QIODevice::ReadOnly)) {
QString data = file.readAll();
Expand Down
1 change: 0 additions & 1 deletion client/ui/controllers/installController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include "core/errorstrings.h"
#include "core/servercontroller.h"
#include "fileUtilites.h"
#include "utilities.h"

namespace
Expand Down
4 changes: 0 additions & 4 deletions client/ui/controllers/pageController.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ public slots:
void showPassphraseRequestDrawer();
void passphraseRequestDrawerClosed(QString passphrase);

void setupFileDialogForConfig();
void setupFileDialogForSites(bool replaceExistingSites);
void setupFileDialogForBackup();

private:
QSharedPointer<ServersModel> m_serversModel;

Expand Down
8 changes: 4 additions & 4 deletions client/ui/controllers/settingsController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <QStandardPaths>

#include "fileUtilites.h"
#include "logger.h"
#include "systemController.h"
#include "ui/qautostart.h"
#include "version.h"

Expand Down Expand Up @@ -70,7 +70,7 @@ void SettingsController::openLogsFolder()

void SettingsController::exportLogsFile(const QString &fileName)
{
FileUtilites::saveFile(fileName, Logger::getLogFile());
SystemController::saveFile(fileName, Logger::getLogFile());
}

void SettingsController::clearLogs()
Expand All @@ -81,12 +81,12 @@ void SettingsController::clearLogs()

void SettingsController::backupAppConfig(const QString &fileName)
{
FileUtilites::saveFile(fileName, m_settings->backupAppConfig());
SystemController::saveFile(fileName, m_settings->backupAppConfig());
}

void SettingsController::restoreAppConfig(const QString &fileName)
{
QFile file(FileUtilites::getFileName(fileName));
QFile file(fileName);

file.open(QIODevice::ReadOnly);

Expand Down
2 changes: 2 additions & 0 deletions client/ui/controllers/settingsController.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public slots:
void changeSettingsFinished(const QString &finishedMessage);
void changeSettingsErrorOccurred(const QString &errorMessage);

void saveFile(const QString &fileName, const QString &data);

private:
QSharedPointer<ServersModel> m_serversModel;
QSharedPointer<ContainersModel> m_containersModel;
Expand Down
6 changes: 3 additions & 3 deletions client/ui/controllers/sitesController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <QHostInfo>
#include <QStandardPaths>

#include "fileUtilites.h"
#include "systemController.h"
#include "utilities.h"

SitesController::SitesController(const std::shared_ptr<Settings> &settings,
Expand Down Expand Up @@ -82,7 +82,7 @@ void SitesController::removeSite(int index)

void SitesController::importSites(const QString &fileName, bool replaceExisting)
{
QFile file(FileUtilites::getFileName(fileName));
QFile file(fileName);

if (!file.open(QIODevice::ReadOnly)) {
emit errorOccurred(tr("Can't open file: ") + fileName);
Expand Down Expand Up @@ -145,7 +145,7 @@ void SitesController::exportSites(const QString &fileName)
QJsonDocument jsonDocument(jsonArray);
QByteArray jsonData = jsonDocument.toJson();

FileUtilites::saveFile(fileName, jsonData);
SystemController::saveFile(fileName, jsonData);

emit finished(tr("Export completed"));
}
2 changes: 2 additions & 0 deletions client/ui/controllers/sitesController.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public slots:
void errorOccurred(const QString &errorMessage);
void finished(const QString &message);

void saveFile(const QString &fileName, const QString &data);

private:
std::shared_ptr<Settings> m_settings;

Expand Down
36 changes: 28 additions & 8 deletions client/ui/controllers/systemController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ void SystemController::saveFile(QString fileName, const QString &data)
QUrl fileUrl = QDir::tempPath() + "/" + fileName;
QFile file(fileUrl.toString());
#else
QUrl fileUrl = QUrl(fileName);
QFile file(fileUrl.toLocalFile());
QFile file(fileName);
#endif

// todo check if save successful
Expand All @@ -49,14 +48,18 @@ void SystemController::saveFile(QString fileName, const QString &data)
MobileUtils::shareText(filesToSend);
return;
#else
QFileInfo fi(fileUrl.toLocalFile());
QFileInfo fi(fileName);
QDesktopServices::openUrl(fi.absoluteDir().absolutePath());
#endif
}

QString SystemController::getFileName()
QString SystemController::getFileName(const QString &acceptLabel, const QString &nameFilter,
const QString &selectedFile, const bool isSaveMode, const QString &defaultSuffix)
{
QString fileName;
#ifdef Q_OS_IOS
MobileUtils::openFile();

CFURLRef url = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault,
CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(fileName.unicode()), fileName.length()),
Expand All @@ -69,17 +72,34 @@ QString SystemController::getFileName()
return fileName;
#endif

auto mainFileDialog = m_qmlRoot->findChild<QObject>("mainFileDialog").parent();
QObject *mainFileDialog = m_qmlRoot->findChild<QObject>("mainFileDialog").parent();
if (!mainFileDialog) {
return "";
}
QMetaObject::invokeMethod(mainFileDialog, "open", Qt::DirectConnection);

mainFileDialog->setProperty("acceptLabel", QVariant::fromValue(acceptLabel));
mainFileDialog->setProperty("nameFilters", QVariant::fromValue(QStringList(nameFilter)));
if (!selectedFile.isEmpty()) {
mainFileDialog->setProperty("selectedFile", QVariant::fromValue(selectedFile));
}
mainFileDialog->setProperty("isSaveMode", QVariant::fromValue(isSaveMode));
mainFileDialog->setProperty("defaultSuffix", QVariant::fromValue(defaultSuffix));
QMetaObject::invokeMethod(mainFileDialog, "open");

bool isFileDialogAccepted = false;
QEventLoop wait;
QObject::connect(this, &SystemController::fileDialogAccepted, &wait, &QEventLoop::quit);
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 "";
}

auto fileName = mainFileDialog->property("selectedFile").toString();
fileName = mainFileDialog->property("selectedFile").toString();

#ifdef Q_OS_ANDROID
// patch for files containing spaces etc
Expand Down
9 changes: 5 additions & 4 deletions client/ui/controllers/systemController.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ class SystemController : public QObject
public:
explicit SystemController(const std::shared_ptr<Settings> &setting, QObject *parent = nullptr);

static void saveFile(QString fileName, const QString &data);

public slots:
void saveFile(QString fileName, const QString &data);
QString getFileName();
QString getFileName(const QString &acceptLabel, const QString &nameFilter, const QString &selectedFile = "",
const bool isSaveMode = false, const QString &defaultSuffix = "");

void setQmlRoot(QObject *qmlRoot);

signals:
void fileDialogAccepted();
void fileDialogRejected();
void fileDialogClosed(const bool isAccepted);

private:
std::shared_ptr<Settings> m_settings;
Expand Down
Loading

0 comments on commit b5dd48a

Please sign in to comment.