FileDataStorageManager.java 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  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 = true;
  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. } else {
  421. return false;
  422. }
  423. return success;
  424. }
  425. public boolean removeFolder(OCFile folder, boolean removeDBData, boolean removeLocalContent) {
  426. boolean success = false;
  427. if (folder != null && folder.isFolder()) {
  428. if (removeDBData && folder.getFileId() != -1) {
  429. success = removeFolderInDb(folder);
  430. }
  431. if (removeLocalContent && success) {
  432. success = removeLocalFolder(folder);
  433. }
  434. }
  435. return success;
  436. }
  437. private boolean removeFolderInDb(OCFile folder) {
  438. Uri folder_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, "" + folder.getFileId()); // URI
  439. // for recursive deletion
  440. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?";
  441. String [] whereArgs = new String[]{mAccount.name, folder.getRemotePath()};
  442. int deleted = 0;
  443. if (getContentProviderClient() != null) {
  444. try {
  445. deleted = getContentProviderClient().delete(folder_uri, where, whereArgs);
  446. } catch (RemoteException e) {
  447. Log_OC.d(TAG, e.getMessage(), e);
  448. }
  449. } else {
  450. deleted = getContentResolver().delete(folder_uri, where, whereArgs);
  451. }
  452. return deleted > 0;
  453. }
  454. private boolean removeLocalFolder(OCFile folder) {
  455. boolean success = true;
  456. String localFolderPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, folder);
  457. File localFolder = new File(localFolderPath);
  458. if (localFolder.exists()) {
  459. // stage 1: remove the local files already registered in the files database
  460. Vector<OCFile> files = getFolderContent(folder.getFileId(), false);
  461. if (files != null) {
  462. for (OCFile file : files) {
  463. if (file.isFolder()) {
  464. success &= removeLocalFolder(file);
  465. } else {
  466. if (file.isDown()) {
  467. File localFile = new File(file.getStoragePath());
  468. success &= localFile.delete();
  469. if (success) {
  470. // notify MediaScanner about removed file
  471. deleteFileInMediaScan(file.getStoragePath());
  472. file.setStoragePath(null);
  473. saveFile(file);
  474. }
  475. }
  476. }
  477. }
  478. }
  479. // stage 2: remove the folder itself and any local file inside out of sync;
  480. // for instance, after clearing the app cache or reinstalling
  481. success &= removeLocalFolder(localFolder);
  482. }
  483. return success;
  484. }
  485. private boolean removeLocalFolder(File localFolder) {
  486. boolean success = true;
  487. File[] localFiles = localFolder.listFiles();
  488. if (localFiles != null) {
  489. for (File localFile : localFiles) {
  490. if (localFile.isDirectory()) {
  491. success &= removeLocalFolder(localFile);
  492. } else {
  493. String path = localFile.getAbsolutePath();
  494. success &= localFile.delete();
  495. }
  496. }
  497. }
  498. success &= localFolder.delete();
  499. return success;
  500. }
  501. /**
  502. * Updates database and file system for a file or folder that was moved to a different location.
  503. *
  504. * TODO explore better (faster) implementations
  505. * TODO throw exceptions up !
  506. */
  507. public void moveLocalFile(OCFile file, String targetPath, String targetParentPath) {
  508. if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) {
  509. OCFile targetParent = getFileByPath(targetParentPath);
  510. if (targetParent == null) {
  511. throw new IllegalStateException("Parent folder of the target path does not exist!!");
  512. }
  513. /// 1. get all the descendants of the moved element in a single QUERY
  514. Cursor c = null;
  515. if (getContentProviderClient() != null) {
  516. try {
  517. c = getContentProviderClient().query(
  518. ProviderTableMeta.CONTENT_URI,
  519. null,
  520. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ",
  521. new String[]{mAccount.name, file.getRemotePath() + "%"},
  522. ProviderTableMeta.FILE_PATH + " ASC "
  523. );
  524. } catch (RemoteException e) {
  525. Log_OC.e(TAG, e.getMessage(), e);
  526. }
  527. } else {
  528. c = getContentResolver().query(
  529. ProviderTableMeta.CONTENT_URI,
  530. null,
  531. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ",
  532. new String[]{mAccount.name, file.getRemotePath() + "%"},
  533. ProviderTableMeta.FILE_PATH + " ASC "
  534. );
  535. }
  536. /// 2. prepare a batch of update operations to change all the descendants
  537. ArrayList<ContentProviderOperation> operations = new ArrayList<>(c.getCount());
  538. String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
  539. List<String> originalPathsToTriggerMediaScan = new ArrayList<String>();
  540. List<String> newPathsToTriggerMediaScan = new ArrayList<String>();
  541. if (c.moveToFirst()) {
  542. int lengthOfOldPath = file.getRemotePath().length();
  543. int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
  544. do {
  545. ContentValues cv = new ContentValues(); // keep construction in the loop
  546. OCFile child = createFileInstance(c);
  547. cv.put(
  548. ProviderTableMeta.FILE_PATH,
  549. targetPath + child.getRemotePath().substring(lengthOfOldPath)
  550. );
  551. if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) {
  552. // update link to downloaded content - but local move is not done here!
  553. String targetLocalPath = defaultSavePath + targetPath +
  554. child.getStoragePath().substring(lengthOfOldStoragePath);
  555. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath);
  556. originalPathsToTriggerMediaScan.add(child.getStoragePath());
  557. newPathsToTriggerMediaScan.add(targetLocalPath);
  558. }
  559. if (child.getRemotePath().equals(file.getRemotePath())) {
  560. cv.put(ProviderTableMeta.FILE_PARENT, targetParent.getFileId());
  561. }
  562. operations.add(
  563. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  564. withValues(cv).
  565. withSelection(
  566. ProviderTableMeta._ID + "=?",
  567. new String[]{String.valueOf(child.getFileId())}
  568. )
  569. .build());
  570. } while (c.moveToNext());
  571. }
  572. c.close();
  573. /// 3. apply updates in batch
  574. try {
  575. if (getContentResolver() != null) {
  576. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  577. } else {
  578. getContentProviderClient().applyBatch(operations);
  579. }
  580. } catch (Exception e) {
  581. Log_OC.e(TAG, "Fail to update " + file.getFileId() + " and descendants in database", e);
  582. }
  583. /// 4. move in local file system
  584. String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
  585. String targetLocalPath = defaultSavePath + targetPath;
  586. File localFile = new File(originalLocalPath);
  587. boolean renamed = false;
  588. if (localFile.exists()) {
  589. File targetFile = new File(targetLocalPath);
  590. File targetFolder = targetFile.getParentFile();
  591. if (!targetFolder.exists()) {
  592. targetFolder.mkdirs();
  593. }
  594. renamed = localFile.renameTo(targetFile);
  595. }
  596. if (renamed) {
  597. Iterator<String> it = originalPathsToTriggerMediaScan.iterator();
  598. while (it.hasNext()) {
  599. // Notify MediaScanner about removed file
  600. deleteFileInMediaScan(it.next());
  601. }
  602. it = newPathsToTriggerMediaScan.iterator();
  603. while (it.hasNext()) {
  604. // Notify MediaScanner about new file/folder
  605. triggerMediaScan(it.next());
  606. }
  607. }
  608. }
  609. }
  610. public void copyLocalFile(OCFile file, String targetPath) {
  611. if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) {
  612. String localPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file);
  613. File localFile = new File(localPath);
  614. boolean copied = false;
  615. String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
  616. if (localFile.exists()) {
  617. File targetFile = new File(defaultSavePath + targetPath);
  618. File targetFolder = targetFile.getParentFile();
  619. if (!targetFolder.exists()) {
  620. targetFolder.mkdirs();
  621. }
  622. copied = FileStorageUtils.copyFile(localFile, targetFile);
  623. }
  624. Log_OC.d(TAG, "Local file COPIED : " + copied);
  625. }
  626. }
  627. public void migrateStoredFiles(String srcPath, String dstPath) throws Exception {
  628. Cursor cursor;
  629. try {
  630. if (getContentResolver() != null) {
  631. cursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI_FILE,
  632. null,
  633. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL",
  634. null,
  635. null);
  636. } else {
  637. cursor = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI_FILE,
  638. new String[]{ProviderTableMeta._ID, ProviderTableMeta.FILE_STORAGE_PATH},
  639. ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL",
  640. null,
  641. null);
  642. }
  643. } catch (RemoteException e) {
  644. Log_OC.e(TAG, e.getMessage(), e);
  645. throw e;
  646. }
  647. ArrayList<ContentProviderOperation> operations = new ArrayList<>(cursor.getCount());
  648. if (cursor.moveToFirst()) {
  649. do {
  650. ContentValues cv = new ContentValues();
  651. long fileId = cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID));
  652. String oldFileStoragePath =
  653. cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
  654. if (oldFileStoragePath.startsWith(srcPath)) {
  655. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, oldFileStoragePath.replaceFirst(srcPath, dstPath));
  656. operations.add(
  657. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  658. withValues(cv).
  659. withSelection(ProviderTableMeta._ID + "=?", new String[]{String.valueOf(fileId)})
  660. .build());
  661. }
  662. } while (cursor.moveToNext());
  663. }
  664. cursor.close();
  665. /// 3. apply updates in batch
  666. if (getContentResolver() != null) {
  667. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  668. } else {
  669. getContentProviderClient().applyBatch(operations);
  670. }
  671. }
  672. private Vector<OCFile> getFolderContent(long parentId, boolean onlyOnDevice) {
  673. Vector<OCFile> ret = new Vector<>();
  674. Uri req_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(parentId));
  675. Cursor c = null;
  676. if (getContentProviderClient() != null) {
  677. try {
  678. c = getContentProviderClient().query(
  679. req_uri,
  680. null,
  681. ProviderTableMeta.FILE_PARENT + "=?",
  682. new String[]{String.valueOf(parentId)},
  683. null
  684. );
  685. } catch (RemoteException e) {
  686. Log_OC.e(TAG, e.getMessage(), e);
  687. return ret;
  688. }
  689. } else {
  690. c = getContentResolver().query(
  691. req_uri,
  692. null,
  693. ProviderTableMeta.FILE_PARENT + "=?",
  694. new String[]{String.valueOf(parentId)},
  695. null
  696. );
  697. }
  698. if (c != null && c.moveToFirst()) {
  699. do {
  700. OCFile child = createFileInstance(c);
  701. if (child.isFolder() || !onlyOnDevice || child.isDown()) {
  702. ret.add(child);
  703. }
  704. } while (c.moveToNext());
  705. c.close();
  706. }
  707. Collections.sort(ret);
  708. return ret;
  709. }
  710. private OCFile createRootDir() {
  711. OCFile file = new OCFile(OCFile.ROOT_PATH);
  712. file.setMimetype(MimeType.DIRECTORY);
  713. file.setParentId(FileDataStorageManager.ROOT_PARENT_ID);
  714. saveFile(file);
  715. return file;
  716. }
  717. private boolean fileExists(String cmp_key, String value) {
  718. Cursor c;
  719. if (getContentResolver() != null) {
  720. c = getContentResolver()
  721. .query(ProviderTableMeta.CONTENT_URI,
  722. null,
  723. cmp_key + AND
  724. + ProviderTableMeta.FILE_ACCOUNT_OWNER
  725. + "=?",
  726. new String[]{value, mAccount.name}, null);
  727. } else {
  728. try {
  729. c = getContentProviderClient().query(
  730. ProviderTableMeta.CONTENT_URI,
  731. null,
  732. cmp_key + AND
  733. + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
  734. new String[]{value, mAccount.name}, null);
  735. } catch (RemoteException e) {
  736. Log_OC.e(TAG, "Couldn't determine file existance, assuming non existance: " + e.getMessage(), e);
  737. return false;
  738. }
  739. }
  740. boolean retval = c.moveToFirst();
  741. c.close();
  742. return retval;
  743. }
  744. private Cursor getFileCursorForValue(String key, String value) {
  745. Cursor c = null;
  746. if (getContentResolver() != null) {
  747. c = getContentResolver()
  748. .query(ProviderTableMeta.CONTENT_URI,
  749. null,
  750. key + AND
  751. + ProviderTableMeta.FILE_ACCOUNT_OWNER
  752. + "=?",
  753. new String[]{value, mAccount.name}, null);
  754. } else {
  755. try {
  756. c = getContentProviderClient().query(
  757. ProviderTableMeta.CONTENT_URI,
  758. null,
  759. key + AND + ProviderTableMeta.FILE_ACCOUNT_OWNER
  760. + "=?", new String[]{value, mAccount.name},
  761. null);
  762. } catch (RemoteException e) {
  763. Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e);
  764. c = null;
  765. }
  766. }
  767. return c;
  768. }
  769. private OCFile createFileInstance(Cursor c) {
  770. OCFile file = null;
  771. if (c != null) {
  772. file = new OCFile(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)));
  773. file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  774. file.setParentId(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_PARENT)));
  775. file.setMimetype(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
  776. if (!file.isFolder()) {
  777. file.setStoragePath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
  778. if (file.getStoragePath() == null) {
  779. // try to find existing file and bind it with current account;
  780. // with the current update of SynchronizeFolderOperation, this won't be
  781. // necessary anymore after a full synchronization of the account
  782. File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
  783. if (f.exists()) {
  784. file.setStoragePath(f.getAbsolutePath());
  785. file.setLastSyncDateForData(f.lastModified());
  786. }
  787. }
  788. }
  789. file.setFileLength(c.getLong(c
  790. .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
  791. file.setCreationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CREATION)));
  792. file.setModificationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
  793. file.setModificationTimestampAtLastSyncForData(c.getLong(
  794. c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA)));
  795. file.setLastSyncDateForProperties(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
  796. file.setLastSyncDateForData(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA)));
  797. file.setFavorite(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false);
  798. file.setEtag(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG)));
  799. file.setShareViaLink(c.getInt(
  800. c.getColumnIndex(ProviderTableMeta.FILE_SHARED_VIA_LINK)) == 1 ? true : false);
  801. file.setShareWithSharee(c.getInt(
  802. c.getColumnIndex(ProviderTableMeta.FILE_SHARED_WITH_SHAREE)) == 1 ? true : false);
  803. file.setPublicLink(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PUBLIC_LINK)));
  804. file.setPermissions(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PERMISSIONS)));
  805. file.setRemoteId(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID)));
  806. file.setNeedsUpdateThumbnail(c.getInt(
  807. c.getColumnIndex(ProviderTableMeta.FILE_UPDATE_THUMBNAIL)) == 1 ? true : false);
  808. file.setDownloading(c.getInt(
  809. c.getColumnIndex(ProviderTableMeta.FILE_IS_DOWNLOADING)) == 1 ? true : false);
  810. file.setEtagInConflict(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG_IN_CONFLICT)));
  811. }
  812. return file;
  813. }
  814. // Methods for Shares
  815. public boolean saveShare(OCShare share) {
  816. boolean overriden = false;
  817. ContentValues cv = new ContentValues();
  818. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  819. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  820. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  821. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  822. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  823. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  824. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  825. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  826. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  827. cv.put(
  828. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  829. share.getSharedWithDisplayName()
  830. );
  831. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  832. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  833. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  834. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  835. if (shareExistsForRemoteId(share.getRemoteId())) {// for renamed files; no more delete and create
  836. overriden = true;
  837. if (getContentResolver() != null) {
  838. getContentResolver().update(ProviderTableMeta.CONTENT_URI_SHARE, cv,
  839. ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  840. new String[]{String.valueOf(share.getRemoteId())});
  841. } else {
  842. try {
  843. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_SHARE,
  844. cv, ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  845. new String[]{String.valueOf(share.getRemoteId())});
  846. } catch (RemoteException e) {
  847. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  848. }
  849. }
  850. } else {
  851. Uri result_uri = null;
  852. if (getContentResolver() != null) {
  853. result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_SHARE, cv);
  854. } else {
  855. try {
  856. result_uri = getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_SHARE, cv);
  857. } catch (RemoteException e) {
  858. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  859. }
  860. }
  861. if (result_uri != null) {
  862. long new_id = Long.parseLong(result_uri.getPathSegments().get(1));
  863. share.setId(new_id);
  864. }
  865. }
  866. return overriden;
  867. }
  868. /**
  869. * Retrieves an stored {@link OCShare} given its id.
  870. *
  871. * @param id Identifier.
  872. * @return Stored {@link OCShare} given its id.
  873. */
  874. public OCShare getShareById(long id) {
  875. OCShare share = null;
  876. Cursor c = getShareCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
  877. if (c != null) {
  878. if (c.moveToFirst()) {
  879. share = createShareInstance(c);
  880. }
  881. c.close();
  882. }
  883. return share;
  884. }
  885. /**
  886. * Checks the existance of an stored {@link OCShare} matching the given remote id (not to be confused with
  887. * the local id) in the current account.
  888. *
  889. * @param remoteId Remote of the share in the server.
  890. * @return 'True' if a matching {@link OCShare} is stored in the current account.
  891. */
  892. private boolean shareExistsForRemoteId(long remoteId) {
  893. return shareExistsForValue(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId));
  894. }
  895. /**
  896. * Checks the existance of an stored {@link OCShare} in the current account
  897. * matching a given column and a value for that column
  898. *
  899. * @param key Name of the column to match.
  900. * @param value Value of the column to match.
  901. * @return 'True' if a matching {@link OCShare} is stored in the current account.
  902. */
  903. private boolean shareExistsForValue(String key, String value) {
  904. Cursor c = getShareCursorForValue(key, value);
  905. boolean retval = c.moveToFirst();
  906. c.close();
  907. return retval;
  908. }
  909. /**
  910. * Gets a {@link Cursor} for 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 Cursor getShareCursorForValue(String key, String value) {
  918. Cursor c;
  919. if (getContentResolver() != null) {
  920. c = getContentResolver()
  921. .query(ProviderTableMeta.CONTENT_URI_SHARE,
  922. null,
  923. key + AND
  924. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
  925. new String[]{value, mAccount.name},
  926. null
  927. );
  928. } else {
  929. try {
  930. c = getContentProviderClient().query(
  931. ProviderTableMeta.CONTENT_URI_SHARE,
  932. null,
  933. key + AND + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
  934. new String[]{value, mAccount.name},
  935. null
  936. );
  937. } catch (RemoteException e) {
  938. Log_OC.w(TAG, "Could not get details, assuming share does not exist: "+ e.getMessage());
  939. c = null;
  940. }
  941. }
  942. return c;
  943. }
  944. /**
  945. * Get first share bound to a file with a known path and given {@link ShareType}.
  946. *
  947. * @param path Path of the file.
  948. * @param type Type of the share to get
  949. * @param shareWith Target of the share. Ignored in type is {@link ShareType#PUBLIC_LINK}
  950. * @return First {@link OCShare} instance found in DB bound to the file in 'path'
  951. */
  952. public OCShare getFirstShareByPathAndType(String path, ShareType type, String shareWith) {
  953. Cursor cursor = null;
  954. if (shareWith == null) {
  955. shareWith = "";
  956. }
  957. String selection = ProviderTableMeta.OCSHARES_PATH + AND
  958. + ProviderTableMeta.OCSHARES_SHARE_TYPE + AND
  959. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?" ;
  960. if (!ShareType.PUBLIC_LINK.equals(type)) {
  961. selection += " AND " + ProviderTableMeta.OCSHARES_SHARE_WITH + "=?";
  962. }
  963. String [] selectionArgs;
  964. if (ShareType.PUBLIC_LINK.equals(type)) {
  965. selectionArgs = new String[]{
  966. path,
  967. Integer.toString(type.getValue()),
  968. mAccount.name
  969. };
  970. } else {
  971. selectionArgs = new String[]{
  972. path,
  973. Integer.toString(type.getValue()),
  974. mAccount.name,
  975. shareWith
  976. };
  977. }
  978. if (getContentResolver() != null) {
  979. cursor = getContentResolver().query(
  980. ProviderTableMeta.CONTENT_URI_SHARE,
  981. null,
  982. selection, selectionArgs,
  983. null);
  984. } else {
  985. try {
  986. cursor = getContentProviderClient().query(
  987. ProviderTableMeta.CONTENT_URI_SHARE,
  988. null,
  989. selection, selectionArgs,
  990. null);
  991. } catch (RemoteException e) {
  992. Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e);
  993. cursor = null;
  994. }
  995. }
  996. OCShare share = null;
  997. if (cursor != null) {
  998. if (cursor.moveToFirst()) {
  999. share = createShareInstance(cursor);
  1000. }
  1001. cursor.close();
  1002. }
  1003. return share;
  1004. }
  1005. private OCShare createShareInstance(Cursor c) {
  1006. OCShare share = null;
  1007. if (c != null) {
  1008. share = new OCShare(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_PATH)));
  1009. share.setId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  1010. share.setFileSource(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ITEM_SOURCE)));
  1011. share.setShareType(ShareType.fromValue(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_TYPE))));
  1012. share.setShareWith(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH)));
  1013. share.setPermissions(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_PERMISSIONS)));
  1014. share.setSharedDate(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARED_DATE)));
  1015. share.setExpirationDate(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_EXPIRATION_DATE)));
  1016. share.setToken(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_TOKEN)));
  1017. share.setSharedWithDisplayName(
  1018. c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME)));
  1019. share.setIsFolder(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_IS_DIRECTORY)) == 1);
  1020. share.setUserId(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_USER_ID)));
  1021. share.setIdRemoteShared(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED)));
  1022. }
  1023. return share;
  1024. }
  1025. private void resetShareFlagsInAllFiles() {
  1026. ContentValues cv = new ContentValues();
  1027. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1028. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1029. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1030. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  1031. String[] whereArgs = new String[]{mAccount.name};
  1032. if (getContentResolver() != null) {
  1033. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1034. } else {
  1035. try {
  1036. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1037. } catch (RemoteException e) {
  1038. Log_OC.e(TAG, "Exception in resetShareFlagsInAllFiles" + e.getMessage(), e);
  1039. }
  1040. }
  1041. }
  1042. private void resetShareFlagsInFolder(OCFile folder) {
  1043. ContentValues cv = new ContentValues();
  1044. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1045. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1046. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1047. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PARENT + "=?";
  1048. String [] whereArgs = new String[] { mAccount.name , String.valueOf(folder.getFileId()) };
  1049. if (getContentResolver() != null) {
  1050. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1051. } else {
  1052. try {
  1053. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1054. } catch (RemoteException e) {
  1055. Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e);
  1056. }
  1057. }
  1058. }
  1059. private void resetShareFlagInAFile(String filePath){
  1060. ContentValues cv = new ContentValues();
  1061. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1062. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1063. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1064. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH+ "=?";
  1065. String [] whereArgs = new String[] { mAccount.name , filePath };
  1066. if (getContentResolver() != null) {
  1067. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1068. } else {
  1069. try {
  1070. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1071. } catch (RemoteException e) {
  1072. Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e);
  1073. }
  1074. }
  1075. }
  1076. private void cleanShares() {
  1077. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1078. String[] whereArgs = new String[]{mAccount.name};
  1079. if (getContentResolver() != null) {
  1080. getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
  1081. } else {
  1082. try {
  1083. getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
  1084. } catch (RemoteException e) {
  1085. Log_OC.e(TAG, "Exception in cleanShares" + e.getMessage(), e);
  1086. }
  1087. }
  1088. }
  1089. public void saveShares(Collection<OCShare> shares) {
  1090. cleanShares();
  1091. if (shares != null) {
  1092. ArrayList<ContentProviderOperation> operations =
  1093. new ArrayList<ContentProviderOperation>(shares.size());
  1094. // prepare operations to insert or update files to save in the given folder
  1095. for (OCShare share : shares) {
  1096. ContentValues cv = new ContentValues();
  1097. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  1098. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  1099. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  1100. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  1101. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  1102. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  1103. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  1104. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  1105. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  1106. cv.put(
  1107. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  1108. share.getSharedWithDisplayName()
  1109. );
  1110. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  1111. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  1112. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  1113. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  1114. if (shareExistsForRemoteId(share.getRemoteId())) {
  1115. // updating an existing file
  1116. operations.add(
  1117. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
  1118. withValues(cv).
  1119. withSelection(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  1120. new String[]{String.valueOf(share.getRemoteId())})
  1121. .build());
  1122. } else {
  1123. // adding a new file
  1124. operations.add(
  1125. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).
  1126. withValues(cv).
  1127. build()
  1128. );
  1129. }
  1130. }
  1131. // apply operations in batch
  1132. if (operations.size() > 0) {
  1133. @SuppressWarnings("unused")
  1134. ContentProviderResult[] results = null;
  1135. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1136. try {
  1137. if (getContentResolver() != null) {
  1138. results = getContentResolver().applyBatch(MainApp.getAuthority(),
  1139. operations);
  1140. } else {
  1141. results = getContentProviderClient().applyBatch(operations);
  1142. }
  1143. } catch (OperationApplicationException | RemoteException e) {
  1144. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1145. }
  1146. }
  1147. }
  1148. }
  1149. public void updateSharedFiles(Collection<OCFile> sharedFiles) {
  1150. resetShareFlagsInAllFiles();
  1151. if (sharedFiles != null) {
  1152. ArrayList<ContentProviderOperation> operations = new ArrayList<>(sharedFiles.size());
  1153. // prepare operations to insert or update files to save in the given folder
  1154. for (OCFile file : sharedFiles) {
  1155. ContentValues cv = new ContentValues();
  1156. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  1157. cv.put(
  1158. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  1159. file.getModificationTimestampAtLastSyncForData()
  1160. );
  1161. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  1162. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  1163. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  1164. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  1165. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  1166. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  1167. if (!file.isFolder()) {
  1168. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  1169. }
  1170. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  1171. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  1172. cv.put(
  1173. ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA,
  1174. file.getLastSyncDateForData()
  1175. );
  1176. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.isFavorite() ? 1 : 0);
  1177. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  1178. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
  1179. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
  1180. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
  1181. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  1182. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  1183. cv.put(
  1184. ProviderTableMeta.FILE_UPDATE_THUMBNAIL,
  1185. file.needsUpdateThumbnail() ? 1 : 0
  1186. );
  1187. cv.put(
  1188. ProviderTableMeta.FILE_IS_DOWNLOADING,
  1189. file.isDownloading() ? 1 : 0
  1190. );
  1191. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
  1192. boolean existsByPath = fileExists(file.getRemotePath());
  1193. if (existsByPath || fileExists(file.getFileId())) {
  1194. // updating an existing file
  1195. operations.add(
  1196. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  1197. withValues(cv).
  1198. withSelection(ProviderTableMeta._ID + "=?",
  1199. new String[]{String.valueOf(file.getFileId())})
  1200. .build());
  1201. } else {
  1202. // adding a new file
  1203. operations.add(
  1204. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).
  1205. withValues(cv).
  1206. build()
  1207. );
  1208. }
  1209. }
  1210. // apply operations in batch
  1211. if (operations.size() > 0) {
  1212. @SuppressWarnings("unused")
  1213. ContentProviderResult[] results = null;
  1214. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1215. try {
  1216. if (getContentResolver() != null) {
  1217. results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1218. } else {
  1219. results = getContentProviderClient().applyBatch(operations);
  1220. }
  1221. } catch (OperationApplicationException | RemoteException e) {
  1222. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1223. }
  1224. }
  1225. }
  1226. }
  1227. public void removeShare(OCShare share) {
  1228. Uri share_uri = ProviderTableMeta.CONTENT_URI_SHARE;
  1229. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND +
  1230. ProviderTableMeta._ID + "=?";
  1231. String [] whereArgs = new String[]{mAccount.name, Long.toString(share.getId())};
  1232. if (getContentProviderClient() != null) {
  1233. try {
  1234. getContentProviderClient().delete(share_uri, where, whereArgs);
  1235. } catch (RemoteException e) {
  1236. Log_OC.d(TAG, e.getMessage(), e);
  1237. }
  1238. } else {
  1239. getContentResolver().delete(share_uri, where, whereArgs);
  1240. }
  1241. }
  1242. public void saveSharesDB(ArrayList<OCShare> shares) {
  1243. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1244. // Reset flags & Remove shares for this files
  1245. String filePath = "";
  1246. for (OCShare share: shares) {
  1247. if (filePath != share.getPath()){
  1248. filePath = share.getPath();
  1249. resetShareFlagInAFile(filePath);
  1250. operations = prepareRemoveSharesInFile(filePath, operations);
  1251. }
  1252. }
  1253. // Add operations to insert shares
  1254. operations = prepareInsertShares(shares, operations);
  1255. // apply operations in batch
  1256. if (operations.size() > 0) {
  1257. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1258. try {
  1259. if (getContentResolver() != null) {
  1260. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1261. } else {
  1262. getContentProviderClient().applyBatch(operations);
  1263. }
  1264. } catch (OperationApplicationException | RemoteException e) {
  1265. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1266. }
  1267. }
  1268. // // TODO: review if it is needed
  1269. // // Update shared files
  1270. // ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
  1271. //
  1272. // for (OCShare share : shares) {
  1273. // // Get the path
  1274. // String path = share.getPath();
  1275. // if (share.isFolder()) {
  1276. // path = path + FileUtils.PATH_SEPARATOR;
  1277. // }
  1278. //
  1279. // // Update OCFile with data from share: ShareByLink, publicLink and
  1280. // OCFile file = getFileByPath(path);
  1281. // if (file != null) {
  1282. // if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
  1283. // file.setShareViaLink(true);
  1284. // sharedFiles.add(file);
  1285. // }
  1286. // }
  1287. // }
  1288. //
  1289. // // TODO: Review
  1290. // updateSharedFiles(sharedFiles);
  1291. }
  1292. public void removeSharesForFile(String remotePath) {
  1293. resetShareFlagInAFile(remotePath);
  1294. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1295. operations = prepareRemoveSharesInFile(remotePath, operations);
  1296. // apply operations in batch
  1297. if (operations.size() > 0) {
  1298. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1299. try {
  1300. if (getContentResolver() != null) {
  1301. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1302. } else {
  1303. getContentProviderClient().applyBatch(operations);
  1304. }
  1305. } catch (OperationApplicationException | RemoteException e) {
  1306. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1307. }
  1308. }
  1309. }
  1310. public void saveSharesInFolder(ArrayList<OCShare> shares, OCFile folder) {
  1311. resetShareFlagsInFolder(folder);
  1312. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1313. operations = prepareRemoveSharesInFolder(folder, operations);
  1314. if (shares != null) {
  1315. // prepare operations to insert or update files to save in the given folder
  1316. operations = prepareInsertShares(shares, operations);
  1317. }
  1318. // apply operations in batch
  1319. if (operations.size() > 0) {
  1320. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1321. try {
  1322. if (getContentResolver() != null) {
  1323. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1324. } else {
  1325. getContentProviderClient().applyBatch(operations);
  1326. }
  1327. } catch (OperationApplicationException | RemoteException e) {
  1328. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1329. }
  1330. }
  1331. }
  1332. /**
  1333. * Prepare operations to insert or update files to save in the given folder
  1334. * @param shares List of shares to insert
  1335. * @param operations List of operations
  1336. * @return
  1337. */
  1338. private ArrayList<ContentProviderOperation> prepareInsertShares(
  1339. ArrayList<OCShare> shares, ArrayList<ContentProviderOperation> operations) {
  1340. if (shares != null) {
  1341. // prepare operations to insert or update files to save in the given folder
  1342. for (OCShare share : shares) {
  1343. ContentValues cv = new ContentValues();
  1344. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  1345. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  1346. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  1347. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  1348. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  1349. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  1350. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  1351. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  1352. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  1353. cv.put(
  1354. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  1355. share.getSharedWithDisplayName()
  1356. );
  1357. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  1358. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  1359. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  1360. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  1361. // adding a new share resource
  1362. operations.add(
  1363. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).
  1364. withValues(cv).
  1365. build()
  1366. );
  1367. }
  1368. }
  1369. return operations;
  1370. }
  1371. private ArrayList<ContentProviderOperation> prepareRemoveSharesInFolder(
  1372. OCFile folder, ArrayList<ContentProviderOperation> preparedOperations) {
  1373. if (folder != null) {
  1374. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1375. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1376. String [] whereArgs = new String[]{ "", mAccount.name };
  1377. Vector<OCFile> files = getFolderContent(folder, false);
  1378. for (OCFile file : files) {
  1379. whereArgs[0] = file.getRemotePath();
  1380. preparedOperations.add(
  1381. ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE).
  1382. withSelection(where, whereArgs).
  1383. build()
  1384. );
  1385. }
  1386. }
  1387. return preparedOperations;
  1388. }
  1389. private ArrayList<ContentProviderOperation> prepareRemoveSharesInFile(
  1390. String filePath, ArrayList<ContentProviderOperation> preparedOperations) {
  1391. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1392. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1393. String[] whereArgs = new String[]{filePath, mAccount.name};
  1394. preparedOperations.add(
  1395. ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE).
  1396. withSelection(where, whereArgs).
  1397. build()
  1398. );
  1399. return preparedOperations;
  1400. }
  1401. public ArrayList<OCShare> getSharesWithForAFile(String filePath, String accountName){
  1402. // Condition
  1403. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1404. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND
  1405. + " (" + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR "
  1406. + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR "
  1407. + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR "
  1408. + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? ) ";
  1409. String [] whereArgs = new String[]{ filePath, accountName ,
  1410. Integer.toString(ShareType.USER.getValue()),
  1411. Integer.toString(ShareType.GROUP.getValue()),
  1412. Integer.toString(ShareType.EMAIL.getValue()),
  1413. Integer.toString(ShareType.FEDERATED.getValue())};
  1414. Cursor cursor = null;
  1415. if (getContentResolver() != null) {
  1416. cursor = getContentResolver().query(
  1417. ProviderTableMeta.CONTENT_URI_SHARE,
  1418. null, where, whereArgs, null);
  1419. } else {
  1420. try {
  1421. cursor = getContentProviderClient().query(
  1422. ProviderTableMeta.CONTENT_URI_SHARE,
  1423. null, where, whereArgs, null);
  1424. } catch (RemoteException e) {
  1425. Log_OC.e(TAG, "Could not get list of shares with: " + e.getMessage(), e);
  1426. cursor = null;
  1427. }
  1428. }
  1429. ArrayList<OCShare> shares = new ArrayList<OCShare>();
  1430. OCShare share = null;
  1431. if (cursor != null) {
  1432. if (cursor.moveToFirst()) {
  1433. do {
  1434. share = createShareInstance(cursor);
  1435. shares.add(share);
  1436. } while (cursor.moveToNext());
  1437. }
  1438. cursor.close();
  1439. }
  1440. return shares;
  1441. }
  1442. public static void triggerMediaScan(String path) {
  1443. if (path != null) {
  1444. Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  1445. intent.setData(Uri.fromFile(new File(path)));
  1446. MainApp.getAppContext().sendBroadcast(intent);
  1447. }
  1448. }
  1449. public void deleteFileInMediaScan(String path) {
  1450. String mimetypeString = FileStorageUtils.getMimeTypeFromName(path);
  1451. ContentResolver contentResolver = getContentResolver();
  1452. if (contentResolver != null) {
  1453. if (MimeTypeUtil.isImage(mimetypeString)) {
  1454. // Images
  1455. contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  1456. MediaStore.Images.Media.DATA + "=?", new String[]{path});
  1457. } else if (MimeTypeUtil.isAudio(mimetypeString)) {
  1458. // Audio
  1459. contentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  1460. MediaStore.Audio.Media.DATA + "=?", new String[]{path});
  1461. } else if (MimeTypeUtil.isVideo(mimetypeString)) {
  1462. // Video
  1463. contentResolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  1464. MediaStore.Video.Media.DATA + "=?", new String[]{path});
  1465. }
  1466. } else {
  1467. ContentProviderClient contentProviderClient = getContentProviderClient();
  1468. try {
  1469. if (MimeTypeUtil.isImage(mimetypeString)) {
  1470. // Images
  1471. contentProviderClient.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  1472. MediaStore.Images.Media.DATA + "=?", new String[]{path});
  1473. } else if (MimeTypeUtil.isAudio(mimetypeString)) {
  1474. // Audio
  1475. contentProviderClient.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  1476. MediaStore.Audio.Media.DATA + "=?", new String[]{path});
  1477. } else if (MimeTypeUtil.isVideo(mimetypeString)) {
  1478. // Video
  1479. contentProviderClient.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  1480. MediaStore.Video.Media.DATA + "=?", new String[]{path});
  1481. }
  1482. } catch (RemoteException e) {
  1483. Log_OC.e(TAG, "Exception deleting media file in MediaStore " + e.getMessage(), e);
  1484. }
  1485. }
  1486. }
  1487. public void saveConflict(OCFile file, String etagInConflict) {
  1488. if (!file.isDown()) {
  1489. etagInConflict = null;
  1490. }
  1491. ContentValues cv = new ContentValues();
  1492. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, etagInConflict);
  1493. int updated = 0;
  1494. if (getContentResolver() != null) {
  1495. updated = getContentResolver().update(
  1496. ProviderTableMeta.CONTENT_URI_FILE,
  1497. cv,
  1498. ProviderTableMeta._ID + "=?",
  1499. new String[] { String.valueOf(file.getFileId())}
  1500. );
  1501. } else {
  1502. try {
  1503. updated = getContentProviderClient().update(
  1504. ProviderTableMeta.CONTENT_URI_FILE,
  1505. cv,
  1506. ProviderTableMeta._ID + "=?",
  1507. new String[]{String.valueOf(file.getFileId())}
  1508. );
  1509. } catch (RemoteException e) {
  1510. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1511. }
  1512. }
  1513. Log_OC.d(TAG, "Number of files updated with CONFLICT: " + updated);
  1514. if (updated > 0) {
  1515. if (etagInConflict != null) {
  1516. /// set conflict in all ancestor folders
  1517. long parentId = file.getParentId();
  1518. Set<String> ancestorIds = new HashSet<>();
  1519. while (parentId != FileDataStorageManager.ROOT_PARENT_ID) {
  1520. ancestorIds.add(Long.toString(parentId));
  1521. parentId = getFileById(parentId).getParentId();
  1522. }
  1523. if (ancestorIds.size() > 0) {
  1524. final StringBuffer whereBuffer = new StringBuffer();
  1525. whereBuffer.append(ProviderTableMeta._ID).append(" IN (");
  1526. for (int i = 0; i < ancestorIds.size() - 1; i++) {
  1527. whereBuffer.append("?,");
  1528. }
  1529. whereBuffer.append("?)");
  1530. if (getContentResolver() != null) {
  1531. updated = getContentResolver().update(
  1532. ProviderTableMeta.CONTENT_URI_FILE,
  1533. cv,
  1534. whereBuffer.toString(),
  1535. ancestorIds.toArray(new String[]{})
  1536. );
  1537. } else {
  1538. try {
  1539. updated = getContentProviderClient().update(
  1540. ProviderTableMeta.CONTENT_URI_FILE,
  1541. cv,
  1542. whereBuffer.toString(),
  1543. ancestorIds.toArray(new String[]{})
  1544. );
  1545. } catch (RemoteException e) {
  1546. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1547. }
  1548. }
  1549. } // else file is ROOT folder, no parent to set in conflict
  1550. } else {
  1551. /// update conflict in ancestor folders
  1552. // (not directly unset; maybe there are more conflicts below them)
  1553. String parentPath = file.getRemotePath();
  1554. if (parentPath.endsWith(OCFile.PATH_SEPARATOR)) {
  1555. parentPath = parentPath.substring(0, parentPath.length() - 1);
  1556. }
  1557. parentPath = parentPath.substring(0, parentPath.lastIndexOf(OCFile.PATH_SEPARATOR) + 1);
  1558. Log_OC.d(TAG, "checking parents to remove conflict; STARTING with " + parentPath);
  1559. while (parentPath.length() > 0) {
  1560. String whereForDescencentsInConflict =
  1561. ProviderTableMeta.FILE_ETAG_IN_CONFLICT + " IS NOT NULL AND " +
  1562. ProviderTableMeta.FILE_CONTENT_TYPE + " != 'DIR' AND " +
  1563. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1564. ProviderTableMeta.FILE_PATH + " LIKE ?";
  1565. Cursor descendentsInConflict = null;
  1566. if (getContentResolver() != null) {
  1567. descendentsInConflict = getContentResolver().query(
  1568. ProviderTableMeta.CONTENT_URI_FILE,
  1569. new String[]{ProviderTableMeta._ID},
  1570. whereForDescencentsInConflict,
  1571. new String[]{mAccount.name, parentPath + "%"},
  1572. null
  1573. );
  1574. } else {
  1575. try {
  1576. descendentsInConflict = getContentProviderClient().query(
  1577. ProviderTableMeta.CONTENT_URI_FILE,
  1578. new String[]{ProviderTableMeta._ID},
  1579. whereForDescencentsInConflict,
  1580. new String[]{mAccount.name, parentPath + "%"},
  1581. null
  1582. );
  1583. } catch (RemoteException e) {
  1584. Log_OC.e(TAG, "Failed querying for descendents in conflict " + e.getMessage(), e);
  1585. }
  1586. }
  1587. if (descendentsInConflict == null || descendentsInConflict.getCount() == 0) {
  1588. Log_OC.d(TAG, "NO MORE conflicts in " + parentPath);
  1589. if (getContentResolver() != null) {
  1590. updated = getContentResolver().update(
  1591. ProviderTableMeta.CONTENT_URI_FILE,
  1592. cv,
  1593. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1594. ProviderTableMeta.FILE_PATH + "=?",
  1595. new String[]{mAccount.name, parentPath}
  1596. );
  1597. } else {
  1598. try {
  1599. updated = getContentProviderClient().update(
  1600. ProviderTableMeta.CONTENT_URI_FILE,
  1601. cv,
  1602. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1603. ProviderTableMeta.FILE_PATH + "=?"
  1604. , new String[]{mAccount.name, parentPath}
  1605. );
  1606. } catch (RemoteException e) {
  1607. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1608. }
  1609. }
  1610. } else {
  1611. Log_OC.d(TAG, "STILL " + descendentsInConflict.getCount() + " in " + parentPath);
  1612. }
  1613. if (descendentsInConflict != null) {
  1614. descendentsInConflict.close();
  1615. }
  1616. parentPath = parentPath.substring(0, parentPath.length() - 1); // trim last /
  1617. parentPath = parentPath.substring(0, parentPath.lastIndexOf(OCFile.PATH_SEPARATOR) + 1);
  1618. Log_OC.d(TAG, "checking parents to remove conflict; NEXT " + parentPath);
  1619. }
  1620. }
  1621. }
  1622. }
  1623. public OCCapability saveCapabilities(OCCapability capability){
  1624. // Prepare capabilities data
  1625. ContentValues cv = new ContentValues();
  1626. cv.put(ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME, mAccount.name);
  1627. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MAYOR, capability.getVersionMayor());
  1628. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MINOR, capability.getVersionMinor());
  1629. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MICRO, capability.getVersionMicro());
  1630. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_STRING, capability.getVersionString());
  1631. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_EDITION, capability.getVersionEdition());
  1632. cv.put(ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL, capability.getCorePollinterval());
  1633. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED, capability.getFilesSharingApiEnabled().getValue());
  1634. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED,
  1635. capability.getFilesSharingPublicEnabled().getValue());
  1636. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED,
  1637. capability.getFilesSharingPublicPasswordEnforced().getValue());
  1638. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED,
  1639. capability.getFilesSharingPublicExpireDateEnabled().getValue());
  1640. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS,
  1641. capability.getFilesSharingPublicExpireDateDays());
  1642. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED,
  1643. capability.getFilesSharingPublicExpireDateEnforced().getValue());
  1644. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL,
  1645. capability.getFilesSharingPublicSendMail().getValue());
  1646. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD,
  1647. capability.getFilesSharingPublicUpload().getValue());
  1648. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL,
  1649. capability.getFilesSharingUserSendMail().getValue());
  1650. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_RESHARING, capability.getFilesSharingResharing().getValue());
  1651. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING,
  1652. capability.getFilesSharingFederationOutgoing().getValue());
  1653. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING,
  1654. capability.getFilesSharingFederationIncoming().getValue());
  1655. cv.put(ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING, capability.getFilesBigFileChuncking().getValue());
  1656. cv.put(ProviderTableMeta.CAPABILITIES_FILES_UNDELETE, capability.getFilesUndelete().getValue());
  1657. cv.put(ProviderTableMeta.CAPABILITIES_FILES_VERSIONING, capability.getFilesVersioning().getValue());
  1658. cv.put(ProviderTableMeta.CAPABILITIES_FILES_DROP, capability.getFilesFileDrop().getValue());
  1659. if (capabilityExists(mAccount.name)) {
  1660. if (getContentResolver() != null) {
  1661. getContentResolver().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv,
  1662. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?",
  1663. new String[]{mAccount.name});
  1664. } else {
  1665. try {
  1666. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1667. cv, ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?",
  1668. new String[]{mAccount.name});
  1669. } catch (RemoteException e) {
  1670. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  1671. }
  1672. }
  1673. } else {
  1674. Uri result_uri = null;
  1675. if (getContentResolver() != null) {
  1676. result_uri = getContentResolver().insert(
  1677. ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv);
  1678. } else {
  1679. try {
  1680. result_uri = getContentProviderClient().insert(
  1681. ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv);
  1682. } catch (RemoteException e) {
  1683. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  1684. }
  1685. }
  1686. if (result_uri != null) {
  1687. long new_id = Long.parseLong(result_uri.getPathSegments()
  1688. .get(1));
  1689. capability.setId(new_id);
  1690. capability.setAccountName(mAccount.name);
  1691. }
  1692. }
  1693. return capability;
  1694. }
  1695. private boolean capabilityExists(String accountName) {
  1696. Cursor c = getCapabilityCursorForAccount(accountName);
  1697. boolean exists = false;
  1698. if (c != null) {
  1699. exists = c.moveToFirst();
  1700. c.close();
  1701. }
  1702. return exists;
  1703. }
  1704. private Cursor getCapabilityCursorForAccount(String accountName){
  1705. Cursor c = null;
  1706. if (getContentResolver() != null) {
  1707. c = getContentResolver()
  1708. .query(ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1709. null,
  1710. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ",
  1711. new String[]{accountName}, null);
  1712. } else {
  1713. try {
  1714. c = getContentProviderClient().query(
  1715. ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1716. null,
  1717. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ",
  1718. new String[]{accountName}, null);
  1719. } catch (RemoteException e) {
  1720. Log_OC.e(TAG, "Couldn't determine capability existence, assuming non existance: " + e.getMessage(), e);
  1721. }
  1722. }
  1723. return c;
  1724. }
  1725. public OCCapability getCapability(String accountName){
  1726. OCCapability capability = null;
  1727. Cursor c = getCapabilityCursorForAccount(accountName);
  1728. if (c.moveToFirst()) {
  1729. capability = createCapabilityInstance(c);
  1730. } else {
  1731. capability = new OCCapability(); // return default with all UNKNOWN
  1732. }
  1733. c.close();
  1734. return capability;
  1735. }
  1736. private OCCapability createCapabilityInstance(Cursor c) {
  1737. OCCapability capability = null;
  1738. if (c != null) {
  1739. capability = new OCCapability();
  1740. capability.setId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  1741. capability.setAccountName(c.getString(c
  1742. .getColumnIndex(ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME)));
  1743. capability.setVersionMayor(c.getInt(c
  1744. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MAYOR)));
  1745. capability.setVersionMinor(c.getInt(c
  1746. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MINOR)));
  1747. capability.setVersionMicro(c.getInt(c
  1748. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MICRO)));
  1749. capability.setVersionString(c.getString(c
  1750. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_STRING)));
  1751. capability.setVersionEdition(c.getString(c
  1752. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_EDITION)));
  1753. capability.setCorePollinterval(c.getInt(c
  1754. .getColumnIndex(ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL)));
  1755. capability.setFilesSharingApiEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1756. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED))));
  1757. capability.setFilesSharingPublicEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1758. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED))));
  1759. capability.setFilesSharingPublicPasswordEnforced(CapabilityBooleanType.fromValue(c.getInt(c
  1760. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED))));
  1761. capability.setFilesSharingPublicExpireDateEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1762. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED))));
  1763. capability.setFilesSharingPublicExpireDateDays(c.getInt(c
  1764. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS)));
  1765. capability.setFilesSharingPublicExpireDateEnforced(CapabilityBooleanType.fromValue(c.getInt(c
  1766. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED))));
  1767. capability.setFilesSharingPublicSendMail(CapabilityBooleanType.fromValue(c.getInt(c
  1768. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL))));
  1769. capability.setFilesSharingPublicUpload(CapabilityBooleanType.fromValue(c.getInt(c
  1770. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD))));
  1771. capability.setFilesSharingUserSendMail(CapabilityBooleanType.fromValue(c.getInt(c
  1772. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL))));
  1773. capability.setFilesSharingResharing(CapabilityBooleanType.fromValue(c.getInt(c
  1774. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_RESHARING))));
  1775. capability.setFilesSharingFederationOutgoing(CapabilityBooleanType.fromValue(c.getInt(c
  1776. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING))));
  1777. capability.setFilesSharingFederationIncoming(CapabilityBooleanType.fromValue(c.getInt(c
  1778. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING))));
  1779. capability.setFilesBigFileChuncking(CapabilityBooleanType.fromValue(c.getInt(c
  1780. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING))));
  1781. capability.setFilesUndelete(CapabilityBooleanType.fromValue(c.getInt(c
  1782. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_UNDELETE))));
  1783. capability.setFilesVersioning(CapabilityBooleanType.fromValue(c.getInt(c
  1784. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_VERSIONING))));
  1785. capability.setFilesFileDrop(CapabilityBooleanType.fromValue(c.getInt(c
  1786. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_DROP))));
  1787. }
  1788. return capability;
  1789. }
  1790. }