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