559e0029a686ab9309f69741ddff2458e5913039
[platform/upstream/syncevolution.git] / src / syncevo / TrackingSyncSource.cpp
1 /*
2  * Copyright (C) 2008-2009 Patrick Ohly <patrick.ohly@gmx.de>
3  * Copyright (C) 2009 Intel Corporation
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) version 3.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301  USA
19  */
20
21 #include <syncevo/TrackingSyncSource.h>
22 #include <syncevo/SafeConfigNode.h>
23 #include <syncevo/PrefixConfigNode.h>
24
25 #include <boost/bind.hpp>
26
27 #include <syncevo/declarations.h>
28 SE_BEGIN_CXX
29
30 TrackingSyncSource::TrackingSyncSource(const SyncSourceParams &params,
31                                        int granularitySeconds) :
32     TestingSyncSource(params),
33     m_trackingNode(new PrefixConfigNode("item-",
34                                         boost::shared_ptr<ConfigNode>(new SafeConfigNode(params.m_nodes.getTrackingNode()))))
35 {
36     m_operations.m_checkStatus = boost::bind(&TrackingSyncSource::checkStatus, this, _1);
37     m_operations.m_isEmpty = boost::bind(&TrackingSyncSource::isEmpty, this);
38     SyncSourceRevisions::init(this, this, granularitySeconds, m_operations);
39 }
40
41 void TrackingSyncSource::checkStatus(SyncSourceReport &changes)
42 {
43     detectChanges(*m_trackingNode);
44     // copy our item counts into the report
45     changes.setItemStat(ITEM_LOCAL, ITEM_ADDED, ITEM_TOTAL, getNewItems().size());
46     changes.setItemStat(ITEM_LOCAL, ITEM_UPDATED, ITEM_TOTAL, getUpdatedItems().size());
47     changes.setItemStat(ITEM_LOCAL, ITEM_REMOVED, ITEM_TOTAL, getDeletedItems().size());
48     changes.setItemStat(ITEM_LOCAL, ITEM_ANY, ITEM_TOTAL, getAllItems().size());
49 }
50
51 void TrackingSyncSource::beginSync(const std::string &lastToken, const std::string &resumeToken)
52 {
53     detectChanges(*m_trackingNode);
54 }
55
56 std::string TrackingSyncSource::endSync(bool success)
57 {
58     // store changes persistently
59     flush();
60
61     if (success) {
62         m_trackingNode->flush();
63     } else {
64         // The Synthesis docs say that we should rollback in case of
65         // failure. Cannot do that for data, so lets at least keep
66         // the revision map unchanged.
67     }
68
69     // no token handling at the moment (not needed for clients)
70     return "";
71 }
72
73 TrackingSyncSource::InsertItemResult TrackingSyncSource::insertItem(const std::string &luid, const std::string &item)
74 {
75     InsertItemResult res = insertItem(luid, item, false);
76     updateRevision(*m_trackingNode, luid, res.m_luid, res.m_revision);
77     return res;
78 }
79
80 TrackingSyncSource::InsertItemResult TrackingSyncSource::insertItemRaw(const std::string &luid, const std::string &item)
81 {
82     InsertItemResult res = insertItem(luid, item, true);
83     updateRevision(*m_trackingNode, luid, res.m_luid, res.m_revision);
84     return res;
85 }
86
87 void TrackingSyncSource::readItem(const std::string &luid, std::string &item)
88 {
89     readItem(luid, item, false);
90 }
91
92 void TrackingSyncSource::readItemRaw(const std::string &luid, std::string &item)
93 {
94     readItem(luid, item, true);
95 }
96
97 void TrackingSyncSource::deleteItem(const std::string &luid)
98 {
99     removeItem(luid);
100     deleteRevision(*m_trackingNode, luid);
101 }
102
103 void TrackingSyncSource::enableServerMode()
104 {
105     SyncSourceAdmin::init(m_operations, this);
106 }
107
108 bool TrackingSyncSource::serverModeEnabled() const
109 {
110     return m_operations.m_loadAdminData;
111 }
112
113 const char *TrackingSyncSource::getPeerMimeType() const
114 {
115     return getMimeType();
116 }
117
118 SE_END_CXX