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 intent2
.setData(Uri
.fromFile(new File(folder
+ PATH_SEPARATOR
+name
)));
312 MainApp
.getAppContext().sendBroadcast(intent2
);
317 * Can be used to get the Mimetype
319 * @return the Mimetype as a String
321 public String
getMimetype() {
326 * Adds a file to this directory. If this file is not a directory, an
327 * exception gets thrown.
330 * @throws IllegalStateException if you try to add a something and this is
333 public void addFile(OCFile file
) throws IllegalStateException
{
335 file
.mParentId
= mId
;
336 mNeedsUpdating
= true
;
339 throw new IllegalStateException(
340 "This is not a directory where you can add stuff to!");
344 * Used internally. Reset all file properties
346 private void resetData() {
353 mCreationTimestamp
= 0;
354 mModifiedTimestamp
= 0;
355 mModifiedTimestampAtLastSyncForData
= 0;
356 mLastSyncDateForProperties
= 0;
357 mLastSyncDateForData
= 0;
359 mNeedsUpdating
= false
;
361 mShareByLink
= false
;
365 mNeedsUpdateThumbnail
= false
;
369 * Sets the ID of the file
371 * @param file_id to set
373 public void setFileId(long file_id
) {
378 * Sets the Mime-Type of the
380 * @param mimetype to set
382 public void setMimetype(String mimetype
) {
383 mMimeType
= mimetype
;
387 * Sets the ID of the parent folder
389 * @param parent_id to set
391 public void setParentId(long parent_id
) {
392 mParentId
= parent_id
;
396 * Sets the file size in bytes
398 * @param file_len to set
400 public void setFileLength(long file_len
) {
405 * Returns the size of the file in bytes
407 * @return The filesize in bytes
409 public long getFileLength() {
414 * Returns the ID of the parent Folder
418 public long getParentId() {
423 * Check, if this file needs updating
427 public boolean needsUpdatingWhileSaving() {
428 return mNeedsUpdating
;
431 public boolean needsUpdateThumbnail() {
432 return mNeedsUpdateThumbnail
;
435 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail
) {
436 this.mNeedsUpdateThumbnail
= needsUpdateThumbnail
;
439 public long getLastSyncDateForProperties() {
440 return mLastSyncDateForProperties
;
443 public void setLastSyncDateForProperties(long lastSyncDate
) {
444 mLastSyncDateForProperties
= lastSyncDate
;
447 public long getLastSyncDateForData() {
448 return mLastSyncDateForData
;
451 public void setLastSyncDateForData(long lastSyncDate
) {
452 mLastSyncDateForData
= lastSyncDate
;
455 public void setKeepInSync(boolean keepInSync
) {
456 mKeepInSync
= keepInSync
;
459 public boolean keepInSync() {
464 public int describeContents() {
465 return this.hashCode();
469 public int compareTo(OCFile another
) {
470 if (isFolder() && another
.isFolder()) {
471 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
472 } else if (isFolder()) {
474 } else if (another
.isFolder()) {
477 return new AlphanumComparator().compare(this, another
);
481 public boolean equals(Object o
) {
482 if(o
instanceof OCFile
){
483 OCFile that
= (OCFile
) o
;
485 return this.mId
== that
.mId
;
493 public String
toString() {
494 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]";
495 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
);
499 public String
getEtag() {
503 public void setEtag(String etag
) {
508 public boolean isShareByLink() {
512 public void setShareByLink(boolean shareByLink
) {
513 this.mShareByLink
= shareByLink
;
516 public String
getPublicLink() {
520 public void setPublicLink(String publicLink
) {
521 this.mPublicLink
= publicLink
;
524 public long getLocalModificationTimestamp() {
525 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
526 File f
= new File(mLocalPath
);
527 return f
.lastModified();
532 /** @return 'True' if the file contains audio */
533 public boolean isAudio() {
534 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
537 /** @return 'True' if the file contains video */
538 public boolean isVideo() {
539 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
542 /** @return 'True' if the file contains an image */
543 public boolean isImage() {
544 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
545 getMimeTypeFromName().startsWith("image/"));
548 public String
getMimeTypeFromName() {
549 String extension
= "";
550 int pos
= mRemotePath
.lastIndexOf('.');
552 extension
= mRemotePath
.substring(pos
+ 1);
554 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
555 return (result
!= null
) ? result
: "";
558 public String
getPermissions() {
562 public void setPermissions(String permissions
) {
563 this.mPermissions
= permissions
;
566 public String
getRemoteId() {
570 public void setRemoteId(String remoteId
) {
571 this.mRemoteId
= remoteId
;