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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
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.
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/>.
20 package com
.owncloud
.android
.datamodel
;
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
;
31 public class OCFile
implements Parcelable
, Comparable
<OCFile
> {
33 public static final Parcelable
.Creator
<OCFile
> CREATOR
= new Parcelable
.Creator
<OCFile
>() {
35 public OCFile
createFromParcel(Parcel source
) {
36 return new OCFile(source
);
40 public OCFile
[] newArray(int size
) {
41 return new OCFile
[size
];
45 public static final String 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
;
66 * Create new {@link OCFile} with given path.
68 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
70 * @param path The remote path of the file.
72 public OCFile(String path
) {
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
);
82 * Reconstruct from parcel
84 * @param source The source parcel
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();
103 public void writeToParcel(Parcel dest
, int flags
) {
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
);
120 * Gets the ID of the file
122 * @return the file ID
124 public long getFileId() {
129 * Returns the remote path of the file on ownCloud
131 * @return The remote path to the file
133 public String
getRemotePath() {
138 * Can be used to check, whether or not this file exists in the database
141 * @return true, if the file exists in the database
143 public boolean fileExists() {
148 * Use this to find out if this file is a Directory
150 * @return true if it is a directory
152 public boolean isDirectory() {
153 return mMimeType
!= null
&& mMimeType
.equals("DIR");
157 * Use this to check if this file is available locally
159 * @return true if it is
161 public boolean isDown() {
162 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
163 File file
= new File(mLocalPath
);
164 return (file
.exists());
170 * The path, where the file is stored locally
172 * @return The local path to the file
174 public String
getStoragePath() {
179 * Can be used to set the path where the file is stored
181 * @param storage_path to set
183 public void setStoragePath(String storage_path
) {
184 mLocalPath
= storage_path
;
188 * Get a UNIX timestamp of the file creation time
190 * @return A UNIX timestamp of the time that file was created
192 public long getCreationTimestamp() {
193 return mCreationTimestamp
;
197 * Set a UNIX timestamp of the time the file was created
199 * @param creation_timestamp to set
201 public void setCreationTimestamp(long creation_timestamp
) {
202 mCreationTimestamp
= creation_timestamp
;
206 * Get a UNIX timestamp of the file modification time.
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.
211 public long getModificationTimestamp() {
212 return mModifiedTimestamp
;
216 * Set a UNIX timestamp of the time the time the file was modified.
218 * To update with the value returned by the server in every synchronization of the properties
221 * @param modification_timestamp to set
223 public void setModificationTimestamp(long modification_timestamp
) {
224 mModifiedTimestamp
= modification_timestamp
;
229 * Get a UNIX timestamp of the file modification time.
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.
234 public long getModificationTimestampAtLastSyncForData() {
235 return mModifiedTimestampAtLastSyncForData
;
239 * Set a UNIX timestamp of the time the time the file was modified.
241 * To update with the value returned by the server in every synchronization of THE CONTENTS
244 * @param modification_timestamp to set
246 public void setModificationTimestampAtLastSyncForData(long modificationTimestamp
) {
247 mModifiedTimestampAtLastSyncForData
= modificationTimestamp
;
253 * Returns the filename and "/" for the root directory
255 * @return The name of the file
257 public String
getFileName() {
258 File f
= new File(getRemotePath());
259 return f
.getName().length() == 0 ? PATH_SEPARATOR
: f
.getName();
263 * Sets the name of the file
265 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
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
;
274 mRemotePath
+= PATH_SEPARATOR
;
276 Log
.d(TAG
, "OCFile name changed to " + mRemotePath
);
281 * Can be used to get the Mimetype
283 * @return the Mimetype as a String
285 public String
getMimetype() {
290 * Adds a file to this directory. If this file is not a directory, an
291 * exception gets thrown.
294 * @throws IllegalStateException if you try to add a something and this is
297 public void addFile(OCFile file
) throws IllegalStateException
{
299 file
.mParentId
= mId
;
300 mNeedsUpdating
= true
;
303 throw new IllegalStateException(
304 "This is not a directory where you can add stuff to!");
308 * Used internally. Reset all file properties
310 private void resetData() {
317 mCreationTimestamp
= 0;
318 mModifiedTimestamp
= 0;
319 mModifiedTimestampAtLastSyncForData
= 0;
320 mLastSyncDateForProperties
= 0;
321 mLastSyncDateForData
= 0;
323 mNeedsUpdating
= false
;
327 * Sets the ID of the file
329 * @param file_id to set
331 public void setFileId(long file_id
) {
336 * Sets the Mime-Type of the
338 * @param mimetype to set
340 public void setMimetype(String mimetype
) {
341 mMimeType
= mimetype
;
345 * Sets the ID of the parent folder
347 * @param parent_id to set
349 public void setParentId(long parent_id
) {
350 mParentId
= parent_id
;
354 * Sets the file size in bytes
356 * @param file_len to set
358 public void setFileLength(long file_len
) {
363 * Returns the size of the file in bytes
365 * @return The filesize in bytes
367 public long getFileLength() {
372 * Returns the ID of the parent Folder
376 public long getParentId() {
381 * Check, if this file needs updating
385 public boolean needsUpdatingWhileSaving() {
386 return mNeedsUpdating
;
389 public long getLastSyncDateForProperties() {
390 return mLastSyncDateForProperties
;
393 public void setLastSyncDateForProperties(long lastSyncDate
) {
394 mLastSyncDateForProperties
= lastSyncDate
;
397 public long getLastSyncDateForData() {
398 return mLastSyncDateForData
;
401 public void setLastSyncDateForData(long lastSyncDate
) {
402 mLastSyncDateForData
= lastSyncDate
;
405 public void setKeepInSync(boolean keepInSync
) {
406 mKeepInSync
= keepInSync
;
409 public boolean keepInSync() {
414 public int describeContents() {
415 return this.hashCode();
419 public int compareTo(OCFile another
) {
420 if (isDirectory() && another
.isDirectory()) {
421 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
422 } else if (isDirectory()) {
424 } else if (another
.isDirectory()) {
427 return getRemotePath().toLowerCase().compareTo(another
.getRemotePath().toLowerCase());
431 public boolean equals(Object o
) {
432 if(o
instanceof OCFile
){
433 OCFile that
= (OCFile
) o
;
435 return this.mId
== that
.mId
;
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
));
449 public String
getEtag() {
453 public long getLocalModificationTimestamp() {
454 if (mLocalPath
!= null
&& mLocalPath
.length() > 0) {
455 File f
= new File(mLocalPath
);
456 return f
.lastModified();
461 /** @return 'True' if the file contains audio */
462 public boolean isAudio() {
463 return (mMimeType
!= null
&& mMimeType
.startsWith("audio/"));
466 /** @return 'True' if the file contains video */
467 public boolean isVideo() {
468 return (mMimeType
!= null
&& mMimeType
.startsWith("video/"));
471 /** @return 'True' if the file contains an image */
472 public boolean isImage() {
473 return ((mMimeType
!= null
&& mMimeType
.startsWith("image/")) ||
474 getMimeTypeFromName().startsWith("image/"));
477 public String
getMimeTypeFromName() {
478 String extension
= "";
479 int pos
= mRemotePath
.lastIndexOf('.');
481 extension
= mRemotePath
.substring(pos
+ 1);
483 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
484 return (result
!= null
) ? result
: "";