WIP Dropping in code for Haiku trash can support

h/t Jérôme Duval <https://github.com/korli> for pointing us to this
(see https://github.com/haikuports/haikuports/issues/10568)
Code used is copyright (c) 2004, Francois Revol <revol@free.fr>,
provided under the MIT licence.

(Also, ran `indent.sh`)
This commit is contained in:
Bill Kendrick 2024-06-06 23:00:36 -07:00
parent 33db4c16f5
commit 3c5303563a
6 changed files with 172 additions and 39 deletions

View file

@ -6,7 +6,7 @@ Copyright (c) 2002-2024
Various contributors (see below, and CHANGES.txt)
https://tuxpaint.org/
June 17, 2002 - May 28, 2024
June 17, 2002 - June 6, 2024
* Design and Coding:
@ -1274,6 +1274,10 @@ June 17, 2002 - May 28, 2024
Marcin 'Shard' Konicki <shard at beosjournal.org> [retired]
Gerasim Troeglazov <3dEyes@gmail.com>
* Haiku trash from Haiku "trash.cpp"
Copyright (c) 2004, Francois Revol <revol@free.fr>,
provided under the MIT licence
* Debian Linux packages
Ben Armstrong <synrg@sanctuary.nslug.ns.ca>

View file

@ -95,6 +95,13 @@ https://tuxpaint.org/
or animated GIF (via Open->Slideshow).
Bill Kendrick <bill@newbreedsoftware.com>
* WIP Add Trash support when using Erase option in Open dialog
on Haiku.
h/t Jérôme Duval <https://github.com/korli> for pointing us to this
(see https://github.com/haikuports/haikuports/issues/10568)
Code used is copyright (c) 2004, Francois Revol <revol@free.fr>,
provided under the MIT licence.
* Removed unnecessary (and now unhelpful) button label text
vertical position nudging (had only been used by Khmer locale).
Bill Kendrick <bill@newbreedsoftware.com>

View file

@ -64,11 +64,12 @@ TX_EXTERN void mirror_f_switchout(magic_api *, int, int, SDL_Surface *);
TX_EXTERN int mirror_f_modes(magic_api *, int);
TX_EXTERN Uint8 mirror_f_accepted_sizes(magic_api * api, int which, int mode);
TX_EXTERN Uint8 mirror_f_default_size(magic_api * api, int which, int mode);
TX_EXTERN void mirror_f_set_size(magic_api * api, int which, int mode, SDL_Surface * canvas, SDL_Surface * last, Uint8 size,
SDL_Rect * update_rect);
TX_EXTERN void mirror_f_set_size(magic_api * api, int which, int mode, SDL_Surface * canvas, SDL_Surface * last,
Uint8 size, SDL_Rect * update_rect);
// No setup required:
TX_EXTERN int mirror_f_init(magic_api * api, Uint8 disabled_features ATTRIBUTE_UNUSED, Uint8 complexity_level ATTRIBUTE_UNUSED)
TX_EXTERN int mirror_f_init(magic_api * api, Uint8 disabled_features ATTRIBUTE_UNUSED,
Uint8 complexity_level ATTRIBUTE_UNUSED)
{
char fname[1024];
@ -239,12 +240,14 @@ TX_EXTERN int mirror_f_requires_colors(magic_api * api ATTRIBUTE_UNUSED, int whi
}
TX_EXTERN void mirror_f_switchin(magic_api * api ATTRIBUTE_UNUSED,
int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED)
{
}
TX_EXTERN void mirror_f_switchout(magic_api * api ATTRIBUTE_UNUSED,
int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED)
int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED)
{
}
@ -260,13 +263,15 @@ TX_EXTERN Uint8 mirror_f_accepted_sizes(magic_api * api ATTRIBUTE_UNUSED, int wh
return 0;
}
TX_EXTERN Uint8 mirror_f_default_size(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED)
TX_EXTERN Uint8 mirror_f_default_size(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
int mode ATTRIBUTE_UNUSED)
{
return 0;
}
TX_EXTERN void mirror_f_set_size(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED, int mode ATTRIBUTE_UNUSED,
SDL_Surface * canvas ATTRIBUTE_UNUSED, SDL_Surface * last ATTRIBUTE_UNUSED,
Uint8 size ATTRIBUTE_UNUSED, SDL_Rect * update_rect ATTRIBUTE_UNUSED)
TX_EXTERN void mirror_f_set_size(magic_api * api ATTRIBUTE_UNUSED, int which ATTRIBUTE_UNUSED,
int mode ATTRIBUTE_UNUSED, SDL_Surface * canvas ATTRIBUTE_UNUSED,
SDL_Surface * last ATTRIBUTE_UNUSED, Uint8 size ATTRIBUTE_UNUSED,
SDL_Rect * update_rect ATTRIBUTE_UNUSED)
{
}

112
src/haiku_trash.c Normal file
View file

@ -0,0 +1,112 @@
/*
* Place a file in the Trash under Haiku.
*
* From Haiku `trash.cpp`
* (https://github.com/haiku/haiku/blob/3e910e720f3408a2ccf7dae9019621a390f3527b/src/bin/trash.cpp)
*
* Copyright (c) 2004 by Francois Revol <revol@free.fr>
* provided under the MIT licence:
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the Software), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* h/t Jérôme Duval <https://github.com/korli> for pointing us to this
* (https://github.com/haikuports/haikuports/issues/10568)
*
* Last modified 2024-06-06
*/
/* FIXME: Not sure how many of these are strictly necessary for
_just_ the `haiku_trash()` function. Luc, please check!
-bjk 2024-06-06 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <app/Message.h>
#include <app/Messenger.h>
#include <kernel/fs_attr.h>
#include <kernel/fs_info.h>
#include <storage/Directory.h>
#include <storage/Entry.h>
#include <storage/FindDirectory.h>
#include <storage/Node.h>
#include <storage/Path.h>
#include <support/TypeConstants.h>
status_t haiku_trash(const char *f)
{
status_t err;
attr_info ai;
dev_t dev = -1;
int nr;
const char *original_path;
char trash_dir[B_PATH_NAME_LENGTH];
char trashed_file[B_PATH_NAME_LENGTH];
dev = dev_for_path(f);
err = find_directory(B_TRASH_DIRECTORY, dev, false, trash_dir, B_PATH_NAME_LENGTH);
if (err < 0)
return err;
BNode node(f);
err = node.InitCheck();
if (err < 0)
return err;
err = node.GetAttrInfo(kAttrOriginalPath, &ai);
if (err == B_OK)
return EALREADY;
if (!strncmp(f, trash_dir, strlen(trash_dir)))
return EALREADY;
entry_ref er;
err = get_ref_for_path(f, &er);
if (err < 0)
return err;
BPath orgPath(&er);
err = orgPath.InitCheck();
if (err < 0)
return err;
original_path = orgPath.Path();
BDirectory trashDir(trash_dir);
err = trashDir.InitCheck();
if (err < 0)
return err;
for (nr = 0;; nr++)
{
if (nr > INT_MAX - 1)
return B_ERROR;
if (nr)
snprintf(trashed_file, B_PATH_NAME_LENGTH - 1, "%s/%s %d", trash_dir, er.name, nr);
else
snprintf(trashed_file, B_PATH_NAME_LENGTH - 1, "%s/%s", trash_dir, er.name);
if (!trashDir.Contains(trashed_file))
break;
}
err = rename(original_path, trashed_file);
if (err < 0)
return err;
err = node.WriteAttr(kAttrOriginalPath, B_STRING_TYPE, 0LL, original_path, strlen(original_path) + 1);
if (err < 0)
return err;
return 0;
}

View file

@ -1242,8 +1242,7 @@ static void set_current_language(const char *restrict loc, int *ptr_num_wished_l
need_right_to_left = wished_langs[0].need_right_to_left;
#ifdef DEBUG
fprintf(stderr, "DEBUG: Language is %s (%d) %s\n",
lang_prefix, langint, need_right_to_left ? "(RTL)" : "");
fprintf(stderr, "DEBUG: Language is %s (%d) %s\n", lang_prefix, langint, need_right_to_left ? "(RTL)" : "");
fflush(stderr);
#endif

View file

@ -331,6 +331,8 @@ typedef struct safer_dirent
char d_name[FILENAME_MAX];
} safer_dirent;
extern status_t haiku_trash(const char *f);
#define dirent safer_dirent
#else /* __BEOS__ */
@ -2220,7 +2222,8 @@ static int do_slideshow(void);
static void play_slideshow(int *selected, int num_selected, char *dirname, char **d_names, char **d_exts, int speed);
static void draw_selection_digits(int right, int bottom, int n);
static int export_gif(int *selected, int num_selected, char *dirname, char **d_names, char **d_exts, int speed, char **dest_fname);
static int export_gif(int *selected, int num_selected, char *dirname, char **d_names, char **d_exts, int speed,
char **dest_fname);
int export_gif_monitor_events(void);
/* Locations where export_pict() can save */
@ -30962,6 +30965,8 @@ static int trash(char *path)
return win32_trash(path);
#elif defined(__APPLE__)
return apple_trash(path);
#elif defined __BEOS__ || defined __HAIKU__
return haiku_trash(path);
#else
char fname[MAX_PATH], trashpath[MAX_PATH], dest[MAX_PATH], infoname[MAX_PATH], bname[MAX_PATH + 1], ext[MAX_PATH];
char deldate[32];
@ -31713,7 +31718,8 @@ char *get_xdg_user_dir(const char *dir_type, const char *fallback)
* @param int speed -- how fast to play the slideshow (0 and 1 both = slowest, 10 = fasted)
* @return int -- 0 if export failed or was aborted, 1 if successful
*/
static int export_gif(int *selected, int num_selected, char *dirname, char **d_names, char **d_exts, int speed, char **dest_fname)
static int export_gif(int *selected, int num_selected, char *dirname, char **d_names, char **d_exts, int speed,
char **dest_fname)
{
char *tmp_starter_id, *tmp_template_id, *tmp_file_id;
int tmp_starter_mirrored, tmp_starter_flipped, tmp_starter_personal;