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
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.
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.
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
26 #include "XMLRPCSyncSource.h"
28 #include <boost/algorithm/string/split.hpp>
30 #include <syncevo/declarations.h>
33 XMLRPCSyncSource::XMLRPCSyncSource(const SyncSourceParams ¶ms,
34 const string &dataformat) :
35 TrackingSyncSource(params)
37 if (dataformat.empty()) {
38 throwError("a data format must be specified");
40 size_t sep = dataformat.find(':');
41 if (sep == dataformat.npos) {
42 throwError(string("data format not specified as <mime type>:<mime version>: " + dataformat));
44 m_mimeType.assign(dataformat, 0, sep);
45 m_mimeVersion = dataformat.substr(sep + 1);
46 m_supportedTypes = dataformat;
48 string const dbid = getDatabaseID();
49 boost::split(m_splitDatabase, dbid, boost::is_any_of("|"));
50 m_serverUrl = m_splitDatabase[0];
53 const char *XMLRPCSyncSource::getMimeType() const
55 return m_mimeType.c_str();
58 const char *XMLRPCSyncSource::getMimeVersion() const
60 return m_mimeVersion.c_str();
63 void XMLRPCSyncSource::open()
67 void XMLRPCSyncSource::close()
71 XMLRPCSyncSource::Databases XMLRPCSyncSource::getDatabases()
75 result.push_back(Database("select database via URL",
80 void XMLRPCSyncSource::listAllItems(RevisionMap_t &revisions)
83 xmlrpc_c::value result;
85 client.call(m_serverUrl, "listAllItems", prepareParamList(), &result);
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;
93 for(it = resultMap.begin(); it != resultMap.end(); it++)
94 revisions[(*it).first] = xmlrpc_c::value_string((*it).second);
99 void XMLRPCSyncSource::readItem(const string &uid, std::string &item, bool raw)
102 xmlrpc_c::paramList p = prepareParamList();
103 p.add(xmlrpc_c::value_string(uid));
104 xmlrpc_c::value result;
106 client.call(m_serverUrl, "readItem", p, &result);
108 item = xmlrpc_c::value_string(result);
112 TrackingSyncSource::InsertItemResult XMLRPCSyncSource::insertItem(const string &uid, const std::string &item, bool raw)
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;
120 client.call(m_serverUrl, "insertItem", p, &result);
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));
126 if(resultMap.size() != 1) {
127 throw("Return value of insertItem has wrong length.");
130 map<string, xmlrpc_c::value>::const_iterator it;
131 it = resultMap.begin();
133 return InsertItemResult((*it).first,
134 xmlrpc_c::value_string((*it).second),
139 void XMLRPCSyncSource::removeItem(const string &uid)
141 xmlrpc_c::paramList p = prepareParamList();
142 p.add(xmlrpc_c::value_string(uid));
143 xmlrpc_c::value result;
145 client.call(m_serverUrl, "removeItem", p, &result);
148 xmlrpc_c::paramList XMLRPCSyncSource::prepareParamList()
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]));
160 #endif /* ENABLE_XMLRPC */
162 #ifdef ENABLE_MODULES
163 # include "XMLRPCSyncSourceRegister.cpp"