FileDataStorageManager.java 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  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 && cursor.moveToFirst()) {
  996. share = createShareInstance(cursor);
  997. cursor.close();
  998. }
  999. return share;
  1000. }
  1001. private OCShare createShareInstance(Cursor c) {
  1002. OCShare share = null;
  1003. if (c != null) {
  1004. share = new OCShare(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_PATH)));
  1005. share.setId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  1006. share.setFileSource(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ITEM_SOURCE)));
  1007. share.setShareType(ShareType.fromValue(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_TYPE))));
  1008. share.setShareWith(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH)));
  1009. share.setPermissions(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_PERMISSIONS)));
  1010. share.setSharedDate(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARED_DATE)));
  1011. share.setExpirationDate(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_EXPIRATION_DATE)));
  1012. share.setToken(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_TOKEN)));
  1013. share.setSharedWithDisplayName(
  1014. c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME)));
  1015. share.setIsFolder(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_IS_DIRECTORY)) == 1);
  1016. share.setUserId(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_USER_ID)));
  1017. share.setIdRemoteShared(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED)));
  1018. }
  1019. return share;
  1020. }
  1021. private void resetShareFlagsInAllFiles() {
  1022. ContentValues cv = new ContentValues();
  1023. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1024. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1025. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1026. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
  1027. String[] whereArgs = new String[]{mAccount.name};
  1028. if (getContentResolver() != null) {
  1029. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1030. } else {
  1031. try {
  1032. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1033. } catch (RemoteException e) {
  1034. Log_OC.e(TAG, "Exception in resetShareFlagsInAllFiles" + e.getMessage(), e);
  1035. }
  1036. }
  1037. }
  1038. private void resetShareFlagsInFolder(OCFile folder) {
  1039. ContentValues cv = new ContentValues();
  1040. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1041. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1042. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1043. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PARENT + "=?";
  1044. String [] whereArgs = new String[] { mAccount.name , String.valueOf(folder.getFileId()) };
  1045. if (getContentResolver() != null) {
  1046. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1047. } else {
  1048. try {
  1049. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1050. } catch (RemoteException e) {
  1051. Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e);
  1052. }
  1053. }
  1054. }
  1055. private void resetShareFlagInAFile(String filePath){
  1056. ContentValues cv = new ContentValues();
  1057. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, false);
  1058. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, false);
  1059. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
  1060. String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH+ "=?";
  1061. String [] whereArgs = new String[] { mAccount.name , filePath };
  1062. if (getContentResolver() != null) {
  1063. getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1064. } else {
  1065. try {
  1066. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
  1067. } catch (RemoteException e) {
  1068. Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e);
  1069. }
  1070. }
  1071. }
  1072. private void cleanShares() {
  1073. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1074. String[] whereArgs = new String[]{mAccount.name};
  1075. if (getContentResolver() != null) {
  1076. getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
  1077. } else {
  1078. try {
  1079. getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
  1080. } catch (RemoteException e) {
  1081. Log_OC.e(TAG, "Exception in cleanShares" + e.getMessage(), e);
  1082. }
  1083. }
  1084. }
  1085. public void saveShares(Collection<OCShare> shares) {
  1086. cleanShares();
  1087. if (shares != null) {
  1088. ArrayList<ContentProviderOperation> operations =
  1089. new ArrayList<ContentProviderOperation>(shares.size());
  1090. // prepare operations to insert or update files to save in the given folder
  1091. for (OCShare share : shares) {
  1092. ContentValues cv = new ContentValues();
  1093. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  1094. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  1095. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  1096. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  1097. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  1098. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  1099. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  1100. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  1101. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  1102. cv.put(
  1103. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  1104. share.getSharedWithDisplayName()
  1105. );
  1106. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  1107. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  1108. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  1109. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  1110. if (shareExistsForRemoteId(share.getRemoteId())) {
  1111. // updating an existing file
  1112. operations.add(
  1113. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
  1114. withValues(cv).
  1115. withSelection(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
  1116. new String[]{String.valueOf(share.getRemoteId())})
  1117. .build());
  1118. } else {
  1119. // adding a new file
  1120. operations.add(
  1121. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).
  1122. withValues(cv).
  1123. build()
  1124. );
  1125. }
  1126. }
  1127. // apply operations in batch
  1128. if (operations.size() > 0) {
  1129. @SuppressWarnings("unused")
  1130. ContentProviderResult[] results = null;
  1131. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1132. try {
  1133. if (getContentResolver() != null) {
  1134. results = getContentResolver().applyBatch(MainApp.getAuthority(),
  1135. operations);
  1136. } else {
  1137. results = getContentProviderClient().applyBatch(operations);
  1138. }
  1139. } catch (OperationApplicationException | RemoteException e) {
  1140. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1141. }
  1142. }
  1143. }
  1144. }
  1145. public void updateSharedFiles(Collection<OCFile> sharedFiles) {
  1146. resetShareFlagsInAllFiles();
  1147. if (sharedFiles != null) {
  1148. ArrayList<ContentProviderOperation> operations = new ArrayList<>(sharedFiles.size());
  1149. // prepare operations to insert or update files to save in the given folder
  1150. for (OCFile file : sharedFiles) {
  1151. ContentValues cv = new ContentValues();
  1152. cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
  1153. cv.put(
  1154. ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
  1155. file.getModificationTimestampAtLastSyncForData()
  1156. );
  1157. cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
  1158. cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
  1159. cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
  1160. cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
  1161. cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
  1162. cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
  1163. if (!file.isFolder()) {
  1164. cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
  1165. }
  1166. cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
  1167. cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
  1168. cv.put(
  1169. ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA,
  1170. file.getLastSyncDateForData()
  1171. );
  1172. cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.isFavorite() ? 1 : 0);
  1173. cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
  1174. cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0);
  1175. cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0);
  1176. cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
  1177. cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
  1178. cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
  1179. cv.put(
  1180. ProviderTableMeta.FILE_UPDATE_THUMBNAIL,
  1181. file.needsUpdateThumbnail() ? 1 : 0
  1182. );
  1183. cv.put(
  1184. ProviderTableMeta.FILE_IS_DOWNLOADING,
  1185. file.isDownloading() ? 1 : 0
  1186. );
  1187. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict());
  1188. boolean existsByPath = fileExists(file.getRemotePath());
  1189. if (existsByPath || fileExists(file.getFileId())) {
  1190. // updating an existing file
  1191. operations.add(
  1192. ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
  1193. withValues(cv).
  1194. withSelection(ProviderTableMeta._ID + "=?",
  1195. new String[]{String.valueOf(file.getFileId())})
  1196. .build());
  1197. } else {
  1198. // adding a new file
  1199. operations.add(
  1200. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).
  1201. withValues(cv).
  1202. build()
  1203. );
  1204. }
  1205. }
  1206. // apply operations in batch
  1207. if (operations.size() > 0) {
  1208. @SuppressWarnings("unused")
  1209. ContentProviderResult[] results = null;
  1210. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1211. try {
  1212. if (getContentResolver() != null) {
  1213. results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1214. } else {
  1215. results = getContentProviderClient().applyBatch(operations);
  1216. }
  1217. } catch (OperationApplicationException | RemoteException e) {
  1218. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1219. }
  1220. }
  1221. }
  1222. }
  1223. public void removeShare(OCShare share) {
  1224. Uri share_uri = ProviderTableMeta.CONTENT_URI_SHARE;
  1225. String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND +
  1226. ProviderTableMeta._ID + "=?";
  1227. String [] whereArgs = new String[]{mAccount.name, Long.toString(share.getId())};
  1228. if (getContentProviderClient() != null) {
  1229. try {
  1230. getContentProviderClient().delete(share_uri, where, whereArgs);
  1231. } catch (RemoteException e) {
  1232. Log_OC.d(TAG, e.getMessage(), e);
  1233. }
  1234. } else {
  1235. getContentResolver().delete(share_uri, where, whereArgs);
  1236. }
  1237. }
  1238. public void saveSharesDB(ArrayList<OCShare> shares) {
  1239. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1240. // Reset flags & Remove shares for this files
  1241. String filePath = "";
  1242. for (OCShare share: shares) {
  1243. if (filePath != share.getPath()){
  1244. filePath = share.getPath();
  1245. resetShareFlagInAFile(filePath);
  1246. operations = prepareRemoveSharesInFile(filePath, operations);
  1247. }
  1248. }
  1249. // Add operations to insert shares
  1250. operations = prepareInsertShares(shares, operations);
  1251. // apply operations in batch
  1252. if (operations.size() > 0) {
  1253. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1254. try {
  1255. if (getContentResolver() != null) {
  1256. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1257. } else {
  1258. getContentProviderClient().applyBatch(operations);
  1259. }
  1260. } catch (OperationApplicationException | RemoteException e) {
  1261. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1262. }
  1263. }
  1264. // // TODO: review if it is needed
  1265. // // Update shared files
  1266. // ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
  1267. //
  1268. // for (OCShare share : shares) {
  1269. // // Get the path
  1270. // String path = share.getPath();
  1271. // if (share.isFolder()) {
  1272. // path = path + FileUtils.PATH_SEPARATOR;
  1273. // }
  1274. //
  1275. // // Update OCFile with data from share: ShareByLink, publicLink and
  1276. // OCFile file = getFileByPath(path);
  1277. // if (file != null) {
  1278. // if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
  1279. // file.setShareViaLink(true);
  1280. // sharedFiles.add(file);
  1281. // }
  1282. // }
  1283. // }
  1284. //
  1285. // // TODO: Review
  1286. // updateSharedFiles(sharedFiles);
  1287. }
  1288. public void removeSharesForFile(String remotePath) {
  1289. resetShareFlagInAFile(remotePath);
  1290. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1291. operations = prepareRemoveSharesInFile(remotePath, operations);
  1292. // apply operations in batch
  1293. if (operations.size() > 0) {
  1294. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1295. try {
  1296. if (getContentResolver() != null) {
  1297. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1298. } else {
  1299. getContentProviderClient().applyBatch(operations);
  1300. }
  1301. } catch (OperationApplicationException | RemoteException e) {
  1302. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1303. }
  1304. }
  1305. }
  1306. public void saveSharesInFolder(ArrayList<OCShare> shares, OCFile folder) {
  1307. resetShareFlagsInFolder(folder);
  1308. ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
  1309. operations = prepareRemoveSharesInFolder(folder, operations);
  1310. if (shares != null) {
  1311. // prepare operations to insert or update files to save in the given folder
  1312. operations = prepareInsertShares(shares, operations);
  1313. }
  1314. // apply operations in batch
  1315. if (operations.size() > 0) {
  1316. Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size()));
  1317. try {
  1318. if (getContentResolver() != null) {
  1319. getContentResolver().applyBatch(MainApp.getAuthority(), operations);
  1320. } else {
  1321. getContentProviderClient().applyBatch(operations);
  1322. }
  1323. } catch (OperationApplicationException | RemoteException e) {
  1324. Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e);
  1325. }
  1326. }
  1327. }
  1328. /**
  1329. * Prepare operations to insert or update files to save in the given folder
  1330. * @param shares List of shares to insert
  1331. * @param operations List of operations
  1332. * @return
  1333. */
  1334. private ArrayList<ContentProviderOperation> prepareInsertShares(
  1335. ArrayList<OCShare> shares, ArrayList<ContentProviderOperation> operations) {
  1336. if (shares != null) {
  1337. // prepare operations to insert or update files to save in the given folder
  1338. for (OCShare share : shares) {
  1339. ContentValues cv = new ContentValues();
  1340. cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
  1341. cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
  1342. cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
  1343. cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
  1344. cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
  1345. cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
  1346. cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
  1347. cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
  1348. cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
  1349. cv.put(
  1350. ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
  1351. share.getSharedWithDisplayName()
  1352. );
  1353. cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
  1354. cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
  1355. cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId());
  1356. cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
  1357. // adding a new share resource
  1358. operations.add(
  1359. ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).
  1360. withValues(cv).
  1361. build()
  1362. );
  1363. }
  1364. }
  1365. return operations;
  1366. }
  1367. private ArrayList<ContentProviderOperation> prepareRemoveSharesInFolder(
  1368. OCFile folder, ArrayList<ContentProviderOperation> preparedOperations) {
  1369. if (folder != null) {
  1370. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1371. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1372. String [] whereArgs = new String[]{ "", mAccount.name };
  1373. Vector<OCFile> files = getFolderContent(folder, false);
  1374. for (OCFile file : files) {
  1375. whereArgs[0] = file.getRemotePath();
  1376. preparedOperations.add(
  1377. ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE).
  1378. withSelection(where, whereArgs).
  1379. build()
  1380. );
  1381. }
  1382. }
  1383. return preparedOperations;
  1384. }
  1385. private ArrayList<ContentProviderOperation> prepareRemoveSharesInFile(
  1386. String filePath, ArrayList<ContentProviderOperation> preparedOperations) {
  1387. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1388. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
  1389. String[] whereArgs = new String[]{filePath, mAccount.name};
  1390. preparedOperations.add(
  1391. ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE).
  1392. withSelection(where, whereArgs).
  1393. build()
  1394. );
  1395. return preparedOperations;
  1396. }
  1397. public ArrayList<OCShare> getSharesWithForAFile(String filePath, String accountName){
  1398. // Condition
  1399. String where = ProviderTableMeta.OCSHARES_PATH + AND
  1400. + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND
  1401. + " (" + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR "
  1402. + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR "
  1403. + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? ) ";
  1404. String [] whereArgs = new String[]{ filePath, accountName ,
  1405. Integer.toString(ShareType.USER.getValue()),
  1406. Integer.toString(ShareType.GROUP.getValue()),
  1407. Integer.toString(ShareType.FEDERATED.getValue())};
  1408. Cursor cursor = null;
  1409. if (getContentResolver() != null) {
  1410. cursor = getContentResolver().query(
  1411. ProviderTableMeta.CONTENT_URI_SHARE,
  1412. null, where, whereArgs, null);
  1413. } else {
  1414. try {
  1415. cursor = getContentProviderClient().query(
  1416. ProviderTableMeta.CONTENT_URI_SHARE,
  1417. null, where, whereArgs, null);
  1418. } catch (RemoteException e) {
  1419. Log_OC.e(TAG, "Could not get list of shares with: " + e.getMessage(), e);
  1420. cursor = null;
  1421. }
  1422. }
  1423. ArrayList<OCShare> shares = new ArrayList<OCShare>();
  1424. OCShare share = null;
  1425. if (cursor != null && cursor.moveToFirst()) {
  1426. do {
  1427. share = createShareInstance(cursor);
  1428. shares.add(share);
  1429. } while (cursor.moveToNext());
  1430. cursor.close();
  1431. }
  1432. return shares;
  1433. }
  1434. public static void triggerMediaScan(String path) {
  1435. if (path != null) {
  1436. Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
  1437. intent.setData(Uri.fromFile(new File(path)));
  1438. MainApp.getAppContext().sendBroadcast(intent);
  1439. }
  1440. }
  1441. public void deleteFileInMediaScan(String path) {
  1442. String mimetypeString = FileStorageUtils.getMimeTypeFromName(path);
  1443. ContentResolver contentResolver = getContentResolver();
  1444. if (contentResolver != null) {
  1445. if (MimeTypeUtil.isImage(mimetypeString)) {
  1446. // Images
  1447. contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  1448. MediaStore.Images.Media.DATA + "=?", new String[]{path});
  1449. } else if (MimeTypeUtil.isAudio(mimetypeString)) {
  1450. // Audio
  1451. contentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  1452. MediaStore.Audio.Media.DATA + "=?", new String[]{path});
  1453. } else if (MimeTypeUtil.isVideo(mimetypeString)) {
  1454. // Video
  1455. contentResolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  1456. MediaStore.Video.Media.DATA + "=?", new String[]{path});
  1457. }
  1458. } else {
  1459. ContentProviderClient contentProviderClient = getContentProviderClient();
  1460. try {
  1461. if (MimeTypeUtil.isImage(mimetypeString)) {
  1462. // Images
  1463. contentProviderClient.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
  1464. MediaStore.Images.Media.DATA + "=?", new String[]{path});
  1465. } else if (MimeTypeUtil.isAudio(mimetypeString)) {
  1466. // Audio
  1467. contentProviderClient.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  1468. MediaStore.Audio.Media.DATA + "=?", new String[]{path});
  1469. } else if (MimeTypeUtil.isVideo(mimetypeString)) {
  1470. // Video
  1471. contentProviderClient.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  1472. MediaStore.Video.Media.DATA + "=?", new String[]{path});
  1473. }
  1474. } catch (RemoteException e) {
  1475. Log_OC.e(TAG, "Exception deleting media file in MediaStore " + e.getMessage(), e);
  1476. }
  1477. }
  1478. }
  1479. public void saveConflict(OCFile file, String etagInConflict) {
  1480. if (!file.isDown()) {
  1481. etagInConflict = null;
  1482. }
  1483. ContentValues cv = new ContentValues();
  1484. cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, etagInConflict);
  1485. int updated = 0;
  1486. if (getContentResolver() != null) {
  1487. updated = getContentResolver().update(
  1488. ProviderTableMeta.CONTENT_URI_FILE,
  1489. cv,
  1490. ProviderTableMeta._ID + "=?",
  1491. new String[] { String.valueOf(file.getFileId())}
  1492. );
  1493. } else {
  1494. try {
  1495. updated = getContentProviderClient().update(
  1496. ProviderTableMeta.CONTENT_URI_FILE,
  1497. cv,
  1498. ProviderTableMeta._ID + "=?",
  1499. new String[]{String.valueOf(file.getFileId())}
  1500. );
  1501. } catch (RemoteException e) {
  1502. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1503. }
  1504. }
  1505. Log_OC.d(TAG, "Number of files updated with CONFLICT: " + updated);
  1506. if (updated > 0) {
  1507. if (etagInConflict != null) {
  1508. /// set conflict in all ancestor folders
  1509. long parentId = file.getParentId();
  1510. Set<String> ancestorIds = new HashSet<>();
  1511. while (parentId != FileDataStorageManager.ROOT_PARENT_ID) {
  1512. ancestorIds.add(Long.toString(parentId));
  1513. parentId = getFileById(parentId).getParentId();
  1514. }
  1515. if (ancestorIds.size() > 0) {
  1516. final StringBuffer whereBuffer = new StringBuffer();
  1517. whereBuffer.append(ProviderTableMeta._ID).append(" IN (");
  1518. for (int i = 0; i < ancestorIds.size() - 1; i++) {
  1519. whereBuffer.append("?,");
  1520. }
  1521. whereBuffer.append("?)");
  1522. if (getContentResolver() != null) {
  1523. updated = getContentResolver().update(
  1524. ProviderTableMeta.CONTENT_URI_FILE,
  1525. cv,
  1526. whereBuffer.toString(),
  1527. ancestorIds.toArray(new String[]{})
  1528. );
  1529. } else {
  1530. try {
  1531. updated = getContentProviderClient().update(
  1532. ProviderTableMeta.CONTENT_URI_FILE,
  1533. cv,
  1534. whereBuffer.toString(),
  1535. ancestorIds.toArray(new String[]{})
  1536. );
  1537. } catch (RemoteException e) {
  1538. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1539. }
  1540. }
  1541. } // else file is ROOT folder, no parent to set in conflict
  1542. } else {
  1543. /// update conflict in ancestor folders
  1544. // (not directly unset; maybe there are more conflicts below them)
  1545. String parentPath = file.getRemotePath();
  1546. if (parentPath.endsWith(OCFile.PATH_SEPARATOR)) {
  1547. parentPath = parentPath.substring(0, parentPath.length() - 1);
  1548. }
  1549. parentPath = parentPath.substring(0, parentPath.lastIndexOf(OCFile.PATH_SEPARATOR) + 1);
  1550. Log_OC.d(TAG, "checking parents to remove conflict; STARTING with " + parentPath);
  1551. while (parentPath.length() > 0) {
  1552. String whereForDescencentsInConflict =
  1553. ProviderTableMeta.FILE_ETAG_IN_CONFLICT + " IS NOT NULL AND " +
  1554. ProviderTableMeta.FILE_CONTENT_TYPE + " != 'DIR' AND " +
  1555. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1556. ProviderTableMeta.FILE_PATH + " LIKE ?";
  1557. Cursor descendentsInConflict = null;
  1558. if (getContentResolver() != null) {
  1559. descendentsInConflict = getContentResolver().query(
  1560. ProviderTableMeta.CONTENT_URI_FILE,
  1561. new String[]{ProviderTableMeta._ID},
  1562. whereForDescencentsInConflict,
  1563. new String[]{mAccount.name, parentPath + "%"},
  1564. null
  1565. );
  1566. } else {
  1567. try {
  1568. descendentsInConflict = getContentProviderClient().query(
  1569. ProviderTableMeta.CONTENT_URI_FILE,
  1570. new String[]{ProviderTableMeta._ID},
  1571. whereForDescencentsInConflict,
  1572. new String[]{mAccount.name, parentPath + "%"},
  1573. null
  1574. );
  1575. } catch (RemoteException e) {
  1576. Log_OC.e(TAG, "Failed querying for descendents in conflict " + e.getMessage(), e);
  1577. }
  1578. }
  1579. if (descendentsInConflict == null || descendentsInConflict.getCount() == 0) {
  1580. Log_OC.d(TAG, "NO MORE conflicts in " + parentPath);
  1581. if (getContentResolver() != null) {
  1582. updated = getContentResolver().update(
  1583. ProviderTableMeta.CONTENT_URI_FILE,
  1584. cv,
  1585. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1586. ProviderTableMeta.FILE_PATH + "=?",
  1587. new String[]{mAccount.name, parentPath}
  1588. );
  1589. } else {
  1590. try {
  1591. updated = getContentProviderClient().update(
  1592. ProviderTableMeta.CONTENT_URI_FILE,
  1593. cv,
  1594. ProviderTableMeta.FILE_ACCOUNT_OWNER + AND +
  1595. ProviderTableMeta.FILE_PATH + "=?"
  1596. , new String[]{mAccount.name, parentPath}
  1597. );
  1598. } catch (RemoteException e) {
  1599. Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e);
  1600. }
  1601. }
  1602. } else {
  1603. Log_OC.d(TAG, "STILL " + descendentsInConflict.getCount() + " in " + parentPath);
  1604. }
  1605. if (descendentsInConflict != null) {
  1606. descendentsInConflict.close();
  1607. }
  1608. parentPath = parentPath.substring(0, parentPath.length() - 1); // trim last /
  1609. parentPath = parentPath.substring(0, parentPath.lastIndexOf(OCFile.PATH_SEPARATOR) + 1);
  1610. Log_OC.d(TAG, "checking parents to remove conflict; NEXT " + parentPath);
  1611. }
  1612. }
  1613. }
  1614. }
  1615. public OCCapability saveCapabilities(OCCapability capability){
  1616. // Prepare capabilities data
  1617. ContentValues cv = new ContentValues();
  1618. cv.put(ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME, mAccount.name);
  1619. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MAYOR, capability.getVersionMayor());
  1620. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MINOR, capability.getVersionMinor());
  1621. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MICRO, capability.getVersionMicro());
  1622. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_STRING, capability.getVersionString());
  1623. cv.put(ProviderTableMeta.CAPABILITIES_VERSION_EDITION, capability.getVersionEdition());
  1624. cv.put(ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL, capability.getCorePollinterval());
  1625. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED, capability.getFilesSharingApiEnabled().getValue());
  1626. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED,
  1627. capability.getFilesSharingPublicEnabled().getValue());
  1628. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED,
  1629. capability.getFilesSharingPublicPasswordEnforced().getValue());
  1630. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED,
  1631. capability.getFilesSharingPublicExpireDateEnabled().getValue());
  1632. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS,
  1633. capability.getFilesSharingPublicExpireDateDays());
  1634. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED,
  1635. capability.getFilesSharingPublicExpireDateEnforced().getValue());
  1636. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL,
  1637. capability.getFilesSharingPublicSendMail().getValue());
  1638. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD,
  1639. capability.getFilesSharingPublicUpload().getValue());
  1640. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL,
  1641. capability.getFilesSharingUserSendMail().getValue());
  1642. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_RESHARING, capability.getFilesSharingResharing().getValue());
  1643. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING,
  1644. capability.getFilesSharingFederationOutgoing().getValue());
  1645. cv.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING,
  1646. capability.getFilesSharingFederationIncoming().getValue());
  1647. cv.put(ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING, capability.getFilesBigFileChuncking().getValue());
  1648. cv.put(ProviderTableMeta.CAPABILITIES_FILES_UNDELETE, capability.getFilesUndelete().getValue());
  1649. cv.put(ProviderTableMeta.CAPABILITIES_FILES_VERSIONING, capability.getFilesVersioning().getValue());
  1650. cv.put(ProviderTableMeta.CAPABILITIES_FILES_DROP, capability.getFilesFileDrop().getValue());
  1651. if (capabilityExists(mAccount.name)) {
  1652. if (getContentResolver() != null) {
  1653. getContentResolver().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv,
  1654. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?",
  1655. new String[]{mAccount.name});
  1656. } else {
  1657. try {
  1658. getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1659. cv, ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?",
  1660. new String[]{mAccount.name});
  1661. } catch (RemoteException e) {
  1662. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  1663. }
  1664. }
  1665. } else {
  1666. Uri result_uri = null;
  1667. if (getContentResolver() != null) {
  1668. result_uri = getContentResolver().insert(
  1669. ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv);
  1670. } else {
  1671. try {
  1672. result_uri = getContentProviderClient().insert(
  1673. ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv);
  1674. } catch (RemoteException e) {
  1675. Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e);
  1676. }
  1677. }
  1678. if (result_uri != null) {
  1679. long new_id = Long.parseLong(result_uri.getPathSegments()
  1680. .get(1));
  1681. capability.setId(new_id);
  1682. capability.setAccountName(mAccount.name);
  1683. }
  1684. }
  1685. return capability;
  1686. }
  1687. private boolean capabilityExists(String accountName) {
  1688. Cursor c = getCapabilityCursorForAccount(accountName);
  1689. boolean exists = false;
  1690. if (c != null) {
  1691. exists = c.moveToFirst();
  1692. c.close();
  1693. }
  1694. return exists;
  1695. }
  1696. private Cursor getCapabilityCursorForAccount(String accountName){
  1697. Cursor c = null;
  1698. if (getContentResolver() != null) {
  1699. c = getContentResolver()
  1700. .query(ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1701. null,
  1702. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ",
  1703. new String[]{accountName}, null);
  1704. } else {
  1705. try {
  1706. c = getContentProviderClient().query(
  1707. ProviderTableMeta.CONTENT_URI_CAPABILITIES,
  1708. null,
  1709. ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ",
  1710. new String[]{accountName}, null);
  1711. } catch (RemoteException e) {
  1712. Log_OC.e(TAG, "Couldn't determine capability existence, assuming non existance: " + e.getMessage(), e);
  1713. }
  1714. }
  1715. return c;
  1716. }
  1717. public OCCapability getCapability(String accountName){
  1718. OCCapability capability = null;
  1719. Cursor c = getCapabilityCursorForAccount(accountName);
  1720. if (c.moveToFirst()) {
  1721. capability = createCapabilityInstance(c);
  1722. } else {
  1723. capability = new OCCapability(); // return default with all UNKNOWN
  1724. }
  1725. c.close();
  1726. return capability;
  1727. }
  1728. private OCCapability createCapabilityInstance(Cursor c) {
  1729. OCCapability capability = null;
  1730. if (c != null) {
  1731. capability = new OCCapability();
  1732. capability.setId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
  1733. capability.setAccountName(c.getString(c
  1734. .getColumnIndex(ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME)));
  1735. capability.setVersionMayor(c.getInt(c
  1736. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MAYOR)));
  1737. capability.setVersionMinor(c.getInt(c
  1738. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MINOR)));
  1739. capability.setVersionMicro(c.getInt(c
  1740. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_MICRO)));
  1741. capability.setVersionString(c.getString(c
  1742. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_STRING)));
  1743. capability.setVersionEdition(c.getString(c
  1744. .getColumnIndex(ProviderTableMeta.CAPABILITIES_VERSION_EDITION)));
  1745. capability.setCorePollinterval(c.getInt(c
  1746. .getColumnIndex(ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL)));
  1747. capability.setFilesSharingApiEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1748. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED))));
  1749. capability.setFilesSharingPublicEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1750. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED))));
  1751. capability.setFilesSharingPublicPasswordEnforced(CapabilityBooleanType.fromValue(c.getInt(c
  1752. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED))));
  1753. capability.setFilesSharingPublicExpireDateEnabled(CapabilityBooleanType.fromValue(c.getInt(c
  1754. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED))));
  1755. capability.setFilesSharingPublicExpireDateDays(c.getInt(c
  1756. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS)));
  1757. capability.setFilesSharingPublicExpireDateEnforced(CapabilityBooleanType.fromValue(c.getInt(c
  1758. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED))));
  1759. capability.setFilesSharingPublicSendMail(CapabilityBooleanType.fromValue(c.getInt(c
  1760. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL))));
  1761. capability.setFilesSharingPublicUpload(CapabilityBooleanType.fromValue(c.getInt(c
  1762. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD))));
  1763. capability.setFilesSharingUserSendMail(CapabilityBooleanType.fromValue(c.getInt(c
  1764. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL))));
  1765. capability.setFilesSharingResharing(CapabilityBooleanType.fromValue(c.getInt(c
  1766. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_RESHARING))));
  1767. capability.setFilesSharingFederationOutgoing(CapabilityBooleanType.fromValue(c.getInt(c
  1768. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING))));
  1769. capability.setFilesSharingFederationIncoming(CapabilityBooleanType.fromValue(c.getInt(c
  1770. .getColumnIndex(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING))));
  1771. capability.setFilesBigFileChuncking(CapabilityBooleanType.fromValue(c.getInt(c
  1772. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING))));
  1773. capability.setFilesUndelete(CapabilityBooleanType.fromValue(c.getInt(c
  1774. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_UNDELETE))));
  1775. capability.setFilesVersioning(CapabilityBooleanType.fromValue(c.getInt(c
  1776. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_VERSIONING))));
  1777. capability.setFilesFileDrop(CapabilityBooleanType.fromValue(c.getInt(c
  1778. .getColumnIndex(ProviderTableMeta.CAPABILITIES_FILES_DROP))));
  1779. }
  1780. return capability;
  1781. }
  1782. }