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