OCFile.java 14 KB

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