File indexing completed on 2024-04-21 15:55:39

0001 /**************************************************************************
0002 *   Copyright (C) 2004 by Jeroen Wijnhout (Jeroen.Wijnhout@kdemail.net)   *
0003 *             (C) 2006-2018 by Michel Ludwig (michel.ludwig@kdemail.net)  *
0004 ***************************************************************************/
0005 
0006 /***************************************************************************
0007  *                                                                         *
0008  *   This program is free software; you can redistribute it and/or modify  *
0009  *   it under the terms of the GNU General Public License as published by  *
0010  *   the Free Software Foundation; either version 2 of the License, or     *
0011  *   (at your option) any later version.                                   *
0012  *                                                                         *
0013  ***************************************************************************/
0014 
0015 #ifndef KILEDOCUMENTKILEDOCMANAGER_H
0016 #define KILEDOCUMENTKILEDOCMANAGER_H
0017 
0018 #include <QDropEvent>
0019 #include <QObject>
0020 #include <QPointer>
0021 #include <QStringList>
0022 
0023 #include <KTextEditor/Editor>
0024 #include <KTextEditor/ModificationInterface>
0025 
0026 #include "kileconstants.h"
0027 #include "kileproject.h"
0028 #include "widgets/progressdialog.h"
0029 
0030 class QUrl;
0031 class KFileItem;
0032 class QProgressDialog;
0033 namespace KTextEditor {
0034 class Document;
0035 class View;
0036 }
0037 
0038 class TemplateItem;
0039 class KileInfo;
0040 class KileProjectItem;
0041 
0042 namespace KileParser {
0043 class ParserOutput;
0044 }
0045 
0046 namespace KileDocument
0047 {
0048 
0049 class Info;
0050 class TextInfo;
0051 
0052 /**
0053  * The design of the document manager is based on the following invariants:
0054  * - Several projects can be open simultaneously
0055  * - For every URL there can only be at most one associated KileDocument::Info* object, but
0056  *   there can be several KileProject* items (each one belonging to a different project)
0057  * - It is guaranteed that every project item that represents textual data contains a TextInfo* object
0058  **/
0059 
0060 class Manager : public QObject
0061 {
0062     Q_OBJECT
0063 public:
0064     explicit Manager(KileInfo *info, QObject *parent = Q_NULLPTR, const char *name = Q_NULLPTR);
0065     ~Manager();
0066 
0067     void readConfig();
0068     void writeConfig();
0069 
0070 public Q_SLOTS:
0071     KTextEditor::View* createNewJScript();
0072     KTextEditor::View* createNewLaTeXDocument();
0073 
0074 //files
0075     void newDocumentStatus(KTextEditor::Document *doc);
0076 
0077     /**
0078      * Creates a new file on disk.
0079      **/
0080     void fileNew(const QUrl&);
0081     void fileNewScript();
0082     void fileNew(KileDocument::Type type = LaTeX);
0083 
0084     void fileSelected(const QUrl&);
0085     void fileSelected(const KileProjectItem *item);
0086     void fileSelected(const KFileItem& file);
0087 
0088     void fileOpen();
0089     inline TextInfo* fileOpen(const QString& localFile, const QString& encoding = QString(), int index = -1)
0090     {
0091         return fileOpen(QUrl::fromLocalFile(localFile), encoding, index);
0092     }
0093     TextInfo* fileOpen(const QUrl &url, const QString& encoding = QString(), int index = -1);
0094 
0095     bool fileSave(KTextEditor::View* = Q_NULLPTR);
0096     bool fileSaveAs(KTextEditor::View* = Q_NULLPTR);
0097 
0098     void saveURL(const QUrl&);
0099     bool fileSaveAll(bool disUntitled = false);
0100 
0101     void fileSaveCompiledDocument();
0102 
0103     bool fileCloseAllOthers(KTextEditor::View *view = Q_NULLPTR);
0104     bool fileCloseAll();
0105     bool fileClose(const QUrl &url);
0106     bool fileClose(KTextEditor::View *view = Q_NULLPTR);
0107     bool fileClose(KTextEditor::Document *doc, bool closingproject = false);
0108 
0109 //templates
0110     KTextEditor::View* loadTemplate(TemplateItem*);
0111     void createTemplate();
0112     void removeTemplate();
0113     void replaceTemplateVariables(QString &line);
0114 
0115 //projects
0116     void projectNew();
0117     void projectOpen();
0118 
0119     /**
0120      * @param openProjectItemViews Opens project files in the editor iff openProjectItemViews is set to 'true'.
0121      **/
0122     void projectOpen(const QUrl&, int step = 0, int max = 1, bool openProjectItemViews = true);
0123 
0124     /**
0125      * Saves the state of the project, if @param project is zero, the active project is saved.
0126      **/
0127     void projectSave(KileProject* project = Q_NULLPTR);
0128     void projectAddFiles(const QUrl&);
0129     void projectAddFiles(KileProject* project = Q_NULLPTR,const QUrl &url = QUrl());
0130     void toggleArchive(KileProjectItem *);
0131     void buildProjectTree(KileProject *project = Q_NULLPTR);
0132     void buildProjectTree(const QUrl&);
0133     void projectOptions(const QUrl&);
0134     void projectOptions(KileProject *project = Q_NULLPTR);
0135     bool projectClose(const QUrl &url = QUrl());
0136     bool projectCloseAll();
0137 
0138     void projectShow();
0139     void projectRemoveFiles();
0140     void projectShowFiles();
0141     void projectAddFile(QString filename, bool graphics=false);
0142     void projectOpenAllFiles();
0143     void projectOpenAllFiles(const QUrl&);
0144 
0145     KileProject* selectProject(const QString&);
0146 
0147     void addProject(KileProject *project);
0148     void addToProject(const QUrl&);
0149     void addToProject(KileProject*, const QUrl&);
0150     void removeFromProject(KileProjectItem *item);
0151     void storeProjectItem(KileProjectItem *item, KTextEditor::Document *doc);
0152 
0153     void cleanUpTempFiles(const QUrl &url, bool silent = false);
0154 
0155     void openDroppedURLs(QDropEvent *e);
0156 
0157     void reloadXMLOnAllDocumentsAndViews();
0158 
0159     void handleParsingComplete(const QUrl &url, KileParser::ParserOutput* output);
0160 
0161 Q_SIGNALS:
0162     void projectTreeChanged(const KileProject*);
0163     void closingDocument(KileDocument::Info*);
0164     void documentInfoCreated(KileDocument::Info*);
0165 
0166     void updateStructure(bool needToParse, KileDocument::Info*);
0167     void updateModeStatus();
0168     void updateReferences(KileDocument::Info*);
0169 
0170     void documentModificationStatusChanged(KTextEditor::Document*, bool, KTextEditor::ModificationInterface::ModifiedOnDiskReason);
0171     void documentUrlChanged(KTextEditor::Document*);
0172     void documentNameChanged(KTextEditor::Document*);
0173     void documentReadWriteStateChanged(KTextEditor::Document*);
0174 
0175     void addToRecentFiles(const QUrl&);
0176     void addToRecentProjects(const QUrl&);
0177     void removeFromRecentProjects(const QUrl&);
0178 
0179     void startWizard();
0180 
0181     void removeFromProjectView(const QUrl&);
0182     void removeFromProjectView(const KileProject*);
0183     void removeItemFromProjectView(const KileProjectItem*, bool);
0184     void addToProjectView(const QUrl&);
0185     void addToProjectView(KileProjectItem *item);
0186     void addToProjectView(const KileProject*);
0187 
0188     void documentSavedAs(KTextEditor::View*, KileDocument::TextInfo*);
0189 
0190     void projectOpened(KileProject *project);
0191     void documentOpened(KileDocument::TextInfo *textInfo);
0192 
0193 public:
0194     bool isOpeningFile();
0195 
0196     KTextEditor::Editor* getEditor();
0197 
0198     QList<KileProject*> projects() {
0199         return m_projects;
0200     }
0201     QList<TextInfo*> textDocumentInfos() {
0202         return m_textInfoList;
0203     }
0204 
0205     KTextEditor::Document* docFor(const QUrl &url);
0206 
0207     TextInfo* getInfo() const;
0208 
0209     inline TextInfo* textInfoFor(const QString& localFile)
0210     {
0211         return textInfoFor(QUrl::fromLocalFile(localFile));
0212     }
0213 
0214     TextInfo* textInfoFor(const QUrl &url);
0215     TextInfo* textInfoFor(KTextEditor::Document* doc) const;
0216 
0217     QUrl urlFor(TextInfo* textInfo);
0218 
0219     void updateInfos();
0220 
0221     KileProject* projectForMember(const QUrl &memberUrl);
0222     KileProject* projectFor(const QUrl &projecturl);
0223     KileProject* projectFor(const QString & name);
0224     KileProject* activeProject();
0225     bool isProjectOpen();
0226     void updateProjectReferences(KileProject *project);
0227     QStringList getProjectFiles();
0228 
0229     KileProjectItem* activeProjectItem();
0230     /**
0231      * Finds the project item for the file with URL @param url.
0232      * @returns a pointer to the project item, 0 if this file does not belong to a project
0233      **/
0234     KileProjectItem* itemFor(const QUrl &url, KileProject *project = Q_NULLPTR) const;
0235     KileProjectItem* itemFor(TextInfo *docinfo, KileProject *project = Q_NULLPTR) const;
0236     KileProjectItem* selectProjectFileItem(const QString &label);
0237     QList<KileProjectItem*> selectProjectFileItems(const QString &label);
0238 
0239     /**
0240      * The next method returns a list as several projects can be open simultaneously, i.e.
0241      * one URL can be associated with several project item belonging to different projects.
0242      **/
0243     QList<KileProjectItem*> itemsFor(Info *docinfo) const;
0244     QList<KileProjectItem*> itemsFor(const QUrl &url) const;
0245 
0246 protected:
0247     /**
0248      * @param openProjectItemViews Opens project files in the editor iff openProjectItemViews is set to 'true'.
0249      **/
0250     void projectOpenItem(KileProjectItem *item, bool openProjectItemViews = true);
0251     void createTextInfoForProjectItem(KileProjectItem *item);
0252 
0253     void trashDoc(TextInfo *docinfo, KTextEditor::Document *doc = Q_NULLPTR);
0254 
0255     TextInfo* createTextDocumentInfo(KileDocument::Type type, const QUrl &url, const QUrl& baseDirectory = QUrl());
0256     void recreateTextDocumentInfo(TextInfo *oldinfo);
0257 
0258     /**
0259      * Tries to remove and delete a TextInfo object. The TextInfo object will only be deleted if it isn't referenced
0260      * by any project item or if it is only referenced by a project that should be closed.
0261      * @param closingproject Indicates whether the TextInfo object should be removed as part of a project close
0262      *                       operation.
0263      * @warning This method does not close or delete any Kate documents that are associated with the TextInfo object !
0264      **/
0265     bool removeTextDocumentInfo(TextInfo *docinfo, bool closingproject = false);
0266     KTextEditor::Document* createDocument(const QUrl &url, TextInfo *docinfo, const QString& encoding, const QString& mode, const QString& highlight);
0267 
0268     /**
0269      *  Creates a document with the specified text.
0270      *
0271      *  @param extension The extension of the file that should be created without leading "."
0272      **/
0273     KTextEditor::View* createDocumentWithText(const QString& text, KileDocument::Type type = KileDocument::Text, const QString& extension = QString(), const QUrl &baseDirectory = QUrl());
0274 
0275     KTextEditor::View* loadText(KileDocument::Type type, const QUrl &url, const QString& encoding = QString(), bool create = true, const QString& mode = QString(), const QString& highlight = QString(), const QString &text = QString(), int index = -1, const QUrl& baseDirectory = QUrl());
0276     KTextEditor::View* loadItem(KileDocument::Type type, KileProjectItem *item, const QString& text = QString(), bool openProjectItemViews = true);
0277 
0278     QStringList loadTextURLContents(const QUrl &url, const QString& encoding);
0279 
0280 private:
0281     KTextEditor::Editor         *m_editor;
0282     QList<TextInfo*>            m_textInfoList;
0283     KileInfo                *m_ki;
0284     QList<KileProject*>         m_projects;
0285     QPointer<KileWidget::ProgressDialog>    m_progressDialog;
0286     unsigned int                m_autoSaveLock;
0287     bool                    m_currentlySavingAll, m_currentlyOpeningFile;
0288 
0289     void dontOpenWarning(KileProjectItem *item, const QString &action, const QString &filetype);
0290     void cleanupDocumentInfoForProjectItems(KileDocument::Info *info);
0291 
0292     void createProgressDialog();
0293 
0294     QStringList autosaveWarnings;
0295 
0296     void loadDocumentAndViewSettings(KileDocument::TextInfo *textInfo);
0297     void saveDocumentAndViewSettings(KileDocument::TextInfo *textInfo);
0298     KConfigGroup configGroupForDocumentSettings(KTextEditor::Document *doc) const;
0299     QString configGroupNameForDocumentSettings(const QUrl &url) const;
0300     KConfigGroup configGroupForViewSettings(KTextEditor::Document *doc, int viewIndex) const;
0301     QString configGroupNameForViewSettings(const QUrl &url, int viewIndex) const;
0302     void deleteDocumentAndViewSettingsGroups(const QUrl &url);
0303 
0304     bool checkForFileOverwritePermission(const QUrl& url);
0305 };
0306 
0307 }
0308 
0309 #endif