aboutsummaryrefslogtreecommitdiffstats
path: root/src/livedocument.cpp
blob: d4347cfa8b595f725c6eea812ebd4f7edccad837 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/****************************************************************************
**
** Copyright (C) 2016 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QmlLive tool.
**
** $QT_BEGIN_LICENSE:GPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/

#include "livedocument.h"

#include <QDebug>

/*!
 * \class LiveDocument
 * \brief Encapsulates a relative path to a workspace document
 * \inmodule qmllive
 */

/*!
 * Constructs a null instance.
 *
 * \sa isNull(), errorString()
 */
LiveDocument::LiveDocument()
{
    m_errorString = tr("Internal error: A null LiveDocument passed");
}

/*!
 * Constructs instance for the given \a relativeFilePath.
 *
 * The \a relativeFilePath MUST NOT be an empty string, it MUST be a relative
 * path, and when resolved relatively to a directory it MUST NOT resolve to a
 * path outside of the directory.
 */
LiveDocument::LiveDocument(const QString &relativeFilePath)
{
    LIVE_ASSERT(!relativeFilePath.isEmpty(), return);
    LIVE_ASSERT(QDir::isRelativePath(relativeFilePath), return);
    LIVE_ASSERT(!QDir::cleanPath(relativeFilePath).startsWith(QLatin1String("../")), return);

    m_relativeFilePath = relativeFilePath;
}

/*!
 * \fn bool LiveDocument::isNull() const
 *
 * Returns true if this is a null instance.
 *
 * A null instance has been either contructed with the default constructor or
 * the resolve() call failed.
 *
 * \sa errorString()
 */

/*!
 * \fn QString LiveDocument::errorString() const
 *
 * When called just after resolve(), existsIn() or isFileIn() failed, returns a
 * descriptive message suitable for displaying in user interface. When called in
 * other context, the result is undefined.
 */

/*!
 * Returns \c true if this document exists in the given \a workspace directory.
 *
 * \sa errorString()
 */
bool LiveDocument::existsIn(const QDir &workspace) const
{
    LIVE_ASSERT(!isNull(), return false);

    bool exists = QFileInfo(workspace, m_relativeFilePath).exists();
    if (!exists) {
        m_errorString = tr("Document '%1' does not exist in workspace '%2'")
            .arg(m_relativeFilePath)
            .arg(workspace.path());
    }
    return exists;
}

/*!
 * Returns \c true if this document exists as a regular file (or a symbolic link
 * to a regular file) in the given \a workspace directory.
 *
 * Symbolic links resolution applies.
 *
 * \sa errorString()
 */
bool LiveDocument::isFileIn(const QDir &workspace) const
{
    LIVE_ASSERT(!isNull(), return false);

    if (!existsIn(workspace))
        return false;

    bool isFile = QFileInfo(workspace, m_relativeFilePath).isFile();
    if (!isFile) {
        m_errorString = tr("Document '%1' is a non-regular file in workspace '%2'")
            .arg(m_relativeFilePath)
            .arg(workspace.path());
    }
    return isFile;
}

/*!
 * Returns the relative file path including the file name.
 */
QString LiveDocument::relativeFilePath() const
{
    LIVE_ASSERT(!isNull(), return QString());

    return m_relativeFilePath;
}

/*!
 * Returns the absolute file path within a \a workspace, including the file name.
 */
QString LiveDocument::absoluteFilePathIn(const QDir &workspace) const
{
    LIVE_ASSERT(!isNull(), return QString());

    return QDir::cleanPath(workspace.absoluteFilePath(m_relativeFilePath));
}

/*!
 * Constructs a non-null instance unless the \a filePath resolves outside of the
 * \a workspace directory.
 *
 * \a filePath may be an absolute or relative file path to a file which is NOT
 * required to exist.
 *
 * \sa isNull(), errorString()
 */
LiveDocument LiveDocument::resolve(const QDir &workspace, const QString &filePath)
{
    LiveDocument retv;

    if (filePath.isEmpty()) {
        qWarning() << "filePath is an empty string";
        retv.m_errorString = tr("Not a valid file path: ''");
        return retv;
    }

    QString relativeFilePath = workspace.relativeFilePath(filePath);
    QString cleanPath = QDir::cleanPath(relativeFilePath);
    if (cleanPath.isEmpty()) {
        retv.m_relativeFilePath = QStringLiteral(".");
    } else if (!cleanPath.startsWith(QLatin1String("../"))) {
        retv.m_relativeFilePath = relativeFilePath;
    } else {
        retv.m_errorString = tr("Document path '%1' is outside the workspace directory '%2'")
            .arg(filePath).arg(workspace.path());
    }

    return retv;
}

/*!
 * Allows to print LiveDocument \a document via debug stream \a dbg.
 */
QDebug operator<<(QDebug dbg, const LiveDocument &document)
{
    if (document.isNull())
        dbg << QStringLiteral("<null>");
    else
        dbg << document.relativeFilePath();

    return dbg;
}