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