8158a5a2dd71de40198082aa02c758d73c5d4796
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / RemoveFileDialogFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.ui.dialog;
19
20 /**
21 * Dialog requiring confirmation before removing a given OCFile.
22 *
23 * Triggers the removal according to the user response.
24 *
25 * @author David A. Velasco
26 */
27 import java.io.File;
28 import java.util.Vector;
29
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;
35
36 import android.app.Dialog;
37 import android.media.MediaScannerConnection;
38 import android.os.Bundle;
39
40 public class RemoveFileDialogFragment extends ConfirmationDialogFragment
41 implements ConfirmationDialogFragmentListener {
42
43 private static final String ARG_TARGET_FILE = "TARGET_FILE";
44
45 /**
46 * Public factory method to create new RemoveFileDialogFragment instances.
47 *
48 * @param file File to remove.
49 * @return Dialog ready to show.
50 */
51 public static RemoveFileDialogFragment newInstance(OCFile file) {
52 RemoveFileDialogFragment frag = new RemoveFileDialogFragment();
53 Bundle args = new Bundle();
54
55 int messageStringId = R.string.confirmation_remove_alert;
56
57 int posBtn = R.string.confirmation_remove_remote;
58 int neuBtn = -1;
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;
66 }
67
68
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);
76
77 return frag;
78 }
79
80 private OCFile mTargetFile;
81
82 @Override
83 public Dialog onCreateDialog(Bundle savedInstanceState) {
84 Dialog dialog = super.onCreateDialog(savedInstanceState);
85 mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);
86
87 setOnConfirmationListener(this);
88
89 return dialog;
90 }
91
92 /**
93 * Performs the removal of the target file, both locally and in the server.
94 */
95 @Override
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);
103 }
104 }
105
106 /**
107 * Performs the removal of the local copy of the target file
108 */
109 @Override
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);
115
116 FileDataStorageManager storageManager = cg.getStorageManager();
117
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;
123
124 if (containsKeepInSync)
125 break;
126 }
127 }
128
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;
135 } else {
136 folder = storageManager.getFileById(mTargetFile.getParentId());
137 }
138
139 folder.setEtag("");
140 storageManager.saveFile(folder);
141 }
142
143 // Trigger MediaScan
144 triggerMediaScan(path);
145 }
146
147 @Override
148 public void onCancel(String callerTag) {
149 // nothing to do here
150 }
151
152 private void triggerMediaScan(String path){
153 MediaScannerConnection.scanFile(
154 getActivity().getApplicationContext(),
155 new String[]{path},
156 null,null);
157 }
158 }