FileDataStorageManager.java 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2012 Bartek Przybylski
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.datamodel;
  21. import android.accounts.Account;
  22. import android.content.ContentProviderClient;
  23. import android.content.ContentProviderOperation;
  24. import android.content.ContentProviderResult;
  25. import android.content.ContentResolver;
  26. import android.content.ContentUris;
  27. import android.content.ContentValues;
  28. import android.content.Intent;
  29. import android.content.OperationApplicationException;
  30. import android.database.Cursor;
  31. import android.net.Uri;
  32. import android.os.RemoteException;
  33. import android.provider.MediaStore;
  34. import com.owncloud.android.MainApp;
  35. import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
  36. import com.owncloud.android.lib.common.utils.Log_OC;
  37. import com.owncloud.android.lib.resources.shares.OCShare;
  38. import com.owncloud.android.lib.resources.shares.ShareType;
  39. import com.owncloud.android.lib.resources.status.CapabilityBooleanType;
  40. import com.owncloud.android.lib.resources.status.OCCapability;
  41. import com.owncloud.android.utils.FileStorageUtils;
  42. import com.owncloud.android.utils.MimeType;
  43. import com.owncloud.android.utils.MimeTypeUtil;
  44. import java.io.File;
  45. import java.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import java.util.HashSet;
  49. import java.util.Iterator;
  50. import java.util.List;
  51. import java.util.Locale;
  52. import java.util.Set;
  53. import java.util.Vector;
  54. public class FileDataStorageManager {
  55. public static final int ROOT_PARENT_ID = 0;
  56. private ContentResolver mContentResolver;
  57. private ContentProviderClient mContentProviderClient;
  58. private Account mAccount;
  59. private static final String AND = "=? AND ";
  60. private static final String FAILED_TO_INSERT_MSG = "Fail to insert insert file to database ";
  61. private static final String SENDING_TO_FILECONTENTPROVIDER_MSG = "Sending %d operations to FileContentProvider";
  62. private static final String EXCEPTION_MSG = "Exception in batch of operations ";
  63. private static final String TAG = FileDataStorageManager.class.getSimpleName();
  64. public FileDataStorageManager(Account account, ContentResolver cr) {
  65. mContentProviderClient = null;
  66. mContentResolver = cr;
  67. mAccount = account;
  68. }
  69. public FileDataStorageManager(Account account, ContentProviderClient cp) {
  70. mContentProviderClient = cp;
  71. mContentResolver = null;
  72. mAccount = account;
  73. }
  74. public void setAccount(Account account) {
  75. mAccount = account;
  76. }
  77. public Account getAccount() {
  78. return mAccount;
  79. }
  80. public ContentResolver getContentResolver() {
  81. return mContentResolver;
  82. }
  83. public ContentProviderClient getContentProviderClient() {
  84. return mContentProviderClient;
  85. }
  86. public OCFile getFileByPath(String path) {
  87. Cursor c = getFileCursorForValue(ProviderTableMeta.FILE_PATH, path);
  88. OCFile file = null;
  89. if (c.moveToFirst()) {
  90. file = createFileInstance(c);
  91. }
  92. c.close();
  93. if (file == null && OCFile.ROOT_PATH.equals(path)) {
  94. return createRootDir(); // root should always exist
  95. }
  96. return file;
  97. }
  98. public OCFile getFileById(long id) {
  99. Cursor c = getFileCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
  100. OCFile file = null;
  101. if (c.moveToFirst()) {
  102. file = createFileInstance(c);
  103. }
  104. c.close();
  105. return file;
  106. }
  107. public OCFile getFileByLocalPath(String path) {
  108. Cursor c = getFileCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
  109. OCFile file = null;
  110. if (c.moveToFirst()) {
  111. file = createFileInstance(c);
  112. }
  113. c.close();
  114. return file;
  115. }
  116. public boolean fileExists(long id) {
  117. return fileExists(ProviderTableMeta._ID, String.valueOf(id));
  118. }
  119. public boolean fileExists(String path) {
  120. return fileExists(ProviderTableMeta.FILE_PATH, path);
  121. }
  122. public Vector<OCFile> getFolderContent(OCFile f, boolean onlyOnDevice) {
  123. if (f != null && f.isFolder() && f.getFileId() != -1) {
  124. return getFolderContent(f.getFileId(), onlyOnDevice);
  125. } else {
  126. return new Vector<OCFile>();
  127. }
  128. }
  129. public Vector<OCFile> getFolderImages(OCFile folder, boolean onlyOnDevice) {
  130. Vector<OCFile> ret = new Vector<OCFile>();
  131. if (folder != null) {
  132. // TODO better implementation, filtering in the access to database instead of here
  133. Vector<OCFile> tmp = getFolderContent(folder, onlyOnDevice);
  134. OCFile current = null;
  135. for (int i=0; i<tmp.size(); i++) {
  136. current = tmp.get(i);
  137. if (MimeTypeUtil.isImage(current)) {
  138. ret.add(current);
  139. }
  140. }
  141. }
  142. return ret;
  143. }
  144. public boolean saveFile(OCFile file) {
  145. boolean overriden = false;
  146. ContentValues cv = new ContentValues();
  147. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  148. cv.put(
  149. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  150. file.getModificationTimestampAtLastSyncForData()
  151. );
  152. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  153. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  154. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  155. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  156. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  157. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  158. if (!file.isFolder()) {
  159. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  160. }
  161. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  162. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  163. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
  164. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.isFavorite() ? 1 : 0);
  165. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  166. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
  167. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
  168. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
  169. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  170. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  171. cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail());
  172. cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading());
  173. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
  174. boolean sameRemotePath = fileExists(file.getRemotePath());
  175. if (sameRemotePath ||
  176. fileExists(file.getFileId())) { // for renamed files; no more delete and create
  177. OCFile oldFile;
  178. if (sameRemotePath) {
  179. oldFile = getFileByPath(file.getRemotePath());
  180. file.setFileId(oldFile.getFileId());
  181. } else {
  182. oldFile = getFileById(file.getFileId());
  183. }
  184. overriden = true;
  185. if (getContentResolver() != null) {
  186. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv,
  187. ProviderTableMeta._ID + "=?",
  188. new String[]{String.valueOf(file.getFileId())});
  189. } else {
  190. try {
  191. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI,
  192. cv, ProviderTableMeta._ID + "=?",
  193. new String[]{String.valueOf(file.getFileId())});
  194. } catch (RemoteException e) {
  195. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  196. }
  197. }
  198. } else {
  199. Uri result_uri = null;
  200. if (getContentResolver() != null) {
  201. result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
  202. } else {
  203. try {
  204. result_uri = getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
  205. } catch (RemoteException e) {
  206. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  207. }
  208. }
  209. if (result_uri != null) {
  210. long new_id = Long.parseLong(result_uri.getPathSegments().get(1));
  211. file.setFileId(new_id);
  212. }
  213. }
  214. return overriden;
  215. }
  216. public void saveNewFile(OCFile newFile) {
  217. String remoteParentPath = new File(newFile.getRemotePath()).getParent();
  218. remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ?
  219. remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
  220. OCFile parent = getFileByPath(remoteParentPath);
  221. if (parent != null) {
  222. newFile.setParentId(parent.getFileId());
  223. saveFile(newFile);
  224. } else {
  225. throw new IllegalArgumentException("Saving a new file in an unexisting folder");
  226. }
  227. }
  228. /**
  229. * Inserts or updates the list of files contained in a given folder.
  230. * <p/>
  231. * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
  232. * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
  233. *
  234. * @param folder
  235. * @param updatedFiles
  236. * @param filesToRemove
  237. */
  238. public void saveFolder(
  239. OCFile folder, Collection<OCFile> updatedFiles, Collection<OCFile> filesToRemove
  240. ) {
  241. Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size()
  242. + " children and " + filesToRemove.size() + " files to remove");
  243. ArrayList<ContentProviderOperation> operations =
  244. new ArrayList<ContentProviderOperation>(updatedFiles.size());
  245. // prepare operations to insert or update files to save in the given folder
  246. for (OCFile file : updatedFiles) {
  247. ContentValues cv = new ContentValues();
  248. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  249. cv.put(
  250. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  251. file.getModificationTimestampAtLastSyncForData()
  252. );
  253. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  254. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  255. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  256. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  257. //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  258. cv.put(ProviderTableMeta.FILE_PARENT, folder.getFileId());
  259. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  260. if (!file.isFolder()) {
  261. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  262. }
  263. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  264. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  265. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
  266. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.isFavorite() ? 1 : 0);
  267. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  268. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
  269. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
  270. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
  271. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  272. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  273. cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail());
  274. cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading());
  275. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
  276. boolean existsByPath = fileExists(file.getRemotePath());
  277. if (existsByPath || fileExists(file.getFileId())) {
  278. // updating an existing file
  279. operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  280. withValues(cv).
  281. withSelection(ProviderTableMeta._ID + "=?",
  282. new String[]{String.valueOf(file.getFileId())})
  283. .build());
  284. } else {
  285. // adding a new file
  286. operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).
  287. withValues(cv).build());
  288. }
  289. }
  290. // prepare operations to remove files in the given folder
  291. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  292. ProviderTableMeta.FILE_PATH + "=?";
  293. String [] whereArgs = null;
  294. for (OCFile file : filesToRemove) {
  295. if (file.getParentId() == folder.getFileId()) {
  296. whereArgs = new String[]{mAccount.name, file.getRemotePath()};
  297. if (file.isFolder()) {
  298. operations.add(ContentProviderOperation.newDelete(
  299. ContentUris.withAppendedId(
  300. ProviderTableMeta.CONTENT_URI_DIR, file.getFileId()
  301. )
  302. ).withSelection(where, whereArgs).build());
  303. File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
  304. if (localFolder.exists()) {
  305. removeLocalFolder(localFolder);
  306. }
  307. } else {
  308. operations.add(ContentProviderOperation.newDelete(
  309. ContentUris.withAppendedId(
  310. ProviderTableMeta.CONTENT_URI_FILE, file.getFileId()
  311. )
  312. ).withSelection(where, whereArgs).build());
  313. if (file.isDown()) {
  314. String path = file.getStoragePath();
  315. new File(path).delete();
  316. triggerMediaScan(path); // notify MediaScanner about removed file
  317. }
  318. }
  319. }
  320. }
  321. // update metadata of folder
  322. ContentValues cv = new ContentValues();
  323. cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp());
  324. cv.put(
  325. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  326. folder.getModificationTimestampAtLastSyncForData()
  327. );
  328. cv.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp());
  329. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0);
  330. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, folder.getMimetype());
  331. cv.put(ProviderTableMeta.FILE_NAME, folder.getFileName());
  332. cv.put(ProviderTableMeta.FILE_PARENT, folder.getParentId());
  333. cv.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath());
  334. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  335. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties());
  336. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData());
  337. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, folder.isFavorite() ? 1 : 0);
  338. cv.put(ProviderTableMeta.FILE_ETAG, folder.getEtag());
  339. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, folder.isSharedViaLink() ? 1 : 0);
  340. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, folder.isSharedWithSharee() ? 1 : 0);
  341. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, folder.getPublicLink());
  342. cv.put(ProviderTableMeta.FILE_PERMISSIONS, folder.getPermissions());
  343. cv.put(ProviderTableMeta.FILE_REMOTE_ID, folder.getRemoteId());
  344. operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  345. withValues(cv).
  346. withSelection(ProviderTableMeta._ID + "=?",
  347. new String[]{String.valueOf(folder.getFileId())})
  348. .build());
  349. // apply operations in batch
  350. ContentProviderResult[] results = null;
  351. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  352. try {
  353. if (getContentResolver() != null) {
  354. results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  355. } else {
  356. results = getContentProviderClient().applyBatch(operations);
  357. }
  358. } catch (OperationApplicationException | RemoteException e) {
  359. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  360. }
  361. // update new id in file objects for insertions
  362. if (results != null) {
  363. long newId;
  364. Iterator<OCFile> filesIt = updatedFiles.iterator();
  365. OCFile file = null;
  366. for (ContentProviderResult result : results) {
  367. if (filesIt.hasNext()) {
  368. file = filesIt.next();
  369. } else {
  370. file = null;
  371. }
  372. if (result.uri != null) {
  373. newId = Long.parseLong(result.uri.getPathSegments().get(1));
  374. //updatedFiles.get(i).setFileId(newId);
  375. if (file != null) {
  376. file.setFileId(newId);
  377. }
  378. }
  379. }
  380. }
  381. }
  382. public boolean removeFile(OCFile file, boolean removeDBData, boolean removeLocalCopy) {
  383. boolean success = false;
  384. if (file != null) {
  385. if (file.isFolder()) {
  386. success = removeFolder(file, removeDBData, removeLocalCopy);
  387. } else {
  388. if (removeDBData) {
  389. //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE,
  390. // ""+file.getFileId());
  391. Uri file_uri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId());
  392. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?";
  393. String[] whereArgs = new String[]{mAccount.name, file.getRemotePath()};
  394. int deleted = 0;
  395. if (getContentProviderClient() != null) {
  396. try {
  397. deleted = getContentProviderClient().delete(file_uri, where, whereArgs);
  398. } catch (RemoteException e) {
  399. Log_OC.d(TAG, e.getMessage(), e);
  400. }
  401. } else {
  402. deleted = getContentResolver().delete(file_uri, where, whereArgs);
  403. }
  404. success &= (deleted > 0);
  405. }
  406. String localPath = file.getStoragePath();
  407. if (removeLocalCopy && file.isDown() && localPath != null && success) {
  408. success = new File(localPath).delete();
  409. if (success) {
  410. deleteFileInMediaScan(localPath);
  411. }
  412. if (!removeDBData && success) {
  413. // maybe unnecessary, but should be checked TODO remove if unnecessary
  414. file.setStoragePath(null);
  415. saveFile(file);
  416. saveConflict(file, null);
  417. }
  418. }
  419. }
  420. }
  421. return success;
  422. }
  423. public boolean removeFolder(OCFile folder, boolean removeDBData, boolean removeLocalContent) {
  424. boolean success = false;
  425. if (folder != null && folder.isFolder()) {
  426. if (removeDBData && folder.getFileId() != -1) {
  427. success = removeFolderInDb(folder);
  428. }
  429. if (removeLocalContent && success) {
  430. success = removeLocalFolder(folder);
  431. }
  432. }
  433. return success;
  434. }
  435. private boolean removeFolderInDb(OCFile folder) {
  436. Uri folder_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, "" + folder.getFileId()); // URI
  437. // for recursive deletion
  438. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?";
  439. String [] whereArgs = new String[]{mAccount.name, folder.getRemotePath()};
  440. int deleted = 0;
  441. if (getContentProviderClient() != null) {
  442. try {
  443. deleted = getContentProviderClient().delete(folder_uri, where, whereArgs);
  444. } catch (RemoteException e) {
  445. Log_OC.d(TAG, e.getMessage(), e);
  446. }
  447. } else {
  448. deleted = getContentResolver().delete(folder_uri, where, whereArgs);
  449. }
  450. return deleted > 0;
  451. }
  452. private boolean removeLocalFolder(OCFile folder) {
  453. boolean success = true;
  454. String localFolderPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, folder);
  455. File localFolder = new File(localFolderPath);
  456. if (localFolder.exists()) {
  457. // stage 1: remove the local files already registered in the files database
  458. Vector<OCFile> files = getFolderContent(folder.getFileId(), false);
  459. if (files != null) {
  460. for (OCFile file : files) {
  461. if (file.isFolder()) {
  462. success &= removeLocalFolder(file);
  463. } else {
  464. if (file.isDown()) {
  465. File localFile = new File(file.getStoragePath());
  466. success &= localFile.delete();
  467. if (success) {
  468. // notify MediaScanner about removed file
  469. deleteFileInMediaScan(file.getStoragePath());
  470. file.setStoragePath(null);
  471. saveFile(file);
  472. }
  473. }
  474. }
  475. }
  476. }
  477. // stage 2: remove the folder itself and any local file inside out of sync;
  478. // for instance, after clearing the app cache or reinstalling
  479. success &= removeLocalFolder(localFolder);
  480. }
  481. return success;
  482. }
  483. private boolean removeLocalFolder(File localFolder) {
  484. boolean success = true;
  485. File[] localFiles = localFolder.listFiles();
  486. if (localFiles != null) {
  487. for (File localFile : localFiles) {
  488. if (localFile.isDirectory()) {
  489. success &= removeLocalFolder(localFile);
  490. } else {
  491. String path = localFile.getAbsolutePath();
  492. success &= localFile.delete();
  493. }
  494. }
  495. }
  496. success &= localFolder.delete();
  497. return success;
  498. }
  499. /**
  500. * Updates database and file system for a file or folder that was moved to a different location.
  501. *
  502. * TODO explore better (faster) implementations
  503. * TODO throw exceptions up !
  504. */
  505. public void moveLocalFile(OCFile file, String targetPath, String targetParentPath) {
  506. if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) {
  507. OCFile targetParent = getFileByPath(targetParentPath);
  508. if (targetParent == null) {
  509. throw new IllegalStateException("Parent folder of the target path does not exist!!");
  510. }
  511. /// 1. get all the descendants of the moved element in a single QUERY
  512. Cursor c = null;
  513. if (getContentProviderClient() != null) {
  514. try {
  515. c = getContentProviderClient().query(
  516. ProviderTableMeta.CONTENT_URI,
  517. null,
  518. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ",
  519. new String[]{mAccount.name, file.getRemotePath() + "%"},
  520. ProviderTableMeta.FILE_PATH + " ASC "
  521. );
  522. } catch (RemoteException e) {
  523. Log_OC.e(TAG, e.getMessage(), e);
  524. }
  525. } else {
  526. c = getContentResolver().query(
  527. ProviderTableMeta.CONTENT_URI,
  528. null,
  529. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ",
  530. new String[]{mAccount.name, file.getRemotePath() + "%"},
  531. ProviderTableMeta.FILE_PATH + " ASC "
  532. );
  533. }
  534. /// 2. prepare a batch of update operations to change all the descendants
  535. ArrayList<ContentProviderOperation> operations = new ArrayList<>(c.getCount());
  536. String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
  537. List<String> originalPathsToTriggerMediaScan = new ArrayList<String>();
  538. List<String> newPathsToTriggerMediaScan = new ArrayList<String>();
  539. if (c.moveToFirst()) {
  540. int lengthOfOldPath = file.getRemotePath().length();
  541. int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
  542. do {
  543. ContentValues cv = new ContentValues(); // keep construction in the loop
  544. OCFile child = createFileInstance(c);
  545. cv.put(
  546. ProviderTableMeta.FILE_PATH,
  547. targetPath + child.getRemotePath().substring(lengthOfOldPath)
  548. );
  549. if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) {
  550. // update link to downloaded content - but local move is not done here!
  551. String targetLocalPath = defaultSavePath + targetPath +
  552. child.getStoragePath().substring(lengthOfOldStoragePath);
  553. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath);
  554. originalPathsToTriggerMediaScan.add(child.getStoragePath());
  555. newPathsToTriggerMediaScan.add(targetLocalPath);
  556. }
  557. if (child.getRemotePath().equals(file.getRemotePath())) {
  558. cv.put(ProviderTableMeta.FILE_PARENT, targetParent.getFileId());
  559. }
  560. operations.add(
  561. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  562. withValues(cv).
  563. withSelection(
  564. ProviderTableMeta._ID + "=?",
  565. new String[]{String.valueOf(child.getFileId())}
  566. )
  567. .build());
  568. } while (c.moveToNext());
  569. }
  570. c.close();
  571. /// 3. apply updates in batch
  572. try {
  573. if (getContentResolver() != null) {
  574. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  575. } else {
  576. getContentProviderClient().applyBatch(operations);
  577. }
  578. } catch (Exception e) {
  579. Log_OC.e(TAG, "Fail to update " + file.getFileId() + " and descendants in database", e);
  580. }
  581. /// 4. move in local file system
  582. String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
  583. String targetLocalPath = defaultSavePath + targetPath;
  584. File localFile = new File(originalLocalPath);
  585. boolean renamed = false;
  586. if (localFile.exists()) {
  587. File targetFile = new File(targetLocalPath);
  588. File targetFolder = targetFile.getParentFile();
  589. if (!targetFolder.exists()) {
  590. targetFolder.mkdirs();
  591. }
  592. renamed = localFile.renameTo(targetFile);
  593. }
  594. if (renamed) {
  595. Iterator<String> it = originalPathsToTriggerMediaScan.iterator();
  596. while (it.hasNext()) {
  597. // Notify MediaScanner about removed file
  598. deleteFileInMediaScan(it.next());
  599. }
  600. it = newPathsToTriggerMediaScan.iterator();
  601. while (it.hasNext()) {
  602. // Notify MediaScanner about new file/folder
  603. triggerMediaScan(it.next());
  604. }
  605. }
  606. }
  607. }
  608. public void copyLocalFile(OCFile file, String targetPath) {
  609. if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) {
  610. String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
  611. File localFile = new File(localPath);
  612. boolean copied = false;
  613. String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
  614. if (localFile.exists()) {
  615. File targetFile = new File(defaultSavePath + targetPath);
  616. File targetFolder = targetFile.getParentFile();
  617. if (!targetFolder.exists()) {
  618. targetFolder.mkdirs();
  619. }
  620. copied = FileStorageUtils.copyFile(localFile, targetFile);
  621. }
  622. Log_OC.d(TAG, "Local file COPIED : " + copied);
  623. }
  624. }
  625. public void migrateStoredFiles(String srcPath, String dstPath) throws Exception {
  626. Cursor cursor = null;
  627. if (getContentResolver() != null) {
  628. cursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI_FILE,
  629. null,
  630. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL",
  631. null,
  632. null);
  633. } else {
  634. try {
  635. cursor = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI_FILE,
  636. new String[]{ProviderTableMeta._ID, ProviderTableMeta.FILE_STORAGE_PATH},
  637. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL",
  638. null,
  639. null);
  640. } catch (RemoteException e) {
  641. Log_OC.e(TAG, e.getMessage(), e);
  642. throw e;
  643. }
  644. } catch (IOException ex) {
  645. ret = false;
  646. } finally {
  647. if (in != null) {
  648. try {
  649. in.close();
  650. } catch (IOException e) {
  651. Log_OC.d(TAG, e.getMessage(), e);
  652. }
  653. }
  654. if (out != null) {
  655. try {
  656. out.close();
  657. } catch (IOException e) {
  658. Log_OC.d(TAG, e.getMessage(), e);
  659. }
  660. }
  661. }
  662. ArrayList<ContentProviderOperation> operations = new ArrayList<>(cursor.getCount());
  663. if (cursor.moveToFirst()) {
  664. do {
  665. ContentValues cv = new ContentValues();
  666. long fileId = cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID));
  667. String oldFileStoragePath = cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
  668. if (oldFileStoragePath.startsWith(srcPath)) {
  669. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, oldFileStoragePath.replaceFirst(srcPath, dstPath));
  670. operations.add(
  671. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  672. withValues(cv).
  673. withSelection(ProviderTableMeta._ID + "=?", new String[]{String.valueOf(fileId)})
  674. .build());
  675. }
  676. } while (cursor.moveToNext());
  677. }
  678. cursor.close();
  679. /// 3. apply updates in batch
  680. if (getContentResolver() != null) {
  681. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  682. } else {
  683. getContentProviderClient().applyBatch(operations);
  684. }
  685. }
  686. private Vector<OCFile> getFolderContent(long parentId, boolean onlyOnDevice) {
  687. Vector<OCFile> ret = new Vector<>();
  688. Uri req_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(parentId));
  689. Cursor c = null;
  690. if (getContentProviderClient() != null) {
  691. try {
  692. c = getContentProviderClient().query(
  693. req_uri,
  694. null,
  695. ProviderTableMeta.FILE_PARENT + "=?",
  696. new String[]{String.valueOf(parentId)},
  697. null
  698. );
  699. } catch (RemoteException e) {
  700. Log_OC.e(TAG, e.getMessage(), e);
  701. return ret;
  702. }
  703. } else {
  704. c = getContentResolver().query(
  705. req_uri,
  706. null,
  707. ProviderTableMeta.FILE_PARENT + "=?",
  708. new String[]{String.valueOf(parentId)},
  709. null
  710. );
  711. }
  712. if (c != null && c.moveToFirst()) {
  713. do {
  714. OCFile child = createFileInstance(c);
  715. if (child.isFolder() || !onlyOnDevice || child.isDown()) {
  716. ret.add(child);
  717. }
  718. } while (c.moveToNext());
  719. c.close();
  720. }
  721. Collections.sort(ret);
  722. return ret;
  723. }
  724. private OCFile createRootDir() {
  725. OCFile file = new OCFile(OCFile.ROOT_PATH);
  726. file.setMimetype(MimeType.DIRECTORY);
  727. file.setParentId(FileDataStorageManager.ROOT_PARENT_ID);
  728. saveFile(file);
  729. return file;
  730. }
  731. private boolean fileExists(String cmp_key, String value) {
  732. Cursor c;
  733. if (getContentResolver() != null) {
  734. c = getContentResolver()
  735. .query(ProviderTableMeta.CONTENT_URI,
  736. null,
  737. cmp_key + AND
  738. + ProviderTableMeta.FILE_ACCOUNT_OWNER
  739. + "=?",
  740. new String[]{value, mAccount.name}, null);
  741. } else {
  742. try {
  743. c = getContentProviderClient().query(
  744. ProviderTableMeta.CONTENT_URI,
  745. null,
  746. cmp_key + AND
  747. + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  748. new String[]{value, mAccount.name}, null);
  749. } catch (RemoteException e) {
  750. Log_OC.e(TAG, "Couldn't determine file existance, assuming non existance: " + e.getMessage(), e);
  751. return false;
  752. }
  753. }
  754. boolean retval = c.moveToFirst();
  755. c.close();
  756. return retval;
  757. }
  758. private Cursor getFileCursorForValue(String key, String value) {
  759. Cursor c = null;
  760. if (getContentResolver() != null) {
  761. c = getContentResolver()
  762. .query(ProviderTableMeta.CONTENT_URI,
  763. null,
  764. key + AND
  765. + ProviderTableMeta.FILE_ACCOUNT_OWNER
  766. + "=?",
  767. new String[]{value, mAccount.name}, null);
  768. } else {
  769. try {
  770. c = getContentProviderClient().query(
  771. ProviderTableMeta.CONTENT_URI,
  772. null,
  773. key + AND + ProviderTableMeta.FILE_ACCOUNT_OWNER
  774. + "=?", new String[]{value, mAccount.name},
  775. null);
  776. } catch (RemoteException e) {
  777. Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e);
  778. c = null;
  779. }
  780. }
  781. return c;
  782. }
  783. private OCFile createFileInstance(Cursor c) {
  784. OCFile file = null;
  785. if (c != null) {
  786. file = new OCFile(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)));
  787. file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  788. file.setParentId(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_PARENT)));
  789. file.setMimetype(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
  790. if (!file.isFolder()) {
  791. file.setStoragePath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
  792. if (file.getStoragePath() == null) {
  793. // try to find existing file and bind it with current account;
  794. // with the current update of SynchronizeFolderOperation, this won't be
  795. // necessary anymore after a full synchronization of the account
  796. File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
  797. if (f.exists()) {
  798. file.setStoragePath(f.getAbsolutePath());
  799. file.setLastSyncDateForData(f.lastModified());
  800. }
  801. }
  802. }
  803. file.setFileLength(c.getLong(c
  804. .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
  805. file.setCreationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CREATION)));
  806. file.setModificationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
  807. file.setModificationTimestampAtLastSyncForData(c.getLong(
  808. c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA)));
  809. file.setLastSyncDateForProperties(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
  810. file.setLastSyncDateForData(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA)));
  811. file.setFavorite(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false);
  812. file.setEtag(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG)));
  813. file.setShareViaLink(c.getInt(
  814. c.getColumnIndex(ProviderTableMeta.FILE_SHARED_VIA_LINK)) == 1 ? true : false);
  815. file.setShareWithSharee(c.getInt(
  816. c.getColumnIndex(ProviderTableMeta.FILE_SHARED_WITH_SHAREE)) == 1 ? true : false);
  817. file.setPublicLink(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PUBLIC_LINK)));
  818. file.setPermissions(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PERMISSIONS)));
  819. file.setRemoteId(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID)));
  820. file.setNeedsUpdateThumbnail(c.getInt(
  821. c.getColumnIndex(ProviderTableMeta.FILE_UPDATE_THUMBNAIL)) == 1 ? true : false);
  822. file.setDownloading(c.getInt(
  823. c.getColumnIndex(ProviderTableMeta.FILE_IS_DOWNLOADING)) == 1 ? true : false);
  824. file.setEtagInConflict(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG_IN_CONFLICT)));
  825. }
  826. return file;
  827. }
  828. // Methods for Shares
  829. public boolean saveShare(OCShare share) {
  830. boolean overriden = false;
  831. ContentValues cv = new ContentValues();
  832. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  833. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  834. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  835. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  836. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  837. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  838. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  839. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  840. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  841. cv.put(
  842. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  843. share.getSharedWithDisplayName()
  844. );
  845. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  846. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  847. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  848. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  849. if (shareExistsForRemoteId(share.getRemoteId())) {// for renamed files; no more delete and create
  850. overriden = true;
  851. if (getContentResolver() != null) {
  852. getContentResolver().update(ProviderTableMeta.CONTENT_URI_SHARE, cv,
  853. ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  854. new String[]{String.valueOf(share.getRemoteId())});
  855. } else {
  856. try {
  857. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_SHARE,
  858. cv, ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  859. new String[]{String.valueOf(share.getRemoteId())});
  860. } catch (RemoteException e) {
  861. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  862. }
  863. }
  864. } else {
  865. Uri result_uri = null;
  866. if (getContentResolver() != null) {
  867. result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_SHARE, cv);
  868. } else {
  869. try {
  870. result_uri = getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_SHARE, cv);
  871. } catch (RemoteException e) {
  872. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  873. }
  874. }
  875. if (result_uri != null) {
  876. long new_id = Long.parseLong(result_uri.getPathSegments().get(1));
  877. share.setId(new_id);
  878. }
  879. }
  880. return overriden;
  881. }
  882. /**
  883. * Retrieves an stored {@link OCShare} given its id.
  884. *
  885. * @param id Identifier.
  886. * @return Stored {@link OCShare} given its id.
  887. */
  888. public OCShare getShareById(long id) {
  889. OCShare share = null;
  890. Cursor c = getShareCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
  891. if (c != null) {
  892. if (c.moveToFirst()) {
  893. share = createShareInstance(c);
  894. }
  895. c.close();
  896. }
  897. return share;
  898. }
  899. /**
  900. * Checks the existance of an stored {@link OCShare} matching the given remote id (not to be confused with
  901. * the local id) in the current account.
  902. *
  903. * @param remoteId Remote of the share in the server.
  904. * @return 'True' if a matching {@link OCShare} is stored in the current account.
  905. */
  906. private boolean shareExistsForRemoteId(long remoteId) {
  907. return shareExistsForValue(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId));
  908. }
  909. /**
  910. * Checks the existance of an stored {@link OCShare} in the current account
  911. * matching a given column and a value for that column
  912. *
  913. * @param key Name of the column to match.
  914. * @param value Value of the column to match.
  915. * @return 'True' if a matching {@link OCShare} is stored in the current account.
  916. */
  917. private boolean shareExistsForValue(String key, String value) {
  918. Cursor c = getShareCursorForValue(key, value);
  919. boolean retval = c.moveToFirst();
  920. c.close();
  921. return retval;
  922. }
  923. /**
  924. * Gets a {@link Cursor} for an stored {@link OCShare} in the current account
  925. * matching a given column and a value for that column
  926. *
  927. * @param key Name of the column to match.
  928. * @param value Value of the column to match.
  929. * @return 'True' if a matching {@link OCShare} is stored in the current account.
  930. */
  931. private Cursor getShareCursorForValue(String key, String value) {
  932. Cursor c;
  933. if (getContentResolver() != null) {
  934. c = getContentResolver()
  935. .query(ProviderTableMeta.CONTENT_URI_SHARE,
  936. null,
  937. key + AND
  938. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
  939. new String[]{value, mAccount.name},
  940. null
  941. );
  942. } else {
  943. try {
  944. c = getContentProviderClient().query(
  945. ProviderTableMeta.CONTENT_URI_SHARE,
  946. null,
  947. key + AND + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
  948. new String[]{value, mAccount.name},
  949. null
  950. );
  951. } catch (RemoteException e) {
  952. Log_OC.w(TAG, "Could not get details, assuming share does not exist: "+ e.getMessage());
  953. c = null;
  954. }
  955. }
  956. return c;
  957. }
  958. /**
  959. * Get first share bound to a file with a known path and given {@link ShareType}.
  960. *
  961. * @param path Path of the file.
  962. * @param type Type of the share to get
  963. * @param shareWith Target of the share. Ignored in type is {@link ShareType#PUBLIC_LINK}
  964. * @return First {@link OCShare} instance found in DB bound to the file in 'path'
  965. */
  966. public OCShare getFirstShareByPathAndType(String path, ShareType type, String shareWith) {
  967. Cursor cursor = null;
  968. if (shareWith == null) {
  969. shareWith = "";
  970. }
  971. String selection = ProviderTableMeta.OCSHARES_PATH + AND
  972. + ProviderTableMeta.OCSHARES_SHARE_TYPE + AND
  973. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?" ;
  974. if (!ShareType.PUBLIC_LINK.equals(type)) {
  975. selection += " AND " + ProviderTableMeta.OCSHARES_SHARE_WITH + "=?";
  976. }
  977. String [] selectionArgs;
  978. if (ShareType.PUBLIC_LINK.equals(type)) {
  979. selectionArgs = new String[]{
  980. path,
  981. Integer.toString(type.getValue()),
  982. mAccount.name
  983. };
  984. } else {
  985. selectionArgs = new String[]{
  986. path,
  987. Integer.toString(type.getValue()),
  988. mAccount.name,
  989. shareWith
  990. };
  991. }
  992. if (getContentResolver() != null) {
  993. cursor = getContentResolver().query(
  994. ProviderTableMeta.CONTENT_URI_SHARE,
  995. null,
  996. selection, selectionArgs,
  997. null);
  998. } else {
  999. try {
  1000. cursor = getContentProviderClient().query(
  1001. ProviderTableMeta.CONTENT_URI_SHARE,
  1002. null,
  1003. selection, selectionArgs,
  1004. null);
  1005. } catch (RemoteException e) {
  1006. Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e);
  1007. cursor = null;
  1008. }
  1009. }
  1010. OCShare share = null;
  1011. if (cursor != null && cursor.moveToFirst()) {
  1012. share = createShareInstance(cursor);
  1013. cursor.close();
  1014. }
  1015. return share;
  1016. }
  1017. private OCShare createShareInstance(Cursor c) {
  1018. OCShare share = null;
  1019. if (c != null) {
  1020. share = new OCShare(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_PATH)));
  1021. share.setId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  1022. share.setFileSource(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ITEM_SOURCE)));
  1023. share.setShareType(ShareType.fromValue(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_TYPE))));
  1024. share.setShareWith(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH)));
  1025. share.setPermissions(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_PERMISSIONS)));
  1026. share.setSharedDate(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARED_DATE)));
  1027. share.setExpirationDate(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_EXPIRATION_DATE)));
  1028. share.setToken(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_TOKEN)));
  1029. share.setSharedWithDisplayName(
  1030. c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME)));
  1031. share.setIsFolder(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_IS_DIRECTORY)) == 1);
  1032. share.setUserId(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_USER_ID)));
  1033. share.setIdRemoteShared(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED)));
  1034. }
  1035. return share;
  1036. }
  1037. private void resetShareFlagsInAllFiles() {
  1038. ContentValues cv = new ContentValues();
  1039. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1040. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1041. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1042. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  1043. String[] whereArgs = new String[]{mAccount.name};
  1044. if (getContentResolver() != null) {
  1045. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1046. } else {
  1047. try {
  1048. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1049. } catch (RemoteException e) {
  1050. Log_OC.e(TAG, "Exception in resetShareFlagsInAllFiles" + e.getMessage(), e);
  1051. }
  1052. }
  1053. }
  1054. private void resetShareFlagsInFolder(OCFile folder) {
  1055. ContentValues cv = new ContentValues();
  1056. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1057. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1058. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1059. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PARENT + "=?";
  1060. String [] whereArgs = new String[] { mAccount.name , String.valueOf(folder.getFileId()) };
  1061. if (getContentResolver() != null) {
  1062. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1063. } else {
  1064. try {
  1065. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1066. } catch (RemoteException e) {
  1067. Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e);
  1068. }
  1069. }
  1070. }
  1071. private void resetShareFlagInAFile(String filePath){
  1072. ContentValues cv = new ContentValues();
  1073. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1074. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1075. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1076. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH+ "=?";
  1077. String [] whereArgs = new String[] { mAccount.name , filePath };
  1078. if (getContentResolver() != null) {
  1079. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1080. } else {
  1081. try {
  1082. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1083. } catch (RemoteException e) {
  1084. Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e);
  1085. }
  1086. }
  1087. }
  1088. private void cleanShares() {
  1089. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1090. String[] whereArgs = new String[]{mAccount.name};
  1091. if (getContentResolver() != null) {
  1092. getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
  1093. } else {
  1094. try {
  1095. getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
  1096. } catch (RemoteException e) {
  1097. Log_OC.e(TAG, "Exception in cleanShares" + e.getMessage(), e);
  1098. }
  1099. }
  1100. }
  1101. public void saveShares(Collection<OCShare> shares) {
  1102. cleanShares();
  1103. if (shares != null) {
  1104. ArrayList<ContentProviderOperation> operations =
  1105. new ArrayList<ContentProviderOperation>(shares.size());
  1106. // prepare operations to insert or update files to save in the given folder
  1107. for (OCShare share : shares) {
  1108. ContentValues cv = new ContentValues();
  1109. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  1110. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  1111. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  1112. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  1113. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  1114. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  1115. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  1116. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  1117. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  1118. cv.put(
  1119. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  1120. share.getSharedWithDisplayName()
  1121. );
  1122. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  1123. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  1124. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  1125. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  1126. if (shareExistsForRemoteId(share.getRemoteId())) {
  1127. // updating an existing file
  1128. operations.add(
  1129. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
  1130. withValues(cv).
  1131. withSelection(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  1132. new String[]{String.valueOf(share.getRemoteId())})
  1133. .build());
  1134. } else {
  1135. // adding a new file
  1136. operations.add(
  1137. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).
  1138. withValues(cv).
  1139. build()
  1140. );
  1141. }
  1142. }
  1143. // apply operations in batch
  1144. if (operations.size() > 0) {
  1145. @SuppressWarnings("unused")
  1146. ContentProviderResult[] results = null;
  1147. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1148. try {
  1149. if (getContentResolver() != null) {
  1150. results = getContentResolver().applyBatch(MainApp.getAuthority(),
  1151. operations);
  1152. } else {
  1153. results = getContentProviderClient().applyBatch(operations);
  1154. }
  1155. } catch (OperationApplicationException | RemoteException e) {
  1156. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1157. }
  1158. }
  1159. }
  1160. }
  1161. public void updateSharedFiles(Collection<OCFile> sharedFiles) {
  1162. resetShareFlagsInAllFiles();
  1163. if (sharedFiles != null) {
  1164. ArrayList<ContentProviderOperation> operations = new ArrayList<>(sharedFiles.size());
  1165. // prepare operations to insert or update files to save in the given folder
  1166. for (OCFile file : sharedFiles) {
  1167. ContentValues cv = new ContentValues();
  1168. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  1169. cv.put(
  1170. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  1171. file.getModificationTimestampAtLastSyncForData()
  1172. );
  1173. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  1174. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  1175. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  1176. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  1177. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  1178. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  1179. if (!file.isFolder()) {
  1180. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  1181. }
  1182. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  1183. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  1184. cv.put(
  1185. ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA,
  1186. file.getLastSyncDateForData()
  1187. );
  1188. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.isFavorite() ? 1 : 0);
  1189. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  1190. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
  1191. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
  1192. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
  1193. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  1194. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  1195. cv.put(
  1196. ProviderTableMeta.FILE_UPDATE_THUMBNAIL,
  1197. file.needsUpdateThumbnail() ? 1 : 0
  1198. );
  1199. cv.put(
  1200. ProviderTableMeta.FILE_IS_DOWNLOADING,
  1201. file.isDownloading() ? 1 : 0
  1202. );
  1203. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
  1204. boolean existsByPath = fileExists(file.getRemotePath());
  1205. if (existsByPath || fileExists(file.getFileId())) {
  1206. // updating an existing file
  1207. operations.add(
  1208. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  1209. withValues(cv).
  1210. withSelection(ProviderTableMeta._ID + "=?",
  1211. new String[]{String.valueOf(file.getFileId())})
  1212. .build());
  1213. } else {
  1214. // adding a new file
  1215. operations.add(
  1216. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).
  1217. withValues(cv).
  1218. build()
  1219. );
  1220. }
  1221. }
  1222. // apply operations in batch
  1223. if (operations.size() > 0) {
  1224. @SuppressWarnings("unused")
  1225. ContentProviderResult[] results = null;
  1226. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1227. try {
  1228. if (getContentResolver() != null) {
  1229. results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1230. } else {
  1231. results = getContentProviderClient().applyBatch(operations);
  1232. }
  1233. } catch (OperationApplicationException | RemoteException e) {
  1234. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1235. }
  1236. }
  1237. }
  1238. }
  1239. public void removeShare(OCShare share) {
  1240. Uri share_uri = ProviderTableMeta.CONTENT_URI_SHARE;
  1241. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND +
  1242. ProviderTableMeta._ID + "=?";
  1243. String [] whereArgs = new String[]{mAccount.name, Long.toString(share.getId())};
  1244. if (getContentProviderClient() != null) {
  1245. try {
  1246. getContentProviderClient().delete(share_uri, where, whereArgs);
  1247. } catch (RemoteException e) {
  1248. Log_OC.d(TAG, e.getMessage(), e);
  1249. }
  1250. } else {
  1251. getContentResolver().delete(share_uri, where, whereArgs);
  1252. }
  1253. }
  1254. public void saveSharesDB(ArrayList<OCShare> shares) {
  1255. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1256. // Reset flags & Remove shares for this files
  1257. String filePath = "";
  1258. for (OCShare share: shares) {
  1259. if (filePath != share.getPath()){
  1260. filePath = share.getPath();
  1261. resetShareFlagInAFile(filePath);
  1262. operations = prepareRemoveSharesInFile(filePath, operations);
  1263. }
  1264. }
  1265. // Add operations to insert shares
  1266. operations = prepareInsertShares(shares, operations);
  1267. // apply operations in batch
  1268. if (operations.size() > 0) {
  1269. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1270. try {
  1271. if (getContentResolver() != null) {
  1272. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1273. } else {
  1274. getContentProviderClient().applyBatch(operations);
  1275. }
  1276. } catch (OperationApplicationException | RemoteException e) {
  1277. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1278. }
  1279. }
  1280. // // TODO: review if it is needed
  1281. // // Update shared files
  1282. // ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
  1283. //
  1284. // for (OCShare share : shares) {
  1285. // // Get the path
  1286. // String path = share.getPath();
  1287. // if (share.isFolder()) {
  1288. // path = path + FileUtils.PATH_SEPARATOR;
  1289. // }
  1290. //
  1291. // // Update OCFile with data from share: ShareByLink, publicLink and
  1292. // OCFile file = getFileByPath(path);
  1293. // if (file != null) {
  1294. // if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
  1295. // file.setShareViaLink(true);
  1296. // sharedFiles.add(file);
  1297. // }
  1298. // }
  1299. // }
  1300. //
  1301. // // TODO: Review
  1302. // updateSharedFiles(sharedFiles);
  1303. }
  1304. public void removeSharesForFile(String remotePath) {
  1305. resetShareFlagInAFile(remotePath);
  1306. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1307. operations = prepareRemoveSharesInFile(remotePath, operations);
  1308. // apply operations in batch
  1309. if (operations.size() > 0) {
  1310. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1311. try {
  1312. if (getContentResolver() != null) {
  1313. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1314. } else {
  1315. getContentProviderClient().applyBatch(operations);
  1316. }
  1317. } catch (OperationApplicationException | RemoteException e) {
  1318. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1319. }
  1320. }
  1321. }
  1322. public void saveSharesInFolder(ArrayList<OCShare> shares, OCFile folder) {
  1323. resetShareFlagsInFolder(folder);
  1324. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1325. operations = prepareRemoveSharesInFolder(folder, operations);
  1326. if (shares != null) {
  1327. // prepare operations to insert or update files to save in the given folder
  1328. operations = prepareInsertShares(shares, operations);
  1329. }
  1330. // apply operations in batch
  1331. if (operations.size() > 0) {
  1332. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1333. try {
  1334. if (getContentResolver() != null) {
  1335. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1336. } else {
  1337. getContentProviderClient().applyBatch(operations);
  1338. }
  1339. } catch (OperationApplicationException | RemoteException e) {
  1340. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1341. }
  1342. }
  1343. }
  1344. /**
  1345. * Prepare operations to insert or update files to save in the given folder
  1346. * @param shares List of shares to insert
  1347. * @param operations List of operations
  1348. * @return
  1349. */
  1350. private ArrayList<ContentProviderOperation> prepareInsertShares(
  1351. ArrayList<OCShare> shares, ArrayList<ContentProviderOperation> operations) {
  1352. if (shares != null) {
  1353. // prepare operations to insert or update files to save in the given folder
  1354. for (OCShare share : shares) {
  1355. ContentValues cv = new ContentValues();
  1356. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  1357. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  1358. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  1359. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  1360. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  1361. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  1362. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  1363. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  1364. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  1365. cv.put(
  1366. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  1367. share.getSharedWithDisplayName()
  1368. );
  1369. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  1370. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  1371. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  1372. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  1373. // adding a new share resource
  1374. operations.add(
  1375. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).
  1376. withValues(cv).
  1377. build()
  1378. );
  1379. }
  1380. }
  1381. return operations;
  1382. }
  1383. private ArrayList<ContentProviderOperation> prepareRemoveSharesInFolder(
  1384. OCFile folder, ArrayList<ContentProviderOperation> preparedOperations) {
  1385. if (folder != null) {
  1386. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1387. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1388. String [] whereArgs = new String[]{ "", mAccount.name };
  1389. Vector<OCFile> files = getFolderContent(folder, false);
  1390. for (OCFile file : files) {
  1391. whereArgs[0] = file.getRemotePath();
  1392. preparedOperations.add(
  1393. ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE).
  1394. withSelection(where, whereArgs).
  1395. build()
  1396. );
  1397. }
  1398. }
  1399. return preparedOperations;
  1400. }
  1401. private ArrayList<ContentProviderOperation> prepareRemoveSharesInFile(
  1402. String filePath, ArrayList<ContentProviderOperation> preparedOperations) {
  1403. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1404. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1405. String[] whereArgs = new String[]{filePath, mAccount.name};
  1406. preparedOperations.add(
  1407. ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE).
  1408. withSelection(where, whereArgs).
  1409. build()
  1410. );
  1411. return preparedOperations;
  1412. }
  1413. public ArrayList<OCShare> getSharesWithForAFile(String filePath, String accountName){
  1414. // Condition
  1415. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1416. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND
  1417. + " (" + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR "
  1418. + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR "
  1419. + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? ) ";
  1420. String [] whereArgs = new String[]{ filePath, accountName ,
  1421. Integer.toString(ShareType.USER.getValue()),
  1422. Integer.toString(ShareType.GROUP.getValue()),
  1423. Integer.toString(ShareType.FEDERATED.getValue())};
  1424. Cursor cursor = null;
  1425. if (getContentResolver() != null) {
  1426. cursor = getContentResolver().query(
  1427. ProviderTableMeta.CONTENT_URI_SHARE,
  1428. null, where, whereArgs, null);
  1429. } else {
  1430. try {
  1431. cursor = getContentProviderClient().query(
  1432. ProviderTableMeta.CONTENT_URI_SHARE,
  1433. null, where, whereArgs, null);
  1434. } catch (RemoteException e) {
  1435. Log_OC.e(TAG, "Could not get list of shares with: " + e.getMessage(), e);
  1436. cursor = null;
  1437. }
  1438. }
  1439. ArrayList<OCShare> shares = new ArrayList<OCShare>();
  1440. OCShare share = null;
  1441. if (cursor != null && cursor.moveToFirst()) {
  1442. do {
  1443. share = createShareInstance(cursor);
  1444. shares.add(share);
  1445. } while (cursor.moveToNext());
  1446. cursor.close();
  1447. }
  1448. return shares;
  1449. }
  1450. public static void triggerMediaScan(String path) {
  1451. if (path != null) {
  1452. Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  1453. intent.setData(Uri.fromFile(new File(path)));
  1454. MainApp.getAppContext().sendBroadcast(intent);
  1455. }
  1456. }
  1457. public void deleteFileInMediaScan(String path) {
  1458. String mimetypeString = FileStorageUtils.getMimeTypeFromName(path);
  1459. ContentResolver contentResolver = getContentResolver();
  1460. if (contentResolver != null) {
  1461. if (MimeTypeUtil.isImage(mimetypeString)) {
  1462. // Images
  1463. contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  1464. MediaStore.Images.Media.DATA + "=?", new String[]{path});
  1465. } else if (MimeTypeUtil.isAudio(mimetypeString)) {
  1466. // Audio
  1467. contentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  1468. MediaStore.Audio.Media.DATA + "=?", new String[]{path});
  1469. } else if (MimeTypeUtil.isVideo(mimetypeString)) {
  1470. // Video
  1471. contentResolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  1472. MediaStore.Video.Media.DATA + "=?", new String[]{path});
  1473. }
  1474. } else {
  1475. ContentProviderClient contentProviderClient = getContentProviderClient();
  1476. try {
  1477. if (MimeTypeUtil.isImage(mimetypeString)) {
  1478. // Images
  1479. contentProviderClient.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  1480. MediaStore.Images.Media.DATA + "=?", new String[]{path});
  1481. } else if (MimeTypeUtil.isAudio(mimetypeString)) {
  1482. // Audio
  1483. contentProviderClient.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  1484. MediaStore.Audio.Media.DATA + "=?", new String[]{path});
  1485. } else if (MimeTypeUtil.isVideo(mimetypeString)) {
  1486. // Video
  1487. contentProviderClient.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  1488. MediaStore.Video.Media.DATA + "=?", new String[]{path});
  1489. }
  1490. } catch (RemoteException e) {
  1491. Log_OC.e(TAG, "Exception deleting media file in MediaStore " + e.getMessage(), e);
  1492. }
  1493. }
  1494. }
  1495. public void saveConflict(OCFile file, String etagInConflict) {
  1496. if (!file.isDown()) {
  1497. etagInConflict = null;
  1498. }
  1499. ContentValues cv = new ContentValues();
  1500. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, etagInConflict);
  1501. int updated = 0;
  1502. if (getContentResolver() != null) {
  1503. updated = getContentResolver().update(
  1504. ProviderTableMeta.CONTENT_URI_FILE,
  1505. cv,
  1506. ProviderTableMeta._ID + "=?",
  1507. new String[] { String.valueOf(file.getFileId())}
  1508. );
  1509. } else {
  1510. try {
  1511. updated = getContentProviderClient().update(
  1512. ProviderTableMeta.CONTENT_URI_FILE,
  1513. cv,
  1514. ProviderTableMeta._ID + "=?",
  1515. new String[]{String.valueOf(file.getFileId())}
  1516. );
  1517. } catch (RemoteException e) {
  1518. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1519. }
  1520. }
  1521. Log_OC.d(TAG, "Number of files updated with CONFLICT: " + updated);
  1522. if (updated > 0) {
  1523. if (etagInConflict != null) {
  1524. /// set conflict in all ancestor folders
  1525. long parentId = file.getParentId();
  1526. Set<String> ancestorIds = new HashSet<>();
  1527. while (parentId != FileDataStorageManager.ROOT_PARENT_ID) {
  1528. ancestorIds.add(Long.toString(parentId));
  1529. parentId = getFileById(parentId).getParentId();
  1530. }
  1531. if (ancestorIds.size() > 0) {
  1532. final StringBuffer whereBuffer = new StringBuffer();
  1533. whereBuffer.append(ProviderTableMeta._ID).append(" IN (");
  1534. for (int i = 0; i < ancestorIds.size() - 1; i++) {
  1535. whereBuffer.append("?,");
  1536. }
  1537. whereBuffer.append("?)");
  1538. if (getContentResolver() != null) {
  1539. updated = getContentResolver().update(
  1540. ProviderTableMeta.CONTENT_URI_FILE,
  1541. cv,
  1542. whereBuffer.toString(),
  1543. ancestorIds.toArray(new String[]{})
  1544. );
  1545. } else {
  1546. try {
  1547. updated = getContentProviderClient().update(
  1548. ProviderTableMeta.CONTENT_URI_FILE,
  1549. cv,
  1550. whereBuffer.toString(),
  1551. ancestorIds.toArray(new String[]{})
  1552. );
  1553. } catch (RemoteException e) {
  1554. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1555. }
  1556. }
  1557. } // else file is ROOT folder, no parent to set in conflict
  1558. } else {
  1559. /// update conflict in ancestor folders
  1560. // (not directly unset; maybe there are more conflicts below them)
  1561. String parentPath = file.getRemotePath();
  1562. if (parentPath.endsWith(OCFile.PATH_SEPARATOR)) {
  1563. parentPath = parentPath.substring(0, parentPath.length() - 1);
  1564. }
  1565. parentPath = parentPath.substring(0, parentPath.lastIndexOf(OCFile.PATH_SEPARATOR) + 1);
  1566. Log_OC.d(TAG, "checking parents to remove conflict; STARTING with " + parentPath);
  1567. while (parentPath.length() > 0) {
  1568. String whereForDescencentsInConflict =
  1569. ProviderTableMeta.FILE_ETAG_IN_CONFLICT + " IS NOT NULL AND " +
  1570. ProviderTableMeta.FILE_CONTENT_TYPE + " != 'DIR' AND " +
  1571. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1572. ProviderTableMeta.FILE_PATH + " LIKE ?";
  1573. Cursor descendentsInConflict = null;
  1574. if (getContentResolver() != null) {
  1575. descendentsInConflict = getContentResolver().query(
  1576. ProviderTableMeta.CONTENT_URI_FILE,
  1577. new String[]{ProviderTableMeta._ID},
  1578. whereForDescencentsInConflict,
  1579. new String[]{mAccount.name, parentPath + "%"},
  1580. null
  1581. );
  1582. } else {
  1583. try {
  1584. descendentsInConflict = getContentProviderClient().query(
  1585. ProviderTableMeta.CONTENT_URI_FILE,
  1586. new String[]{ProviderTableMeta._ID},
  1587. whereForDescencentsInConflict,
  1588. new String[]{mAccount.name, parentPath + "%"},
  1589. null
  1590. );
  1591. } catch (RemoteException e) {
  1592. Log_OC.e(TAG, "Failed querying for descendents in conflict " + e.getMessage(), e);
  1593. }
  1594. }
  1595. if (descendentsInConflict == null || descendentsInConflict.getCount() == 0) {
  1596. Log_OC.d(TAG, "NO MORE conflicts in " + parentPath);
  1597. if (getContentResolver() != null) {
  1598. updated = getContentResolver().update(
  1599. ProviderTableMeta.CONTENT_URI_FILE,
  1600. cv,
  1601. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1602. ProviderTableMeta.FILE_PATH + "=?",
  1603. new String[]{mAccount.name, parentPath}
  1604. );
  1605. } else {
  1606. try {
  1607. updated = getContentProviderClient().update(
  1608. ProviderTableMeta.CONTENT_URI_FILE,
  1609. cv,
  1610. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1611. ProviderTableMeta.FILE_PATH + "=?"
  1612. , new String[]{mAccount.name, parentPath}
  1613. );
  1614. } catch (RemoteException e) {
  1615. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1616. }
  1617. }
  1618. } else {
  1619. Log_OC.d(TAG, "STILL " + descendentsInConflict.getCount() + " in " + parentPath);
  1620. }
  1621. if (descendentsInConflict != null) {
  1622. descendentsInConflict.close();
  1623. }
  1624. parentPath = parentPath.substring(0, parentPath.length() - 1); // trim last /
  1625. parentPath = parentPath.substring(0, parentPath.lastIndexOf(OCFile.PATH_SEPARATOR) + 1);
  1626. Log_OC.d(TAG, "checking parents to remove conflict; NEXT " + parentPath);
  1627. }
  1628. }
  1629. }
  1630. }
  1631. public OCCapability saveCapabilities(OCCapability capability){
  1632. // Prepare capabilities data
  1633. ContentValues cv = new ContentValues();
  1634. cv.put(ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME, mAccount.name);
  1635. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MAYOR, capability.getVersionMayor());
  1636. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MINOR, capability.getVersionMinor());
  1637. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MICRO, capability.getVersionMicro());
  1638. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_STRING, capability.getVersionString());
  1639. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_EDITION, capability.getVersionEdition());
  1640. cv.put(ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL, capability.getCorePollinterval());
  1641. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED, capability.getFilesSharingApiEnabled().getValue());
  1642. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED,
  1643. capability.getFilesSharingPublicEnabled().getValue());
  1644. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED,
  1645. capability.getFilesSharingPublicPasswordEnforced().getValue());
  1646. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED,
  1647. capability.getFilesSharingPublicExpireDateEnabled().getValue());
  1648. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS,
  1649. capability.getFilesSharingPublicExpireDateDays());
  1650. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED,
  1651. capability.getFilesSharingPublicExpireDateEnforced().getValue());
  1652. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL,
  1653. capability.getFilesSharingPublicSendMail().getValue());
  1654. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD,
  1655. capability.getFilesSharingPublicUpload().getValue());
  1656. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL,
  1657. capability.getFilesSharingUserSendMail().getValue());
  1658. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_RESHARING, capability.getFilesSharingResharing().getValue());
  1659. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING,
  1660. capability.getFilesSharingFederationOutgoing().getValue());
  1661. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING,
  1662. capability.getFilesSharingFederationIncoming().getValue());
  1663. cv.put(ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING, capability.getFilesBigFileChuncking().getValue());
  1664. cv.put(ProviderTableMeta.CAPABILITIES_FILES_UNDELETE, capability.getFilesUndelete().getValue());
  1665. cv.put(ProviderTableMeta.CAPABILITIES_FILES_VERSIONING, capability.getFilesVersioning().getValue());
  1666. cv.put(ProviderTableMeta.CAPABILITIES_FILES_DROP, capability.getFilesFileDrop().getValue());
  1667. if (capabilityExists(mAccount.name)) {
  1668. if (getContentResolver() != null) {
  1669. getContentResolver().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv,
  1670. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?",
  1671. new String[]{mAccount.name});
  1672. } else {
  1673. try {
  1674. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1675. cv, ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?",
  1676. new String[]{mAccount.name});
  1677. } catch (RemoteException e) {
  1678. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  1679. }
  1680. }
  1681. } else {
  1682. Uri result_uri = null;
  1683. if (getContentResolver() != null) {
  1684. result_uri = getContentResolver().insert(
  1685. ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv);
  1686. } else {
  1687. try {
  1688. result_uri = getContentProviderClient().insert(
  1689. ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv);
  1690. } catch (RemoteException e) {
  1691. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  1692. }
  1693. }
  1694. if (result_uri != null) {
  1695. long new_id = Long.parseLong(result_uri.getPathSegments()
  1696. .get(1));
  1697. capability.setId(new_id);
  1698. capability.setAccountName(mAccount.name);
  1699. }
  1700. }
  1701. return capability;
  1702. }
  1703. private boolean capabilityExists(String accountName) {
  1704. Cursor c = getCapabilityCursorForAccount(accountName);
  1705. boolean exists = false;
  1706. if (c != null) {
  1707. exists = c.moveToFirst();
  1708. c.close();
  1709. }
  1710. return exists;
  1711. }
  1712. private Cursor getCapabilityCursorForAccount(String accountName){
  1713. Cursor c = null;
  1714. if (getContentResolver() != null) {
  1715. c = getContentResolver()
  1716. .query(ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1717. null,
  1718. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ",
  1719. new String[]{accountName}, null);
  1720. } else {
  1721. try {
  1722. c = getContentProviderClient().query(
  1723. ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1724. null,
  1725. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ",
  1726. new String[]{accountName}, null);
  1727. } catch (RemoteException e) {
  1728. Log_OC.e(TAG, "Couldn't determine capability existence, assuming non existance: " + e.getMessage(), e);
  1729. }
  1730. }
  1731. return c;
  1732. }
  1733. public OCCapability getCapability(String accountName){
  1734. OCCapability capability = null;
  1735. Cursor c = getCapabilityCursorForAccount(accountName);
  1736. if (c.moveToFirst()) {
  1737. capability = createCapabilityInstance(c);
  1738. } else {
  1739. capability = new OCCapability(); // return default with all UNKNOWN
  1740. }
  1741. c.close();
  1742. return capability;
  1743. }
  1744. private OCCapability createCapabilityInstance(Cursor c) {
  1745. OCCapability capability = null;
  1746. if (c != null) {
  1747. capability = new OCCapability();
  1748. capability.setId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  1749. capability.setAccountName(c.getString(c
  1750. .getColumnIndex(ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME)));
  1751. capability.setVersionMayor(c.getInt(c
  1752. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MAYOR)));
  1753. capability.setVersionMinor(c.getInt(c
  1754. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MINOR)));
  1755. capability.setVersionMicro(c.getInt(c
  1756. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MICRO)));
  1757. capability.setVersionString(c.getString(c
  1758. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_STRING)));
  1759. capability.setVersionEdition(c.getString(c
  1760. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_EDITION)));
  1761. capability.setCorePollinterval(c.getInt(c
  1762. .getColumnIndex(ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL)));
  1763. capability.setFilesSharingApiEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1764. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED))));
  1765. capability.setFilesSharingPublicEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1766. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED))));
  1767. capability.setFilesSharingPublicPasswordEnforced(CapabilityBooleanType.fromValue(c.getInt(c
  1768. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED))));
  1769. capability.setFilesSharingPublicExpireDateEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1770. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED))));
  1771. capability.setFilesSharingPublicExpireDateDays(c.getInt(c
  1772. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS)));
  1773. capability.setFilesSharingPublicExpireDateEnforced(CapabilityBooleanType.fromValue(c.getInt(c
  1774. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED))));
  1775. capability.setFilesSharingPublicSendMail(CapabilityBooleanType.fromValue(c.getInt(c
  1776. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL))));
  1777. capability.setFilesSharingPublicUpload(CapabilityBooleanType.fromValue(c.getInt(c
  1778. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD))));
  1779. capability.setFilesSharingUserSendMail(CapabilityBooleanType.fromValue(c.getInt(c
  1780. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL))));
  1781. capability.setFilesSharingResharing(CapabilityBooleanType.fromValue(c.getInt(c
  1782. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_RESHARING))));
  1783. capability.setFilesSharingFederationOutgoing(CapabilityBooleanType.fromValue(c.getInt(c
  1784. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING))));
  1785. capability.setFilesSharingFederationIncoming(CapabilityBooleanType.fromValue(c.getInt(c
  1786. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING))));
  1787. capability.setFilesBigFileChuncking(CapabilityBooleanType.fromValue(c.getInt(c
  1788. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING))));
  1789. capability.setFilesUndelete(CapabilityBooleanType.fromValue(c.getInt(c
  1790. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_UNDELETE))));
  1791. capability.setFilesVersioning(CapabilityBooleanType.fromValue(c.getInt(c
  1792. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_VERSIONING))));
  1793. capability.setFilesFileDrop(CapabilityBooleanType.fromValue(c.getInt(c
  1794. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_DROP))));
  1795. }
  1796. return capability;
  1797. }
  1798. }