📄 contactbuilder.cpp
字号:
return value;
}
int writeToTempFile(FILE* f, char *ptr, int numBytes) {
int numWritten = fwrite(ptr, sizeof(char), numBytes, f);
return numWritten;
}
wstring getPicturePropertyFromMapi(IItem *pContact) {
wstring imagew = L"";
string image = "";
// get the stream properties like images
//http://www.imc.org/pdi/vcard-21.txt
//
IStream *pStream = NULL;
HRESULT hr = pContact->OpenProperty(PIMPR_PICTURE, GENERIC_READ|GENERIC_WRITE, &pStream);
if (hr == S_OK) {
char szBuf[2048];
ULONG ulNumChars = 0;
wchar_t tmpPath[64];
char tmpFileName[MAX_PATH]; // System constants for the path
wchar_t tmpFileNameW[MAX_PATH];
GetTempPath(64, tmpPath);
GetTempFileName(tmpPath, L"pct>", 0, tmpFileNameW);
sprintf(tmpFileName, "%S", tmpFileNameW);
FILE* f = NULL;
f = fopen(tmpFileName, "w+b");
if (f != NULL) {
int m = 0;
do {
pStream->Read(szBuf, 2048, &ulNumChars);
writeToTempFile(f, szBuf, ulNumChars);
m = m + ulNumChars;
} while (ulNumChars >= 2048);
if (f) {
fflush(f);
fclose(f);
}
pStream->Release();
if (m != 0) {
char* ret = loadFromFileAndConvert(tmpFileName, "base64");
wchar_t* imagewchar = toWideChar(ret);
if (ret) {
delete [] ret; ret = NULL;
}
imagew += imagewchar;
if (imagewchar) {
delete [] imagewchar; imagewchar = NULL;
}
}
DeleteFile(tmpFileNameW);
}
}
return imagew;
}
/*
* Use the WinContact object provided by the C++ API.
* This is only for Windows Mobile 5 devices. It uses windows
* api that are a low level.
*/
WinContact* contactToWinContact(IItem *pContact, bool isSif) {
HRESULT hr = E_FAIL;
CEPROPVAL *prgPropvalPoom = NULL;
ULONG cbBuffer = 0;
HANDLE hHeap = GetProcessHeap();
WinContact* winC = NULL;
if (isSif) {
winC = new WinContactSIF();
} else {
winC = new WinContact();
}
wstring value, imagew = L"";
cbBuffer = 0;
hr = pContact->GetProps(PIMPR_props, CEDB_ALLOWREALLOC, cProps, &prgPropvalPoom, &cbBuffer, hHeap);
if(FAILED(hr) || NULL == prgPropvalPoom || 0 == cbBuffer) {
goto Exit;
}
for (int i = 0; i < NUM_COLS; i++) {
if (i == ePIMPR_BIRTHDAY || i == ePIMPR_ANNIVERSARY) {
value = getDatePropertyFromMapi(&prgPropvalPoom[i]);
}
else if (i == ePIMPR_PICTURE) {
// currently it is set empty because if there is an image it will be
// set later with a value
value = L"";
}
else {
value = getStringPropertyFromMapi(&prgPropvalPoom[i]);
}
winC->setProperty(contactFields[i], value);
}
// to get the picture of the contact
imagew = getPicturePropertyFromMapi(pContact);
winC->setProperty(PICTURE, imagew);
hr = S_OK;
Exit:
// Free memory.
HeapFree(hHeap, 0, prgPropvalPoom);
return winC;
}
FILETIME getDatePropertyFromWinContact(const wstring date) {
FILETIME t;
SYSTEMTIME ft;
WCHAR tmp[20];
if (date.size() > 0) {
wsprintf(tmp, date.c_str());
wstring::size_type pos = date.find(L"-", 0);
if (pos != wstring::npos) {
// Old format still accepted: "yyyy-MM-dd"
swscanf(tmp, TEXT("%4d-%2d-%2d"), &ft.wYear, &ft.wMonth, &ft.wDay);
}
else {
// "yyyyMMdd"
swscanf(tmp, TEXT("%4d%2d%2d"), &ft.wYear, &ft.wMonth, &ft.wDay);
}
}
else {
// This is to reset the date (MAX_DATE_DOUBLE)
ft.wYear = 4501;
ft.wMonth = 1;
ft.wDay = 1;
}
ft.wHour = 0;
ft.wMinute = 0;
ft.wSecond = 0;
ft.wMilliseconds = 0;
ft.wDayOfWeek = 0;
SystemTimeToFileTime(&ft, &t);
return t;
}
BOOL winContactToContact(WinContact& winC, IItem *pContact) {
//Handle the picture separately
wstring picture;
if (winC.getProperty(PICTURE, picture)) {
winC.removeElement(PICTURE);
}
HRESULT hr = E_FAIL;
int numberOfProperty = winC.getPropertyMapSize();
int incNumProp = 0;
CEPROPVAL *prgPropvalPoom = new CEPROPVAL[numberOfProperty];
FILETIME date;
bool exists = false;
int i = 0;
for (i = 0; i < NUM_COLS; i++) {
exists = false;
wstring& value = winC.getPropertyRef(contactFields[i], &exists);
if (exists) {
prgPropvalPoom[incNumProp].wFlags = 0;
prgPropvalPoom[incNumProp].wLenData = 0;
if (i == ePIMPR_BIRTHDAY || i == ePIMPR_ANNIVERSARY) {
prgPropvalPoom[incNumProp].propid = PIMPR_props[i];
date = getDatePropertyFromWinContact(value);
prgPropvalPoom[incNumProp].val.filetime = date;
}
else if (i == ePIMPR_PICTURE) {
// Picture is handled later. BTW it should not be here because it is removed
// before
incNumProp--;
}
else {
prgPropvalPoom[incNumProp].propid = PIMPR_props[i];
prgPropvalPoom[incNumProp].val.lpwstr = (wchar_t*)value.c_str();
}
incNumProp++;
}
}
hr = pContact->SetProps(0, incNumProp, prgPropvalPoom);
hr = pContact->Save();
if (FAILED(hr)) {
LOG.error("Error saving item");
}
// insert the image if exists
if (picture != L"") {
const wstring charsToRemove = L" \r\n"; // remove spaces, '\r' and '\n'
IStream* pStream = NULL;
size_t len = 0;
ULONG cbWritten = 0;
int idx;
while( (idx=picture.find_first_of(charsToRemove)) >= 0 ) {
picture.replace( idx, 1, L"" );
}
char* valueA = toMultibyte(picture.c_str());
int rc = b64_decode(valueA, valueA);
if(rc > 0) {
hr = pContact->OpenProperty(PIMPR_PICTURE, GENERIC_READ|GENERIC_WRITE, &pStream);
hr = pStream->Write(valueA, rc, &cbWritten);
pStream->Commit(STGC_DEFAULT);
pStream->Release();
}
if (valueA) { delete [] valueA;}
}
if (prgPropvalPoom) {
delete [] prgPropvalPoom;
}
return TRUE;
}
long getIItemOid(IItem *pContact) {
HRESULT hr = E_FAIL;
CEPROPVAL *prgPropvalPoom = NULL;
int cProps = 1;
CEPROPID rgPropId[1] = {PIMPR_OID};
ULONG cbBuffer = 0;
HANDLE hHeap = GetProcessHeap();
ULONG oid = 0;
hr = pContact->GetProps(rgPropId, CEDB_ALLOWREALLOC, cProps, &prgPropvalPoom, &cbBuffer, hHeap);
if(FAILED(hr) || NULL == prgPropvalPoom || 0 == cbBuffer) {
goto Exit;
}
oid = prgPropvalPoom[0].val.ulVal;
Exit:
hr = S_OK;
// Free memory.
HeapFree(hHeap, 0, prgPropvalPoom);
return oid;
}
/*
* Encode the message in base64, splitting the result in lines of 72 columns
* each.
*/
char* encodeWithSpaces(const char *msg, int len)
{
int i, step=54, dlen=0;
char* res = new char[len*3]; // b64 is 4/3, but we have also the newlines....
memset(res, 0, len*3);
res[0] = ' ';
res[1] = ' ';
res[2] = ' ';
res[3] = ' ';
char* ret = &res[4];
for(i=0; i<len; i+=step) {
if(len-i < step) {
step = len-i;
}
dlen += b64_encode(ret+dlen, (void *)(msg+i), step);
ret[dlen++]='\r';
ret[dlen++]='\n';
ret[dlen++]=' ';
ret[dlen++]=' ';
ret[dlen++]=' ';
ret[dlen++]=' ';
}
// Terminate the string
ret[dlen]=0;
int ll = strlen(res);
return res;
}
char* loadFromFileAndConvert(const char *filename, const char *encoding)
{
char *msg = 0;
bool binary = true;
size_t msglen=0;
char *ret = 0;
if(!filename)
return 0;
if( strcmp(encoding, "base64") == 0 ) {
binary = true;
}
// Read file
if(!readFile(filename, &msg, &msglen, binary))
return 0;
// Encode the file
if( strcmp(encoding, "base64") == 0 ) {
ret = encodeWithSpaces(msg, msglen);
delete [] msg;
}
return ret;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -