maincpp.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 1,441 行 · 第 1/4 页

CPP
1,441
字号
            NULL,
            REG_OPTION_NON_VOLATILE,
            KEY_ALL_ACCESS,
            NULL,
            &key,
            &res
            );

    if (key == 0) {
        //lastErrorCode = ERR_INVALID_CONTEXT;
        //sprintf(lastErrorMsg, "Invalid context path: %s", "Software/" ROOT_CONTEXT "/spds/sources/mails");
        setErrorF(ERR_INVALID_CONTEXT, "Invalid context path: %s", "Software/" ROOT_CONTEXT "/spds/sources/mails");
        goto finally;
    }

    // Get value length
    err = RegQueryValueEx(
            key,
            p,
            NULL,
            NULL,  // we currently support only strings
            NULL,
            &dim
            );

    if (err == ERROR_SUCCESS) {
        if (dim > 0) {
            buf = new TCHAR[dim + 1];

            err = RegQueryValueEx(
                    key,
                    p,
                    NULL,
                    NULL,  // we currently support only strings
                    (UCHAR*)buf,
                    &dim
                    );
        }
    }
    //else MessageBox(NULL,  T("Error"), T("getConfigParameter"), MB_OK);

    if (!buf)
        buf = wstrdup(TEXT(""));

finally:

    if (key != 0) {
        RegCloseKey(key);
    }

    return buf;
}


/*
* return code -10: no source to sync
*/

DWORD checkStartSync() {

    DWORD pid = 0;
    HANDLE hMutex = NULL;
    // Try to create the mutex
    hMutex = CreateMutex(NULL, TRUE, TEXT("FunSyncInProgress") );
    if(hMutex){
        DWORD err = GetLastError();
        if(err == ERROR_ALREADY_EXISTS){
            pid = getProcessPid( TEXT("startsync.exe") );
        }
        CloseHandle( hMutex );
    }
    return pid;

}

/*
* To get the complete mail id. The AdviseSink of the syncml transport returns an array of /id1, /id2
* But the complete id mapped with the server is I/id1, S/id2. So to ask the right mail is necessary to retrieve
* the complete id that is stored in the files containing the cached situation
*/
static wchar_t* findMailIdInFile(const wchar_t* fileName, const wchar_t* partialID) {

    wchar_t* idComplete = NULL;
    wchar_t line[512];
    FILE* f = _wfopen(fileName, TEXT("r"));

    if (f == NULL)
        return NULL;

    while(fgetws(line, 511, f) != NULL) {

        if (wcsstr(line, partialID ) != NULL) {
            if (idComplete = getElementContent(line, TEXT("id"), 0))
                break;

        }
    }
    fclose(f);
    return idComplete;
}

static wchar_t* completeMailId(const wchar_t* path, const wchar_t* partialID) {

    bool isLast = false;
    wchar_t fileName[255];
    wchar_t* idComplete = NULL;

    WIN32_FIND_DATA FileData;
    wchar_t filter[255];
    wsprintf(filter, TEXT("%s\\mails_*"), path);
    HANDLE hFind = FindFirstFile(filter, &FileData);

    if (hFind == INVALID_HANDLE_VALUE) {
        LOG.debug("No mails_ file found.");
        return NULL;
    }

    while (!isLast) {
        wsprintf(fileName, TEXT("%s\\%s"), path, FileData.cFileName);
        idComplete = findMailIdInFile(fileName, partialID);

        if (idComplete) { // id completed found
            break;
        } else {
            if (!FindNextFile(hFind, &FileData))
                isLast = TRUE;
        }
    }

    FindClose(hFind);
    return idComplete;

}

/*
* Return the number of email that are not sent in outbox folder
* When the connector will send the messate cause for every item
* we can get the message too
*/

static wstring getMailUnsent(MailSyncSource& mailSource) {

    wstring messageToRet;
    SyncSourceReport* rep = mailSource.getReport();
    int res = rep->getItemReportFailedCount(SERVER, COMMAND_ADD);
    int number = 0, i = 0;
    BOOL found = FALSE;
    for (i = 0; i < res; i++) {
        ItemReport* r = rep->getItemReport(SERVER, COMMAND_ADD, i);
        if (wcsstr(r->getId(), L"O/") != 0 && (r->getStatus() == 500 || r->getStatus() == 418 || r->getStatus() == 419)) {
            found = TRUE;
            if (r->getStatusMessage()) {
                LOG.debug("The outbox mail (id: %S) cannot be sent. Server says: %S", r->getId(), r->getStatusMessage());
                messageToRet = r->getStatusMessage();
                if (messageToRet.length() > 130) {
                    messageToRet = messageToRet.substr(0,130);
                    messageToRet.append(L"...");
                }

            }
            number++;
        }
    }

    if (!found) {
        res = rep->getItemReportFailedCount(SERVER, COMMAND_REPLACE);
        for (i = 0; i < res; i++) {
            ItemReport* r = rep->getItemReport(SERVER, COMMAND_REPLACE, i);
            if (wcscmp(r->getId(), L"O/") != 0 && (r->getStatus() == 500 || r->getStatus() == 418 || r->getStatus() == 419)) {
                if (r->getStatusMessage()) {
                    LOG.debug("The outbox mail (id: %S) cannot be sent. Server says: %S", r->getId(), r->getStatusMessage());
                    messageToRet = r->getStatusMessage();
                    if (messageToRet.length() > 130) {
                        messageToRet = messageToRet.substr(0,130);
                        messageToRet.append(L"...");
                    }
                }
                number++;
            }
        }

    }

    if (number == 1) {
        /*
        * Added extension to the name if there isn't one
        */
        // messageToRet = getLocalizationUtils()->getLocalizationString(IDS_ONE_OUT_MESSAGE_NOT_SENT);
        messageToRet = getEmailErrorMessage(messageToRet);
        
        /*
        wstring msg(messageToRet);
        std::transform(messageToRet.begin(), messageToRet.end(), msg.begin(), tolower);

        if (messageToRet == L"" || (msg.find(L"error sending") == wstring::npos &&
                                    msg.find(L"error adding") == wstring::npos)
                                ||  msg.find(L"com.funambol") != wstring::npos ) {
            messageToRet = getLocalizationUtils()->getLocalizationString(IDS_ONE_OUT_MESSAGE_NOT_SENT);
        } else {
            messageToRet.insert(0, getLocalizationUtils()->getLocalizationString(IDS_ONE_OUT_MESSAGE_NOT_SENT));
        }
        */
    } else {
        messageToRet = getLocalizationUtils()->getLocalizationString(IDS_MORE_OUT_MESSAGE_NOT_SENT);
    }

    return messageToRet;
}

DWORD WINAPI cleanupAllItems (vector<const wchar_t*> sources) {

    wchar_t path[DIM_PATH];
    getClientConfigurationInternal(NULL, PROPERTY_SPDM_PATH_INFO, path, NULL);

    int retValue = performSync(*(ClientSettings::getInstance()), path, sources, NULL, NULL, true);
    return retValue;
}


/**
 * Reads the property 'queueList' from Win registry and returns a
 * NULL terminated array of source names to sync (new buffer).
 * The 'queueList' is cleared at the end.
 * The 'queueList' value is a bitmask value used to identify which sources
 * have to be synced on the next (queued) sync.
 * The 'queueList' bitmask is formatted as:
 *      bit 0  ->  mail
 *      bit 1  ->  contact
 *      bit 2  ->  calendar
 *      bit 3  ->  task
 *      bit 4  ->  note
 *      bit 5  ->  briefcase
 *
 * @return  the NULL terminated array of source names to sync
 */
wchar_t** readQueueList() {

    wchar_t** sources    = NULL;
    DMTree* dmt          = NULL;
    ManagementNode* node = NULL;
    char* value          = NULL;
    int mask = 0;
    list<wstring> sourceList;
    list<wstring>::iterator it;

    dmt = DMTreeFactory::getDMTree(APPLICATION_URI);
    if (!dmt)   goto finally;
    node = dmt->readManagementNode(APPLICATION_URI);
    if (!node)  goto finally;

    // Check if we use the queue (queuePush = "1")
    value = node->readPropertyValue(CTP_QUEUE_PUSH);
    if ( !value || strcmp(value, "1") ) {
        // Not using queuePush
        goto finally;
    }

    // Read current value of 'queueList' from registry
    if (value) {
        delete [] value; value = NULL;
    }
    value = node->readPropertyValue(CTP_QUEUE_LIST);
    if (!value) {
        goto finally;
    }
    mask = atoi(value);
    if (mask <= 0) {
        goto finally;
    }


    // Create the NULL terminated array of source names
    // (first need the number of sources, to allocate the WCHAR**)
    sourceList.clear();
    if (mask &  1)  sourceList.push_back(MAILS_NAME       );
    if (mask &  2)  sourceList.push_back(CONTACTS_NAME    );
    if (mask &  4)  sourceList.push_back(APPOINTMENTS_NAME);
    if (mask &  8)  sourceList.push_back(TASKS_NAME       );
    if (mask & 16)  sourceList.push_back(NOTES_NAME       );
    if (mask & 32)  sourceList.push_back(FILES_NAME       );

    int i=0;
    sources = new wchar_t*[sourceList.size()+1];
    it = sourceList.begin();
    while (it != sourceList.end()) {
        sources[i++] = wstrdup((*it).c_str());
        it++;
    }
    sources[i] = NULL;


    // Clear the 'queueList' value on registry.
    node->setPropertyValue("queueList", "0");
    node->setPropertyValue("queuePush", "1");

finally:

    if (dmt)   delete dmt;
    if (node)  delete node;
    if (value) delete [] value;

    return sources;
}

/**
 * Reads and updates the property 'queueList' from Win registry with
 * the list of sources passed as parameter.
 * The 'queueList' value is a bitmask value used to identify which sources
 * have to be synced on the next (queued) sync. This value will be read
 * at the end of the sync, and a new sync will automatically start (if value != 0).
 * The 'queueList' bitmask is formatted as:
 *      bit 0  ->  mail
 *      bit 1  ->  contact
 *      bit 2  ->  calendar
 *      bit 3  ->  task
 *      bit 4  ->  note
 *      bit 5  ->  briefcase
 *
 * @param sources  the NULL terminated array of source names
 */
void updateQueueList(const wchar_t** sources) {

    if (!sources) {
        return;
    }
    DMTree* dmt          = NULL;
    ManagementNode* node = NULL;
    char* value          = NULL;
    int mask = 0;
    char buf[4];

    dmt = DMTreeFactory::getDMTree(APPLICATION_URI);
    if (!dmt)   goto finally;
    node = dmt->readManagementNode(APPLICATION_URI);
    if (!node)  goto finally;

    // Check if we use the queue (queuePush = "1")
    value = node->readPropertyValue(CTP_QUEUE_PUSH);
    if ( !value || strcmp(value, "1") ) {
        // Not using queuePush
        goto finally;
    }

    // Read current value of 'queueList' from registry
    if (value) {
        delete [] value; value = NULL;
    }
    value = node->readPropertyValue(CTP_QUEUE_LIST);
    if (!value) value = stringdup("0");
    mask = atoi(value);

    //
    // Update the bit-mask with current sources
    //
    for (int i=0; sources[i]; i++) {
        if (!wcscmp(sources[i], MAILS_NAME)) {
            mask = mask | 1;
        }
        else if (!wcscmp(sources[i], CONTACTS_NAME)) {
            mask = mask | 2;
        }

⌨️ 快捷键说明

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