Imported Upstream version 1.0beta1
[platform/upstream/syncevolution.git] / src / backends / xmlrpc / XMLRPCSyncSource.cpp
1 /*
2  * Copyright (C) 2009 m-otion communications GmbH <knipp@m-otion.com>, waived
3  * Copyright (C) 2007-2009 Patrick Ohly <patrick.ohly@gmx.de>
4  * Copyright (C) 2009 Intel Corporation
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301  USA
20  */
21
22 #include "config.h"
23
24 #ifdef ENABLE_XMLRPC
25
26 #include "XMLRPCSyncSource.h"
27
28 #include <boost/algorithm/string/split.hpp>
29
30 #include <syncevo/declarations.h>
31 SE_BEGIN_CXX
32
33 XMLRPCSyncSource::XMLRPCSyncSource(const SyncSourceParams &params,
34                                    const string &dataformat) :
35     TrackingSyncSource(params)
36 {
37     if (dataformat.empty()) {
38         throwError("a data format must be specified");
39     }
40     size_t sep = dataformat.find(':');
41     if (sep == dataformat.npos) {
42         throwError(string("data format not specified as <mime type>:<mime version>: " + dataformat));
43     }
44     m_mimeType.assign(dataformat, 0, sep);
45     m_mimeVersion = dataformat.substr(sep + 1);
46     m_supportedTypes = dataformat;
47
48     string const dbid = getDatabaseID();
49     boost::split(m_splitDatabase, dbid, boost::is_any_of("|"));
50     m_serverUrl = m_splitDatabase[0];
51 }
52
53 const char *XMLRPCSyncSource::getMimeType() const
54 {
55     return m_mimeType.c_str();
56 }
57
58 const char *XMLRPCSyncSource::getMimeVersion() const
59 {
60     return m_mimeVersion.c_str();
61 }
62
63 void XMLRPCSyncSource::open()
64 {
65 }
66
67 void XMLRPCSyncSource::close()
68 {
69 }
70
71 XMLRPCSyncSource::Databases XMLRPCSyncSource::getDatabases()
72 {
73     Databases result;
74
75     result.push_back(Database("select database via URL",
76                               "<serverUrl>"));
77     return result;
78 }
79
80 void XMLRPCSyncSource::listAllItems(RevisionMap_t &revisions)
81 {
82
83     xmlrpc_c::value result;
84
85     client.call(m_serverUrl, "listAllItems", prepareParamList(), &result);
86
87     if(result.type() == xmlrpc_c::value::TYPE_STRUCT) {
88         xmlrpc_c::value_struct const tmp(result);
89         map<string, xmlrpc_c::value> const resultMap(
90             static_cast<map<string, xmlrpc_c::value> >(tmp));
91         map<string, xmlrpc_c::value>::const_iterator it;
92
93         for(it = resultMap.begin(); it != resultMap.end(); it++)
94             revisions[(*it).first] = xmlrpc_c::value_string((*it).second);
95
96     }
97 }
98
99 void XMLRPCSyncSource::readItem(const string &uid, std::string &item, bool raw)
100 {
101
102     xmlrpc_c::paramList p = prepareParamList();
103     p.add(xmlrpc_c::value_string(uid));
104     xmlrpc_c::value result;
105
106     client.call(m_serverUrl, "readItem", p, &result);
107
108     item = xmlrpc_c::value_string(result);
109
110 }
111
112 TrackingSyncSource::InsertItemResult XMLRPCSyncSource::insertItem(const string &uid, const std::string &item, bool raw)
113 {
114
115     xmlrpc_c::paramList p = prepareParamList();
116     p.add(xmlrpc_c::value_string(uid));
117     p.add(xmlrpc_c::value_string(item));
118     xmlrpc_c::value result;
119
120     client.call(m_serverUrl, "insertItem", p, &result);
121
122     xmlrpc_c::value_struct const tmp(result);
123     map<string, xmlrpc_c::value> const resultMap(
124         static_cast<map<string, xmlrpc_c::value> >(tmp));
125
126     if(resultMap.size() != 1) {
127         throw("Return value of insertItem has wrong length.");
128     }
129
130     map<string, xmlrpc_c::value>::const_iterator it;
131     it = resultMap.begin();
132
133     return InsertItemResult((*it).first,
134                             xmlrpc_c::value_string((*it).second),
135                             false);
136 }
137
138
139 void XMLRPCSyncSource::removeItem(const string &uid)
140 {
141     xmlrpc_c::paramList p = prepareParamList();
142     p.add(xmlrpc_c::value_string(uid));
143     xmlrpc_c::value result;
144
145     client.call(m_serverUrl, "removeItem", p, &result);
146 }
147
148 xmlrpc_c::paramList XMLRPCSyncSource::prepareParamList()
149 {
150     xmlrpc_c::paramList p;
151     for(size_t i = 1; i < m_splitDatabase.size(); i++) {
152         p.add(xmlrpc_c::value_string(m_splitDatabase[i]));
153     }
154
155     return p;
156 }
157
158 SE_END_CXX
159
160 #endif /* ENABLE_XMLRPC */
161
162 #ifdef ENABLE_MODULES
163 # include "XMLRPCSyncSourceRegister.cpp"
164 #endif