File: fileprovider.cpp

package info (click to toggle)
libkode 0.0~git20241211.68f9908-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: cpp: 6,612; makefile: 5
file content (148 lines) | stat: -rw-r--r-- 4,429 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
    This file is part of KDE Schema Parser

    Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>

    This library 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 library 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 Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
 */

#include "fileprovider.h"

#include <QCoreApplication>
#include <QEventLoop>
#include <QFile>
#include <QUrl>
#include <QDebug>
#include <QDir>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QTemporaryFile>

#ifndef Q_OS_WIN
#    include <unistd.h>
#endif

static QHash<QUrl, QByteArray> fileProviderCache;

FileProvider::FileProvider(bool useLocalFilesOnly, const QStringList &importPathList,
                           const QMap<QUrl, QString> &localSchemas)
    : mUseLocalFilesOnly(useLocalFilesOnly),
      mImportPathList(importPathList),
      mLocalSchemas(localSchemas)
{
}

FileProvider::~FileProvider()
{
    cleanUp();
}

void FileProvider::cleanUp()
{
    if (!mFileName.isEmpty()) {
        QFile::remove(mFileName);
        mFileName = QString();
    }
}

bool FileProvider::get(const QUrl &url, QString &target)
{
    if (!mFileName.isEmpty()) {
        cleanUp();
    }

    if (url.isLocalFile()) {
        target = url.toLocalFile();
        return true;
    }
    if (url.scheme() == QLatin1String("qrc")) {
        target = QLatin1String(":") + url.path();
        return true;
    }

    const auto localSchemaIt = mLocalSchemas.constFind(url);
    if (localSchemaIt != mLocalSchemas.constEnd()) {
        target = localSchemaIt.value();
        return true;
    }

    Q_FOREACH (const QString &importPath, mImportPathList) {
        QDir importDir(importPath);
        QString path = importDir.absoluteFilePath(url.host() + QDir::separator() + url.path());
        if (QFile::exists(path)) {
            qDebug("Using import path '%s'", qPrintable(path));
            target = path;
            return true;
        }
    }

    if (mUseLocalFilesOnly) {
        qCritical("ERROR: Could not find the local file for '%s'", qPrintable(url.toEncoded()));
        qCritical("ERROR: Try to download the file using:");
        qCritical("ERROR:  $ cd %s", qPrintable(mImportPathList.first()));
        qCritical("ERROR:  $ wget -r %s", qPrintable(url.toEncoded()));
        qCritical("ERROR: Or use the -import-path argument to set the correct search path");
        QCoreApplication::exit(12);
        return false;
    }

    if (target.isEmpty()) {
        QTemporaryFile tmpFile;
        tmpFile.setAutoRemove(false);
        tmpFile.open();
        target = tmpFile.fileName();
        mFileName = target;
    }

    QByteArray data;
    const QHash<QUrl, QByteArray>::const_iterator it = fileProviderCache.constFind(url);
    if (it != fileProviderCache.constEnd()) {
        data = it.value();
    } else {
        qDebug("Downloading '%s'", url.toEncoded().constData());

        QNetworkAccessManager manager;
        manager.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
        QNetworkRequest request(url);
        QNetworkReply *job = manager.get(request);

        QEventLoop loop;
        QObject::connect(job, &QNetworkReply::finished, &loop, &QEventLoop::quit);
        loop.exec();

        if (job->error()) {
            qWarning("Error downloading '%s': %s", url.toEncoded().constData(),
                     qPrintable(job->errorString()));
            return false;
        }

        qDebug("Download successful");
        data = job->readAll();
        fileProviderCache[url] = data;
    }

    QFile file(mFileName);
    if (!file.open(QIODevice::WriteOnly)) {
        qWarning() << "Unable to create" << mFileName << ":" << file.errorString();
        return false;
    }

    file.write(data);
    file.close();

    return true;
}