/** * Saves a OC File after a successful upload. * * <p>A PROPFIND is necessary to keep the props in the local database synchronized with the * server, specially the modification time and Etag (where available) * * <p>TODO refactor this ugly thing */ private void saveUploadedFile() { OCFile file = mCurrentUpload.getFile(); if (file.fileExists()) { file = mStorageManager.getFileById(file.getFileId()); } long syncDate = System.currentTimeMillis(); file.setLastSyncDateForData(syncDate); // new PROPFIND to keep data consistent with server // in theory, should return the same we already have ReadRemoteFileOperation operation = new ReadRemoteFileOperation(mCurrentUpload.getRemotePath()); RemoteOperationResult result = operation.execute(mUploadClient); if (result.isSuccess()) { updateOCFile(file, (RemoteFile) result.getData().get(0)); file.setLastSyncDateForProperties(syncDate); } // / maybe this would be better as part of UploadFileOperation... or // maybe all this method if (mCurrentUpload.wasRenamed()) { OCFile oldFile = mCurrentUpload.getOldFile(); if (oldFile.fileExists()) { oldFile.setStoragePath(null); mStorageManager.saveFile(oldFile); } // else: it was just an automatic renaming due to a name // coincidence; nothing else is needed, the storagePath is right // in the instance returned by mCurrentUpload.getFile() } mStorageManager.saveFile(file); }
private OCFile createRootDir() { OCFile file = new OCFile(OCFile.PATH_SEPARATOR); file.setMimetype("DIR"); file.setParentId(DataStorageManager.ROOT_PARENT_ID); saveFile(file); return file; }
@Override public void onNeutral(String callerTag) { File f = null; if (mFile.isDown() && (f = new File(mFile.getStoragePath())).exists()) { f.delete(); mFile.setStoragePath(null); mStorageManager.saveFile(mFile); updateFileDetails(mFile, mAccount); } }
/** Save new directory in local database */ public void saveFolderInDB() { OCFile newDir = new OCFile(mRemotePath); newDir.setMimetype("DIR"); long parentId = mStorageManager.getFileByPath(FileStorageUtils.getParentPath(mRemotePath)).getFileId(); newDir.setParentId(parentId); newDir.setModificationTimestamp(System.currentTimeMillis()); mStorageManager.saveFile(newDir); Log_OC.d(TAG, "Create directory " + mRemotePath + " in Database"); }
@Override public void onNeutral(String callerTag) { FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver()); File f = null; if (mFile.isDown() && (f = new File(mFile.getStoragePath())).exists()) { f.delete(); mFile.setStoragePath(null); fdsm.saveFile(mFile); updateFileDetails(mFile, mAccount); } }
private void saveLocalFile() { mFile.setFileName(mNewName); // try to rename the local copy of the file if (mFile.isDown()) { File f = new File(mFile.getStoragePath()); String parentStoragePath = f.getParent(); if (!parentStoragePath.endsWith(File.separator)) parentStoragePath += File.separator; if (f.renameTo(new File(parentStoragePath + mNewName))) { mFile.setStoragePath(parentStoragePath + mNewName); } // else - NOTHING: the link to the local file is kept although the local name can't be updated // TODO - study conditions when this could be a problem } mStorageManager.saveFile(mFile); }
private OCFile createLocalFolder(String remotePath) { String parentPath = new File(remotePath).getParent(); parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR; OCFile parent = mStorageManager.getFileByPath(parentPath); if (parent == null) { parent = createLocalFolder(parentPath); } if (parent != null) { OCFile createdFolder = new OCFile(remotePath); createdFolder.setMimetype("DIR"); createdFolder.setParentId(parent.getFileId()); mStorageManager.saveFile(createdFolder); return createdFolder; } return null; }
@Override public boolean saveFile(OCFile file) { boolean overriden = false; ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); cv.put( ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData()); cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype()); cv.put(ProviderTableMeta.FILE_NAME, file.getFileName()); if (file.getParentId() != 0) cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); if (!file.isDirectory()) cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData()); cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0); boolean sameRemotePath = fileExists(file.getRemotePath()); boolean changesSizeOfAncestors = false; if (sameRemotePath || fileExists(file.getFileId())) { // for renamed files; no more delete and create OCFile oldFile = null; if (sameRemotePath) { oldFile = getFileByPath(file.getRemotePath()); file.setFileId(oldFile.getFileId()); } else { oldFile = getFileById(file.getFileId()); } changesSizeOfAncestors = (oldFile.getFileLength() != file.getFileLength()); overriden = true; if (getContentResolver() != null) { getContentResolver() .update( ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?", new String[] {String.valueOf(file.getFileId())}); } else { try { getContentProvider() .update( ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?", new String[] {String.valueOf(file.getFileId())}); } catch (RemoteException e) { Log_OC.e(TAG, "Fail to insert insert file to database " + e.getMessage()); } } } else { changesSizeOfAncestors = true; Uri result_uri = null; if (getContentResolver() != null) { result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, cv); } else { try { result_uri = getContentProvider().insert(ProviderTableMeta.CONTENT_URI_FILE, cv); } catch (RemoteException e) { Log_OC.e(TAG, "Fail to insert insert file to database " + e.getMessage()); } } if (result_uri != null) { long new_id = Long.parseLong(result_uri.getPathSegments().get(1)); file.setFileId(new_id); } } if (file.isDirectory()) { calculateFolderSize(file.getFileId()); if (file.needsUpdatingWhileSaving()) { for (OCFile f : getDirectoryContent(file)) saveFile(f); } } if (changesSizeOfAncestors || file.isDirectory()) { updateSizesToTheRoot(file.getParentId()); } return overriden; }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.fdDownloadBtn: { // if (FileDownloader.isDownloading(mAccount, mFile.getRemotePath())) { FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder(); FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder(); if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) { downloaderBinder.cancel(mAccount, mFile); if (mFile.isDown()) { setButtonsForDown(); } else { setButtonsForRemote(); } } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile)) { uploaderBinder.cancel(mAccount, mFile); if (!mFile.fileExists()) { // TODO make something better if (getActivity() instanceof FileDisplayActivity) { // double pane FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction(); transaction.replace( R.id.file_details_container, new FileDetailFragment(null, null), FTAG); // empty FileDetailFragment transaction.commit(); mContainerActivity.onFileStateChanged(); } else { getActivity().finish(); } } else if (mFile.isDown()) { setButtonsForDown(); } else { setButtonsForRemote(); } } else { mLastRemoteOperation = new SynchronizeFileOperation( mFile, null, mStorageManager, mAccount, true, false, getActivity()); WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient( mAccount, getSherlockActivity().getApplicationContext()); mLastRemoteOperation.execute(wc, this, mHandler); // update ui boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; getActivity() .showDialog( (inDisplayActivity) ? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); setButtonsForTransferring(); // disable button immediately, although the synchronization // does not result in a file transference } break; } case R.id.fdKeepInSync: { CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync); mFile.setKeepInSync(cb.isChecked()); mStorageManager.saveFile(mFile); /// register the OCFile instance in the observer service to monitor local updates; /// if necessary, the file is download Intent intent = new Intent(getActivity().getApplicationContext(), FileObserverService.class); intent.putExtra( FileObserverService.KEY_FILE_CMD, (cb.isChecked() ? FileObserverService.CMD_ADD_OBSERVED_FILE : FileObserverService.CMD_DEL_OBSERVED_FILE)); intent.putExtra(FileObserverService.KEY_CMD_ARG_FILE, mFile); intent.putExtra(FileObserverService.KEY_CMD_ARG_ACCOUNT, mAccount); Log.e(TAG, "starting observer service"); getActivity().startService(intent); if (mFile.keepInSync()) { onClick( getView().findViewById(R.id.fdDownloadBtn)); // force an immediate synchronization } break; } case R.id.fdRenameBtn: { EditNameDialog dialog = EditNameDialog.newInstance(mFile.getFileName()); dialog.setOnDismissListener(this); dialog.show(getFragmentManager(), "nameeditdialog"); break; } case R.id.fdRemoveBtn: { ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance( R.string.confirmation_remove_alert, new String[] {mFile.getFileName()}, mFile.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote, mFile.isDown() ? R.string.confirmation_remove_local : -1, R.string.common_cancel); confDialog.setOnConfirmationListener(this); confDialog.show(getFragmentManager(), FTAG_CONFIRMATION); break; } case R.id.fdOpenBtn: { String storagePath = mFile.getStoragePath(); String encodedStoragePath = WebdavUtils.encodePath(storagePath); try { Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mFile.getMimetype()); i.setFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivity(i); } catch (Throwable t) { Log.e( TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype()); boolean toastIt = true; String mimeType = ""; try { Intent i = new Intent(Intent.ACTION_VIEW); mimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension( storagePath.substring(storagePath.lastIndexOf('.') + 1)); if (mimeType != null && !mimeType.equals(mFile.getMimetype())) { i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mimeType); i.setFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivity(i); toastIt = false; } } catch (IndexOutOfBoundsException e) { Log.e( TAG, "Trying to find out MIME type of a file without extension: " + storagePath); } catch (ActivityNotFoundException e) { Log.e( TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension"); } catch (Throwable th) { Log.e(TAG, "Unexpected problem when opening: " + storagePath, th); } finally { if (toastIt) { Toast.makeText( getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT) .show(); } } } break; } default: Log.e(TAG, "Incorrect view clicked!"); } /* else if (v.getId() == R.id.fdShareBtn) { Thread t = new Thread(new ShareRunnable(mFile.getRemotePath())); t.start(); }*/ }
public void run() { WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient( mAccount, getSherlockActivity().getApplicationContext()); AccountManager am = AccountManager.get(getSherlockActivity()); String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL); OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION)); String webdav_path = AccountUtils.getWebdavPath(ocv); Log.d("ASD", "" + baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath())); Log.e( "ASD", Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath())); LocalMoveMethod move = new LocalMoveMethod( baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath()), Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath())); boolean success = false; try { int status = wc.executeMethod(move); success = move.succeeded(); move.getResponseBodyAsString(); // exhaust response, although not interesting Log.d(TAG, "Move returned status: " + status); } catch (HttpException e) { Log.e( TAG, "HTTP Exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e); } catch (IOException e) { Log.e( TAG, "I/O Exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e); } catch (Exception e) { Log.e( TAG, "Unexpected exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e); } finally { move.releaseConnection(); } if (success) { FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver()); fdsm.removeFile(mOld, false); fdsm.saveFile(mNew); mFile = mNew; mHandler.post( new Runnable() { @Override public void run() { boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; getActivity() .dismissDialog( (inDisplayActivity) ? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); updateFileDetails(mFile, mAccount); mContainerActivity.onFileStateChanged(); } }); } else { mHandler.post( new Runnable() { @Override public void run() { // undo the local rename if (mNew.isDown()) { File f = new File(mNew.getStoragePath()); if (!f.renameTo(new File(mOld.getStoragePath()))) { // the local rename undoing failed; last chance: save the new local storage path // in the old file mFile.setStoragePath(mNew.getStoragePath()); FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver()); fdsm.saveFile(mFile); } } boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; getActivity() .dismissDialog( (inDisplayActivity) ? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); try { Toast msg = Toast.makeText( getActivity(), R.string.rename_server_fail_msg, Toast.LENGTH_LONG); msg.show(); } catch (NotFoundException e) { e.printStackTrace(); } } }); } }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.fdDownloadBtn: { Intent i = new Intent(getActivity(), FileDownloader.class); i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount); i.putExtra(FileDownloader.EXTRA_REMOTE_PATH, mFile.getRemotePath()); i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getRemotePath()); i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength()); // update ui setButtonsForTransferring(); getActivity().startService(i); mContainerActivity .onFileStateChanged(); // this is not working; it is performed before the // fileDownloadService registers it as 'in progress' break; } case R.id.fdKeepInSync: { CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync); mFile.setKeepInSync(cb.isChecked()); FileDataStorageManager fdsm = new FileDataStorageManager( mAccount, getActivity().getApplicationContext().getContentResolver()); fdsm.saveFile(mFile); if (mFile.keepInSync()) { onClick(getView().findViewById(R.id.fdDownloadBtn)); } else { mContainerActivity .onFileStateChanged(); // put inside 'else' to not call it twice (here, and in the // virtual click on fdDownloadBtn) } Intent intent = new Intent(getActivity().getApplicationContext(), FileObserverService.class); intent.putExtra( FileObserverService.KEY_FILE_CMD, (cb.isChecked() ? FileObserverService.CMD_ADD_OBSERVED_FILE : FileObserverService.CMD_DEL_OBSERVED_FILE)); intent.putExtra(FileObserverService.KEY_CMD_ARG, mFile.getStoragePath()); getActivity().startService(intent); break; } case R.id.fdRenameBtn: { EditNameFragment dialog = EditNameFragment.newInstance(mFile.getFileName()); dialog.show(getFragmentManager(), "nameeditdialog"); dialog.setOnDismissListener(this); break; } case R.id.fdRemoveBtn: { ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance( R.string.confirmation_remove_alert, new String[] {mFile.getFileName()}, mFile.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote, mFile.isDown() ? R.string.confirmation_remove_local : -1, R.string.common_cancel); confDialog.setOnConfirmationListener(this); confDialog.show(getFragmentManager(), FTAG_CONFIRMATION); break; } case R.id.fdOpenBtn: { String storagePath = mFile.getStoragePath(); String encodedStoragePath = WebdavUtils.encodePath(storagePath); try { Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mFile.getMimetype()); i.setFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivity(i); } catch (Throwable t) { Log.e( TAG, "Fail when trying to open with the mimeType provided from the ownCloud server: " + mFile.getMimetype()); boolean toastIt = true; String mimeType = ""; try { Intent i = new Intent(Intent.ACTION_VIEW); mimeType = MimeTypeMap.getSingleton() .getMimeTypeFromExtension( storagePath.substring(storagePath.lastIndexOf('.') + 1)); if (mimeType != null && !mimeType.equals(mFile.getMimetype())) { i.setDataAndType(Uri.parse("file://" + encodedStoragePath), mimeType); i.setFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivity(i); toastIt = false; } } catch (IndexOutOfBoundsException e) { Log.e( TAG, "Trying to find out MIME type of a file without extension: " + storagePath); } catch (ActivityNotFoundException e) { Log.e( TAG, "No activity found to handle: " + storagePath + " with MIME type " + mimeType + " obtained from extension"); } catch (Throwable th) { Log.e(TAG, "Unexpected problem when opening: " + storagePath, th); } finally { if (toastIt) { Toast.makeText( getActivity(), "There is no application to handle file " + mFile.getFileName(), Toast.LENGTH_SHORT) .show(); } } } break; } default: Log.e(TAG, "Incorrect view clicked!"); } /* else if (v.getId() == R.id.fdShareBtn) { Thread t = new Thread(new ShareRunnable(mFile.getRemotePath())); t.start(); }*/ }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.fdDownloadBtn: { FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder(); FileUploaderBinder uploaderBinder = mContainerActivity.getFileUploaderBinder(); if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) { downloaderBinder.cancel(mAccount, mFile); if (mFile.isDown()) { setButtonsForDown(); } else { setButtonsForRemote(); } } else if (uploaderBinder != null && uploaderBinder.isUploading(mAccount, mFile)) { uploaderBinder.cancel(mAccount, mFile); if (!mFile.fileExists()) { // TODO make something better if (getActivity() instanceof FileDisplayActivity) { // double pane FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction(); transaction.replace( R.id.file_details_container, new FileDetailFragment(null, null), FTAG); // empty FileDetailFragment transaction.commit(); mContainerActivity.onFileStateChanged(); } else { getActivity().finish(); } } else if (mFile.isDown()) { setButtonsForDown(); } else { setButtonsForRemote(); } } else { mLastRemoteOperation = new SynchronizeFileOperation( mFile, null, mStorageManager, mAccount, true, false, getActivity()); WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient( mAccount, getSherlockActivity().getApplicationContext()); mLastRemoteOperation.execute(wc, this, mHandler); // update ui boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity; getActivity() .showDialog( (inDisplayActivity) ? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT); } break; } case R.id.fdKeepInSync: { CheckBox cb = (CheckBox) getView().findViewById(R.id.fdKeepInSync); mFile.setKeepInSync(cb.isChecked()); mStorageManager.saveFile(mFile); /// register the OCFile instance in the observer service to monitor local updates; /// if necessary, the file is download Intent intent = new Intent(getActivity().getApplicationContext(), FileObserverService.class); intent.putExtra( FileObserverService.KEY_FILE_CMD, (cb.isChecked() ? FileObserverService.CMD_ADD_OBSERVED_FILE : FileObserverService.CMD_DEL_OBSERVED_FILE)); intent.putExtra(FileObserverService.KEY_CMD_ARG_FILE, mFile); intent.putExtra(FileObserverService.KEY_CMD_ARG_ACCOUNT, mAccount); getActivity().startService(intent); if (mFile.keepInSync()) { onClick( getView().findViewById(R.id.fdDownloadBtn)); // force an immediate synchronization } break; } case R.id.fdRenameBtn: { EditNameDialog dialog = EditNameDialog.newInstance( getString(R.string.rename_dialog_title), mFile.getFileName(), this); dialog.show(getFragmentManager(), "nameeditdialog"); break; } case R.id.fdRemoveBtn: { ConfirmationDialogFragment confDialog = ConfirmationDialogFragment.newInstance( R.string.confirmation_remove_alert, new String[] {mFile.getFileName()}, mFile.isDown() ? R.string.confirmation_remove_remote_and_local : R.string.confirmation_remove_remote, mFile.isDown() ? R.string.confirmation_remove_local : -1, R.string.common_cancel); confDialog.setOnConfirmationListener(this); confDialog.show(getFragmentManager(), FTAG_CONFIRMATION); break; } case R.id.fdOpenBtn: { openFile(); break; } case R.id.fdShareBtn: {; break; } default: Log.e(TAG, "Incorrect view clicked!"); } /* else if (v.getId() == R.id.fdShareBtn) { Thread t = new Thread(new ShareRunnable(mFile.getRemotePath())); t.start(); }*/ }