Merge remote-tracking branch 'upstream/develop' into
[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.util.Vector;
28
29 import com.owncloud.android.R;
30 import com.owncloud.android.datamodel.FileDataStorageManager;
31 import com.owncloud.android.datamodel.OCFile;
32 import com.owncloud.android.ui.activity.ComponentsGetter;
33 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
34
35 import android.app.Dialog;
36 import android.os.Bundle;
37
38 public class RemoveFileDialogFragment extends ConfirmationDialogFragment
39 implements ConfirmationDialogFragmentListener {
40
41 private static final String ARG_TARGET_FILE = "TARGET_FILE";
42
43 /**
44 * Public factory method to create new RemoveFileDialogFragment instances.
45 *
46 * @param file File to remove.
47 * @return Dialog ready to show.
48 */
49 public static RemoveFileDialogFragment newInstance(OCFile file) {
50 RemoveFileDialogFragment frag = new RemoveFileDialogFragment();
51 Bundle args = new Bundle();
52
53 int messageStringId = R.string.confirmation_remove_alert;
54
55 int posBtn = R.string.confirmation_remove_remote;
56 int neuBtn = -1;
57 if (file.isFolder()) {
58 messageStringId = R.string.confirmation_remove_folder_alert;
59 posBtn = R.string.confirmation_remove_remote_and_local;
60 neuBtn = R.string.confirmation_remove_folder_local;
61 } else if (file.isDown()) {
62 posBtn = R.string.confirmation_remove_remote_and_local;
63 neuBtn = R.string.confirmation_remove_local;
64 }
65
66
67 args.putInt(ARG_CONF_RESOURCE_ID, messageStringId);
68 args.putStringArray(ARG_CONF_ARGUMENTS, new String[]{file.getFileName()});
69 args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
70 args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
71 args.putInt(ARG_NEGATIVE_BTN_RES, R.string.common_cancel);
72 args.putParcelable(ARG_TARGET_FILE, file);
73 frag.setArguments(args);
74
75 return frag;
76 }
77
78 private OCFile mTargetFile;
79
80 @Override
81 public Dialog onCreateDialog(Bundle savedInstanceState) {
82 Dialog dialog = super.onCreateDialog(savedInstanceState);
83 mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);
84
85 setOnConfirmationListener(this);
86
87 return dialog;
88 }
89
90 /**
91 * Performs the removal of the target file, both locally and in the server.
92 */
93 @Override
94 public void onConfirmation(String callerTag) {
95 ComponentsGetter cg = (ComponentsGetter)getSherlockActivity();
96 FileDataStorageManager storageManager = cg.getStorageManager();
97 if (storageManager.getFileById(mTargetFile.getFileId()) != null) {
98 cg.getFileOperationsHelper().removeFile(mTargetFile, false);
99 }
100 }
101
102 /**
103 * Performs the removal of the local copy of the target file
104 */
105 @Override
106 public void onNeutral(String callerTag) {
107 ComponentsGetter cg = (ComponentsGetter)getSherlockActivity();
108 cg.getFileOperationsHelper()
109 .removeFile(mTargetFile, true);
110
111 FileDataStorageManager storageManager = cg.getStorageManager();
112
113 boolean containsKeepInSync = false;
114 if (mTargetFile.isFolder()) {
115 Vector<OCFile> files = storageManager.getFolderContent(mTargetFile);
116 for(OCFile file: files) {
117 containsKeepInSync = file.keepInSync() || containsKeepInSync;
118
119 if (containsKeepInSync)
120 break;
121 }
122 }
123
124 // Remove etag for parent, if file is a keep_in_sync
125 // or is a folder and contains keep_in_sync
126 if (mTargetFile.keepInSync() || containsKeepInSync) {
127 OCFile folder = null;
128 if (mTargetFile.isFolder()) {
129 folder = mTargetFile;
130 } else {
131 folder = storageManager.getFileById(mTargetFile.getParentId());
132 }
133
134 folder.setEtag("");
135 storageManager.saveFile(folder);
136 }
137 }
138
139 @Override
140 public void onCancel(String callerTag) {
141 // nothing to do here
142 }
143
144 }