desktop: fixed Evolution config writing, setting enabled only if URL is present
[barry/progweb.git] / desktop / src / osconv22.cc
blobd9005b3512310fdd51407df5b87d6ea0272c346e
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 // and as long as either one is configured, we're good
134 return config.GetAddressPath().size() ||
135 config.GetCalendarPath().size();
138 bool Converter22::IsConfigured(const Config::Evolution3 &config) const
140 return false;
143 bool Converter22::IsConfigured(const Config::Google &config) const
145 return false;
148 bool Converter22::IsConfigured(const Config::KDEPim &config) const
150 // KDEPim on 0.22 needs no configuration, so it is always configured
151 return true;
154 bool Converter22::IsConfigured(const Config::Unsupported &) const
156 return false;
159 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Barry &) const
161 return PST_CONTACTS | PST_EVENTS;
164 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Evolution &) const
166 return PST_CONTACTS | PST_EVENTS | PST_TODOS;
169 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Evolution3 &) const
171 return PST_NONE;
174 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Google &) const
176 return PST_CONTACTS | PST_EVENTS;
179 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::KDEPim &) const
181 return PST_CONTACTS | PST_EVENTS | PST_NOTES | PST_TODOS;
184 Config::pst_type Converter22::GetSupportedSyncTypes(const Config::Unsupported &) const
186 return PST_NONE;
189 void Converter22::Load(Config::Barry &config, const Member &member)
191 // start with a default setting
192 config.DebugMode(false);
193 config.SetPassword("");
194 config.SetPin(Barry::Pin());
196 // grab the config
197 string cfg = m_api.GetConfiguration(member.group_name, member.id);
199 // The config data should contain:
200 // - Keyword: DebugMode
201 // - if the single word "DebugMode" is found, enable Debug
203 // - Keyword: Device <pin> ...
204 // - PIN of device to sync with
205 // - or a flag that says "autoconfig with first device found"
206 // which will autodetect, and update the config
207 // automatically with the found PIN... all future syncs
208 // will then have a PIN
209 // - checkboxes for (both can be on):
210 // - sync calendar items
211 // - sync contacts
213 istringstream iss(cfg);
214 string line;
215 while( getline(iss, line) ) {
217 if( line[0] == '#' )
218 continue;
220 istringstream ils(line);
221 string key;
222 ils >> key;
224 if( key == "DebugMode" ) {
225 config.DebugMode(true);
227 else if( key == "Device" ) {
228 Barry::Pin pin;
229 int cal = 0, con = 0;
230 ils >> pin >> cal >> con;
232 config.SetPin(pin);
234 // ignore cal and con, since syncs are
235 // not reliable if they are set... assume 1 for both
237 else if ( key == "Password" ) {
238 string password;
239 ils >> password;
240 config.SetPassword(password);
245 // yes, I know that I should use an XML library here, but... but... but :-)
246 // this is such a simple format, I should be able to do this manually....
247 // (famous last words, eh?)
248 std::string Converter22::GrabField(const std::string &cfg,
249 const std::string &name)
251 string start = "<" + name + ">";
252 string end = "</" + name + ">";
254 size_t spos = cfg.find(start);
255 size_t epos = cfg.find(end);
257 if( spos == string::npos || epos == string::npos )
258 return "";
260 spos += start.size();
261 int count = epos - spos;
263 if( spos > epos )
264 return "";
266 return cfg.substr(spos, count);
269 void Converter22::Load(Config::Evolution &config, const Member &member)
271 string cfg = m_api.GetConfiguration(member.group_name, member.id);
273 config.SetAddressPath(GrabField(cfg, "address_path"));
274 config.SetCalendarPath(GrabField(cfg, "calendar_path"));
275 config.SetTasksPath(GrabField(cfg, "tasks_path"));
278 void Converter22::Load(Config::Evolution3 &config, const Member &member)
280 throw std::logic_error("Loading config for Evolution3 plugin is not supported for 0.22. Use the Unsupported class.");
283 void Converter22::Load(Config::Google &config, const Member &member)
285 throw std::logic_error("Loading config for Google calendar plugin is not supported for 0.22. Use the Unsupported class.");
288 void Converter22::Load(Config::KDEPim &config, const Member &member)
290 // KDEPim on 0.22 needs no config, so nothing to do here
293 void Converter22::Load(Config::Unsupported &config, const Member &member)
295 string cfg = m_api.GetConfiguration(member.group_name, member.id);
296 config.SetRawConfig(cfg);
299 void Converter22::Save(const Config::Barry &config,
300 const std::string &group_name)
302 if( config.GetMemberId() == -1 )
303 throw Config::SaveError("Cannot save a plugin with a member_id of -1");
305 ostringstream oss;
307 oss << "Device " << config.GetPin().Str() << " 1 1" << endl;
308 if( config.IsDebugMode() )
309 oss << "DebugMode" << endl;
310 if( config.GetPassword().size() )
311 oss << "Password " << config.GetPassword() << endl;
313 m_api.SetConfiguration(group_name, config.GetMemberId(), oss.str());
316 void Converter22::Save(const Config::Evolution &config,
317 const std::string &group_name)
319 if( config.GetMemberId() == -1 )
320 throw Config::SaveError("Cannot save a plugin with a member_id of -1");
322 ostringstream oss;
323 oss << "<config>\n";
324 if( config.GetAddressPath().size() )
325 oss << " <address_path>" << config.GetAddressPath() << "</address_path>\n";
326 if( config.GetCalendarPath().size() )
327 oss << " <calendar_path>" << config.GetCalendarPath() << "</calendar_path>\n";
328 if( config.GetTasksPath().size() )
329 oss << " <tasks_path>" << config.GetTasksPath() << "</tasks_path>\n";
330 oss << "</config>" << endl;
332 m_api.SetConfiguration(group_name, config.GetMemberId(), oss.str());
335 void Converter22::Save(const Config::Evolution3 &config,
336 const std::string &group_name)
338 throw std::logic_error("Saving config for Evolution3 plugin is not supported for 0.22. Use the Unsupported class.");
341 void Converter22::Save(const Config::Google &config,
342 const std::string &group_name)
344 throw std::logic_error("Saving config for Google calendar plugin is not supported for 0.22. Use the Unsupported class.");
347 void Converter22::Save(const Config::KDEPim &config,
348 const std::string &group_name)
350 // KDEPim 0.22 needs no config, so nothing to do here
353 void Converter22::Save(const Config::Unsupported &config,
354 const std::string &group_name)
356 if( config.GetMemberId() == -1 )
357 throw Config::SaveError("Cannot save a plugin with a member_id of -1");
359 m_api.SetConfiguration(group_name, config.GetMemberId(),
360 config.GetRawConfig());
363 } // namespace OpenSync