File indexing completed on 2024-03-24 16:41:38

0001 /***************************************************************************************
0002     begin                : Fri Aug 1 2003
0003     copyright            : (C) 2003 by Jeroen Wijnhout (Jeroen.Wijnhout@kdemail.net)
0004                            (C) 2009-2022 by Michel Ludwig (michel.ludwig@kdemail.net)
0005  ***************************************************************************************/
0006 
0007 /***************************************************************************
0008  *                                                                         *
0009  *   This program is free software; you can redistribute it and/or modify  *
0010  *   it under the terms of the GNU General Public License as published by  *
0011  *   the Free Software Foundation; either version 2 of the License, or     *
0012  *   (at your option) any later version.                                   *
0013  *                                                                         *
0014  ***************************************************************************/
0015 #ifndef KILEPROJECT_H
0016 #define KILEPROJECT_H
0017 
0018 #include <QDir>
0019 #include <QFileInfo>
0020 #include <QList>
0021 #include <QObject>
0022 #include <QRegExp>
0023 
0024 #include <KConfig>
0025 #include <QUrl>
0026 #include <KTextEditor/View>
0027 
0028 #include "kiledebug.h"
0029 #include "kileversion.h"
0030 #include "livepreview_utils.h"
0031 #include "outputinfo.h"
0032 
0033 class QString;
0034 class QStringList;
0035 namespace KileDocument {
0036 class Info;
0037 class TextInfo;
0038 class Extensions;
0039 }
0040 
0041 /**
0042  * KileProjectItem
0043  **/
0044 class KileProject;
0045 class KileProjectItem : public QObject
0046 {
0047     Q_OBJECT
0048 
0049 public:
0050     explicit KileProjectItem(KileProject *project = 0, const QUrl &url = QUrl(), int type = Source);
0051     ~KileProjectItem() {
0052         KILE_DEBUG_MAIN << "DELETING PROJITEM " << m_path << Qt::endl;
0053     }
0054 
0055     bool operator==(const KileProjectItem& item) {
0056         return m_url  == item.url();
0057     }
0058 
0059     enum Type { ProjectFile = 0, Source, Package, Image, Bibliography, Other /* should be the last item*/ };
0060 
0061     int type() const {
0062         return m_type;
0063     }
0064     void setType(int type) {
0065         m_type = type;
0066     }
0067 
0068     bool archive() const {
0069         return m_archive;
0070     }
0071     void setArchive(bool ar) {
0072         m_archive = ar;
0073     }
0074 
0075     void setInfo(KileDocument::TextInfo * docinfo);
0076     KileDocument::TextInfo* getInfo() const {
0077         return m_docinfo;
0078     }
0079 
0080     KileProject* project() const {
0081         return m_project;
0082     }
0083 
0084     /**
0085      * @returns absolute URL of this item
0086      **/
0087     const QUrl &url() const {
0088         return m_url;
0089     }
0090 
0091     /**
0092      * @returns path of this item relative to the project file
0093      **/
0094     const QString& path() const {
0095         return m_path;
0096     }
0097 
0098     bool isOpen() const {
0099         return m_bOpen;
0100     }
0101     void setOpenState(bool state) {
0102         m_bOpen = state;
0103     }
0104 
0105     const QString& encoding() const {
0106         return m_encoding;
0107     }
0108     void setEncoding(const QString& encoding) {
0109         m_encoding = encoding;
0110     }
0111 
0112     const QString& highlight() {
0113         return m_highlight;
0114     }
0115     void setHighlight(const QString& highlight) {
0116         m_highlight = highlight;
0117     }
0118 
0119     const QString& mode() {
0120         return m_mode;
0121     }
0122     void setMode(const QString& mode) {
0123         m_mode = mode;
0124     }
0125 
0126     int order() const {
0127         return m_order;
0128     }
0129     void setOrder(int i);
0130 
0131     //project tree functions
0132     void setParent(KileProjectItem * item);
0133 
0134     void load();
0135     void save();
0136 
0137     void loadDocumentAndViewSettings();
0138     void saveDocumentAndViewSettings();
0139 
0140 protected:
0141     void setChild(KileProjectItem *item) {
0142         m_child = item;
0143     }
0144     void setSibling(KileProjectItem *item) {
0145         m_sibling = item;
0146     }
0147 
0148     void loadViewSettings(KTextEditor::View *view, int viewIndex);
0149     void saveViewSettings(KTextEditor::View *view, int viewIndex);
0150 
0151     void loadDocumentSettings(KTextEditor::Document *document);
0152     void saveDocumentSettings(KTextEditor::Document *document);
0153 
0154 public:
0155     KileProjectItem* parent() const {
0156         return m_parent;
0157     }
0158     KileProjectItem* firstChild() const {
0159         return m_child;
0160     }
0161     KileProjectItem* sibling() const {
0162         return m_sibling;
0163     }
0164 
0165     void allChildren(QList<KileProjectItem*>* list) const;
0166 
0167     void print(int level);
0168 
0169 public Q_SLOTS:
0170     /**
0171      * @warning Does nothing if "url" is empty !
0172      **/
0173     void changeURL(const QUrl &url);
0174     void changePath(const QString& path) {
0175         m_path = path;
0176     }
0177 
0178 private Q_SLOTS:
0179     void slotChangeURL(KileDocument::Info* info, const QUrl &url);
0180 
0181 Q_SIGNALS:
0182     void urlChanged(KileProjectItem*);
0183 
0184 private:
0185     KileProject     *m_project;
0186     QUrl            m_url;
0187     QString         m_path;
0188     QString         m_encoding;
0189     QString         m_mode;
0190     QString         m_highlight;
0191     bool            m_bOpen, m_archive;
0192     int         m_type;
0193     KileDocument::TextInfo  *m_docinfo;
0194     KileProjectItem     *m_parent, *m_child, *m_sibling;
0195     int         m_order;
0196 };
0197 
0198 /**
0199  * KileProject
0200  **/
0201 class KileProject : public QObject, public KileTool::LivePreviewUserStatusHandler, public LaTeXOutputHandler
0202 {
0203     Q_OBJECT
0204     friend class KileProjectItem;
0205 
0206 public:
0207     KileProject(const QString& name, const QUrl &url, KileDocument::Extensions *extensions);
0208     KileProject(const QUrl &url, KileDocument::Extensions *extensions);
0209 
0210     ~KileProject();
0211 
0212     void setName(const QString & name) {
0213         m_name = name;
0214         emit (nameChanged(name));
0215     }
0216     const QString& name() const {
0217         return m_name;
0218     }
0219 
0220     void setMasterDocument(const QString & master);
0221     const QString& masterDocument() const {
0222         return m_masterDocument;
0223     }
0224 
0225     void setExtensions(KileProjectItem::Type type, const QString & ext);
0226     const QString & extensions(KileProjectItem::Type type) {
0227         return m_extensions[type-1];
0228     }
0229 
0230     void setDefaultGraphicExt(const QString & ext);
0231     const QString & defaultGraphicExt();
0232 
0233     void setQuickBuildConfig(const QString & cfg) {
0234         m_quickBuildConfig = cfg;
0235     }
0236     const QString & quickBuildConfig() {
0237         return m_quickBuildConfig;
0238     }
0239 
0240     void setLastDocument(const QUrl &url);
0241     const QUrl &lastDocument() const {
0242         return m_lastDocument;
0243     }
0244 
0245     void setMakeIndexOptions(const QString & opt) {
0246         m_makeIndexOptions = opt;
0247     }
0248     const QString & makeIndexOptions() {
0249         return m_makeIndexOptions;
0250     }
0251     void readMakeIndexOptions();
0252     void setUseMakeIndexOptions(bool use) {
0253         m_useMakeIndexOptions = use;
0254     }
0255     void writeUseMakeIndexOptions();
0256     bool useMakeIndexOptions() {
0257         return m_useMakeIndexOptions;
0258     }
0259 
0260     QUrl url() const {
0261         return m_projecturl;
0262     }
0263     void setURL(const QUrl &url ) {
0264         m_projecturl = url;
0265     }
0266     QUrl baseURL() const {
0267         return m_baseurl;
0268     }
0269 
0270     KileProjectItem* item(const QUrl &);
0271     KileProjectItem* item(const KileDocument::Info *info);
0272     QList<KileProjectItem*> items() {
0273         return m_projectItems;
0274     }
0275 
0276     KConfig *config() {
0277         return m_config;
0278     }
0279     KConfig *guiConfig() {
0280         return m_guiConfig;
0281     }
0282 
0283     bool contains(const QUrl&);
0284     bool contains(const KileDocument::Info *info);
0285     KileProjectItem *rootItem(KileProjectItem *) const;
0286     const QList<KileProjectItem*>& rootItems() const {
0287         return m_rootItems;
0288     }
0289     bool isInvalid() {
0290         return m_invalid;
0291     }
0292     QString archiveFileList() const;
0293 
0294     bool appearsToBeValidProjectFile();
0295 
0296     inline bool isOfCurrentVersion()
0297     {
0298         return (getProjectFileVersion() == KILE_PROJECTFILE_VERSION);
0299     }
0300 
0301     int getProjectFileVersion();
0302 
0303     bool migrateProjectFileToCurrentVersion();
0304 
0305     static inline QString getPathForGUISettingsProjectFile(const QString& projectFilePath)
0306     {
0307         QFileInfo fi(projectFilePath);
0308 
0309         return getPathForPrivateKileDirectory(fi) + QStringLiteral("/") + fi.fileName() + QStringLiteral(".gui");
0310     }
0311 
0312     static inline QString getPathForGUISettingsProjectFile(const QUrl& projectUrl)
0313     {
0314         return getPathForGUISettingsProjectFile(projectUrl.toLocalFile());
0315     }
0316 
0317     static inline QString getPathForPrivateKileDirectory(const QUrl& projectUrl)
0318     {
0319         return getPathForPrivateKileDirectory(projectUrl.toLocalFile());
0320     }
0321 
0322     static inline QString getPathForPrivateKileDirectory(const QFileInfo& projectFilePath)
0323     {
0324         return projectFilePath.dir().absoluteFilePath(".kile");
0325     }
0326 
0327     static inline QString getPathForPrivateKileDirectory(const QString& projectFilePath)
0328     {
0329         return getPathForPrivateKileDirectory(QFileInfo(projectFilePath));
0330     }
0331 
0332     static inline bool ensurePrivateKileDirectoryExists(const QUrl& projectUrl)
0333     {
0334         return QFileInfo(projectUrl.toLocalFile()).dir().mkpath(".kile");
0335     }
0336 
0337 Q_SIGNALS:
0338     void nameChanged(const QString &);
0339     void masterDocumentChanged(const QString &);
0340     void projectTreeChanged(const KileProject *);
0341     void projectItemAdded(KileProject *project, KileProjectItem *item);
0342     void projectItemRemoved(KileProject *project, KileProjectItem *item);
0343     void aboutToBeDestroyed(KileProject*);
0344 
0345 public Q_SLOTS:
0346     bool load();
0347     bool save();
0348 
0349     void add(KileProjectItem*);
0350     void remove(KileProjectItem*);
0351 
0352     void itemRenamed(KileProjectItem*);
0353 
0354     void buildProjectTree(); // moved to slots by tbraun
0355 
0356     //debugging
0357     void dump();
0358 
0359 Q_SIGNALS:
0360     void loadFile(const QUrl &url, const QString & encoding);
0361 
0362 private:
0363     bool migrateProjectFileToVersion3();
0364 
0365     void init(const QUrl &url);
0366     QString findRelativePath(const QUrl&);
0367     QString findRelativePath(const QString&);
0368 
0369     void setType(KileProjectItem *item);
0370     QString addBaseURL(const QString &path);
0371     QString removeBaseURL(const QString &path);
0372     void writeConfigEntry(const QString &key,const QString &standardExt,KileProjectItem::Type type);
0373 
0374     enum ConfigScope {
0375         ProjectFile,
0376         GUIFile
0377     };
0378 
0379     KConfigGroup configGroupForItem(KileProjectItem *item, ConfigScope scope) const;
0380     KConfigGroup configGroupForItemDocumentSettings(KileProjectItem *item) const;
0381     KConfigGroup configGroupForItemViewSettings(KileProjectItem *item, int viewIndex) const;
0382 
0383     void removeConfigGroupsForItem(KileProjectItem *item);
0384 
0385 private:
0386 
0387     QString     m_name, m_quickBuildConfig, m_defGraphicExt;
0388     QUrl        m_projecturl, m_baseurl, m_lastDocument;
0389     bool        m_invalid;
0390     QList<KileProjectItem*> m_rootItems;
0391     QList<KileProjectItem*> m_projectItems;
0392 
0393     QString     m_extensions[4];
0394     QRegExp     m_reExtensions[4];
0395 
0396     QString             m_masterDocument, m_makeIndexOptions;
0397     bool                m_useMakeIndexOptions;
0398 
0399     KConfig *m_config;     // stores project structure
0400     KConfig *m_guiConfig;  // stores project GUI settings: last document, items view settings, etc.
0401     KileDocument::Extensions *m_extmanager;
0402 };
0403 
0404 #endif