OCFile.java 14 KB

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