1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.ui
.dialog
;
21 * Dialog requiring confirmation before removing a given OCFile.
23 * Triggers the removal according to the user response.
25 * @author David A. Velasco
28 import java
.util
.Vector
;
30 import com
.owncloud
.android
.R
;
31 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
32 import com
.owncloud
.android
.datamodel
.OCFile
;
33 import com
.owncloud
.android
.ui
.activity
.ComponentsGetter
;
34 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
;
36 import android
.app
.Dialog
;
37 import android
.media
.MediaScannerConnection
;
38 import android
.os
.Bundle
;
40 public class RemoveFileDialogFragment
extends ConfirmationDialogFragment
41 implements ConfirmationDialogFragmentListener
{
43 private static final String ARG_TARGET_FILE
= "TARGET_FILE";
46 * Public factory method to create new RemoveFileDialogFragment instances.
48 * @param file File to remove.
49 * @return Dialog ready to show.
51 public static RemoveFileDialogFragment
newInstance(OCFile file
) {
52 RemoveFileDialogFragment frag
= new RemoveFileDialogFragment();
53 Bundle args
= new Bundle();
55 int messageStringId
= R
.string
.confirmation_remove_alert
;
57 int posBtn
= R
.string
.confirmation_remove_remote
;
59 if (file
.isFolder()) {
60 messageStringId
= R
.string
.confirmation_remove_folder_alert
;
61 posBtn
= R
.string
.confirmation_remove_remote_and_local
;
62 neuBtn
= R
.string
.confirmation_remove_folder_local
;
63 } else if (file
.isDown()) {
64 posBtn
= R
.string
.confirmation_remove_remote_and_local
;
65 neuBtn
= R
.string
.confirmation_remove_local
;
69 args
.putInt(ARG_CONF_RESOURCE_ID
, messageStringId
);
70 args
.putStringArray(ARG_CONF_ARGUMENTS
, new String
[]{file
.getFileName()});
71 args
.putInt(ARG_POSITIVE_BTN_RES
, posBtn
);
72 args
.putInt(ARG_NEUTRAL_BTN_RES
, neuBtn
);
73 args
.putInt(ARG_NEGATIVE_BTN_RES
, R
.string
.common_cancel
);
74 args
.putParcelable(ARG_TARGET_FILE
, file
);
75 frag
.setArguments(args
);
80 private OCFile mTargetFile
;
83 public Dialog
onCreateDialog(Bundle savedInstanceState
) {
84 Dialog dialog
= super.onCreateDialog(savedInstanceState
);
85 mTargetFile
= getArguments().getParcelable(ARG_TARGET_FILE
);
87 setOnConfirmationListener(this);
93 * Performs the removal of the target file, both locally and in the server.
96 public void onConfirmation(String callerTag
) {
97 ComponentsGetter cg
= (ComponentsGetter
)getSherlockActivity();
98 FileDataStorageManager storageManager
= cg
.getStorageManager();
99 if (storageManager
.getFileById(mTargetFile
.getFileId()) != null
) {
100 String path
= new File(mTargetFile
.getStoragePath()).getParent();
101 cg
.getFileOperationsHelper().removeFile(mTargetFile
, false
);
102 triggerMediaScan(path
);
107 * Performs the removal of the local copy of the target file
110 public void onNeutral(String callerTag
) {
111 String path
= new File(mTargetFile
.getStoragePath()).getParent();
112 ComponentsGetter cg
= (ComponentsGetter
)getSherlockActivity();
113 cg
.getFileOperationsHelper()
114 .removeFile(mTargetFile
, true
);
116 FileDataStorageManager storageManager
= cg
.getStorageManager();
118 boolean containsKeepInSync
= false
;
119 if (mTargetFile
.isFolder()) {
120 Vector
<OCFile
> files
= storageManager
.getFolderContent(mTargetFile
);
121 for(OCFile file
: files
) {
122 containsKeepInSync
= file
.keepInSync() || containsKeepInSync
;
124 if (containsKeepInSync
)
129 // Remove etag for parent, if file is a keep_in_sync
130 // or is a folder and contains keep_in_sync
131 if (mTargetFile
.keepInSync() || containsKeepInSync
) {
132 OCFile folder
= null
;
133 if (mTargetFile
.isFolder()) {
134 folder
= mTargetFile
;
136 folder
= storageManager
.getFileById(mTargetFile
.getParentId());
140 storageManager
.saveFile(folder
);
144 triggerMediaScan(path
);
148 public void onCancel(String callerTag
) {
149 // nothing to do here
152 private void triggerMediaScan(String path
){
153 MediaScannerConnection
.scanFile(
154 getActivity().getApplicationContext(),