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