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