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