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