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