e2cebcfc327600708f8d223a18b2117383bd79db
[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 The remote path of the file
54 */
55 public OCFile(String path) {
56 resetData();
57 mNeedsUpdating = false;
58 mRemotePath = path;
59 }
60
61 /**
62 * Reconstruct from parcel
63 *
64 * @param source The source parcel
65 */
66 private OCFile(Parcel source) {
67 mId = source.readLong();
68 mParentId = source.readLong();
69 mLength = source.readLong();
70 mCreationTimestamp = source.readLong();
71 mModifiedTimestamp = source.readLong();
72 mRemotePath = source.readString();
73 mLocalPath = source.readString();
74 mMimeType = source.readString();
75 mNeedsUpdating = 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 mId;
85 }
86
87 /**
88 * Returns the path of the file
89 *
90 * @return The path
91 */
92 public String getPath() {
93 return mRemotePath;
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 mId != -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 mMimeType != null && mMimeType.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 mLocalPath != null || mLocalPath.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 mLocalPath;
131 }
132
133 /**
134 * Can be used to set the path where the file is stored
135 *
136 * @param storage_path to set
137 */
138 public void setStoragePath(String storage_path) {
139 mLocalPath = storage_path;
140 }
141
142 /**
143 * Get a UNIX timestamp of the file creation time
144 *
145 * @return A UNIX timestamp of the time that file was created
146 */
147 public long getCreationTimestamp() {
148 return mCreationTimestamp;
149 }
150
151 /**
152 * Set a UNIX timestamp of the time the file was created
153 *
154 * @param creation_timestamp to set
155 */
156 public void setCreationTimestamp(long creation_timestamp) {
157 mCreationTimestamp = creation_timestamp;
158 }
159
160 /**
161 * Get a UNIX timestamp of the file modification time
162 *
163 * @return A UNIX timestamp of the modification time
164 */
165 public long getModificationTimestamp() {
166 return mModifiedTimestamp;
167 }
168
169 /**
170 * Set a UNIX timestamp of the time the time the file was modified.
171 *
172 * @param modification_timestamp to set
173 */
174 public void setModificationTimestamp(long modification_timestamp) {
175 mModifiedTimestamp = modification_timestamp;
176 }
177
178 /**
179 * Returns the filename and "/" for the root directory
180 *
181 * @return The name of the file
182 */
183 public String getFileName() {
184 if (mRemotePath != null) {
185 File f = new File(mRemotePath);
186 return f.getName().equals("") ? "/" : f.getName();
187 }
188 return null;
189 }
190
191 /**
192 * Can be used to get the Mimetype
193 *
194 * @return the Mimetype as a String
195 */
196 public String getMimetype() {
197 return mMimeType;
198 }
199
200 /**
201 * Adds a file to this directory. If this file is not a directory, an
202 * exception gets thrown.
203 *
204 * @param file to add
205 * @throws IllegalStateException if you try to add a something and this is
206 * not a directory
207 */
208 public void addFile(OCFile file) throws IllegalStateException {
209 if (isDirectory()) {
210 file.mParentId = mId;
211 mNeedsUpdating = true;
212 return;
213 }
214 throw new IllegalStateException(
215 "This is not a directory where you can add stuff to!");
216 }
217
218 /**
219 * Used internally. Reset all file properties
220 */
221 private void resetData() {
222 mId = -1;
223 mRemotePath = null;
224 mParentId = 0;
225 mLocalPath = null;
226 mMimeType = null;
227 mLength = 0;
228 mCreationTimestamp = 0;
229 mModifiedTimestamp = 0;
230 }
231
232 /**
233 * Sets the ID of the file
234 *
235 * @param file_id to set
236 */
237 public void setFileId(long file_id) {
238 mId = file_id;
239 }
240
241 /**
242 * Sets the Mime-Type of the
243 *
244 * @param mimetype to set
245 */
246 public void setMimetype(String mimetype) {
247 mMimeType = mimetype;
248 }
249
250 /**
251 * Sets the ID of the parent folder
252 *
253 * @param parent_id to set
254 */
255 public void setParentId(long parent_id) {
256 mParentId = parent_id;
257 }
258
259 /**
260 * Sets the file size in bytes
261 *
262 * @param file_len to set
263 */
264 public void setFileLength(long file_len) {
265 mLength = file_len;
266 }
267
268 /**
269 * Returns the size of the file in bytes
270 *
271 * @return The filesize in bytes
272 */
273 public long getFileLength() {
274 return mLength;
275 }
276
277 /**
278 * Returns the ID of the parent Folder
279 *
280 * @return The ID
281 */
282 public long getParentId() {
283 return mParentId;
284 }
285
286 /**
287 * Check, if this file needs updating
288 *
289 * @return
290 */
291 public boolean needsUpdatingWhileSaving() {
292 return mNeedsUpdating;
293 }
294
295 @Override
296 public int describeContents() {
297 return this.hashCode();
298 }
299
300 @Override
301 public void writeToParcel(Parcel dest, int flags) {
302 dest.writeLong(mId);
303 dest.writeLong(mParentId);
304 dest.writeLong(mLength);
305 dest.writeLong(mCreationTimestamp);
306 dest.writeLong(mModifiedTimestamp);
307 dest.writeString(mRemotePath);
308 dest.writeString(mLocalPath);
309 dest.writeString(mMimeType);
310 dest.writeInt(mNeedsUpdating ? 0 : 1); // No writeBoolean method exists
311 // - yay :D
312 }
313
314 }