filemanager.h 6.22 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12
13
14
15
16
17
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21
22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23
24
25
26
27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33
34
35
#ifndef FILEMANAGER_H
#define FILEMANAGER_H

hjk's avatar
hjk committed
36
#include <coreplugin/id.h>
con's avatar
con committed
37
38
39

#include <QtCore/QObject>
#include <QtCore/QStringList>
40
41
#include <QtCore/QPair>
#include <QtCore/QVariant>
con's avatar
con committed
42
43

QT_BEGIN_NAMESPACE
44
class QAction;
45
class QMainWindow;
46
class QMenu;
con's avatar
con committed
47
48
49
50
51
52
QT_END_NAMESPACE

namespace Core {

class IContext;
class IFile;
53
class IVersionControl;
con's avatar
con committed
54
55
56
57
58

class CORE_EXPORT FileManager : public QObject
{
    Q_OBJECT
public:
59
60
61
62
63
    enum FixMode {
        ResolveLinks,
        KeepLinks
    };

hjk's avatar
hjk committed
64
    typedef QPair<QString, Id> RecentFile;
65

66
67
    explicit FileManager(QMainWindow *ew);
    virtual ~FileManager();
con's avatar
con committed
68

69
70
    static FileManager *instance();

con's avatar
con committed
71
    // file pool to monitor
72
73
74
75
    static void addFiles(const QList<IFile *> &files, bool addWatcher = true);
    static void addFile(IFile *file, bool addWatcher = true);
    static bool removeFile(IFile *file);
    static QList<IFile *> modifiedFiles();
con's avatar
con committed
76

77
    static void renamedFile(const QString &from, const QString &to);
dt's avatar
dt committed
78

79
80
    static void expectFileChange(const QString &fileName);
    static void unexpectFileChange(const QString &fileName);
81

con's avatar
con committed
82
    // recent files
83
    static void addToRecentFiles(const QString &fileName, const Id &editorId = Id());
84
    Q_SLOT void clearRecentFiles();
85
    static QList<RecentFile> recentFiles();
86

87
    static void saveSettings();
dt's avatar
dt committed
88

con's avatar
con committed
89
    // current file
90
91
    static void setCurrentFile(const QString &filePath);
    static QString currentFile();
con's avatar
con committed
92
93

    // helper methods
94
    static QString fixFileName(const QString &fileName, FixMode fixmode);
con's avatar
con committed
95

96
    static bool saveFile(IFile *file, const QString &fileName = QString(), bool *isReadOnly = 0);
97

98
    static QStringList getOpenFileNames(const QString &filters,
99
100
                                 const QString path = QString(),
                                 QString *selectedFilter = 0);
101
    static QString getSaveFileName(const QString &title, const QString &pathIn,
102
                            const QString &filter = QString(), QString *selectedFilter = 0);
103
    static QString getSaveFileNameWithExtension(const QString &title, const QString &pathIn,
104
                                         const QString &filter);
105
    static QString getSaveAsFileName(IFile *file, const QString &filter = QString(),
106
                              QString *selectedFilter = 0);
con's avatar
con committed
107

108
109
    static QList<IFile *> saveModifiedFilesSilently(const QList<IFile *> &files, bool *cancelled = 0);
    static QList<IFile *> saveModifiedFiles(const QList<IFile *> &files,
hjk's avatar
hjk committed
110
                                     bool *cancelled = 0,
111
                                     const QString &message = QString(),
112
                                     const QString &alwaysSaveMessage = QString(),
113
                                     bool *alwaysSave = 0);
con's avatar
con committed
114

115

116
117
118
119
120
121
122
123
    // Helper to display a message dialog when encountering a read-only
    // file, prompting the user about how to make it writeable.
    enum ReadOnlyAction { RO_Cancel, RO_OpenVCS, RO_MakeWriteable, RO_SaveAs };
    static ReadOnlyAction promptReadOnlyFile(const QString &fileName,
                                             const IVersionControl *versionControl,
                                             QWidget *parent,
                                             bool displaySaveAsButton = false);

124
125
    static QString fileDialogLastVisitedDirectory();
    static void setFileDialogLastVisitedDirectory(const QString &);
126

127
    static QString fileDialogInitialDirectory();
128

129
130
    static bool useProjectsDirectory();
    static void setUseProjectsDirectory(bool);
131

132
133
    static QString projectsDirectory();
    static void setProjectsDirectory(const QString &);
134

135
136
    static void populateOpenWithMenu(QMenu *menu, const QString &fileName);

137
138
    /* Used to notify e.g. the code model to update the given files. Does *not*
       lead to any editors to reload or any other editor manager actions. */
139
    static void notifyFilesChangedInternally(const QStringList &files);
140

141
142
143
144
    static void executeOpenWithMenuAction(QAction *action);

public slots:
    void slotExecuteOpenWithMenuAction(QAction *action);
145

con's avatar
con committed
146
147
signals:
    void currentFileChanged(const QString &filePath);
148
149
150
    /* Used to notify e.g. the code model to update the given files. Does *not*
       lead to any editors to reload or any other editor manager actions. */
    void filesChangedInternally(const QStringList &files);
con's avatar
con committed
151
152
153
154
155
156
157
158
159
160

private slots:
    void fileDestroyed(QObject *obj);
    void checkForNewFileName();
    void checkForReload();
    void changedFile(const QString &file);
    void mainWindowActivated();
    void syncWithEditor(Core::IContext *context);
};

161
162
163
164
165
166
167
168
169
/*! The FileChangeBlocker blocks all change notifications to all IFile * that
    match the given filename. And unblocks in the destructor.

    To also reload the IFile in the destructor class set modifiedReload to true

  */
class CORE_EXPORT FileChangeBlocker
{
public:
Friedemann Kleint's avatar
Friedemann Kleint committed
170
    explicit FileChangeBlocker(const QString &fileName);
171
172
    ~FileChangeBlocker();
private:
Friedemann Kleint's avatar
Friedemann Kleint committed
173
    const QString m_fileName;
174
    Q_DISABLE_COPY(FileChangeBlocker)
175
176
};

con's avatar
con committed
177
178
} // namespace Core

179
180
Q_DECLARE_METATYPE(Core::FileManager::RecentFile)

con's avatar
con committed
181
#endif // FILEMANAGER_H