lib: added implicit ctor converter from DatabaseDatabase to DBListType
[barry/progweb.git] / desktop / src / osconv22.cc
blob49ea692ab38a9d589ba14ccf6c28389df873cd44
1 ///
2 /// \file osconv22.cc
3 /// Converter class for opensync 0.22 plugins
4 ///
6 /*
7 Copyright (C) 2009-2012, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "osconv22.h"
23 #include "osconfig.h"
24 #include <sstream>
26 using namespace std;
28 // Supported plugin names
29 #define PLUGIN_BARRY "barry-sync"
30 #define PLUGIN_EVOLUTION "evo2-sync"
31 #define PLUGIN_GOOGLE "google-calendar" // "google-data" ???
32 #define PLUGIN_KDEPIM "kdepim-sync"
33 #define PLUGIN_FILE "file-sync"
34 #define PLUGIN_SUNBIRD "sunbird-sync"
35 #define PLUGIN_LDAP "ldap-sync"
37 namespace OpenSync {
39 //////////////////////////////////////////////////////////////////////////////
40 // Converter22
42 Converter22::Converter22(OpenSync::API &api)
43 : m_api(api)
47 bool Converter22::IsPluginSupported(const std::string &plugin_name,
48 std::string *appname) const
50 if( plugin_name == PLUGIN_BARRY ) {
51 if( appname )
52 *appname = Config::Barry::AppName();
53 return true;
55 else if( plugin_name == PLUGIN_EVOLUTION ) {
56 if( appname )
57 *appname = Config::Evolution::AppName();
58 return true;
60 else if( plugin_name == PLUGIN_KDEPIM ) {
61 if( appname )
62 *appname = Config::KDEPim::AppName();
63 return true;
66 return false;
69 Converter::plugin_ptr Converter22::CreateAndLoadPlugin(const Member &member)
71 Converter::plugin_ptr ptr;
73 // compare plugin name in member with all known plugins that
74 // we support... and default to Unsupported if not
75 if( member.plugin_name == PLUGIN_BARRY ) {
76 ptr.reset( new Config::Barry(this, member) );
78 else if( member.plugin_name == PLUGIN_EVOLUTION ) {
79 ptr.reset( new Config::Evolution(this, member) );
81 else if( member.plugin_name == PLUGIN_KDEPIM ) {
82 ptr.reset( new Config::KDEPim(this, member) );
84 // default: Unsupported
85 else {
86 ptr.reset( new Config::Unsupported(this, member) );
89 return ptr;
92 std::string Converter22::GetPluginName(const Config::Barry &) const
94 return PLUGIN_BARRY;
97 std::string Converter22::GetPluginName(const Config::Evolution &) const
99 return PLUGIN_EVOLUTION;
102 std::string Converter22::GetPluginName(const Config::Google &) const
104 return PLUGIN_GOOGLE;
107 std::string Converter22::GetPluginName(const Config::KDEPim &) const
109 return PLUGIN_KDEPIM;
112 std::string Converter22::GetPluginName(const Config::Unsupported &) const
114 return "unsupported-sync";
117 bool Converter22::IsConfigured(const Config::Barry &config) const
119 return config.GetPin().Valid();
122 bool Converter22::IsConfigured(const Config::Evolution &config) const
124 // the 22 barry plugin only supports address and calendar,
125 // but the evolution plugin seems to like these 3
126 return config.GetAddressPath().size() &&
127 config.GetCalendarPath().size() &&
128 config.GetTasksPath().size();
131 bool Converter22::IsConfigured(const Config::Google &config) const
133 return false;
136 bool Converter22::IsConfigured(const Config::KDEPim &config) const
138 // KDEPim on 0.22 needs no configuration, so it is always configured
139 return true;
142 bool Converter22::IsConfigured(const Config::Unsupported &) const
144 return false;
147 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Barry &) const
149 return PST_CONTACTS | PST_EVENTS;
152 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Evolution &) const
154 return PST_CONTACTS | PST_EVENTS | PST_TODOS;
157 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Google &) const
159 return PST_CONTACTS | PST_EVENTS;
162 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::KDEPim &) const
164 return PST_CONTACTS | PST_EVENTS | PST_NOTES | PST_TODOS;
167 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Unsupported &) const
169 return PST_NONE;
172 void Converter22::Load(Config::Barry &config, const Member &member)
174 // start with a default setting
175 config.DebugMode(false);
176 config.SetPassword("");
177 config.SetPin(Barry::Pin());
179 // grab the config
180 string cfg = m_api.GetConfiguration(member.group_name, member.id);
182 // The config data should contain:
183 // - Keyword: DebugMode
184 // - if the single word "DebugMode" is found, enable Debug
186 // - Keyword: Device <pin> ...
187 // - PIN of device to sync with
188 // - or a flag that says "autoconfig with first device found"
189 // which will autodetect, and update the config
190 // automatically with the found PIN... all future syncs
191 // will then have a PIN
192 // - checkboxes for (both can be on):
193 // - sync calendar items
194 // - sync contacts
196 istringstream iss(cfg);
197 string line;
198 while( getline(iss, line) ) {
200 if( line[0] == '#' )
201 continue;
203 istringstream ils(line);
204 string key;
205 ils >> key;
207 if( key == "DebugMode" ) {
208 config.DebugMode(true);
210 else if( key == "Device" ) {
211 Barry::Pin pin;
212 int cal = 0, con = 0;
213 ils >> pin >> cal >> con;
215 config.SetPin(pin);
217 // ignore cal and con, since syncs are
218 // not reliable if they are set... assume 1 for both
220 else if ( key == "Password" ) {
221 string password;
222 ils >> password;
223 config.SetPassword(password);
228 // yes, I know that I should use an XML library here, but... but... but :-)
229 // this is such a simple format, I should be able to do this manually....
230 // (famous last words, eh?)
231 std::string Converter22::GrabField(const std::string &cfg,
232 const std::string &name)
234 string start = "<" + name + ">";
235 string end = "</" + name + ">";
237 size_t spos = cfg.find(start);
238 size_t epos = cfg.find(end);
240 if( spos == string::npos || epos == string::npos )
241 return "";
243 spos += start.size();
244 int count = epos - spos;
246 if( spos > epos )
247 return "";
249 return cfg.substr(spos, count);
252 void Converter22::Load(Config::Evolution &config, const Member &member)
254 string cfg = m_api.GetConfiguration(member.group_name, member.id);
256 config.SetAddressPath(GrabField(cfg, "address_path"));
257 config.SetCalendarPath(GrabField(cfg, "calendar_path"));
258 config.SetTasksPath(GrabField(cfg, "tasks_path"));
261 void Converter22::Load(Config::Google &config, const Member &member)
263 throw std::logic_error("Loading config for Google calendar plugin is not supported for 0.22. Use the Unsupported class.");
266 void Converter22::Load(Config::KDEPim &config, const Member &member)
268 // KDEPim on 0.22 needs no config, so nothing to do here
271 void Converter22::Load(Config::Unsupported &config, const Member &member)
273 string cfg = m_api.GetConfiguration(member.group_name, member.id);
274 config.SetRawConfig(cfg);
277 void Converter22::Save(const Config::Barry &config,
278 const std::string &group_name)
280 if( config.GetMemberId() == -1 )
281 throw Config::SaveError("Cannot save a plugin with a member_id of -1");
283 ostringstream oss;
285 oss << "Device " << config.GetPin().Str() << " 1 1" << endl;
286 if( config.IsDebugMode() )
287 oss << "DebugMode" << endl;
288 if( config.GetPassword().size() )
289 oss << "Password " << config.GetPassword() << endl;
291 m_api.SetConfiguration(group_name, config.GetMemberId(), oss.str());
294 void Converter22::Save(const Config::Evolution &config,
295 const std::string &group_name)
297 if( config.GetMemberId() == -1 )
298 throw Config::SaveError("Cannot save a plugin with a member_id of -1");
300 ostringstream oss;
301 oss << "<config>\n"
302 << " <address_path>" << config.GetAddressPath() << "</address_path>\n"
303 << " <calendar_path>" << config.GetCalendarPath() << "</calendar_path>\n"
304 << " <tasks_path>" << config.GetTasksPath() << "</tasks_path>\n"
305 << "</config>"
306 << endl;
308 m_api.SetConfiguration(group_name, config.GetMemberId(), oss.str());
311 void Converter22::Save(const Config::Google &config,
312 const std::string &group_name)
314 throw std::logic_error("Saving config for Google calendar plugin is not supported for 0.22. Use the Unsupported class.");
317 void Converter22::Save(const Config::KDEPim &config,
318 const std::string &group_name)
320 // KDEPim 0.22 needs no config, so nothing to do here
323 void Converter22::Save(const Config::Unsupported &config,
324 const std::string &group_name)
326 if( config.GetMemberId() == -1 )
327 throw Config::SaveError("Cannot save a plugin with a member_id of -1");
329 m_api.SetConfiguration(group_name, config.GetMemberId(),
330 config.GetRawConfig());
333 } // namespace OpenSync