File indexing completed on 2024-05-12 16:39:25

0001 /* This file is part of the KDE project
0002   Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org>
0003   Copyright (C) 2004 - 2009 Dag Andersen <danders@get2net.dk>
0004   Copyright (C) 2006 Raphael Langerhorst <raphael.langerhorst@kdemail.net>
0005   Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
0006   Copyright (C) 2007 - 2009 Dag Andersen <danders@get2net.dk>
0007 
0008   This library is free software; you can redistribute it and/or
0009   modify it under the terms of the GNU Library General Public
0010   License as published by the Free Software Foundation; either
0011   version 2 of the License, or (at your option) any later version.
0012 
0013   This library is distributed in the hope that it will be useful,
0014   but WITHOUT ANY WARRANTY; without even the implied warranty of
0015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016   Library General Public License for more details.
0017 
0018   You should have received a copy of the GNU Library General Public License
0019   along with this library; see the file COPYING.LIB.  If not, write to
0020   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0021 * Boston, MA 02110-1301, USA.
0022 */
0023 
0024 #ifndef KPLATOWORK_PART_H
0025 #define KPLATOWORK_PART_H
0026 
0027 #include "planwork_export.h"
0028 
0029 #include "kptxmlloaderobject.h"
0030 
0031 #include <KoDocument.h>
0032 
0033 #include <QFileInfo>
0034 #include <QProcess>
0035 
0036 #include <kservice.h>
0037 #include <kparts/readwritepart.h>
0038 
0039 class KUndo2QStack;
0040 
0041 class KoStore;
0042 
0043 class KProcess;
0044 
0045 class QFileSystemWatcher;
0046 
0047 namespace KPlato
0048 {
0049     class Project;
0050     class Document;
0051     class MacroCommand;
0052 }
0053 
0054 using namespace KPlato;
0055 
0056 /// The main namespace for KPlato WorkPackage Handler
0057 namespace KPlatoWork
0058 {
0059 
0060 class Part;
0061 class WorkPackage;
0062 class View;
0063 
0064 /**
0065  * DocumentChild stores info about documents opened for editing.
0066  * Editors can be KParts, Calligra or Other.
0067  */
0068 class DocumentChild : public QObject
0069 {
0070     Q_OBJECT
0071 public:
0072     // The type of document this child handles
0073     enum DocType { Type_Unknown = 0, Type_Calligra, Type_KParts, Type_Other };
0074 
0075     explicit DocumentChild(WorkPackage *parent);
0076 //    DocumentChild(KParts::ReadWritePart *editor, const QUrl &url, const Document *doc, Part *parent);
0077     
0078     ~DocumentChild() override;
0079     
0080     WorkPackage *parentPackage() const;
0081     const Document *doc() const { return m_doc; }
0082     /// Set document, return true if ok, false if failure
0083     bool setDoc(const Document *doc);
0084     /// Open @p doc from @p store
0085     bool openDoc(const Document *doc, KoStore *store);
0086     /// Open document for editing, return true if ok, false if failure
0087     bool editDoc();
0088     bool isOpen() const { return m_process != 0; }
0089     bool isModified() const;
0090     bool isFileModified() const;
0091     
0092     QString fileName() const { return m_fileinfo.fileName(); }
0093     QString filePath() const { return m_fileinfo.canonicalFilePath(); }
0094     void setFileInfo(const QUrl &url);
0095     const QFileInfo &fileInfo() const { return m_fileinfo; }
0096 
0097     QUrl url() const { return QUrl::fromLocalFile(filePath()); }
0098     KParts::ReadWritePart *editor() const { return m_editor; }
0099     bool startProcess(KService::Ptr service, const QUrl &url = QUrl());
0100     int type() const { return m_type; }
0101     void setType(int type) { m_type = type; }
0102     
0103     bool saveToStore(KoStore *store);
0104 
0105 Q_SIGNALS:
0106     void modified(bool);
0107     void fileModified(bool);
0108     
0109 public Q_SLOTS:
0110     void setModified(bool mod);
0111     
0112 protected Q_SLOTS:
0113     void slotEditFinished(int,  QProcess::ExitStatus);
0114     void slotEditError(QProcess::ProcessError status);
0115     
0116     void slotDirty(const QString &file);
0117 
0118     void slotUpdateModified();
0119     
0120 protected:
0121     const Document *m_doc;
0122     int m_type;
0123     bool m_copy;
0124     KProcess *m_process; // Used if m_type == Type_Other;
0125     KParts::ReadWritePart *m_editor; // 0 if m_type == Type_Other
0126     QFileInfo m_fileinfo;
0127     bool m_editormodified;
0128     bool m_filemodified;
0129     QFileSystemWatcher *m_fileSystemWatcher;
0130 };
0131 
0132 /**
0133  This part handles work packages.
0134  A work package file consists of a Project node and one Task node
0135  along with scheduling information and assigned resources.
0136 */
0137 
0138 class PLANWORK_EXPORT Part : public KParts::ReadWritePart
0139 {
0140     Q_OBJECT
0141 
0142 public:
0143     explicit Part(QWidget *parentWidget, QObject *parent, const QVariantList & /*args*/ = QVariantList());
0144     ~Part() override;
0145 
0146     int docType(const Document *doc) const;
0147     
0148     bool loadWorkPackages();
0149     virtual bool loadXML(const KoXmlDocument &document, KoStore *store);
0150     virtual QDomDocument saveXML();
0151     
0152     bool saveAs(const QUrl &url) override;
0153     /// Check if we have documents open for editing before saving
0154     virtual bool completeSaving(KoStore* store);
0155 
0156     /// Extract document file from the store to disk
0157     QUrl extractFile(const Document *doc);
0158     
0159     //Config &config() { return m_config; }
0160     
0161     /// Open Calligra document for editing
0162 //     DocumentChild *openCalligraDocument(KMimeType::Ptr mimetype, const Document *doc);
0163     /// Open KParts document for editing
0164 //     DocumentChild *openKPartsDocument(KService::Ptr service, const Document *doc);
0165     /// Open document for editing, return true if ok, false if failure
0166     bool editWorkpackageDocument(const Document *doc);
0167     /// Open document for editing, return true if ok, false if failure
0168     bool editOtherDocument(const Document *doc);
0169     /// Remove the document @p doc from its workpackage
0170     bool removeDocument(Document *doc);
0171     /// Remove the child document
0172 //    void removeChildDocument(DocumentChild *child);
0173     /// Find the child that handles document @p doc
0174     DocumentChild *findChild(const Document *doc) const;
0175     /// Add @p child document to work package @p wp
0176 //    void addChild(WorkPackage *wp, DocumentChild *child);
0177 
0178     QMap<QString, WorkPackage*> workPackages() const { return m_packageMap; }
0179     /// Number of workpackages
0180     int workPackageCount() const { return m_packageMap.count(); }
0181     /// Work package at index
0182     WorkPackage *workPackage(int index) const {
0183         const QList<WorkPackage*> &lst = m_packageMap.values();
0184         return lst.value(index);
0185     }
0186     /// Work package containing node
0187     WorkPackage *workPackage(Node *node) const { 
0188         return m_packageMap.value(node->projectNode()->id() + node->id());
0189     }
0190     int indexOf(WorkPackage *package) const {
0191         const QList<WorkPackage*> &lst = m_packageMap.values();
0192         return lst.indexOf(package);
0193     }
0194     void addWorkPackage(WorkPackage *wp);
0195     void removeWorkPackage(WorkPackage *wp);
0196     void removeWorkPackage(Node *node, MacroCommand *m = 0);
0197     void removeWorkPackages(const QList<Node*> &nodes);
0198 
0199     /// Find the work package that handles document @p doc
0200     WorkPackage *findWorkPackage(const Document *doc) const;
0201     /// Find the work package that handles document child @p child
0202     WorkPackage *findWorkPackage(const DocumentChild *child) const;
0203     /// Find the work package that handles @p node
0204     WorkPackage *findWorkPackage(const Node *node) const;
0205 
0206     /// Save all work packages
0207     bool saveWorkPackages(bool silent);
0208 
0209     Node *node() const;
0210     
0211     bool queryClose() override;
0212 
0213     bool openFile() override;
0214     bool saveFile() override;
0215 
0216     KUndo2QStack *undoStack() const { return m_undostack; }
0217     int commandIndex() const { return m_undostack->index(); }
0218 
0219 public Q_SLOTS:
0220     /**
0221      * Called by the undo stack when the document is saved or all changes has been undone
0222      * @param clean if the document's undo stack is clean or not
0223      */
0224     virtual void setDocumentClean(bool clean);
0225 
0226     void setModified(bool mod) override;
0227     void saveModifiedWorkPackages();
0228     void saveWorkPackage(KPlatoWork::WorkPackage *wp);
0229     void addCommand(KUndo2Command *cmd);
0230 
0231     void viewWorkpackageDocument(KPlato::Document *doc);
0232 
0233 Q_SIGNALS:
0234     void changed();
0235     void workPackageAdded(KPlatoWork::WorkPackage *package, int index);
0236     void workPackageRemoved(KPlatoWork::WorkPackage *wp, int index);
0237     void captionChanged(const QString&, bool);
0238 
0239 protected:
0240     /// Load the old kplato format
0241     bool loadKPlatoXML(const KoXmlDocument &document, KoStore *store);
0242     /// Adds work package @p wp to the list of workpackages.
0243     /// If it already exists, the user is asked if it shall be merged with the existing one.
0244     bool setWorkPackage(WorkPackage *wp, KoStore *store = 0);
0245     bool completeLoading(KoStore *store);
0246 
0247     bool loadAndParse(KoStore* store, const QString& filename, KoXmlDocument& doc);
0248     bool loadNativeFormatFromStore(const QString& file);
0249     bool loadNativeFormatFromStoreInternal(KoStore * store);
0250     
0251     bool viewDocument(const QUrl &filename);
0252 
0253 private:
0254     View *m_view;
0255     XMLLoaderObject m_xmlLoader;
0256     //Config m_config;
0257     
0258     QMap<QString, WorkPackage*> m_packageMap;
0259 
0260     bool m_modified;
0261     bool m_loadingFromProjectStore;
0262 
0263     KUndo2QStack *m_undostack;
0264 
0265 
0266 };
0267 
0268 
0269 }  //KPlatoWork namespace
0270 
0271 #endif