/*
 * Files: the file manager, and the file picker other apps get through
 * the FileChooser portal. One process serves both (c8ui_app_main makes
 * it the one copy); every window is a browser, in one of three modes.
 */
#ifndef FILES_H
#define FILES_H

#include <stdbool.h>
#include <gio/gio.h>
#include <c8ui/c8ui.h>
#include <c8ui/c8ui-files.h>
#include <c8ui/c8ui-wayland.h>

#define APP_ID "org.cont8nuum.Files"
/* File type and place icons: cont8nuum's own theme is glyphs for tiles */
#define ICON_THEME "Adwaita"

/* folder.c: what is in a folder, kept up to date */

struct folder;

struct entry {
	struct folder *folder;
	GFile *file;
	GFile *target; // where a shortcut goes, or NULL
	char *name; // to show
	char *key; // sorts like a person would (file2 before file10)
	char *mime; // NULL for folders
	char *type; // "PNG image"
	GIcon *gicon;
	guint64 size, mtime;
	bool dir, hidden;
	/* Looked up when first drawn */
	struct c8ui_icon *icon;
	bool icon_looked, thumb_asked;
	cairo_surface_t *thumb;
	struct c8ui_thumbnail *thumb_req;
	/* As the details show them, see entry_when and entry_size */
	char *when, *size_text;
	gint64 when_made; // seconds
};

enum sort_by {
	SORT_NAME,
	SORT_MODIFIED,
	SORT_TYPE,
	SORT_SIZE,
};

typedef void (*folder_fn)(struct folder *folder, void *data);
typedef bool (*entry_filter_fn)(struct entry *entry, void *data);

struct folder {
	GFile *dir;
	GPtrArray *all; // struct entry
	GPtrArray *shown; // of all: sorted, without what is filtered out
	bool loading;
	char *error; // why it couldn't be read, or NULL
	bool not_mounted; // a network place, say: mount it, then reload
	/* How it is shown */
	bool show_hidden;
	enum sort_by sort;
	bool descending;
	entry_filter_fn filter; // may be NULL
	void *filter_data;
	/* changed: shown has other entries; redraw: one looks different */
	folder_fn changed, redraw;
	void *data;
	/* Reading */
	GCancellable *cancel;
	GPtrArray *incoming; // the next all, while reading
	GFileMonitor *monitor;
	guint reload_source;
	/* Changed since, to ask about when the burst is over */
	GHashTable *changes; // GFile set
	bool changes_all; // too many, or the folder itself: read it again
	int asking; // answers still to come
	GPtrArray *answers; // struct answer, applied when the last comes
};

struct folder *folder_new(GFile *dir, folder_fn changed, folder_fn redraw,
	void *data);
void folder_free(struct folder *folder);
void folder_reload(struct folder *folder);
/* After changing show_hidden, sort or filter */
void folder_update_view(struct folder *folder);
struct entry *folder_find(struct folder *folder, const char *name, int *index);

/* NULL if the theme has none */
struct c8ui_icon *entry_icon(struct entry *entry);
/* NULL until it has come (then redraw runs), or if there is none */
cairo_surface_t *entry_thumbnail(struct entry *entry);
struct c8ui_icon *icon_named(const char *name);

/* What to call a folder: "Home", "Pictures", "Trash", a drive's name; as
 * shown (stealth mode's stand-ins in it), or the real one to keep */
char *folder_title(GFile *dir);
char *folder_title_real(GFile *dir);
char *format_size(guint64 size);
char *format_time(guint64 mtime);
/* The same for an entry, kept with it: the details draw every row again
 * for each frame of a scroll */
const char *entry_when(struct entry *entry);
const char *entry_size(struct entry *entry);

/* places.c: the list down the side */

enum place_kind {
	PLACE_HEADING,
	PLACE_FOLDER,
	PLACE_DRIVE, // mounted, or a volume to mount when opened
};

struct place {
	enum place_kind kind;
	char *name;
	char *icon;
	GFile *file; // NULL for headings and volumes not mounted
	GMount *mount;
	GVolume *volume;
	bool can_eject;
	bool pinned; // the user's: can be unpinned
	bool is_file; // a pinned file: opened, not gone into
};

typedef void (*places_fn)(void *data);
void places_init(void);
/* Current places, in order; valid until the next change */
GPtrArray *places_get(void);
/* Keeps uri in the list (GTK's bookmarks, shared with other apps) */
void places_add_bookmark(const char *uri);
/* Pins a folder (a bookmark) or a file (ours: files.ini), and takes
 * either out again */
void places_pin(GFile *file, bool dir);
void places_unpin(GFile *file);
/* Called whenever drives come and go; returns an id to remove it by */
guint places_watch(places_fn fn, void *data);
void places_unwatch(guint id);
/* The place a folder is in (the deepest one holding it), or -1 */
int places_find(GFile *dir);

/* view.c: a window browsing folders */

enum browser_mode {
	MODE_BROWSE,
	MODE_OPEN, // a picker: choosing files to open
	MODE_SAVE, // a picker: choosing where to save
};

struct pick_filter {
	char *name;
	GPtrArray *globs; // char *: shell patterns, matched case-blind
	GPtrArray *mimes; // char *
};

struct browser;
/* The picker's answer: uris NULL when cancelled */
typedef void (*pick_fn)(struct browser *browser, char **uris, void *data);

struct pick_options {
	enum browser_mode mode;
	const char *title;
	const char *accept; // the button's label, or NULL
	const char *parent; // "wayland:HANDLE", or NULL
	bool multiple, folders;
	GPtrArray *filters; // struct pick_filter, taken
	int filter; // the one chosen at first
	GFile *folder; // where to start, or NULL
	const char *name; // save: the name to suggest
	pick_fn done;
	void *data;
};

struct browser *browser_new(GFile *dir);
struct browser *browser_new_picker(const struct pick_options *options);
/* Shows the file in its folder, selected */
void browser_reveal(struct browser *browser, GFile *file);
/* Selects file (in the folder shown) once it is read, besides any
 * others: after browser_reveal, or on a new window */
void browser_select_too(struct browser *browser, GFile *file);
void browser_present(struct browser *browser);
/* prefs changed: every window shows them */
void browsers_apply_prefs(void);
/* Something new was copied: Paste shows or goes in every window */
void browsers_clipboard_changed(struct c8ui_app *app, void *data);
/* Closes a picker without an answer (the app that asked gave up) */
void browser_close(struct browser *browser);
/* The newest window, to send a launch to */
struct browser *browser_last(void);
void browser_search(struct browser *browser, const char *query);
GFile *browser_folder(struct browser *browser);

void pick_filter_free(gpointer filter);

/* ops.c: copying, moving, deleting, renaming */

/* uris to copy (cut false) or move (cut true) when pasted, here or in
 * another file manager */
void clipboard_set(struct c8ui_window *win, char **uris, bool cut);
/* The clipboard has files, from us or another app */
bool clipboard_has(void);
/* GFiles from a text/uri-list, or from x-special/gnome-copied-files
 * (gnome: its first line says copy or cut) */
GList *files_from_uris(const char *text, bool gnome, bool *cut);
void clipboard_paste(struct c8ui_window *win, GFile *into);

/* A name like name in dir that isn't taken: "a (2).txt" */
GFile *files_unique_child(GFile *dir, const char *name);

/* Each runs in the background; errors show in win if it is still open */
void ops_copy(struct c8ui_window *win, GList *files, GFile *into,
	bool move);
void ops_trash(struct c8ui_window *win, GList *files);
/* Asks first */
void ops_delete(struct c8ui_window *win, GList *files);
void ops_empty_trash(struct c8ui_window *win);
void ops_restore(struct c8ui_window *win, GList *files);
typedef void (*made_fn)(GFile *made, void *data);
/* "New folder", or "New folder (2)" if that is taken; fn may be NULL */
void ops_new_folder(struct c8ui_window *win, GFile *in, made_fn fn,
	void *data);
void ops_rename(struct c8ui_window *win, GFile *file, const char *name);
/* The window went: its operations keep going without it */
void ops_window_gone(struct c8ui_window *win);
/* Each archive comes out into `into`: one thing in it as itself, several
 * in a folder with the archive's name. fn (may be NULL) hears what the
 * last one became, or NULL */
void ops_extract(struct c8ui_window *win, GList *archives, GFile *into,
	made_fn fn, void *data);

/* archive.c: archives, looked into as folders (gvfs), taken apart and
 * made (libarchive) */

enum arc_format {
	ARC_ZIP,
	ARC_7Z,
	ARC_TAR_GZ,
	ARC_TAR_XZ,
	ARC_TAR_ZST,
	ARC_FORMATS,
};

/* files go into a new archive, dest (or a name like it that's free) */
void ops_compress(struct c8ui_window *win, GList *files, GFile *dest,
	enum arc_format format, made_fn fn, void *data);
/* "Zip (.zip)" and ".zip" */
const char *arc_format_name(enum arc_format format);
const char *arc_format_ext(enum arc_format format);
/* One we can extract, by its type */
bool arc_is(const char *mime);
/* One that opens like a folder (needs gvfs's archive backend) */
bool arc_can_browse(const char *mime);
/* The archive:// folder that is the inside of file */
GFile *arc_root(GFile *file);
/* The archive a place inside one is in, or NULL when it isn't */
GFile *arc_file(GFile *inside);
/* "photos.tar.gz" → "photos" */
char *arc_stem(const char *name);

/* prefs.c: how folders are shown, in every window */

struct prefs {
	bool details; // a list with columns, not tiles
	bool show_hidden;
	bool thumbnails;
	enum sort_by sort;
	bool descending;
};
extern struct prefs prefs;

typedef void (*prefs_fn)(void);
void prefs_load(void);
/* After changing prefs: saves them and shows them everywhere */
void prefs_changed(void);
/* Runs after every change (the Settings charm rebuilds its page) */
void prefs_watch(prefs_fn fn);

/* openwith.c: the apps for a type, to open uris with one */
void open_with_show(struct c8ui_window *win, struct c8ui_root *root,
	const struct c8ui_rect *anchor, char **uris, const char *mime);

/* props.c: what files are, in a flyout by anchor */
void props_show(struct c8ui_root *root, const struct c8ui_rect *anchor,
	GList *files);

/* mount.c: connecting to network places, asking for passwords */

/* ok, or why not (NULL if the user gave up) */
typedef void (*mounted_fn)(bool ok, const char *error, void *data);
void mount_location(struct c8ui_root *root, GFile *location, mounted_fn done,
	void *data);
/* The window went: its mounts carry on but can't ask anything */
void mount_forget_root(struct c8ui_root *root);

/* portal.c: org.freedesktop.impl.portal.FileChooser */

void portal_init(struct c8ui_app *app);
/* Started for a call: stay up for it (and a while after) */
void portal_expect(void);

/* filemanager.c: org.freedesktop.FileManager1, how other apps ask to
 * show a file in its folder (a browser's "Open containing folder") */
void filemanager_init(struct c8ui_app *app);

/* main.c */
struct c8ui_app *files_app(void);

#endif
