Show share with users option if the server supports search users function
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / ErrorMessageAdapter.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * Copyright (C) 2014 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
22 package com.owncloud.android.utils;
23
24 import android.content.res.Resources;
25
26 import com.owncloud.android.R;
27 import com.owncloud.android.lib.common.operations.RemoteOperation;
28 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
29 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
30 import com.owncloud.android.operations.CopyFileOperation;
31 import com.owncloud.android.operations.CreateFolderOperation;
32 import com.owncloud.android.operations.CreateShareOperation;
33 import com.owncloud.android.operations.DownloadFileOperation;
34 import com.owncloud.android.operations.MoveFileOperation;
35 import com.owncloud.android.operations.RemoveFileOperation;
36 import com.owncloud.android.operations.RenameFileOperation;
37 import com.owncloud.android.operations.SynchronizeFileOperation;
38 import com.owncloud.android.operations.SynchronizeFolderOperation;
39 import com.owncloud.android.operations.UnshareLinkOperation;
40 import com.owncloud.android.operations.UploadFileOperation;
41
42 import org.apache.commons.httpclient.ConnectTimeoutException;
43
44 import java.io.File;
45 import java.net.SocketTimeoutException;
46
47 /**
48 * Class to choose proper error messages to show to the user depending on the results of operations,
49 * always following the same policy
50 */
51
52 public class ErrorMessageAdapter {
53
54 public ErrorMessageAdapter() {
55
56 }
57
58 public static String getErrorCauseMessage(RemoteOperationResult result,
59 RemoteOperation operation, Resources res) {
60
61 String message = null;
62
63 if (operation instanceof UploadFileOperation) {
64
65 if (result.isSuccess()) {
66 message = String.format(
67 res.getString(R.string.uploader_upload_succeeded_content_single),
68 ((UploadFileOperation) operation).getFileName());
69 } else {
70 if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
71 || result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
72 message = String.format(
73 res.getString(R.string.error__upload__local_file_not_copied),
74 ((UploadFileOperation) operation).getFileName(),
75 res.getString(R.string.app_name));
76 /*
77 } else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
78 message = res.getString(R.string.failed_upload_quota_exceeded_text);
79 */
80
81 } else if (result.getCode() == ResultCode.FORBIDDEN) {
82 message = String.format(res.getString(R.string.forbidden_permissions),
83 res.getString(R.string.uploader_upload_forbidden_permissions));
84
85 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
86 message = res.getString(R.string.filename_forbidden_charaters_from_server);
87
88 } else {
89 message = String.format(
90 res.getString(R.string.uploader_upload_failed_content_single),
91 ((UploadFileOperation) operation).getFileName());
92 }
93 }
94
95 } else if (operation instanceof DownloadFileOperation) {
96
97 if (result.isSuccess()) {
98 message = String.format(
99 res.getString(R.string.downloader_download_succeeded_content),
100 new File(((DownloadFileOperation) operation).getSavePath()).getName());
101
102 } else {
103 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
104 message = res.getString(R.string.downloader_download_file_not_found);
105
106 } else {
107 message = String.format(
108 res.getString(R.string.downloader_download_failed_content), new File(
109 ((DownloadFileOperation) operation).getSavePath()).getName());
110 }
111 }
112
113 } else if (operation instanceof RemoveFileOperation) {
114 if (result.isSuccess()) {
115 message = res.getString(R.string.remove_success_msg);
116
117 } else {
118 if (result.getCode().equals(ResultCode.FORBIDDEN)) {
119 // Error --> No permissions
120 message = String.format(res.getString(R.string.forbidden_permissions),
121 res.getString(R.string.forbidden_permissions_delete));
122 } else if (isNetworkError(result.getCode())) {
123 message = getErrorMessage(result, res);
124
125 } else {
126 message = res.getString(R.string.remove_fail_msg);
127 }
128 }
129
130 } else if (operation instanceof RenameFileOperation) {
131 if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
132 message = res.getString(R.string.rename_local_fail_msg);
133
134 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
135 // Error --> No permissions
136 message = String.format(res.getString(R.string.forbidden_permissions),
137 res.getString(R.string.forbidden_permissions_rename));
138
139 } else if (result.getCode().equals(ResultCode.INVALID_CHARACTER_IN_NAME)) {
140 message = res.getString(R.string.filename_forbidden_characters);
141
142 } else if (isNetworkError(result.getCode())) {
143 message = getErrorMessage(result, res);
144
145 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
146 message = res.getString(R.string.filename_forbidden_charaters_from_server);
147
148 } else {
149 message = res.getString(R.string.rename_server_fail_msg);
150 }
151
152 } else if (operation instanceof SynchronizeFileOperation) {
153 if (!((SynchronizeFileOperation) operation).transferWasRequested()) {
154 message = res.getString(R.string.sync_file_nothing_to_do_msg);
155 }
156
157 } else if (operation instanceof CreateFolderOperation) {
158 if (result.getCode() == ResultCode.INVALID_CHARACTER_IN_NAME) {
159 message = res.getString(R.string.filename_forbidden_characters);
160
161 } else if (result.getCode().equals(ResultCode.FORBIDDEN)) {
162 message = String.format(res.getString(R.string.forbidden_permissions),
163 res.getString(R.string.forbidden_permissions_create));
164
165 } else if (isNetworkError(result.getCode())) {
166 message = getErrorMessage(result, res);
167
168 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
169 message = res.getString(R.string.filename_forbidden_charaters_from_server);
170 } else {
171 message = res.getString(R.string.create_dir_fail_msg);
172 }
173 } else if (operation instanceof CreateShareOperation) {
174 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
175 message = res.getString(R.string.share_link_file_no_exist);
176
177 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
178 // Error --> No permissions
179 message = String.format(res.getString(R.string.forbidden_permissions),
180 res.getString(R.string.share_link_forbidden_permissions));
181
182 } else if (isNetworkError(result.getCode())) {
183 message = getErrorMessage(result, res);
184
185 } else { // Generic error
186 // Show a Message, operation finished without success
187 message = res.getString(R.string.share_link_file_error);
188 }
189
190 } else if (operation instanceof UnshareLinkOperation) {
191
192 if (result.getCode() == ResultCode.SHARE_NOT_FOUND) { // Error --> SHARE_NOT_FOUND
193 message = res.getString(R.string.unshare_link_file_no_exist);
194
195 } else if (result.getCode() == ResultCode.SHARE_FORBIDDEN) {
196 // Error --> No permissions
197 message = String.format(res.getString(R.string.forbidden_permissions),
198 res.getString(R.string.unshare_link_forbidden_permissions));
199
200 } else if (isNetworkError(result.getCode())) {
201 message = getErrorMessage(result, res);
202
203 } else { // Generic error
204 // Show a Message, operation finished without success
205 message = res.getString(R.string.unshare_link_file_error);
206 }
207 } else if (operation instanceof MoveFileOperation) {
208
209 if(isNetworkError(result.getCode())){
210 message = getErrorMessage(result, res);
211 } else if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
212 message = res.getString(R.string.move_file_not_found);
213 } else if (result.getCode() == ResultCode.INVALID_MOVE_INTO_DESCENDANT) {
214 message = res.getString(R.string.move_file_invalid_into_descendent);
215
216 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
217 message = res.getString(R.string.move_file_invalid_overwrite);
218
219 } else if (result.getCode() == ResultCode.FORBIDDEN) {
220 message = String.format(res.getString(R.string.forbidden_permissions),
221 res.getString(R.string.forbidden_permissions_move));
222
223 } else if (result.getCode() == ResultCode.INVALID_CHARACTER_DETECT_IN_SERVER) {
224 message = res.getString(R.string.filename_forbidden_charaters_from_server);
225
226 } else { // Generic error
227 // Show a Message, operation finished without success
228 message = res.getString(R.string.move_file_error);
229 }
230 } else if (operation instanceof SynchronizeFolderOperation) {
231
232 if (!result.isSuccess()) {
233 String folderPathName = new File(
234 ((SynchronizeFolderOperation) operation).getFolderPath()).getName();
235 if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
236 message = String.format(res.getString(R.string.sync_current_folder_was_removed),
237 folderPathName);
238
239 } else { // Generic error
240 // Show a Message, operation finished without success
241 message = String.format(res.getString(R.string.sync_folder_failed_content),
242 folderPathName);
243 }
244 }
245 } else if (operation instanceof CopyFileOperation) {
246 if(isNetworkError(result.getCode())){
247 message = getErrorMessage(result, res);
248 } else if (result.getCode() == ResultCode.FILE_NOT_FOUND) {
249 message = res.getString(R.string.copy_file_not_found);
250 } else if (result.getCode() == ResultCode.INVALID_COPY_INTO_DESCENDANT) {
251 message = res.getString(R.string.copy_file_invalid_into_descendent);
252
253 } else if (result.getCode() == ResultCode.INVALID_OVERWRITE) {
254 message = res.getString(R.string.copy_file_invalid_overwrite);
255
256 } else if (result.getCode() == ResultCode.FORBIDDEN) {
257 message = String.format(res.getString(R.string.forbidden_permissions),
258 res.getString(R.string.forbidden_permissions_copy));
259
260 } else { // Generic error
261 // Show a Message, operation finished without success
262 message = res.getString(R.string.copy_file_error);
263 }
264 }
265
266 return message;
267 }
268
269 private static String getErrorMessage(RemoteOperationResult result, Resources res) {
270
271 String message = null;
272
273 if (!result.isSuccess()) {
274
275 if (result.getCode() == ResultCode.WRONG_CONNECTION) {
276 message = res.getString(R.string.network_error_socket_exception);
277
278 } else if (result.getCode() == ResultCode.TIMEOUT) {
279 message = res.getString(R.string.network_error_socket_exception);
280
281 if (result.getException() instanceof SocketTimeoutException) {
282 message = res.getString(R.string.network_error_socket_timeout_exception);
283 } else if (result.getException() instanceof ConnectTimeoutException) {
284 message = res.getString(R.string.network_error_connect_timeout_exception);
285 }
286
287 } else if (result.getCode() == ResultCode.HOST_NOT_AVAILABLE) {
288 message = res.getString(R.string.network_host_not_available);
289 }
290 }
291
292 return message;
293 }
294
295 private static boolean isNetworkError(RemoteOperationResult.ResultCode code) {
296 if (code == ResultCode.WRONG_CONNECTION ||
297 code == ResultCode.TIMEOUT ||
298 code == ResultCode.HOST_NOT_AVAILABLE) {
299 return true;
300 } else
301 return false;
302 }
303 }