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