00001 /* 00002 * Funambol is a mobile platform developed by Funambol, Inc. 00003 * Copyright (C) 2003 - 2007 Funambol, Inc. 00004 * 00005 * This program is free software; you can redistribute it and/or modify it under 00006 * the terms of the GNU Affero General Public License version 3 as published by 00007 * the Free Software Foundation with the addition of the following permission 00008 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED 00009 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 00010 * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 00011 * 00012 * This program is distributed in the hope that it will be useful, but WITHOUT 00013 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00014 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 00015 * details. 00016 * 00017 * You should have received a copy of the GNU Affero General Public License 00018 * along with this program; if not, see http://www.gnu.org/licenses or write to 00019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00020 * MA 02110-1301 USA. 00021 * 00022 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 00023 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com. 00024 * 00025 * The interactive user interfaces in modified source and object code versions 00026 * of this program must display Appropriate Legal Notices, as required under 00027 * Section 5 of the GNU Affero General Public License version 3. 00028 * 00029 * In accordance with Section 7(b) of the GNU Affero General Public License 00030 * version 3, these Appropriate Legal Notices must retain the display of the 00031 * "Powered by Funambol" logo. If the display of the logo is not reasonably 00032 * feasible for technical reasons, the Appropriate Legal Notices must display 00033 * the words "Powered by Funambol". 00034 */ 00035 00036 #ifndef INCL_SYNC_ITEM 00037 #define INCL_SYNC_ITEM 00038 00040 #include "base/fscapi.h" 00041 #include "base/constants.h" 00042 #include "base/util/ArrayElement.h" 00043 #include "spds/constants.h" 00044 #include "spds/SyncStatus.h" 00045 #include "inputStream/InputStream.h" 00046 00047 #include <string.h> 00048 #include "base/globalsdef.h" 00049 00050 BEGIN_NAMESPACE 00051 00052 typedef enum { 00053 SYNC_STATE_NEW = 'N', 00054 SYNC_STATE_UPDATED = 'U', 00055 SYNC_STATE_DELETED = 'D', 00056 SYNC_STATE_NONE = ' ' 00057 } SyncState; 00058 00059 class SyncItem : public ArrayElement { 00060 00061 protected: 00062 00063 char* data; 00064 char* encoding; 00065 long size; 00066 00067 WCHAR key[DIM_KEY]; 00068 WCHAR type[DIM_MIME_TYPE]; 00069 00070 long lastModificationTime; 00071 SyncState state; 00072 00073 WCHAR* targetParent; 00074 WCHAR* sourceParent; 00075 00076 private: 00080 void initialize(); 00081 00082 00083 protected: 00084 00089 InputStream* inputStream; 00090 00091 00092 public: 00093 /* 00094 * Default constructor 00095 */ 00096 SyncItem(); 00097 00098 virtual ~SyncItem(); 00099 00100 /* 00101 * Constructs a new SyncItem identified by the given key. The key must 00102 * not be longer than DIM_KEY (see SPDS Constants). 00103 * 00104 * @param key - the key 00105 */ 00106 SyncItem(const WCHAR* key); 00107 00108 /* 00109 * Returns the SyncItem's key. If key is NULL, the internal buffer is 00110 * returned; if key is not NULL, the value is copied in the caller 00111 * allocated buffer and the given buffer pointer is returned. 00112 * 00113 * @param key - buffer where the key will be stored 00114 */ 00115 const WCHAR* getKey() const; 00116 00117 /* 00118 * Changes the SyncItem key. The key must not be longer than DIM_KEY 00119 * (see SPDS Constants). 00120 * 00121 * @param key - the key 00122 */ 00123 void setKey(const WCHAR* key); 00124 00125 /* 00126 * Sets the SyncItem modification timestamp. timestamp is a milliseconds 00127 * timestamp since a reference time (which is platform specific). 00128 * 00129 * @param timestamp - last modification timestamp 00130 */ 00131 void setModificationTime(long timestamp); 00132 00133 /* 00134 * Returns the SyncItem modeification timestamp. The returned value 00135 * is a milliseconds timestamp since a reference time (which is 00136 * platform specific). 00137 */ 00138 long getModificationTime() const; 00139 00140 /* 00141 * Sets the SyncItem content data. The passed data is copied into an 00142 * internal buffer so that the caller can release the buffer after 00143 * calling setData(). 00144 * The SyncItem's inputStream is created NEW everytime setData() is called, 00145 * and it's linked to the 'data' buffer (it's a BufferInputStream). 00146 * To read chunked data, just call getInputStream()->read(buffer, size). 00147 * 00148 * Data which is to be sent as it is currently cannot contain nul-bytes 00149 * because it is treated like a C-style string. The size parameter should 00150 * not include the nul-byte which terminates C strings, so 00151 * pass size==0 for an empty string. A nul-byte is always 00152 * appended at the end of the data automatically. 00153 * 00154 * Binary data can be sent if it is encoded during transmission. The client 00155 * can decide which encoding to use like this: 00156 * - setData() with binary data 00157 * - changeEncoding(SyncItem::encodings::...) 00158 * or 00159 * - setData() with data that is already encoded in some way 00160 * - setDataEncoding(<the encoding name>) 00161 * 00162 * If the client neither changes nor sets the encoding, then 00163 * the default encoding specified in the SyncSource's 00164 * configuration is automatically applied by the client 00165 * library. 00166 * 00167 * @param data memory to be copied, may be NULL; in that case an empty buffer is allocated 00168 * @param size length of the given data or, if data is NULL, the desired buffer size 00169 */ 00170 virtual void* setData(const void* data, long size); 00171 00172 /* 00173 * Returns the SyncItem data buffer, in read-write mode. 00174 * 00175 * There is guaranteed to be a nul-byte after the data which 00176 * is not included in the data size. 00177 */ 00178 virtual void* getData() const; 00179 00187 virtual InputStream* getInputStream(); 00188 00189 /* 00190 * Returns the amount of bytes stored in the item, 00191 * excluding the implicit nul-byte after the real data. 00192 */ 00193 virtual long getDataSize() const; 00194 00195 /* 00196 * Sets the SyncItem data size without changing the data buffer. 00197 * 00198 * @param s the new size 00199 */ 00200 virtual void setDataSize(long s); 00201 00213 void setDataEncoding(const char* encoding); 00214 00221 const char* getDataEncoding() const; 00222 00239 virtual int changeDataEncoding(const char* encoding, const char* encryption, const char* credentialInfo = NULL); 00240 00241 /* 00242 * Sets the SyncItem data mime type 00243 * 00244 * @param - type the content mimetype 00245 */ 00246 void setDataType(const WCHAR* type); 00247 00248 /* 00249 * Returns the SyncItem data mime type. 00250 */ 00251 const WCHAR* getDataType() const; 00252 00253 /* 00254 * Sets the SyncItem state 00255 * 00256 * @param state the new SyncItem state 00257 */ 00258 void setState(SyncState newState); 00259 00260 /* 00261 * Gets the SyncItem state 00262 */ 00263 SyncState getState() const; 00264 00270 const WCHAR* getTargetParent() const; 00271 00278 void setTargetParent(const WCHAR* parent); 00279 00285 const WCHAR* getSourceParent() const; 00286 00293 void setSourceParent(const WCHAR* parent); 00294 00300 virtual ArrayElement* clone(); 00301 00305 struct encodings { 00306 static const char* const plain; 00307 static const char* const escaped; 00308 static const char* const des; 00312 static const char* encodingString(const char* encoding) { 00313 return encoding ? encoding : plain; 00314 } 00315 00317 static bool isSupported(const char* encoding) { 00318 const char* enc = encodingString(encoding); 00319 return !strcmp(enc, plain) || 00320 !strcmp(enc, escaped) || 00321 !strcmp(enc, des); 00322 } 00323 }; 00324 00325 private: 00327 int transformData(const char* name, bool encode, const char* password); 00328 }; 00329 00330 00331 END_NAMESPACE 00332 00334 #endif