Bumped copyright dates for 2013
[barry.git] / desktop / src / osconv22.cc
blob67d95decc8a0085fde2065df9d9736a2fd058189
1 ///
2 /// \file osconv22.cc
3 /// Converter class for opensync 0.22 plugins
4 ///
6 /*
7 Copyright (C) 2009-2013, 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>
25 #include "i18n.h"
27 using namespace std;
29 // Supported plugin names
30 #define PLUGIN_BARRY "barry-sync"
31 #define PLUGIN_EVOLUTION "evo2-sync"
32 #define PLUGIN_EVOLUTION3 "evo3-sync" // not supported on 0.2x
33 #define PLUGIN_GOOGLE "google-calendar" // "google-data" ???
34 #define PLUGIN_KDEPIM "kdepim-sync"
35 #define PLUGIN_FILE "file-sync"
36 #define PLUGIN_SUNBIRD "sunbird-sync"
37 #define PLUGIN_LDAP "ldap-sync"
39 namespace OpenSync {
41 //////////////////////////////////////////////////////////////////////////////
42 // Converter22
44 Converter22::Converter22(OpenSync::API &api)
45 : m_api(api)
49 bool Converter22::IsPluginSupported(const std::string &plugin_name,
50 std::string *appname) const
52 if( plugin_name == PLUGIN_BARRY ) {
53 if( appname )
54 *appname = Config::Barry::AppName();
55 return true;
57 else if( plugin_name == PLUGIN_EVOLUTION ) {
58 // only Evolution, not Evolution3, which is not available on
59 // version 0.2x
60 if( appname )
61 *appname = Config::Evolution::AppName();
62 return true;
64 else if( plugin_name == PLUGIN_KDEPIM ) {
65 if( appname )
66 *appname = Config::KDEPim::AppName();
67 return true;
70 return false;
73 Converter::plugin_ptr Converter22::CreateAndLoadPlugin(const Member &member)
75 Converter::plugin_ptr ptr;
77 // compare plugin name in member with all known plugins that
78 // we support... and default to Unsupported if not
79 if( member.plugin_name == PLUGIN_BARRY ) {
80 ptr.reset( new Config::Barry(this, member) );
82 else if( member.plugin_name == PLUGIN_EVOLUTION ) {
83 ptr.reset( new Config::Evolution(this, member) );
85 else if( member.plugin_name == PLUGIN_KDEPIM ) {
86 ptr.reset( new Config::KDEPim(this, member) );
88 // default: Unsupported
89 else {
90 ptr.reset( new Config::Unsupported(this, member) );
93 return ptr;
96 std::string Converter22::GetPluginName(const Config::Barry &) const
98 return PLUGIN_BARRY;
101 std::string Converter22::GetPluginName(const Config::Evolution &) const
103 return PLUGIN_EVOLUTION;
106 std::string Converter22::GetPluginName(const Config::Evolution3 &) const
108 return "unsupported-evo3-sync";
111 std::string Converter22::GetPluginName(const Config::Google &) const
113 return PLUGIN_GOOGLE;
116 std::string Converter22::GetPluginName(const Config::KDEPim &) const
118 return PLUGIN_KDEPIM;
121 std::string Converter22::GetPluginName(const Config::Unsupported &) const
123 return "unsupported-sync";
126 bool Converter22::IsConfigured(const Config::Barry &config) const
128 return config.GetPin().Valid();
131 bool Converter22::IsConfigured(const Config::Evolution &config) const
133 // the 22 barry plugin only supports address and calendar,
134 // and as long as either one is configured, we're good
135 return config.GetAddressPath().size() ||
136 config.GetCalendarPath().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(_C("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(_C("Cannot save a plugin with a member_id of -1"));
323 ostringstream oss;
324 oss << "<config>\n";
325 if( config.GetAddressPath().size() )
326 oss << " <address_path>" << config.GetAddressPath() << "</address_path>\n";
327 if( config.GetCalendarPath().size() )
328 oss << " <calendar_path>" << config.GetCalendarPath() << "</calendar_path>\n";
329 if( config.GetTasksPath().size() )
330 oss << " <tasks_path>" << config.GetTasksPath() << "</tasks_path>\n";
331 oss << "</config>" << endl;
333 m_api.SetConfiguration(group_name, config.GetMemberId(), oss.str());
336 void Converter22::Save(const Config::Evolution3 &config,
337 const std::string &group_name)
339 throw std::logic_error("Saving config for Evolution3 plugin is not supported for 0.22. Use the Unsupported class.");
342 void Converter22::Save(const Config::Google &config,
343 const std::string &group_name)
345 throw std::logic_error("Saving config for Google calendar plugin is not supported for 0.22. Use the Unsupported class.");
348 void Converter22::Save(const Config::KDEPim &config,
349 const std::string &group_name)
351 // KDEPim 0.22 needs no config, so nothing to do here
354 void Converter22::Save(const Config::Unsupported &config,
355 const std::string &group_name)
357 if( config.GetMemberId() == -1 )
358 throw Config::SaveError(_C("Cannot save a plugin with a member_id of -1"));
360 m_api.SetConfiguration(group_name, config.GetMemberId(),
361 config.GetRawConfig());
364 } // namespace OpenSync