Commit 964236c8 authored by con's avatar con

Move some API around and make some IFile based instead of IEditor.

promptReadOnlyFile: EditorManager-->FileManager
fileFilters: EditorManager-->MimeDataBase
saveFile & saveFileAs --> IFile based
parent 5b5a33d0
......@@ -1161,30 +1161,6 @@ QString EditorManager::getOpenWithEditorId(const QString &fileName,
return selectedId;
}
static QString formatFileFilters(const Core::ICore *core, QString *selectedFilter = 0)
{
if (selectedFilter)
selectedFilter->clear();
// Compile list of filter strings, sort, and remove duplicates (different mime types might
// generate the same filter).
QStringList filters = core->mimeDatabase()->filterStrings();
if (filters.empty())
return QString();
filters.sort();
filters.erase(std::unique(filters.begin(), filters.end()), filters.end());
static const QString allFilesFilter =
QCoreApplication::translate("Core", Constants::ALL_FILES_FILTER);
if (selectedFilter)
*selectedFilter = allFilesFilter;
// Prepend all files filter (instead of appending to work around a bug in Qt/Mac).
filters.prepend(allFilesFilter);
return filters.join(QLatin1String(";;"));
}
IEditor *EditorManager::openEditor(const QString &fileName, const QString &editorId,
OpenEditorFlags flags, bool *newEditor)
{
......@@ -1279,7 +1255,7 @@ bool EditorManager::openExternalEditor(const QString &fileName, const QString &e
QStringList EditorManager::getOpenFileNames() const
{
QString selectedFilter;
const QString &fileFilters = formatFileFilters(m_d->m_core, &selectedFilter);
const QString &fileFilters = m_d->m_core->mimeDatabase()->allFiltersString(&selectedFilter);
return ICore::instance()->fileManager()->getOpenFileNames(fileFilters,
QString(), &selectedFilter);
}
......@@ -1379,23 +1355,23 @@ void EditorManager::restoreEditorState(IEditor *editor)
bool EditorManager::saveEditor(IEditor *editor)
{
return saveFile(editor);
return saveFile(editor->file());
}
bool EditorManager::saveFile(IEditor *editor)
bool EditorManager::saveFile(IFile *fileParam)
{
if (!editor)
editor = currentEditor();
if (!editor)
IFile *file = fileParam;
if (!file && currentEditor())
file = currentEditor()->file();
if (!file)
return false;
IFile *file = editor->file();
file->checkPermissions();
const QString &fileName = file->fileName();
if (fileName.isEmpty())
return saveFileAs(editor);
return saveFileAs(file);
bool success = false;
......@@ -1406,7 +1382,7 @@ bool EditorManager::saveFile(IEditor *editor)
if (!success) {
MakeWritableResult answer =
makeEditorWritable(editor);
makeFileWritable(file);
if (answer == Failed)
return false;
if (answer == SavedAs)
......@@ -1419,74 +1395,31 @@ bool EditorManager::saveFile(IEditor *editor)
m_d->m_core->fileManager()->unblockFileChange(file);
}
if (success && !editor->isTemporary())
m_d->m_core->fileManager()->addToRecentFiles(editor->file()->fileName());
return success;
}
EditorManager::ReadOnlyAction
EditorManager::promptReadOnlyFile(const QString &fileName,
const IVersionControl *versionControl,
QWidget *parent,
bool displaySaveAsButton)
{
// Version Control: If automatic open is desired, open right away.
bool promptVCS = false;
if (versionControl && versionControl->supportsOperation(IVersionControl::OpenOperation)) {
if (versionControl->settingsFlags() & IVersionControl::AutoOpen)
return RO_OpenVCS;
promptVCS = true;
if (success) {
addFileToRecentFiles(file);
}
// Create message box.
QMessageBox msgBox(QMessageBox::Question, tr("File is Read Only"),
tr("The file <i>%1</i> is read only.").arg(QDir::toNativeSeparators(fileName)),
QMessageBox::Cancel, parent);
QPushButton *vcsButton = 0;
if (promptVCS)
vcsButton = msgBox.addButton(tr("Open with VCS (%1)").arg(versionControl->displayName()), QMessageBox::AcceptRole);
QPushButton *makeWritableButton = msgBox.addButton(tr("Make writable"), QMessageBox::AcceptRole);
QPushButton *saveAsButton = 0;
if (displaySaveAsButton)
saveAsButton = msgBox.addButton(tr("Save as ..."), QMessageBox::ActionRole);
msgBox.setDefaultButton(vcsButton ? vcsButton : makeWritableButton);
msgBox.exec();
QAbstractButton *clickedButton = msgBox.clickedButton();
if (clickedButton == vcsButton)
return RO_OpenVCS;
if (clickedButton == makeWritableButton)
return RO_MakeWriteable;
if (clickedButton == saveAsButton)
return RO_SaveAs;
return RO_Cancel;
return success;
}
MakeWritableResult
EditorManager::makeEditorWritable(IEditor *editor)
EditorManager::makeFileWritable(IFile *file)
{
if (!editor || !editor->file())
if (!file)
return Failed;
QString directory = QFileInfo(editor->file()->fileName()).absolutePath();
QString directory = QFileInfo(file->fileName()).absolutePath();
IVersionControl *versionControl = m_d->m_core->vcsManager()->findVersionControlForDirectory(directory);
IFile *file = editor->file();
const QString &fileName = file->fileName();
switch (promptReadOnlyFile(fileName, versionControl, m_d->m_core->mainWindow(), true)) {
case RO_OpenVCS:
switch (FileManager::promptReadOnlyFile(fileName, versionControl, m_d->m_core->mainWindow(), true)) {
case FileManager::RO_OpenVCS:
if (!versionControl->vcsOpen(fileName)) {
QMessageBox::warning(m_d->m_core->mainWindow(), tr("Failed!"), tr("Could not open the file for editing with SCC."));
return Failed;
}
file->checkPermissions();
return OpenedWithVersionControl;
case RO_MakeWriteable: {
case FileManager::RO_MakeWriteable: {
const bool permsOk = QFile::setPermissions(fileName, QFile::permissions(fileName) | QFile::WriteUser);
if (!permsOk) {
QMessageBox::warning(m_d->m_core->mainWindow(), tr("Failed!"), tr("Could not set permissions to writable."));
......@@ -1495,23 +1428,23 @@ EditorManager::makeEditorWritable(IEditor *editor)
}
file->checkPermissions();
return MadeWritable;
case RO_SaveAs :
return saveFileAs(editor) ? SavedAs : Failed;
case RO_Cancel:
case FileManager::RO_SaveAs :
return saveFileAs(file) ? SavedAs : Failed;
case FileManager::RO_Cancel:
break;
}
return Failed;
}
bool EditorManager::saveFileAs(IEditor *editor)
bool EditorManager::saveFileAs(IFile *fileParam)
{
if (!editor)
editor = currentEditor();
if (!editor)
IFile *file = fileParam;
if (!file && currentEditor())
file = currentEditor()->file();
if (!file)
return false;
IFile *file = editor->file();
const QString &filter = formatFileFilters(m_d->m_core);
const QString &filter = m_d->m_core->mimeDatabase()->allFiltersString();
QString selectedFilter =
m_d->m_core->mimeDatabase()->findByFile(QFileInfo(file->fileName())).filterString();
const QString &absoluteFilePath =
......@@ -1519,7 +1452,9 @@ bool EditorManager::saveFileAs(IEditor *editor)
if (absoluteFilePath.isEmpty())
return false;
if (absoluteFilePath != file->fileName()) {
// close existing editors for the new file name
const QList<IEditor *> existList = editorsForFileName(absoluteFilePath);
if (!existList.isEmpty()) {
closeEditors(existList, false);
......@@ -1537,13 +1472,27 @@ bool EditorManager::saveFileAs(IEditor *editor)
// a good way out either (also the undo stack would be lost). Perhaps the best is to
// re-think part of the editors design.
if (success && !editor->isTemporary())
m_d->m_core->fileManager()->addToRecentFiles(file->fileName());
if (success) {
addFileToRecentFiles(file);
}
updateActions();
return success;
}
void EditorManager::addFileToRecentFiles(IFile *file)
{
bool isTemporary = true;
QList<IEditor *> editors = editorsForFile(file);
foreach (IEditor *editor, editors) {
if (!editor->isTemporary()) {
isTemporary = false;
break;
}
}
if (!isTemporary)
m_d->m_core->fileManager()->addToRecentFiles(file->fileName());
}
void EditorManager::gotoNextDocHistory()
{
OpenEditorsWindow *dialog = windowPopup();
......@@ -1573,7 +1522,7 @@ void EditorManager::gotoPreviousDocHistory()
void EditorManager::makeCurrentEditorWritable()
{
if (IEditor* curEditor = currentEditor())
makeEditorWritable(curEditor);
makeFileWritable(curEditor->file());
}
void EditorManager::updateWindowTitle()
......
......@@ -152,7 +152,7 @@ public:
bool closeEditors(const QList<IEditor *> &editorsToClose, bool askAboutModifiedEditors = true);
MakeWritableResult makeEditorWritable(IEditor *editor);
MakeWritableResult makeFileWritable(IFile *file);
QByteArray saveState() const;
bool restoreState(const QByteArray &state);
......@@ -197,15 +197,6 @@ public:
QTextCodec *defaultTextEncoding() const;
// 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);
static qint64 maxTextFileSize();
void setWindowTitleAddition(const QString &addition);
......@@ -222,8 +213,8 @@ public slots:
bool closeAllEditors(bool askAboutModifiedEditors = true);
void openInExternalEditor();
bool saveFile(Core::IEditor *editor = 0);
bool saveFileAs(Core::IEditor *editor = 0);
bool saveFile(Core::IFile *file = 0);
bool saveFileAs(Core::IFile *file = 0);
void revertToSaved();
void closeEditor();
void closeOtherEditors();
......@@ -272,6 +263,7 @@ private:
void emptyView(Core::Internal::EditorView *view);
Core::Internal::EditorView *currentEditorView() const;
IEditor *pickUnusedEditor() const;
void addFileToRecentFiles(IFile *file);
static EditorManager *m_instance;
EditorManagerPrivate *m_d;
......
......@@ -311,7 +311,7 @@ void EditorToolBar::listContextMenu(QPoint pos)
void EditorToolBar::makeEditorWritable()
{
if (currentEditor())
ICore::instance()->editorManager()->makeEditorWritable(currentEditor());
ICore::instance()->editorManager()->makeFileWritable(currentEditor()->file());
}
void EditorToolBar::setCanGoBack(bool canGoBack)
......
......@@ -53,6 +53,7 @@
#include <QtGui/QFileDialog>
#include <QtGui/QMessageBox>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
/*!
\class Core::FileManager
......@@ -107,8 +108,9 @@ struct FileState
struct FileManagerPrivate {
explicit FileManagerPrivate(QObject *q, QMainWindow *mw);
explicit FileManagerPrivate(FileManager *q, QMainWindow *mw);
static FileManager *m_instance;
QMap<QString, FileState> m_states;
QStringList m_changedFiles;
QList<IFile *> m_filesWithoutWatch;
......@@ -133,7 +135,9 @@ struct FileManagerPrivate {
IFile *m_blockedIFile;
};
FileManagerPrivate::FileManagerPrivate(QObject *q, QMainWindow *mw) :
FileManager *FileManagerPrivate::m_instance = 0;
FileManagerPrivate::FileManagerPrivate(FileManager *q, QMainWindow *mw) :
m_mainWindow(mw),
m_fileWatcher(new QFileSystemWatcher(q)),
m_blockActivated(false),
......@@ -145,6 +149,7 @@ FileManagerPrivate::FileManagerPrivate(QObject *q, QMainWindow *mw) :
#endif
m_blockedIFile(0)
{
m_instance = q;
q->connect(m_fileWatcher, SIGNAL(fileChanged(QString)),
q, SLOT(changedFile(QString)));
#ifdef Q_OS_UNIX
......@@ -177,6 +182,11 @@ FileManager::~FileManager()
delete d;
}
FileManager *FileManager::instance()
{
return Internal::FileManagerPrivate::m_instance;
}
/*!
\fn bool FileManager::addFiles(const QList<IFile *> &files, bool addWatcher)
......@@ -189,7 +199,7 @@ void FileManager::addFiles(const QList<IFile *> &files, bool addWatcher)
if (!addWatcher) {
// We keep those in a separate list
foreach(IFile *file, files) {
foreach (IFile *file, files) {
if (file && !d->m_filesWithoutWatch.contains(file)) {
connect(file, SIGNAL(destroyed(QObject *)), this, SLOT(fileDestroyed(QObject *)));
d->m_filesWithoutWatch.append(file);
......@@ -300,7 +310,7 @@ void FileManager::dump()
information to avoid annoying the user with "file has been removed"
popups.
*/
void FileManager::renamedFile(const QString &from, QString &to)
void FileManager::renamedFile(const QString &from, const QString &to)
{
const QString &fixedFrom = fixFileName(from, KeepLinks);
......@@ -793,6 +803,47 @@ QStringList FileManager::getOpenFileNames(const QString &filters,
return files;
}
FileManager::ReadOnlyAction
FileManager::promptReadOnlyFile(const QString &fileName,
const IVersionControl *versionControl,
QWidget *parent,
bool displaySaveAsButton)
{
// Version Control: If automatic open is desired, open right away.
bool promptVCS = false;
if (versionControl && versionControl->supportsOperation(IVersionControl::OpenOperation)) {
if (versionControl->settingsFlags() & IVersionControl::AutoOpen)
return RO_OpenVCS;
promptVCS = true;
}
// Create message box.
QMessageBox msgBox(QMessageBox::Question, tr("File is Read Only"),
tr("The file <i>%1</i> is read only.").arg(QDir::toNativeSeparators(fileName)),
QMessageBox::Cancel, parent);
QPushButton *vcsButton = 0;
if (promptVCS)
vcsButton = msgBox.addButton(tr("Open with VCS (%1)").arg(versionControl->displayName()), QMessageBox::AcceptRole);
QPushButton *makeWritableButton = msgBox.addButton(tr("Make writable"), QMessageBox::AcceptRole);
QPushButton *saveAsButton = 0;
if (displaySaveAsButton)
saveAsButton = msgBox.addButton(tr("Save as ..."), QMessageBox::ActionRole);
msgBox.setDefaultButton(vcsButton ? vcsButton : makeWritableButton);
msgBox.exec();
QAbstractButton *clickedButton = msgBox.clickedButton();
if (clickedButton == vcsButton)
return RO_OpenVCS;
if (clickedButton == makeWritableButton)
return RO_MakeWriteable;
if (clickedButton == saveAsButton)
return RO_SaveAs;
return RO_Cancel;
}
void FileManager::changedFile(const QString &fileName)
{
......
......@@ -44,6 +44,7 @@ namespace Core {
class ICore;
class IContext;
class IFile;
class IVersionControl;
namespace Internal {
struct FileManagerPrivate;
......@@ -61,13 +62,15 @@ public:
explicit FileManager(QMainWindow *ew);
virtual ~FileManager();
static FileManager *instance();
// file pool to monitor
void addFiles(const QList<IFile *> &files, bool addWatcher = true);
void addFile(IFile *file, bool addWatcher = true);
void removeFile(IFile *file);
QList<IFile *> modifiedFiles() const;
void renamedFile(const QString &from, QString &to);
void renamedFile(const QString &from, const QString &to);
void blockFileChange(IFile *file);
void unblockFileChange(IFile *file);
......@@ -105,6 +108,14 @@ public:
bool *alwaysSave = 0);
// 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);
QString fileDialogLastVisitedDirectory() const;
void setFileDialogLastVisitedDirectory(const QString &);
......
......@@ -28,6 +28,7 @@
**************************************************************************/
#include "mimedatabase.h"
#include "coreconstants.h"
#include <utils/qtcassert.h>
......@@ -1303,6 +1304,29 @@ QStringList MimeDatabase::filterStrings() const
m_mutex.unlock();
return rc;
}
QString MimeDatabase::allFiltersString(QString *allFilesFilter) const
{
if (allFilesFilter)
allFilesFilter->clear();
// Compile list of filter strings, sort, and remove duplicates (different mime types might
// generate the same filter).
QStringList filters = filterStrings();
if (filters.empty())
return QString();
filters.sort();
filters.erase(std::unique(filters.begin(), filters.end()), filters.end());
static const QString allFiles =
QCoreApplication::translate("Core", Constants::ALL_FILES_FILTER);
if (allFilesFilter)
*allFilesFilter = allFiles;
// Prepend all files filter (instead of appending to work around a bug in Qt/Mac).
filters.prepend(allFiles);
return filters.join(QLatin1String(";;"));
}
QString MimeDatabase::preferredSuffixByType(const QString &type) const
{
......
......@@ -271,6 +271,8 @@ public:
friend QDebug operator<<(QDebug d, const MimeDatabase &mt);
// returns a string with all the possible file filters, for use with file dialogs
QString allFiltersString(QString *allFilesFilter = 0) const;
private:
MimeType findByFileUnlocked(const QFileInfo &f) const;
......
......@@ -937,14 +937,14 @@ bool Qt4PriFileNode::priFileWritable(const QString &path)
const QString dir = QFileInfo(path).dir().path();
Core::ICore *core = Core::ICore::instance();
Core::IVersionControl *versionControl = core->vcsManager()->findVersionControlForDirectory(dir);
switch (Core::EditorManager::promptReadOnlyFile(path, versionControl, core->mainWindow(), false)) {
case Core::EditorManager::RO_OpenVCS:
switch (Core::FileManager::promptReadOnlyFile(path, versionControl, core->mainWindow(), false)) {
case Core::FileManager::RO_OpenVCS:
if (!versionControl->vcsOpen(path)) {
QMessageBox::warning(core->mainWindow(), tr("Failed!"), tr("Could not open the file for edit with VCS."));
return false;
}
break;
case Core::EditorManager::RO_MakeWriteable: {
case Core::FileManager::RO_MakeWriteable: {
const bool permsOk = QFile::setPermissions(path, QFile::permissions(path) | QFile::WriteUser);
if (!permsOk) {
QMessageBox::warning(core->mainWindow(), tr("Failed!"), tr("Could not set permissions to writable."));
......@@ -952,8 +952,8 @@ bool Qt4PriFileNode::priFileWritable(const QString &path)
}
break;
}
case Core::EditorManager::RO_SaveAs:
case Core::EditorManager::RO_Cancel:
case Core::FileManager::RO_SaveAs:
case Core::FileManager::RO_Cancel:
return false;
}
return true;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment