00001 /* 00002 * Copyright (C) 2003-2007 Funambol, Inc 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License version 2 as 00006 * published by the Free Software Foundation. 00007 * 00008 * This program is distributed in the hope that it will be useful, 00009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR 00011 * PURPOSE. See the GNU General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU General Public License 00014 * along with this program; if not, write to the Free Software 00015 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 00016 * 02111-1307 USA 00017 */ 00018 00019 #ifndef INCL_SYNC_ITEM 00020 #define INCL_SYNC_ITEM 00021 00023 #include "base/fscapi.h" 00024 #include "base/constants.h" 00025 #include "base/util/ArrayElement.h" 00026 #include "spds/constants.h" 00027 #include "spds/SyncStatus.h" 00028 00029 #include <string.h> 00030 00031 typedef enum { 00032 SYNC_STATE_NEW = 'N', 00033 SYNC_STATE_UPDATED = 'U', 00034 SYNC_STATE_DELETED = 'D', 00035 SYNC_STATE_NONE = ' ' 00036 } SyncState; 00037 00038 class SyncItem : public ArrayElement { 00039 00040 private: 00041 00042 char* data; 00043 char* encoding; 00044 long size; 00045 00046 WCHAR key[DIM_KEY]; 00047 WCHAR type[DIM_MIME_TYPE]; 00048 00049 long lastModificationTime; 00050 SyncState state; 00051 00052 WCHAR* targetParent; 00053 WCHAR* sourceParent; 00054 00058 void initialize(); 00059 00060 public: 00061 /* 00062 * Default constructor 00063 */ 00064 SyncItem(); 00065 00066 ~SyncItem(); 00067 00068 /* 00069 * Constructs a new SyncItem identified by the given key. The key must 00070 * not be longer than DIM_KEY (see SPDS Constants). 00071 * 00072 * @param key - the key 00073 */ 00074 SyncItem(const WCHAR* key); 00075 00076 /* 00077 * Returns the SyncItem's key. If key is NULL, the internal buffer is 00078 * returned; if key is not NULL, the value is copied in the caller 00079 * allocated buffer and the given buffer pointer is returned. 00080 * 00081 * @param key - buffer where the key will be stored 00082 */ 00083 const WCHAR* getKey(); 00084 00085 /* 00086 * Changes the SyncItem key. The key must not be longer than DIM_KEY 00087 * (see SPDS Constants). 00088 * 00089 * @param key - the key 00090 */ 00091 void setKey(const WCHAR* key); 00092 00093 /* 00094 * Sets the SyncItem modification timestamp. timestamp is a milliseconds 00095 * timestamp since a reference time (which is platform specific). 00096 * 00097 * @param timestamp - last modification timestamp 00098 */ 00099 void setModificationTime(long timestamp); 00100 00101 /* 00102 * Returns the SyncItem modeification timestamp. The returned value 00103 * is a milliseconds timestamp since a reference time (which is 00104 * platform specific). 00105 */ 00106 long getModificationTime(); 00107 00108 /* 00109 * Sets the SyncItem content data. The passed data is copied into an 00110 * internal buffer so that the caller can release the buffer after 00111 * calling setData(). 00112 * 00113 * Data which is to be sent as it is currently cannot contain nul-bytes 00114 * because it is treated like a C-style string. The size parameter should 00115 * not include the nul-byte which terminates C strings, so 00116 * pass size==0 for an empty string. A nul-byte is always 00117 * appended at the end of the data automatically. 00118 * 00119 * Binary data can be sent if it is encoded during transmission. The client 00120 * can decide which encoding to use like this: 00121 * - setData() with binary data 00122 * - changeEncoding(SyncItem::encodings::...) 00123 * or 00124 * - setData() with data that is already encoded in some way 00125 * - setDataEncoding(<the encoding name>) 00126 * 00127 * If the client neither changes nor sets the encoding, then 00128 * the default encoding specified in the SyncSource's 00129 * configuration is automatically applied by the client 00130 * library. 00131 * 00132 * @param data memory to be copied, may be NULL; in that case an empty buffer is allocated 00133 * @param size length of the given data or, if data is NULL, the desired buffer size 00134 */ 00135 void* setData(const void* data, long size); 00136 00137 /* 00138 * Returns the SyncItem data buffer, in read-write mode. 00139 * 00140 * There is guaranteed to be a nul-byte after the data which 00141 * is not included in the data size. 00142 */ 00143 void* getData(); 00144 00145 /* 00146 * Returns the amount of bytes stored in the item, 00147 * excluding the implicit nul-byte after the real data. 00148 */ 00149 long getDataSize(); 00150 00151 /* 00152 * Sets the SyncItem data size without changing the data buffer. 00153 * 00154 * @param s the new size 00155 */ 00156 void setDataSize(long s); 00157 00169 void setDataEncoding(const char* encoding); 00170 00177 const char* getDataEncoding(); 00178 00195 int changeDataEncoding(const char* encoding, const char* encryption, const char* credentialInfo = NULL); 00196 00197 /* 00198 * Sets the SyncItem data mime type 00199 * 00200 * @param - type the content mimetype 00201 */ 00202 void setDataType(const WCHAR* type); 00203 00204 /* 00205 * Returns the SyncItem data mime type. 00206 */ 00207 const WCHAR* getDataType(); 00208 00209 /* 00210 * Sets the SyncItem state 00211 * 00212 * @param state the new SyncItem state 00213 */ 00214 void setState(SyncState newState); 00215 00216 /* 00217 * Gets the SyncItem state 00218 */ 00219 SyncState getState(); 00220 00226 const WCHAR* getTargetParent(); 00227 00234 void setTargetParent(const WCHAR* parent); 00235 00241 const WCHAR* getSourceParent(); 00242 00249 void setSourceParent(const WCHAR* parent); 00250 00256 ArrayElement* clone(); 00257 00261 struct encodings { 00262 static const char* const plain; 00263 static const char* const escaped; 00264 static const char* const des; 00268 static const char* encodingString(const char* encoding) { 00269 return encoding ? encoding : plain; 00270 } 00271 00273 static const BOOL isSupported(const char* encoding) { 00274 const char* enc = encodingString(encoding); 00275 return !strcmp(enc, plain) || 00276 !strcmp(enc, escaped) || 00277 !strcmp(enc, des); 00278 } 00279 }; 00280 00281 private: 00283 int transformData(const char* name, BOOL encode, const char* password); 00284 }; 00285 00287 #endif