FileDataStorageManager.java 90 KB

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