Merge remote-tracking branch 'remotes/upstream/descendIntoFolder' into beta
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / RemoveFilesDialogFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
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
21 package com.owncloud.android.ui.dialog;
22
23 /**
24 * Dialog requiring confirmation before removing a given OCFile.
25 *
26 * Triggers the removal according to the user response.
27 */
28
29 import android.app.Dialog;
30 import android.content.res.Resources;
31 import android.os.Bundle;
32
33 import com.owncloud.android.MainApp;
34 import com.owncloud.android.R;
35 import com.owncloud.android.datamodel.FileDataStorageManager;
36 import com.owncloud.android.datamodel.OCFile;
37 import com.owncloud.android.ui.activity.ComponentsGetter;
38 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
39
40 import java.util.ArrayList;
41 import java.util.Vector;
42
43 public class RemoveFilesDialogFragment extends ConfirmationDialogFragment
44 implements ConfirmationDialogFragmentListener {
45
46 private ArrayList<OCFile> mTargetFiles;
47
48 private static final String ARG_TARGET_FILES = "TARGET_FILES";
49
50 /**
51 * Public factory method to create new RemoveFileDialogFragment instances.
52 *
53 * @param files Files to remove.
54 * @return Dialog ready to show.
55 */
56 public static RemoveFilesDialogFragment newInstance(ArrayList<OCFile> files) {
57 RemoveFilesDialogFragment frag = new RemoveFilesDialogFragment();
58 Bundle args = new Bundle();
59
60 int messageStringId = R.string.confirmation_remove_files_alert;
61
62 int posBtn = R.string.confirmation_remove_file_remote;
63 int negBtn = -1;
64
65 boolean containsFolder = false;
66 boolean containsDown = false;
67 for (OCFile file: files) {
68 if (file.isFolder()) containsFolder = true;
69 if (file.isDown()) containsDown = true;
70 }
71
72 if (containsFolder) {
73 messageStringId = R.string.confirmation_remove_folders_alert;
74 posBtn = R.string.confirmation_remove_remote_and_local;
75 negBtn = R.string.confirmation_remove_local;
76 } else if (containsDown) {
77 posBtn = R.string.confirmation_remove_remote_and_local;
78 negBtn = R.string.confirmation_remove_local;
79 }
80
81 args.putInt(ARG_CONF_RESOURCE_ID, messageStringId);
82 args.putStringArray(ARG_CONF_ARGUMENTS, new String[]{MainApp.getAppContext().getString(R.string.confirmation_remove_files)});
83 args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
84 args.putInt(ARG_NEUTRAL_BTN_RES, R.string.common_no);
85 args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
86 args.putParcelableArrayList(ARG_TARGET_FILES, files);
87 frag.setArguments(args);
88
89 return frag;
90 }
91
92 @Override
93 public Dialog onCreateDialog(Bundle savedInstanceState) {
94 Dialog dialog = super.onCreateDialog(savedInstanceState);
95 mTargetFiles = getArguments().getParcelableArrayList(ARG_TARGET_FILES);
96
97 setOnConfirmationListener(this);
98
99 return dialog;
100 }
101
102 /**
103 * Performs the removal of the target file, both locally and in the server.
104 */
105 @Override
106 public void onConfirmation(String callerTag) {
107 ComponentsGetter cg = (ComponentsGetter) getActivity();
108 FileDataStorageManager storageManager = cg.getStorageManager();
109 for (OCFile targetFile : mTargetFiles) {
110 if (storageManager.getFileById(targetFile.getFileId()) != null) {
111 cg.getFileOperationsHelper().removeFile(targetFile, false);
112 }
113 }
114 }
115
116 /**
117 * Performs the removal of the local copy of the target file
118 */
119 @Override
120 public void onCancel(String callerTag) {
121 ComponentsGetter cg = (ComponentsGetter) getActivity();
122
123 for (OCFile targetFile : mTargetFiles) {
124 cg.getFileOperationsHelper().removeFile(targetFile, true);
125
126 FileDataStorageManager storageManager = cg.getStorageManager();
127
128 boolean containsFavorite = false;
129 if (targetFile.isFolder()) {
130 Vector<OCFile> files = storageManager.getFolderContent(targetFile, false);
131 for (OCFile file : files) {
132 containsFavorite = file.isFavorite() || containsFavorite;
133
134 if (containsFavorite)
135 break;
136 }
137 }
138
139 // Remove etag for parent, if file is a favorite
140 // or is a folder and contains favorite
141 if (targetFile.isFavorite() || containsFavorite) {
142 OCFile folder = null;
143 if (targetFile.isFolder()) {
144 folder = targetFile;
145 } else {
146 folder = storageManager.getFileById(targetFile.getParentId());
147 }
148
149 folder.setEtag("");
150 storageManager.saveFile(folder);
151 }
152 }
153 }
154
155 @Override
156 public void onNeutral(String callerTag) {
157 // nothing to do here
158 }
159 }