tzwrapper.cc: fixed use of iterator after erase
[barry.git] / desktop / src / deviceset.h
blobb01194e99eee818d63bf43e3845430541f3df3fc
1 ///
2 /// \file deviceset.h
3 /// Class which detects a set of available or known devices
4 /// in an opensync-able system.
5 ///
7 /*
8 Copyright (C) 2009-2013, 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_DEVICE_SET_H__
24 #define __BARRY_DEVICE_SET_H__
26 #include "osconfig.h"
27 #include "ostypes.h"
28 #include <iosfwd>
30 namespace Barry {
31 class GlobalConfigFile;
35 // DeviceExtras
37 /// Config class to hold, load, and save device-related extras that are
38 /// not saved in either the Barry::ConfigFile or the OpenSync group
39 /// config.
40 ///
41 /// These items are not stored in Barry::ConfigFile since they pertain
42 /// to a particular OpenSync sync group. The user may have groups
43 /// configured outside of BarryDesktop.
44 class DeviceExtras
46 Barry::Pin m_pin;
48 public:
49 // config data... The Extras
50 std::string m_favour_plugin_name; // if empty, ask user
51 time_t m_last_sync_time;
52 OpenSync::Config::pst_type m_sync_types;
54 protected:
55 std::string MakeBaseKey(const std::string &group_name);
57 public:
58 explicit DeviceExtras(const Barry::Pin &pin);
59 DeviceExtras(const Barry::Pin& pin,
60 const Barry::GlobalConfigFile &config,
61 const std::string &group_name);
64 // operations
66 void Load(const Barry::GlobalConfigFile &config,
67 const std::string &group_name);
68 void Save(Barry::GlobalConfigFile &config,
69 const std::string &group_name);
74 // DeviceEntry
76 /// Entry in the DeviceSet class. This class must be STL container safe.
77 ///
78 class DeviceEntry
80 public:
81 typedef std::tr1::shared_ptr<OpenSync::Config::Group> group_ptr;
82 typedef std::tr1::shared_ptr<DeviceExtras> extras_ptr;
84 private:
85 // pointers to external data
86 // pointers may be 0 if data is not available (such as when
87 // a device is not connected, it would not have a ProbeResult)
88 const Barry::ProbeResult *m_result; // pointer to external data
90 group_ptr m_group; // may contain 0
91 OpenSync::API *m_engine; // may be 0
92 extras_ptr m_extras; // may contain 0
94 std::string m_device_name;
96 protected:
97 OpenSync::Config::Barry* FindBarry(); // returns pointer to the Barry
98 // plugin object in m_group
99 // or 0 if not available
101 public:
102 DeviceEntry(const Barry::GlobalConfigFile &config,
103 const Barry::ProbeResult *result,
104 group_ptr group,
105 OpenSync::API *engine,
106 const std::string &secondary_device_name = "");
108 Barry::Pin GetPin() const;
109 std::string GetDeviceName() const;
110 bool IsConnected() const { return m_result; }
111 bool IsConfigured() const { return m_group.get() && m_engine && m_group->AllConfigured(*m_engine); }
112 std::string GetAppNames() const { return m_group.get() && m_engine ? m_group->GetAppNames() : ""; }
114 /// Returns a string uniquely identifying this DeviceEntry
115 std::string GetIdentifyingString() const;
117 const Barry::ProbeResult* GetProbeResult() { return m_result; }
118 OpenSync::Config::Group* GetConfigGroup() { return m_group.get(); }
119 const OpenSync::Config::Group* GetConfigGroup() const { return m_group.get(); }
120 OpenSync::API* GetEngine() { return m_engine; }
121 const OpenSync::API* GetEngine() const { return m_engine; }
123 DeviceExtras* GetExtras() { return m_extras.get(); }
124 const DeviceExtras* GetExtras() const { return m_extras.get(); }
126 void SetConfigGroup(group_ptr group, OpenSync::API *engine,
127 extras_ptr extras);
128 void SetDeviceName(const std::string &name) { m_device_name = name; }
131 std::ostream& operator<< (std::ostream &os, const DeviceEntry &de);
134 // DeviceSet
136 /// This class detects known devices on an opensync-able system.
137 /// It will search for connected (USB) devices and devices that have been
138 /// configured in OpenSync (both 0.22 and 0.4x) but are not currently
139 /// connected.
141 /// For each device entry, it will know the following:
142 /// - pin
143 /// - device name (from barrybackup configs)
144 /// - whether connected or not
145 /// - whether configured for opensync or not... if so, it will also
146 /// keep track of:
147 /// - the app(s) it will sync with (in one sync group only)
148 /// - the version of the engine it is configured with
150 /// If a device is configured in both 0.4x and 0.22, or even in two
151 /// groups in one engine, then all are loaded. Use FindDuplicates()
152 /// and KillDuplicates() to sort that out with the user's help.
154 /// Since this class needs to open and parse a lot of information during
155 /// construction anyway, it will store as much as possible, to allow for
156 /// editing in a GUI and saving it later. The domain specific data
157 /// may be encapsulated in further classes.
159 class DeviceSet : public std::vector<DeviceEntry>
161 public:
162 typedef std::vector<DeviceEntry> base_type;
163 typedef base_type::iterator iterator;
164 typedef base_type::const_iterator const_iterator;
165 typedef std::vector<iterator> subset_type;
167 private:
168 const Barry::GlobalConfigFile &m_config;
169 OpenSync::APISet &m_apiset;
170 Barry::Probe::Results m_results;
172 protected:
173 void LoadSet();
174 void LoadConfigured(OpenSync::API &api);
175 void LoadUnconfigured();
176 void Sort();
178 public:
179 /// Does a USB probe automatically
180 DeviceSet(const Barry::GlobalConfigFile &config,
181 OpenSync::APISet &apiset);
183 /// Skips the USB probe and uses the results set given
184 DeviceSet(const Barry::GlobalConfigFile &config,
185 OpenSync::APISet &apiset,
186 const Barry::Probe::Results &results);
188 iterator FindPin(const Barry::Pin &pin);
189 const_iterator FindPin(const Barry::Pin &pin) const;
190 static subset_type::const_iterator FindPin(const subset_type &subset, const Barry::Pin &pin);
191 static std::string Subset2String(const subset_type &set);
192 subset_type String2Subset(const std::string &list);
194 /// Searches for DeviceEntry's in the set that have the same
195 /// pin number. This is most likely due to OpenSync having
196 /// multiple configurations with the same device. This
197 /// function returns a vector of iterators into the DeviceSet,
198 /// or an empty vector if no duplicates are found.
200 /// You can solve the duplicate by erase()ing the chosen
201 /// iterator. Call this function multiple times to find
202 /// all the duplicates.
203 subset_type FindDuplicates();
205 /// Safely removes all entries that are referenced by
206 /// the iterators in dups. Note that a call to erase()
207 /// invalidates the other iterators, so this function
208 /// does it safely.
209 void KillDuplicates(const subset_type &dups);
212 std::ostream& operator<< (std::ostream &os, const DeviceSet &ds);
214 #endif