79158c20a5248eb0c9d07c3108e823bf6e4e1a80
[platform/upstream/syncevolution.git] / src / synthesis / src / sysync / multifielditemtype.h
1 /*
2  *  File:         MultiFieldItemType.h
3  *
4  *  Author:                       Lukas Zeller (luz@synthesis.ch)
5  *
6  *  TMultiFieldItemType
7  *    Type consisting of multiple data fields (TItemField objects)
8  *    To be used as base class for field formats like vCard,
9  *    vCalendar etc.
10  *
11  *  Copyright (c) 2001-2009 by Synthesis AG (www.synthesis.ch)
12  *
13  *  2001-08-13 : luz : created
14  *
15  */
16
17 #ifndef MultiFieldItemType_H
18 #define MultiFieldItemType_H
19
20 // includes
21 #include "syncitemtype.h"
22 #include "multifielditem.h"
23 #include "scriptcontext.h"
24
25
26 using namespace sysync;
27
28
29 namespace sysync {
30
31 // special field IDs
32 #define FID_NOT_SUPPORTED -1 // no field-ID
33
34 // special field offsets
35 #define OFFS_NOSTORE -9999 // offset meaning "do not store"
36
37 // special repeat counts
38 #define REP_REWRITE 0      // unlimited repeating, but later occurrences overwrite previous ones
39 #define REP_ARRAY 32767    // virtually unlimited repeating, should be used with array fields only
40
41 // default profile mode (devivates might define their own)
42 #define PROFILEMODE_DEFAULT 0 // default mode of the profile, usually MIME-DIR
43
44
45
46 // type-instance specific options (there can be multiple
47 // TSyncItemType instances with the same TFieldListConfig,
48 // but different sets of field options, e.g. different
49 // versions of the same content type where one has fewer
50 // fields implemented, or server and client types with
51 // differing sets of available fields)
52 #define FIELD_OPT_MAXSIZE_UNKNOWN -1
53 #define FIELD_OPT_MAXSIZE_NONE 0
54 typedef struct {
55   bool available; // set if field is available in this type
56   sInt32 maxsize; // maximum field length, FIELD_OPT_MAXSIZE_NONE=none, FIELD_OPT_MAXSIZE_UNKNOWN=limited, but unknown
57   bool notruncate; // set if type doesn't want to get truncated values for this field from remote
58   sInt32 maxoccur; // maximum number of repetitions of properties based on this field
59 } TFieldOptions;
60
61
62 // multi-field based datatype
63 class TMultiFieldTypeConfig : public TDataTypeConfig
64 {
65   typedef TDataTypeConfig inherited;
66 public:
67   TMultiFieldTypeConfig(const char *aElementName, TConfigElement *aParentElementP);
68   virtual ~TMultiFieldTypeConfig();
69   // properties
70   // - associated field list config (normally set from derived type)
71   TFieldListConfig *fFieldListP;
72   // - associated profile
73   TProfileConfig *fProfileConfigP;
74   // - mode for profile
75   sInt32 fProfileMode;
76   #ifdef SCRIPT_SUPPORT
77   // - scripts
78   string fInitScript; // executed once per usage by a datastore
79   string fIncomingScript; // script that is executed after receiving item
80   string fOutgoingScript; // script that is executed just before sending item
81   string fFilterInitScript; // script that is executed once per session when filter params are all available. Must return true if something to filter at all
82   string fPostFetchFilterScript; // script that is executed after item is fetched from DB. Must return true if item passes
83   string fCompareScript; // script that is executed to compare items
84   string fMergeScript; // script that is executed to merge items
85   string fProcessItemScript; // script that is executed to decide what to do with an incoming item
86   #endif
87   #ifdef HARDCODED_TYPE_SUPPORT
88   void setConfig(TFieldListConfig *aFieldList, const char *aTypeName, const char* aTypeVers);
89   #endif
90 protected:
91   #ifdef CONFIGURABLE_TYPE_SUPPORT
92   // check config elements
93   virtual bool localStartElement(const char *aElementName, const char **aAttributes, sInt32 aLine);
94   virtual void localResolve(bool aLastPass);
95   #endif
96   virtual void clear();
97 }; // TMultiFieldTypeConfig
98
99
100 class TMultiFieldItemType: public TSyncItemType
101 {
102   typedef TSyncItemType inherited;
103 public:
104   TMultiFieldItemType(
105     TSyncSession *aSessionP,
106     TDataTypeConfig *aTypeConfigP,
107     const char *aCTType,
108     const char *aVerCT,
109     TSyncDataStore *aRelatedDatastoreP,
110     TFieldListConfig *aFieldDefinitions // field definition list
111   );
112   virtual ~TMultiFieldItemType();
113   // access to type
114   virtual uInt16 getTypeID(void) const { return ity_multifield; };
115   virtual bool isBasedOn(uInt16 aItemTypeID) const { return aItemTypeID==ity_multifield ? true : TSyncItemType::isBasedOn(aItemTypeID); };
116   // differentiation between implemented and just descriptive TSyncTypeItems
117   virtual bool isImplemented(void) { return true; }; // MultiFields are implemented
118   // compatibility (=assignment compatibility between items based on these types)
119   virtual bool isCompatibleWith(TSyncItemType *aReferenceType);
120   // test if item could contain cut-off data (e.g. because of field size restrictions)
121   // compared to specified reference item
122   virtual bool mayContainCutOffData(TSyncItemType *aReferenceType);
123   // helper to create same-typed instance via base class
124   // MUST BE IMPLEMENTED IN ALL non-virtual DERIVED CLASSES!
125   virtual TSyncItemType *newCopyForSameType(
126     TSyncSession *aSessionP,     // the session
127     TSyncDataStore *aDatastoreP  // the datastore
128   );
129   /// @brief copy CTCap derived info from another SyncItemType
130   virtual bool copyCTCapInfoFrom(TSyncItemType &aSourceItemP);
131   // apply default limits to type (e.g. from hard-coded template in config)
132   virtual void addDefaultTypeLimits(void);
133   // get config pointer
134   TMultiFieldTypeConfig *getMultifieldTypeConfig(void) { return static_cast<TMultiFieldTypeConfig *>(fTypeConfigP); };
135   // access field definitions
136   TFieldListConfig *getFieldDefinitions(void) { return fFieldDefinitionsP; };
137   // access to fields by name (returns -1 if field not found)
138   sInt16 getFieldIndex(const char *aFieldName);
139   bool isFieldIndexValid(sInt16 aFieldIndex);
140   // access to field options (returns NULL if field not found)
141   TFieldOptions *getFieldOptions(sInt16 aFieldIndex);
142   TFieldOptions *getFieldOptions(const char *aFieldName)
143     { return getFieldOptions(getFieldIndex(aFieldName)); };
144   virtual bool hasReceivedFieldOptions(void) { return false; }; // returns true if field options are based on remote devinf (and not just defaults)
145   // access to field definitions
146   TFieldDefinition *getFieldDefinition(sInt16 aFieldIndex)
147     { if (fFieldDefinitionsP) return &(fFieldDefinitionsP->fFields[aFieldIndex]); else return NULL; }
148   // Prepare datatype for use with a datastore. This might be implemented
149   // in derived classes to initialize the datastore's script context etc.
150   virtual void initDataTypeUse(TLocalEngineDS *aDatastoreP, bool aForSending, bool aForReceiving);
151   #ifdef OBJECT_FILTERING
152   // new style generic filtering
153   // - init new-style filtering, returns flag if needed at all
154   virtual void initPostFetchFiltering(bool &aNeeded, bool &aNeededForAll, TLocalEngineDS *aDatastoreP);
155   // - do the actual filtering
156   bool postFetchFiltering(TMultiFieldItem *aItemP, TLocalEngineDS *aDatastoreP);
157   // old style expression filtering
158   // - get field index of given filter expression identifier. Note that
159   //   derived classes might first check for MIME property names etc.
160   virtual sInt16 getFilterIdentifierFieldIndex(const char *aIdentifier, uInt16 aIndex)
161     { return getFieldIndex(aIdentifier); }; // base class just handles field names
162   #endif
163   // comparing and merging
164   sInt16 compareItems(TMultiFieldItem &aFirstItem, TMultiFieldItem &aSecondItem, TEqualityMode aEqMode, bool aDebugShow, TLocalEngineDS *aDatastoreP);
165   void mergeItems(
166     TMultiFieldItem &aWinningItem,
167     TMultiFieldItem &aLoosingItem,
168     bool &aChangedWinning,
169     bool &aChangedLoosing,
170     TLocalEngineDS *aDatastoreP
171   );
172   // check item before processing it
173   bool checkItem(TMultiFieldItem &aItem, TLocalEngineDS *aDatastoreP);
174 protected:
175   // Item data management
176   // - create new sync item of proper type and optimization for specified target
177   /* MultiFields are only a base class and lack implementation of
178      the following:
179   virtual TSyncItem *internalNewSyncItem(TSyncItemType *aTargetItemTypeP, TLocalEngineDS *aLocalDatastoreP);
180   */
181   // - fill in SyncML data (but leaves IDs empty)
182   //   Note: for MultiFieldItem, this is for post-processing data (FieldFillers)
183   virtual bool internalFillInData(
184     TSyncItem *aSyncItemP,      // SyncItem to be filled with data
185     SmlItemPtr_t aItemP,        // SyncML toolkit item Data to be converted into SyncItem (may be NULL if no data, in case of Delete or Map)
186     TLocalEngineDS *aLocalDatastoreP, // local datastore
187     TStatusCommand &aStatusCmd  // status command that might be modified in case of error
188   );
189   // - sets data and meta from SyncItem data, but leaves source & target untouched
190   //   Note: for MultiFieldItem, this is for pre-processing data (FieldFillers)
191   virtual bool internalSetItemData(
192     TSyncItem *aSyncItemP,  // the syncitem to be represented as SyncML
193     SmlItemPtr_t aItem,     // item with NULL meta and NULL data
194     TLocalEngineDS *aLocalDatastoreP // local datastore
195   );
196   // field definition list pointer
197   TFieldListConfig *fFieldDefinitionsP;
198 public:
199   // temporary variables needed by filter script context funcs
200   bool fNeedToFilterAll; // need to filter all records, even not-changed ones (dynamic syncset)
201   #ifdef SCRIPT_SUPPORT
202   TLocalEngineDS *fDsP; // helper for script context funcs
203   TMultiFieldItem *fFirstItemP; // helper for script context funcs
204   TMultiFieldItem *fSecondItemP; // helper for script context funcs
205   bool fChangedFirst; // helper for script context funcs
206   bool fChangedSecond; // helper for script context funcs
207   TEqualityMode fEqMode; // helper for script context funcs
208   TSyncOperation fCurrentSyncOp; // helper for script context funcs
209   #endif
210 private:
211   // array of field options
212   TFieldOptions *fFieldOptionsP;
213 }; // TMultiFieldItemType
214
215 }       // namespace sysync
216
217 #endif  // MultiFieldItemType_H
218
219 // eof