668e9917a8db9197095f426a72137d927b3cd2bd
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / OCFile.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package com.owncloud.android.datamodel;
21
22 import java.io.File;
23
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 import android.util.Log;
27
28 public class OCFile implements Parcelable, Comparable<OCFile> {
29
30 public static final Parcelable.Creator<OCFile> CREATOR = new Parcelable.Creator<OCFile>() {
31 @Override
32 public OCFile createFromParcel(Parcel source) {
33 return new OCFile(source);
34 }
35
36 @Override
37 public OCFile[] newArray(int size) {
38 return new OCFile[size];
39 }
40 };
41
42 public static final String PATH_SEPARATOR = "/";
43
44 private static final String TAG = OCFile.class.getSimpleName();
45
46 private long mId;
47 private long mParentId;
48 private long mLength;
49 private long mCreationTimestamp;
50 private long mModifiedTimestamp;
51 private long mModifiedTimestampAtLastSyncForData;
52 private String mRemotePath;
53 private String mLocalPath;
54 private String mMimeType;
55 private boolean mNeedsUpdating;
56 private long mLastSyncDateForProperties;
57 private long mLastSyncDateForData;
58 private boolean mKeepInSync;
59
60 private String mEtag;
61
62 /**
63 * Create new {@link OCFile} with given path.
64 *
65 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
66 *
67 * @param path The remote path of the file.
68 */
69 public OCFile(String path) {
70 resetData();
71 mNeedsUpdating = false;
72 if (path == null || path.length() <= 0 || !path.startsWith(PATH_SEPARATOR)) {
73 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
74 }
75 mRemotePath = path;
76 }
77
78 /**
79 * Reconstruct from parcel
80 *
81 * @param source The source parcel
82 */
83 private OCFile(Parcel source) {
84 mId = source.readLong();
85 mParentId = source.readLong();
86 mLength = source.readLong();
87 mCreationTimestamp = source.readLong();
88 mModifiedTimestamp = source.readLong();
89 mModifiedTimestampAtLastSyncForData = source.readLong();
90 mRemotePath = source.readString();
91 mLocalPath = source.readString();
92 mMimeType = source.readString();
93 mNeedsUpdating = source.readInt() == 0;
94 mKeepInSync = source.readInt() == 1;
95 mLastSyncDateForProperties = source.readLong();
96 mLastSyncDateForData = source.readLong();
97 }
98
99 @Override
100 public void writeToParcel(Parcel dest, int flags) {
101 dest.writeLong(mId);
102 dest.writeLong(mParentId);
103 dest.writeLong(mLength);
104 dest.writeLong(mCreationTimestamp);
105 dest.writeLong(mModifiedTimestamp);
106 dest.writeLong(mModifiedTimestampAtLastSyncForData);
107 dest.writeString(mRemotePath);
108 dest.writeString(mLocalPath);
109 dest.writeString(mMimeType);
110 dest.writeInt(mNeedsUpdating ? 1 : 0);
111 dest.writeInt(mKeepInSync ? 1 : 0);
112 dest.writeLong(mLastSyncDateForProperties);
113 dest.writeLong(mLastSyncDateForData);
114 }
115
116 /**
117 * Gets the ID of the file
118 *
119 * @return the file ID
120 */
121 public long getFileId() {
122 return mId;
123 }
124
125 /**
126 * Returns the remote path of the file on ownCloud
127 *
128 * @return The remote path to the file
129 */
130 public String getRemotePath() {
131 return mRemotePath;
132 }
133
134 /**
135 * Can be used to check, whether or not this file exists in the database
136 * already
137 *
138 * @return true, if the file exists in the database
139 */
140 public boolean fileExists() {
141 return mId != -1;
142 }
143
144 /**
145 * Use this to find out if this file is a Directory
146 *
147 * @return true if it is a directory
148 */
149 public boolean isDirectory() {
150 return mMimeType != null && mMimeType.equals("DIR");
151 }
152
153 /**
154 * Use this to check if this file is available locally
155 *
156 * @return true if it is
157 */
158 public boolean isDown() {
159 if (mLocalPath != null && mLocalPath.length() > 0) {
160 File file = new File(mLocalPath);
161 return (file.exists());
162 }
163 return false;
164 }
165
166 /**
167 * The path, where the file is stored locally
168 *
169 * @return The local path to the file
170 */
171 public String getStoragePath() {
172 return mLocalPath;
173 }
174
175 /**
176 * Can be used to set the path where the file is stored
177 *
178 * @param storage_path to set
179 */
180 public void setStoragePath(String storage_path) {
181 mLocalPath = storage_path;
182 }
183
184 /**
185 * Get a UNIX timestamp of the file creation time
186 *
187 * @return A UNIX timestamp of the time that file was created
188 */
189 public long getCreationTimestamp() {
190 return mCreationTimestamp;
191 }
192
193 /**
194 * Set a UNIX timestamp of the time the file was created
195 *
196 * @param creation_timestamp to set
197 */
198 public void setCreationTimestamp(long creation_timestamp) {
199 mCreationTimestamp = creation_timestamp;
200 }
201
202 /**
203 * Get a UNIX timestamp of the file modification time.
204 *
205 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
206 * in the last synchronization of the properties of this file.
207 */
208 public long getModificationTimestamp() {
209 return mModifiedTimestamp;
210 }
211
212 /**
213 * Set a UNIX timestamp of the time the time the file was modified.
214 *
215 * To update with the value returned by the server in every synchronization of the properties
216 * of this file.
217 *
218 * @param modification_timestamp to set
219 */
220 public void setModificationTimestamp(long modification_timestamp) {
221 mModifiedTimestamp = modification_timestamp;
222 }
223
224
225 /**
226 * Get a UNIX timestamp of the file modification time.
227 *
228 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
229 * in the last synchronization of THE CONTENTS of this file.
230 */
231 public long getModificationTimestampAtLastSyncForData() {
232 return mModifiedTimestampAtLastSyncForData;
233 }
234
235 /**
236 * Set a UNIX timestamp of the time the time the file was modified.
237 *
238 * To update with the value returned by the server in every synchronization of THE CONTENTS
239 * of this file.
240 *
241 * @param modification_timestamp to set
242 */
243 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp) {
244 mModifiedTimestampAtLastSyncForData = modificationTimestamp;
245 }
246
247
248
249 /**
250 * Returns the filename and "/" for the root directory
251 *
252 * @return The name of the file
253 */
254 public String getFileName() {
255 File f = new File(getRemotePath());
256 return f.getName().length() == 0 ? PATH_SEPARATOR : f.getName();
257 }
258
259 /**
260 * Sets the name of the file
261 *
262 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
263 */
264 public void setFileName(String name) {
265 Log.d(TAG, "OCFile name changin from " + mRemotePath);
266 if (name != null && name.length() > 0 && !name.contains(PATH_SEPARATOR) && !mRemotePath.equals(PATH_SEPARATOR)) {
267 String parent = (new File(getRemotePath())).getParent();
268 parent = (parent.endsWith(PATH_SEPARATOR)) ? parent : parent + PATH_SEPARATOR;
269 mRemotePath = parent + name;
270 if (isDirectory()) {
271 mRemotePath += PATH_SEPARATOR;
272 }
273 Log.d(TAG, "OCFile name changed to " + mRemotePath);
274 }
275 }
276
277 /**
278 * Can be used to get the Mimetype
279 *
280 * @return the Mimetype as a String
281 */
282 public String getMimetype() {
283 return mMimeType;
284 }
285
286 /**
287 * Adds a file to this directory. If this file is not a directory, an
288 * exception gets thrown.
289 *
290 * @param file to add
291 * @throws IllegalStateException if you try to add a something and this is
292 * not a directory
293 */
294 public void addFile(OCFile file) throws IllegalStateException {
295 if (isDirectory()) {
296 file.mParentId = mId;
297 mNeedsUpdating = true;
298 return;
299 }
300 throw new IllegalStateException(
301 "This is not a directory where you can add stuff to!");
302 }
303
304 /**
305 * Used internally. Reset all file properties
306 */
307 private void resetData() {
308 mId = -1;
309 mRemotePath = null;
310 mParentId = 0;
311 mLocalPath = null;
312 mMimeType = null;
313 mLength = 0;
314 mCreationTimestamp = 0;
315 mModifiedTimestamp = 0;
316 mModifiedTimestampAtLastSyncForData = 0;
317 mLastSyncDateForProperties = 0;
318 mLastSyncDateForData = 0;
319 mKeepInSync = false;
320 mNeedsUpdating = false;
321 }
322
323 /**
324 * Sets the ID of the file
325 *
326 * @param file_id to set
327 */
328 public void setFileId(long file_id) {
329 mId = file_id;
330 }
331
332 /**
333 * Sets the Mime-Type of the
334 *
335 * @param mimetype to set
336 */
337 public void setMimetype(String mimetype) {
338 mMimeType = mimetype;
339 }
340
341 /**
342 * Sets the ID of the parent folder
343 *
344 * @param parent_id to set
345 */
346 public void setParentId(long parent_id) {
347 mParentId = parent_id;
348 }
349
350 /**
351 * Sets the file size in bytes
352 *
353 * @param file_len to set
354 */
355 public void setFileLength(long file_len) {
356 mLength = file_len;
357 }
358
359 /**
360 * Returns the size of the file in bytes
361 *
362 * @return The filesize in bytes
363 */
364 public long getFileLength() {
365 return mLength;
366 }
367
368 /**
369 * Returns the ID of the parent Folder
370 *
371 * @return The ID
372 */
373 public long getParentId() {
374 return mParentId;
375 }
376
377 /**
378 * Check, if this file needs updating
379 *
380 * @return
381 */
382 public boolean needsUpdatingWhileSaving() {
383 return mNeedsUpdating;
384 }
385
386 public long getLastSyncDateForProperties() {
387 return mLastSyncDateForProperties;
388 }
389
390 public void setLastSyncDateForProperties(long lastSyncDate) {
391 mLastSyncDateForProperties = lastSyncDate;
392 }
393
394 public long getLastSyncDateForData() {
395 return mLastSyncDateForData;
396 }
397
398 public void setLastSyncDateForData(long lastSyncDate) {
399 mLastSyncDateForData = lastSyncDate;
400 }
401
402 public void setKeepInSync(boolean keepInSync) {
403 mKeepInSync = keepInSync;
404 }
405
406 public boolean keepInSync() {
407 return mKeepInSync;
408 }
409
410 @Override
411 public int describeContents() {
412 return this.hashCode();
413 }
414
415 @Override
416 public int compareTo(OCFile another) {
417 if (isDirectory() && another.isDirectory()) {
418 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
419 } else if (isDirectory()) {
420 return -1;
421 } else if (another.isDirectory()) {
422 return 1;
423 }
424 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
425 }
426
427 @Override
428 public boolean equals(Object o) {
429 if(o instanceof OCFile){
430 OCFile that = (OCFile) o;
431 if(that != null){
432 return this.mId == that.mId;
433 }
434 }
435
436 return false;
437 }
438
439 @Override
440 public String toString() {
441 String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
442 asString = String.format(asString, Long.valueOf(mId), getFileName(), mMimeType, isDown(), mLocalPath, mRemotePath, Long.valueOf(mParentId), Boolean.valueOf(mKeepInSync));
443 return asString;
444 }
445
446 public String getEtag() {
447 return mEtag;
448 }
449
450 public long getLocalModificationTimestamp() {
451 if (mLocalPath != null && mLocalPath.length() > 0) {
452 File f = new File(mLocalPath);
453 return f.lastModified();
454 }
455 return 0;
456 }
457
458 }