mapiutils.cpp
来自「funambol window mobile客户端源代码」· C++ 代码 · 共 401 行
CPP
401 行
/*
* Funambol is a mobile platform developed by Funambol, Inc.
* Copyright (C) 2003 - 2007 Funambol, Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*
* You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
* 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by Funambol" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by Funambol".
*/
#include <stdio.h>
#include <windows.h>
#include <Winbase.h>
#include <tchar.h>
#include <imaging.h>
#include "base/Log.h"
#include "base/util/utils.h"
#include "pim/Utils.h"
#include "pim/ContactBuilder.h"
#include "pim/MAPIUtils.h"
#include <string>
using namespace std;
#define WSTRING_NULL TEXT("__@_N_U_L_L___")
long getIItemOid(IItem *pItem) {
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 = pItem->GetProps(rgPropId, CEDB_ALLOWREALLOC, cProps, &prgPropvalPoom, &cbBuffer, hHeap);
if(FAILED(hr) || NULL == prgPropvalPoom || 0 == cbBuffer) {
goto Exit;
}
oid = prgPropvalPoom[0].val.ulVal;
Exit:
// Free memory.
HeapFree(hHeap, 0, prgPropvalPoom);
return oid;
}
wstring getStringPropertyFromMapi(const CEPROPVAL *pValSrc) {
wstring value = L"";
if (!(pValSrc->wFlags & CEDB_PROPNOTFOUND)) {
wchar_t* val = pValSrc->val.lpwstr;
if (val && wcslen(val)>0) {
value = val;
}
}
return value;
/*
This piece of code will be fine when the DevInfo is
proper handled by the client and the server.
When the client will send its capability, it can
send only the field that are filled. The server will
erase the other that doesn't receive
if (!(pValSrc->wFlags & CEDB_PROPNOTFOUND)) {
wstring value(pValSrc->val.lpwstr);
return value;
} else {
return WSTRING_NULL;
}
*/
}
wstring getDatePropertyFromMapi(const CEPROPVAL *pValSrc, bool toUTC, bool onlyDate) {
wstring value = TEXT("");
if (!(pValSrc->wFlags & CEDB_PROPNOTFOUND)) {
SYSTEMTIME t;
FILETIME d = pValSrc->val.filetime;
FileTimeToSystemTime(&d, &t);
if (t.wYear > 4000) {
return TEXT("");
}
if (toUTC) {
localTimeToUTC2(t);
}
wchar_t tempDate[50];
wsprintf(tempDate, TEXT("%04d%02d%02d"), t.wYear, t.wMonth, t.wDay);
if (!onlyDate) {
wsprintf(&tempDate[8], TEXT("T%02i%02i%02i"), t.wHour, t.wMinute, t.wSecond);
}
value = tempDate;
if (!onlyDate && toUTC) {
value += TEXT("Z");
}
}
return value;
}
int getIntPropertyFromMapi(const CEPROPVAL *pValSrc) {
int value = 0;
if (!(pValSrc->wFlags & CEDB_PROPNOTFOUND)) {
value = pValSrc->val.iVal;
}
return value;
}
TIME_ZONE_INFORMATION* readTimezoneFromMapi(const CEPROPVAL *pValSrc) {
TIME_ZONE_INFORMATION* tz = NULL;
if (!(pValSrc->wFlags & CEDB_PROPNOTFOUND)) {
DWORD length = pValSrc->val.blob.dwCount;
if (length > 0) {
byte* data = new byte[length+1];
memcpy(data, pValSrc->val.blob.lpb, length);
tz = (TIME_ZONE_INFORMATION*)data;
}
}
return tz;
}
wstring getPicturePropertyFromMapi(IItem *pContact, WinContact& winC) {
wstring imagew = L"";
string image = "";
IImagingFactory* pFactory = NULL;
IImage* pImage = NULL;
ImageInfo imgInfo = {0};
wstring photoType = L"";
// 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) {
// try to understand the format of the image
hr = CoCreateInstance(CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER,
IID_IImagingFactory, (void**) &pFactory);
if (hr == S_OK) {
hr = pFactory->CreateImageFromStream(pStream, &pImage);
if (hr == S_OK) {
hr = pImage->GetImageInfo(&imgInfo);
if (hr == S_OK) {
if (imgInfo.RawDataFormat == IMGFMT_JPEG) {
photoType = TEXT("JPEG");
} else if (imgInfo.RawDataFormat == IMGFMT_GIF) {
photoType = TEXT("GIF");
} else if (imgInfo.RawDataFormat == IMGFMT_BMP) {
photoType = TEXT("BMP");
} else if (imgInfo.RawDataFormat == IMGFMT_PNG) {
photoType = TEXT("PNG");
} else if (imgInfo.RawDataFormat == IMGFMT_PNG) {
photoType = TEXT("TIFF");
}
}
}
}
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");
LARGE_INTEGER l; l.HighPart = 0; l.LowPart = 0;
if (f != NULL) {
int m = 0;
pStream->Seek(l, STREAM_SEEK_SET, NULL);
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);
}
}
if (pFactory != NULL) {
pFactory->Release();
pFactory = NULL;
}
if (pImage != NULL) {
pImage->Release();
pImage = NULL;
}
if (imagew != TEXT("")) {
winC.setPhotoType(photoType);
}
return imagew;
}
void setIntPropertyToMapi(CEPROPVAL *pValSrc, CEPROPID propId, const int propValue) {
pValSrc->wFlags = 0;
pValSrc->wLenData = 0;
pValSrc->propid = propId;
pValSrc->val.lVal = propValue;
}
void setDatePropertyToMapi(CEPROPVAL *pValSrc, CEPROPID propId, const FILETIME propValue) {
pValSrc->wFlags = 0;
pValSrc->wLenData = 0;
pValSrc->propid = propId;
pValSrc->val.filetime = propValue;
}
void setStringPropertyToMapi(CEPROPVAL *pValSrc, CEPROPID propId, const wstring& propValue) {
pValSrc->wFlags = 0;
pValSrc->wLenData = 0;
pValSrc->propid = propId;
pValSrc->val.lpwstr = (wchar_t*)propValue.c_str();
}
void setTimezoneToMapi(CEPROPVAL *pValSrc, CEPROPID propId, const TIME_ZONE_INFORMATION* tz) {
pValSrc->wFlags = 0;
pValSrc->wLenData = 0;
pValSrc->propid = propId;
if (tz) {
pValSrc->val.blob.lpb = (byte*)tz;
}
}
HRESULT saveItemProps(IItem* pItem, int numProps, CEPROPVAL* prgPropvalPoom) {
//LOG.debug("Setting %d properties into IItem...", numProps);
HRESULT hr = pItem->SetProps(0, numProps, prgPropvalPoom);
if (FAILED(hr)) {
LOG.error("Error setting properties to pOutlook. Please check if incoming item is correct.");
return hr;
}
//LOG.debug("Saving IItem...");
return pItem->Save();
}
// ------------- move to API ---------------
FILETIME getDatePropertyFromString(const wstring& date, bool onlyDate) {
FILETIME ft;
SYSTEMTIME t;
WCHAR tmp[20];
int len = date.size();
bool isUTC = false;
if (len > 0) {
wsprintf(tmp, date.c_str());
wstring::size_type pos = date.find(L"-", 0);
if (pos == wstring::npos) {
// "yyyyMMdd"
swscanf(tmp, TEXT("%4d%2d%2d"), &t.wYear, &t.wMonth, &t.wDay);
if (len > 9 && len < 17) {
// "hhmmss"
swscanf(&tmp[9], L"%2d%2d%2d", &t.wHour, &t.wMinute, &t.wSecond);
// Check if UTC format.
pos = date.find(L"Z", 8);
if (pos != wstring::npos) isUTC = true;
}
else {
t.wHour = 0;
t.wMinute = 0;
t.wSecond = 0;
}
}
else {
// Old format still accepted: "yyyy-MM-dd"
swscanf(tmp, TEXT("%4d-%2d-%2d"), &t.wYear, &t.wMonth, &t.wDay);
t.wHour = 0;
t.wMinute = 0;
t.wSecond = 0;
}
}
else {
// This is to reset the date (MAX_DATE_DOUBLE)
t.wYear = 4501;
t.wMonth = 1;
t.wDay = 1;
}
t.wMilliseconds = 0;
t.wDayOfWeek = 0;
// Convert to local-time if necessary.
if(!onlyDate && isUTC) {
UTCToLocalTime2(t);
}
// Force hours to 00:00 if asked.
if (onlyDate) {
t.wHour = 0;
t.wMinute = 0;
t.wSecond = 0;
}
SystemTimeToFileTime(&t, &ft);
return ft;
}
int getIntPropertyFromWinItem(const wstring& value) {
int ret = 0;
if (value.size() > 0) {
ret = _wtoi(value.c_str());
}
return ret;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?