Merge branch 'master' into copy
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / RemoveFileDialogFragment.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 import java.util.Vector;
29
30 import android.app.Dialog;
31 import android.os.Bundle;
32
33 import com.owncloud.android.R;
34 import com.owncloud.android.datamodel.FileDataStorageManager;
35 import com.owncloud.android.datamodel.OCFile;
36 import com.owncloud.android.ui.activity.ComponentsGetter;
37 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
38
39 public class RemoveFileDialogFragment extends ConfirmationDialogFragment
40 implements ConfirmationDialogFragmentListener {
41
42 private OCFile mTargetFile;
43
44 private static final String ARG_TARGET_FILE = "TARGET_FILE";
45
46 /**
47 * Public factory method to create new RemoveFileDialogFragment instances.
48 *
49 * @param file File to remove.
50 * @return Dialog ready to show.
51 */
52 public static RemoveFileDialogFragment newInstance(OCFile file) {
53 RemoveFileDialogFragment frag = new RemoveFileDialogFragment();
54 Bundle args = new Bundle();
55
56 int messageStringId = R.string.confirmation_remove_alert;
57
58 int posBtn = R.string.confirmation_remove_remote;
59 int negBtn = -1;
60 if (file.isFolder()) {
61 messageStringId = R.string.confirmation_remove_folder_alert;
62 posBtn = R.string.confirmation_remove_remote_and_local;
63 negBtn = R.string.confirmation_remove_local;
64 } else if (file.isDown()) {
65 posBtn = R.string.confirmation_remove_remote_and_local;
66 negBtn = R.string.confirmation_remove_local;
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, R.string.common_no);
73 args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
74 args.putParcelable(ARG_TARGET_FILE, file);
75 frag.setArguments(args);
76
77 return frag;
78 }
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)getActivity();
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 onCancel(String callerTag) {
107 ComponentsGetter cg = (ComponentsGetter)getActivity();
108 cg.getFileOperationsHelper().removeFile(mTargetFile, true);
109
110 FileDataStorageManager storageManager = cg.getStorageManager();
111
112 boolean containsFavorite = false;
113 if (mTargetFile.isFolder()) {
114 // TODO Enable when "On Device" is recovered ?
115 Vector<OCFile> files = storageManager.getFolderContent(mTargetFile/*, false*/);
116 for(OCFile file: files) {
117 containsFavorite = file.isFavorite() || containsFavorite;
118
119 if (containsFavorite)
120 break;
121 }
122 }
123
124 // Remove etag for parent, if file is a favorite
125 // or is a folder and contains favorite
126 if (mTargetFile.isFavorite() || containsFavorite) {
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 onNeutral(String callerTag) {
141 // nothing to do here
142 }
143 }