QGCFileDialogController.cc 3.05 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9 10 11
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/


#include "QGCFileDialogController.h"
12 13 14
#include "QGCApplication.h"
#include "SettingsManager.h"
#include "AppSettings.h"
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

#include <QStandardPaths>
#include <QDebug>
#include <QDir>

QGC_LOGGING_CATEGORY(QGCFileDialogControllerLog, "QGCFileDialogControllerLog")

QStringList QGCFileDialogController::getFiles(const QString& directoryPath, const QStringList& fileExtensions)
{
    qCDebug(QGCFileDialogControllerLog) << "getFiles" << directoryPath << fileExtensions;
    QStringList files;

    QDir fileDir(directoryPath);

    QStringList infoListExtensions;
30
    for (const QString& extension: fileExtensions) {
31 32 33 34 35
        infoListExtensions.append(QStringLiteral("*.%1").arg(extension));
    }

    QFileInfoList fileInfoList = fileDir.entryInfoList(infoListExtensions,  QDir::Files, QDir::Name);

36
    for (const QFileInfo& fileInfo: fileInfoList) {
37 38 39 40 41 42 43
        qCDebug(QGCFileDialogControllerLog) << "getFiles found" << fileInfo.fileName();
        files << fileInfo.fileName();
    }

    return files;
}

44
QString QGCFileDialogController::filenameWithExtension(const QString& filename, const QStringList& rgFileExtensions)
45 46 47
{
    QString filenameWithExtension(filename);

48 49
    bool matchFound = false;
    for (const QString& extension : rgFileExtensions) {
Don Gagne's avatar
Don Gagne committed
50
        QString dotExtension = QStringLiteral(".%1").arg(extension);
51 52 53 54
        matchFound = filenameWithExtension.endsWith(dotExtension);
        if (matchFound) {
            break;
        }
55 56
    }

57
    if (!matchFound) {
Don Gagne's avatar
Don Gagne committed
58
        filenameWithExtension += QStringLiteral(".%1").arg(rgFileExtensions[0]);
59 60
    }

Don Gagne's avatar
Don Gagne committed
61
    return filenameWithExtension;
62 63 64 65 66 67 68
}

bool QGCFileDialogController::fileExists(const QString& filename)
{
    return QFile(filename).exists();
}

69
QString QGCFileDialogController::fullyQualifiedFilename(const QString& directoryPath, const QString& filename, const QStringList& rgFileExtensions)
70
{
71
    return directoryPath + QStringLiteral("/") + filenameWithExtension(filename, rgFileExtensions);
72 73 74 75 76 77
}

void QGCFileDialogController::deleteFile(const QString& filename)
{
    QFile::remove(filename);
}
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

QString QGCFileDialogController::fullFolderPathToShortMobilePath(const QString& fullFolderPath)
{
#ifdef __mobile__
    QString defaultSavePath = qgcApp()->toolbox()->settingsManager()->appSettings()->savePath()->rawValueString();
    if (fullFolderPath.startsWith(defaultSavePath)) {
        int lastDirSepIndex = fullFolderPath.lastIndexOf(QStringLiteral("/"));
        return qgcApp()->applicationName() + QStringLiteral("/") + fullFolderPath.right(fullFolderPath.length() - lastDirSepIndex);
    } else {
        return fullFolderPath;
    }
#else
    qWarning() << "QGCFileDialogController::fullFolderPathToShortMobilePath should only be used in mobile builds";
    return fullFolderPath;
#endif
}