Merge pull request #1184 from Wikinaut/show-certificate-digest
[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
29 import android.app.Dialog;
30 import android.os.Bundle;
31
32 import com.owncloud.android.R;
33 import com.owncloud.android.datamodel.FileDataStorageManager;
34 import com.owncloud.android.datamodel.OCFile;
35 import com.owncloud.android.ui.activity.ComponentsGetter;
36 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
37
38 public class RemoveFileDialogFragment extends ConfirmationDialogFragment
39 implements ConfirmationDialogFragmentListener {
40
41 private OCFile mTargetFile;
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 negBtn = -1;
59 if (file.isFolder()) {
60 messageStringId = R.string.confirmation_remove_folder_alert;
61 posBtn = R.string.confirmation_remove_remote_and_local;
62 negBtn = R.string.confirmation_remove_local;
63 } else if (file.isDown()) {
64 posBtn = R.string.confirmation_remove_remote_and_local;
65 negBtn = R.string.confirmation_remove_local;
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, R.string.common_no);
72 args.putInt(ARG_NEGATIVE_BTN_RES, negBtn);
73 args.putParcelable(ARG_TARGET_FILE, file);
74 frag.setArguments(args);
75
76 return frag;
77 }
78
79 @Override
80 public Dialog onCreateDialog(Bundle savedInstanceState) {
81 Dialog dialog = super.onCreateDialog(savedInstanceState);
82 mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);
83
84 setOnConfirmationListener(this);
85
86 return dialog;
87 }
88
89 /**
90 * Performs the removal of the target file, both locally and in the server.
91 */
92 @Override
93 public void onConfirmation(String callerTag) {
94 ComponentsGetter cg = (ComponentsGetter)getActivity();
95 FileDataStorageManager storageManager = cg.getStorageManager();
96 if (storageManager.getFileById(mTargetFile.getFileId()) != null) {
97 cg.getFileOperationsHelper().removeFile(mTargetFile, false);
98 }
99 }
100
101 /**
102 * Performs the removal of the local copy of the target file
103 */
104 @Override
105 public void onCancel(String callerTag) {
106 ComponentsGetter cg = (ComponentsGetter)getActivity();
107 cg.getFileOperationsHelper().removeFile(mTargetFile, true);
108 }
109
110 @Override
111 public void onNeutral(String callerTag) {
112 // nothing to do here
113 }
114 }