Imported Upstream version 1.0beta3
[platform/upstream/syncevolution.git] / src / syncevo / SynthesisEngine.cpp
1 /*
2  * Copyright (C) 2009 Intel Corporation
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) version 3.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301  USA
18  */
19
20 #include <syncevo/SynthesisEngine.h>
21 #include <syncevo/util.h>
22
23 #include <synthesis/SDK_util.h>
24
25 #include <syncevo/declarations.h>
26 SE_BEGIN_CXX
27
28 void SharedEngine::Connect(const string &aEngineName,
29                            sysync::CVersion aPrgVersion,
30                            sysync::uInt16 aDebugFlags)
31 {
32     sysync::TSyError err = m_engine->Connect(aEngineName, aPrgVersion, aDebugFlags);
33     if (err) {
34         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, std::string("cannot connect to engine '") + aEngineName + "'", static_cast<sysync::TSyErrorEnum>(err));
35     }
36 }
37
38 void SharedEngine::Disconnect()
39 {
40     sysync::TSyError err = m_engine->Disconnect();
41     if (err) {
42         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, "cannot disconnect engine", static_cast<sysync::TSyErrorEnum>(err));
43     }
44 }
45
46 void SharedEngine::InitEngineXML(const string &aConfigXML)
47 {
48     sysync::TSyError err = m_engine->InitEngineXML(aConfigXML.c_str());
49     if (err) {
50         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, "Synthesis XML config parser error", static_cast<sysync::TSyErrorEnum>(err));
51     }
52 }
53
54 class FreeEngineItem {
55     SharedEngine m_engine;
56 public:
57     FreeEngineItem(const SharedEngine &engine) :
58         m_engine(engine)
59     {}
60     void operator () (sysync::KeyH key) {
61         m_engine.get()->CloseKey(key);
62     }
63     void operator () (sysync::SessionH session) {
64         m_engine.get()->CloseSession(session);
65     }
66 };
67
68
69 SharedSession SharedEngine::OpenSession(const string &aSessionID)
70 {
71     sysync::SessionH sessionH = NULL;
72     sysync::TSyError err = m_engine->OpenSession(sessionH, 0,
73                                                  aSessionID.empty() ? NULL : aSessionID.c_str());
74     if (err) {
75         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, "opening session failed", static_cast<sysync::TSyErrorEnum>(err));
76     }
77     return SharedSession(sessionH, FreeEngineItem(*this));
78 }
79
80 SharedKey SharedEngine::OpenSessionKey(SharedSession &aSessionH)
81 {
82     sysync::KeyH key;
83     sysync::TSyError err = m_engine->OpenSessionKey(aSessionH.get(), key, 0);
84     if (err) {
85         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, "opening session key failed", static_cast<sysync::TSyErrorEnum>(err));
86     }
87     return SharedKey(key, FreeEngineItem(*this));
88 }
89
90 void SharedEngine::SessionStep(const SharedSession &aSessionH,
91                                sysync::uInt16 &aStepCmd,
92                                sysync::TEngineProgressInfo *aInfoP)
93 {
94     sysync::TSyError err = m_engine->SessionStep(aSessionH.get(),
95                                                  aStepCmd,
96                                                  aInfoP);
97     if (err) {
98         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, "proceeding with session failed", static_cast<sysync::TSyErrorEnum>(err));
99     }
100 }
101
102 class FreeSyncMLBuffer {
103     SharedEngine m_engine;
104     SharedSession m_session;
105     bool m_forSend;
106     size_t m_size;
107 public:
108     FreeSyncMLBuffer(const SharedEngine &engine,
109                      const SharedSession &session,
110                      bool forSend,
111                      size_t size) :
112         m_engine(engine),
113         m_session(session),
114         m_forSend(forSend),
115         m_size(size)
116     {}
117     void operator () (char *buffer) {
118         m_engine.get()->RetSyncMLBuffer(m_session.get(), m_forSend, m_size);
119     }
120 };
121
122 SharedBuffer SharedEngine::GetSyncMLBuffer(const SharedSession &aSessionH, bool aForSend)
123 {
124     sysync::appPointer buffer;
125     sysync::memSize bufSize;
126     sysync::TSyError err = m_engine->GetSyncMLBuffer(aSessionH.get(),
127                                                      aForSend,
128                                                      buffer, bufSize);
129     if (err) {
130         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult,"acquiring SyncML buffer failed", static_cast<sysync::TSyErrorEnum>(err));
131     }
132
133     return SharedBuffer((char *)buffer, (size_t)bufSize,
134                         FreeSyncMLBuffer(*this, aSessionH, aForSend, bufSize));
135 }
136
137 void SharedEngine::WriteSyncMLBuffer(const SharedSession &aSessionH, const char *data, size_t len)
138 {
139     sysync::TSyError err = m_engine->WriteSyncMLBuffer(aSessionH.get(), const_cast<char *>(data), len);
140     if (err) {
141         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, "writing SyncML buffer failed", static_cast<sysync::TSyErrorEnum>(err));
142     }
143 }
144
145 SharedKey SharedEngine::OpenKeyByPath(const SharedKey &aParentKeyH,
146                                       const string &aPath,
147                                       bool noThrow)
148 {
149     sysync::KeyH key = NULL;
150     sysync::TSyError err = m_engine->OpenKeyByPath(key, aParentKeyH.get(), aPath.c_str(), 0);
151     if (err && noThrow) {
152         return SharedKey();
153     }
154     if (err) {
155         string what = "opening key ";
156         what += aPath;
157         if (err == sysync::DB_NoContent) {
158             SE_THROW_EXCEPTION(NoSuchKey, what);
159         } else {
160             SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, what, static_cast<sysync::TSyErrorEnum>(err));
161         }
162     }
163     return SharedKey(key, FreeEngineItem(*this));
164 }
165
166 SharedKey SharedEngine::OpenSubkey(const SharedKey &aParentKeyH,
167                                    sysync::sInt32 aID,
168                                    bool noThrow)
169 {
170     sysync::KeyH key = NULL;
171     sysync::TSyError err = m_engine->OpenSubkey(key, aParentKeyH.get(), aID, 0);
172     if (err && noThrow) {
173         return SharedKey();
174     }
175     if (err) {
176         string what = "opening sub key";
177         if (err == sysync::DB_NoContent) {
178             SE_THROW_EXCEPTION(NoSuchKey, what);
179         } else {
180             SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, what, static_cast<sysync::TSyErrorEnum>(err));
181         }
182     }
183     return SharedKey(key, FreeEngineItem(*this));
184 }
185
186 string SharedEngine::GetStrValue(const SharedKey &aKeyH, const string &aValName)
187 {
188     std::string s;
189     sysync::TSyError err = m_engine->GetStrValue(aKeyH.get(), aValName.c_str(), s);
190     if (err) {
191         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, string("error reading value ") + aValName, static_cast<sysync::TSyErrorEnum>(err));
192     }
193     return s;
194 }
195
196 void SharedEngine::SetStrValue(const SharedKey &aKeyH, const string &aValName, const string &aValue)
197 {
198     sysync::TSyError err = m_engine->SetStrValue(aKeyH.get(), aValName.c_str(), aValue);
199     if (err) {
200         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, string("error writing value ") + aValName, static_cast<sysync::TSyErrorEnum>(err));
201     }
202 }
203
204 sysync::sInt32 SharedEngine::GetInt32Value(const SharedKey &aKeyH, const string &aValName)
205 {
206     sysync::sInt32 v;
207     sysync::TSyError err = m_engine->GetInt32Value(aKeyH.get(), aValName.c_str(), v);
208     if (err) {
209         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, string("error reading value ") + aValName, static_cast<sysync::TSyErrorEnum>(err));
210     }
211     return v;
212 }
213
214 void SharedEngine::SetInt32Value(const SharedKey &aKeyH, const string &aValName, sysync::sInt32 aValue)
215 {
216     sysync::TSyError err = m_engine->SetInt32Value(aKeyH.get(), aValName.c_str(), aValue);
217     if (err) {
218         SE_THROW_EXCEPTION_STATUS(BadSynthesisResult, string("error writing value ") + aValName, static_cast<sysync::TSyErrorEnum>(err));
219     }
220 }
221
222 void SharedEngine::doDebug(Logger::Level level,
223                            const char *prefix,
224                            const char *file,
225                            int line,
226                            const char *function,
227                            const char *format,
228                            va_list args)
229 {
230     std::string str = StringPrintfV(format, args);
231     SySyncDebugPuts(m_engine->fCI, file, line, function,
232                     level <= Logger::ERROR ? DBG_ERROR :
233                     level <= Logger::INFO ? DBG_HOT :
234                     0, prefix,
235                     str.c_str());
236 }
237
238
239 sysync::TSyError SDKInterface::setValue(sysync::KeyH aItemKey,
240                                         const std::string &field,
241                                         const char *data,
242                                         size_t datalen)
243 {
244     return this->ui.SetValue(this,
245                              aItemKey,
246                              field.c_str(),
247                              sysync::VALTYPE_TEXT,
248                              data,
249                              datalen);
250 }
251
252 sysync::TSyError SDKInterface::getValue(sysync::KeyH aItemKey,
253                                         const std::string &field,
254                                         SharedBuffer &data)
255 {
256     sysync::memSize len;
257     TSyError res =
258         this->ui.GetValue(this,
259                           aItemKey,
260                           field.c_str(),
261                           sysync::VALTYPE_TEXT,
262                           NULL, 0,
263                           &len);
264     if (!res) {
265         data = SharedBuffer(new char[len + 1], len);
266         data[len] = 0;
267         res = this->ui.GetValue(this,
268                                 aItemKey,
269                                 field.c_str(),
270                                 sysync::VALTYPE_TEXT,
271                                 data.get(), len + 1,
272                                 &len);
273     }
274
275     return res;
276 }
277
278 SE_END_CXX