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