OCFile.java 9.7 KB

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