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