Reformat debian/copyright to suit informal style habits.
[barry.git] / desktop / src / osconv22.cc
blob37ee62bc2b01f579ddfae95726e1040a46de0e25
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_EVOLUTION3 "evo3-sync" // not supported on 0.2x
32 #define PLUGIN_GOOGLE "google-calendar" // "google-data" ???
33 #define PLUGIN_KDEPIM "kdepim-sync"
34 #define PLUGIN_FILE "file-sync"
35 #define PLUGIN_SUNBIRD "sunbird-sync"
36 #define PLUGIN_LDAP "ldap-sync"
38 namespace OpenSync {
40 //////////////////////////////////////////////////////////////////////////////
41 // Converter22
43 Converter22::Converter22(OpenSync::API &api)
44 : m_api(api)
48 bool Converter22::IsPluginSupported(const std::string &plugin_name,
49 std::string *appname) const
51 if( plugin_name == PLUGIN_BARRY ) {
52 if( appname )
53 *appname = Config::Barry::AppName();
54 return true;
56 else if( plugin_name == PLUGIN_EVOLUTION ) {
57 // only Evolution, not Evolution3, which is not available on
58 // version 0.2x
59 if( appname )
60 *appname = Config::Evolution::AppName();
61 return true;
63 else if( plugin_name == PLUGIN_KDEPIM ) {
64 if( appname )
65 *appname = Config::KDEPim::AppName();
66 return true;
69 return false;
72 Converter::plugin_ptr Converter22::CreateAndLoadPlugin(const Member &member)
74 Converter::plugin_ptr ptr;
76 // compare plugin name in member with all known plugins that
77 // we support... and default to Unsupported if not
78 if( member.plugin_name == PLUGIN_BARRY ) {
79 ptr.reset( new Config::Barry(this, member) );
81 else if( member.plugin_name == PLUGIN_EVOLUTION ) {
82 ptr.reset( new Config::Evolution(this, member) );
84 else if( member.plugin_name == PLUGIN_KDEPIM ) {
85 ptr.reset( new Config::KDEPim(this, member) );
87 // default: Unsupported
88 else {
89 ptr.reset( new Config::Unsupported(this, member) );
92 return ptr;
95 std::string Converter22::GetPluginName(const Config::Barry &) const
97 return PLUGIN_BARRY;
100 std::string Converter22::GetPluginName(const Config::Evolution &) const
102 return PLUGIN_EVOLUTION;
105 std::string Converter22::GetPluginName(const Config::Evolution3 &) const
107 return "unsupported-evo3-sync";
110 std::string Converter22::GetPluginName(const Config::Google &) const
112 return PLUGIN_GOOGLE;
115 std::string Converter22::GetPluginName(const Config::KDEPim &) const
117 return PLUGIN_KDEPIM;
120 std::string Converter22::GetPluginName(const Config::Unsupported &) const
122 return "unsupported-sync";
125 bool Converter22::IsConfigured(const Config::Barry &config) const
127 return config.GetPin().Valid();
130 bool Converter22::IsConfigured(const Config::Evolution &config) const
132 // the 22 barry plugin only supports address and calendar,
133 // but the evolution plugin seems to like these 3
134 return config.GetAddressPath().size() &&
135 config.GetCalendarPath().size() &&
136 config.GetTasksPath().size();
139 bool Converter22::IsConfigured(const Config::Evolution3 &config) const
141 return false;
144 bool Converter22::IsConfigured(const Config::Google &config) const
146 return false;
149 bool Converter22::IsConfigured(const Config::KDEPim &config) const
151 // KDEPim on 0.22 needs no configuration, so it is always configured
152 return true;
155 bool Converter22::IsConfigured(const Config::Unsupported &) const
157 return false;
160 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Barry &) const
162 return PST_CONTACTS | PST_EVENTS;
165 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Evolution &) const
167 return PST_CONTACTS | PST_EVENTS | PST_TODOS;
170 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Evolution3 &) const
172 return PST_NONE;
175 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Google &) const
177 return PST_CONTACTS | PST_EVENTS;
180 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::KDEPim &) const
182 return PST_CONTACTS | PST_EVENTS | PST_NOTES | PST_TODOS;
185 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Unsupported &) const
187 return PST_NONE;
190 void Converter22::Load(Config::Barry &config, const Member &member)
192 // start with a default setting
193 config.DebugMode(false);
194 config.SetPassword("");
195 config.SetPin(Barry::Pin());
197 // grab the config
198 string cfg = m_api.GetConfiguration(member.group_name, member.id);
200 // The config data should contain:
201 // - Keyword: DebugMode
202 // - if the single word "DebugMode" is found, enable Debug
204 // - Keyword: Device <pin> ...
205 // - PIN of device to sync with
206 // - or a flag that says "autoconfig with first device found"
207 // which will autodetect, and update the config
208 // automatically with the found PIN... all future syncs
209 // will then have a PIN
210 // - checkboxes for (both can be on):
211 // - sync calendar items
212 // - sync contacts
214 istringstream iss(cfg);
215 string line;
216 while( getline(iss, line) ) {
218 if( line[0] == '#' )
219 continue;
221 istringstream ils(line);
222 string key;
223 ils >> key;
225 if( key == "DebugMode" ) {
226 config.DebugMode(true);
228 else if( key == "Device" ) {
229 Barry::Pin pin;
230 int cal = 0, con = 0;
231 ils >> pin >> cal >> con;
233 config.SetPin(pin);
235 // ignore cal and con, since syncs are
236 // not reliable if they are set... assume 1 for both
238 else if ( key == "Password" ) {
239 string password;
240 ils >> password;
241 config.SetPassword(password);
246 // yes, I know that I should use an XML library here, but... but... but :-)
247 // this is such a simple format, I should be able to do this manually....
248 // (famous last words, eh?)
249 std::string Converter22::GrabField(const std::string &cfg,
250 const std::string &name)
252 string start = "<" + name + ">";
253 string end = "</" + name + ">";
255 size_t spos = cfg.find(start);
256 size_t epos = cfg.find(end);
258 if( spos == string::npos || epos == string::npos )
259 return "";
261 spos += start.size();
262 int count = epos - spos;
264 if( spos > epos )
265 return "";
267 return cfg.substr(spos, count);
270 void Converter22::Load(Config::Evolution &config, const Member &member)
272 string cfg = m_api.GetConfiguration(member.group_name, member.id);
274 config.SetAddressPath(GrabField(cfg, "address_path"));
275 config.SetCalendarPath(GrabField(cfg, "calendar_path"));
276 config.SetTasksPath(GrabField(cfg, "tasks_path"));
279 void Converter22::Load(Config::Evolution3 &config, const Member &member)
281 throw std::logic_error("Loading config for Evolution3 plugin is not supported for 0.22. Use the Unsupported class.");
284 void Converter22::Load(Config::Google &config, const Member &member)
286 throw std::logic_error("Loading config for Google calendar plugin is not supported for 0.22. Use the Unsupported class.");
289 void Converter22::Load(Config::KDEPim &config, const Member &member)
291 // KDEPim on 0.22 needs no config, so nothing to do here
294 void Converter22::Load(Config::Unsupported &config, const Member &member)
296 string cfg = m_api.GetConfiguration(member.group_name, member.id);
297 config.SetRawConfig(cfg);
300 void Converter22::Save(const Config::Barry &config,
301 const std::string &group_name)
303 if( config.GetMemberId() == -1 )
304 throw Config::SaveError("Cannot save a plugin with a member_id of -1");
306 ostringstream oss;
308 oss << "Device " << config.GetPin().Str() << " 1 1" << endl;
309 if( config.IsDebugMode() )
310 oss << "DebugMode" << endl;
311 if( config.GetPassword().size() )
312 oss << "Password " << config.GetPassword() << endl;
314 m_api.SetConfiguration(group_name, config.GetMemberId(), oss.str());
317 void Converter22::Save(const Config::Evolution &config,
318 const std::string &group_name)
320 if( config.GetMemberId() == -1 )
321 throw Config::SaveError("Cannot save a plugin with a member_id of -1");
323 ostringstream oss;
324 oss << "<config>\n"
325 << " <address_path>" << config.GetAddressPath() << "</address_path>\n"
326 << " <calendar_path>" << config.GetCalendarPath() << "</calendar_path>\n"
327 << " <tasks_path>" << config.GetTasksPath() << "</tasks_path>\n"
328 << "</config>"
329 << endl;
331 m_api.SetConfiguration(group_name, config.GetMemberId(), oss.str());
334 void Converter22::Save(const Config::Evolution3 &config,
335 const std::string &group_name)
337 throw std::logic_error("Saving config for Evolution3 plugin is not supported for 0.22. Use the Unsupported class.");
340 void Converter22::Save(const Config::Google &config,
341 const std::string &group_name)
343 throw std::logic_error("Saving config for Google calendar plugin is not supported for 0.22. Use the Unsupported class.");
346 void Converter22::Save(const Config::KDEPim &config,
347 const std::string &group_name)
349 // KDEPim 0.22 needs no config, so nothing to do here
352 void Converter22::Save(const Config::Unsupported &config,
353 const std::string &group_name)
355 if( config.GetMemberId() == -1 )
356 throw Config::SaveError("Cannot save a plugin with a member_id of -1");
358 m_api.SetConfiguration(group_name, config.GetMemberId(),
359 config.GetRawConfig());
362 } // namespace OpenSync