1 /***************************************************************************
2 Copyright 2006 David Nolden <david.nolden.kdevelop@art-master.de>
3 ***************************************************************************/
5 /***************************************************************************
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 ***************************************************************************/
14 #ifndef DOCUMENT_WRAPPER_H
15 #define DOCUMENT_WRAPPER_H
20 #include <KParts/MainWindow>
24 #include "lib/dynamictext/vectortimestamp.h"
25 #include "lib/dynamictext/flexibletextnotifier.h"
26 #include "lib/network/safesharedptr.h"
27 #include "lib/network/weaksafesharedptr.h"
28 #include "safelogger.h"
30 #include <autoconstructpointer.h>
31 #include "filesynchronizemessage.h"
35 typedef SafeSharedPtr
<Logger
> LoggerPointer
;
38 namespace KTextEditor
{
43 class FileSynchronize
;
45 class QStandardItemModel
;
49 class DocumentWrapperTreeAction
;
53 class DocumentWrapperMessage
;
55 class FileCollaborationSession
;
56 class LocalPatchSource
;
59 using namespace Teamwork
;
61 typedef SafeSharedPtr
<LocalPatchSource
> LocalPatchSourcePointer
;
62 typedef SharedPtr
<QDynamicText
, BoostSerializationNormal
> QDynamicTextPointer
;
64 class OutputFileWriter
: public QObject
{
67 OutputFileWriter( const QString
& file
);
70 void receivedStdout(K3Process
*proc
, char *buffer
, int buflen
);
76 ///A class that cares about the state of a document and it's history
77 class DocumentWrapper
: public QObject
, public SafeLogger
, public Shared
, public SumTree::FlexibleTextNotifier
<std::string
> {
80 ///If readFile is true, the file is read from disk. Else we wait for the file from the other side. May throw QString on error.
81 BIND_LIST_3( Messages
, FileEditMessage
, FileEditRejectMessage
, FileSynchronize
)
83 DocumentWrapper( uint id
, const LoggerPointer
& logger
, QString fileName
, uint ownIndex
, bool readFile
, FileCollaborationSession
* session
, bool fromBuffer
);
85 QString
fileName() const;
87 KDevelop::IDocument
* document();
89 void updateTree( QModelIndex
& i
, QStandardItemModel
* model
);
91 void fillContextMenu( int var
, QMenu
* menu
);
93 bool hadError() const;
97 VectorTimestamp
state() const;
99 const QDynamicText
& dynamicText() const;
101 ///Returns an id that is unique within the wrapper's parent-session, and is equal on all collaborating peers.
104 void processMessage( DocumentWrapperMessage
* msg
);
106 ///If the document-wrapper is dead, that means the it is not part of the session anymore
107 void setDead( bool dead
);
110 ///This can be usedto completely disable the file(no editing is allowed anymore)
111 void setDisabled( bool disabled
);
113 bool disabled() const;
117 bool synchronize( const UserPointer
& user
);
120 void publishEdit( const VectorTimestamp
& state
, const SimpleReplacement
& replacement
, DocumentWrapper
* sender
);
123 void saveAsBufferFile();
124 LocalPatchSourcePointer
saveAsPatch( bool addToList
= true, bool edit
= true );
128 void toggleEnabled();
129 void saveNewBuffer();
130 void manageDynamicText();
133 void documentDestroyed();
134 void textChanged ( KTextEditor::Document
* document
,
135 const KTextEditor::Range
& oldRange
,
136 const KTextEditor::Range
& newRange
);
137 void textRemoved ( KTextEditor::Document
* document
,
138 const KTextEditor::Range
& range
);
139 void textInserted ( KTextEditor::Document
* document
,
140 const KTextEditor::Range
& range
);
142 ///Notifications from FlexibleText
143 virtual void notifyFlexibleTextErase( int position
, int length
);
144 virtual void notifyFlexibleTextInsert( int position
, const std::string
& text
);
145 virtual void notifyFlexibleTextReplace( int position
, int length
, const std::string
& replacement
);
148 int receiveMessage( FileEditMessage
* msg
);
149 int receiveMessage( FileSynchronize
* msg
);
150 int receiveMessage( FileEditRejectMessage
* msg
);
151 int receiveMessage( MessageInterface
* msg
);
153 bool checkDocumentSync() const; ///Expensive, only for debugging
155 void fillDocumentText();
156 void openDocument( bool toForeground
= false );
158 ///If fromBuffer is true, it is tried to get the content from an already open buffer (@todo)
159 void readFile( bool fromBuffer
) throw ( QString
);
161 virtual std::string
logPrefix();
163 QDynamicTextPointer m_text
;
168 QString m_fileName
; ///Relative path of the file(from project-dir)
169 KDevelop::IDocument
* m_document
;
170 WeakSharedPtr
<FileCollaborationSession
> m_session
;
173 bool m_hadError
, m_disabled
, m_dead
;
177 friend class DocumentWrapperTreeAction
;
179 friend class MessageDispatcher
< DocumentWrapper
, Messages
> ;
180 MessageDispatcher
< DocumentWrapper
, Messages
> m_dispatcher
;
183 typedef SharedPtr
<DocumentWrapper
> DocumentWrapperPointer
;
187 // kate: space-indent on; indent-width 2; tab-width 2; replace-tabs on