9020dd0f129c31a05bc0b0ed42601e2a2d1df4ab
[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 com.owncloud.android.R;
28 import com.owncloud.android.datamodel.FileDataStorageManager;
29 import com.owncloud.android.datamodel.OCFile;
30 import com.owncloud.android.ui.activity.ComponentsGetter;
31 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
32
33 import android.app.Dialog;
34 import android.os.Bundle;
35
36 public class RemoveFileDialogFragment extends ConfirmationDialogFragment
37 implements ConfirmationDialogFragmentListener {
38
39 private static final String ARG_TARGET_FILE = "TARGET_FILE";
40
41 /**
42 * Public factory method to create new RemoveFileDialogFragment instances.
43 *
44 * @param file File to remove.
45 * @return Dialog ready to show.
46 */
47 public static RemoveFileDialogFragment newInstance(OCFile file) {
48 RemoveFileDialogFragment frag = new RemoveFileDialogFragment();
49 Bundle args = new Bundle();
50
51 int messageStringId = R.string.confirmation_remove_alert;
52
53 int posBtn = R.string.confirmation_remove_remote;
54 int neuBtn = -1;
55 if (file.isFolder()) {
56 messageStringId = R.string.confirmation_remove_folder_alert;
57 posBtn = R.string.confirmation_remove_remote_and_local;
58 neuBtn = R.string.confirmation_remove_folder_local;
59 } else if (file.isDown()) {
60 posBtn = R.string.confirmation_remove_remote_and_local;
61 neuBtn = R.string.confirmation_remove_local;
62 }
63
64
65 args.putInt(ARG_CONF_RESOURCE_ID, messageStringId);
66 args.putStringArray(ARG_CONF_ARGUMENTS, new String[]{file.getFileName()});
67 args.putInt(ARG_POSITIVE_BTN_RES, posBtn);
68 args.putInt(ARG_NEUTRAL_BTN_RES, neuBtn);
69 args.putInt(ARG_NEGATIVE_BTN_RES, R.string.common_cancel);
70 args.putParcelable(ARG_TARGET_FILE, file);
71 frag.setArguments(args);
72
73 return frag;
74 }
75
76 private OCFile mTargetFile;
77
78 @Override
79 public Dialog onCreateDialog(Bundle savedInstanceState) {
80 Dialog dialog = super.onCreateDialog(savedInstanceState);
81 mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);
82
83 setOnConfirmationListener(this);
84
85 return dialog;
86 }
87
88 /**
89 * Performs the removal of the target file, both locally and in the server.
90 */
91 @Override
92 public void onConfirmation(String callerTag) {
93 ComponentsGetter cg = (ComponentsGetter)getSherlockActivity();
94 FileDataStorageManager storageManager = cg.getStorageManager();
95 if (storageManager.getFileById(mTargetFile.getFileId()) != null) {
96 cg.getFileOperationsHelper().removeFile(mTargetFile, false);
97 }
98 }
99
100 /**
101 * Performs the removal of the local copy of the taget file
102 */
103 @Override
104 public void onNeutral(String callerTag) {
105 ((ComponentsGetter)getSherlockActivity()).getFileOperationsHelper()
106 .removeFile(mTargetFile, true);
107 }
108
109 @Override
110 public void onCancel(String callerTag) {
111 // nothing to do here
112 }
113
114 }