Workaround to show hidden accents in options menu
[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 mLastSyncDateForProperties;
52 private long mLastSyncDateForData;
53 private boolean mKeepInSync;
54
55 private String mEtag;
56
57 /**
58 * Create new {@link OCFile} with given path.
59 *
60 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
61 *
62 * @param path The remote path of the file.
63 */
64 public OCFile(String path) {
65 resetData();
66 mNeedsUpdating = false;
67 if (path == null || path.length() <= 0 || !path.startsWith(PATH_SEPARATOR)) {
68 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
69 }
70 mRemotePath = path;
71 }
72
73 /**
74 * Reconstruct from parcel
75 *
76 * @param source The source parcel
77 */
78 private OCFile(Parcel source) {
79 mId = source.readLong();
80 mParentId = source.readLong();
81 mLength = source.readLong();
82 mCreationTimestamp = source.readLong();
83 mModifiedTimestamp = source.readLong();
84 mRemotePath = source.readString();
85 mLocalPath = source.readString();
86 mMimeType = source.readString();
87 mNeedsUpdating = source.readInt() == 0;
88 mKeepInSync = source.readInt() == 1;
89 mLastSyncDateForProperties = source.readLong();
90 mLastSyncDateForData = source.readLong();
91 }
92
93 @Override
94 public void writeToParcel(Parcel dest, int flags) {
95 dest.writeLong(mId);
96 dest.writeLong(mParentId);
97 dest.writeLong(mLength);
98 dest.writeLong(mCreationTimestamp);
99 dest.writeLong(mModifiedTimestamp);
100 dest.writeString(mRemotePath);
101 dest.writeString(mLocalPath);
102 dest.writeString(mMimeType);
103 dest.writeInt(mNeedsUpdating ? 1 : 0);
104 dest.writeInt(mKeepInSync ? 1 : 0);
105 dest.writeLong(mLastSyncDateForProperties);
106 dest.writeLong(mLastSyncDateForData);
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 * The path, where the file is stored locally
161 *
162 * @return The local path to the file
163 */
164 public String getStoragePath() {
165 return mLocalPath;
166 }
167
168 /**
169 * Can be used to set the path where the file is stored
170 *
171 * @param storage_path to set
172 */
173 public void setStoragePath(String storage_path) {
174 mLocalPath = storage_path;
175 }
176
177 /**
178 * Get a UNIX timestamp of the file creation time
179 *
180 * @return A UNIX timestamp of the time that file was created
181 */
182 public long getCreationTimestamp() {
183 return mCreationTimestamp;
184 }
185
186 /**
187 * Set a UNIX timestamp of the time the file was created
188 *
189 * @param creation_timestamp to set
190 */
191 public void setCreationTimestamp(long creation_timestamp) {
192 mCreationTimestamp = creation_timestamp;
193 }
194
195 /**
196 * Get a UNIX timestamp of the file modification time
197 *
198 * @return A UNIX timestamp of the modification time
199 */
200 public long getModificationTimestamp() {
201 return mModifiedTimestamp;
202 }
203
204 /**
205 * Set a UNIX timestamp of the time the time the file was modified.
206 *
207 * @param modification_timestamp to set
208 */
209 public void setModificationTimestamp(long modification_timestamp) {
210 mModifiedTimestamp = modification_timestamp;
211 }
212
213 /**
214 * Returns the filename and "/" for the root directory
215 *
216 * @return The name of the file
217 */
218 public String getFileName() {
219 File f = new File(getRemotePath());
220 return f.getName().length() == 0 ? "/" : f.getName();
221 }
222
223 /**
224 * Can be used to get the Mimetype
225 *
226 * @return the Mimetype as a String
227 */
228 public String getMimetype() {
229 return mMimeType;
230 }
231
232 /**
233 * Adds a file to this directory. If this file is not a directory, an
234 * exception gets thrown.
235 *
236 * @param file to add
237 * @throws IllegalStateException if you try to add a something and this is
238 * not a directory
239 */
240 public void addFile(OCFile file) throws IllegalStateException {
241 if (isDirectory()) {
242 file.mParentId = mId;
243 mNeedsUpdating = true;
244 return;
245 }
246 throw new IllegalStateException(
247 "This is not a directory where you can add stuff to!");
248 }
249
250 /**
251 * Used internally. Reset all file properties
252 */
253 private void resetData() {
254 mId = -1;
255 mRemotePath = null;
256 mParentId = 0;
257 mLocalPath = null;
258 mMimeType = null;
259 mLength = 0;
260 mCreationTimestamp = 0;
261 mModifiedTimestamp = 0;
262 mLastSyncDateForProperties = 0;
263 mLastSyncDateForData = 0;
264 mKeepInSync = false;
265 mNeedsUpdating = false;
266 }
267
268 /**
269 * Sets the ID of the file
270 *
271 * @param file_id to set
272 */
273 public void setFileId(long file_id) {
274 mId = file_id;
275 }
276
277 /**
278 * Sets the Mime-Type of the
279 *
280 * @param mimetype to set
281 */
282 public void setMimetype(String mimetype) {
283 mMimeType = mimetype;
284 }
285
286 /**
287 * Sets the ID of the parent folder
288 *
289 * @param parent_id to set
290 */
291 public void setParentId(long parent_id) {
292 mParentId = parent_id;
293 }
294
295 /**
296 * Sets the file size in bytes
297 *
298 * @param file_len to set
299 */
300 public void setFileLength(long file_len) {
301 mLength = file_len;
302 }
303
304 /**
305 * Returns the size of the file in bytes
306 *
307 * @return The filesize in bytes
308 */
309 public long getFileLength() {
310 return mLength;
311 }
312
313 /**
314 * Returns the ID of the parent Folder
315 *
316 * @return The ID
317 */
318 public long getParentId() {
319 return mParentId;
320 }
321
322 /**
323 * Check, if this file needs updating
324 *
325 * @return
326 */
327 public boolean needsUpdatingWhileSaving() {
328 return mNeedsUpdating;
329 }
330
331 public long getLastSyncDateForProperties() {
332 return mLastSyncDateForProperties;
333 }
334
335 public void setLastSyncDateForProperties(long lastSyncDate) {
336 mLastSyncDateForProperties = lastSyncDate;
337 }
338
339 public long getLastSyncDateForData() {
340 return mLastSyncDateForData;
341 }
342
343 public void setLastSyncDateForData(long lastSyncDate) {
344 mLastSyncDateForData = lastSyncDate;
345 }
346
347 public void setKeepInSync(boolean keepInSync) {
348 mKeepInSync = keepInSync;
349 }
350
351 public boolean keepInSync() {
352 return mKeepInSync;
353 }
354
355 @Override
356 public int describeContents() {
357 return this.hashCode();
358 }
359
360 @Override
361 public int compareTo(OCFile another) {
362 if (isDirectory() && another.isDirectory()) {
363 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
364 } else if (isDirectory()) {
365 return -1;
366 } else if (another.isDirectory()) {
367 return 1;
368 }
369 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
370 }
371
372 @Override
373 public boolean equals(Object o) {
374 if(o instanceof OCFile){
375 OCFile that = (OCFile) o;
376 if(that != null){
377 return this.mId == that.mId;
378 }
379 }
380
381 return false;
382 }
383
384 @Override
385 public String toString() {
386 String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
387 asString = String.format(asString, new Long(mId), getFileName(), mMimeType, isDown(), mLocalPath, mRemotePath, new Long(mParentId), new Boolean(mKeepInSync));
388 return asString;
389 }
390
391 public String getEtag() {
392 return mEtag;
393 }
394
395 public long getLocalModificationTimestamp() {
396 if (mLocalPath != null && mLocalPath.length() > 0) {
397 File f = new File(mLocalPath);
398 return f.lastModified();
399 }
400 return 0;
401 }
402
403 }