Пример #1
0
// Synchronize remote notebooks with the current database
// If there is a conflict, the remote wins
void SyncRunner::syncRemoteNotebooks(QList<Notebook> books, qint32 account) {
    QLOG_TRACE() << "Entering SyncRunner::syncRemoteNotebooks";
    NotebookTable notebookTable(db);
    LinkedNotebookTable ltable(db);
    SharedNotebookTable stable(db);

    for (int i=0; i<books.size() && keepRunning; i++) {
        Notebook t = books.at(i);

        // There are a few to get the notebook.
        // We can get it by the guid, the share key, the uri, or the name.
        qint32 lid = account;
        if (lid == 0)
            lid = notebookTable.getLid(t.guid);
        if (lid == 0)
            lid = ltable.getLid(t.guid);
        if (lid == 0 && t.sharedNotebooks.isSet()) {
            QList<SharedNotebook> sharedNotebooks = t.sharedNotebooks;
            for (int j=0; j<sharedNotebooks.size() && lid == 0; j++) {
                lid = stable.findById(sharedNotebooks[j].id);
            }
        }
        Publishing publishing;
        if (t.publishing.isSet())
            publishing = t.publishing;
        if (lid == 0 && publishing.uri.isSet()) {
            lid = notebookTable.findByUri(publishing.uri);
        }
        if (lid == 0)
            lid = notebookTable.findByName(t.name);


        if (lid > 0) {
            notebookTable.sync(lid, t);
        } else {
            lid = notebookTable.sync(t);
        }
        changedNotebooks.insert(t.guid, t.name);
        QString stack = "";
        if (t.stack.isSet())
            stack = t.stack;
        bool shared = false;
        if (t.sharedNotebookIds.isSet() || t.sharedNotebooks.isSet())
            shared = true;
        if (account > 0) {
            LinkedNotebookTable ltb(db);
            LinkedNotebook lbook;
            ltb.get(lbook, account);
            if (lbook.username.isSet())
                stack = QString::fromStdString(username);
        }
        if (!finalSync) {
            if (t.name.isSet())
                emit notebookUpdated(lid, t.name, stack, false, shared);
            else
                emit notebookUpdated(lid, "", stack, false, shared);
        }
    }
    QLOG_TRACE() << "Leaving SyncRunner::syncRemoteNotebooks";
}
Пример #2
0
// Synchronize remote linked notebooks
void SyncRunner::syncRemoteLinkedNotebooksChunk(QList<LinkedNotebook> books) {
    LinkedNotebookTable ltable(db);
    for (int i=0; i<books.size(); i++) {
        qint32 lid = ltable.sync(books[i]);
        LinkedNotebook lbk = books[i];
        QString sharename = "";
        QString username = "";
        if (lbk.shareName.isSet())
            sharename = lbk.shareName;
        if (lbk.username.isSet())
            username = lbk.username;
        if (!finalSync)
            emit notebookUpdated(lid, sharename, username, true, false);
    }
}
Пример #3
0
// Synchronize remote linked notebooks
bool SyncRunner::syncRemoteLinkedNotebooksActual() {
    LinkedNotebookTable ltable(db);
    QList<qint32> lids;
    ltable.getAll(lids);
    bool fs;
    for (int i=0; i<lids.size(); i++) {
        LinkedNotebook book;
        qint32 usn = ltable.getLastUpdateSequenceNumber(lids[i]);
        qint32 startingUSN = usn;
        ltable.get(book, lids[i]);
        int chunkSize = 5000;

        // If the share key is set, we need to authenticate
        if (!comm->authenticateToLinkedNotebookShard(book)) {
            this->communicationErrorHandler();
            error = true;
            return false;
        }
        bool more = true;
        SyncState syncState;
        if (!comm->getLinkedNotebookSyncState(syncState, book)) {
            this->communicationErrorHandler();
            error = true;
            return false;
        }
        if (syncState.updateCount <= usn)
            more=false;
        qint32 startingSequenceNumber = usn;
        if (usn == 0)
            fs = true;
        else
            fs = false;


        // ***** STARTING PASS #1
        while (more && keepRunning) {
            SyncChunk chunk;
            if (!comm->getLinkedNotebookSyncChunk(chunk, book, usn, chunkSize, fs)) {
                more = false;
                if (comm->error.type == CommunicationError::EDAMNotFoundException) {
                    ltable.expunge(lids[i]);
                    if (!finalSync)
                        emit(notebookExpunged(lids[i]));
                } else {
                    this->communicationErrorHandler();
                    error = true;
                    return false;
                }
            } else {
                processSyncChunk(chunk, lids[i]);
                usn = chunk.chunkHighUSN;
                if (chunk.updateCount > 0 && chunk.updateCount > startingSequenceNumber) {
                    int pct = (usn-startingSequenceNumber)*100/(chunk.updateCount-startingSequenceNumber);
                    QString sharename = "";
                    if (book.shareName.isSet())
                        sharename = book.shareName;
                    emit setMessage(tr("Downloading ") +QString::number(pct) + tr("% complete for tags in shared notebook ") +sharename + tr("."), defaultMsgTimeout);
                }
                if (!chunk.chunkHighUSN.isSet()|| chunk.chunkHighUSN >= chunk.updateCount)
                    more = false;
            }
        }

        //************* STARTING PASS 2

        usn = startingUSN;
        more = true;
        chunkSize = 50;
        if (error == true)
            more=false;

        QString sharename = "";
        if (book.shareName.isSet())
            sharename = book.shareName;
        emit setMessage(tr("Downloading notes for shared notebook ") +sharename + tr("."), defaultMsgTimeout);
        while (more && keepRunning) {
            SyncChunk chunk;
            if (!comm->getLinkedNotebookSyncChunk(chunk, book, usn, chunkSize, fs)) {
                more = false;
                if (comm->error.type == CommunicationError::EDAMNotFoundException) {
                    ltable.expunge(lids[i]);
                    if (!finalSync)
                        emit(notebookExpunged(lids[i]));
                } else {
                    this->communicationErrorHandler();
                    error = true;
                    return false;
                }
            } else {
                processSyncChunk(chunk, lids[i]);
                usn = chunk.chunkHighUSN;
                if (chunk.updateCount > 0 && chunk.updateCount > startingSequenceNumber) {
                    int pct = (usn-startingSequenceNumber)*100/(chunk.updateCount-startingSequenceNumber);
                    QString sharename = "";
                    if (book.shareName.isSet())
                        sharename = book.shareName;
                    emit setMessage(tr("Downloading ") +QString::number(pct) + tr("% complete for shared notebook ") +sharename + tr("."), defaultMsgTimeout);
                }
                if (!chunk.chunkHighUSN.isSet() || chunk.chunkHighUSN >= chunk.updateCount) {
                    more = false;
                    ltable.setLastUpdateSequenceNumber(lids[i], syncState.updateCount);
                }
            }
        }

        qint32 noteUSN = uploadLinkedNotes(lids[i]);
        if (noteUSN > usn)
            ltable.setLastUpdateSequenceNumber(lids[i], noteUSN);
    }
    TagTable tagTable(db);
    tagTable.cleanupLinkedTags();
    return true;
}
Пример #4
0
// Implement of dropEvent so dropMimeData gets called
void FavoritesView::dropEvent(QDropEvent *event) {
    QTreeView::dropEvent(event);
    const QMimeData* data = event->mimeData();
    QModelIndex droppedIndex = indexAt( event->pos() );
    if (!droppedIndex.isValid())
        return;
    int row = droppedIndex.row();

    qint32 lid = 0;
    if (data->hasFormat("application/x-nixnote-tag")) {
        QByteArray d = data->data("application/x-nixnote-tag");
        lid = d.trimmed().toInt();
        addRecord(lid, FavoritesRecord::Tag, row);
    }

    if (data->hasFormat("application/x-nixnote-note")) {
        QByteArray d = data->data("application/x-nixnote-note");
        lid = d.trimmed().toInt();
        addRecord(lid, FavoritesRecord::Note, row);
    }
    if (data->hasFormat("application/x-nixnote-search")) {
        QByteArray d = data->data("application/x-nixnote-search");
        lid = d.trimmed().toInt();
        addRecord(lid, FavoritesRecord::Search, row);
    }
    if (data->hasFormat("application/x-nixnote-favorite")) {
        QByteArray d = data->data("application/x-nixnote-favorite");
        lid = d.trimmed().toInt();
        FavoritesTable ftable(global.db);
        FavoritesRecord rec;
        if (ftable.get(rec,lid)) {
            ftable.expunge(lid);
            rec.order = row;
            ftable.insert(rec);
        }
    }
    if (data->hasFormat("application/x-nixnote-notebook")) {
        QString d = data->data("application/x-nixnote-notebook");
        int pos = d.indexOf("/");
        if (pos == -1) return;
        int type = d.mid(0,pos).toInt();
        d = d.mid(pos+1);
        pos = d.indexOf("/");
        if (pos == -1) return;
        lid= d.mid(0,pos).toInt();
        QString stack = d.mid(pos+1);
        FavoritesRecord::FavoritesRecordType rectype = FavoritesRecord::LocalNotebook;
        switch (type) {
        case NNotebookViewItem::Local :
            rectype = FavoritesRecord::LocalNotebook;
            break;
        case NNotebookViewItem::Synchronized :
            rectype = FavoritesRecord::SynchronizedNotebook;
            break;
        case NNotebookViewItem::LinkedStack :
            rectype = FavoritesRecord::LinkedStack;
            break;
        case NNotebookViewItem::Conflict :
            rectype = FavoritesRecord::ConflictNotebook;
            break;
        case NNotebookViewItem::Stack :
            rectype = FavoritesRecord::NotebookStack;
            break;
        case NNotebookViewItem::Shared:
            rectype = FavoritesRecord::SharedNotebook;
            break;
        case NNotebookViewItem::Linked :
            rectype = FavoritesRecord::LinkedNotebook;
            break;
        }
        if (lid > 0)
            addRecord(lid, rectype, row);
        else {
            FavoritesTable table(global.db);
            FavoritesRecord record;
            record.type = rectype;
            record.target= stack;
            record.lid = 0;
            record.order = row;
            record.parent = 0;
            qint32 newLid = table.insert(record);

            QList<qint32> lids;
            NotebookTable ntable(global.db);
            ntable.findByStack(lids, stack);
            for (int i=0; i<lids.size(); i++) {
                Notebook book;
                if (ntable.get(book, lids[i])) {
                    FavoritesRecord rec;
                    rec.parent = newLid;
                    if (book.name.isSet())
                        rec.displayName = book.name;
                    rec.type = FavoritesRecord::SynchronizedNotebook;
                    if (ntable.isLocal(lids[i]))
                        rec.type = FavoritesRecord::LocalNotebook;
                    LinkedNotebookTable ltable(global.db);
                    if (ltable.exists(lids[i]))
                        rec.type = FavoritesRecord::LinkedNotebook;
                    SharedNotebookTable stable(global.db);
                    if (stable.exists(lids[i]))
                        rec.type = FavoritesRecord::SharedNotebook;
                    rec.target = lids[i];
                    rec.order = 1;
                    table.add(rec);
                }
            }

        }
    }


    rebuildFavoritesTreeNeeded = true;
    this->loadData();

}