~willismonroe/ubuntu/precise/xdg-utils/typo-fix-996304

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
Description: Make the default mail client and browser settings work with the
 x-scheme-handler method of registering URI handlers in gnome3. Note, this is loosely
 based on http://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=6f49c05ceb2a1935d07c49c2e100b5cf3cdf5f26,
 however, we don't maintain separate methods for gnome and gnome3. The upstream gnome3
 detection doesn't work on Natty because we're not really using gnome3. In addition to this,
 when we set the defaults we use both the new and old methods so that we preserve the
 functionality of legacy apps using gnomevfs.

 This also contains additional changes to get/set default mimetype handlers from the users
 mimeapps.list rather than defaults.list, which isn't used by anything
 (see http://www.freedesktop.org/wiki/Specifications/mime-actions-spec)
Author: Bastien Nocera <hadess@hadess.net>
Author: Chris Coulson <chris.coulson@canonical.com>
Bug-Ubuntu: https://launchpad.net/bugs/670128

--- a/scripts/xdg-email
+++ b/scripts/xdg-email
@@ -310,6 +310,24 @@ check_output_file()
     fi
 }
 
+desktop_file_to_binary()
+{
+    search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
+    desktop="`basename "$1"`"
+    IFS=:
+    for dir in $search; do
+        unset IFS
+        [ "$dir" -a -d "$dir/applications" ] || continue
+        file="$dir/applications/$desktop"
+        [ -r "$file" ] || continue
+        # Remove any arguments (%F, %f, %U, %u, etc.).
+        command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
+        command="`which "$command"`"
+        readlink -f "$command"
+        return
+    done
+}
+
 #----------------------------------------
 # Checks for shared commands, e.g. --help
 
@@ -479,7 +497,9 @@ open_kde()
 open_gnome()
 {
     local client
-    client=`gconftool-2 --get /desktop/gnome/url-handlers/mailto/command | cut -d ' ' -f 1` || ""
+    local desktop
+    desktop=`xdg-mime query default "x-scheme-handler/mailto"`
+    client=`desktop_file_to_binary "$browser"`
     echo $client | grep thunderbird > /dev/null 2>&1
     if [ $? -eq 0 ] ; then
         run_thunderbird "$client" "$1"
--- a/scripts/xdg-settings
+++ b/scripts/xdg-settings
@@ -418,21 +418,26 @@ desktop_file_to_binary()
 # This function is hard-coded for text/html but it could be adapted if needed.
 fix_local_desktop_file()
 {
+    if test -z "$2" ; then
+        MIME="text/html"
+    else
+        MIME="$2"
+    fi
     apps="${XDG_DATA_HOME:-$HOME/.local/share}/applications"
     # No local desktop file?
     [ ! -f "$apps/$1" ] && return
-    MIME="`grep "^MimeType=" "$apps/$1" | cut -d= -f 2-`"
-    case "$MIME" in
-      text/html\;*|*\;text/html\;*|*\;text/html\;|*\;text/html)
-        # Already has text/html? Great!
+    MIMETYPES="`grep "^MimeType=" "$apps/$1" | cut -d= -f 2-`"
+    case "$MIMETYPES" in
+        $MIME\;*|*\;$MIME\;*|*\;$MIME\;|*\;$MIME)
+        # Already has the mime-type? Great!
         return 0
         ;;
     esac
 
-    # Add text/html to the list
+    # Add the mime-type to the list
     temp="`mktemp "$apps/$1.XXXXXX"`" || return
     grep -v "^MimeType=" "$apps/$1" >> "$temp"
-    echo "MimeType=text/html;$MIME" >> "$temp"
+    echo "MimeType=$MIME;$MIMETYPES" >> "$temp"
 
     oldlines="`wc -l < "$apps/$1"`"
     newlines="`wc -l < "$temp"`"
@@ -466,22 +471,32 @@ xdg_mime_fixup()
 
 get_browser_mime()
 {
+    if test -z "$1" ; then
+        MIME="text/html"
+    else
+        MIME="$1"
+    fi
     xdg_mime_fixup
-    xdg-mime query default text/html
+    xdg-mime query default "$MIME"
 }
 
 set_browser_mime()
 {
     xdg_mime_fixup
-    orig="`get_browser_mime`"
+    if test -z "$2" ; then
+        MIME="text/html"
+    else
+        MIME="$2"
+    fi
+    orig="`get_browser_mime $MIME`"
     # Fixing the local desktop file can actually change the default browser all
     # by itself, so we fix it only after querying to find the current default.
-    fix_local_desktop_file "$1" || return
+    fix_local_desktop_file "$1" "$MIME" || return
     mkdir -p "${XDG_DATA_HOME:-$HOME/.local/share}/applications"
-    xdg-mime default "$1" text/html || return
-    if [ x"`get_browser_mime`" != x"$1" ]; then
+    xdg-mime default "$1" "$MIME" || return
+    if [ x"`get_browser_mime $MIME`" != x"$1" ]; then
         # Put back the original value
-        xdg-mime default "$orig" text/html
+        xdg-mime default "$orig" "$MIME"
         exit_failure_operation_failed
     fi
 }
@@ -585,32 +600,32 @@ set_browser_kde()
 
 get_browser_gnome()
 {
-    binary="`gconftool-2 --get /desktop/gnome/applications/browser/exec | first_word`"
-    if [ x"$binary" = x ]; then
-        # No default browser; GNOME might use the MIME type text/html.
-        get_browser_mime
-    else
-        # gconftool gives the binary (maybe with %s etc. afterward),
-        # but we want the desktop file name, not the binary. So, we
-        # have to find the desktop file to which it corresponds.
-        desktop="`binary_to_desktop_file "$binary"`"
-        basename "$desktop"
-    fi
+    get_browser_mime "x-scheme-handler/http"
 }
 
 check_browser_gnome()
 {
+    desktop="$1"
     check="`desktop_file_to_binary "$1"`"
     if [ -z "$check" ]; then
         echo no
         exit_success
     fi
+    # Check HTTP and HTTPS, but not about: and unknown:, using the GNOME 3 scheme.
+    for protocol in http https; do
+        browser="`get_browser_mime "x-scheme-handler/$protocol"`"
+        if [ x"$browser" != x"$desktop" ]; then
+            echo no
+            exit_success
+        fi
+    done
+
     binary="`gconftool-2 --get /desktop/gnome/applications/browser/exec | first_word`"
     if [ x"$binary" != x"$check" ]; then
         echo no
         exit_success
     fi
-    # Check HTTP and HTTPS, but not about: and unknown:.
+    # Check HTTP and HTTPS, but not about: and unknown:, using the legacy GNOME scheme.
     for protocol in http https; do
         binary="`gconftool-2 --get /desktop/gnome/url-handlers/$protocol/command | first_word`"
         if [ x"$binary" != x"$check" ]; then
@@ -634,7 +649,12 @@ set_browser_gnome()
     [ "$binary" ] || exit_failure_file_missing
     set_browser_mime "$1" || return
 
-    # Set the default browser.
+    # Set the default browser using the GNOME 3 scheme.
+    for protocol in http https about unknown; do
+        set_browser_mime "$1" "x-scheme-handler/$protocol" || return
+    done
+
+    # Set the default browser using the legacy GNOME scheme.
     gconftool-2 --type string --set /desktop/gnome/applications/browser/exec "$binary"
     gconftool-2 --type bool --set /desktop/gnome/applications/browser/needs_term false
     gconftool-2 --type bool --set /desktop/gnome/applications/browser/nremote true
--- a/scripts/xdg-mime
+++ b/scripts/xdg-mime
@@ -667,15 +667,57 @@ make_default_generic()
     # Add $2=$1 to XDG_DATA_HOME/applications/defaults.list
     xdg_user_dir="$XDG_DATA_HOME"
     [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
-    default_file="$xdg_user_dir/applications/defaults.list"
+    default_file="$xdg_user_dir/applications/mimeapps.list"
     DEBUG 2 "make_default_generic $1 $2"
     DEBUG 1 "Updating $default_file"
-    grep -v "$2=" $default_file > ${default_file}.new 2> /dev/null
-    if ! grep "[Default Applications]" ${default_file}.new > /dev/null; then
-       echo "[Default Applications]" >> ${default_file}.new
-    fi
-    echo $2=$1 >> ${default_file}.new
-    mv ${default_file}.new $default_file
+    awk -v mimetype="$2" -v application="$1" '
+    BEGIN {
+        prefix=mimetype "="
+        indefault=0
+        added=0
+        blanks=0
+        found=0
+    }
+    {
+        suppress=0
+        if (index($0, "[Default Applications]") == 1) {
+            indefault=1
+            found=1
+        } else if (index($0, "[") == 1) {
+            if (!added && indefault) {
+                print prefix application
+                added=1
+            }
+            indefault=0
+        } else if ($0 == "") {
+            suppress=1
+            blanks++
+        } else if (indefault && !added && index($0, prefix) == 1) {
+                $0=prefix application
+                added=1
+        }
+        if (!suppress) {
+            while (blanks > 0) {
+                print ""
+                blanks--
+            }
+            print $0
+        }
+    }
+    END {
+        if (!added) {
+            if (!found) {
+                print ""
+                print "[Default Applications]"
+            }
+            print prefix application
+        }
+        while (blanks > 0) {
+            print ""
+            blanks--
+        }
+    }
+'   $default_file > ${default_file}.new && mv ${default_file}.new $default_file
 }
 
 defapp_generic()
@@ -687,7 +729,34 @@ defapp_generic()
     xdg_system_dirs="$XDG_DATA_DIRS"
     [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
 
-    for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
+    for x in `echo "$xdg_user_dir" | sed 's/:/ /g'`; do
+        mimeapps_list="$x/applications/mimeapps.list"
+        if [ -f "$mimeapps_list" ] ; then
+            trader_result=`awk -v mimetype="$MIME" '
+    BEGIN {
+        prefix=mimetype "="
+        indefault=0
+        found=0
+    }
+    {
+        if (index($0, "[Default Applications]") == 1) {
+            indefault=1
+        } else if (index($0, "[") == 1) {
+            indefault=0
+        } else if (!found && indefault && index($0, prefix) == 1) {
+            print substr($0, length(prefix) +1, length)
+            found=1
+        }
+    }
+'           $mimeapps_list`
+            if [ -n "$trader_result" ] ; then
+                echo $trader_result
+                exit_success
+            fi
+        fi
+    done
+
+    for x in `echo "$xdg_system_dirs" | sed 's/:/ /g'`; do
        DEBUG 2 "Checking $x/applications/defaults.list"
        trader_result=`grep "$MIME=" $x/applications/defaults.list 2> /dev/null | cut -d '=' -f 2 | cut -d ';' -f 1`
        if [ -n "$trader_result" ] ; then