allow account deleting from account picker
[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 public class OCFile {
24
25 private long id;
26 private long parentId;
27 private long length;
28 private long creationTimestamp;
29 private long modifiedTimestamp;
30 private String remotePath;
31 private String localPath;
32 private String mimeType;
33 private boolean needsUpdating;
34
35 /**
36 * Create new {@link OCFile} with given path
37 *
38 * @param path The remote path of the file
39 */
40 public OCFile(String path) {
41 resetData();
42 needsUpdating = false;
43 remotePath = path;
44 }
45
46 /**
47 * Gets the ID of the file
48 *
49 * @return the file ID
50 */
51 public long getFileId() {
52 return id;
53 }
54
55 /**
56 * Returns the path of the file
57 *
58 * @return The path
59 */
60 public String getPath() {
61 return remotePath;
62 }
63
64 /**
65 * Can be used to check, whether or not this file exists in the database
66 * already
67 *
68 * @return true, if the file exists in the database
69 */
70 public boolean fileExists() {
71 return id != -1;
72 }
73
74 /**
75 * Use this to find out if this file is a Directory
76 *
77 * @return true if it is a directory
78 */
79 public boolean isDirectory() {
80 return mimeType != null && mimeType.equals("DIR");
81 }
82
83 /**
84 * Use this to check if this file is available locally
85 *
86 * @return true if it is
87 */
88 public boolean isDownloaded() {
89 return localPath != null || localPath.equals("");
90 }
91
92 /**
93 * The path, where the file is stored locally
94 *
95 * @return The local path to the file
96 */
97 public String getStoragePath() {
98 return localPath;
99 }
100
101 /**
102 * Can be used to set the path where the file is stored
103 *
104 * @param storage_path
105 * to set
106 */
107 public void setStoragePath(String storage_path) {
108 localPath = storage_path;
109 }
110
111 /**
112 * Get a UNIX timestamp of the file creation time
113 *
114 * @return A UNIX timestamp of the time that file was created
115 */
116 public long getCreationTimestamp() {
117 return creationTimestamp;
118 }
119
120 /**
121 * Set a UNIX timestamp of the time the file was created
122 *
123 * @param creation_timestamp
124 * to set
125 */
126 public void setCreationTimestamp(long creation_timestamp) {
127 creationTimestamp = creation_timestamp;
128 }
129
130 /**
131 * Get a UNIX timestamp of the file modification time
132 *
133 * @return A UNIX timestamp of the modification time
134 */
135 public long getModificationTimestamp() {
136 return modifiedTimestamp;
137 }
138
139 /**
140 * Set a UNIX timestamp of the time the time the file was modified.
141 *
142 * @param modification_timestamp
143 * to set
144 */
145 public void setModificationTimestamp(long modification_timestamp) {
146 modifiedTimestamp = modification_timestamp;
147 }
148
149 /**
150 * Returns the filename and "/" for the root directory
151 *
152 * @return The name of the file
153 */
154 public String getFileName() {
155 if (remotePath != null) {
156 File f = new File(remotePath);
157 return f.getName().equals("") ? "/" : f.getName();
158 }
159 return null;
160 }
161
162 /**
163 * Can be used to get the Mimetype
164 *
165 * @return the Mimetype as a String
166 */
167 public String getMimetype() {
168 return mimeType;
169 }
170
171 /**
172 * Adds a file to this directory. If this file is not a directory, an
173 * exception gets thrown.
174 *
175 * @param file to add
176 * @throws IllegalStateException if you try to add a something and this is not a directory
177 */
178 public void addFile(OCFile file) throws IllegalStateException {
179 if (isDirectory()) {
180 file.parentId = id;
181 needsUpdating = true;
182 return;
183 }
184 throw new IllegalStateException("This is not a directory where you can add stuff to!");
185 }
186
187 /**
188 * Used internally. Reset all file properties
189 */
190 private void resetData() {
191 id = -1;
192 remotePath = null;
193 parentId = 0;
194 localPath = null;
195 mimeType = null;
196 length = 0;
197 creationTimestamp = 0;
198 modifiedTimestamp = 0;
199 }
200
201 /**
202 * Sets the ID of the file
203 * @param file_id to set
204 */
205 public void setFileId(long file_id) {
206 id = file_id;
207 }
208
209 /**
210 * Sets the Mime-Type of the
211 * @param mimetype to set
212 */
213 public void setMimetype(String mimetype) {
214 mimeType = mimetype;
215 }
216
217 /**
218 * Sets the ID of the parent folder
219 * @param parent_id to set
220 */
221 public void setParentId(long parent_id) {
222 parentId = parent_id;
223 }
224
225 /**
226 * Sets the file size in bytes
227 * @param file_len to set
228 */
229 public void setFileLength(long file_len) {
230 length = file_len;
231 }
232
233 /**
234 * Returns the size of the file in bytes
235 * @return The filesize in bytes
236 */
237 public long getFileLength() {
238 return length;
239 }
240
241 /**
242 * Returns the ID of the parent Folder
243 * @return The ID
244 */
245 public long getParentId() {
246 return parentId;
247 }
248
249 /**
250 * Check, if this file needs updating
251 * @return
252 */
253 public boolean needsUpdatingWhileSaving() {
254 return needsUpdating;
255 }
256 }