This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository xdg-desktop-portal-efl.

View the commit online.

commit ac0fc8476d5802b3c45f23e9c6cc3cdd37971efe
Author: Carsten Haitzler (Rasterman) <[email protected]>
AuthorDate: Mon Nov 30 19:39:59 2026 +0000

    filechooser - handle dnd and switch to dir and sel files dnd'd
---
 src/lib/filechooser/dialog_filechooser.c | 220 +++++++++++++++++++++++++++++++
 src/tests/meson.build                    |   5 +
 2 files changed, 225 insertions(+)

diff --git a/src/lib/filechooser/dialog_filechooser.c b/src/lib/filechooser/dialog_filechooser.c
index 2f02fde..07f6c6b 100644
--- a/src/lib/filechooser/dialog_filechooser.c
+++ b/src/lib/filechooser/dialog_filechooser.c
@@ -7,6 +7,137 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <ctype.h>
+#include <limits.h>
+
+typedef struct
+{
+  Eina_List *paths;
+  char *folder;
+  Ecore_Timer *timer;
+  double deadline;
+} Drop_Selection;
+
+static void
+_drop_clear(Evas_Object *fs)
+{
+  Drop_Selection *drop = evas_object_data_del(fs, "portal-drop-selection");
+  char *path;
+
+  if (!drop) return;
+  if (drop->timer) ecore_timer_del(drop->timer);
+  EINA_LIST_FREE(drop->paths, path) free(path);
+  free(drop->folder);
+  free(drop);
+}
+
+static void
+_drop_deleted(void *data EINA_UNUSED, Evas *evas EINA_UNUSED,
+              Evas_Object *obj, void *event EINA_UNUSED)
+{
+  _drop_clear(obj);
+}
+
+static void
+_drop_selected(void *data EINA_UNUSED, Evas_Object *obj,
+               void *event EINA_UNUSED)
+{
+  /* A manual selection supersedes a drop that is still loading. */
+  _drop_clear(obj);
+}
+
+static void
+_drop_directory(void *data EINA_UNUSED, Evas_Object *obj, void *event)
+{
+  Drop_Selection *drop = evas_object_data_get(obj, "portal-drop-selection");
+
+  if (drop && (!event || strcmp(drop->folder, event))) _drop_clear(obj);
+}
+
+static Eina_Bool
+_drop_select(void *data)
+{
+  Evas_Object *fs = data;
+  Drop_Selection *drop = evas_object_data_get(fs, "portal-drop-selection");
+  Evas_Object *view = elm_object_part_content_get(fs, "elm.swallow.files");
+  Elm_Object_Item *it;
+  Eina_List *items = NULL, *l;
+  const char *path;
+
+  /* selected_set() repopulates the directory for each path. Select the
+   * genlist items instead, once the asynchronous listing contains the set.
+   * Use the item class to read even filenames outside the visible viewport. */
+  for (it = elm_genlist_first_item_get(view); it;
+       it = elm_genlist_item_next_get(it))
+    {
+      const Elm_Genlist_Item_Class *itc = elm_genlist_item_item_class_get(it);
+      char *markup, *name;
+
+      if (elm_genlist_item_parent_get(it) || !itc || !itc->func.text_get) continue;
+      markup = itc->func.text_get(elm_object_item_data_get(it), view, "elm.text");
+      name = markup ? elm_entry_markup_to_utf8(markup) : NULL;
+      free(markup);
+      if (!name) continue;
+      EINA_LIST_FOREACH(drop->paths, l, path)
+        if (!strcmp(name, ecore_file_file_get(path)))
+          {
+            items = eina_list_append(items, it);
+            break;
+          }
+      free(name);
+    }
+  if (eina_list_count(items) == eina_list_count(drop->paths))
+    {
+      /* Detach before selecting: selection callbacks may change the dialog. */
+      drop->timer = NULL;
+      _drop_clear(fs);
+      EINA_LIST_FREE(items, it) elm_genlist_item_selected_set(it, EINA_TRUE);
+      return ECORE_CALLBACK_CANCEL;
+    }
+  eina_list_free(items);
+  /* Files can disappear or become filtered out while the listing loads. */
+  if (ecore_time_get() >= drop->deadline)
+    {
+      drop->timer = NULL;
+      _drop_clear(fs);
+      return ECORE_CALLBACK_CANCEL;
+    }
+  return ECORE_CALLBACK_RENEW;
+}
+
+static char *
+_drop_path(const char *text)
+{
+  Efreet_Uri *uri;
+  const char *p;
+  char *path = NULL;
+  size_t length = 0;
+
+  /* Plain absolute paths are already decoded; '%' is a literal filename
+   * character here, not the start of a URI escape. */
+  if (text[0] == '/')
+    return ecore_file_exists(text) ? strdup(text) : NULL;
+
+  /* Validate escapes before calling Efreet, which expects complete %HH
+   * sequences and stores decoded paths in a PATH_MAX-sized buffer. */
+  for (p = text; *p; p++, length++)
+    if (*p == '%')
+      {
+        if (!p[1] || !p[2] || !isxdigit((unsigned char)p[1]) ||
+            !isxdigit((unsigned char)p[2]) ||
+            (p[1] == '0' && p[2] == '0')) return NULL;
+        p += 2;
+      }
+  if (length >= PATH_MAX) return NULL;
+  uri = efreet_uri_decode(text);
+  if (!uri) return NULL;
+  if (!strcasecmp(uri->protocol, "file") && uri->path[0] == '/' &&
+      (!uri->hostname[0] || !strcasecmp(uri->hostname, "localhost")) &&
+      ecore_file_exists(uri->path))
+    path = strdup(uri->path);
+  efreet_uri_free(uri);
+  return path;
+}
 
 static Eina_Bool
 _filter_match(const char *path, Eina_Bool directory, void *data)
@@ -32,6 +163,90 @@ _filter_match(const char *path, Eina_Bool directory, void *data)
   return EINA_FALSE;
 }
 
+static Eina_Bool
+_drop(void *data, Evas_Object *obj, Elm_Selection_Data *selection)
+{
+  Dialog *d = data;
+  Drop_Selection *drop;
+  char *text, *line, *saveptr, *path;
+  Eina_Bool hidden = EINA_FALSE;
+
+  if (d->confirmation || !selection ||
+      !(selection->format & ELM_SEL_FORMAT_URILIST) ||
+      !selection->data || !selection->len ||
+      selection->len > DIALOG_WIRE_MAX) return EINA_FALSE;
+  text = strndup(selection->data, selection->len);
+  drop = calloc(1, sizeof(*drop));
+  if (!text || !drop)
+    {
+      free(text);
+      free(drop);
+      return EINA_FALSE;
+    }
+  for (line = strtok_r(text, "\r\n", &saveptr); line;
+       line = strtok_r(NULL, "\r\n", &saveptr))
+    {
+      char *folder;
+      Eina_Bool directory;
+
+      if (line[0] == '#') continue;
+      path = _drop_path(line);
+      if (!path) goto error;
+      directory = ecore_file_is_dir(path);
+      if ((!!directory != !!d->directory) ||
+          (d->active && !_filter_match(path, directory, d->active)))
+        {
+          free(path);
+          goto error;
+        }
+      folder = ecore_file_dir_get(path);
+      if (!folder || (drop->folder && strcmp(drop->folder, folder)))
+        {
+          free(folder);
+          free(path);
+          goto error;
+        }
+      if (!drop->folder) drop->folder = folder;
+      else free(folder);
+      if (eina_list_search_unsorted(drop->paths, EINA_COMPARE_CB(strcmp), path))
+        {
+          free(path);
+          continue;
+        }
+      hidden |= ecore_file_file_get(path)[0] == '.';
+      drop->paths = eina_list_append(drop->paths, path);
+      /* A portal request allowing one selection must stay single-select. */
+      if (!d->multiple) break;
+    }
+  if (!drop->paths) goto error;
+  free(text);
+  text = NULL;
+  _drop_clear(obj);
+  /* These changes can repopulate the old directory, so make them before
+   * installing the pending selection and navigating to its directory. */
+  elm_object_text_set(elm_object_part_content_get(obj, "elm.swallow.search"), "");
+  if (hidden)
+    {
+      Evas_Object *check = evas_object_name_find(evas_object_evas_get(obj),
+                                                "portal-show-hidden");
+      elm_fileselector_hidden_visible_set(obj, EINA_TRUE);
+      if (check) elm_check_state_set(check, EINA_TRUE);
+    }
+  drop->deadline = ecore_time_get() + 10.0;
+  drop->timer = ecore_timer_add(0.05, _drop_select, obj);
+  if (!drop->timer) goto error;
+  evas_object_data_set(obj, "portal-drop-selection", drop);
+  elm_fileselector_path_set(obj, drop->folder);
+  return EINA_TRUE;
+
+error:
+  free(text);
+  EINA_LIST_FREE(drop->paths, path) free(path);
+  free(drop->folder);
+  free(drop);
+  return EINA_FALSE;
+}
+
 static void
 _filter_selected(void *data, Evas_Object *obj, void *event)
 {
@@ -265,6 +480,11 @@ dialog_file_show(Dialog *d)
   elm_fileselector_folder_only_set(o, d->directory);
   elm_fileselector_multi_select_set(o, d->multiple);
   elm_fileselector_buttons_ok_cancel_set(o, EINA_FALSE);
+  elm_drop_target_add(o, ELM_SEL_FORMAT_URILIST,
+                      NULL, NULL, NULL, NULL,  NULL, NULL, _drop, d);
+  evas_object_event_callback_add(o, EVAS_CALLBACK_DEL, _drop_deleted, NULL);
+  evas_object_smart_callback_add(o, "directory,open", _drop_directory, NULL);
+  evas_object_smart_callback_add(o, "selected", _drop_selected, NULL);
   evas_object_smart_callback_add(o, "done", _done, d);
   evas_object_smart_callback_add(o, "activated", _selected, d);
   /* Entries consume Return/Escape before the fileselector can handle them. */
diff --git a/src/tests/meson.build b/src/tests/meson.build
index 02f720c..931cb95 100644
--- a/src/tests/meson.build
+++ b/src/tests/meson.build
@@ -12,6 +12,11 @@ gui_env.set('XDG_DESKTOP_PORTAL_EFL_LIB_DIR', meson.current_build_dir() / 'lib')
 gui_env.set('ELM_ENGINE', 'buffer')
 gui_env.set('ELM_DISPLAY', 'buffer')
 gui_env.set('EINA_LOG_LEVEL', '0')
+filechooser_drop_test = executable('filechooser-drop-test',
+  ['filechooser_drop_test.c', dialog_shared_sources, dialog_ui_sources],
+  include_directories: [inc, dialog_inc],
+  dependencies: dialog_deps + [efreet, ecore_file])
+test('filechooser-drop', filechooser_drop_test, env: gui_env, timeout: 30)
 portal_test = executable('portal-test', 'portal_test.c',
   dependencies: [eldbus, ecore], install: false)
 python = find_program('python3')

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to