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