1 /* This file is part of the KDE project
2   Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org>
3   Copyright (C) 2004 - 2009 Dag Andersen <danders@get2net.dk>
4   Copyright (C) 2006 Raphael Langerhorst <raphael.langerhorst@kdemail.net>
5   Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
6   Copyright (C) 2007 - 2009 Dag Andersen <danders@get2net.dk>
7 
8   This library is free software; you can redistribute it and/or
9   modify it under the terms of the GNU Library General Public
10   License as published by the Free Software Foundation; either
11   version 2 of the License, or (at your option) any later version.
12 
13   This library is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   Library General Public License for more details.
17 
18   You should have received a copy of the GNU Library General Public License
19   along with this library; see the file COPYING.LIB.  If not, write to
20   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23 
24 #ifndef KPLATOWORK_PART_H
25 #define KPLATOWORK_PART_H
26 
27 #include "planwork_export.h"
28 
29 #include "kptxmlloaderobject.h"
30 
31 #include <KoDocument.h>
32 
33 #include <QFileInfo>
34 #include <QProcess>
35 
36 #include <kservice.h>
37 #include <kparts/readwritepart.h>
38 
39 class KUndo2QStack;
40 
41 class KoStore;
42 
43 class KProcess;
44 
45 class QFileSystemWatcher;
46 
47 namespace KPlato
48 {
49     class Project;
50     class Document;
51     class MacroCommand;
52 }
53 
54 using namespace KPlato;
55 
56 /// The main namespace for KPlato WorkPackage Handler
57 namespace KPlatoWork
58 {
59 
60 class Part;
61 class WorkPackage;
62 class View;
63 
64 /**
65  * DocumentChild stores info about documents opened for editing.
66  * Editors can be KParts, Calligra or Other.
67  */
68 class DocumentChild : public QObject
69 {
70     Q_OBJECT
71 public:
72     // The type of document this child handles
73     enum DocType { Type_Unknown = 0, Type_Calligra, Type_KParts, Type_Other };
74 
75     explicit DocumentChild(WorkPackage *parent);
76 //    DocumentChild(KParts::ReadWritePart *editor, const QUrl &url, const Document *doc, Part *parent);
77 
78     ~DocumentChild() override;
79 
80     WorkPackage *parentPackage() const;
doc()81     const Document *doc() const { return m_doc; }
82     /// Set document, return true if ok, false if failure
83     bool setDoc(const Document *doc);
84     /// Open @p doc from @p store
85     bool openDoc(const Document *doc, KoStore *store);
86     /// Open document for editing, return true if ok, false if failure
87     bool editDoc();
isOpen()88     bool isOpen() const { return m_process != 0; }
89     bool isModified() const;
90     bool isFileModified() const;
91 
fileName()92     QString fileName() const { return m_fileinfo.fileName(); }
filePath()93     QString filePath() const { return m_fileinfo.canonicalFilePath(); }
94     void setFileInfo(const QUrl &url);
fileInfo()95     const QFileInfo &fileInfo() const { return m_fileinfo; }
96 
url()97     QUrl url() const { return QUrl::fromLocalFile(filePath()); }
editor()98     KParts::ReadWritePart *editor() const { return m_editor; }
99     bool startProcess(KService::Ptr service, const QUrl &url = QUrl());
type()100     int type() const { return m_type; }
setType(int type)101     void setType(int type) { m_type = type; }
102 
103     bool saveToStore(KoStore *store);
104 
105 Q_SIGNALS:
106     void modified(bool);
107     void fileModified(bool);
108 
109 public Q_SLOTS:
110     void setModified(bool mod);
111 
112 protected Q_SLOTS:
113     void slotEditFinished(int,  QProcess::ExitStatus);
114     void slotEditError(QProcess::ProcessError status);
115 
116     void slotDirty(const QString &file);
117 
118     void slotUpdateModified();
119 
120 protected:
121     const Document *m_doc;
122     int m_type;
123     bool m_copy;
124     KProcess *m_process; // Used if m_type == Type_Other;
125     KParts::ReadWritePart *m_editor; // 0 if m_type == Type_Other
126     QFileInfo m_fileinfo;
127     bool m_editormodified;
128     bool m_filemodified;
129     QFileSystemWatcher *m_fileSystemWatcher;
130 };
131 
132 /**
133  This part handles work packages.
134  A work package file consists of a Project node and one Task node
135  along with scheduling information and assigned resources.
136 */
137 
138 class PLANWORK_EXPORT Part : public KParts::ReadWritePart
139 {
140     Q_OBJECT
141 
142 public:
143     explicit Part(QWidget *parentWidget, QObject *parent, const QVariantList & /*args*/ = QVariantList());
144     ~Part() override;
145 
146     int docType(const Document *doc) const;
147 
148     bool loadWorkPackages();
149     virtual bool loadXML(const KoXmlDocument &document, KoStore *store);
150     virtual QDomDocument saveXML();
151 
152     bool saveAs(const QUrl &url) override;
153     /// Check if we have documents open for editing before saving
154     virtual bool completeSaving(KoStore* store);
155 
156     /// Extract document file from the store to disk
157     QUrl extractFile(const Document *doc);
158 
159     //Config &config() { return m_config; }
160 
161     /// Open Calligra document for editing
162 //     DocumentChild *openCalligraDocument(KMimeType::Ptr mimetype, const Document *doc);
163     /// Open KParts document for editing
164 //     DocumentChild *openKPartsDocument(KService::Ptr service, const Document *doc);
165     /// Open document for editing, return true if ok, false if failure
166     bool editWorkpackageDocument(const Document *doc);
167     /// Open document for editing, return true if ok, false if failure
168     bool editOtherDocument(const Document *doc);
169     /// Remove the document @p doc from its workpackage
170     bool removeDocument(Document *doc);
171     /// Remove the child document
172 //    void removeChildDocument(DocumentChild *child);
173     /// Find the child that handles document @p doc
174     DocumentChild *findChild(const Document *doc) const;
175     /// Add @p child document to work package @p wp
176 //    void addChild(WorkPackage *wp, DocumentChild *child);
177 
workPackages()178     QMap<QString, WorkPackage*> workPackages() const { return m_packageMap; }
179     /// Number of workpackages
workPackageCount()180     int workPackageCount() const { return m_packageMap.count(); }
181     /// Work package at index
workPackage(int index)182     WorkPackage *workPackage(int index) const {
183         const QList<WorkPackage*> &lst = m_packageMap.values();
184         return lst.value(index);
185     }
186     /// Work package containing node
workPackage(Node * node)187     WorkPackage *workPackage(Node *node) const {
188         return m_packageMap.value(node->projectNode()->id() + node->id());
189     }
indexOf(WorkPackage * package)190     int indexOf(WorkPackage *package) const {
191         const QList<WorkPackage*> &lst = m_packageMap.values();
192         return lst.indexOf(package);
193     }
194     void addWorkPackage(WorkPackage *wp);
195     void removeWorkPackage(WorkPackage *wp);
196     void removeWorkPackage(Node *node, MacroCommand *m = 0);
197     void removeWorkPackages(const QList<Node*> &nodes);
198 
199     /// Find the work package that handles document @p doc
200     WorkPackage *findWorkPackage(const Document *doc) const;
201     /// Find the work package that handles document child @p child
202     WorkPackage *findWorkPackage(const DocumentChild *child) const;
203     /// Find the work package that handles @p node
204     WorkPackage *findWorkPackage(const Node *node) const;
205 
206     /// Save all work packages
207     bool saveWorkPackages(bool silent);
208 
209     Node *node() const;
210 
211     bool queryClose() override;
212 
213     bool openFile() override;
214     bool saveFile() override;
215 
undoStack()216     KUndo2QStack *undoStack() const { return m_undostack; }
commandIndex()217     int commandIndex() const { return m_undostack->index(); }
218 
219 public Q_SLOTS:
220     /**
221      * Called by the undo stack when the document is saved or all changes has been undone
222      * @param clean if the document's undo stack is clean or not
223      */
224     virtual void setDocumentClean(bool clean);
225 
226     void setModified(bool mod) override;
227     void saveModifiedWorkPackages();
228     void saveWorkPackage(KPlatoWork::WorkPackage *wp);
229     void addCommand(KUndo2Command *cmd);
230 
231     void viewWorkpackageDocument(KPlato::Document *doc);
232 
233 Q_SIGNALS:
234     void changed();
235     void workPackageAdded(KPlatoWork::WorkPackage *package, int index);
236     void workPackageRemoved(KPlatoWork::WorkPackage *wp, int index);
237     void captionChanged(const QString&, bool);
238 
239 protected:
240     /// Load the old kplato format
241     bool loadKPlatoXML(const KoXmlDocument &document, KoStore *store);
242     /// Adds work package @p wp to the list of workpackages.
243     /// If it already exists, the user is asked if it shall be merged with the existing one.
244     bool setWorkPackage(WorkPackage *wp, KoStore *store = 0);
245     bool completeLoading(KoStore *store);
246 
247     bool loadAndParse(KoStore* store, const QString& filename, KoXmlDocument& doc);
248     bool loadNativeFormatFromStore(const QString& file);
249     bool loadNativeFormatFromStoreInternal(KoStore * store);
250 
251     bool viewDocument(const QUrl &filename);
252 
253 private:
254     View *m_view;
255     XMLLoaderObject m_xmlLoader;
256     //Config m_config;
257 
258     QMap<QString, WorkPackage*> m_packageMap;
259 
260     bool m_modified;
261     bool m_loadingFromProjectStore;
262 
263     KUndo2QStack *m_undostack;
264 
265 
266 };
267 
268 
269 }  //KPlatoWork namespace
270 
271 #endif
272