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