3 /// Converter class for opensync 0.22 plugins
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.
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"
39 //////////////////////////////////////////////////////////////////////////////
42 Converter22::Converter22(OpenSync::API
&api
)
47 bool Converter22::IsPluginSupported(const std::string
&plugin_name
,
48 std::string
*appname
) const
50 if( plugin_name
== PLUGIN_BARRY
) {
52 *appname
= Config::Barry::AppName();
55 else if( plugin_name
== PLUGIN_EVOLUTION
) {
57 *appname
= Config::Evolution::AppName();
60 else if( plugin_name
== PLUGIN_KDEPIM
) {
62 *appname
= Config::KDEPim::AppName();
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
86 ptr
.reset( new Config::Unsupported(this, member
) );
92 std::string
Converter22::GetPluginName(const Config::Barry
&) const
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
136 bool Converter22::IsConfigured(const Config::KDEPim
&config
) const
138 // KDEPim on 0.22 needs no configuration, so it is always configured
142 bool Converter22::IsConfigured(const Config::Unsupported
&) const
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
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());
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
196 istringstream
iss(cfg
);
198 while( getline(iss
, line
) ) {
203 istringstream
ils(line
);
207 if( key
== "DebugMode" ) {
208 config
.DebugMode(true);
210 else if( key
== "Device" ) {
212 int cal
= 0, con
= 0;
213 ils
>> pin
>> cal
>> con
;
217 // ignore cal and con, since syncs are
218 // not reliable if they are set... assume 1 for both
220 else if ( key
== "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
)
243 spos
+= start
.size();
244 int count
= epos
- spos
;
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");
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");
302 << " <address_path>" << config
.GetAddressPath() << "</address_path>\n"
303 << " <calendar_path>" << config
.GetCalendarPath() << "</calendar_path>\n"
304 << " <tasks_path>" << config
.GetTasksPath() << "</tasks_path>\n"
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