menu: added new Keywords tag to .desktop files
[barry.git] / src / ldif.h
blobc6b38782ace565c6ddbcbed425d0bfece89cea84
1 ///
2 /// \file ldif.h
3 /// Routines for reading and writing LDAP LDIF data.
4 ///
6 /*
7 Copyright (C) 2005-2013, 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 #ifndef __BARRY_LDIF_H__
23 #define __BARRY_LDIF_H__
25 #include "dll.h"
26 #include <string>
27 #include <map>
29 // forward declarations
30 namespace Barry {
31 class Contact;
34 namespace Barry {
37 // ContactLdif
39 /// Class for generating LDIF output based on a Barry::Contact record object.
40 /// This class supports LDIF attribute mapping, and a heuristics mechanism
41 /// for parsing LDIF fields that may not have consistent data.
42 ///
43 /// To use this class, create an instance of it, then call DumpLdif(), passing
44 /// the Contact record object to base the work on. Output will be written
45 /// to the stream you provide. ReadLdif() goes in the other direction.
46 ///
47 /// To override LDIF attribute mapping, call Map() or Unmap() as appropriate.
48 ///
49 /// To get a list of supported Barry::Contact field names, call GetFieldNames().
50 /// This function returns a pointer to an array of ContactLdif::NameToFunc
51 /// structures, ending with NameToFunc::name as null. You can cycle through the
52 /// array with code like this:
53 ///
54 /// <pre>
55 /// for( ContactLdif::NameToFunc *n = o.GetFieldNames(); n->name; n++ ) {
56 /// ...
57 /// }
58 /// </pre>
59 ///
60 /// Note that all Get/Set functions used in attribute mapping are virtual,
61 /// and can be overridden by a derived class. This includes the heuristics
62 /// functions, which are called by DumpLdif().
63 ///
64 /// Note that the description field in NameToFunc is translatable.
65 /// You can pass it to gettext() for a translated string.
66 ///
67 class BXEXPORT ContactLdif
69 public:
70 typedef std::string (ContactLdif::*GetFunctionType)(const Barry::Contact&) const;
71 typedef void (ContactLdif::*SetFunctionType)(Barry::Contact&, const std::string &) const;
73 /// Used to create a List of supported Barry field names, including
74 /// calculated names, such as full postal address.
75 struct NameToFunc
77 const char *name;
78 const char *description;
79 GetFunctionType read;
80 SetFunctionType write;
83 struct LdifAttribute
85 std::string name;
86 std::string objectClass;
87 int order;
89 LdifAttribute() : order(0) {}
90 LdifAttribute(const char *name, const std::string &oc = "")
91 : name(name), objectClass(oc), order(0)
93 LdifAttribute(const std::string &name, const std::string &oc = "")
94 : name(name), objectClass(oc), order(0)
97 bool operator<(const LdifAttribute &other) const;
98 bool operator==(const LdifAttribute &other) const;
101 struct AccessPair
103 GetFunctionType read;
104 SetFunctionType write;
106 AccessPair() : read(0), write(0) {}
107 AccessPair(GetFunctionType r, SetFunctionType w)
108 : read(r), write(w)
112 typedef std::map<LdifAttribute, AccessPair> AccessMapType;
113 typedef std::map<std::string, std::string*> HookMapType;
115 protected:
116 static const NameToFunc FieldMap[];
117 AccessMapType m_map;
118 std::string m_baseDN;
119 HookMapType m_hookMap;
120 LdifAttribute m_dnAttr;
122 void DoWrite(Barry::Contact &con, const std::string &attr,
123 const std::string &data);
125 // Array getter state
126 mutable unsigned int m_emailIndex;
128 // name heuristics
129 std::string m_cn, m_displayName, m_sn, m_givenName;
131 // heuristics hooking - saves each found value in the variable
132 // pointed at by var
133 void Hook(const std::string &ldifname, std::string *var);
135 public:
136 explicit ContactLdif(const std::string &baseDN);
137 virtual ~ContactLdif();
139 const NameToFunc* GetFieldNames() const;
140 const NameToFunc* GetField(const std::string &fieldname) const;
141 std::string GetFieldReadName(GetFunctionType read) const;
142 std::string GetFieldWriteName(SetFunctionType write) const;
144 bool Map(const LdifAttribute &ldifname, const std::string &readField,
145 const std::string &writeField);
146 void Map(const LdifAttribute &ldifname, GetFunctionType read,
147 SetFunctionType write);
148 void Unmap(const LdifAttribute &ldifname);
150 void SetBaseDN(const std::string &baseDN) { m_baseDN = baseDN; }
151 bool SetDNAttr(const LdifAttribute &name);
152 bool SetObjectClass(const LdifAttribute &name, const std::string &objectClass);
153 bool SetObjectOrder(const LdifAttribute &name, int order);
156 // Access functions
159 virtual std::string Email(const Barry::Contact &con) const;
160 virtual std::string Phone(const Barry::Contact &con) const;
161 virtual std::string Fax(const Barry::Contact &con) const;
162 virtual std::string WorkPhone(const Barry::Contact &con) const;
163 virtual std::string HomePhone(const Barry::Contact &con) const;
164 virtual std::string MobilePhone(const Barry::Contact &con) const;
165 virtual std::string Pager(const Barry::Contact &con) const;
166 virtual std::string PIN(const Barry::Contact &con) const;
167 virtual std::string FirstName(const Barry::Contact &con) const;
168 virtual std::string LastName(const Barry::Contact &con) const;
169 virtual std::string Company(const Barry::Contact &con) const;
170 virtual std::string DefaultCommunicationsMethod(const Barry::Contact &con) const;
171 virtual std::string WorkAddress1(const Barry::Contact &con) const;
172 virtual std::string WorkAddress2(const Barry::Contact &con) const;
173 virtual std::string WorkAddress3(const Barry::Contact &con) const;
174 virtual std::string WorkCity(const Barry::Contact &con) const;
175 virtual std::string WorkProvince(const Barry::Contact &con) const;
176 virtual std::string WorkPostalCode(const Barry::Contact &con) const;
177 virtual std::string WorkCountry(const Barry::Contact &con) const;
178 virtual std::string JobTitle(const Barry::Contact &con) const;
179 virtual std::string PublicKey(const Barry::Contact &con) const;
180 virtual std::string Notes(const Barry::Contact &con) const;
181 virtual std::string Image(const Barry::Contact &con) const;
182 // calculated values...
183 virtual std::string WorkPostalAddress(const Barry::Contact &con) const;
184 virtual std::string HomePostalAddress(const Barry::Contact &con) const;
185 virtual std::string FullName(const Barry::Contact &con) const;
186 virtual std::string FQDN(const Barry::Contact &con) const;
189 // Array modifier functions for above Access functions
192 virtual bool IsArrayFunc(GetFunctionType getf) const;
193 void ClearArrayState() const;
196 // Write functions
199 virtual void SetEmail(Barry::Contact &con, const std::string &val) const;
200 virtual void SetPhone(Barry::Contact &con, const std::string &val) const;
201 virtual void SetFax(Barry::Contact &con, const std::string &val) const;
202 virtual void SetWorkPhone(Barry::Contact &con, const std::string &val) const;
203 virtual void SetHomePhone(Barry::Contact &con, const std::string &val) const;
204 virtual void SetMobilePhone(Barry::Contact &con, const std::string &val) const;
205 virtual void SetPager(Barry::Contact &con, const std::string &val) const;
206 virtual void SetPIN(Barry::Contact &con, const std::string &val) const;
207 virtual void SetFirstName(Barry::Contact &con, const std::string &val) const;
208 virtual void SetLastName(Barry::Contact &con, const std::string &val) const;
209 virtual void SetCompany(Barry::Contact &con, const std::string &val) const;
210 virtual void SetDefaultCommunicationsMethod(Barry::Contact &con, const std::string &val) const;
211 virtual void SetWorkAddress1(Barry::Contact &con, const std::string &val) const;
212 virtual void SetWorkAddress2(Barry::Contact &con, const std::string &val) const;
213 virtual void SetWorkAddress3(Barry::Contact &con, const std::string &val) const;
214 virtual void SetWorkCity(Barry::Contact &con, const std::string &val) const;
215 virtual void SetWorkProvince(Barry::Contact &con, const std::string &val) const;
216 virtual void SetWorkPostalCode(Barry::Contact &con, const std::string &val) const;
217 virtual void SetWorkCountry(Barry::Contact &con, const std::string &val) const;
218 virtual void SetJobTitle(Barry::Contact &con, const std::string &val) const;
219 virtual void SetPublicKey(Barry::Contact &con, const std::string &val) const;
220 virtual void SetNotes(Barry::Contact &con, const std::string &val) const;
221 virtual void SetImage(Barry::Contact &con, const std::string &val) const;
222 virtual void SetWorkPostalAddress(Barry::Contact &con, const std::string &val) const;
223 virtual void SetHomePostalAddress(Barry::Contact &con, const std::string &val) const;
224 virtual void SetFullName(Barry::Contact &con, const std::string &val) const;
225 virtual void SetFQDN(Barry::Contact &con, const std::string &val) const;
229 // Name heuristics
232 virtual void ClearHeuristics();
233 virtual bool RunHeuristics(Barry::Contact &con);
236 // Operations
239 void DumpLdif(std::ostream &os, const Barry::Contact &contact) const;
240 bool ReadLdif(std::istream &is, Barry::Contact &contact);
241 // returns true on success
242 void DumpMap(std::ostream &os) const;
245 static std::string MakeLdifData(const std::string &str);
246 static bool NeedsEncoding(const std::string &str);
249 BXEXPORT inline std::ostream& operator<< (std::ostream &os, const ContactLdif &ldif) {
250 ldif.DumpMap(os);
251 return os;
254 } // namespace Barry
256 #endif