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