From 08331dfbe0716f91df47043c5f4749a7c77bede3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= <revol@free.fr>
Date: Mon, 19 Nov 2018 21:40:33 +0100
Subject: [PATCH 1/2] Add Haiku support

---
 scripts/xdg-copy.in         |  21 +++++
 scripts/xdg-email.in        |  30 ++++++++
 scripts/xdg-file-dialog.in  | 148 ++++++++++++++++++++++++++++++++++++
 scripts/xdg-open.in         |  15 ++++
 scripts/xdg-screensaver.in  |  56 +++++++++++++-
 scripts/xdg-settings.in     |  54 +++++++++++++
 scripts/xdg-terminal.in     |  19 +++++
 scripts/xdg-utils-common.in |  36 +++++++++
 8 files changed, 377 insertions(+), 2 deletions(-)

diff --git a/scripts/xdg-copy.in b/scripts/xdg-copy.in
index 12317b6..dc8b2a1 100644
--- a/scripts/xdg-copy.in
+++ b/scripts/xdg-copy.in
@@ -58,6 +58,23 @@ copy_gnome()
     fi
 }
 
+copy_haiku()
+{
+    if [ "$1" = "${1#file://}" ]; then
+        exit_failure_operation_impossible "no method available for copying '$source' to '$dest'"
+    fi
+    if [ "$2" = "${2#file://}" ]; then
+        exit_failure_operation_impossible "no method available for copying '$source' to '$dest'"
+    fi
+
+    # XXX: wasn't there a Tracker suite to copy files???
+    if [ $? -eq 0 ]; then
+        exit_success
+    else
+        exit_failure_operation_failed
+    fi
+}
+
 [ x"$1" != x"" ] || exit_failure_syntax
 
 source=
@@ -102,6 +119,10 @@ case "$DE" in
     copy_gnome "$source" "$dest"
     ;;
 
+#    haiku)
+#    copy_haiku "$source" "$dest"
+#    ;;
+#
     *)
     exit_failure_operation_impossible "no method available for copying '$source' to '$dest'"
     ;;
diff --git a/scripts/xdg-email.in b/scripts/xdg-email.in
index 6db58ad..fd5a568 100644
--- a/scripts/xdg-email.in
+++ b/scripts/xdg-email.in
@@ -234,6 +234,32 @@ open_flatpak()
     fi
 }
 
+open_haiku()
+{
+    DEBUG 1 "Running haiku-open \"$1\""
+    if [ "$(setmime -dump text/x-email | grep application/x-vnd.Be-MAIL)" = "" ]; then
+        # not using BeMail, just let registrar call the prefered app and hope it handles it
+        /bin/open "$1"
+    else
+        # BeMail has its own args
+        # at least until it handles a full mailto:
+        for app in /boot/system/apps/Mail; do
+            test -x $app && break
+        done
+        test -x "$app" || exit_failure_operation_impossible
+        # xargs doesn't like multiline stuff... we have to do that to unescape urls but that's a bit dangerous.
+        builtin echo -ne "$app " "`echo "$1" | tr '&?' '\n\n' | \
+            sed 's/^to=/mailto:/;s/^cc=/ccto:/;s/^bcc=/bccto:/;s/^subject=\(.*\)/-subject "\1"/;s/body=\(.*\)/-body "\1"/;s/^attach=\(.*\)/"enclosure:\1"/;' | \
+            sed 's/%22/\\\\"/g;s/%0D//g;s/%/\\\x/g;s/$/ /' | sed 's/$/\\\\/' `" | /bin/sh
+    fi
+
+    if [ $? -eq 0 ]; then
+        exit_success
+    else
+        exit_failure_operation_failed
+    fi
+}
+
 open_generic()
 {
     local client
@@ -484,6 +510,10 @@ case "$DE" in
     open_flatpak "$url"
     ;;
 
+    haiku)
+    open_haiku "${mailto}"
+    ;;
+
     generic|lxqt|enlightenment)
     open_generic "${mailto}"
     ;;
diff --git a/scripts/xdg-file-dialog.in b/scripts/xdg-file-dialog.in
index 2b17fd5..cd472d6 100644
--- a/scripts/xdg-file-dialog.in
+++ b/scripts/xdg-file-dialog.in
@@ -85,6 +85,39 @@ open_zenity()
     fi
 }
 
+open_haiku()
+{
+    FILEPANEL=/bin/filepanel
+    if [ -x $FILEPANEL ]; then
+        if [ x"$1" != x"" ]; then
+            FOLDER=`dirname "$1"`
+            FILENAME=`basename "$1"`
+        fi
+
+        if [ x"$FILENAME" != x"" ]; then
+            if [ x"$TITLE" != x"" ]; then
+                $FILEPANEL --title "$TITLE" --load --single --kind fs --default "$FILENAME"
+            else
+                $FILEPANEL --load --single --kind fs --default "$FILENAME"
+            fi
+        else
+            if [ x"$TITLE" != x"" ]; then
+                $FILEPANEL --title "$TITLE" --load --single --kind fs
+            else
+                $FILEPANEL --load --single --kind fs
+            fi
+        fi
+
+        if [ $? -eq 0 ]; then
+            exit_success
+        else
+            exit_failure_operation_failed
+        fi
+    else
+        exit_failure_operation_impossible
+    fi
+}
+
 open_multi_kde()
 {
     DIALOG=`which kdialog`
@@ -143,6 +176,39 @@ open_multi_zenity()
     fi
 }
 
+open_multi_haiku()
+{
+    FILEPANEL=/bin/filepanel
+    if [ -x $FILEPANEL ]; then
+        if [ x"$1" != x"" ]; then
+            FOLDER=`dirname "$1"`
+            FILENAME=`basename "$1"`
+        fi
+
+        if [ x"$FILENAME" != x"" ]; then
+            if [ x"$TITLE" != x"" ]; then
+                $FILEPANEL --title "$TITLE" --load --kind fs --default "$FILENAME"
+            else
+                $FILEPANEL --load --kind fs --default "$FILENAME"
+            fi
+        else
+            if [ x"$TITLE" != x"" ]; then
+                $FILEPANEL --title "$TITLE" --load --kind fs
+            else
+                $FILEPANEL --load --kind fs
+            fi
+        fi
+
+        if [ $? -eq 0 ]; then
+            exit_success
+        else
+            exit_failure_operation_failed
+        fi
+    else
+        exit_failure_operation_impossible
+    fi
+}
+
 save_kde()
 {
     DIALOG=`which kdialog`
@@ -199,6 +265,39 @@ save_zenity()
     fi
 }
 
+save_haiku()
+{
+    FILEPANEL=/bin/filepanel
+    if [ -x $FILEPANEL ]; then
+        if [ x"$1" != x"" ]; then
+            FOLDER=`dirname "$1"`
+            FILENAME=`basename "$1"`
+        fi
+
+        if [ x"$FILENAME" != x"" ]; then
+            if [ x"$TITLE" != x"" ]; then
+                $FILEPANEL --title "$TITLE" --save --single --kind fs --default "$FILENAME"
+            else
+                $FILEPANEL --save --single --kind fs --default "$FILENAME"
+            fi
+        else
+            if [ x"$TITLE" != x"" ]; then
+                $FILEPANEL --title "$TITLE" --save --single --kind fs
+            else
+                $FILEPANEL --save --single --kind fs
+            fi
+        fi
+
+        if [ $? -eq 0 ]; then
+            exit_success
+        else
+            exit_failure_operation_failed
+        fi
+    else
+        exit_failure_operation_impossible
+    fi
+}
+
 directory_kde()
 {
     DIALOG=`which kdialog`
@@ -243,6 +342,39 @@ directory_zenity()
     fi
 }
 
+directory_haiku()
+{
+    FILEPANEL=/bin/filepanel
+    if [ -x $FILEPANEL ]; then
+        if [ x"$1" != x"" ]; then
+            FOLDER=`dirname "$1"`
+            FILENAME=`basename "$1"`
+        fi
+
+        if [ x"$FILENAME" != x"" ]; then
+            if [ x"$TITLE" != x"" ]; then
+                $FILEPANEL --title "$TITLE" --load --single --kind d --default "$FILENAME"
+            else
+                $FILEPANEL --load --single --kind d "$FILENAME"
+            fi
+        else
+            if [ x"$TITLE" != x"" ]; then
+                $FILEPANEL --title "$TITLE" --load --single --kind d
+            else
+                $FILEPANEL --load --single --kind d
+            fi
+        fi
+
+        if [ $? -eq 0 ]; then
+            exit_success
+        else
+            exit_failure_operation_failed
+        fi
+    else
+        exit_failure_operation_impossible
+    fi
+}
+
 [ x"$1" != x"" ] || exit_failure_syntax
 
 TITLE=
@@ -316,6 +448,10 @@ if [ "$action" = "openfilename" ]; then
         open_zenity "$filename"
         ;;
 
+        haiku)
+        open_haiku "$filename"
+        ;;
+
         *)
         exit_failure_operation_impossible "no method available for opening a filename dialog"
         ;;
@@ -330,6 +466,10 @@ elif [ "$action" = "openfilenamelist" ]; then
         open_multi_zenity "$filename"
         ;;
 
+        haiku)
+        open_multi_haiku "$filename"
+        ;;
+
         *)
         exit_failure_operation_impossible "no method available for opening a filename dialog"
         ;;
@@ -344,6 +484,10 @@ elif [ "$action" = "savefilename" ]; then
         save_zenity "$filename"
         ;;
 
+        haiku)
+        save_haiku "$filename"
+        ;;
+
         *)
         exit_failure_operation_impossible "no method available for opening a filename dialog"
         ;;
@@ -358,6 +502,10 @@ elif [ "$action" = "directory" ]; then
         directory_zenity "$filename"
         ;;
 
+        haiku)
+        directory_haiku "$filename"
+        ;;
+
         *)
         exit_failure_operation_impossible "no method available for opening a directory dialog"
         ;;
diff --git a/scripts/xdg-open.in b/scripts/xdg-open.in
index 4928538..83c5edf 100644
--- a/scripts/xdg-open.in
+++ b/scripts/xdg-open.in
@@ -119,6 +119,17 @@ open_darwin()
     fi
 }
 
+open_haiku()
+{
+    open "$1"
+
+    if [ $? -eq 0 ]; then
+        exit_success
+    else
+        exit_failure_operation_failed
+    fi
+}
+
 open_kde()
 {
     if [ -n "${KDE_SESSION_VERSION}" ]; then
@@ -568,6 +579,10 @@ case "$DE" in
     open_flatpak "$url"
     ;;
 
+    haiku)
+    open_haiku "$url"
+    ;;
+
     generic)
     open_generic "$url"
     ;;
diff --git a/scripts/xdg-screensaver.in b/scripts/xdg-screensaver.in
index 9e68196..9fda213 100644
--- a/scripts/xdg-screensaver.in
+++ b/scripts/xdg-screensaver.in
@@ -70,7 +70,7 @@ perform_action()
 {
   result=1
 
-  if [ "$1" = "resume" ] ; then
+  if [ -n "$DISPLAY" -a "$1" = "resume" ] ; then
       # Restore DPMS state
       if [ -f "$screensaver_file.dpms" ]; then
           rm "$screensaver_file.dpms"
@@ -78,7 +78,7 @@ perform_action()
           xset +dpms
       fi
   fi
-  if [ "$1" = "reset" ] ; then
+  if [ -n "$DISPLAY" -a "$1" = "reset" ] ; then
       if xset -q | grep 'DPMS is Enabled' > /dev/null 2> /dev/null; then
           xset -dpms
           xset +dpms
@@ -118,6 +118,10 @@ perform_action()
       [ -n "$DISPLAY" ] && screensaver_xserver "$1"
       ;;
 
+    haiku)
+      screensaver_haiku "$1"
+      ;;
+
     ''|generic)
       [ -n "$DISPLAY" ] && screensaver_xserver "$1"
       ;;
@@ -827,6 +831,54 @@ xautolock_screensaver()
     esac
 }
 
+screensaver_haiku()
+{
+    case "$1" in
+        suspend)
+        # unimplemented
+        result=0
+        ;;
+
+        resume)
+        # unimplemented
+        result=0
+        ;;
+
+        activate)
+        /bin/screen_blanker > /dev/null 2> /dev/null &
+        result=$?
+        ;;
+
+        lock)
+        # should lock
+        /bin/screen_blanker > /dev/null 2> /dev/null &
+        result=$?
+        ;;
+
+        reset)
+        # Turns the screensaver off right now
+        /bin/quit application/x-vnd.Be.screenblanker > /dev/null 2> /dev/null
+        result=$?
+        ;;
+
+        status)
+        result=0
+        roster | grep application/x-vnd.Haiku.screenblanker > /dev/null 2> /dev/null
+        result=$?
+        if [ $result -eq 0 ]; then
+            echo "enabled"
+        else
+            echo "disabled"
+        fi
+        ;;
+
+        *)
+        echo "ERROR:  Unknown command '$1" >&2
+        return 1
+        ;;
+    esac
+}
+
 [ x"$1" != x"" ] || exit_failure_syntax
 
 action=
diff --git a/scripts/xdg-settings.in b/scripts/xdg-settings.in
index 3781de8..c0c61d1 100644
--- a/scripts/xdg-settings.in
+++ b/scripts/xdg-settings.in
@@ -471,6 +471,60 @@ set_browser_xfce()
 }
 
 # }}} xfce
+# {{{ Haiku
+
+get_browser_haiku()
+{
+    # For historical reasons Haiku does like BeOS and uses
+    # the default handler for text/html
+    # instead of application/x-vnd.Be.URL.http
+    sig="`setmime -dump text/html | sed 's/.*-preferredAppSig //;s/ .*//'`"
+    binary="`setmime -dump text/html | sed 's/.*-preferredApp //;s/ .*//'`"
+    # Haiku does not use .desktop files so we fake them
+    [ -n "$binary" ] && desktop="`binary_to_desktop_file "$binary"`" && return
+    [ -n "$sig" ] && echo "${sig#application/x-vnd.}.desktop"
+}
+
+check_browser_haiku()
+{
+    desktop="$1"
+    check="`desktop_file_to_binary "$1"`"
+    if [ -z "$check" ]; then
+        echo no
+        exit_success
+    fi
+    browser="`get_browser_haiku`"
+    if [ x"$browser" != x"$desktop" ]; then
+        echo no
+        exit_success
+    fi
+    echo no
+    # Check HTTP and HTTPS, but not about: and unknown:.
+    for protocol in http https; do
+        sig="`setmime -dump application/x-vnd.Be.URL.$protocol | sed 's/.*-preferredAppSig //;s/ .*//'`"
+        browser="${sig#application/x-vnd.}.desktop"
+        if [ x"$browser" != x"$desktop" ]; then
+            echo no
+            exit_success
+        fi
+    done
+    echo yes
+    exit_success
+}
+
+set_browser_haiku()
+{
+    binary="`desktop_file_to_binary "$1"`"
+    [ "$binary" ] || exit_failure_file_missing
+    sig="application/x-vnd.${desktop%.desktop}"
+
+    # Set the default browser.
+    setmime -set text/html -preferredAppSig "$sig" || return
+    for protocol in http https; do
+        setmime -set application/x-vnd.Be.URL.$protocol -preferredAppSig "$sig" || return
+    done
+}
+# }}} Haiku
 # {{{ generic
 
 get_browser_generic()
diff --git a/scripts/xdg-terminal.in b/scripts/xdg-terminal.in
index f67897d..02ebbc1 100644
--- a/scripts/xdg-terminal.in
+++ b/scripts/xdg-terminal.in
@@ -127,6 +127,21 @@ terminal_xfce()
     fi
 }
 
+terminal_haiku()
+{
+    if [ x"$1" == x"" ]; then
+        Terminal &
+    else
+        Terminal -c /bin/sh -c "$1" &
+    fi
+
+    if [ $? -eq 0 ]; then
+        exit_success
+    else
+        exit_failure_operation_failed
+    fi
+}
+
 terminal_generic()
 {
     # if $TERM is a known non-command, use hard-coded fallbacks
@@ -266,6 +281,10 @@ case "$DE" in
     terminal_enlightenment "$command"
     ;;
 
+    haiku)
+    terminal_haiku "$command"
+    ;;
+
     generic)
     terminal_generic "$command"
     ;;
diff --git a/scripts/xdg-utils-common.in b/scripts/xdg-utils-common.in
index b4333e2..b1fabe4 100644
--- a/scripts/xdg-utils-common.in
+++ b/scripts/xdg-utils-common.in
@@ -87,6 +87,35 @@ desktop_file_to_binary()
     done
 }
 
+if [ -n "$BE_HOST_CPU" ]; then
+# Haiku does not use .desktop files, we fake a mapping to app signatures
+
+#-------------------------------------------------------------
+# map a binary to a .desktop file
+binary_to_desktop_file()
+{
+    binary="`which "$1"`"
+    binary="`readlink -f "$binary"`"
+    sig="`catattr -r BEOS:APP_SIG "$binary"`"
+    # TODO fallback to resources?
+    [ -n "$sig" ] && echo "${sig#application/x-vnd.}.desktop"
+}
+
+#-------------------------------------------------------------
+# map a .desktop file to a binary
+desktop_file_to_binary()
+{
+    desktop="`basename "$1"`"
+	sig="application/x-vnd.${desktop%.desktop}"
+	# first check system packages
+	command="`query -v /boot/system "(BEOS:APP_SIG==\"$sig\")" | head -1`"
+	# if not, try everywhere
+	[ -z "$command" ] && command="`query -a "(BEOS:APP_SIG==\"$sig\")" | head -1`"
+    command="`which "$command"`"
+    readlink -f "$command"
+}
+fi
+
 #-------------------------------------------------------------
 # Exit script on successfully completing the desired operation
 
@@ -314,6 +343,7 @@ detectDE()
       if [ x"$KDE_FULL_SESSION" != x"" ]; then DE=kde;
       elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
       elif [ x"$MATE_DESKTOP_SESSION_ID" != x"" ]; then DE=mate;
+      elif [ x"$BE_HOST_CPU" != x"" ]; then DE=haiku;
       elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
       elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
       elif xprop -root 2> /dev/null | grep -i '^xfce_desktop_window' >/dev/null 2>&1; then DE=xfce
@@ -349,6 +379,9 @@ detectDE()
         Darwin)
           DE=darwin;
           ;;
+        Haiku)
+          DE=haiku;
+          ;;
       esac
     fi
 
@@ -382,11 +415,14 @@ kfmclient_fix_exit_code()
 
 #----------------------------------------------------------------------------
 # Returns true if there is a graphical display attached.
+#TODO: test for ssh session
 
 has_display()
 {
     if [ -n "$DISPLAY" ] || [ -n "$WAYLAND_DISPLAY" ]; then
         return 0
+    elif [ -n "$APP_SERVER_NAME" ] || [ -n "$BE_HOST_CPU" ]; then
+        return 0
     else
         return 1
     fi
-- 
2.19.1


From 741e1302e8a25c34692f34622b091677b2ef758c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Revol?= <revol@free.fr>
Date: Mon, 19 Nov 2018 23:43:33 +0100
Subject: [PATCH 2/2] Fix iconv arg

there's no "utf8" in iconv -l
---
 scripts/xdg-email.in | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/xdg-email.in b/scripts/xdg-email.in
index fd5a568..4436a62 100644
--- a/scripts/xdg-email.in
+++ b/scripts/xdg-email.in
@@ -340,7 +340,7 @@ LC_ALL="$ORIG_LC_ALL"
 
 options=
 mailto=
-utf8="iconv -t utf8"
+utf8="iconv -t UTF-8"
 while [ $# -gt 0 ] ; do
     parm="$1"
     shift
-- 
2.19.1

