62bf0656804c1099f6e35f32119d41ece66fc990
[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 parent_id_;
27 private long length_;
28 private long creation_timestamp_;
29 private long modified_timestamp_;
30 private String path_;
31 private String storage_path_;
32 private String mimetype_;
33 private boolean update_while_saving_;
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 update_while_saving_ = false;
43 path_ = 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 path_;
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 storage_path_ != null;
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 storage_path_;
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 storage_path_ = 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 creation_timestamp_;
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 creation_timestamp_ = 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 modified_timestamp_;
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 modified_timestamp_ = 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 (path_ != null) {
156 File f = new File(path_);
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.parent_id_ = id_;
181 update_while_saving_ = 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 path_ = null;
193 parent_id_ = 0;
194 storage_path_ = null;
195 mimetype_ = null;
196 length_ = 0;
197 creation_timestamp_ = 0;
198 modified_timestamp_ = 0;
199 }
200
201 public void setFileId(long file_id) {
202 id_ = file_id;
203 }
204
205 public void setMimetype(String mimetype) {
206 mimetype_ = mimetype;
207 }
208
209 public void setParentId(long parent_id) {
210 parent_id_ = parent_id;
211 }
212
213 public void setFileLength(long file_len) {
214 length_ = file_len;
215 }
216
217 public long getFileLength() {
218 return length_;
219 }
220
221 public long getParentId() {
222 return parent_id_;
223 }
224
225 public boolean needsUpdatingWhileSaving() {
226 return update_while_saving_;
227 }
228 }