lib: added implicit ctor converter from DatabaseDatabase to DBListType
[barry/progweb.git] / desktop / src / osconfig.h
blob0dc4064f712576c6d48eb5aed71044940e224f22
1 ///
2 /// \file osconfig.h
3 /// Class which detects a set of available or known devices
4 /// in an opensync-able system.
5 ///
7 /*
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>
27 #include <memory>
28 #include <tr1/memory>
29 #include <stdexcept>
30 #include "osbase.h"
31 #include "ostypes.h"
32 #include <iostream>
34 namespace OpenSync { namespace Config {
36 // Exception class for configuration load errors
37 class LoadError : public std::exception
39 std::string m_msg;
40 public:
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
48 std::string m_msg;
49 public:
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
58 public:
59 DeleteError(const std::string &msg) : std::logic_error(msg) {}
62 // Base class for all opensync plugin configurations
63 class Plugin
65 long m_member_id; // -1 if not saved to the group yet
67 public:
68 Plugin()
69 : m_member_id(-1)
73 virtual ~Plugin()
77 long GetMemberId() const { return m_member_id; }
78 void SetMemberId(long id) { m_member_id = id; }
81 // operations
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:
97 //{
98 // api.GetConverter().Save(*this, group_name);
99 //}
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
130 private:
131 // configuration settings
132 std::string m_raw_config;
134 public:
135 Unsupported()
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; }
148 // virtual overrides
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);
169 // statics
170 static std::string AppName() { return "Unsupported"; }
173 class Barry : public Plugin
175 private:
176 // configuration settings
177 bool m_debug_mode;
178 ::Barry::Pin m_pin;
179 std::string m_password;
181 protected:
182 // This constructor is protected, so that only derived
183 // classes can create a configuration without a pin number.
184 Barry()
185 : m_debug_mode(false)
189 public:
190 explicit Barry(const ::Barry::Pin &pin)
191 : m_debug_mode(false)
192 , m_pin(pin)
194 if( !m_pin.Valid() )
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
210 // the left overs.
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; }
227 // virtual overrides
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);
252 if( other ) {
253 sametype = true;
255 if( m_debug_mode == other->m_debug_mode &&
256 m_pin == other->m_pin &&
257 m_password == other->m_password )
258 equal = true;
260 return sametype && equal;
263 // statics
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
277 private:
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;
284 protected:
285 static void SetIfExists(std::string &var, const std::string &dir);
287 public:
288 Evolution()
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??
310 // virtual overrides
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);
335 if( other ) {
336 sametype = true;
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 )
342 equal = true;
344 return sametype && equal;
347 // statics
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 Google : public Plugin
361 private:
362 // configuration settings
363 std::string m_username;
364 std::string m_password;
365 bool m_contacts_enabled;
366 bool m_calendar_enabled;
368 public:
369 Google()
370 : m_contacts_enabled(true)
371 , m_calendar_enabled(true)
375 explicit Google(Converter *load_converter, const Member &member)
377 load_converter->Load(*this, member);
380 const std::string& GetUsername() const { return m_username; }
381 const std::string& GetPassword() const { return m_password; }
382 bool IsContactsEnabled() const { return m_contacts_enabled; }
383 bool IsCalendarEnabled() const { return m_calendar_enabled; }
385 void SetUsername(const std::string &u) { m_username = u; }
386 void SetPassword(const std::string &p) { m_password = p; }
387 bool EnableContacts(bool setting = true)
389 bool old = m_contacts_enabled;
390 m_contacts_enabled = setting;
391 return old;
393 bool EnableCalendar(bool setting = true)
395 bool old = m_calendar_enabled;
396 m_calendar_enabled = setting;
397 return old;
400 // virtual overrides
401 virtual Google* Clone() const { return new Google(*this); }
402 virtual bool IsUnsupported() const { return false; }
403 virtual std::string GetAppName() const { return AppName(); }
404 virtual void Save(OpenSync::API &api, const std::string &group_name) const
406 api.GetConverter().Save(*this, group_name);
408 virtual std::string GetPluginName(OpenSync::API &api) const
410 return api.GetConverter().GetPluginName(*this);
412 virtual bool IsConfigured(OpenSync::API &api) const
414 return api.GetConverter().IsConfigured(*this);
416 virtual pst_type GetSupportedSyncTypes(OpenSync::API &api) const
418 return api.GetConverter().GetSupportedSyncTypes(*this);
420 virtual bool Compare(const Plugin &plugin,
421 bool &sametype, bool &equal) const
423 sametype = equal = false;
424 const Google *other = dynamic_cast<const Google*> (&plugin);
425 if( other ) {
426 sametype = true;
428 if( m_username == other->m_username &&
429 m_password == other->m_password &&
430 m_contacts_enabled == other->m_contacts_enabled &&
431 m_calendar_enabled == other->m_calendar_enabled )
432 equal = true;
434 return sametype && equal;
437 // statics
438 static std::string AppName() { return "Google Calendar"; }
439 static std::string PluginName(OpenSync::API &api)
441 return api.GetConverter().GetPluginName(Google());
443 static pst_type SupportedSyncTypes(OpenSync::API &api)
445 return api.GetConverter().GetSupportedSyncTypes(Google());
449 class KDEPim : public Plugin
451 private:
452 // version 0.22 has no config
454 public:
455 KDEPim()
459 explicit KDEPim(Converter *load_converter, const Member &member)
461 load_converter->Load(*this, member);
464 // virtual overrides
465 virtual KDEPim* Clone() const { return new KDEPim(*this); }
466 virtual bool IsUnsupported() const { return false; }
467 virtual std::string GetAppName() const { return AppName(); }
468 virtual void Save(OpenSync::API &api, const std::string &group_name) const
470 api.GetConverter().Save(*this, group_name);
472 virtual std::string GetPluginName(OpenSync::API &api) const
474 return api.GetConverter().GetPluginName(*this);
476 virtual bool IsConfigured(OpenSync::API &api) const
478 return api.GetConverter().IsConfigured(*this);
480 virtual pst_type GetSupportedSyncTypes(OpenSync::API &api) const
482 return api.GetConverter().GetSupportedSyncTypes(*this);
484 virtual bool Compare(const Plugin &plugin,
485 bool &sametype, bool &equal) const
487 sametype = equal = false;
488 const KDEPim *other = dynamic_cast<const KDEPim*> (&plugin);
489 if( other ) {
490 sametype = true;
492 // no data for this config, so always equal
493 equal = true;
495 return sametype && equal;
498 // statics
499 static std::string AppName() { return "KDEPim / Kontact"; }
500 static std::string PluginName(OpenSync::API &api)
502 return api.GetConverter().GetPluginName(KDEPim());
504 static pst_type SupportedSyncTypes(OpenSync::API &api)
506 return api.GetConverter().GetSupportedSyncTypes(KDEPim());
511 // Group
513 /// This class handles the loading of all the config plugins in a
514 /// given group.
516 class Group :
517 private std::vector<std::tr1::shared_ptr<OpenSync::Config::Plugin> >
519 public:
520 typedef std::tr1::shared_ptr<OpenSync::Config::Plugin> value_type;
521 typedef std::tr1::shared_ptr<Group> group_ptr;
522 typedef value_type plugin_ptr;
523 typedef std::vector<value_type> base_type;
524 typedef base_type::iterator iterator;
525 typedef base_type::const_iterator const_iterator;
527 private:
528 std::string m_group_name;
530 protected:
531 static void BarryCheck(OpenSync::API &api,
532 const std::string &group_name,
533 const member_list_type &members,
534 unsigned throw_mask);
536 // not copyable... use Clone()
537 Group(const Group &other);
538 Group& operator=(const Group &other);
540 public:
541 // OpenSync Config Group Throw Masks
542 #define OSCG_THROW_ON_UNSUPPORTED 0x01
543 #define OSCG_THROW_ON_NO_BARRY 0x02
544 #define OSCG_THROW_ON_MULTIPLE_BARRIES 0x04
546 /// This constructor loads an existing Group from the
547 /// given OpenSync api resource, filling the vector
548 /// array with the required plugin config classes.
550 /// If desired, specify a mask, which will cause the
551 /// constructor to throw exceptions on the given conditions.
553 /// OSCG_THROW_ON_UNSUPPORTED - if set, the constructor will
554 /// throw LoadError if there is a plugin in the group
555 /// for which there is no corresponding Plugin-
556 /// derived class to handle it
557 /// OSCG_THROW_ON_NO_BARRY - if set, the constructor will
558 /// throw LoadError if there are no Barry plugins
559 /// in the group
560 /// OSCG_THROW_ON_MULTIPLE_BARRIES - if set, the constructor will
561 /// throw LoadError if there is more than one Barry
562 /// plugin in the group
564 Group(const std::string &group_name, OpenSync::API &api,
565 unsigned throw_mask = 0);
567 /// This constructor creates an empty, but named, Group.
568 explicit Group(const std::string &group_name);
570 bool HasUnsupportedPlugins() const;
571 bool HasBarryPlugins() const;
572 bool GroupExists(OpenSync::API &api) const;
573 bool AllConfigured(OpenSync::API &api) const;
574 int GetConnectedCount() const;
575 const std::string& GetGroupName() const { return m_group_name; }
577 /// Cycles through all plugins and ANDs the supported types
578 /// for each together, and returns it. The returned value is
579 /// the minimum set of supportable sync types for this group for
580 /// the given API engine.
581 pst_type GetSupportedSyncTypes(OpenSync::API &api) const;
583 /// Returns comma separated string of application/plugin names,
584 /// excluding Barry
585 std::string GetAppNames() const;
587 /// Returns a reference to the (first) Barry plugin in the group.
588 /// Will throw std::logic_error if not found.
589 OpenSync::Config::Barry& GetBarryPlugin();
590 const OpenSync::Config::Barry& GetBarryPlugin() const;
592 /// Returns a pointer to the first non-Barry plugin in the group.
593 /// Returns 0 if not found.
594 OpenSync::Config::Plugin* GetNonBarryPlugin();
595 const OpenSync::Config::Plugin* GetNonBarryPlugin() const;
597 /// Sets the member_id of all plugins to -1, thereby making them
598 /// appear like they have never been saved to an API before.
599 /// A call to Save() after a call to DisconnectMembers() will
600 /// create a completely new group. Note that if the group name
601 /// already exists in the API, saving to it again may cause
602 /// a low level exception to be thrown.
603 void DisconnectMembers();
605 /// Loads all available plugins from the named group, using
606 /// the api given. If this Group isn't empty (size() != 0),
607 /// then DisconnectMembers() will be called first.
608 void Load(OpenSync::API &api, unsigned throw_mask = 0);
609 /// Same as Load() above, but loads from src_group_name instead
610 /// of m_group_name. m_group_name will not be changed.
611 void Load(const std::string &src_group_name, OpenSync::API &api,
612 unsigned throw_mask = 0);
614 /// Overwrites m_group_name with a new one.
615 void ResetGroupName(const std::string &new_group_name);
617 /// Adds plugin to group, and takes ownership of the pointer
618 void AddPlugin(OpenSync::Config::Plugin *plugin);
620 /// Remove the plugin from the group. Only can guarantee removal
621 /// of non-saved (i.e. disconnected) plugins. If a plugin has
622 /// already been saved to the underlying OpenSync API, it
623 /// may not be possible to delete just one member from the
624 /// group (0.22), and a DeleteError will be thrown.
626 /// If you don't want to actually delete any members from any
627 /// existing configs, then DisconnectMembers() first.
628 void DeletePlugin(iterator i, OpenSync::API &api);
630 /// Returns true if it is possible to resave this
631 /// Group to the existing opensync config. If this returns
632 /// false, and there are connected members, Save() will
633 /// throw. Save() will automatically call this function
634 /// during saving.
636 /// Note: only call this if GroupExists() is true, otherwise,
637 /// this function is irrelevant.
638 bool GroupMatchesExistingConfig(OpenSync::API &api);
640 /// Save all plugins as members of the group. If the group doesn't
641 /// exist, it will be created. If the group does exist, and its
642 /// existing plugin list and member_id's don't match this Group's
643 /// list, SaveError will be thrown. If a plugin doesn't have a
644 /// member_id, it will be added as a new member to the group and
645 /// given an id. Note that an exception thrown from this function
646 /// means that the group is left in an undefined state. Best to
647 /// delete it and start over. Or load it, delete it (through the API),
648 /// disconnect it, and save it again.
649 void Save(OpenSync::API &api);
651 /// Compares against another group, including all the plugins,
652 /// and each plugin data. Order of plugins in group does not
653 /// matter, nor do file-specific data, like member IDs.
654 /// If saving both groups would result in functionally equivalent
655 /// sync groups, then they are equal. This is used to avoid
656 /// re-saving when not necessary.
657 /// Returns true if equal.
658 bool Compare(const Group &group) const;
660 /// Clones this Group object and returns a group_ptr of the new one
661 group_ptr Clone() const;
663 /// Forget all plugins and delete them all
664 using base_type::clear;
666 // bring underlying implementation forward
667 using base_type::size;
668 using base_type::begin;
669 using base_type::end;
670 using base_type::operator[];
674 }} // namespace OpenSync::Config
676 #endif