mailutils.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 272 行

CPP
272
字号
/*
 * 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 "pim/mail/MailUtils.h"
#include "notify/checknet.h"
#include "Tlhelp32.h"
#include "pim/utils.h"

wchar_t* c2wc (wchar_t t) {
    wchar_t* ret = new wchar_t[2];
    wsprintf(ret, TEXT("%c"), t);
    return ret;
}

wchar_t* convertBinaryToWChar(SBinary sbEntry, wchar_t folder) {

    int len = sbEntry.cb;
    char* b64 = new char[((len/3+1)<<2) + 1];
    len = b64_encode(b64, sbEntry.lpb, len);
    b64[len] = 0;
    wchar_t* t = toWideChar(b64);
    wchar_t* ret = new wchar_t[wcslen(t) + 1 + 2];
    wsprintf(ret, TEXT("%c/%s"), folder, t);

    // AAAAAG8TAAECAAAAAQAAA==
    delete [] b64; b64 = NULL;
    delete [] t; t = NULL;
    return ret;
}

char* convertBinaryToChar(SBinary sbEntry, wchar_t folder) {

    int len = sbEntry.cb;
    char* b64 = new char[((len/3+1)<<2) + 1];
    len = b64_encode(b64, sbEntry.lpb, len);
    b64[len] = 0;
    char* ret = new char[strlen(b64) + 1 + 2];
    sprintf(ret, "%lc/%s", folder, b64);

    // AAAAAG8TAAECAAAAAQAAA==
    delete [] b64; b64 = NULL;
    return ret;
}

/*
 * Check if the t char starts with I/, O/, D/, T/, S/
 */
static inline bool startWithFolder(const char* t) {
    bool ret = false;
    if (strstr(t, "I/") != NULL ||
        strstr(t, "O/") != NULL ||
        strstr(t, "D/") != NULL ||
        strstr(t, "S/") != NULL ||
        strstr(t, "T/") != NULL )

        ret = true;

    return ret;
}

size_t decodeEntryId(const wchar_t *entryId, char **id) {
    char* eid = toMultibyte(entryId);
    if (!eid) {
        LOG.error("decodeEntryId: error coverting entryId");
        return NULL;
    }
    // Skip folder letter + /

    char *p = (startWithFolder(eid)) ? eid+2 : eid ;
    // Decode the id
    *id = new char[strlen(p)];
    size_t len = b64_decode(*id, p);

    /*
    * To fix in the next releases. The b64_decode must return a value -1
    * if the decoding procedure fails.
    * At the moment we handle only the last error code that is 1, set by b64_decode.
    * So here we reset the errorCode to 0 and the lastErrorMsg to "".
    */
    if (getLastErrorCode() == 1) {
        LOG.debug("The decoding of the entry id %s is failed.", eid);
        delete [] *id; 
        *id = NULL;
        setError(0, "");
    }
    delete [] eid;
    return len;
}

ULONG getFolderId(wchar_t t) {

    ULONG ret = -1;

    if (t == INBOX_FOLDER)
        ret = PR_CE_IPM_INBOX_ENTRYID;
    else if (t == OUTBOX_FOLDER)
         ret = PR_IPM_OUTBOX_ENTRYID;
    else if (t == TRASH_FOLDER)
         ret = PR_IPM_WASTEBASKET_ENTRYID;
    else if (t == DRAFT_FOLDER)
         ret = PR_CE_IPM_DRAFTS_ENTRYID;
    else if (t == SENT_FOLDER)
         ret = PR_IPM_SENTMAIL_ENTRYID;
    return ret;
}


void getFolderToSync(MailSyncSourceConfig &sc, OUT wchar_t* tt) {
    int i = 0;

    if (sc.getInbox()) {
        tt[i] = INBOX_FOLDER;
        i++;
    }
    if (sc.getOutbox()) {
        tt[i] = OUTBOX_FOLDER;
        i++;
    }
    if (sc.getDraft()) {
        tt[i] = DRAFT_FOLDER;
        i++;
    }
    if (sc.getSent()) {
        tt[i] = SENT_FOLDER;
        i++;
    }
    if (sc.getTrash()) {
        tt[i] = TRASH_FOLDER;
        i++;
    }
    tt[i] = 0;

}

char* createMessageID(const wchar_t* entryID) {
    const char* address ;

    if(checkNetwork())
        address = getLocalAddress();
    else
        address = "127.0.0.1";

    char* messageID = new char[wcslen(entryID)+strlen(address)+2];

    sprintf(messageID, "%ls@%s", entryID, address);

    return messageID;
}

int closeMailProcess() {
    
     /**
    * Way to close the mail application. It close it gracefully (it should...)
    */
    HWND hwndInbox = FindWindow(_T("Inbox.MainWnd"), NULL); 
    if (hwndInbox) { 
        SendMessage(hwndInbox, WM_CLOSE, 0, 0); 
    }    
    Sleep(2000);
    // try to kill if the first part fails
    DWORD out = 0;
    PROCESSENTRY32 lppe;
    BOOL next = TRUE;
    HANDLE hProcess = 0;

    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); // 0 is however ignored

    if (hSnapshot == INVALID_HANDLE_VALUE)
        return 0;

    lppe.dwSize = sizeof( PROCESSENTRY32 );

    if (!Process32First(hSnapshot, &lppe))
        return 1;

    if (wcsstr(lppe.szExeFile, TEXT("tmail.exe")) != NULL) {
        hProcess = OpenProcess(0, FALSE, lppe.th32ProcessID);
        TerminateProcess(hProcess, -1);
        goto exit;
    }

    do {
        next = Process32Next(hSnapshot, &lppe);

        if (!next)
            break;

        if (wcsstr(lppe.szExeFile, TEXT("tmail.exe")) != NULL ) {
            hProcess = OpenProcess(0, FALSE, lppe.th32ProcessID);
            TerminateProcess(hProcess, -1);
            goto exit;
        }

    } while(next);

exit:
    if (hSnapshot)
        CloseToolhelp32Snapshot(hSnapshot);
    CloseHandle(hProcess);
    return 0;

}

void FileTimeToBasicDate(FILETIME ft, BasicTime *b)
{
    SYSTEMTIME t;
    FILETIME loctime;

    FileTimeToLocalFileTime(&ft, &loctime);
    FileTimeToSystemTime(&loctime, &t);

    b->setYear(t.wYear);
    b->setMonth(t.wMonth);
    b->setWeekday(t.wDayOfWeek);
    b->setDay(t.wDay);
    b->setHour(t.wHour);
    b->setMin(t.wMinute);
    b->setSec(t.wSecond);

    time_t tt = FileTimeToUnixTime(ft);

    TIME_ZONE_INFORMATION tzi;
    SafeGetTimeZoneInformation(&tzi);
    int hour = 0;
    double hourDstMinutes = 0;
    int minutes = 0;
    if (IsDST(&tzi, tt)) {
        hour = abs((tzi.Bias + tzi.DaylightBias)/60);
        hourDstMinutes = (tzi.Bias + tzi.DaylightBias)/60;
    }
    else {
        hour = abs(tzi.Bias/60);
        hourDstMinutes = tzi.Bias*1.0/60;
    }
    minutes = abs((int)(hourDstMinutes*60)) - hour*60;
    if (tzi.Bias > 0)
        hour = -hour;
    b->setTzHour(hour);
    b->setTzMin(minutes);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?