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 CONVERSATIONMANAGER_H
15 #define CONVERSATIONMANAGER_H
18 #include <QtCore/QPointer>
20 #include <ktexteditor/document.h>
21 #include <ktexteditor/smartcursor.h>
22 #include <ktexteditor/smartinterface.h>
23 #include "teamworkfwd.h"
24 #include "indocumentreference.h"
25 #include "messagehistorymanager.h"
26 #include "lib/network/crossmap.h"
27 #include "safelogger.h"
28 #include "ui_kdevteamwork_internalmessage.h"
31 class QStandardItemModel
;
32 class TemporaryConversationConnector
;
33 class KDevTeamworkClient
;
35 class InDocumentMessage
;
41 typedef SafeSharedPtr
<InDocumentMessage
, MessageSerialization
> InDocumentMessagePointer
;
43 ///This is a class that helps detecting when the document is deleted and then deleting the smartcursor, also it automatically creates the cursor. @todo replace with QOwnedPointer
44 class SmartCursorContainer
{
45 QPointer
<KTextEditor::Document
> m_document
;
46 KTextEditor::SmartCursor
* m_smartCursor
;
48 SmartCursorContainer( KTextEditor::Document
* doc
= 0 ) : m_document( doc
), m_smartCursor( 0 ) {
50 KTextEditor::SmartInterface
* smart
=
51 qobject_cast
<KTextEditor::SmartInterface
*>( doc
);
54 m_smartCursor
= smart
->newSmartCursor();
57 KTextEditor::SmartCursor
* operator -> () {
60 ~SmartCursorContainer() {
65 KTextEditor::SmartCursor
& operator *() {
66 return * m_smartCursor
;
68 SmartCursorContainer( const SmartCursorContainer
& rhs
) {
71 if ( m_smartCursor
&& m_document
)
73 m_document
= rhs
.m_document
;
74 m_smartCursor
= rhs
.m_smartCursor
;
75 const_cast<SmartCursorContainer
&>( rhs
).m_smartCursor
= 0;
77 SmartCursorContainer
& operator = ( const SmartCursorContainer
& rhs
) {
80 if ( m_smartCursor
&& m_document
)
82 m_document
= rhs
.m_document
;
83 m_smartCursor
= rhs
.m_smartCursor
;
84 const_cast<SmartCursorContainer
&>( rhs
).m_smartCursor
= 0;
89 return m_document
&& m_smartCursor
&& m_smartCursor
->isValid();
93 namespace KTextEditor
{
98 class InDocumentMessage
;
100 class ConversationManager
;
104 CROSSMAP_DEFINE_CONTAINER( QList
)
106 struct OrderedDocumentMessage
{
108 InDocumentMessagePointer message
;
109 OrderedDocumentMessage( uint pos
, const InDocumentMessagePointer
& msg
) : position( pos
), message( msg
) {}
110 OrderedDocumentMessage() : position( 0 ), message( 0 ) {}
112 operator bool() const {
113 return ( bool ) message
;
115 /*template<class Archive>
116 void serialize( Archive& arch, const uint version ) {
117 arch & position & message;
121 class InDocumentConversation
: public QObject
, public Shared
, public SafeLogger
{
123 ///Order, message, file
124 BIND_LIST_3( Keys
, InDocumentMessagePointer
, int, QString
)
125 typedef Utils::CrossMap
< OrderedDocumentMessage
, Keys
> MessageSet
;
127 InDocumentConversation( InDocumentMessage
* msg
= 0 );
128 ~InDocumentConversation();
130 void addMessage( InDocumentMessage
* msg
);
132 template <class ArchType
>
133 void load( ArchType
& arch
, unsigned int version
);
135 template <class ArchType
>
136 void save( ArchType
& arch
, unsigned int version
) const;
138 ConversationManager
* manager() const;
140 void fillContextMenu( QMenu
* menu
, KDevTeamwork
* teamwork
, MessagePointer msg
);
142 ///Returns true if the message belongs to this conversation
143 bool match( InDocumentMessage
* msg
) const;
145 void setActive( bool );
147 UserPointer
primaryUser();
150 void userStateChanged( KDevTeamworkUserPointer
);
151 void documentActivated( KDevelop::IDocument
* document
);
152 void verticalScrollPositionChanged ( KTextEditor::View
*view
, const KTextEditor::Cursor
& newPos
);
153 void cursorPositionChanged ( KTextEditor::View
*view
, const KTextEditor::Cursor
& newPos
);
154 void horizontalScrollPositionChanged ( KTextEditor::View
*view
);
158 void gotReply( MessagePointer msg
);
160 void messageClicked( const QModelIndex
& );
161 void messageContextMenu ( const QPoint
& );
162 void selectMessage( InDocumentMessagePointer msg
);
165 void log( const QString
& str
, LogLevel level
= Info
) const;
166 void selectNearestMessage();
168 void textChanged ( KTextEditor::Document
* document
, const KTextEditor::Range
& oldRange
, const KTextEditor::Range
& newRange
);
169 void textRemoved ( KTextEditor::Document
* document
, const KTextEditor::Range
& range
);
170 void textInserted ( KTextEditor::Document
* document
, const KTextEditor::Range
& range
);
174 virtual std::string
logPrefix();
176 InDocumentMessagePointer
selectedMessage();
178 void documentActivated( KDevelop::IDocument
* document
, const InDocumentMessagePointer
& msg
);
180 void messageSelected( const MessagePointer
& msg
);
181 template <class ArchType
>
182 void serial( ArchType
& arch
, unsigned int /*version*/ ) {
183 arch
& m_documentName
;
187 void pushMessage( const InDocumentMessagePointer
& msg
);
189 KTextEditor::Cursor
currentDocCursor() const;
191 QString
context() const;
192 void messageSendReady( bool success
);
193 void addListItem( const QString
& txt
, const QString
& icon
= "error" );
194 SessionPointer
session();
196 void fillMessageModel();
197 void fillMessageToModel( const LockedSharedPtr
<InDocumentMessage
>& );
198 void setupWidget( QWidget
* parent
);
199 void embedInView( KTextEditor::View
* view
, KDevelop::IDocument
* document
, KTextEditor::Cursor position
);
200 KTextEditor::Cursor
findPositionInDocument( InDocumentMessagePointer::Locked l
, KTextEditor::Cursor
* endTarget
= 0 );
201 void placeWidget( KTextEditor::View
* view
, const KTextEditor::Cursor
* awayFrom
= 0, bool forceShow
= false );
203 string m_documentName
;
205 int m_conversationId
;
209 //list<InDocumentMessagePointer> m_messages;
210 MessageSet m_messages
;
212 QPointer
<QWidget
> m_widget
;
213 Ui_InternalMessage m_widgets
;
214 SmartCursorContainer m_smartCursor
;
215 QAction
* m_jumpToAction
;
216 QAction
* m_hideAction
;
217 QAction
* m_userInfoAction
;
218 QStandardItemModel
* m_messagesModel
;
219 InDocumentMessagePointer m_sendingMessage
;
220 SharedPtr
<TemporaryConversationConnector
> m_userConnector
;
223 QTimer
* m_selectNearestMessageTimer
;
225 QPointer
<KTextEditor::Document
> m_currentConnectedDocument
;
227 KTextEditor::SmartRange
* m_currentRange
;
229 InDocumentReference::TextSearchInstance m_currentSearchInstance
;
231 QTime m_lastSendTime
;
233 BOOST_SERIALIZATION_SPLIT_MEMBER()
236 typedef SharedPtr
<InDocumentConversation
> InDocumentConversationPointer
;
238 class ConversationManager
: public QObject
{
242 ConversationManager( MessageManager
* mng
);
243 ~ConversationManager();
245 int processMessage( InDocumentMessage
* msg
);
247 static ConversationManager
* instance() {
251 void log( const QString
& str
, LogLevel level
);
253 /* template <class ArchType>
254 void serialize( ArchType& arch, unsigned int ) {
255 arch & m_conversations;
258 MessageManager
* manager() const {
259 return const_cast<MessageManager
*>( m_manager
);
262 ///Returns zero if the conversation does not exist.
263 InDocumentConversationPointer
findConversation( const QString
& context
);
265 ///If the conversation does not exist, it is created.
266 InDocumentConversationPointer
getConversation( InDocumentMessage
* );
271 void documentActivated( KDevelop::IDocument
* document
);
274 static ConversationManager
* m_instance
;
275 typedef map
<string
, InDocumentConversationPointer
> ConversationSet
;
276 ConversationSet m_conversations
; ///maps conversation-ids to conversations
277 MessageManager
* m_manager
;
278 static ConversationManager
* globalManager
;
283 // kate: space-indent on; indent-width 2; tab-width 2; replace-tabs on