~mzanetti/reminders-app/improve-edit-tags

« back to all changes in this revision

Viewing changes to src/libqtevernote/notesstore.cpp

  • Committer: Michael Zanetti
  • Date: 2015-06-12 09:48:22 UTC
  • mfrom: (449.1.3 trunk)
  • Revision ID: michael.zanetti@canonical.com-20150612094822-a743ndphskuvs4bu
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
#include "jobs/fetchtagsjob.h"
42
42
#include "jobs/createtagjob.h"
43
43
#include "jobs/savetagjob.h"
 
44
#include "jobs/expungetagjob.h"
44
45
 
45
46
#include "libintl.h"
46
47
 
404
405
 
405
406
void NotesStore::expungeNotebook(const QString &guid)
406
407
{
 
408
#ifdef NO_EXPUNGE_NOTEBOOKS
 
409
    // This snipped can be used if the app is compiled with a restricted api key
 
410
    // that can't expunge notebooks on Evernote. Compile with
 
411
    // cmake -DNO_EXPUNGE_NOTEBOOKS=1
407
412
    if (m_username != "@local") {
408
413
        qCWarning(dcNotesStore) << "Account managed by Evernote. Cannot delete notebooks.";
409
414
        m_errorQueue.append(QString(gettext("This account is managed by Evernote. Use the Evernote website to delete notebooks.")));
410
415
        emit errorChanged();
411
416
        return;
412
417
    }
 
418
#endif
413
419
 
414
420
    Notebook* notebook = m_notebooksHash.value(guid);
415
421
    if (!notebook) {
453
459
        }
454
460
    }
455
461
 
456
 
    m_notebooks.removeAll(notebook);
457
 
    m_notebooksHash.remove(notebook->guid());
458
 
    emit notebookRemoved(notebook->guid());
459
 
 
460
 
    QSettings settings(m_cacheFile, QSettings::IniFormat);
461
 
    settings.beginGroup("notebooks");
462
 
    settings.remove(notebook->guid());
463
 
    settings.endGroup();
464
 
 
465
 
    notebook->deleteInfoFile();
466
 
    notebook->deleteLater();
 
462
    if (notebook->lastSyncedSequenceNumber() == 0) {
 
463
        emit notebookRemoved(notebook->guid());
 
464
        m_notebooks.removeAll(notebook);
 
465
        m_notebooksHash.remove(notebook->guid());
 
466
        emit notebookRemoved(notebook->guid());
 
467
 
 
468
        QSettings settings(m_cacheFile, QSettings::IniFormat);
 
469
        settings.beginGroup("notebooks");
 
470
        settings.remove(notebook->guid());
 
471
        settings.endGroup();
 
472
 
 
473
        notebook->deleteInfoFile();
 
474
        notebook->deleteLater();
 
475
    } else {
 
476
        qCDebug(dcNotesStore) << "Setting notebook to deleted:" << notebook->guid();
 
477
        notebook->setDeleted(true);
 
478
        notebook->setUpdateSequenceNumber(notebook->updateSequenceNumber()+1);
 
479
        emit notebookChanged(notebook->guid());
 
480
        syncToCacheFile(notebook);
 
481
 
 
482
        if (EvernoteConnection::instance()->isConnected()) {
 
483
            ExpungeNotebookJob *job = new ExpungeNotebookJob(guid, this);
 
484
            connect(job, &ExpungeNotebookJob::jobDone, this, &NotesStore::expungeNotebookJobDone);
 
485
            EvernoteConnection::instance()->enqueue(job);
 
486
        }
 
487
    }
467
488
}
468
489
 
469
490
QList<Tag *> NotesStore::tags() const
566
587
    syncToCacheFile(tag);
567
588
}
568
589
 
 
590
void NotesStore::expungeTagJobDone(EvernoteConnection::ErrorCode errorCode, const QString &errorMessage, const QString &guid)
 
591
{
 
592
    handleUserError(errorCode);
 
593
    if (errorCode != EvernoteConnection::ErrorCodeNoError) {
 
594
        qCWarning(dcSync) << "Error expunging tag:" << errorMessage;
 
595
        return;
 
596
    }
 
597
 
 
598
    if (!m_tagsHash.contains(guid)) {
 
599
        qCWarning(dcSync) << "Received a response for a expungeTag call, but can't find tag around any more.";
 
600
        return;
 
601
    }
 
602
 
 
603
    emit tagRemoved(guid);
 
604
    Tag *tag = m_tagsHash.take(guid);
 
605
    m_tags.removeAll(tag);
 
606
 
 
607
    QSettings cacheFile(m_cacheFile, QSettings::IniFormat);
 
608
    cacheFile.beginGroup("tags");
 
609
    cacheFile.remove(guid);
 
610
    cacheFile.endGroup();
 
611
    tag->syncToInfoFile();
 
612
 
 
613
    tag->deleteInfoFile();
 
614
    tag->deleteLater();
 
615
}
 
616
 
569
617
void NotesStore::tagNote(const QString &noteGuid, const QString &tagGuid)
570
618
{
571
619
    Note *note = m_notesHash.value(noteGuid);
1036
1084
                syncToCacheFile(notebook);
1037
1085
            }
1038
1086
        } else {
1039
 
            // Local notebook changed. See if we can push our changes
1040
1087
            if (result.updateSequenceNum == notebook->lastSyncedSequenceNumber()) {
1041
 
                qCDebug(dcNotesStore) << "Local Notebook changed. Uploading changes to Evernote:" << notebook->guid();
1042
 
                SaveNotebookJob *job = new SaveNotebookJob(notebook);
1043
 
                connect(job, &SaveNotebookJob::jobDone, this, &NotesStore::saveNotebookJobDone);
1044
 
                EvernoteConnection::instance()->enqueue(job);
1045
 
                notebook->setLoading(true);
1046
 
                emit notebookChanged(notebook->guid());
 
1088
                // Local notebook changed. See if we can push our changes
 
1089
                if (notebook->deleted()) {
 
1090
                    qCDebug(dcNotesStore) << "Local notebook has been deleted. Deleting from server";
 
1091
                    expungeNotebook(notebook->guid());
 
1092
                } else {
 
1093
                    qCDebug(dcNotesStore) << "Local Notebook changed. Uploading changes to Evernote:" << notebook->guid();
 
1094
                    SaveNotebookJob *job = new SaveNotebookJob(notebook);
 
1095
                    connect(job, &SaveNotebookJob::jobDone, this, &NotesStore::saveNotebookJobDone);
 
1096
                    EvernoteConnection::instance()->enqueue(job);
 
1097
                    notebook->setLoading(true);
 
1098
                    emit notebookChanged(notebook->guid());
 
1099
                }
1047
1100
            } else {
1048
1101
                qCWarning(dcNotesStore) << "Sync conflict in notebook:" << notebook->name();
1049
1102
                qCWarning(dcNotesStore) << "Resolving of sync conflicts is not implemented yet.";
1140
1193
        } else {
1141
1194
            // local tag changed. See if we can sync it to the server
1142
1195
            if (result.updateSequenceNum == tag->lastSyncedSequenceNumber()) {
1143
 
                SaveTagJob *job = new SaveTagJob(tag);
1144
 
                connect(job, &SaveTagJob::jobDone, this, &NotesStore::saveTagJobDone);
1145
 
                EvernoteConnection::instance()->enqueue(job);
1146
 
                tag->setLoading(true);
1147
 
                emit tagChanged(tag->guid());
 
1196
                if (tag->deleted()) {
 
1197
                    qCDebug(dcNotesStore) << "Tag has been deleted locally";
 
1198
                    expungeTag(tag->guid());
 
1199
                } else {
 
1200
                    SaveTagJob *job = new SaveTagJob(tag);
 
1201
                    connect(job, &SaveTagJob::jobDone, this, &NotesStore::saveTagJobDone);
 
1202
                    EvernoteConnection::instance()->enqueue(job);
 
1203
                    tag->setLoading(true);
 
1204
                    emit tagChanged(tag->guid());
 
1205
                }
1148
1206
            } else {
1149
1207
                qCWarning(dcSync) << "CONFLICT in tag" << tag->name();
1150
1208
                tag->setSyncError(true);
1479
1537
        qCWarning(dcSync) << "Error expunging notebook:" << errorMessage;
1480
1538
        return;
1481
1539
    }
 
1540
 
 
1541
    if (!m_notebooksHash.contains(guid)) {
 
1542
        qCWarning(dcSync) << "Received a response for a expungeNotebook call, but can't find notebook around any more.";
 
1543
        return;
 
1544
    }
 
1545
 
1482
1546
    emit notebookRemoved(guid);
1483
1547
    Notebook *notebook = m_notebooksHash.take(guid);
1484
1548
    m_notebooks.removeAll(notebook);
 
1549
 
 
1550
    QSettings settings(m_cacheFile, QSettings::IniFormat);
 
1551
    settings.beginGroup("notebooks");
 
1552
    settings.remove(notebook->guid());
 
1553
    settings.endGroup();
 
1554
 
 
1555
    notebook->deleteInfoFile();
1485
1556
    notebook->deleteLater();
1486
1557
}
1487
1558
 
1718
1789
 
1719
1790
void NotesStore::expungeTag(const QString &guid)
1720
1791
{
 
1792
#ifdef NO_EXPUNGE_TAGS
 
1793
    // This snipped can be used if the app is compiled with a restricted api key
 
1794
    // that can't expunge tags on Evernote. Compile with
 
1795
    // cmake -DNO_EXPUNGE_TAGS=1
 
1796
 
1721
1797
    if (m_username != "@local") {
1722
1798
        qCWarning(dcNotesStore) << "This account is managed by Evernote. Cannot delete tags.";
1723
1799
        m_errorQueue.append(gettext("This account is managed by Evernote. Please use the Evernote website to delete tags."));
1724
1800
        emit errorChanged();
1725
1801
        return;
1726
1802
    }
 
1803
#endif
1727
1804
 
1728
1805
    Tag *tag = m_tagsHash.value(guid);
1729
1806
    if (!tag) {
1736
1813
        Note *note = m_notesHash.value(noteGuid);
1737
1814
        if (!note) {
1738
1815
            qCWarning(dcNotesStore) << "Tag holds note" << noteGuid << "which hasn't been found in Notes Store";
 
1816
            Q_ASSERT(false);
1739
1817
            continue;
1740
1818
        }
1741
1819
        untagNote(noteGuid, guid);
1742
1820
    }
1743
1821
 
1744
 
    emit tagRemoved(guid);
1745
 
    m_tagsHash.remove(guid);
1746
 
    m_tags.removeAll(tag);
1747
 
 
1748
 
    QSettings cacheFile(m_cacheFile, QSettings::IniFormat);
1749
 
    cacheFile.beginGroup("tags");
1750
 
    cacheFile.remove(guid);
1751
 
    cacheFile.endGroup();
1752
 
    tag->syncToInfoFile();
1753
 
 
1754
 
    tag->deleteInfoFile();
1755
 
    tag->deleteLater();
 
1822
    if (tag->lastSyncedSequenceNumber() == 0) {
 
1823
        emit tagRemoved(guid);
 
1824
        m_tagsHash.remove(guid);
 
1825
        m_tags.removeAll(tag);
 
1826
 
 
1827
        QSettings cacheFile(m_cacheFile, QSettings::IniFormat);
 
1828
        cacheFile.beginGroup("tags");
 
1829
        cacheFile.remove(guid);
 
1830
        cacheFile.endGroup();
 
1831
        tag->syncToInfoFile();
 
1832
 
 
1833
        tag->deleteInfoFile();
 
1834
        tag->deleteLater();
 
1835
    } else {
 
1836
        qCDebug(dcNotesStore) << "Setting tag to deleted:" << tag->guid();
 
1837
        tag->setDeleted(true);
 
1838
        tag->setUpdateSequenceNumber(tag->updateSequenceNumber()+1);
 
1839
        emit tagChanged(tag->guid());
 
1840
        syncToCacheFile(tag);
 
1841
 
 
1842
        if (EvernoteConnection::instance()->isConnected()) {
 
1843
            ExpungeTagJob *job = new ExpungeTagJob(guid, this);
 
1844
            connect(job, &ExpungeTagJob::jobDone, this, &NotesStore::expungeTagJobDone);
 
1845
            EvernoteConnection::instance()->enqueue(job);
 
1846
        }
 
1847
    }
1756
1848
}
1757
1849
 
1758
1850
void NotesStore::resolveConflict(const QString &noteGuid, NotesStore::ConflictResolveMode mode)