PageRenderTime 126ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/nemo-error-reporting.c

https://github.com/aldatsa/nemo
C | 363 lines | 262 code | 63 blank | 38 comment | 41 complexity | a4a277192e4d342f3656792c2b0b4c33 MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, LGPL-2.0
  1. /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
  2. /* nemo-error-reporting.h - implementation of file manager functions that report
  3. errors to the user.
  4. Copyright (C) 2000 Eazel, Inc.
  5. The Gnome Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. The Gnome Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the Gnome Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 51 Franklin Street - Suite 500,
  16. Boston, MA 02110-1335, USA.
  17. Authors: John Sullivan <sullivan@eazel.com>
  18. */
  19. #include <config.h>
  20. #include "nemo-error-reporting.h"
  21. #include <string.h>
  22. #include <glib/gi18n.h>
  23. #include <libnemo-private/nemo-file.h>
  24. #include <eel/eel-string.h>
  25. #include <eel/eel-stock-dialogs.h>
  26. #define DEBUG_FLAG NEMO_DEBUG_DIRECTORY_VIEW
  27. #include <libnemo-private/nemo-debug.h>
  28. #define NEW_NAME_TAG "Nemo: new name"
  29. #define MAXIMUM_DISPLAYED_FILE_NAME_LENGTH 50
  30. static void finish_rename (NemoFile *file, gboolean stop_timer, GError *error);
  31. void
  32. nemo_report_error_loading_directory (NemoFile *file,
  33. GError *error,
  34. GtkWindow *parent_window)
  35. {
  36. char *file_name;
  37. char *message;
  38. if (error == NULL ||
  39. error->message == NULL) {
  40. return;
  41. }
  42. if (error->domain == G_IO_ERROR &&
  43. error->code == G_IO_ERROR_NOT_MOUNTED) {
  44. /* This case is retried automatically */
  45. return;
  46. }
  47. file_name = nemo_file_get_display_name (file);
  48. if (error->domain == G_IO_ERROR) {
  49. switch (error->code) {
  50. case G_IO_ERROR_PERMISSION_DENIED:
  51. message = g_strdup_printf (_("You do not have the permissions necessary to view the contents of \"%s\"."),
  52. file_name);
  53. break;
  54. case G_IO_ERROR_NOT_FOUND:
  55. message = g_strdup_printf (_("\"%s\" could not be found. Perhaps it has recently been deleted."),
  56. file_name);
  57. break;
  58. default:
  59. message = g_strdup_printf (_("Sorry, could not display all the contents of \"%s\": %s"), file_name,
  60. error->message);
  61. }
  62. } else {
  63. message = g_strdup (error->message);
  64. }
  65. eel_show_error_dialog (_("The folder contents could not be displayed."), message, parent_window);
  66. g_free (file_name);
  67. g_free (message);
  68. }
  69. void
  70. nemo_report_error_setting_group (NemoFile *file,
  71. GError *error,
  72. GtkWindow *parent_window)
  73. {
  74. char *file_name;
  75. char *message;
  76. if (error == NULL) {
  77. return;
  78. }
  79. file_name = nemo_file_get_display_name (file);
  80. message = NULL;
  81. if (error->domain == G_IO_ERROR) {
  82. switch (error->code) {
  83. case G_IO_ERROR_PERMISSION_DENIED:
  84. message = g_strdup_printf (_("You do not have the permissions necessary to change the group of \"%s\"."),
  85. file_name);
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. if (message == NULL) {
  92. /* We should invent decent error messages for every case we actually experience. */
  93. g_warning ("Hit unhandled case %s:%d in nemo_report_error_setting_group",
  94. g_quark_to_string (error->domain), error->code);
  95. /* fall through */
  96. message = g_strdup_printf (_("Sorry, could not change the group of \"%s\": %s"), file_name,
  97. error->message);
  98. }
  99. eel_show_error_dialog (_("The group could not be changed."), message, parent_window);
  100. g_free (file_name);
  101. g_free (message);
  102. }
  103. void
  104. nemo_report_error_setting_owner (NemoFile *file,
  105. GError *error,
  106. GtkWindow *parent_window)
  107. {
  108. char *file_name;
  109. char *message;
  110. if (error == NULL) {
  111. return;
  112. }
  113. file_name = nemo_file_get_display_name (file);
  114. message = g_strdup_printf (_("Sorry, could not change the owner of \"%s\": %s"), file_name, error->message);
  115. eel_show_error_dialog (_("The owner could not be changed."), message, parent_window);
  116. g_free (file_name);
  117. g_free (message);
  118. }
  119. void
  120. nemo_report_error_setting_permissions (NemoFile *file,
  121. GError *error,
  122. GtkWindow *parent_window)
  123. {
  124. char *file_name;
  125. char *message;
  126. if (error == NULL) {
  127. return;
  128. }
  129. file_name = nemo_file_get_display_name (file);
  130. message = g_strdup_printf (_("Sorry, could not change the permissions of \"%s\": %s"), file_name, error->message);
  131. eel_show_error_dialog (_("The permissions could not be changed."), message, parent_window);
  132. g_free (file_name);
  133. g_free (message);
  134. }
  135. typedef struct _NemoRenameData {
  136. char *name;
  137. NemoFileOperationCallback callback;
  138. gpointer callback_data;
  139. } NemoRenameData;
  140. void
  141. nemo_report_error_renaming_file (NemoFile *file,
  142. const char *new_name,
  143. GError *error,
  144. GtkWindow *parent_window)
  145. {
  146. char *original_name, *original_name_truncated;
  147. char *new_name_truncated;
  148. char *message;
  149. /* Truncate names for display since very long file names with no spaces
  150. * in them won't get wrapped, and can create insanely wide dialog boxes.
  151. */
  152. original_name = nemo_file_get_display_name (file);
  153. original_name_truncated = eel_str_middle_truncate (original_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
  154. g_free (original_name);
  155. new_name_truncated = eel_str_middle_truncate (new_name, MAXIMUM_DISPLAYED_FILE_NAME_LENGTH);
  156. message = NULL;
  157. if (error->domain == G_IO_ERROR) {
  158. switch (error->code) {
  159. case G_IO_ERROR_EXISTS:
  160. message = g_strdup_printf (_("The name \"%s\" is already used in this folder. "
  161. "Please use a different name."),
  162. new_name_truncated);
  163. break;
  164. case G_IO_ERROR_NOT_FOUND:
  165. message = g_strdup_printf (_("There is no \"%s\" in this folder. "
  166. "Perhaps it was just moved or deleted?"),
  167. original_name_truncated);
  168. break;
  169. case G_IO_ERROR_PERMISSION_DENIED:
  170. message = g_strdup_printf (_("You do not have the permissions necessary to rename \"%s\"."),
  171. original_name_truncated);
  172. break;
  173. case G_IO_ERROR_INVALID_FILENAME:
  174. if (strchr (new_name, '/') != NULL) {
  175. message = g_strdup_printf (_("The name \"%s\" is not valid because it contains the character \"/\". "
  176. "Please use a different name."),
  177. new_name_truncated);
  178. } else {
  179. message = g_strdup_printf (_("The name \"%s\" is not valid. "
  180. "Please use a different name."),
  181. new_name_truncated);
  182. }
  183. break;
  184. case G_IO_ERROR_FILENAME_TOO_LONG:
  185. message = g_strdup_printf (_("The name \"%s\" is too long. "
  186. "Please use a different name."),
  187. new_name_truncated);
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. if (message == NULL) {
  194. /* We should invent decent error messages for every case we actually experience. */
  195. g_warning ("Hit unhandled case %s:%d in nemo_report_error_renaming_file",
  196. g_quark_to_string (error->domain), error->code);
  197. /* fall through */
  198. message = g_strdup_printf (_("Sorry, could not rename \"%s\" to \"%s\": %s"),
  199. original_name_truncated, new_name_truncated,
  200. error->message);
  201. }
  202. g_free (original_name_truncated);
  203. g_free (new_name_truncated);
  204. eel_show_error_dialog (_("The item could not be renamed."), message, parent_window);
  205. g_free (message);
  206. }
  207. static void
  208. nemo_rename_data_free (NemoRenameData *data)
  209. {
  210. g_free (data->name);
  211. g_free (data);
  212. }
  213. static void
  214. rename_callback (NemoFile *file, GFile *result_location,
  215. GError *error, gpointer callback_data)
  216. {
  217. NemoRenameData *data;
  218. g_assert (NEMO_IS_FILE (file));
  219. g_assert (callback_data == NULL);
  220. data = g_object_get_data (G_OBJECT (file), NEW_NAME_TAG);
  221. g_assert (data != NULL);
  222. if (error &&
  223. !(error->domain == G_IO_ERROR && error->code == G_IO_ERROR_CANCELLED)) {
  224. /* If rename failed, notify the user. */
  225. nemo_report_error_renaming_file (file, data->name, error, NULL);
  226. }
  227. finish_rename (file, TRUE, error);
  228. }
  229. static void
  230. cancel_rename_callback (gpointer callback_data)
  231. {
  232. GError *error;
  233. error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled");
  234. finish_rename (NEMO_FILE (callback_data), FALSE, error);
  235. g_error_free (error);
  236. }
  237. static void
  238. finish_rename (NemoFile *file, gboolean stop_timer, GError *error)
  239. {
  240. NemoRenameData *data;
  241. data = g_object_get_data (G_OBJECT (file), NEW_NAME_TAG);
  242. if (data == NULL) {
  243. return;
  244. }
  245. /* Cancel both the rename and the timed wait. */
  246. nemo_file_cancel (file, rename_callback, NULL);
  247. if (stop_timer) {
  248. eel_timed_wait_stop (cancel_rename_callback, file);
  249. }
  250. if (data->callback != NULL) {
  251. data->callback (file, NULL, error, data->callback_data);
  252. }
  253. /* Let go of file name. */
  254. g_object_set_data (G_OBJECT (file), NEW_NAME_TAG, NULL);
  255. }
  256. void
  257. nemo_rename_file (NemoFile *file,
  258. const char *new_name,
  259. NemoFileOperationCallback callback,
  260. gpointer callback_data)
  261. {
  262. char *old_name, *wait_message;
  263. NemoRenameData *data;
  264. char *uri;
  265. GError *error;
  266. g_return_if_fail (NEMO_IS_FILE (file));
  267. g_return_if_fail (new_name != NULL);
  268. /* Stop any earlier rename that's already in progress. */
  269. error = g_error_new (G_IO_ERROR, G_IO_ERROR_CANCELLED, "Cancelled");
  270. finish_rename (file, TRUE, error);
  271. g_error_free (error);
  272. data = g_new0 (NemoRenameData, 1);
  273. data->name = g_strdup (new_name);
  274. data->callback = callback;
  275. data->callback_data = callback_data;
  276. /* Attach the new name to the file. */
  277. g_object_set_data_full (G_OBJECT (file),
  278. NEW_NAME_TAG,
  279. data, (GDestroyNotify)nemo_rename_data_free);
  280. /* Start the timed wait to cancel the rename. */
  281. old_name = nemo_file_get_display_name (file);
  282. wait_message = g_strdup_printf (_("Renaming \"%s\" to \"%s\"."),
  283. old_name,
  284. new_name);
  285. g_free (old_name);
  286. eel_timed_wait_start (cancel_rename_callback, file, wait_message,
  287. NULL); /* FIXME bugzilla.gnome.org 42395: Parent this? */
  288. g_free (wait_message);
  289. uri = nemo_file_get_uri (file);
  290. DEBUG ("Renaming file %s to %s", uri, new_name);
  291. g_free (uri);
  292. /* Start the rename. */
  293. nemo_file_rename (file, new_name,
  294. rename_callback, NULL);
  295. }