comparison src/utilops.c @ 597:5da092a6a92a

moved safe delete functions to separate file
author nadvornik
date Tue, 06 May 2008 21:35:31 +0000
parents f8c93e1d728d
children 8268cbe682f1
comparison
equal deleted inserted replaced
596:f8c93e1d728d 597:5da092a6a92a
25 #include "image.h" 25 #include "image.h"
26 #include "img-view.h" 26 #include "img-view.h"
27 #include "layout.h" 27 #include "layout.h"
28 #include "search.h" 28 #include "search.h"
29 #include "thumb_standard.h" 29 #include "thumb_standard.h"
30 #include "trash.h"
30 #include "ui_bookmark.h" 31 #include "ui_bookmark.h"
31 #include "ui_fileops.h" 32 #include "ui_fileops.h"
32 #include "ui_misc.h" 33 #include "ui_misc.h"
33 #include "ui_tabcomp.h" 34 #include "ui_tabcomp.h"
34 #include "editors.h" 35 #include "editors.h"
1324 } 1325 }
1325 1326
1326 file_util_move_multiple(file_data_multiple_new(list, dest_path, TRUE)); 1327 file_util_move_multiple(file_data_multiple_new(list, dest_path, TRUE));
1327 } 1328 }
1328 1329
1329 /* 1330
1330 *--------------------------------------------------------------------------
1331 * Safe Delete
1332 *--------------------------------------------------------------------------
1333 */
1334
1335 static gint file_util_safe_number(gint64 free_space)
1336 {
1337 gint n = 0;
1338 gint64 total = 0;
1339 GList *list;
1340 GList *work;
1341 gint sorted = FALSE;
1342 gint warned = FALSE;
1343
1344 if (!filelist_read(options->file_ops.safe_delete_path, &list, NULL)) return 0;
1345
1346 work = list;
1347 while (work)
1348 {
1349 FileData *fd;
1350 gint v;
1351
1352 fd = work->data;
1353 work = work->next;
1354
1355 v = (gint)strtol(fd->name, NULL, 10);
1356 if (v >= n) n = v + 1;
1357
1358 total += fd->size;
1359 }
1360
1361 while (options->file_ops.safe_delete_folder_maxsize > 0 && list &&
1362 (free_space < 0 || total + free_space > (gint64)options->file_ops.safe_delete_folder_maxsize * 1048576) )
1363 {
1364 FileData *fd;
1365
1366 if (!sorted)
1367 {
1368 list = filelist_sort(list, SORT_NAME, TRUE);
1369 sorted = TRUE;
1370 }
1371
1372 fd = list->data;
1373 list = g_list_remove(list, fd);
1374
1375 DEBUG_1("expunging from trash for space: %s", fd->name);
1376 if (!unlink_file(fd->path) && !warned)
1377 {
1378 file_util_warning_dialog(_("Delete failed"),
1379 _("Unable to remove old file from trash folder"),
1380 GTK_STOCK_DIALOG_WARNING, NULL);
1381 warned = TRUE;
1382 }
1383 total -= fd->size;
1384 file_data_unref(fd);
1385 }
1386
1387 filelist_free(list);
1388
1389 return n;
1390 }
1391
1392 void file_util_trash_clear(void)
1393 {
1394 file_util_safe_number(-1);
1395 }
1396
1397 static gchar *file_util_safe_dest(const gchar *path)
1398 {
1399 gint n;
1400
1401 n = file_util_safe_number(filesize(path));
1402 return g_strdup_printf("%s/%06d_%s", options->file_ops.safe_delete_path, n, filename_from_path(path));
1403 }
1404
1405 static void file_util_safe_del_toggle_cb(GtkWidget *button, gpointer data)
1406 {
1407 options->file_ops.safe_delete_enable = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button));
1408 }
1409
1410 static void file_util_safe_del_close_cb(GtkWidget *dialog, gpointer data)
1411 {
1412 GenericDialog **gd = data;
1413
1414 *gd = NULL;
1415 }
1416
1417 static gint file_util_unlink(FileData *fd)
1418 {
1419 static GenericDialog *gd = NULL;
1420 gchar *result = NULL;
1421 gint success = TRUE;
1422
1423 if (!isfile(fd->path)) return FALSE;
1424
1425
1426 if (!options->file_ops.safe_delete_enable)
1427 {
1428 return unlink_file(fd->path);
1429 }
1430
1431 if (!isdir(options->file_ops.safe_delete_path))
1432 {
1433 DEBUG_1("creating trash: %s", options->file_ops.safe_delete_path);
1434 if (!options->file_ops.safe_delete_path || !mkdir_utf8(options->file_ops.safe_delete_path, 0755))
1435 {
1436 result = _("Could not create folder");
1437 success = FALSE;
1438 }
1439 }
1440
1441 if (success)
1442 {
1443 gchar *dest;
1444
1445 dest = file_util_safe_dest(fd->path);
1446 if (dest)
1447 {
1448 DEBUG_1("safe deleting %s to %s", fd->path, dest);
1449 success = move_file(fd->path, dest);
1450 }
1451 else
1452 {
1453 success = FALSE;
1454 }
1455
1456 if (!success && !access_file(fd->path, W_OK))
1457 {
1458 result = _("Permission denied");
1459 }
1460 g_free(dest);
1461 }
1462
1463 if (result && !gd)
1464 {
1465 GtkWidget *button;
1466 gchar *buf;
1467
1468 buf = g_strdup_printf(_("Unable to access or create the trash folder.\n\"%s\""), options->file_ops.safe_delete_path);
1469 gd = file_util_warning_dialog(result, buf, GTK_STOCK_DIALOG_WARNING, NULL);
1470 g_free(buf);
1471
1472 button = gtk_check_button_new_with_label(_("Turn off safe delete"));
1473 g_signal_connect(G_OBJECT(button), "toggled",
1474 G_CALLBACK(file_util_safe_del_toggle_cb), NULL);
1475 gtk_box_pack_start(GTK_BOX(gd->vbox), button, FALSE, FALSE, 0);
1476 gtk_widget_show(button);
1477
1478 g_signal_connect(G_OBJECT(gd->dialog), "destroy",
1479 G_CALLBACK(file_util_safe_del_close_cb), &gd);
1480 }
1481
1482 return success;
1483 }
1484
1485 static void box_append_safe_delete_status(GenericDialog *gd)
1486 {
1487 GtkWidget *label;
1488 gchar *buf;
1489
1490 if (options->editor_command[CMD_DELETE])
1491 {
1492 buf = g_strdup(_("Deletion by external command"));
1493 }
1494 else
1495 {
1496 if (options->file_ops.safe_delete_enable)
1497 {
1498 gchar *buf2;
1499 if (options->file_ops.safe_delete_folder_maxsize > 0)
1500 buf2 = g_strdup_printf(_(" (max. %d MB)"), options->file_ops.safe_delete_folder_maxsize);
1501 else
1502 buf2 = g_strdup("");
1503
1504 buf = g_strdup_printf(_("Safe delete: %s%s\nTrash: %s"), _("on"), buf2, options->file_ops.safe_delete_path);
1505 g_free(buf2);
1506 }
1507 else
1508 {
1509 buf = g_strdup_printf(_("Safe delete: %s"), _("off"));
1510 }
1511 }
1512
1513 label = pref_label_new(gd->vbox, buf);
1514 g_free(buf);
1515
1516 gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
1517 gtk_widget_set_sensitive(label, FALSE);
1518 }
1519 1331
1520 /* 1332 /*
1521 *-------------------------------------------------------------------------- 1333 *--------------------------------------------------------------------------
1522 * Delete routines 1334 * Delete routines
1523 *-------------------------------------------------------------------------- 1335 *--------------------------------------------------------------------------
1524 */ 1336 */
1337
1338 static void box_append_safe_delete_status(GenericDialog *gd)
1339 {
1340 GtkWidget *label;
1341 gchar *buf;
1342
1343 buf = file_util_safe_delete_status();
1344 label = pref_label_new(gd->vbox, buf);
1345 g_free(buf);
1346
1347 gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
1348 gtk_widget_set_sensitive(label, FALSE);
1349 }
1350
1351
1352 static gint file_util_unlink(FileData *fd)
1353 {
1354 if (!isfile(fd->path)) return FALSE;
1355
1356
1357 if (!options->file_ops.safe_delete_enable)
1358 {
1359 return unlink_file(fd->path);
1360 }
1361
1362 return file_util_safe_unlink(fd->path);
1363 }
1525 1364
1526 /* 1365 /*
1527 * delete multiple files 1366 * delete multiple files
1528 */ 1367 */
1529 1368