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 ***************************************************************************/
21 #include <QDataStream>
24 #include <QStandardItemModel>
26 #include <ksharedptr.h>
28 #include "lib/network/serialization.h"
29 #include "lib/network/safesharedptr.h"
30 #include "lib/network/messagetypeset.h"
31 #include "lib/network/easymessage.h"
33 #include "kdevteamwork_messages.h"
37 #include <boost/serialization/split_member.hpp>
45 QStringList
splitArgs( const QString
& str
);
47 enum PatchAccessRights
{
52 Ask
///This means that the patch is publically visible, but on request, the owner is asked whether it really should be sent.
55 class LocalPatchSource
: public SafeShared
{
57 ///This class should be used for identification instead of just the name, because the type of comparison might change in future
63 Identity( const LocalPatchSource
& src
) : name_(src
.name
) {
66 template<class Archive
>
67 void serialize( Archive
& arch
, const uint
/*version*/ ) {
74 bool operator == ( const Identity
& rhs
) const {
75 return name_
== rhs
.name_
;
89 string
stateAsString() {
100 Identity
identity() {
101 return Identity( *this );
106 string type
; ///This is the mime-type that was chosen for this item
108 string unApplyCommand
;
114 UserIdentity userIdentity
;
115 PatchAccessRights access
;
117 LocalPatchSource() : state( Unknown
), access( Private
) {
120 void setFileName( const string
& str
) {
126 ///Tries to determine the patch-depth(by searching for -pX parameters in the apply-command. Defaults to zero.
127 uint
patchDepth() const;
129 ///Tries to determine the tool to be used for applying the patch. Defaults to "patch"
130 string
patchTool( bool reverse
= false ) const;
132 ///Tries to determine the params for applying the patch. If none are given, uses reasonable default-parameters. If reverse is true, the unapply-parameters are given.
133 string
patchParams( bool reverse
= false ) const;
135 template<class Archive
>
136 void serialize( Archive
& arch
, const uint
/*version*/ ) {
138 filename
= ~u
.prettyUrl(); ///If there is a password, leave it out
140 arch
& NVP( filename
);
141 arch
& NVP( command
);
144 arch
& NVP( applyCommand
);
145 arch
& NVP( unApplyCommand
);
146 arch
& NVP( dependencies
);
147 arch
& NVP( description
);
148 // arch & NVP( mimetype );
149 arch
& NVP( access
);
151 arch
& NVP( author
);
152 arch
& NVP( userIdentity
);
155 static string
accessToString( PatchAccessRights access
) {
162 return "connected only";
171 static PatchAccessRights
accessFromString( const string
& txt
) {
175 if( txt
== "connected only" )
176 return ConnectedOnly
;
178 if( txt
== "request" )
181 if( txt
== "private" )
187 string
accessAsString() {
188 return accessToString( access
);
191 bool operator == ( const LocalPatchSource
& rhs
) const {
192 return name
== rhs
.name
;
195 QIcon
getIcon( IconCache
& icons
);
197 void setUser( const UserPointer
& u
) {
201 const UserPointer
& user() {
205 /*void setMimeType( KSharedPtr<KMimeType> mimeType );
207 KSharedPtr<KMimeType> getMimeType();*/
213 //BOOST_CLASS_EXPORT( LocalPatchSource )
215 typedef SafeSharedPtr
<LocalPatchSource
> LocalPatchSourcePointer
;
218 ///This message is just a base-class for all messages that should be forwarded to the PatchesListManager
219 class PatchesManagerMessage
: public SystemMessage
221 DECLARE_MESSAGE( PatchesManagerMessage
, SystemMessage
, 6 );
228 PatchesManagerMessage( InArchive
& arch
, const Teamwork::MessageInfo
& info
) : Precursor( arch
, info
) {
231 explicit PatchesManagerMessage( const Teamwork::MessageConstructionInfo
& info
, Message msg
= None
) : Precursor( info(this), (SystemMessage::Message
)msg
, "" ) {
235 return (Message
) SystemMessage::message();
239 typedef SafeSharedPtr
<PatchesManagerMessage
> PatchesManagerMessagePointer
;
242 class PatchesListMessage
: public PatchesManagerMessage
244 DECLARE_MESSAGE( PatchesListMessage
, PatchesManagerMessage
, 1 );
248 void serial( Arch
& arch
) {
253 list
<LocalPatchSource
> patches
;
255 PatchesListMessage( InArchive
& arch
, const Teamwork::MessageInfo
& info
);
257 PatchesListMessage( const Teamwork::MessageConstructionInfo
& info
, list
<LocalPatchSourcePointer
>& _patches
) : Precursor( info(this), None
) {
258 for( list
<LocalPatchSourcePointer
>::iterator it
= _patches
.begin(); it
!= _patches
.end(); ++it
) {
259 LocalPatchSourcePointer::Locked l
= *it
;
261 patches
.push_back( *l
);
263 ///could not lock the patch-source
268 virtual void serialize( OutArchive
& arch
);
271 class PatchRequestMessage
;
273 class PatchRequestData
: public AbstractGUIMessage
{
289 explicit PatchRequestData( const LocalPatchSourcePointer
& id
= LocalPatchSourcePointer(), KDevTeamwork
* tw
= 0, RequestType req
= View
);
290 virtual ~PatchRequestData();
293 void serialize( Arch
& arch
, const uint
/*version*/ ) {
298 LocalPatchSource::Identity
patchIdentity() {
302 void setStatus( Status st
) {
306 string
patchDesc() const {
307 return ident_
.desc();
310 RequestType
requestType() const {
314 ///This returns the LocalPatchSourcePointer which was used to request the patch(only valid on sender-side, to cache the apply-commands etc. for security)
315 LocalPatchSourcePointer
request() const {
319 virtual void fillContextMenu( QMenu
* menu
, KDevTeamwork
* teamwork
);
321 virtual QIcon
messageIcon() const;
323 virtual QString
messageText() const;
325 PatchRequestMessage
* selfMessage() ;
326 const PatchRequestMessage
* selfMessage() const;
329 LocalPatchSource::Identity ident_
;
330 LocalPatchSourcePointer request_
;
331 RequestType requestType_
;
334 SafeTeamworkEmitterPointer emitter
;
337 EASY_DECLARE_MESSAGE_BEGIN( PatchRequestMessage
, PatchesManagerMessage
, 11, PatchRequestData
, 3 )
338 virtual bool needReply () const { return true; }
339 virtual ReplyResult
gotReply( const MessagePointer
& /*p*/ );
345 ///This needs a helper, because the object must be created in the same thread.
346 class PatchDataReceiver
: public QObject
{
350 PatchDataReceiver( PatchData
* d
);
352 void receivedStdout(K3Process
*proc
, char *buffer
, int buflen
);
353 void transferData( KIO::Job
*, const QByteArray
& );
354 void transferFinished( KJob
*job
);
355 // void transferSpeed( KIO::Job *job, unsigned long speed );
361 explicit PatchData( const LocalPatchSourcePointer
& p
= LocalPatchSourcePointer(), LoggerPointer logg
= LoggerPointer() );
363 //template<class Arch>
364 void load( InArchive
& arch
, const uint
/*version*/ );
366 //template<class Arch>
367 void save( OutArchive
& arch
, const uint version
) const {
368 const_cast<PatchData
*>(this)->saveInternal( arch
, version
);
371 void receivedStdout(K3Process
*proc
, char *buffer
, int buflen
);
373 ///This is only valid if the message was not created locally.
374 const QByteArray
& data();
378 ///Here the signals from TransferJob arrive.
379 void transferData( KIO::Job
*, const QByteArray
& );
381 void transferFinished();
383 void transferCanceled();
385 LocalPatchSourcePointer
patch();
387 BOOST_SERIALIZATION_SPLIT_MEMBER()
390 //template<class Arch>
391 void saveInternal( OutArchive
& arch
, const uint
/*version*/ );
393 LocalPatchSourcePointer m_patch
;
394 LoggerPointer logger
;
395 OutArchive
* currentArchive
;
397 bool deserialized
, finished
, isBinary_
;
398 QByteArray m_data
; ///When this has no content, the patch should be computed from the information in LocalPatchSource while serializing.
411 LoggerPrinter
log( Logger::Level level
= Logger::Debug
) {
412 return LoggerPrinter( logger
, level
) << "PatchMessage: ";
416 EASY_DECLARE_MESSAGE( PatchMessage
, PatchesManagerMessage
, 6, PatchData
, 2 );
420 // kate: space-indent on; indent-width 2; tab-width 2; replace-tabs on