@@ -868,105 +868,116 @@ - (void) processMoveItems: (id <DOMElement>) theDocumentElement
868868{
869869 NSString *srcMessageId, *srcFolderId, *dstFolderId, *dstMessageId;
870870 SOGoMicrosoftActiveSyncFolderType srcFolderType, dstFolderType;
871+ id <DOMElement> aMoveOperation;
872+ NSArray *moveOperations;
873+ NSMutableString *s;
874+ NSData *d;
875+ int i;
876+
877+ moveOperations = (id )[theDocumentElement getElementsByTagName: @" Move" ];
871878
872- srcMessageId = [[(id )[theDocumentElement getElementsByTagName: @" SrcMsgId" ] lastObject ] textValue ];
873- srcFolderId = [[[(id )[theDocumentElement getElementsByTagName: @" SrcFldId" ] lastObject ] textValue ] realCollectionIdWithFolderType: &srcFolderType];
874- dstFolderId = [[[(id )[theDocumentElement getElementsByTagName: @" DstFldId" ] lastObject ] textValue ] realCollectionIdWithFolderType: &dstFolderType];
879+ s = [NSMutableString string ];
875880
876- // FIXME
877- if (srcFolderType == ActiveSyncMailFolder && dstFolderType == ActiveSyncMailFolder)
878- {
879- NGImap4Client *client;
880- id currentCollection;
881+ [s appendString: @" <?xml version=\" 1.0\" encoding=\" utf-8\" ?>" ];
882+ [s appendString: @" <!DOCTYPE ActiveSync PUBLIC \" -//MICROSOFT//DTD ActiveSync//EN\" \" http://www.microsoft.com/\" >" ];
883+ [s appendString: @" <MoveItems xmlns=\" Move:\" >" ];
881884
882- NSDictionary *response;
883- NSString *v;
885+ for (i = 0 ; i < [moveOperations count ]; i++)
886+ {
887+ aMoveOperation = [moveOperations objectAtIndex: i];
884888
885- currentCollection = [self collectionFromId: srcFolderId type: srcFolderType];
886-
887- // [currentFolder lookupName: [NSString stringWithFormat: @"folder%@", srcFolderId]
888- // inContext: context
889- // acquire: NO];
890-
891- client = [[currentCollection imap4Connection ] client ];
892- [client select: srcFolderId];
893- response = [client copyUid: [srcMessageId intValue ]
894- toFolder: [NSString stringWithFormat: @" /%@ " , dstFolderId]];
895-
896- // We extract the destionation message id
897- dstMessageId = nil ;
898-
899- if ([[response objectForKey: @" result" ] boolValue ]
900- && (v = [[[response objectForKey: @" RawResponse" ] objectForKey: @" ResponseResult" ] objectForKey: @" flag" ])
901- && [v hasPrefix: @" COPYUID " ])
902- {
903- dstMessageId = [[v componentsSeparatedByString: @" " ] lastObject ];
904-
905- // We mark the original message as deleted
906- response = [client storeFlags: [NSArray arrayWithObject: @" Deleted" ]
907- forUIDs: [NSArray arrayWithObject: srcMessageId]
908- addOrRemove: YES ];
909-
910- if ([[response valueForKey: @" result" ] boolValue ])
911- [(SOGoMailFolder *)currentCollection expunge ];
912-
913- }
889+ srcMessageId = [[(id )[aMoveOperation getElementsByTagName: @" SrcMsgId" ] lastObject ] textValue ];
890+ srcFolderId = [[[(id )[aMoveOperation getElementsByTagName: @" SrcFldId" ] lastObject ] textValue ] realCollectionIdWithFolderType: &srcFolderType];
891+ dstFolderId = [[[(id )[aMoveOperation getElementsByTagName: @" DstFldId" ] lastObject ] textValue ] realCollectionIdWithFolderType: &dstFolderType];
892+
893+ [s appendString: @" <Response>" ];
914894
915- if (!dstMessageId)
916- {
917- [theResponse setStatus: 500 ];
918- [theResponse appendContentString: @" Unable to move message" ];
919- }
920- else
895+ // FIXME - we should support moving events between calendars, for example, or
896+ // or contacts between address books.
897+ if (srcFolderType == ActiveSyncMailFolder && dstFolderType == ActiveSyncMailFolder)
921898 {
922- NSMutableString *s ;
923- NSData *d ;
899+ NGImap4Client *client ;
900+ id currentCollection ;
924901
925- //
926- // If the MoveItems operation is initiated by an Outlook client, we save the "deviceType+dstMessageId" to use it later in order to
927- // modify the Sync command from "add" to "change" (see SOGoActiveSyncDispatcher+Sync.m: -processSyncGetChanges: ...).
928- // This is to avoid Outlook creating dupes when moving messages across folfers.
929- //
930- if ([[context objectForKey: @" DeviceType" ] isEqualToString: @" WindowsOutlook15" ])
931- {
932- NSString *key;
933-
934- // The key must be pretty verbose. We use the <uid>+<DeviceType>+<target folder>+<DstMsgId>
935- key = [NSString stringWithFormat: @" %@ +%@ +%@ +%@ " ,
936- [[context activeUser ] login ],
937- [context objectForKey: @" DeviceType" ],
938- dstFolderId,
939- dstMessageId];
940-
941-
942- [[SOGoCache sharedCache ] setValue: @" MovedItem"
943- forKey: key];
944- }
945-
902+ NSDictionary *response;
903+ NSString *v;
946904
947- // Everything is alright, lets return the proper response. "Status == 3" means success.
948- s = [NSMutableString string ];
905+ currentCollection = [self collectionFromId: srcFolderId type: srcFolderType];
949906
950- [s appendString: @" <?xml version=\" 1.0\" encoding=\" utf-8\" ?>" ];
951- [s appendString: @" <!DOCTYPE ActiveSync PUBLIC \" -//MICROSOFT//DTD ActiveSync//EN\" \" http://www.microsoft.com/\" >" ];
952- [s appendString: @" <MoveItems xmlns=\" Move:\" >" ];
953- [s appendString: @" <Response>" ];
954- [s appendFormat: @" <SrcMsgId>%@ </SrcMsgId>" , srcMessageId];
955- [s appendFormat: @" <DstMsgId>%@ </DstMsgId>" , dstMessageId];
956- [s appendFormat: @" <Status>%d </Status>" , 3 ];
957- [s appendString: @" </Response>" ];
958- [s appendString: @" </MoveItems>" ];
907+ client = [[currentCollection imap4Connection ] client ];
908+ [client select: srcFolderId];
909+ response = [client copyUid: [srcMessageId intValue ]
910+ toFolder: [NSString stringWithFormat: @" /%@ " , dstFolderId]];
959911
960- d = [[s dataUsingEncoding: NSUTF8StringEncoding] xml2wbxml ];
912+ // We extract the destionation message id
913+ dstMessageId = nil ;
961914
962- [theResponse setContent: d];
915+ if ([[response objectForKey: @" result" ] boolValue ]
916+ && (v = [[[response objectForKey: @" RawResponse" ] objectForKey: @" ResponseResult" ] objectForKey: @" flag" ])
917+ && [v hasPrefix: @" COPYUID " ])
918+ {
919+ dstMessageId = [[v componentsSeparatedByString: @" " ] lastObject ];
920+
921+ // We mark the original message as deleted
922+ response = [client storeFlags: [NSArray arrayWithObject: @" Deleted" ]
923+ forUIDs: [NSArray arrayWithObject: srcMessageId]
924+ addOrRemove: YES ];
925+
926+ if ([[response valueForKey: @" result" ] boolValue ])
927+ [(SOGoMailFolder *)currentCollection expunge ];
928+
929+ }
930+
931+ if (!dstMessageId)
932+ {
933+ // FIXME: should we return 1 or 2 here?
934+ [s appendFormat: @" <Status>%d </Status>" , 2 ];
935+ }
936+ else
937+ {
938+ //
939+ // If the MoveItems operation is initiated by an Outlook client, we save the "deviceType+dstMessageId" to use it later in order to
940+ // modify the Sync command from "add" to "change" (see SOGoActiveSyncDispatcher+Sync.m: -processSyncGetChanges: ...).
941+ // This is to avoid Outlook creating dupes when moving messages across folfers.
942+ //
943+ if ([[context objectForKey: @" DeviceType" ] isEqualToString: @" WindowsOutlook15" ])
944+ {
945+ NSString *key;
946+
947+ // The key must be pretty verbose. We use the <uid>+<DeviceType>+<target folder>+<DstMsgId>
948+ key = [NSString stringWithFormat: @" %@ +%@ +%@ +%@ " ,
949+ [[context activeUser ] login ],
950+ [context objectForKey: @" DeviceType" ],
951+ dstFolderId,
952+ dstMessageId];
953+
954+
955+ [[SOGoCache sharedCache ] setValue: @" MovedItem"
956+ forKey: key];
957+ }
958+
959+ // Everything is alright, lets return the proper response. "Status == 3" means success.
960+ [s appendFormat: @" <SrcMsgId>%@ </SrcMsgId>" , srcMessageId];
961+ [s appendFormat: @" <DstMsgId>%@ </DstMsgId>" , dstMessageId];
962+ [s appendFormat: @" <Status>%d </Status>" , 3 ];
963+ }
964+
963965 }
966+ else
967+ {
968+ // Non-mail move operations - unsupported for now.
969+ [s appendFormat: @" <Status>%d </Status>" , 1 ];
970+ }
971+
972+ [s appendString: @" </Response>" ];
964973 }
965- else
966- {
967- [theResponse setStatus: 500 ];
968- [theResponse appendContentString: @" Unsupported move operation" ];
969- }
974+
975+
976+ [s appendString: @" </MoveItems>" ];
977+
978+ d = [[s dataUsingEncoding: NSUTF8StringEncoding] xml2wbxml ];
979+
980+ [theResponse setContent: d];
970981}
971982
972983//
0 commit comments