1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "core_global.h"
29 
30 #include <utils/fileutils.h>
31 #include <utils/id.h>
32 
33 #include <QObject>
34 
35 namespace Utils {
36 class FilePath;
37 class InfoBar;
38 } // namespace Utils
39 
40 namespace Core {
41 
42 namespace Internal {
43 class IDocumentPrivate;
44 }
45 
46 class CORE_EXPORT IDocument : public QObject
47 {
48     Q_OBJECT
49 
50 public:
51     enum class OpenResult {
52         Success,
53         ReadError,
54         CannotHandle
55     };
56 
57     // This enum must match the indexes of the reloadBehavior widget
58     // in generalsettings.ui
59     enum ReloadSetting {
60         AlwaysAsk = 0,
61         ReloadUnmodified = 1,
62         IgnoreAll = 2
63     };
64 
65     enum ChangeTrigger {
66         TriggerInternal,
67         TriggerExternal
68     };
69 
70     enum ChangeType {
71         TypeContents,
72         TypeRemoved
73     };
74 
75     enum ReloadBehavior {
76         BehaviorAsk,
77         BehaviorSilent
78     };
79 
80     enum ReloadFlag {
81         FlagReload,
82         FlagIgnore
83     };
84 
85     IDocument(QObject *parent = nullptr);
86     ~IDocument() override;
87 
88     void setId(Utils::Id id);
89     Utils::Id id() const;
90 
91     virtual OpenResult open(QString *errorString, const Utils::FilePath &filePath, const Utils::FilePath &realFilePath);
92 
93     virtual bool save(QString *errorString, const Utils::FilePath &filePath = Utils::FilePath(), bool autoSave = false);
94 
95     virtual QByteArray contents() const;
96     virtual bool setContents(const QByteArray &contents);
97 
98     const Utils::FilePath &filePath() const;
99     virtual void setFilePath(const Utils::FilePath &filePath);
100     QString displayName() const;
101     void setPreferredDisplayName(const QString &name);
102     QString preferredDisplayName() const;
103     QString plainDisplayName() const;
104     void setUniqueDisplayName(const QString &name);
105     QString uniqueDisplayName() const;
106 
107     bool isFileReadOnly() const;
108     bool isTemporary() const;
109     void setTemporary(bool temporary);
110 
111     virtual QString fallbackSaveAsPath() const;
112     virtual QString fallbackSaveAsFileName() const;
113 
114     QString mimeType() const;
115     void setMimeType(const QString &mimeType);
116 
117     virtual bool shouldAutoSave() const;
118     virtual bool isModified() const;
119     virtual bool isSaveAsAllowed() const;
120     bool isSuspendAllowed() const;
121     void setSuspendAllowed(bool value);
122 
123     virtual ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
124     virtual bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
125 
126     void checkPermissions();
127 
128     bool autoSave(QString *errorString, const Utils::FilePath &filePath);
129     void setRestoredFrom(const Utils::FilePath &path);
130     void removeAutoSaveFile();
131 
132     bool hasWriteWarning() const;
133     void setWriteWarning(bool has);
134 
135     Utils::InfoBar *infoBar();
136 
137 signals:
138     // For meta data changes: file name, modified state, ...
139     void changed();
140 
141     // For changes in the contents of the document
142     void contentsChanged();
143 
144     void mimeTypeChanged();
145 
146     void aboutToReload();
147     void reloadFinished(bool success);
148 
149     void filePathChanged(const Utils::FilePath &oldName, const Utils::FilePath &newName);
150 
151 private:
152     Internal::IDocumentPrivate *d;
153 };
154 
155 } // namespace Core
156