aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/coreplugin/idocument.cpp
blob: 7be7e410116dc521568853963065f98e314ea7d2 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "idocument.h"

#include "coreplugintr.h"

#include <utils/filepath.h>
#include <utils/infobar.h>
#include <utils/minimizableinfobars.h>
#include <utils/qtcassert.h>

#include <QFile>
#include <QFileInfo>

#include <memory>
#include <optional>

/*!
    \class Core::IDocument
    \inheaderfile coreplugin/idocument.h
    \inmodule QtCreator

    \brief The IDocument class describes a document that can be saved and
    reloaded.

    The class has two use cases.

    \section1 Handling External Modifications

    You can implement IDocument and register instances in DocumentManager to
    let it handle external modifications of a file. When the file specified with
    filePath() has changed externally, the DocumentManager asks the
    corresponding IDocument instance what to do via reloadBehavior(). If that
    returns \c IDocument::BehaviorAsk, the user is asked if the file should be
    reloaded from disk. If the user requests the reload or reloadBehavior()
    returns \c IDocument::BehaviorSilent, the DocumentManager calls reload()
    to initiate a reload of the file from disk.

    Core functions: setFilePath(), reload(), reloadBehavior().

    If the content of the document can change in \QC, diverging from the
    content on disk: isModified(), save(), isSaveAsAllowed(),
    fallbackSaveAsPath(), fallbackSaveAsFileName().

    \section1 Editor Document

    The most common use case for implementing an IDocument subclass is as a
    document for an IEditor implementation. Multiple editor instances can work
    on the same document instance, for example if the document is visible in
    multiple splits simultaneously. So the IDocument subclass should hold all
    data that is independent from the specific IEditor instance, for example
    the content and highlighting information.

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

    An IDocument can either be backed by a file, or solely represent some data
    in memory. Documents backed by a file are automatically added to the
    DocumentManager by the EditorManager.

    Core functions: setId(), isModified(), contents(), setContents().

    If the content of the document is backed by a file: open(), save(),
    setFilePath(), mimeType(), shouldAutoSave(), setSuspendAllowed(), and
    everything from \l{Handling External Modifications}.

    If the content of the document is not backed by a file:
    setPreferredDisplayName(), setTemporary().

    \ingroup mainclasses
*/

/*!
    \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.
*/

/*!
    \enum IDocument::ReloadSetting

    \internal
*/

/*!
    \enum IDocument::ChangeTrigger

    The ChangeTrigger enum describes whether a file was changed from \QC
    internally or from the outside.

    \value TriggerInternal
           The file was changed by \QC.
    \value TriggerExternal
           The file was changed from the outside.

    \sa IDocument::reloadBehavior()
*/

/*!
    \enum IDocument::ChangeType

    The ChangeType enum describes the way in which the file changed.

    \value TypeContents
           The contents of the file changed.
    \value TypeRemoved
           The file was removed.

    \sa IDocument::reloadBehavior()
    \sa IDocument::reload()
*/

/*!
    \enum IDocument::ReloadFlag

    The ReloadFlag enum describes if a file should be reloaded from disk.

    \value FlagReload
           The file should be reloaded.
    \value FlagIgnore
           The file should not be reloaded, but the document state should
           reflect the change.

    \sa IDocument::reload()
*/

/*!
    \fn Core::IDocument::changed()

    This signal is emitted when the document's meta data, like file name or
    modified state, changes.

    \sa isModified()
    \sa filePath()
    \sa displayName()
*/

/*!
    \fn Core::IDocument::contentsChanged()

    This signal is emitted when the document's content changes.

    \sa contents()
*/

/*!
    \fn Core::IDocument::mimeTypeChanged()

    This signal is emitted when the document content's MIME type changes.

    \sa mimeType()
*/

/*!
    \fn Core::IDocument::aboutToReload()

    This signal is emitted before the document is reloaded from the backing
    file.

    \sa reload()
*/

/*!
    \fn Core::IDocument::reloadFinished(bool success)

    This signal is emitted after the document is reloaded from the backing
    file, or if reloading failed.

    The success state is passed in \a success.

    \sa reload()
*/

/*!
    \fn Core::IDocument::aboutToSave(const Utils::FilePath &filePath, bool autoSave)

    This signal is emitted before the document is saved to \a filePath.

    \a autoSave indicates whether this save was triggered by the auto save timer.

    \sa save()
*/

/*!
    \fn Core::IDocument::saved(const Utils::FilePath &filePath, bool autoSave)

    This signal is emitted after the document was saved to \a filePath.

    \a autoSave indicates whether this save was triggered by the auto save timer.

    \sa save()
*/

/*!
    \fn Core::IDocument::filePathChanged(const Utils::FilePath &oldName, const Utils::FilePath &newName)

    This signal is emitted after the file path changed from \a oldName to \a
    newName.

    \sa filePath()
*/

using namespace Utils;

namespace Core {
namespace Internal {

class IDocumentPrivate
{
public:
    ~IDocumentPrivate()
    {
        delete infoBar;
    }

    QString mimeType;
    Utils::FilePath filePath;
    QString preferredDisplayName;
    QString uniqueDisplayName;
    Utils::FilePath autoSavePath;
    Utils::InfoBar *infoBar = nullptr;
    std::unique_ptr<MinimizableInfoBars> minimizableInfoBars;
    Id id;
    std::optional<bool> fileIsReadOnly;
    bool temporary = false;
    bool hasWriteWarning = false;
    bool restored = false;
    bool isSuspendAllowed = false;
};

} // namespace Internal

/*!
    Creates an IDocument with \a parent.

    \note Using the \a parent for ownership of the document is generally a
    bad idea if the IDocument is intended for use with IEditor. It is better to
    use shared ownership in that case.
*/
IDocument::IDocument(QObject *parent) : QObject(parent),
    d(new Internal::IDocumentPrivate)
{
}

/*!
    Destroys the IDocument.
    If there was an auto save file for this document, it is removed.

    \sa shouldAutoSave()
*/
IDocument::~IDocument()
{
    removeAutoSaveFile();
    delete d;
}

/*!
    \fn void IDocument::setId(Utils::Id id)

    Sets the ID for this document type to \a id. This is coupled with the
    corresponding IEditor implementation and the \l{IEditorFactory::id()}{id()}
    of the IEditorFactory. If the IDocument implementation only works with a
    single IEditor type, this is preferably set in the IDocuments's
    constructor.

    \sa id()
*/
void IDocument::setId(Id id)
{
    d->id = id;
}

/*!
    Returns the ID for this document type.

    \sa setId()
*/
Id IDocument::id() const
{
    QTC_CHECK(d->id.isValid());
    return d->id;
}

/*!
    The open() method is used to load the contents of a file when a document is
    opened in an editor.

    If the document is opened from an auto save file, \a realFilePath is the
    name of the auto save file that should be loaded, and \a filePath 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 filePath, and the document state be set to modified.

    If the editor is opened from a regular file, \a filePath and \a
    filePath are the same.

    Use \a errorString to return an error message if this document cannot
    handle the file contents.

    Returns whether the file was opened and read successfully.

    The default implementation does nothing and returns
    CannotHandle.

    \sa EditorManager::openEditor()
    \sa shouldAutoSave()
    \sa setFilePath()
*/
IDocument::OpenResult IDocument::open(QString *errorString, const Utils::FilePath &filePath, const Utils::FilePath &realFilePath)
{
    Q_UNUSED(errorString)
    Q_UNUSED(filePath)
    Q_UNUSED(realFilePath)
    return OpenResult::CannotHandle;
}

/*!
    Saves the contents of the document to the \a filePath on disk.

    If \a autoSave is \c true, the saving is done for an auto-save, so the
    document should avoid cleanups or other operations that it does for
    user-requested saves.

    Use \a errorString to return an error message if saving failed.

    Returns whether saving was successful.

    If saving was successful saved is emitted.

    \sa shouldAutoSave()
    \sa aboutToSave()
    \sa saved()
*/
bool IDocument::save(QString *errorString, const Utils::FilePath &filePath, bool autoSave)
{
    emit aboutToSave(filePath.isEmpty() ? this->filePath() : filePath, autoSave);
    const bool success = saveImpl(errorString, filePath, autoSave);
    if (success)
        emit saved(filePath.isEmpty() ? this->filePath() : filePath, autoSave);
    return success;
}

/*!
    Implementation of saving the contents of the document to the \a filePath on disk.

    If \a autoSave is \c true, the saving is done for an auto-save, so the
    document should avoid cleanups or other operations that it does for
    user-requested saves.

    Use \a errorString to return an error message if saving failed.

    Returns whether saving was successful.

    The default implementation does nothing and returns \c false.
*/
bool IDocument::saveImpl(QString *errorString, const Utils::FilePath &filePath, bool autoSave)
{
    Q_UNUSED(errorString)
    Q_UNUSED(filePath)
    Q_UNUSED(autoSave)
    return false;
}

/*!
    Returns the current contents of the document. The default implementation
    returns an empty QByteArray.

    \sa setContents()
    \sa contentsChanged()
*/
QByteArray IDocument::contents() const
{
    return QByteArray();
}

/*!
    The setContents() method is for example used by
    EditorManager::openEditorWithContents() to set the \a contents of this
    document.

    Returns whether setting the contents was successful.

    The default implementation does nothing and returns false.

    \sa contents()
    \sa EditorManager::openEditorWithContents()
*/
bool IDocument::setContents(const QByteArray &contents)
{
    Q_UNUSED(contents)
    return false;
}

/*!
    Returns the absolute path of the file that this document refers to. May be
    empty for documents that are not backed by a file.

    \sa setFilePath()
*/
const Utils::FilePath &IDocument::filePath() const
{
    return d->filePath;
}

/*!
    The reloadBehavior() method is used by the DocumentManager to ask what to
    do if the file backing this document has changed on disk.

    The \a trigger specifies if the change was triggered by some operation in
    \QC. The \a type specifies if the file changed permissions or contents, or
    was removed completely.

    Returns whether the user should be asked or the document should be
    reloaded silently.

    The default implementation requests a silent reload if only the permissions
    changed or if the contents have changed but the \a trigger is internal and
    the document is not modified.

    \sa isModified()
*/
IDocument::ReloadBehavior IDocument::reloadBehavior(ChangeTrigger trigger, ChangeType type) const
{
    if (type == TypeContents && trigger == TriggerInternal && !isModified())
        return BehaviorSilent;
    return BehaviorAsk;
}

/*!
    Reloads the document from the backing file when that changed on disk.

    If \a flag is FlagIgnore the file should not actually be loaded, but the
    document should reflect the change in its \l{isModified()}{modified state}.

    The \a type specifies whether only the file permissions changed or if the
    contents of the file changed.

    Use \a errorString to return an error message, if this document cannot
    handle the file contents.

    Returns if the file was reloaded successfully.

    The default implementation does nothing and returns \c true.

    Subclasses should emit aboutToReload() before, and reloadFinished() after
    reloading the file.

    \sa isModified()
    \sa aboutToReload()
    \sa reloadFinished()
    \sa changed()
*/
bool IDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type)
{
    Q_UNUSED(errorString)
    Q_UNUSED(flag)
    Q_UNUSED(type)
    return true;
}

/*!
    Updates the cached information about the read-only status of the backing file.
*/
void IDocument::checkPermissions()
{
    bool previousReadOnly = d->fileIsReadOnly.value_or(false);
    if (!filePath().isEmpty()) {
        d->fileIsReadOnly = !filePath().isWritableFile();
    } else {
        d->fileIsReadOnly = false;
    }
    if (previousReadOnly != *(d->fileIsReadOnly))
        emit changed();
}

/*!
    Returns whether the document should automatically be saved at a user-defined
    interval.

    The default implementation returns \c false.
*/
bool IDocument::shouldAutoSave() const
{
    return false;
}

/*!
    Returns whether the document has been modified after it was loaded from a
    file.

    The default implementation returns \c false. Re-implementations should emit
    changed() when this property changes.

    \sa changed()
*/
bool IDocument::isModified() const
{
    return false;
}

/*!
    Returns whether the document may be saved under a different file name.

    The default implementation returns \c false.

    \sa save()
*/
bool IDocument::isSaveAsAllowed() const
{
    return false;
}

/*!
    Returns whether the document may be suspended.

    The EditorManager can automatically suspend editors and its corresponding
    documents if the document is backed by a file, is not modified, and is not
    temporary. Suspended IEditor and IDocument instances are deleted and
    removed from memory, but are still visually accessible as if the document
    was still opened in \QC.

    The default is \c false.

    \sa setSuspendAllowed()
    \sa isModified()
    \sa isTemporary()
*/
bool IDocument::isSuspendAllowed() const
{
    return d->isSuspendAllowed;
}

/*!
    Sets whether the document may be suspended to \a value.

    \sa isSuspendAllowed()
*/
void IDocument::setSuspendAllowed(bool value)
{
    d->isSuspendAllowed = value;
}

/*!
    Returns whether the file backing this document is read-only, or \c false if
    the document is not backed by a file.
*/
bool IDocument::isFileReadOnly() const
{
    if (filePath().isEmpty())
        return false;
    if (!d->fileIsReadOnly)
        const_cast<IDocument *>(this)->checkPermissions();
    return d->fileIsReadOnly.value_or(false);
}

/*!
    Returns if the document is temporary, and should for example not be
    considered when saving or restoring the session state, or added to the recent
    files list.

    The default is \c false.

    \sa setTemporary()
*/
bool IDocument::isTemporary() const
{
    return d->temporary;
}

/*!
    Sets whether the document is \a temporary.

    \sa isTemporary()
*/
void IDocument::setTemporary(bool temporary)
{
    d->temporary = temporary;
}

/*!
    Returns a path to use for the \uicontrol{Save As} file dialog in case the
    document is not backed by a file.

    \sa fallbackSaveAsFileName()
*/
FilePath IDocument::fallbackSaveAsPath() const
{
    return {};
}

/*!
    Returns a file name to use for the \uicontrol{Save As} file dialog in case
    the document is not backed by a file.

    \sa fallbackSaveAsPath()
*/
QString IDocument::fallbackSaveAsFileName() const
{
    return QString();
}

/*!
    Returns the MIME type of the document content, if applicable.

    Subclasses should set this with setMimeType() after setting or loading
    content.

    The default MIME type is empty.

    \sa setMimeType()
    \sa mimeTypeChanged()
*/
QString IDocument::mimeType() const
{
    return d->mimeType;
}

/*!
    Sets the MIME type of the document content to \a mimeType.

    \sa mimeType()
*/
void IDocument::setMimeType(const QString &mimeType)
{
    if (d->mimeType != mimeType) {
        d->mimeType = mimeType;
        emit mimeTypeChanged();
    }
}

/*!
    \internal
*/
bool IDocument::autoSave(QString *errorString, const FilePath &filePath)
{
    if (!save(errorString, filePath, true))
        return false;
    d->autoSavePath = filePath;
    return true;
}

static const char kRestoredAutoSave[] = "RestoredAutoSave";

/*!
    \internal
*/
void IDocument::setRestoredFrom(const Utils::FilePath &path)
{
    d->autoSavePath = path;
    d->restored = true;
    Utils::InfoBarEntry info(Id(kRestoredAutoSave),
                             Tr::tr("File was restored from auto-saved copy. "
                                "Select Save to confirm or Revert to Saved to discard changes."));
    infoBar()->addInfo(info);
}

/*!
    \internal
*/
void IDocument::removeAutoSaveFile()
{
    if (!d->autoSavePath.isEmpty()) {
        QFile::remove(d->autoSavePath.toString());
        d->autoSavePath.clear();
        if (d->restored) {
            d->restored = false;
            infoBar()->removeInfo(Id(kRestoredAutoSave));
        }
    }
}

/*!
    \internal
*/
bool IDocument::hasWriteWarning() const
{
    return d->hasWriteWarning;
}

/*!
    \internal
*/
void IDocument::setWriteWarning(bool has)
{
    d->hasWriteWarning = has;
}

/*!
    Returns the document's Utils::InfoBar, which is shown at the top of an
    editor.
*/
Utils::InfoBar *IDocument::infoBar()
{
    if (!d->infoBar)
        d->infoBar = new Utils::InfoBar;
    return d->infoBar;
}

MinimizableInfoBars *IDocument::minimizableInfoBars()
{
    if (!d->minimizableInfoBars)
        d->minimizableInfoBars.reset(new Utils::MinimizableInfoBars(*infoBar()));
    return d->minimizableInfoBars.get();
}

/*!
    Sets the absolute \a filePath of the file that backs this document. The
    default implementation sets the file name and sends the filePathChanged() and
    changed() signals.

    \sa filePath()
    \sa filePathChanged()
    \sa changed()
*/
void IDocument::setFilePath(const Utils::FilePath &filePath)
{
    if (d->filePath == filePath)
        return;
    Utils::FilePath oldName = d->filePath;
    d->filePath = filePath;
    emit filePathChanged(oldName, d->filePath);
    emit changed();
}

/*!
    Returns the string to display for this document, for example in the
    \uicontrol{Open Documents} view and the documents drop down.

    The display name is one of the following, in order:

    \list 1
        \li Unique display name set by the document model
        \li Preferred display name set by the owner
        \li Base name of the document's file name
    \endlist

    \sa setPreferredDisplayName()
    \sa filePath()
    \sa changed()
*/
QString IDocument::displayName() const
{
    return d->uniqueDisplayName.isEmpty() ? plainDisplayName() : d->uniqueDisplayName;
}

/*!
    Sets the preferred display \a name for this document.

    \sa preferredDisplayName()
    \sa displayName()
 */
void IDocument::setPreferredDisplayName(const QString &name)
{
    if (name == d->preferredDisplayName)
        return;
    d->preferredDisplayName = name;
    emit changed();
}

/*!
    Returns the preferred display name for this document.

    The default preferred display name is empty, which means that the display
    name is preferably the file name of the file backing this document.

    \sa setPreferredDisplayName()
    \sa displayName()
*/
QString IDocument::preferredDisplayName() const
{
    return d->preferredDisplayName;
}

/*!
    \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;
}

/*!
    \internal
*/
QString IDocument::uniqueDisplayName() const
{
    return d->uniqueDisplayName;
}

} // namespace Core