This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ItemsMovedJob.cpp
162 lines (124 loc) · 3.85 KB
/
ItemsMovedJob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "ItemsMovedJob.h"
ItemsMovedJob::ItemsMovedJob(Akonadi::Item::List const& items, const Akonadi::Collection &sourceCollection, const Akonadi::Collection &destinationCollection, Session* session) : items(items), sourceCollection(sourceCollection), destinationCollection(destinationCollection), session(session) {
lpSrcFolder = NULL;
lpDstFolder = NULL;
}
ItemsMovedJob::~ItemsMovedJob() {
if(lpSrcFolder) {
lpSrcFolder->Release();
}
if(lpDstFolder) {
lpDstFolder->Release();
}
}
void ItemsMovedJob::start() {
HRESULT hr = session->init();
if(hr != hrSuccess) {
setError((int) hr);
emitResult();
return;
}
LPMDB lpStore = session->getLpStore();
SBinary sEntryID;
QString remoteIdSrc = sourceCollection.remoteId();
hr = session->EntryIDFromSourceKey(remoteIdSrc, sEntryID);
if(hr != hrSuccess) {
setError((int) hr);
emitResult();
return;
}
qDebug() << "Source " << remoteIdSrc;
ULONG ulObjType;
hr = lpStore->OpenEntry(sEntryID.cb,
(LPENTRYID) sEntryID.lpb,
&IID_IMAPIFolder, 0, &ulObjType,
(LPUNKNOWN *)&lpSrcFolder);
if(hr != hrSuccess) {
setError((int) hr);
emitResult();
return;
}
QString remoteIdDst = destinationCollection.remoteId();
qDebug() << "Destination " << remoteIdDst;
hr = session->EntryIDFromSourceKey(remoteIdDst, sEntryID);
if(hr != hrSuccess) {
setError((int) hr);
emitResult();
return;
}
hr = lpStore->OpenEntry( sEntryID.cb,
(LPENTRYID) sEntryID.lpb,
&IID_IMAPIFolder, MAPI_MODIFY, &ulObjType,
(LPUNKNOWN *)&lpDstFolder);
if(hr != hrSuccess) {
setError((int) hr);
emitResult();
return;
}
qDebug() << "Items count " << items.count();
SBinaryArray ba = {0, NULL};
ba.cValues = items.count();
ba.lpbin = new SBinary[items.count()];
for(int i=0; i < items.count(); i++) {
QStringList splitArr = items[i].remoteId().split(":");
QString itemSourceKey = splitArr[1];
LPMESSAGE lpSrcMessage = NULL;
LPMESSAGE lpDstMessage = NULL;
qDebug() << "Item " << itemSourceKey;
session->EntryIDFromSourceKey(remoteIdSrc, itemSourceKey, sEntryID);
hr = lpStore->OpenEntry(sEntryID.cb, (LPENTRYID) sEntryID.lpb,
&IID_IMessage, 0, &ulObjType,
(LPUNKNOWN*)&lpSrcMessage);
if(hr != hrSuccess) {
qDebug() << "OpenEntry failed";
setError((int) hr);
emitResult();
return;
}
hr = lpDstFolder->CreateMessage(NULL, 0,
&lpDstMessage);
if(hr != hrSuccess) {
qDebug() << "CreateMessage failed";
setError((int) hr);
emitResult();
return;
}
hr = lpSrcMessage->CopyTo(0, NULL, NULL, 0, NULL, &IID_IMessage,
(LPVOID) lpDstMessage, 0, NULL);
if(hr != hrSuccess) {
qDebug() << "CopyTo failed";
setError((int) hr);
emitResult();
return;
}
hr = lpDstMessage->SaveChanges(0);
if(hr != hrSuccess) {
qDebug() << "SaveChanges failed";
setError((int) hr);
emitResult();
return;
}
ba.lpbin[i] = sEntryID;
lpSrcMessage->Release();
lpSrcMessage = NULL;
LPSPropValue lpPropVal = NULL;
hr = HrGetOneProp(lpDstMessage, PR_SOURCE_KEY, &lpPropVal);
if (hr != hrSuccess) {
qDebug() << "GetOneProp failed";
setError((int) hr);
emitResult();
return;
}
QString strSourceKey;
Bin2Hex(lpPropVal->Value.bin, strSourceKey);
qDebug() << "New source key " << strSourceKey;
items[i].setRemoteId(remoteIdDst + ":" + strSourceKey);
items[i].setRemoteRevision(QString::number(1));
items[i].setParentCollection(destinationCollection);
lpDstMessage->Release();
lpDstMessage = NULL;
}
lpSrcFolder->DeleteMessages(&ba, 0, NULL, 0);
qDebug() << "Emitting result";
emitResult();
}