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
;
21 import android
.os
.Parcel
;
22 import android
.os
.Parcelable
;
23 import android
.webkit
.MimeTypeMap
;
25 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
29 import third_parties
.daveKoeller
.AlphanumComparator
;
30 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
32 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
34 public OCFile
createFromParcel(Parcel source
) {
35 return new OCFile(source
);
39 public OCFile
[] newArray(int size
) {
40 return new OCFile
[size
];
44 public static final String PATH_SEPARATOR
= "/";
45 public static final String ROOT_PATH
= PATH_SEPARATOR
;
47 private static final String TAG
= OCFile
.class.getSimpleName();
50 private long mParentId
;
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
;
65 private boolean mShareByLink
;
66 private String mPublicLink
;
68 private String mPermissions
;
69 private String mRemoteId
;
71 private boolean mNeedsUpdateThumbnail
;
73 private boolean mIsDownloading
;
77 * Create new {@link OCFile} with given path.
79 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
81 * @param path The remote path of the file.
83 public OCFile(String path
) {
85 mNeedsUpdating
= false
;
86 if (path
== null
|| path
.length() <= 0 || !path
.startsWith(PATH_SEPARATOR
)) {
87 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path
);
93 * Reconstruct from parcel
95 * @param source The source parcel
97 private OCFile(Parcel source
) {
98 mId
= source
.readLong();
99 mParentId
= source
.readLong();
100 mLength
= source
.readLong();
101 mCreationTimestamp
= source
.readLong();
102 mModifiedTimestamp
= source
.readLong();
103 mModifiedTimestampAtLastSyncForData
= source
.readLong();
104 mRemotePath
= source
.readString();
105 mLocalPath
= source
.readString();
106 mMimeType
= source
.readString();
107 mNeedsUpdating
= source
.readInt() == 0;
108 mKeepInSync
= source
.readInt() == 1;
109 mLastSyncDateForProperties
= source
.readLong();
110 mLastSyncDateForData
= source
.readLong();
111 mEtag
= source
.readString();
112 mShareByLink
= source
.readInt() == 1;
113 mPublicLink
= source
.readString();
114 mPermissions
= source
.readString();
115 mRemoteId
= source
.readString();
116 mNeedsUpdateThumbnail
= source
.readInt() == 0;
117 mIsDownloading
= 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);
142 dest
.writeInt(mIsDownloading ?
1 : 0);
146 * Gets the ID of the file
148 * @return the file ID
150 public long getFileId() {
155 * Returns the remote path of the file on ownCloud
157 * @return The remote path to the file
159 public String
getRemotePath() {
164 * Can be used to check, whether or not this file exists in the database
167 * @return true, if the file exists in the database
169 public boolean fileExists() {
174 * Use this to find out if this file is a folder.
176 * @return true if it is a folder
178 public boolean isFolder() {
179 return mMimeType
!= null
&& mMimeType
.equals("DIR");
183 * Use this to check if this file is available locally
185 * @return true if it is
187 public boolean isDown() {
188 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
189 File file
= new File(mLocalPath
);
190 return (file
.exists());
196 * The path, where the file is stored locally
198 * @return The local path to the file
200 public String
getStoragePath() {
205 * Can be used to set the path where the file is stored
207 * @param storage_path to set
209 public void setStoragePath(String storage_path
) {
210 mLocalPath
= storage_path
;
214 * Get a UNIX timestamp of the file creation time
216 * @return A UNIX timestamp of the time that file was created
218 public long getCreationTimestamp() {
219 return mCreationTimestamp
;
223 * Set a UNIX timestamp of the time the file was created
225 * @param creation_timestamp to set
227 public void setCreationTimestamp(long creation_timestamp
) {
228 mCreationTimestamp
= creation_timestamp
;
232 * Get a UNIX timestamp of the file modification time.
234 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
235 * in the last synchronization of the properties of this file.
237 public long getModificationTimestamp() {
238 return mModifiedTimestamp
;
242 * Set a UNIX timestamp of the time the time the file was modified.
244 * To update with the value returned by the server in every synchronization of the properties
247 * @param modification_timestamp to set
249 public void setModificationTimestamp(long modification_timestamp
) {
250 mModifiedTimestamp
= modification_timestamp
;
255 * Get a UNIX timestamp of the file modification time.
257 * @return A UNIX timestamp of the modification time, corresponding to the value returned by the server
258 * in the last synchronization of THE CONTENTS of this file.
260 public long getModificationTimestampAtLastSyncForData() {
261 return mModifiedTimestampAtLastSyncForData
;
265 * Set a UNIX timestamp of the time the time the file was modified.
267 * To update with the value returned by the server in every synchronization of THE CONTENTS
270 * @param modificationTimestamp to set
272 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
273 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
);
306 * Can be used to get the Mimetype
308 * @return the Mimetype as a String
310 public String
getMimetype() {
315 * Adds a file to this directory. If this file is not a directory, an
316 * exception gets thrown.
319 * @throws IllegalStateException if you try to add a something and this is
322 public void addFile(OCFile file
) throws IllegalStateException
{
324 file
.mParentId
= mId
;
325 mNeedsUpdating
= true
;
328 throw new IllegalStateException(
329 "This is not a directory where you can add stuff to!");
333 * Used internally. Reset all file properties
335 private void resetData() {
342 mCreationTimestamp
= 0;
343 mModifiedTimestamp
= 0;
344 mModifiedTimestampAtLastSyncForData
= 0;
345 mLastSyncDateForProperties
= 0;
346 mLastSyncDateForData
= 0;
348 mNeedsUpdating
= false
;
350 mShareByLink
= false
;
354 mNeedsUpdateThumbnail
= false
;
355 mIsDownloading
= false
;
359 * Sets the ID of the file
361 * @param file_id to set
363 public void setFileId(long file_id
) {
368 * Sets the Mime-Type of the
370 * @param mimetype to set
372 public void setMimetype(String mimetype
) {
373 mMimeType
= mimetype
;
377 * Sets the ID of the parent folder
379 * @param parent_id to set
381 public void setParentId(long parent_id
) {
382 mParentId
= parent_id
;
386 * Sets the file size in bytes
388 * @param file_len to set
390 public void setFileLength(long file_len
) {
395 * Returns the size of the file in bytes
397 * @return The filesize in bytes
399 public long getFileLength() {
404 * Returns the ID of the parent Folder
408 public long getParentId() {
413 * Check, if this file needs updating
417 public boolean needsUpdatingWhileSaving() {
418 return mNeedsUpdating
;
421 public boolean needsUpdateThumbnail() {
422 return mNeedsUpdateThumbnail
;
425 public void setNeedsUpdateThumbnail(boolean needsUpdateThumbnail
) {
426 this.mNeedsUpdateThumbnail
= needsUpdateThumbnail
;
429 public long getLastSyncDateForProperties() {
430 return mLastSyncDateForProperties
;
433 public void setLastSyncDateForProperties(long lastSyncDate
) {
434 mLastSyncDateForProperties
= lastSyncDate
;
437 public long getLastSyncDateForData() {
438 return mLastSyncDateForData
;
441 public void setLastSyncDateForData(long lastSyncDate
) {
442 mLastSyncDateForData
= lastSyncDate
;
445 public void setKeepInSync(boolean keepInSync
) {
446 mKeepInSync
= keepInSync
;
449 public boolean keepInSync() {
454 public int describeContents() {
455 return ((Object
) this).hashCode();
459 public int compareTo(OCFile another
) {
460 if (isFolder() && another
.isFolder()) {
461 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
462 } else if (isFolder()) {
464 } else if (another
.isFolder()) {
467 return new AlphanumComparator().compare(this, another
);
471 public boolean equals(Object o
) {
472 if (o
instanceof OCFile
) {
473 OCFile that
= (OCFile
) o
;
475 return this.mId
== that
.mId
;
483 public String
toString() {
484 String asString
= "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSync=%s etag=%s]";
485 asString
= String
.format(asString
, Long
.valueOf(mId
), getFileName(), mMimeType
, isDown(), mLocalPath
, mRemotePath
, Long
.valueOf(mParentId
), Boolean
.valueOf(mKeepInSync
), mEtag
);
489 public String
getEtag() {
493 public void setEtag(String etag
) {
498 public boolean isShareByLink() {
502 public void setShareByLink(boolean shareByLink
) {
503 this.mShareByLink
= shareByLink
;
506 public String
getPublicLink() {
510 public void setPublicLink(String publicLink
) {
511 this.mPublicLink
= publicLink
;
514 public long getLocalModificationTimestamp() {
515 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
516 File f
= new File(mLocalPath
);
517 return f
.lastModified();
523 * @return 'True' if the file contains audio
525 public boolean isAudio() {
526 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
530 * @return 'True' if the file contains video
532 public boolean isVideo() {
533 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
537 * @return 'True' if the file contains an image
539 public boolean isImage() {
540 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
541 getMimeTypeFromName().startsWith("image/"));
544 public String
getMimeTypeFromName() {
545 String extension
= "";
546 int pos
= mRemotePath
.lastIndexOf('.');
548 extension
= mRemotePath
.substring(pos
+ 1);
550 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
551 return (result
!= null
) ? result
: "";
554 public String
getPermissions() {
558 public void setPermissions(String permissions
) {
559 this.mPermissions
= permissions
;
562 public String
getRemoteId() {
566 public void setRemoteId(String remoteId
) {
567 this.mRemoteId
= remoteId
;
570 public boolean isDownloading() {
571 return mIsDownloading
;
574 public void setDownloading(boolean isDownloading
) {
575 this.mIsDownloading
= isDownloading
;
578 public boolean isSynchronizing() {
579 // TODO real implementation