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 private: 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 00079 void initialize(); 00080 00081 00082 protected: 00083 00088 InputStream* inputStream; 00089 00090 00091 public: 00092 /* 00093 * Default constructor 00094 */ 00095 SyncItem(); 00096 00097 virtual ~SyncItem(); 00098 00099 /* 00100 * Constructs a new SyncItem identified by the given key. The key must 00101 * not be longer than DIM_KEY (see SPDS Constants). 00102 * 00103 * @param key - the key 00104 */ 00105 SyncItem(const WCHAR* key); 00106 00107 /* 00108 * Returns the SyncItem's key. If key is NULL, the internal buffer is 00109 * returned; if key is not NULL, the value is copied in the caller 00110 * allocated buffer and the given buffer pointer is returned. 00111 * 00112 * @param key - buffer where the key will be stored 00113 */ 00114 const WCHAR* getKey() const; 00115 00116 /* 00117 * Changes the SyncItem key. The key must not be longer than DIM_KEY 00118 * (see SPDS Constants). 00119 * 00120 * @param key - the key 00121 */ 00122 void setKey(const WCHAR* key); 00123 00124 /* 00125 * Sets the SyncItem modification timestamp. timestamp is a milliseconds 00126 * timestamp since a reference time (which is platform specific). 00127 * 00128 * @param timestamp - last modification timestamp 00129 */ 00130 void setModificationTime(long timestamp); 00131 00132 /* 00133 * Returns the SyncItem modeification timestamp. The returned value 00134 * is a milliseconds timestamp since a reference time (which is 00135 * platform specific). 00136 */ 00137 long getModificationTime() const; 00138 00139 /* 00140 * Sets the SyncItem content data. The passed data is copied into an 00141 * internal buffer so that the caller can release the buffer after 00142 * calling setData(). 00143 * The SyncItem's inputStream is created NEW everytime setData() is called, 00144 * and it's linked to the 'data' buffer (it's a BufferInputStream). 00145 * To read chunked data, just call getInputStream()->read(buffer, size). 00146 * 00147 * Data which is to be sent as it is currently cannot contain nul-bytes 00148 * because it is treated like a C-style string. The size parameter should 00149 * not include the nul-byte which terminates C strings, so 00150 * pass size==0 for an empty string. A nul-byte is always 00151 * appended at the end of the data automatically. 00152 * 00153 * Binary data can be sent if it is encoded during transmission. The client 00154 * can decide which encoding to use like this: 00155 * - setData() with binary data 00156 * - changeEncoding(SyncItem::encodings::...) 00157 * or 00158 * - setData() with data that is already encoded in some way 00159 * - setDataEncoding(<the encoding name>) 00160 * 00161 * If the client neither changes nor sets the encoding, then 00162 * the default encoding specified in the SyncSource's 00163 * configuration is automatically applied by the client 00164 * library. 00165 * 00166 * @param data memory to be copied, may be NULL; in that case an empty buffer is allocated 00167 * @param size length of the given data or, if data is NULL, the desired buffer size 00168 */ 00169 virtual void* setData(const void* data, long size); 00170 00171 /* 00172 * Returns the SyncItem data buffer, in read-write mode. 00173 * 00174 * There is guaranteed to be a nul-byte after the data which 00175 * is not included in the data size. 00176 */ 00177 virtual void* getData() const; 00178 00186 InputStream* getInputStream(); 00187 00188 /* 00189 * Returns the amount of bytes stored in the item, 00190 * excluding the implicit nul-byte after the real data. 00191 */ 00192 virtual long getDataSize() const; 00193 00194 /* 00195 * Sets the SyncItem data size without changing the data buffer. 00196 * 00197 * @param s the new size 00198 */ 00199 virtual void setDataSize(long s); 00200 00212 void setDataEncoding(const char* encoding); 00213 00220 const char* getDataEncoding() const; 00221 00238 virtual int changeDataEncoding(const char* encoding, const char* encryption, const char* credentialInfo = NULL); 00239 00240 /* 00241 * Sets the SyncItem data mime type 00242 * 00243 * @param - type the content mimetype 00244 */ 00245 void setDataType(const WCHAR* type); 00246 00247 /* 00248 * Returns the SyncItem data mime type. 00249 */ 00250 const WCHAR* getDataType() const; 00251 00252 /* 00253 * Sets the SyncItem state 00254 * 00255 * @param state the new SyncItem state 00256 */ 00257 void setState(SyncState newState); 00258 00259 /* 00260 * Gets the SyncItem state 00261 */ 00262 SyncState getState() const; 00263 00269 const WCHAR* getTargetParent() const; 00270 00277 void setTargetParent(const WCHAR* parent); 00278 00284 const WCHAR* getSourceParent() const; 00285 00292 void setSourceParent(const WCHAR* parent); 00293 00299 virtual ArrayElement* clone(); 00300 00304 struct encodings { 00305 static const char* const plain; 00306 static const char* const escaped; 00307 static const char* const des; 00311 static const char* encodingString(const char* encoding) { 00312 return encoding ? encoding : plain; 00313 } 00314 00316 static const bool isSupported(const char* encoding) { 00317 const char* enc = encodingString(encoding); 00318 return !strcmp(enc, plain) || 00319 !strcmp(enc, escaped) || 00320 !strcmp(enc, des); 00321 } 00322 }; 00323 00324 private: 00326 int transformData(const char* name, bool encode, const char* password); 00327 }; 00328 00329 00330 END_NAMESPACE 00331 00333 #endif