bool CardDavClient::init()
{
    FUNCTION_CALL_TRACE;

    QString accountIdString = iProfile.key(Buteo::KEY_ACCOUNT_ID);
    m_accountId = accountIdString.toInt();
    if (m_accountId == 0) {
        LOG_CRITICAL("profile does not specify" << Buteo::KEY_ACCOUNT_ID);
        return false;
    }

    m_syncDirection = iProfile.syncDirection();
    m_conflictResPolicy = iProfile.conflictResolutionPolicy();
    if (!m_syncer) {
        m_syncer = new Syncer(this, &iProfile);
        connect(m_syncer, SIGNAL(syncSucceeded()),
                this, SLOT(syncSucceeded()));
        connect(m_syncer, SIGNAL(syncFailed()),
                this, SLOT(syncFailed()));
    }

    return true;
}
Example #2
0
void EvernoteSession::sync(){
    if(syncInProgress){
        return;
    }
    syncInProgress = true;
    syncCancelled = false;

    try{
        for(int i=0;i<5;i++){
            try{

                recreateUserStoreClient(false);
                recreateSyncClient(false);

                qDebug() << "EvernoteSession :: start sync...";
                int cacheUsn = DatabaseManager::instance()->getIntSetting(SettingsKeys::SERVER_USN);
                qDebug() << "EvernoteSession :: saved USN: " << cacheUsn;
                SyncChunk chunk;
                int percent = 0;
                while(true){
                    syncStarted(percent);
                    syncClient->getSyncChunk(chunk, Settings::instance()->getAuthToken().toStdString(), cacheUsn, 1024, false);

                    if(cacheUsn >= chunk.updateCount){
                        break;
                    }
                    percent = (int)((double)(100* (double)cacheUsn/(double)chunk.updateCount));
                    syncStarted(percent);
                    std::vector <Tag> tags = chunk.tags;

                    if(!tags.empty()){


                        tagsSyncStarted();
                        DatabaseManager::instance()->beginTransacton();
                        for(int i=0;i<tags.size();i++){
                            if(syncCancelled){
                                syncCancelled = false;
                                syncInProgress = false;
                                syncFinished();
                                return;
                            }
                            Tag tag = tags.at(i);
                            DatabaseManager::instance()->saveTag(tag);
                            qDebug() << "EvernoteSession :: tag " << tag.name.c_str();
                        }
                        DatabaseManager::instance()->commitTransaction();
                    }
                    syncStarted(percent);
                    if(syncCancelled){
                        syncCancelled = false;
                        syncInProgress = false;
                        syncFinished();
                        return;
                    }

                    std::vector <Notebook> notebooks = chunk.notebooks;
                    qDebug() << "EvernoteSession :: notebooks " << chunk.notebooks.size();
                    if(!notebooks.empty()){


                        notebooksSyncStarted();
                        DatabaseManager::instance()->beginTransacton();
                        for(int i=0;i<notebooks.size();i++){
                            if(syncCancelled){
                                syncCancelled = false;
                                syncInProgress = false;
                                syncFinished();
                                return;
                            }
                            Notebook notebook = notebooks.at(i);
                            DatabaseManager::instance()->saveNotebook(notebook);
                            qDebug() << "EvernoteSession :: notebook " << notebook.name.c_str();
                        }
                        DatabaseManager::instance()->commitTransaction();
                    }
                    syncStarted(percent);
                    if(syncCancelled){
                        syncCancelled = false;
                        syncInProgress = false;
                        syncFinished();
                        return;
                    }
                    std::vector <Note> notes = chunk.notes;
                    qDebug() << "EvernoteSession :: notes " << chunk.notes.size();
                    if(!notes.empty()){
                        DatabaseManager::instance()->beginTransacton();
                        for(int i=0;i<notes.size();i++){
                            if(syncCancelled){
                                syncCancelled = false;
                                syncInProgress = false;
                                syncFinished();
                                return;
                            }
                            Note note = notes.at(i);
                            if(note.deleted){
                                DatabaseManager::instance()->deleteNote(note);
                            }else{
                                DatabaseManager::instance()->saveNote(note);
                            }
                            qDebug() << "EvernoteSession :: note " << note.title.c_str();
                        }
                        DatabaseManager::instance()->commitTransaction();
                    }
                    syncStarted(percent);
                    std::vector <SavedSearch> searches = chunk.searches;
                    qDebug() << "EvernoteSession :: searches " << chunk.searches.size();
                    if(!searches.empty()) {
                        DatabaseManager::instance()->beginTransacton();
                        for(int i=0; i < searches.size(); i++) {
                            SavedSearch search = searches.at(i);
                            DatabaseManager::instance()->saveSavedSearch(search);
                            qDebug() << "EvernoteSession :: search " << search.name.c_str();
                        }
                        DatabaseManager::instance()->commitTransaction();
                    }
                    syncStarted(percent);

                    qDebug() << "expunged notes: " << chunk.expungedNotes.size();

                    cacheUsn = chunk.chunkHighUSN;
                    DatabaseManager::instance()->beginTransacton();
                    DatabaseManager::instance()->makeIntSetting(SettingsKeys::SERVER_USN, cacheUsn);
                    DatabaseManager::instance()->commitTransaction();
                    if(cacheUsn >= chunk.updateCount){
                        break;
                    }
                    qDebug() << "Current usn: " << cacheUsn << " high usn: " << chunk.chunkHighUSN << ", update count: " << chunk.updateCount;
                }

                qDebug() << "EvernoteSession :: sync finished";
                break;
            }catch(EDAMSystemException &e){
                qDebug() << "EvernoteSession :: Edam SYSTEM EXCEPTION " << e.what() << " " << e.errorCode;
                if(e.errorCode == 9){
                    reauth();
                }
            }

        }
    }catch(TException &tx){
        qDebug() << "EvernoteSession :: excetion while sync: " << tx.what();
        syncFailed("Network error");
    }
    syncInProgress = false;
    syncFinished();
    Cache::instance()->load();
}
Example #3
0
void Syncer::signInError()
{
    Q_EMIT syncFailed();
}