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