idocument.cpp 9.53 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt 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
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30

31
#include "idocument.h"
32

33
34
#include "infobar.h"

35
#include <utils/fileutils.h>
36
37
#include <utils/qtcassert.h>

38
39
#include <QFile>
#include <QFileInfo>
40

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*!
    \class Core::IDocument
    \brief The IDocument class describes a document that can be saved and reloaded.

    The most common use for implementing an IDocument subclass, is as a document for an IEditor
    implementation. Multiple editors can work in the same document instance, so the IDocument
    subclass should hold all data and functions that are independent from the specific
    IEditor instance, for example the content, highlighting information, the name of the
    corresponding file, and functions for saving and reloading the file.

    Each IDocument subclass works only with the corresponding IEditor subclasses that it
    was designed to work with.

    \mainclass
*/

57
/*!
58
    \fn QString Core::IDocument::filePath() const
59
60
    Returns the absolute path of the file that this document refers to. May be empty for
    non-file documents.
61
    \sa setFilePath()
62
63
*/

64
65
namespace Core {

Orgad Shaneh's avatar
Orgad Shaneh committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
namespace Internal {

class IDocumentPrivate
{
public:
    IDocumentPrivate() :
        infoBar(0),
        temporary(false),
        hasWriteWarning(false),
        restored(false)
    {
    }

    ~IDocumentPrivate()
    {
        delete infoBar;
    }

    QString mimeType;
85
    Utils::FileName filePath;
86
87
    QString preferredDisplayName;
    QString uniqueDisplayName;
Orgad Shaneh's avatar
Orgad Shaneh committed
88
89
    QString autoSaveName;
    InfoBar *infoBar;
90
    Id id;
Orgad Shaneh's avatar
Orgad Shaneh committed
91
92
93
94
95
96
97
    bool temporary;
    bool hasWriteWarning;
    bool restored;
};

} // namespace Internal

98
IDocument::IDocument(QObject *parent) : QObject(parent),
Orgad Shaneh's avatar
Orgad Shaneh committed
99
    d(new Internal::IDocumentPrivate)
100
101
102
{
}

103
IDocument::~IDocument()
104
{
105
    removeAutoSaveFile();
Orgad Shaneh's avatar
Orgad Shaneh committed
106
    delete d;
107
108
}

109
110
void IDocument::setId(Id id)
{
Orgad Shaneh's avatar
Orgad Shaneh committed
111
    d->id = id;
112
113
114
115
}

Id IDocument::id() const
{
Orgad Shaneh's avatar
Orgad Shaneh committed
116
117
    QTC_CHECK(d->id.isValid());
    return d->id;
118
119
}

120
121
122
123
124
125
126
127
128
129
130
131
132
/*!
    \enum IDocument::OpenResult
    The OpenResult enum describes whether a file was successfully opened.

    \value Success
           The file was read successfully and can be handled by this document type.
    \value ReadError
           The file could not be opened for reading, either because it does not exist or
           because of missing permissions.
    \value CannotHandle
           This document type could not handle the file content.
*/

133
134
135
136
137
138
139
140
141
142
/*!
 * Used to load a file if this document is part of an IEditor implementation, when the editor
 * is opened.
 * If the editor is opened from an auto save file, \a realFileName is the name of the auto save
 * that should be loaded, and \a fileName is the file name of the resulting file.
 * In that case, the contents of the auto save file should be loaded, the file name of the
 * IDocument should be set to \a fileName, and the document state be set to modified.
 * If the editor is opened from a regular file, \a fileName and \a realFileName are the same.
 * Use \a errorString to return an error message, if this document can not handle the
 * file contents.
143
 * Returns whether the file was opened and read successfully.
144
 */
145
IDocument::OpenResult IDocument::open(QString *errorString, const QString &fileName, const QString &realFileName)
146
147
148
149
{
    Q_UNUSED(errorString)
    Q_UNUSED(fileName)
    Q_UNUSED(realFileName)
150
    return OpenResult::CannotHandle;
151
152
}

153
154
155
156
157
158
159
160
161
/*!
 * Returns the current contents of the document. The base implementation returns an empty
 * QByteArray.
 */
QByteArray IDocument::contents() const
{
    return QByteArray();
}

162
163
164
165
166
167
168
169
170
171
172
173
/*!
    Used for example by EditorManager::openEditorWithContents() to set the contents
    of this document.
    Returns if setting the contents was successful.
    The base implementation does nothing and returns false.
*/
bool IDocument::setContents(const QByteArray &contents)
{
    Q_UNUSED(contents)
    return false;
}

174
const Utils::FileName &IDocument::filePath() const
Orgad Shaneh's avatar
Orgad Shaneh committed
175
176
177
178
{
    return d->filePath;
}

179
IDocument::ReloadBehavior IDocument::reloadBehavior(ChangeTrigger state, ChangeType type) const
180
181
182
183
184
185
186
187
{
    if (type == TypePermissions)
        return BehaviorSilent;
    if (type == TypeContents && state == TriggerInternal && !isModified())
        return BehaviorSilent;
    return BehaviorAsk;
}

188
void IDocument::checkPermissions()
189
190
191
{
}

192
bool IDocument::shouldAutoSave() const
193
194
195
196
{
    return false;
}

197
bool IDocument::isFileReadOnly() const
198
{
199
    if (filePath().isEmpty())
200
        return false;
201
    return !filePath().toFileInfo().isWritable();
202
203
}

204
205
206
207
208
209
210
/*!
    Returns if the document is a temporary that should for example not be considered
    when saving/restoring the session state, recent files, etc. Defaults to false.
    \sa setTemporary()
*/
bool IDocument::isTemporary() const
{
Orgad Shaneh's avatar
Orgad Shaneh committed
211
    return d->temporary;
212
213
214
215
216
217
218
219
}

/*!
    Sets if the document is \a temporary.
    \sa isTemporary()
*/
void IDocument::setTemporary(bool temporary)
{
Orgad Shaneh's avatar
Orgad Shaneh committed
220
221
222
    d->temporary = temporary;
}

223
224
225
226
227
228
229
230
231
232
QString IDocument::fallbackSaveAsPath() const
{
    return QString();
}

QString IDocument::fallbackSaveAsFileName() const
{
    return QString();
}

Orgad Shaneh's avatar
Orgad Shaneh committed
233
234
235
QString IDocument::mimeType() const
{
    return d->mimeType;
236
237
}

hjk's avatar
hjk committed
238
239
void IDocument::setMimeType(const QString &mimeType)
{
Orgad Shaneh's avatar
Orgad Shaneh committed
240
241
    if (d->mimeType != mimeType) {
        d->mimeType = mimeType;
hjk's avatar
hjk committed
242
243
244
245
        emit mimeTypeChanged();
    }
}

246
bool IDocument::autoSave(QString *errorString, const QString &fileName)
247
248
249
{
    if (!save(errorString, fileName, true))
        return false;
Orgad Shaneh's avatar
Orgad Shaneh committed
250
    d->autoSaveName = fileName;
251
252
253
254
255
    return true;
}

static const char kRestoredAutoSave[] = "RestoredAutoSave";

256
void IDocument::setRestoredFrom(const QString &name)
257
{
Orgad Shaneh's avatar
Orgad Shaneh committed
258
259
    d->autoSaveName = name;
    d->restored = true;
Orgad Shaneh's avatar
Orgad Shaneh committed
260
    InfoBarEntry info(Id(kRestoredAutoSave),
261
          tr("File was restored from auto-saved copy. "
262
             "Select Save to confirm or Revert to Saved to discard changes."));
263
264
265
    infoBar()->addInfo(info);
}

266
void IDocument::removeAutoSaveFile()
267
{
Orgad Shaneh's avatar
Orgad Shaneh committed
268
269
270
271
272
    if (!d->autoSaveName.isEmpty()) {
        QFile::remove(d->autoSaveName);
        d->autoSaveName.clear();
        if (d->restored) {
            d->restored = false;
Orgad Shaneh's avatar
Orgad Shaneh committed
273
            infoBar()->removeInfo(Id(kRestoredAutoSave));
274
275
276
277
        }
    }
}

Orgad Shaneh's avatar
Orgad Shaneh committed
278
279
280
281
282
283
284
285
286
287
bool IDocument::hasWriteWarning() const
{
    return d->hasWriteWarning;
}

void IDocument::setWriteWarning(bool has)
{
    d->hasWriteWarning = has;
}

288
InfoBar *IDocument::infoBar()
289
{
Orgad Shaneh's avatar
Orgad Shaneh committed
290
291
292
    if (!d->infoBar)
        d->infoBar = new InfoBar;
    return d->infoBar;
293
294
}

295
/*!
296
297
    Set absolute file path for this file to \a filePath. Can be empty.
    The default implementation sets the file name and sends filePathChanged() and changed()
298
    signals. Can be reimplemented by subclasses to do more.
299
    \sa filePath()
300
*/
301
void IDocument::setFilePath(const Utils::FileName &filePath)
302
{
Orgad Shaneh's avatar
Orgad Shaneh committed
303
    if (d->filePath == filePath)
304
        return;
305
    Utils::FileName oldName = d->filePath;
Orgad Shaneh's avatar
Orgad Shaneh committed
306
307
    d->filePath = filePath;
    emit filePathChanged(oldName, d->filePath);
308
309
310
    emit changed();
}

311
312
313
/*!
    Returns the string to display for this document, e.g. in the open document combo box
    and pane.
314
315
316
317
318
    The returned string has the following priority:
      * Unique display name set by the document model
      * Preferred display name set by the owner
      * Base name of the document's file name

319
320
321
322
    \sa setDisplayName()
*/
QString IDocument::displayName() const
{
323
    return d->uniqueDisplayName.isEmpty() ? plainDisplayName() : d->uniqueDisplayName;
324
325
326
327
328
329
330
331
332
}

/*!
    Sets the string that is displayed for this document, e.g. in the open document combo box
    and pane, to \a name. Defaults to the file name of the file path for this document.
    You can reset the display name to the default by passing an empty string.
    \sa displayName()
    \sa filePath()
 */
333
void IDocument::setPreferredDisplayName(const QString &name)
334
{
335
    if (name == d->preferredDisplayName)
336
        return;
337
    d->preferredDisplayName = name;
338
339
340
    emit changed();
}

341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*!
    \internal
    Returns displayName without disambiguation.
 */
QString IDocument::plainDisplayName() const
{
    return d->preferredDisplayName.isEmpty() ? d->filePath.fileName() : d->preferredDisplayName;
}

/*!
    \internal
    Sets unique display name for the document. Used by the document model.
 */
void IDocument::setUniqueDisplayName(const QString &name)
{
    d->uniqueDisplayName = name;
}

359
} // namespace Core