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