3 /// Class which detects a set of available or known devices
4 /// in an opensync-able system.
8 Copyright (C) 2009-2012, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #ifndef __BARRY_OSCONFIG_H__
24 #define __BARRY_OSCONFIG_H__
26 #include <barry/barry.h>
34 namespace OpenSync
{ namespace Config
{
36 // Exception class for configuration load errors
37 class LoadError
: public std::exception
41 LoadError(const std::string
&msg
) : m_msg(msg
) {}
42 virtual ~LoadError() throw() {}
43 virtual const char* what() const throw() { return m_msg
.c_str(); }
46 class SaveError
: public std::exception
50 SaveError(const std::string
&msg
) : m_msg(msg
) {}
51 virtual ~SaveError() throw() {}
52 virtual const char* what() const throw() { return m_msg
.c_str(); }
55 /// Thrown when a member cannot be deleted from the group
56 class DeleteError
: public std::logic_error
59 DeleteError(const std::string
&msg
) : std::logic_error(msg
) {}
62 // Base class for all opensync plugin configurations
65 long m_member_id
; // -1 if not saved to the group yet
77 virtual long GetMemberId() const { return m_member_id
; }
78 virtual void SetMemberId(long id
) { m_member_id
= id
; }
84 virtual Plugin
* Clone() const = 0;
86 /// IsUnsupported() returns true if the config format for this
87 /// plugin has no OpenSync::Config::* class to parse / handle it.
88 virtual bool IsUnsupported() const { return true; }
89 virtual std::string
GetAppName() const = 0; // returns a GUI-friendly
90 // name for the application that this plugin
91 // is for... i.e. plugin name might
92 // be "evo2-sync" but the app name
93 // would be "Evolution"
94 /// Throws SaveError if member_id is -1
95 virtual void Save(OpenSync::API
&api
, const std::string
&group_name
) const = 0;
96 // sample implementation:
98 // api.GetConverter().Save(*this, group_name);
100 virtual std::string
GetPluginName(OpenSync::API
&api
) const = 0;
101 // sample implementation:
103 // return api.GetConverter().GetPluginName(*this);
105 virtual bool IsConfigured(OpenSync::API
&api
) const = 0;
106 // sample implementation:
108 // return api.GetConverter().IsConfigured(*this);
110 virtual pst_type
GetSupportedSyncTypes(OpenSync::API
&api
) const = 0;
111 // sample implementation:
113 // return api.GetConverter().GetSupportedSyncTypes(*this);
116 /// Support function for Group::Compare(). If plugin is the
117 /// same type as this, sametype is set to true. If sametype
118 /// is true, and the data is functionally equivalent,
119 /// equal is set to true. Returns (sametype && equal).
120 virtual bool Compare(const Plugin
&plugin
,
121 bool &sametype
, bool &equal
) const
123 sametype
= equal
= false;
124 return sametype
&& equal
;
128 class Unsupported
: public Plugin
131 // configuration settings
132 std::string m_raw_config
;
139 explicit Unsupported(Converter
*load_converter
, const Member
&member
)
141 load_converter
->Load(*this, member
);
144 const std::string
& GetRawConfig() const { return m_raw_config
; }
146 void SetRawConfig(const std::string
&c
) { m_raw_config
= c
; }
149 virtual Unsupported
* Clone() const { return new Unsupported(*this); }
150 virtual bool IsUnsupported() const { return true; }
151 virtual std::string
GetAppName() const { return AppName(); }
152 virtual void Save(OpenSync::API
&api
, const std::string
&group_name
) const
154 api
.GetConverter().Save(*this, group_name
);
156 virtual std::string
GetPluginName(OpenSync::API
&api
) const
158 return api
.GetConverter().GetPluginName(*this);
160 virtual bool IsConfigured(OpenSync::API
&api
) const
162 return api
.GetConverter().IsConfigured(*this);
164 virtual pst_type
GetSupportedSyncTypes(OpenSync::API
&api
) const
166 return api
.GetConverter().GetSupportedSyncTypes(*this);
170 static std::string
AppName() { return "Unsupported"; }
173 class Barry
: public Plugin
176 // configuration settings
179 std::string m_password
;
182 // This constructor is protected, so that only derived
183 // classes can create a configuration without a pin number.
185 : m_debug_mode(false)
190 explicit Barry(const ::Barry::Pin
&pin
)
191 : m_debug_mode(false)
195 throw std::logic_error("Barry config must have valid pin number.");
198 explicit Barry(Converter
*load_converter
, const Member
&member
)
199 : m_debug_mode(false)
201 load_converter
->Load(*this, member
);
203 // check that the loaded pin is valid... if not, it is
204 // likely safe to assume that something is horribly wrong.
205 // in the case where the Application wishes to add a new
206 // barry plugin, it should use the Pin constructor.
207 // if you *really* need to try to salvage an old
208 // corrupt config, you can always do the
209 // converter->Load(barry_obj) manually, and pick out
212 if( !m_pin
.Valid() ) {
213 std::ostringstream oss
;
214 oss
<< "Unable to load pin number from Barry plugin config. Consider this group corrupt, or not fully configured: " << member
;
215 throw LoadError(oss
.str());
219 bool IsDebugMode() const { return m_debug_mode
; }
220 ::Barry::Pin
GetPin() const { return m_pin
; }
221 std::string
GetPassword() const { return m_password
; }
223 void DebugMode(bool mode
= true) { m_debug_mode
= mode
; }
224 void SetPin(const ::Barry::Pin
&pin
) { m_pin
= pin
; }
225 void SetPassword(const std::string
&pass
) { m_password
= pass
; }
228 virtual Barry
* Clone() const { return new Barry(*this); }
229 virtual bool IsUnsupported() const { return false; }
230 virtual std::string
GetAppName() const { return AppName(); }
231 virtual void Save(OpenSync::API
&api
, const std::string
&group_name
) const
233 api
.GetConverter().Save(*this, group_name
);
235 virtual std::string
GetPluginName(OpenSync::API
&api
) const
237 return api
.GetConverter().GetPluginName(*this);
239 virtual bool IsConfigured(OpenSync::API
&api
) const
241 return api
.GetConverter().IsConfigured(*this);
243 virtual pst_type
GetSupportedSyncTypes(OpenSync::API
&api
) const
245 return api
.GetConverter().GetSupportedSyncTypes(*this);
247 virtual bool Compare(const Plugin
&plugin
,
248 bool &sametype
, bool &equal
) const
250 sametype
= equal
= false;
251 const Barry
*other
= dynamic_cast<const Barry
*> (&plugin
);
255 if( m_debug_mode
== other
->m_debug_mode
&&
256 m_pin
== other
->m_pin
&&
257 m_password
== other
->m_password
)
260 return sametype
&& equal
;
264 static std::string
AppName() { return "Barry"; }
265 static std::string
PluginName(OpenSync::API
&api
)
267 return api
.GetConverter().GetPluginName(Barry());
269 static pst_type
SupportedSyncTypes(OpenSync::API
&api
)
271 return api
.GetConverter().GetSupportedSyncTypes(Barry());
275 class Evolution
: public Plugin
278 // configuration settings
279 std::string m_address_path
;
280 std::string m_calendar_path
;
281 std::string m_tasks_path
;
282 std::string m_memos_path
;
285 static void SetIfExists(std::string
&var
, const std::string
&dir
);
292 explicit Evolution(Converter
*load_converter
, const Member
&member
)
294 load_converter
->Load(*this, member
);
297 const std::string
& GetAddressPath() const { return m_address_path
; }
298 const std::string
& GetCalendarPath() const { return m_calendar_path
; }
299 const std::string
& GetTasksPath() const { return m_tasks_path
; }
300 const std::string
& GetMemosPath() const { return m_memos_path
; }
302 void SetAddressPath(const std::string
&p
) { m_address_path
= p
; }
303 void SetCalendarPath(const std::string
&p
) { m_calendar_path
= p
; }
304 void SetTasksPath(const std::string
&p
) { m_tasks_path
= p
; }
305 void SetMemosPath(const std::string
&p
) { m_memos_path
= p
; }
307 // specific operations
308 bool AutoDetect(); // throw if unable to detect??
311 virtual Evolution
* Clone() const { return new Evolution(*this); }
312 virtual bool IsUnsupported() const { return false; }
313 virtual std::string
GetAppName() const { return AppName(); }
314 virtual void Save(OpenSync::API
&api
, const std::string
&group_name
) const
316 api
.GetConverter().Save(*this, group_name
);
318 virtual std::string
GetPluginName(OpenSync::API
&api
) const
320 return api
.GetConverter().GetPluginName(*this);
322 virtual bool IsConfigured(OpenSync::API
&api
) const
324 return api
.GetConverter().IsConfigured(*this);
326 virtual pst_type
GetSupportedSyncTypes(OpenSync::API
&api
) const
328 return api
.GetConverter().GetSupportedSyncTypes(*this);
330 virtual bool Compare(const Plugin
&plugin
,
331 bool &sametype
, bool &equal
) const
333 sametype
= equal
= false;
334 const Evolution
*other
= dynamic_cast<const Evolution
*> (&plugin
);
338 if( m_address_path
== other
->m_address_path
&&
339 m_calendar_path
== other
->m_calendar_path
&&
340 m_tasks_path
== other
->m_tasks_path
&&
341 m_memos_path
== other
->m_memos_path
)
344 return sametype
&& equal
;
348 static std::string
AppName() { return "Evolution"; }
349 static std::string
PluginName(OpenSync::API
&api
)
351 return api
.GetConverter().GetPluginName(Evolution());
353 static pst_type
SupportedSyncTypes(OpenSync::API
&api
)
355 return api
.GetConverter().GetSupportedSyncTypes(Evolution());
359 class Evolution3
: public Evolution
366 explicit Evolution3(Converter
*load_converter
, const Member
&member
)
368 load_converter
->Load(*this, member
);
372 virtual Evolution3
* Clone() const { return new Evolution3(*this); }
373 virtual std::string
GetAppName() const { return AppName(); }
374 virtual void Save(OpenSync::API
&api
, const std::string
&group_name
) const
376 api
.GetConverter().Save(*this, group_name
);
378 virtual std::string
GetPluginName(OpenSync::API
&api
) const
380 return api
.GetConverter().GetPluginName(*this);
382 virtual bool IsConfigured(OpenSync::API
&api
) const
384 return api
.GetConverter().IsConfigured(*this);
386 virtual pst_type
GetSupportedSyncTypes(OpenSync::API
&api
) const
388 return api
.GetConverter().GetSupportedSyncTypes(*this);
390 virtual bool Compare(const Plugin
&plugin
,
391 bool &sametype
, bool &equal
) const
393 sametype
= equal
= false;
394 const Evolution3
*other
= dynamic_cast<const Evolution3
*> (&plugin
);
396 // we've established that Evolution3 == Evolution3,
397 // but the actual compare can be done by the base
399 return Evolution::Compare(plugin
, sametype
, equal
);
406 static std::string
AppName() { return "Evolution 3"; }
407 static std::string
PluginName(OpenSync::API
&api
)
409 return api
.GetConverter().GetPluginName(Evolution3());
411 static pst_type
SupportedSyncTypes(OpenSync::API
&api
)
413 return api
.GetConverter().GetSupportedSyncTypes(Evolution3());
417 class Google
: public Plugin
420 // configuration settings
421 std::string m_username
;
422 std::string m_password
;
423 bool m_contacts_enabled
;
424 bool m_calendar_enabled
;
428 : m_contacts_enabled(true)
429 , m_calendar_enabled(true)
433 explicit Google(Converter
*load_converter
, const Member
&member
)
435 load_converter
->Load(*this, member
);
438 const std::string
& GetUsername() const { return m_username
; }
439 const std::string
& GetPassword() const { return m_password
; }
440 bool IsContactsEnabled() const { return m_contacts_enabled
; }
441 bool IsCalendarEnabled() const { return m_calendar_enabled
; }
443 void SetUsername(const std::string
&u
) { m_username
= u
; }
444 void SetPassword(const std::string
&p
) { m_password
= p
; }
445 bool EnableContacts(bool setting
= true)
447 bool old
= m_contacts_enabled
;
448 m_contacts_enabled
= setting
;
451 bool EnableCalendar(bool setting
= true)
453 bool old
= m_calendar_enabled
;
454 m_calendar_enabled
= setting
;
459 virtual Google
* Clone() const { return new Google(*this); }
460 virtual bool IsUnsupported() const { return false; }
461 virtual std::string
GetAppName() const { return AppName(); }
462 virtual void Save(OpenSync::API
&api
, const std::string
&group_name
) const
464 api
.GetConverter().Save(*this, group_name
);
466 virtual std::string
GetPluginName(OpenSync::API
&api
) const
468 return api
.GetConverter().GetPluginName(*this);
470 virtual bool IsConfigured(OpenSync::API
&api
) const
472 return api
.GetConverter().IsConfigured(*this);
474 virtual pst_type
GetSupportedSyncTypes(OpenSync::API
&api
) const
476 return api
.GetConverter().GetSupportedSyncTypes(*this);
478 virtual bool Compare(const Plugin
&plugin
,
479 bool &sametype
, bool &equal
) const
481 sametype
= equal
= false;
482 const Google
*other
= dynamic_cast<const Google
*> (&plugin
);
486 if( m_username
== other
->m_username
&&
487 m_password
== other
->m_password
&&
488 m_contacts_enabled
== other
->m_contacts_enabled
&&
489 m_calendar_enabled
== other
->m_calendar_enabled
)
492 return sametype
&& equal
;
496 static std::string
AppName() { return "Google Calendar"; }
497 static std::string
PluginName(OpenSync::API
&api
)
499 return api
.GetConverter().GetPluginName(Google());
501 static pst_type
SupportedSyncTypes(OpenSync::API
&api
)
503 return api
.GetConverter().GetSupportedSyncTypes(Google());
507 class KDEPim
: public Plugin
510 // version 0.22 has no config
517 explicit KDEPim(Converter
*load_converter
, const Member
&member
)
519 load_converter
->Load(*this, member
);
523 virtual KDEPim
* Clone() const { return new KDEPim(*this); }
524 virtual bool IsUnsupported() const { return false; }
525 virtual std::string
GetAppName() const { return AppName(); }
526 virtual void Save(OpenSync::API
&api
, const std::string
&group_name
) const
528 api
.GetConverter().Save(*this, group_name
);
530 virtual std::string
GetPluginName(OpenSync::API
&api
) const
532 return api
.GetConverter().GetPluginName(*this);
534 virtual bool IsConfigured(OpenSync::API
&api
) const
536 return api
.GetConverter().IsConfigured(*this);
538 virtual pst_type
GetSupportedSyncTypes(OpenSync::API
&api
) const
540 return api
.GetConverter().GetSupportedSyncTypes(*this);
542 virtual bool Compare(const Plugin
&plugin
,
543 bool &sametype
, bool &equal
) const
545 sametype
= equal
= false;
546 const KDEPim
*other
= dynamic_cast<const KDEPim
*> (&plugin
);
550 // no data for this config, so always equal
553 return sametype
&& equal
;
557 static std::string
AppName() { return "KDEPim / Kontact"; }
558 static std::string
PluginName(OpenSync::API
&api
)
560 return api
.GetConverter().GetPluginName(KDEPim());
562 static pst_type
SupportedSyncTypes(OpenSync::API
&api
)
564 return api
.GetConverter().GetSupportedSyncTypes(KDEPim());
571 /// This class handles the loading of all the config plugins in a
575 private std::vector
<std::tr1::shared_ptr
<OpenSync::Config::Plugin
> >
578 typedef std::tr1::shared_ptr
<OpenSync::Config::Plugin
> value_type
;
579 typedef std::tr1::shared_ptr
<Group
> group_ptr
;
580 typedef value_type plugin_ptr
;
581 typedef std::vector
<value_type
> base_type
;
582 typedef base_type::iterator iterator
;
583 typedef base_type::const_iterator const_iterator
;
586 std::string m_group_name
;
589 static void BarryCheck(OpenSync::API
&api
,
590 const std::string
&group_name
,
591 const member_list_type
&members
,
592 unsigned throw_mask
);
594 // not copyable... use Clone()
595 Group(const Group
&other
);
596 Group
& operator=(const Group
&other
);
599 // OpenSync Config Group Throw Masks
600 #define OSCG_THROW_ON_UNSUPPORTED 0x01
601 #define OSCG_THROW_ON_NO_BARRY 0x02
602 #define OSCG_THROW_ON_MULTIPLE_BARRIES 0x04
604 /// This constructor loads an existing Group from the
605 /// given OpenSync api resource, filling the vector
606 /// array with the required plugin config classes.
608 /// If desired, specify a mask, which will cause the
609 /// constructor to throw exceptions on the given conditions.
611 /// OSCG_THROW_ON_UNSUPPORTED - if set, the constructor will
612 /// throw LoadError if there is a plugin in the group
613 /// for which there is no corresponding Plugin-
614 /// derived class to handle it
615 /// OSCG_THROW_ON_NO_BARRY - if set, the constructor will
616 /// throw LoadError if there are no Barry plugins
618 /// OSCG_THROW_ON_MULTIPLE_BARRIES - if set, the constructor will
619 /// throw LoadError if there is more than one Barry
620 /// plugin in the group
622 Group(const std::string
&group_name
, OpenSync::API
&api
,
623 unsigned throw_mask
= 0);
625 /// This constructor creates an empty, but named, Group.
626 explicit Group(const std::string
&group_name
);
628 bool HasUnsupportedPlugins() const;
629 bool HasBarryPlugins() const;
630 bool GroupExists(OpenSync::API
&api
) const;
631 bool AllConfigured(OpenSync::API
&api
) const;
632 int GetConnectedCount() const;
633 const std::string
& GetGroupName() const { return m_group_name
; }
635 /// Cycles through all plugins and ANDs the supported types
636 /// for each together, and returns it. The returned value is
637 /// the minimum set of supportable sync types for this group for
638 /// the given API engine.
639 pst_type
GetSupportedSyncTypes(OpenSync::API
&api
) const;
641 /// Returns comma separated string of application/plugin names,
643 std::string
GetAppNames() const;
645 /// Returns a reference to the (first) Barry plugin in the group.
646 /// Will throw std::logic_error if not found.
647 OpenSync::Config::Barry
& GetBarryPlugin();
648 const OpenSync::Config::Barry
& GetBarryPlugin() const;
650 /// Returns a pointer to the first non-Barry plugin in the group.
651 /// Returns 0 if not found.
652 OpenSync::Config::Plugin
* GetNonBarryPlugin();
653 const OpenSync::Config::Plugin
* GetNonBarryPlugin() const;
655 /// Sets the member_id of all plugins to -1, thereby making them
656 /// appear like they have never been saved to an API before.
657 /// A call to Save() after a call to DisconnectMembers() will
658 /// create a completely new group. Note that if the group name
659 /// already exists in the API, saving to it again may cause
660 /// a low level exception to be thrown.
661 void DisconnectMembers();
663 /// Loads all available plugins from the named group, using
664 /// the api given. If this Group isn't empty (size() != 0),
665 /// then DisconnectMembers() will be called first.
666 void Load(OpenSync::API
&api
, unsigned throw_mask
= 0);
667 /// Same as Load() above, but loads from src_group_name instead
668 /// of m_group_name. m_group_name will not be changed.
669 void Load(const std::string
&src_group_name
, OpenSync::API
&api
,
670 unsigned throw_mask
= 0);
672 /// Overwrites m_group_name with a new one.
673 void ResetGroupName(const std::string
&new_group_name
);
675 /// Adds plugin to group, and takes ownership of the pointer
676 void AddPlugin(OpenSync::Config::Plugin
*plugin
);
678 /// Remove the plugin from the group. Only can guarantee removal
679 /// of non-saved (i.e. disconnected) plugins. If a plugin has
680 /// already been saved to the underlying OpenSync API, it
681 /// may not be possible to delete just one member from the
682 /// group (0.22), and a DeleteError will be thrown.
684 /// If you don't want to actually delete any members from any
685 /// existing configs, then DisconnectMembers() first.
686 void DeletePlugin(iterator i
, OpenSync::API
&api
);
688 /// Returns true if it is possible to resave this
689 /// Group to the existing opensync config. If this returns
690 /// false, and there are connected members, Save() will
691 /// throw. Save() will automatically call this function
694 /// Note: only call this if GroupExists() is true, otherwise,
695 /// this function is irrelevant.
696 bool GroupMatchesExistingConfig(OpenSync::API
&api
);
698 /// Save all plugins as members of the group. If the group doesn't
699 /// exist, it will be created. If the group does exist, and its
700 /// existing plugin list and member_id's don't match this Group's
701 /// list, SaveError will be thrown. If a plugin doesn't have a
702 /// member_id, it will be added as a new member to the group and
703 /// given an id. Note that an exception thrown from this function
704 /// means that the group is left in an undefined state. Best to
705 /// delete it and start over. Or load it, delete it (through the API),
706 /// disconnect it, and save it again.
707 void Save(OpenSync::API
&api
);
709 /// Compares against another group, including all the plugins,
710 /// and each plugin data. Order of plugins in group does not
711 /// matter, nor do file-specific data, like member IDs.
712 /// If saving both groups would result in functionally equivalent
713 /// sync groups, then they are equal. This is used to avoid
714 /// re-saving when not necessary.
715 /// Returns true if equal.
716 bool Compare(const Group
&group
) const;
718 /// Clones this Group object and returns a group_ptr of the new one
719 group_ptr
Clone() const;
721 /// Forget all plugins and delete them all
722 using base_type::clear
;
724 // bring underlying implementation forward
725 using base_type::size
;
726 using base_type::begin
;
727 using base_type::end
;
728 using base_type::operator[];
732 }} // namespace OpenSync::Config