Don't keep compiling/run if something failed.
[kdevelopdvcssupport.git] / plugins / teamwork / patchmessage.h
blobdcf10acc7dd42209a561d5bd90bb816cb2a0c438
1 /***************************************************************************
2 Copyright 2006 David Nolden <david.nolden.kdevelop@art-master.de>
3 ***************************************************************************/
5 /***************************************************************************
6 * *
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. *
11 * *
12 ***************************************************************************/
14 #ifndef PATCHMESSAGE
15 #define PATCHMESSAGE
17 #include <list>
18 #include <string>
19 #include <vector>
20 #include <QObject>
21 #include <QDataStream>
22 #include <QByteArray>
23 #include <QMetaType>
24 #include <QStandardItemModel>
25 #include <kurl.h>
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"
34 #include "utils.h"
35 #include "nvp.h"
37 #include <boost/serialization/split_member.hpp>
39 namespace KIO {
40 class Job;
42 class KMimeType;
43 class KJob;
45 QStringList splitArgs( const QString& str );
47 enum PatchAccessRights {
48 None,
49 Public,
50 ConnectedOnly,
51 Private,
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
58 public:
59 class Identity {
60 private:
61 string name_;
62 public:
63 Identity( const LocalPatchSource& src ) : name_(src.name) {
66 template<class Archive>
67 void serialize( Archive& arch, const uint /*version*/ ) {
68 arch & name_;
71 Identity() {
74 bool operator == ( const Identity& rhs ) const {
75 return name_ == rhs.name_;
78 string desc() const {
79 return name_;
83 enum State {
84 Applied,
85 NotApplied,
86 Unknown
89 string stateAsString() {
90 switch( state ) {
91 case Applied:
92 return "Applied";
93 case NotApplied:
94 return "NotApplied";
95 default:
96 return "Unknown";
100 Identity identity() {
101 return Identity( *this );
103 string name;
104 string filename;
105 string command;
106 string type; ///This is the mime-type that was chosen for this item
107 string applyCommand;
108 string unApplyCommand;
109 string dependencies;
110 string description;
111 string author;
112 State state;
114 UserIdentity userIdentity;
115 PatchAccessRights access;
117 LocalPatchSource() : state( Unknown ), access( Private ) {
120 void setFileName( const string& str ) {
121 filename = str;
122 command = "";
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*/ ) {
137 KUrl u( ~filename );
138 filename = ~u.prettyUrl(); ///If there is a password, leave it out
140 arch & NVP( filename );
141 arch & NVP( command );
142 arch & NVP( name );
143 arch & NVP( type );
144 arch & NVP( applyCommand );
145 arch & NVP( unApplyCommand );
146 arch & NVP( dependencies );
147 arch & NVP( description );
148 // arch & NVP( mimetype );
149 arch & NVP( access );
150 arch & NVP( state );
151 arch & NVP( author );
152 arch & NVP( userIdentity );
155 static string accessToString( PatchAccessRights access ) {
156 switch( access ) {
157 case None:
158 return "none";
159 case Public:
160 return "public";
161 case ConnectedOnly:
162 return "connected only";
163 case Private:
164 return "private";
165 case Ask:
166 return "request";
168 return "unknown";
171 static PatchAccessRights accessFromString( const string& txt ) {
172 if( txt == "public")
173 return Public;
175 if( txt == "connected only" )
176 return ConnectedOnly;
178 if( txt == "request" )
179 return Ask;
181 if( txt == "private" )
182 return Private;
184 return None;
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 ) {
198 user_ = u;
201 const UserPointer& user() {
202 return user_;
205 /*void setMimeType( KSharedPtr<KMimeType> mimeType );
207 KSharedPtr<KMimeType> getMimeType();*/
209 private:
210 UserPointer user_;
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 );
223 enum Message {
224 None = 50,
225 GetPatchesList
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, "" ) {
234 Message message() {
235 return (Message) SystemMessage::message();
239 typedef SafeSharedPtr<PatchesManagerMessage> PatchesManagerMessagePointer;
242 class PatchesListMessage : public PatchesManagerMessage
244 DECLARE_MESSAGE( PatchesListMessage, PatchesManagerMessage, 1 );
245 private:
247 template<class Arch>
248 void serial( Arch& arch ) {
249 arch & patches;
252 public:
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;
260 if( l ) {
261 patches.push_back( *l );
262 } else {
263 ///could not lock the patch-source
268 virtual void serialize( OutArchive& arch );
271 class PatchRequestMessage;
273 class PatchRequestData : public AbstractGUIMessage {
274 public:
275 enum Status {
276 Waiting,
277 Denied,
278 Accepted,
279 Failed,
280 Unknown
283 enum RequestType {
284 View,
285 Download,
286 Apply
289 explicit PatchRequestData( const LocalPatchSourcePointer& id = LocalPatchSourcePointer(), KDevTeamwork* tw = 0, RequestType req = View );
290 virtual ~PatchRequestData();
292 template<class Arch>
293 void serialize( Arch& arch, const uint /*version*/ ) {
294 arch & ident_;
295 arch & requestType_;
298 LocalPatchSource::Identity patchIdentity() {
299 return ident_;
302 void setStatus( Status st ) {
303 stat = st;
306 string patchDesc() const {
307 return ident_.desc();
310 RequestType requestType() const {
311 return requestType_;
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 {
316 return request_;
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;
328 private:
329 LocalPatchSource::Identity ident_;
330 LocalPatchSourcePointer request_;
331 RequestType requestType_;
332 protected:
333 Status stat;
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*/ );
340 END();
342 class PatchData;
343 class K3Process;
345 ///This needs a helper, because the object must be created in the same thread.
346 class PatchDataReceiver : public QObject {
347 Q_OBJECT
348 PatchData* data;
349 public:
350 PatchDataReceiver( PatchData* d );
351 public slots:
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 );
359 class PatchData {
360 public:
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();
376 bool isBinary();
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()
388 private:
390 //template<class Arch>
391 void saveInternal( OutArchive& arch, const uint /*version*/ );
393 LocalPatchSourcePointer m_patch;
394 LoggerPointer logger;
395 OutArchive* currentArchive;
396 bool errored;
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.
399 KUrl projectDir;
402 enum EntryType {
403 End,
404 BinaryHeader,
405 TextHeader,
406 Text,
407 Vector,
408 None
411 LoggerPrinter log( Logger::Level level = Logger::Debug ) {
412 return LoggerPrinter( logger, level ) << "PatchMessage: ";
416 EASY_DECLARE_MESSAGE( PatchMessage, PatchesManagerMessage, 6, PatchData, 2 );
418 #endif
420 // kate: space-indent on; indent-width 2; tab-width 2; replace-tabs on