Fixed null pointer in connection check
[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 import java.net.MalformedURLException;
23 import java.net.URL;
24
25 import com.owncloud.android.files.services.FileDownloader;
26
27
28 import android.net.Uri;
29 import android.os.Parcel;
30 import android.os.Parcelable;
31
32 public class OCFile implements Parcelable, Comparable<OCFile> {
33
34 public static final Parcelable.Creator<OCFile> CREATOR = new Parcelable.Creator<OCFile>() {
35 @Override
36 public OCFile createFromParcel(Parcel source) {
37 return new OCFile(source);
38 }
39
40 @Override
41 public OCFile[] newArray(int size) {
42 return new OCFile[size];
43 }
44 };
45
46 public static final String PATH_SEPARATOR = "/";
47
48 private long mId;
49 private long mParentId;
50 private long mLength;
51 private long mCreationTimestamp;
52 private long mModifiedTimestamp;
53 private String mRemotePath;
54 private String mLocalPath;
55 private String mMimeType;
56 private boolean mNeedsUpdating;
57 private long mLastSyncDate;
58 private boolean mKeepInSync;
59
60 /**
61 * Create new {@link OCFile} with given path.
62 *
63 * The path received must be URL-decoded. Path separator must be OCFile.PATH_SEPARATOR, and it must be the first character in 'path'.
64 *
65 * @param path The remote path of the file.
66 */
67 public OCFile(String path) {
68 resetData();
69 mNeedsUpdating = false;
70 if (path == null || path.length() <= 0 || !path.startsWith(PATH_SEPARATOR)) {
71 throw new IllegalArgumentException("Trying to create a OCFile with a non valid remote path: " + path);
72 }
73 mRemotePath = path;
74 }
75
76 /**
77 * Reconstruct from parcel
78 *
79 * @param source The source parcel
80 */
81 private OCFile(Parcel source) {
82 mId = source.readLong();
83 mParentId = source.readLong();
84 mLength = source.readLong();
85 mCreationTimestamp = source.readLong();
86 mModifiedTimestamp = source.readLong();
87 mRemotePath = source.readString();
88 mLocalPath = source.readString();
89 mMimeType = source.readString();
90 mNeedsUpdating = source.readInt() == 0;
91 mKeepInSync = source.readInt() == 1;
92 mLastSyncDate = source.readLong();
93 }
94
95 @Override
96 public void writeToParcel(Parcel dest, int flags) {
97 dest.writeLong(mId);
98 dest.writeLong(mParentId);
99 dest.writeLong(mLength);
100 dest.writeLong(mCreationTimestamp);
101 dest.writeLong(mModifiedTimestamp);
102 dest.writeString(mRemotePath);
103 dest.writeString(mLocalPath);
104 dest.writeString(mMimeType);
105 dest.writeInt(mNeedsUpdating ? 1 : 0);
106 dest.writeInt(mKeepInSync ? 1 : 0);
107 dest.writeLong(mLastSyncDate);
108 }
109
110 /**
111 * Gets the ID of the file
112 *
113 * @return the file ID
114 */
115 public long getFileId() {
116 return mId;
117 }
118
119 /**
120 * Returns the remote path of the file on ownCloud
121 *
122 * @return The remote path to the file
123 */
124 public String getRemotePath() {
125 return mRemotePath;
126 }
127
128 /**
129 * Can be used to check, whether or not this file exists in the database
130 * already
131 *
132 * @return true, if the file exists in the database
133 */
134 public boolean fileExists() {
135 return mId != -1;
136 }
137
138 /**
139 * Use this to find out if this file is a Directory
140 *
141 * @return true if it is a directory
142 */
143 public boolean isDirectory() {
144 return mMimeType != null && mMimeType.equals("DIR");
145 }
146
147 /**
148 * Use this to check if this file is available locally
149 *
150 * @return true if it is
151 */
152 public boolean isDown() {
153 if (mLocalPath != null && mLocalPath.length() > 0) {
154 File file = new File(mLocalPath);
155 return (file.exists());
156 }
157 return false;
158 }
159
160 /**
161 * The path, where the file is stored locally
162 *
163 * @return The local path to the file
164 */
165 public String getStoragePath() {
166 return mLocalPath;
167 }
168
169 /**
170 * Can be used to set the path where the file is stored
171 *
172 * @param storage_path to set
173 */
174 public void setStoragePath(String storage_path) {
175 mLocalPath = storage_path;
176 }
177
178 /**
179 * Get a UNIX timestamp of the file creation time
180 *
181 * @return A UNIX timestamp of the time that file was created
182 */
183 public long getCreationTimestamp() {
184 return mCreationTimestamp;
185 }
186
187 /**
188 * Set a UNIX timestamp of the time the file was created
189 *
190 * @param creation_timestamp to set
191 */
192 public void setCreationTimestamp(long creation_timestamp) {
193 mCreationTimestamp = creation_timestamp;
194 }
195
196 /**
197 * Get a UNIX timestamp of the file modification time
198 *
199 * @return A UNIX timestamp of the modification time
200 */
201 public long getModificationTimestamp() {
202 return mModifiedTimestamp;
203 }
204
205 /**
206 * Set a UNIX timestamp of the time the time the file was modified.
207 *
208 * @param modification_timestamp to set
209 */
210 public void setModificationTimestamp(long modification_timestamp) {
211 mModifiedTimestamp = modification_timestamp;
212 }
213
214 /**
215 * Returns the filename and "/" for the root directory
216 *
217 * @return The name of the file
218 */
219 public String getFileName() {
220 File f = new File(getRemotePath());
221 return f.getName().length() == 0 ? "/" : f.getName();
222 }
223
224 /**
225 * Can be used to get the Mimetype
226 *
227 * @return the Mimetype as a String
228 */
229 public String getMimetype() {
230 return mMimeType;
231 }
232
233 /**
234 * Adds a file to this directory. If this file is not a directory, an
235 * exception gets thrown.
236 *
237 * @param file to add
238 * @throws IllegalStateException if you try to add a something and this is
239 * not a directory
240 */
241 public void addFile(OCFile file) throws IllegalStateException {
242 if (isDirectory()) {
243 file.mParentId = mId;
244 mNeedsUpdating = true;
245 return;
246 }
247 throw new IllegalStateException(
248 "This is not a directory where you can add stuff to!");
249 }
250
251 /**
252 * Used internally. Reset all file properties
253 */
254 private void resetData() {
255 mId = -1;
256 mRemotePath = null;
257 mParentId = 0;
258 mLocalPath = null;
259 mMimeType = null;
260 mLength = 0;
261 mCreationTimestamp = 0;
262 mModifiedTimestamp = 0;
263 mLastSyncDate = 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 getLastSyncDate() {
332 return mLastSyncDate;
333 }
334
335 public void setLastSyncDate(long lastSyncDate) {
336 mLastSyncDate = lastSyncDate;
337 }
338
339 public void setKeepInSync(boolean keepInSync) {
340 mKeepInSync = keepInSync;
341 }
342
343 public boolean keepInSync() {
344 return mKeepInSync;
345 }
346
347 @Override
348 public int describeContents() {
349 return this.hashCode();
350 }
351
352 @Override
353 public int compareTo(OCFile another) {
354 if (isDirectory() && another.isDirectory()) {
355 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
356 } else if (isDirectory()) {
357 return -1;
358 } else if (another.isDirectory()) {
359 return 1;
360 }
361 return getRemotePath().toLowerCase().compareTo(another.getRemotePath().toLowerCase());
362 }
363
364 public boolean equals(Object o) {
365 if(o instanceof OCFile){
366 OCFile that = (OCFile) o;
367 if(that != null){
368 return this.mId == that.mId;
369 }
370 }
371
372 return false;
373 }
374
375 @Override
376 public String toString() {
377 String asString = "[id=%s, name=%s, mime=%s, downloaded=%s, local=%s, remote=%s, parentId=%s, keepInSinc=%s]";
378 asString = String.format(asString, new Long(mId), getFileName(), mMimeType, isDown(), mLocalPath, mRemotePath, new Long(mParentId), new Boolean(mKeepInSync));
379 return asString;
380 }
381
382 }