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