1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
19 package com
.owncloud
.android
.datamodel
;
23 import com
.owncloud
.android
.MainApp
;
24 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
26 import third_parties
.daveKoeller
.AlphanumComparator
;
27 import android
.content
.Intent
;
28 import android
.net
.Uri
;
29 import android
.os
.Parcel
;
30 import android
.os
.Parcelable
;
31 import android
.webkit
.MimeTypeMap
;
33 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
35 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
37 public OCFile
createFromParcel(Parcel source
) {
38 return new OCFile(source
);
42 public OCFile
[] newArray(int size
) {
43 return new OCFile
[size
];
47 public static final String PATH_SEPARATOR
= "/";
48 public static final String ROOT_PATH
= PATH_SEPARATOR
;
50 private static final String TAG
= OCFile
.class.getSimpleName();
53 private long mParentId
;
55 private long mCreationTimestamp
;
56 private long mModifiedTimestamp
;
57 private long mModifiedTimestampAtLastSyncForData
;
58 private String mRemotePath
;
59 private String mLocalPath
;
60 private String mMimeType
;
61 private boolean mNeedsUpdating
;
62 private long mLastSyncDateForProperties
;
63 private long mLastSyncDateForData
;
64 private boolean mKeepInSync
;
68 private boolean mShareByLink
;
69 private String mPublicLink
;
71 private String mPermissions
;
72 private String mRemoteId
;
74 private boolean mNeedsUpdateThumbnail
;
78 * Create new {@link OCFile} with given path.
80 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
82 * @param path The remote path of the file.
84 public OCFile(String path
) {
86 mNeedsUpdating
= false
;
87 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
88 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
94 * Reconstruct from parcel
96 * @param source The source parcel
98 private OCFile(Parcel source
) {
99 mId
= source
.readLong();
100 mParentId
= source
.readLong();
101 mLength
= source
.readLong();
102 mCreationTimestamp
= source
.readLong();
103 mModifiedTimestamp
= source
.readLong();
104 mModifiedTimestampAtLastSyncForData
= source
.readLong();
105 mRemotePath
= source
.readString();
106 mLocalPath
= source
.readString();
107 mMimeType
= source
.readString();
108 mNeedsUpdating
= source
.readInt() == 0;
109 mKeepInSync
= source
.readInt() == 1;
110 mLastSyncDateForProperties
= source
.readLong();
111 mLastSyncDateForData
= source
.readLong();
112 mEtag
= source
.readString();
113 mShareByLink
= source
.readInt() == 1;
114 mPublicLink
= source
.readString();
115 mPermissions
= source
.readString();
116 mRemoteId
= source
.readString();
117 mNeedsUpdateThumbnail
= source
.readInt() == 0;
122 public void writeToParcel(Parcel dest
, int flags
) {
124 dest
.writeLong(mParentId
);
125 dest
.writeLong(mLength
);
126 dest
.writeLong(mCreationTimestamp
);
127 dest
.writeLong(mModifiedTimestamp
);
128 dest
.writeLong(mModifiedTimestampAtLastSyncForData
);
129 dest
.writeString(mRemotePath
);
130 dest
.writeString(mLocalPath
);
131 dest
.writeString(mMimeType
);
132 dest
.writeInt(mNeedsUpdating ?
1 : 0);
133 dest
.writeInt(mKeepInSync ?
1 : 0);
134 dest
.writeLong(mLastSyncDateForProperties
);
135 dest
.writeLong(mLastSyncDateForData
);
136 dest
.writeString(mEtag
);
137 dest
.writeInt(mShareByLink ?
1 : 0);
138 dest
.writeString(mPublicLink
);
139 dest
.writeString(mPermissions
);
140 dest
.writeString(mRemoteId
);
141 dest
.writeInt(mNeedsUpdateThumbnail ?
1 : 0);
145 * Gets the ID of the file
147 * @return the file ID
149 public long getFileId() {
154 * Returns the remote path of the file on ownCloud
156 * @return The remote path to the file
158 public String
getRemotePath() {
163 * Can be used to check, whether or not this file exists in the database
166 * @return true, if the file exists in the database
168 public boolean fileExists() {
173 * Use this to find out if this file is a folder.
175 * @return true if it is a folder
177 public boolean isFolder() {
178 return mMimeType
!= null
&& mMimeType
.equals("DIR");
182 * Use this to check if this file is available locally
184 * @return true if it is
186 public boolean isDown() {
187 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
188 File file
= new File(mLocalPath
);
189 return (file
.exists());
195 * The path, where the file is stored locally
197 * @return The local path to the file
199 public String
getStoragePath() {
204 * Can be used to set the path where the file is stored
206 * @param storage_path to set
208 public void setStoragePath(String storage_path
) {
209 mLocalPath
= storage_path
;
213 * Get a UNIX timestamp of the file creation time
215 * @return A UNIX timestamp of the time that file was created
217 public long getCreationTimestamp() {
218 return mCreationTimestamp
;
222 * Set a UNIX timestamp of the time the file was created
224 * @param creation_timestamp to set
226 public void setCreationTimestamp(long creation_timestamp
) {
227 mCreationTimestamp
= creation_timestamp
;
231 * Get a UNIX timestamp of the file modification time.
233 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
234 * in the last synchronization of the properties of this file.
236 public long getModificationTimestamp() {
237 return mModifiedTimestamp
;
241 * Set a UNIX timestamp of the time the time the file was modified.
243 * To update with the value returned by the server in every synchronization of the properties
246 * @param modification_timestamp to set
248 public void setModificationTimestamp(long modification_timestamp
) {
249 mModifiedTimestamp
= modification_timestamp
;
254 * Get a UNIX timestamp of the file modification time.
256 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
257 * in the last synchronization of THE CONTENTS of this file.
259 public long getModificationTimestampAtLastSyncForData() {
260 return mModifiedTimestampAtLastSyncForData
;
264 * Set a UNIX timestamp of the time the time the file was modified.
266 * To update with the value returned by the server in every synchronization of THE CONTENTS
269 * @param modification_timestamp to set
271 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
272 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
278 * Returns the filename and "/" for the root directory
280 * @return The name of the file
282 public String
getFileName() {
283 File f
= new File(getRemotePath());
284 return f
.getName().length() == 0 ? ROOT_PATH
: f
.getName();
288 * Sets the name of the file
290 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
292 public void setFileName(String name
) {
293 Log_OC
.d(TAG
, "OCFile name changin from " + mRemotePath
);
294 if (name
!= null
&& name
.length() > 0 && !name
.contains(PATH_SEPARATOR
) && !mRemotePath
.equals(ROOT_PATH
)) {
295 String parent
= (new File(getRemotePath())).getParent();
296 parent
= (parent
.endsWith(PATH_SEPARATOR
)) ? parent
: parent
+ PATH_SEPARATOR
;
297 mRemotePath
= parent
+ name
;
299 mRemotePath
+= PATH_SEPARATOR
;
301 Log_OC
.d(TAG
, "OCFile name changed to " + mRemotePath
);
303 // Notify MediaScanner about removed file
304 Intent intent1
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
305 intent1
.setData(Uri
.fromFile(new File(this.getStoragePath())));
306 MainApp
.getAppContext().sendBroadcast(intent1
);
308 // Notify MediaScanner about new file
309 Intent intent2
= new Intent(Intent
.ACTION_MEDIA_SCANNER_SCAN_FILE
);
310 String folder
= new File(this.getStoragePath()).getParent();
311 Log_OC
.d(TAG
, "uri: " + folder
+PATH_SEPARATOR
+name
);
312 intent2
.setData(Uri
.fromFile(new File(folder
+ PATH_SEPARATOR
+name
)));
313 MainApp
.getAppContext().sendBroadcast(intent2
);
318 * Can be used to get the Mimetype
320 * @return the Mimetype as a String
322 public String
getMimetype() {
327 * Adds a file to this directory. If this file is not a directory, an
328 * exception gets thrown.
331 * @throws IllegalStateException if you try to add a something and this is
334 public void addFile(OCFile file
) throws IllegalStateException
{
336 file
.mParentId
= mId
;
337 mNeedsUpdating
= true
;
340 throw new IllegalStateException(
341 "This is not a directory where you can add stuff to!");
345 * Used internally. Reset all file properties
347 private void resetData() {
354 mCreationTimestamp
= 0;
355 mModifiedTimestamp
= 0;
356 mModifiedTimestampAtLastSyncForData
= 0;
357 mLastSyncDateForProperties
= 0;
358 mLastSyncDateForData
= 0;
360 mNeedsUpdating
= false
;
362 mShareByLink
= false
;
366 mNeedsUpdateThumbnail
= false
;
370 * Sets the ID of the file
372 * @param file_id to set
374 public void setFileId(long file_id
) {
379 * Sets the Mime-Type of the
381 * @param mimetype to set
383 public void setMimetype(String mimetype
) {
384 mMimeType
= mimetype
;
388 * Sets the ID of the parent folder
390 * @param parent_id to set
392 public void setParentId(long parent_id
) {
393 mParentId
= parent_id
;
397 * Sets the file size in bytes
399 * @param file_len to set
401 public void setFileLength(long file_len
) {
406 * Returns the size of the file in bytes
408 * @return The filesize in bytes
410 public long getFileLength() {
415 * Returns the ID of the parent Folder
419 public long getParentId() {
424 * Check, if this file needs updating
428 public boolean needsUpdatingWhileSaving() {
429 return mNeedsUpdating
;
432 public boolean needsUpdateThumbnail() {
433 return mNeedsUpdateThumbnail
;
436 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail
) {
437 this.mNeedsUpdateThumbnail
= needsUpdateThumbnail
;
440 public long getLastSyncDateForProperties() {
441 return mLastSyncDateForProperties
;
444 public void setLastSyncDateForProperties(long lastSyncDate
) {
445 mLastSyncDateForProperties
= lastSyncDate
;
448 public long getLastSyncDateForData() {
449 return mLastSyncDateForData
;
452 public void setLastSyncDateForData(long lastSyncDate
) {
453 mLastSyncDateForData
= lastSyncDate
;
456 public void setKeepInSync(boolean keepInSync
) {
457 mKeepInSync
= keepInSync
;
460 public boolean keepInSync() {
465 public int describeContents() {
466 return this.hashCode();
470 public int compareTo(OCFile another
) {
471 if (isFolder() && another
.isFolder()) {
472 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
473 } else if (isFolder()) {
475 } else if (another
.isFolder()) {
478 return new AlphanumComparator().compare(this, another
);
482 public boolean equals(Object o
) {
483 if(o
instanceof OCFile
){
484 OCFile that
= (OCFile
) o
;
486 return this.mId
== that
.mId
;
494 public String
toString() {
495 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]";
496 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
);
500 public String
getEtag() {
504 public void setEtag(String etag
) {
509 public boolean isShareByLink() {
513 public void setShareByLink(boolean shareByLink
) {
514 this.mShareByLink
= shareByLink
;
517 public String
getPublicLink() {
521 public void setPublicLink(String publicLink
) {
522 this.mPublicLink
= publicLink
;
525 public long getLocalModificationTimestamp() {
526 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
527 File f
= new File(mLocalPath
);
528 return f
.lastModified();
533 /** @return 'True' if the file contains audio */
534 public boolean isAudio() {
535 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
538 /** @return 'True' if the file contains video */
539 public boolean isVideo() {
540 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
543 /** @return 'True' if the file contains an image */
544 public boolean isImage() {
545 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
546 getMimeTypeFromName().startsWith("image/"));
549 public String
getMimeTypeFromName() {
550 String extension
= "";
551 int pos
= mRemotePath
.lastIndexOf('.');
553 extension
= mRemotePath
.substring(pos
+ 1);
555 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
556 return (result
!= null
) ? result
: "";
559 public String
getPermissions() {
563 public void setPermissions(String permissions
) {
564 this.mPermissions
= permissions
;
567 public String
getRemoteId() {
571 public void setRemoteId(String remoteId
) {
572 this.mRemoteId
= remoteId
;