Fixed lack of File#getUsableSpace() in Android versions previous to GINGERBREAD
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / OCFile.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.datamodel;
20
21 import java.io.File;
22
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.util.Log;
26
27 public class OCFile implements Parcelable, Comparable<OCFile> {
28
29 public static final Parcelable.Creator<OCFile> CREATOR = new Parcelable.Creator<OCFile>() {
30 @Override
31 public OCFile createFromParcel(Parcel source) {
32 return new OCFile(source);
33 }
34
35 @Override
36 public OCFile[] newArray(int size) {
37 return new OCFile[size];
38 }
39 };
40
41 public static final String PATH_SEPARATOR = "/";
42
43 private static final String TAG = OCFile.class.getSimpleName();
44
45 private long mId;
46 private long mParentId;
47 private long mLength;
48 private long mCreationTimestamp;
49 private long mModifiedTimestamp;
50 private String mRemotePath;
51 private String mLocalPath;
52 private String mMimeType;
53 private boolean mNeedsUpdating;
54 private long mLastSyncDateForProperties;
55 private long mLastSyncDateForData;
56 private boolean mKeepInSync;
57
58 private String mEtag;
59
60 /**
61 * Create new {@link OCFile} with given path.
62 *
63 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
64 *
65 * @param path The remote path of the file.
66 */
67 public OCFile(String path) {
68 resetData();
69 mNeedsUpdating = false;
70 if (path == null || path.length() <= 0 || !path.startsWith(PATH_SEPARATOR)) {
71 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
72 }
73 mRemotePath = path;
74 }
75
76 /**
77 * Reconstruct from parcel
78 *
79 * @param source The source parcel
80 */
81 private OCFile(Parcel source) {
82 mId = source.readLong();
83 mParentId = source.readLong();
84 mLength = source.readLong();
85 mCreationTimestamp = source.readLong();
86 mModifiedTimestamp = source.readLong();
87 mRemotePath = source.readString();
88 mLocalPath = source.readString();
89 mMimeType = source.readString();
90 mNeedsUpdating = source.readInt() == 0;
91 mKeepInSync = source.readInt() == 1;
92 mLastSyncDateForProperties = source.readLong();
93 mLastSyncDateForData = source.readLong();
94 }
95
96 @Override
97 public void writeToParcel(Parcel dest, int flags) {
98 dest.writeLong(mId);
99 dest.writeLong(mParentId);
100 dest.writeLong(mLength);
101 dest.writeLong(mCreationTimestamp);
102 dest.writeLong(mModifiedTimestamp);
103 dest.writeString(mRemotePath);
104 dest.writeString(mLocalPath);
105 dest.writeString(mMimeType);
106 dest.writeInt(mNeedsUpdating ? 1 : 0);
107 dest.writeInt(mKeepInSync ? 1 : 0);
108 dest.writeLong(mLastSyncDateForProperties);
109 dest.writeLong(mLastSyncDateForData);
110 }
111
112 /**
113 * Gets the ID of the file
114 *
115 * @return the file ID
116 */
117 public long getFileId() {
118 return mId;
119 }
120
121 /**
122 * Returns the remote path of the file on ownCloud
123 *
124 * @return The remote path to the file
125 */
126 public String getRemotePath() {
127 return mRemotePath;
128 }
129
130 /**
131 * Can be used to check, whether or not this file exists in the database
132 * already
133 *
134 * @return true, if the file exists in the database
135 */
136 public boolean fileExists() {
137 return mId != -1;
138 }
139
140 /**
141 * Use this to find out if this file is a Directory
142 *
143 * @return true if it is a directory
144 */
145 public boolean isDirectory() {
146 return mMimeType != null && mMimeType.equals("DIR");
147 }
148
149 /**
150 * Use this to check if this file is available locally
151 *
152 * @return true if it is
153 */
154 public boolean isDown() {
155 if (mLocalPath != null && mLocalPath.length() > 0) {
156 File file = new File(mLocalPath);
157 return (file.exists());
158 }
159 return false;
160 }
161
162 /**
163 * The path, where the file is stored locally
164 *
165 * @return The local path to the file
166 */
167 public String getStoragePath() {
168 return mLocalPath;
169 }
170
171 /**
172 * Can be used to set the path where the file is stored
173 *
174 * @param storage_path to set
175 */
176 public void setStoragePath(String storage_path) {
177 mLocalPath = storage_path;
178 }
179
180 /**
181 * Get a UNIX timestamp of the file creation time
182 *
183 * @return A UNIX timestamp of the time that file was created
184 */
185 public long getCreationTimestamp() {
186 return mCreationTimestamp;
187 }
188
189 /**
190 * Set a UNIX timestamp of the time the file was created
191 *
192 * @param creation_timestamp to set
193 */
194 public void setCreationTimestamp(long creation_timestamp) {
195 mCreationTimestamp = creation_timestamp;
196 }
197
198 /**
199 * Get a UNIX timestamp of the file modification time
200 *
201 * @return A UNIX timestamp of the modification time
202 */
203 public long getModificationTimestamp() {
204 return mModifiedTimestamp;
205 }
206
207 /**
208 * Set a UNIX timestamp of the time the time the file was modified.
209 *
210 * @param modification_timestamp to set
211 */
212 public void setModificationTimestamp(long modification_timestamp) {
213 mModifiedTimestamp = modification_timestamp;
214 }
215
216 /**
217 * Returns the filename and "/" for the root directory
218 *
219 * @return The name of the file
220 */
221 public String getFileName() {
222 File f = new File(getRemotePath());
223 return f.getName().length() == 0 ? PATH_SEPARATOR : f.getName();
224 }
225
226 /**
227 * Sets the name of the file
228 *
229 * Does nothing if the new name is null, empty or includes "/" ; or if the file is the root directory
230 */
231 public void setFileName(String name) {
232 Log.d(TAG, "OCFile name changin from " + mRemotePath);
233 if (name != null && name.length() > 0 && !name.contains(PATH_SEPARATOR) && !mRemotePath.equals(PATH_SEPARATOR)) {
234 String parent = (new File(getRemotePath())).getParent();
235 parent = (parent.endsWith(PATH_SEPARATOR)) ? parent : parent + PATH_SEPARATOR;
236 mRemotePath = parent + name;
237 if (isDirectory()) {
238 mRemotePath += PATH_SEPARATOR;
239 }
240 Log.d(TAG, "OCFile name changed to " + mRemotePath);
241 }
242 }
243
244 /**
245 * Can be used to get the Mimetype
246 *
247 * @return the Mimetype as a String
248 */
249 public String getMimetype() {
250 return mMimeType;
251 }
252
253 /**
254 * Adds a file to this directory. If this file is not a directory, an
255 * exception gets thrown.
256 *
257 * @param file to add
258 * @throws IllegalStateException if you try to add a something and this is
259 * not a directory
260 */
261 public void addFile(OCFile file) throws IllegalStateException {
262 if (isDirectory()) {
263 file.mParentId = mId;
264 mNeedsUpdating = true;
265 return;
266 }
267 throw new IllegalStateException(
268 "This is not a directory where you can add stuff to!");
269 }
270
271 /**
272 * Used internally. Reset all file properties
273 */
274 private void resetData() {
275 mId = -1;
276 mRemotePath = null;
277 mParentId = 0;
278 mLocalPath = null;
279 mMimeType = null;
280 mLength = 0;
281 mCreationTimestamp = 0;
282 mModifiedTimestamp = 0;
283 mLastSyncDateForProperties = 0;
284 mLastSyncDateForData = 0;
285 mKeepInSync = false;
286 mNeedsUpdating = false;
287 }
288
289 /**
290 * Sets the ID of the file
291 *
292 * @param file_id to set
293 */
294 public void setFileId(long file_id) {
295 mId = file_id;
296 }
297
298 /**
299 * Sets the Mime-Type of the
300 *
301 * @param mimetype to set
302 */
303 public void setMimetype(String mimetype) {
304 mMimeType = mimetype;
305 }
306
307 /**
308 * Sets the ID of the parent folder
309 *
310 * @param parent_id to set
311 */
312 public void setParentId(long parent_id) {
313 mParentId = parent_id;
314 }
315
316 /**
317 * Sets the file size in bytes
318 *
319 * @param file_len to set
320 */
321 public void setFileLength(long file_len) {
322 mLength = file_len;
323 }
324
325 /**
326 * Returns the size of the file in bytes
327 *
328 * @return The filesize in bytes
329 */
330 public long getFileLength() {
331 return mLength;
332 }
333
334 /**
335 * Returns the ID of the parent Folder
336 *
337 * @return The ID
338 */
339 public long getParentId() {
340 return mParentId;
341 }
342
343 /**
344 * Check, if this file needs updating
345 *
346 * @return
347 */
348 public boolean needsUpdatingWhileSaving() {
349 return mNeedsUpdating;
350 }
351
352 public long getLastSyncDateForProperties() {
353 return mLastSyncDateForProperties;
354 }
355
356 public void setLastSyncDateForProperties(long lastSyncDate) {
357 mLastSyncDateForProperties = lastSyncDate;
358 }
359
360 public long getLastSyncDateForData() {
361 return mLastSyncDateForData;
362 }
363
364 public void setLastSyncDateForData(long lastSyncDate) {
365 mLastSyncDateForData = lastSyncDate;
366 }
367
368 public void setKeepInSync(boolean keepInSync) {
369 mKeepInSync = keepInSync;
370 }
371
372 public boolean keepInSync() {
373 return mKeepInSync;
374 }
375
376 @Override
377 public int describeContents() {
378 return this.hashCode();
379 }
380
381 @Override
382 public int compareTo(OCFile another) {
383 if (isDirectory() && another.isDirectory()) {
384 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
385 } else if (isDirectory()) {
386 return -1;
387 } else if (another.isDirectory()) {
388 return 1;
389 }
390 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
391 }
392
393 @Override
394 public boolean equals(Object o) {
395 if(o instanceof OCFile){
396 OCFile that = (OCFile) o;
397 if(that != null){
398 return this.mId == that.mId;
399 }
400 }
401
402 return false;
403 }
404
405 @Override
406 public String toString() {
407 String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
408 asString = String.format(asString, Long.valueOf(mId), getFileName(), mMimeType, isDown(), mLocalPath, mRemotePath, Long.valueOf(mParentId), Boolean.valueOf(mKeepInSync));
409 return asString;
410 }
411
412 public String getEtag() {
413 return mEtag;
414 }
415
416 public long getLocalModificationTimestamp() {
417 if (mLocalPath != null && mLocalPath.length() > 0) {
418 File f = new File(mLocalPath);
419 return f.lastModified();
420 }
421 return 0;
422 }
423
424 }