pkpassmanager.cpp 3.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
    Copyright (C) 2018 Volker Krause <vkrause@kde.org>

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "pkpassmanager.h"
19
#include "logging.h"
20

Volker Krause's avatar
Volker Krause committed
21
#include <KPkPass/Pass>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#include <QDebug>
#include <QDir>
#include <QDirIterator>
#include <QFile>
#include <QStandardPaths>
#include <QUrl>
#include <QVector>

PkPassManager::PkPassManager(QObject* parent)
    : QObject(parent)
{
}

PkPassManager::~PkPassManager() = default;

QVector<QString> PkPassManager::passes() const
{
    const auto basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/passes");
    QDir::root().mkpath(basePath);

    QVector<QString> passIds;
    for (QDirIterator topIt(basePath, QDir::NoDotAndDotDot | QDir::Dirs); topIt.hasNext();) {
        for (QDirIterator subIt(topIt.next(), QDir::Files); subIt.hasNext();) {
            QFileInfo fi(subIt.next());
            passIds.push_back(fi.dir().dirName() + QLatin1Char('/') + fi.baseName());
        }
    }

    return passIds;
}

Volker Krause's avatar
Volker Krause committed
54
KPkPass::Pass* PkPassManager::pass(const QString& passId)
55 56 57 58 59 60
{
    const auto it = m_passes.constFind(passId);
    if (it != m_passes.constEnd())
        return it.value();

    const auto basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/passes/");
Volker Krause's avatar
Volker Krause committed
61
    auto file = KPkPass::Pass::fromFile(basePath + passId + QLatin1String(".pkpass"), this);
62 63 64 65 66 67 68
    // TODO error handling
    m_passes.insert(passId, file);
    return file;
}

void PkPassManager::importPass(const QUrl& url)
{
69 70 71 72 73 74 75 76 77 78 79
    doImportPass(url, Copy);
}

void PkPassManager::importPassFromTempFile(const QString& tmpFile)
{
    doImportPass(QUrl::fromLocalFile(tmpFile), Move);
}

void PkPassManager::doImportPass(const QUrl& url, PkPassManager::ImportMode mode)
{
    qCDebug(Log) << url << mode;
80 81 82 83 84 85
    if (!url.isLocalFile())
        return; // TODO

    const auto basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/passes");
    QDir::root().mkpath(basePath);

Volker Krause's avatar
Volker Krause committed
86
    std::unique_ptr<KPkPass::Pass> file(KPkPass::Pass::fromFile(url.toLocalFile()));
87 88 89 90 91 92 93 94 95 96 97 98 99
    if (!file)
        return; // TODO error handling
    if (file->passTypeIdentifier().isEmpty() || file->serialNumber().isEmpty())
        return; // TODO error handling

    // TODO check for already existing version
    QDir dir(basePath);
    dir.mkdir(file->passTypeIdentifier());
    dir.cd(file->passTypeIdentifier());

    // serialNumber() can contain percent-encoding or slashes,
    // ie stuff we don't want to have in file names
    const auto passId = QString::fromUtf8(file->serialNumber().toUtf8().toBase64(QByteArray::Base64UrlEncoding));
100 101 102 103 104 105 106 107 108 109

    switch (mode) {
        case Move:
            QFile::rename(url.toLocalFile(), dir.absoluteFilePath(passId + QLatin1String(".pkpass")));
            break;
        case Copy:
            QFile::copy(url.toLocalFile(), dir.absoluteFilePath(passId + QLatin1String(".pkpass")));
            break;
    }

110 111 112 113 114 115 116 117 118
    emit passAdded(dir.dirName() + QLatin1Char('/') + passId);
}

void PkPassManager::removePass(const QString& passId)
{
    const auto basePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/passes/");
    QFile::remove(basePath + QLatin1Char('/') + passId + QLatin1String(".pkpass"));
    // TODO change signal
}