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