xdg-settings 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. #!/bin/sh
  2. #---------------------------------------------
  3. # xdg-settings
  4. #
  5. # Utility script to get various settings from the desktop environment.
  6. #
  7. # Refer to the usage() function below for usage.
  8. #
  9. # Copyright 2009, Google Inc.
  10. #
  11. # LICENSE:
  12. #
  13. # Permission is hereby granted, free of charge, to any person obtaining a
  14. # copy of this software and associated documentation files (the "Software"),
  15. # to deal in the Software without restriction, including without limitation
  16. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. # and/or sell copies of the Software, and to permit persons to whom the
  18. # Software is furnished to do so, subject to the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included
  21. # in all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  24. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  27. # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  28. # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  29. # OTHER DEALINGS IN THE SOFTWARE.
  30. #
  31. #---------------------------------------------
  32. manualpage()
  33. {
  34. cat << _MANUALPAGE
  35. Name
  36. xdg-settings - get various settings from the desktop environment
  37. Synopsis
  38. xdg-settings { get | check | set } {property} [subproperty] [value]
  39. xdg-settings { --help | --list | --manual | --version }
  40. Description
  41. xdg-settings gets various settings from the desktop environment. For instance,
  42. desktop environments often provide proxy configuration and default web browser
  43. settings. Using xdg-settings these parameters can be extracted for use by
  44. applications that do not use the desktop environment's libraries (which would
  45. use the settings natively).
  46. xdg-settings is for use inside a desktop session only. It is not recommended to
  47. use xdg-settings as root.
  48. Options
  49. --help
  50. Show command synopsis.
  51. --list
  52. List all properties xdg-settings knows about.
  53. --manual
  54. Show this manualpage.
  55. --version
  56. Show the xdg-utils version information.
  57. Properties
  58. When using xdg-settings to get, check or set a destkop setting, properties and
  59. possibly sub-properties are used to specify the setting to be changed.
  60. Some properties (such as default-web-browser) fully describe the setting to be
  61. changed. Other properties (such as default-url-scheme-handler) require more
  62. information (in this case the actual scheme to set the default handler for)
  63. which must be provided in a sub-property.
  64. Exit Codes
  65. An exit code of 0 indicates success while a non-zero exit code indicates
  66. failure. The following failure codes can be returned:
  67. 1
  68. Error in command line syntax.
  69. 2
  70. One of the files passed on the command line did not exist.
  71. 3
  72. A required tool could not be found.
  73. 4
  74. The action failed.
  75. Examples
  76. Get the desktop file name of the current default web browser
  77. xdg-settings get default-web-browser
  78. Check whether the default web browser is firefox.desktop, which can be false
  79. even if "get default-web-browser" says that is the current value (if only some
  80. of the underlying settings actually reflect that value)
  81. xdg-settings check default-web-browser firefox.desktop
  82. Set the default web browser to google-chrome.desktop
  83. xdg-settings set default-web-browser google-chrome.desktop
  84. Set the default mailto URL scheme handler to be evolution.desktop
  85. xdg-settings set default-url-scheme-handler mailto evolution.desktop
  86. _MANUALPAGE
  87. }
  88. usage()
  89. {
  90. cat << _USAGE
  91. xdg-settings - get various settings from the desktop environment
  92. Synopsis
  93. xdg-settings { get | check | set } {property} [subproperty] [value]
  94. xdg-settings { --help | --list | --manual | --version }
  95. _USAGE
  96. }
  97. #@xdg-utils-common@
  98. #----------------------------------------------------------------------------
  99. # Common utility functions included in all XDG wrapper scripts
  100. #----------------------------------------------------------------------------
  101. DEBUG()
  102. {
  103. [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
  104. [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
  105. shift
  106. echo "$@" >&2
  107. }
  108. # This handles backslashes but not quote marks.
  109. first_word()
  110. {
  111. read first rest
  112. echo "$first"
  113. }
  114. #-------------------------------------------------------------
  115. # map a binary to a .desktop file
  116. binary_to_desktop_file()
  117. {
  118. search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
  119. binary="`which "$1"`"
  120. binary="`readlink -f "$binary"`"
  121. base="`basename "$binary"`"
  122. IFS=:
  123. for dir in $search; do
  124. unset IFS
  125. [ "$dir" ] || continue
  126. [ -d "$dir/applications" -o -d "$dir/applnk" ] || continue
  127. for file in "$dir"/applications/*.desktop "$dir"/applications/*/*.desktop "$dir"/applnk/*.desktop "$dir"/applnk/*/*.desktop; do
  128. [ -r "$file" ] || continue
  129. # Check to make sure it's worth the processing.
  130. grep -q "^Exec.*$base" "$file" || continue
  131. # Make sure it's a visible desktop file (e.g. not "preferred-web-browser.desktop").
  132. grep -Eq "^(NoDisplay|Hidden)=true" "$file" && continue
  133. command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
  134. command="`which "$command"`"
  135. if [ x"`readlink -f "$command"`" = x"$binary" ]; then
  136. # Fix any double slashes that got added path composition
  137. echo "$file" | sed -e 's,//*,/,g'
  138. return
  139. fi
  140. done
  141. done
  142. }
  143. #-------------------------------------------------------------
  144. # map a .desktop file to a binary
  145. ## FIXME: handle vendor dir case
  146. desktop_file_to_binary()
  147. {
  148. search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
  149. desktop="`basename "$1"`"
  150. IFS=:
  151. for dir in $search; do
  152. unset IFS
  153. [ "$dir" -a -d "$dir/applications" ] || continue
  154. file="$dir/applications/$desktop"
  155. [ -r "$file" ] || continue
  156. # Remove any arguments (%F, %f, %U, %u, etc.).
  157. command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
  158. command="`which "$command"`"
  159. readlink -f "$command"
  160. return
  161. done
  162. }
  163. #-------------------------------------------------------------
  164. # Exit script on successfully completing the desired operation
  165. exit_success()
  166. {
  167. if [ $# -gt 0 ]; then
  168. echo "$@"
  169. echo
  170. fi
  171. exit 0
  172. }
  173. #-----------------------------------------
  174. # Exit script on malformed arguments, not enough arguments
  175. # or missing required option.
  176. # prints usage information
  177. exit_failure_syntax()
  178. {
  179. if [ $# -gt 0 ]; then
  180. echo "xdg-settings: $@" >&2
  181. echo "Try 'xdg-settings --help' for more information." >&2
  182. else
  183. usage
  184. echo "Use 'man xdg-settings' or 'xdg-settings --manual' for additional info."
  185. fi
  186. exit 1
  187. }
  188. #-------------------------------------------------------------
  189. # Exit script on missing file specified on command line
  190. exit_failure_file_missing()
  191. {
  192. if [ $# -gt 0 ]; then
  193. echo "xdg-settings: $@" >&2
  194. fi
  195. exit 2
  196. }
  197. #-------------------------------------------------------------
  198. # Exit script on failure to locate necessary tool applications
  199. exit_failure_operation_impossible()
  200. {
  201. if [ $# -gt 0 ]; then
  202. echo "xdg-settings: $@" >&2
  203. fi
  204. exit 3
  205. }
  206. #-------------------------------------------------------------
  207. # Exit script on failure returned by a tool application
  208. exit_failure_operation_failed()
  209. {
  210. if [ $# -gt 0 ]; then
  211. echo "xdg-settings: $@" >&2
  212. fi
  213. exit 4
  214. }
  215. #------------------------------------------------------------
  216. # Exit script on insufficient permission to read a specified file
  217. exit_failure_file_permission_read()
  218. {
  219. if [ $# -gt 0 ]; then
  220. echo "xdg-settings: $@" >&2
  221. fi
  222. exit 5
  223. }
  224. #------------------------------------------------------------
  225. # Exit script on insufficient permission to write a specified file
  226. exit_failure_file_permission_write()
  227. {
  228. if [ $# -gt 0 ]; then
  229. echo "xdg-settings: $@" >&2
  230. fi
  231. exit 6
  232. }
  233. check_input_file()
  234. {
  235. if [ ! -e "$1" ]; then
  236. exit_failure_file_missing "file '$1' does not exist"
  237. fi
  238. if [ ! -r "$1" ]; then
  239. exit_failure_file_permission_read "no permission to read file '$1'"
  240. fi
  241. }
  242. check_vendor_prefix()
  243. {
  244. file_label="$2"
  245. [ -n "$file_label" ] || file_label="filename"
  246. file=`basename "$1"`
  247. case "$file" in
  248. [a-zA-Z]*-*)
  249. return
  250. ;;
  251. esac
  252. echo "xdg-settings: $file_label '$file' does not have a proper vendor prefix" >&2
  253. echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
  254. echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
  255. echo "Use --novendor to override or 'xdg-settings --manual' for additional info." >&2
  256. exit 1
  257. }
  258. check_output_file()
  259. {
  260. # if the file exists, check if it is writeable
  261. # if it does not exists, check if we are allowed to write on the directory
  262. if [ -e "$1" ]; then
  263. if [ ! -w "$1" ]; then
  264. exit_failure_file_permission_write "no permission to write to file '$1'"
  265. fi
  266. else
  267. DIR=`dirname "$1"`
  268. if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
  269. exit_failure_file_permission_write "no permission to create file '$1'"
  270. fi
  271. fi
  272. }
  273. #----------------------------------------
  274. # Checks for shared commands, e.g. --help
  275. check_common_commands()
  276. {
  277. while [ $# -gt 0 ] ; do
  278. parm="$1"
  279. shift
  280. case "$parm" in
  281. --help)
  282. usage
  283. echo "Use 'man xdg-settings' or 'xdg-settings --manual' for additional info."
  284. exit_success
  285. ;;
  286. --manual)
  287. manualpage
  288. exit_success
  289. ;;
  290. --version)
  291. echo "xdg-settings 1.1.0 rc1"
  292. exit_success
  293. ;;
  294. esac
  295. done
  296. }
  297. check_common_commands "$@"
  298. [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
  299. if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
  300. # Be silent
  301. xdg_redirect_output=" > /dev/null 2> /dev/null"
  302. else
  303. # All output to stderr
  304. xdg_redirect_output=" >&2"
  305. fi
  306. #--------------------------------------
  307. # Checks for known desktop environments
  308. # set variable DE to the desktop environments name, lowercase
  309. detectDE()
  310. {
  311. # see https://bugs.freedesktop.org/show_bug.cgi?id=34164
  312. unset GREP_OPTIONS
  313. if [ -n "${XDG_CURRENT_DESKTOP}" ]; then
  314. case "${XDG_CURRENT_DESKTOP}" in
  315. GNOME)
  316. DE=gnome;
  317. ;;
  318. KDE)
  319. DE=kde;
  320. ;;
  321. LXDE)
  322. DE=lxde;
  323. ;;
  324. XFCE)
  325. DE=xfce
  326. esac
  327. fi
  328. if [ x"$DE" = x"" ]; then
  329. # classic fallbacks
  330. if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
  331. elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
  332. 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;
  333. elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
  334. elif xprop -root 2> /dev/null | grep -i '^xfce_desktop_window' >/dev/null 2>&1; then DE=xfce
  335. fi
  336. fi
  337. if [ x"$DE" = x"" ]; then
  338. # fallback to checking $DESKTOP_SESSION
  339. case "$DESKTOP_SESSION" in
  340. gnome)
  341. DE=gnome;
  342. ;;
  343. LXDE)
  344. DE=lxde;
  345. ;;
  346. xfce|xfce4)
  347. DE=xfce;
  348. ;;
  349. esac
  350. fi
  351. if [ x"$DE" = x"" ]; then
  352. # fallback to uname output for other platforms
  353. case "$(uname 2>/dev/null)" in
  354. Darwin)
  355. DE=darwin;
  356. ;;
  357. esac
  358. fi
  359. if [ x"$DE" = x"gnome" ]; then
  360. # gnome-default-applications-properties is only available in GNOME 2.x
  361. # but not in GNOME 3.x
  362. which gnome-default-applications-properties > /dev/null 2>&1 || DE="gnome3"
  363. fi
  364. }
  365. #----------------------------------------------------------------------------
  366. # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
  367. # It also always returns 1 in KDE 3.4 and earlier
  368. # Simply return 0 in such case
  369. kfmclient_fix_exit_code()
  370. {
  371. version=`LC_ALL=C.UTF-8 kde-config --version 2>/dev/null | grep '^KDE'`
  372. major=`echo $version | sed 's/KDE.*: \([0-9]\).*/\1/'`
  373. minor=`echo $version | sed 's/KDE.*: [0-9]*\.\([0-9]\).*/\1/'`
  374. release=`echo $version | sed 's/KDE.*: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
  375. test "$major" -gt 3 && return $1
  376. test "$minor" -gt 5 && return $1
  377. test "$release" -gt 4 && return $1
  378. return 0
  379. }
  380. check_desktop_filename()
  381. {
  382. case "$1" in
  383. */*)
  384. exit_failure_syntax "invalid application name"
  385. ;;
  386. *.desktop)
  387. return
  388. ;;
  389. *)
  390. exit_failure_syntax "invalid application name"
  391. ;;
  392. esac
  393. }
  394. # {{{ default browser
  395. # {{{ utility functions
  396. # In order to remove an application from the automatically-generated list of
  397. # applications for handling a given MIME type, the desktop environment may copy
  398. # the global .desktop file into the user's .local directory, and remove that
  399. # MIME type from its list. In that case, we must restore the MIME type to the
  400. # application's list of MIME types before we can set it as the default for that
  401. # MIME type. (We can't just delete the local version, since the user may have
  402. # made other changes to it as well. So, tweak the existing file.)
  403. # This function is hard-coded for text/html but it could be adapted if needed.
  404. fix_local_desktop_file()
  405. {
  406. if test -z "$2" ; then
  407. MIME="text/html"
  408. else
  409. MIME="$2"
  410. fi
  411. apps="${XDG_DATA_HOME:-$HOME/.local/share}/applications"
  412. # No local desktop file?
  413. [ ! -f "$apps/$1" ] && return
  414. MIMETYPES="`grep "^MimeType=" "$apps/$1" | cut -d= -f 2-`"
  415. case "$MIMETYPES" in
  416. $MIME\;*|*\;$MIME\;*|*\;$MIME\;|*\;$MIME)
  417. # Already has the mime-type? Great!
  418. return 0
  419. ;;
  420. esac
  421. # Add the mime-type to the list
  422. temp="`mktemp "$apps/$1.XXXXXX"`" || return
  423. grep -v "^MimeType=" "$apps/$1" >> "$temp"
  424. echo "MimeType=$MIME;$MIMETYPES" >> "$temp"
  425. oldlines="`wc -l < "$apps/$1"`"
  426. newlines="`wc -l < "$temp"`"
  427. # The new file should have at least as many lines as the old.
  428. if [ $oldlines -le $newlines ]; then
  429. mv "$temp" "$apps/$1"
  430. # This can take a little bit to get noticed.
  431. sleep 4
  432. else
  433. rm -f "$temp"
  434. return 1
  435. fi
  436. }
  437. # }}} utility functions
  438. # {{{ MIME utilities
  439. xdg_mime_fixup()
  440. {
  441. # xdg-mime may use ktradertest, which will fork off a copy of kdeinit if
  442. # one does not already exist. It will exit after about 15 seconds if no
  443. # further processes need it around. But since it does not close its stdout,
  444. # the shell (via grep) will wait around for kdeinit to exit. If we start a
  445. # copy here, that copy will be used in xdg-mime and we will avoid waiting.
  446. if [ "$DE" = kde -a -z "$XDG_MIME_FIXED" ]; then
  447. ktradertest text/html Application > /dev/null 2>&1
  448. # Only do this once, as we only need it once.
  449. XDG_MIME_FIXED=yes
  450. fi
  451. }
  452. get_browser_mime()
  453. {
  454. if test -z "$1" ; then
  455. MIME="text/html"
  456. else
  457. MIME="$1"
  458. fi
  459. xdg_mime_fixup
  460. xdg-mime query default "$MIME"
  461. }
  462. set_browser_mime()
  463. {
  464. xdg_mime_fixup
  465. if test -z "$2" ; then
  466. MIME="text/html"
  467. else
  468. MIME="$2"
  469. fi
  470. orig="`get_browser_mime $MIME`"
  471. # Fixing the local desktop file can actually change the default browser all
  472. # by itself, so we fix it only after querying to find the current default.
  473. fix_local_desktop_file "$1" "$MIME" || return
  474. mkdir -p "${XDG_DATA_HOME:-$HOME/.local/share}/applications"
  475. xdg-mime default "$1" "$MIME" || return
  476. if [ x"`get_browser_mime`" != x"$1" ]; then
  477. # Put back the original value
  478. xdg-mime default "$orig" "$MIME"
  479. exit_failure_operation_failed
  480. fi
  481. }
  482. # }}} MIME utilities
  483. # {{{ KDE utilities
  484. # Reads the KDE configuration setting, compensating for a bug in some versions of kreadconfig.
  485. read_kde_config()
  486. {
  487. configfile="$1"
  488. configsection="$2"
  489. configkey="$3"
  490. application="`kreadconfig --file $configfile --group $configsection --key $configkey`"
  491. if [ x"$application" != x ]; then
  492. echo "$application"
  493. else
  494. # kreadconfig in KDE 4 may not notice Key[$*]=... localized settings, so
  495. # check by hand if it didn't find anything (oddly kwriteconfig works
  496. # fine though).
  497. configfile_dir=`kde${KDE_SESSION_VERSION}-config --path config | cut -d ':' -f 1`
  498. configfile_path="$configfile_dir/$configfile"
  499. [ ! -f "$configfile_path" ] && return
  500. # This will only take the first value if there is more than one.
  501. grep "^$configkey"'\[$[^]=]*\]=' "$configfile_path" | head -n 1 | cut -d= -f 2-
  502. fi
  503. }
  504. # }}} KDE utilities
  505. # {{{ KDE
  506. # Resolves the KDE browser setting to a binary: if prefixed with !, simply removes it;
  507. # otherwise, uses desktop_file_to_binary to get the binary out of the desktop file.
  508. resolve_kde_browser()
  509. {
  510. [ -z "$browser" ] && return
  511. case "$browser" in
  512. !*)
  513. echo "${browser#!}"
  514. ;;
  515. *)
  516. desktop_file_to_binary "$browser"
  517. ;;
  518. esac
  519. }
  520. # Does the opposite of resolve_kde_browser: if prefixed with !, tries to find a desktop
  521. # file corresponding to the binary, otherwise just returns the desktop file name.
  522. resolve_kde_browser_desktop()
  523. {
  524. [ -z "$browser" ] && return
  525. case "$browser" in
  526. !*)
  527. desktop="`binary_to_desktop_file "${browser#!}"`"
  528. basename "$desktop"
  529. ;;
  530. *)
  531. echo "$browser"
  532. ;;
  533. esac
  534. }
  535. read_kde_browser()
  536. {
  537. read_kde_config kdeglobals General BrowserApplication
  538. }
  539. get_browser_kde()
  540. {
  541. browser="`read_kde_browser`"
  542. if [ x"$browser" = x ]; then
  543. # No explicit default browser; KDE will use the MIME type text/html.
  544. get_browser_mime
  545. else
  546. resolve_kde_browser_desktop
  547. fi
  548. }
  549. check_browser_kde()
  550. {
  551. check="`desktop_file_to_binary "$1"`"
  552. if [ -z "$check" ]; then
  553. echo no
  554. exit_success
  555. fi
  556. browser="`read_kde_browser`"
  557. binary="`resolve_kde_browser`"
  558. # Because KDE will use the handler for MIME type text/html if this value
  559. # is empty, we allow either the empty string or a match to $check here.
  560. if [ x"$binary" != x -a x"$binary" != x"$check" ]; then
  561. echo no
  562. exit_success
  563. fi
  564. browser="`get_browser_mime`"
  565. binary="`desktop_file_to_binary "$browser"`"
  566. if [ x"$binary" != x"$check" ]; then
  567. echo no
  568. exit_success
  569. fi
  570. echo yes
  571. exit_success
  572. }
  573. set_browser_kde()
  574. {
  575. set_browser_mime "$1" || return
  576. kwriteconfig --file kdeglobals --group General --key BrowserApplication "$1"
  577. }
  578. # }}} KDE
  579. # {{{ GNOME
  580. get_browser_gnome()
  581. {
  582. binary="`gconftool-2 --get /desktop/gnome/applications/browser/exec | first_word`"
  583. if [ x"$binary" = x ]; then
  584. # No default browser; GNOME might use the MIME type text/html.
  585. get_browser_mime
  586. else
  587. # gconftool gives the binary (maybe with %s etc. afterward),
  588. # but we want the desktop file name, not the binary. So, we
  589. # have to find the desktop file to which it corresponds.
  590. desktop="`binary_to_desktop_file "$binary"`"
  591. basename "$desktop"
  592. fi
  593. }
  594. check_browser_gnome()
  595. {
  596. check="`desktop_file_to_binary "$1"`"
  597. if [ -z "$check" ]; then
  598. echo no
  599. exit_success
  600. fi
  601. binary="`gconftool-2 --get /desktop/gnome/applications/browser/exec | first_word`"
  602. if [ x"$binary" != x"$check" ]; then
  603. echo no
  604. exit_success
  605. fi
  606. # Check HTTP and HTTPS, but not about: and unknown:.
  607. for protocol in http https; do
  608. binary="`gconftool-2 --get /desktop/gnome/url-handlers/$protocol/command | first_word`"
  609. if [ x"$binary" != x"$check" ]; then
  610. echo no
  611. exit_success
  612. fi
  613. done
  614. browser="`get_browser_mime`"
  615. binary="`desktop_file_to_binary "$browser"`"
  616. if [ x"$binary" != x"$check" ]; then
  617. echo no
  618. exit_success
  619. fi
  620. echo yes
  621. exit_success
  622. }
  623. set_browser_gnome()
  624. {
  625. binary="`desktop_file_to_binary "$1"`"
  626. [ "$binary" ] || exit_failure_file_missing
  627. set_browser_mime "$1" || return
  628. # Set the default browser.
  629. gconftool-2 --type string --set /desktop/gnome/applications/browser/exec "$binary"
  630. gconftool-2 --type bool --set /desktop/gnome/applications/browser/needs_term false
  631. gconftool-2 --type bool --set /desktop/gnome/applications/browser/nremote true
  632. # Set the handler for HTTP and HTTPS.
  633. for protocol in http https; do
  634. gconftool-2 --type string --set /desktop/gnome/url-handlers/$protocol/command "$binary %s"
  635. gconftool-2 --type bool --set /desktop/gnome/url-handlers/$protocol/needs_terminal false
  636. gconftool-2 --type bool --set /desktop/gnome/url-handlers/$protocol/enabled true
  637. done
  638. # Set the handler for about: and unknown URL types.
  639. for protocol in about unknown; do
  640. gconftool-2 --type string --set /desktop/gnome/url-handlers/$protocol/command "$binary %s"
  641. done
  642. }
  643. # }}} GNOME
  644. # {{{ GNOME 3.x
  645. get_browser_gnome3()
  646. {
  647. get_browser_mime "x-scheme-handler/http"
  648. }
  649. check_browser_gnome3()
  650. {
  651. desktop="$1"
  652. check="`desktop_file_to_binary "$1"`"
  653. if [ -z "$check" ]; then
  654. echo no
  655. exit_success
  656. fi
  657. # Check HTTP and HTTPS, but not about: and unknown:.
  658. for protocol in http https; do
  659. browser="`get_browser_mime "x-scheme-handler/$protocol"`"
  660. if [ x"$browser" != x"$desktop" ]; then
  661. echo no
  662. exit_success
  663. fi
  664. done
  665. echo yes
  666. exit_success
  667. }
  668. set_browser_gnome3()
  669. {
  670. binary="`desktop_file_to_binary "$1"`"
  671. [ "$binary" ] || exit_failure_file_missing
  672. set_browser_mime "$1" || return
  673. # Set the default browser.
  674. for protocol in http https about unknown; do
  675. set_browser_mime "$1" "x-scheme-handler/$protocol" || return
  676. done
  677. }
  678. # }}} GNOME 3.x
  679. # {{{ xfce
  680. get_browser_xfce()
  681. {
  682. search="${XDG_CONFIG_HOME:-$HOME/.config}:${XDG_CONFIG_DIRS:-/etc/xdg}"
  683. IFS=:
  684. for dir in $search; do
  685. unset IFS
  686. [ "$dir" -a -d "$dir/xfce4" ] || continue
  687. file="$dir/xfce4/helpers.rc"
  688. [ -r "$file" ] || continue
  689. grep -q "^WebBrowser=" "$file" || continue
  690. desktop="`grep "^WebBrowser=" "$file" | cut -d= -f 2-`"
  691. echo "$desktop.desktop"
  692. return
  693. done
  694. exit_failure_operation_failed
  695. }
  696. check_browser_xfce()
  697. {
  698. browser="`get_browser_xfce`"
  699. if [ x"$browser" != x"$1" ]; then
  700. echo no
  701. exit_success
  702. fi
  703. echo yes
  704. exit_success
  705. }
  706. check_xfce_desktop_file()
  707. {
  708. # Annoyingly, xfce wants its .desktop files in a separate directory instead
  709. # of the standard locations, and requires a few custom tweaks to them:
  710. # "Type" must be "X-XFCE-Helper"
  711. # "X-XFCE-Category" must be "WebBrowser" (for web browsers, anyway)
  712. # "X-XFCE-Commands" and "X-XFCE-CommandsWithParameter" must be set
  713. search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
  714. IFS=:
  715. for dir in $search; do
  716. unset IFS
  717. [ "$dir" -a -d "$dir/xfce4/helpers" ] || continue
  718. file="$dir/xfce4/helpers/$1"
  719. # We have the file, no need to create it.
  720. [ -r "$file" ] && return
  721. done
  722. IFS=:
  723. for dir in $search; do
  724. unset IFS
  725. [ "$dir" -a -d "$dir/applications" ] || continue
  726. file="$dir/applications/$1"
  727. if [ -r "$file" ]; then
  728. # Found a file to convert.
  729. target="${XDG_DATA_HOME:-$HOME/.local/share}/xfce4/helpers"
  730. mkdir -p "$target"
  731. grep -v "^Type=" "$file" > "$target/$1"
  732. echo "Type=X-XFCE-Helper" >> "$target/$1"
  733. echo "X-XFCE-Category=WebBrowser" >> "$target/$1"
  734. # Change %F, %f, %U, and %u to "%s".
  735. command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | sed -e 's/%[FfUu]/"%s"/g'`"
  736. echo "X-XFCE-Commands=`echo "$command" | first_word`" >> "$target/$1"
  737. echo "X-XFCE-CommandsWithParameter=$command" >> "$target/$1"
  738. return
  739. fi
  740. done
  741. return 1
  742. }
  743. set_browser_xfce()
  744. {
  745. check_xfce_desktop_file "$1" || exit_failure_operation_failed
  746. helper_dir="${XDG_CONFIG_HOME:-$HOME/.config}/xfce4"
  747. if [ ! -d "$helper_dir" ]; then
  748. mkdir -p "$helper_dir" || exit_failure_operation_failed
  749. fi
  750. helpers_rc="$helper_dir/helpers.rc"
  751. # Create the file if it does not exist to avoid special cases below.
  752. if [ ! -r "$helpers_rc" ]; then
  753. touch "$helpers_rc" || exit_failure_operation_failed
  754. fi
  755. temp="`mktemp "$helpers_rc.XXXXXX"`" || return
  756. grep -v "^WebBrowser=" "$helpers_rc" >> "$temp"
  757. echo "WebBrowser=${1%.desktop}" >> "$temp"
  758. oldlines="`wc -l < "$helpers_rc"`"
  759. newlines="`wc -l < "$temp"`"
  760. # The new file should have at least as many lines as the old.
  761. if [ $oldlines -le $newlines ]; then
  762. mv "$temp" "$helpers_rc"
  763. else
  764. rm -f "$temp"
  765. return 1
  766. fi
  767. }
  768. # }}} xfce
  769. # }}} default browser
  770. # {{{ default url scheme handler
  771. exit_unimplemented_default_handler()
  772. {
  773. exit_failure_operation_impossible "default-url-scheme-handler not implemented for $DE"
  774. }
  775. # {{{ KDE
  776. # Recent versions of KDE support default scheme handler applications using the
  777. # mime type of x-scheme-handler/scheme. Older versions will not support this
  778. # but do have support for setting a default mail handler. There is also a
  779. # system in KDE where .protocol files can be used, however this is not
  780. # supported by this script. When reading a scheme handler we will use the
  781. # default mail handler for the mailto scheme, otherwise we will use the mime
  782. # type x-scheme-handler/scheme.
  783. get_url_scheme_handler_kde()
  784. {
  785. if [ "$1" = "mailto" ]; then
  786. handler="`read_kde_config emaildefaults PROFILE_Default EmailClient | first_word`"
  787. echo "handler is $handler"
  788. if [ x"$handler" != x ]; then
  789. binary_to_desktop_file "$handler"
  790. else
  791. get_browser_mime "x-scheme-handler/$1"
  792. fi
  793. else
  794. get_browser_mime "x-scheme-handler/$1"
  795. fi
  796. }
  797. check_url_scheme_handler_kde()
  798. {
  799. check="`desktop_file_to_binary "$2"`"
  800. if [ -z "$check" ]; then
  801. echo no
  802. exit_success
  803. fi
  804. if [ x"$1" = "mailto" ]; then
  805. binary="`read_kde_config emaildefaults PROFILE_Default EmailClient`"
  806. if [ x"$binary" != x"$check" ]; then
  807. echo no
  808. exit_success
  809. fi
  810. fi
  811. handler="`get_browser_mime x-scheme-handler/$1`"
  812. binary="`desktop_file_to_binary "$handler"`"
  813. if [ x"$binary" != x"$check" ]; then
  814. echo no
  815. exit_success
  816. fi
  817. echo yes
  818. exit_success
  819. }
  820. set_url_scheme_handler_kde()
  821. {
  822. set_browser_mime "$2" "x-scheme-handler/$1" || return
  823. if [ "$1" = "mailto" ]; then
  824. binary="`desktop_file_to_binary "$2"`"
  825. kwriteconfig --file emaildefaults --group PROFILE_Default --key EmailClient "$binary"
  826. fi
  827. }
  828. # }}} KDE
  829. # {{{ GNOME
  830. get_url_scheme_handler_gnome()
  831. {
  832. binary="`gconftool-2 --get /desktop/gnome/url-handlers/$1/command | first_word`"
  833. if [ x"$binary" != x"" ]; then
  834. # gconftool gives the binary (maybe with %s etc. afterward),
  835. # but we want the desktop file name, not the binary. So, we
  836. # have to find the desktop file to which it corresponds.
  837. desktop="`binary_to_desktop_file "$binary"`"
  838. basename "$desktop"
  839. fi
  840. }
  841. check_url_scheme_handler_gnome()
  842. {
  843. check="`desktop_file_to_binary "$2"`"
  844. if [ -z "$check" ]; then
  845. echo no
  846. exit_success
  847. fi
  848. binary="`gconftool-2 --get /desktop/gnome/url-handlers/$1/command | first_word`"
  849. if [ x"$binary" != x"$check" ]; then
  850. echo no
  851. exit_success
  852. fi
  853. echo yes
  854. exit_success
  855. }
  856. set_url_scheme_handler_gnome()
  857. {
  858. binary="`desktop_file_to_binary "$2"`"
  859. [ "$binary" ] || exit_failure_file_missing
  860. gconftool-2 --type string --set /desktop/gnome/url-handlers/$1/command "$binary %s"
  861. gconftool-2 --type bool --set /desktop/gnome/url-handlers/$1/needs_terminal false
  862. gconftool-2 --type bool --set /desktop/gnome/url-handlers/$1/enabled true
  863. }
  864. # }}} GNOME
  865. # {{{ GNOME 3.x
  866. get_url_scheme_handler_gnome3()
  867. {
  868. get_browser_mime "x-scheme-handler/$1"
  869. }
  870. check_url_scheme_handler_gnome3()
  871. {
  872. desktop="$2"
  873. check="`desktop_file_to_binary "$2"`"
  874. if [ -z "$check" ]; then
  875. echo no
  876. exit_success
  877. fi
  878. browser="`get_browser_mime "x-scheme-handler/$1"`"
  879. if [ x"$browser" != x"$desktop" ]; then
  880. echo no
  881. exit_success
  882. fi
  883. echo yes
  884. exit_success
  885. }
  886. set_url_scheme_handler_gnome3()
  887. {
  888. binary="`desktop_file_to_binary "$2"`"
  889. [ "$binary" ] || exit_failure_file_missing
  890. set_browser_mime "$2" || return
  891. # Set the default browser.
  892. set_browser_mime "$2" "x-scheme-handler/$1" || return
  893. }
  894. # }}} GNOME 3.x
  895. # {{{ xfce
  896. get_url_scheme_handler_xfce()
  897. {
  898. exit_unimplemented_default_handler "$1"
  899. }
  900. check_url_scheme_handler_xfce()
  901. {
  902. exit_unimplemented_default_handler "$1"
  903. }
  904. set_url_scheme_handler_xfce()
  905. {
  906. exit_unimplemented_default_handler "$1"
  907. }
  908. # }}} xfce
  909. # }}} default protocol handler
  910. dispatch_specific()
  911. {
  912. # The PROP comments in this function are used to generate the output of
  913. # the --list option. The formatting is important. Make sure to line up the
  914. # property descriptions with spaces so that it will look nice.
  915. if [ x"$op" = x"get" ]; then
  916. case "$parm" in
  917. default-web-browser) # PROP: Default web browser
  918. get_browser_$DE
  919. ;;
  920. default-url-scheme-handler) # PROP: Default handler for URL scheme
  921. get_url_scheme_handler_$DE "$1"
  922. ;;
  923. *)
  924. exit_failure_syntax
  925. ;;
  926. esac
  927. elif [ x"$op" = x"check" ]; then
  928. case "$parm" in
  929. default-web-browser)
  930. check_desktop_filename "$1"
  931. check_browser_$DE "$1"
  932. ;;
  933. default-url-scheme-handler)
  934. check_desktop_filename "$2"
  935. check_url_scheme_handler_$DE "$1" "$2"
  936. ;;
  937. *)
  938. exit_failure_syntax
  939. ;;
  940. esac
  941. else # set
  942. case "$parm" in
  943. default-web-browser)
  944. [ $# -eq 1 ] || exit_failure_syntax "unexpected/missing argument"
  945. check_desktop_filename "$1"
  946. set_browser_$DE "$1"
  947. ;;
  948. default-url-scheme-handler)
  949. [ $# -eq 2 ] || exit_failure_syntax "unexpected/missing argument"
  950. check_desktop_filename "$2"
  951. set_url_scheme_handler_$DE "$1" "$2"
  952. ;;
  953. *)
  954. exit_failure_syntax
  955. ;;
  956. esac
  957. fi
  958. if [ $? -eq 0 ]; then
  959. exit_success
  960. else
  961. exit_failure_operation_failed
  962. fi
  963. }
  964. dispatch_generic()
  965. {
  966. # We only know how to get or check the default web browser.
  967. [ x"$op" != x"get" -a x"$op" != x"check" ] && exit_failure_operation_impossible
  968. [ x"$parm" != x"default-web-browser" ] && exit_failure_operation_impossible
  969. # First look in $BROWSER
  970. if [ x"$BROWSER" != x ]; then
  971. binary="`which "${BROWSER%%:*}"`"
  972. else
  973. # Debian and Ubuntu (and others?) have x-www-browser.
  974. binary="`which x-www-browser`"
  975. fi
  976. [ "$binary" ] || exit_failure_operation_failed
  977. binary="`readlink -f "$binary"`"
  978. [ "$binary" ] || exit_failure_operation_failed
  979. if [ x"$op" = x"get" ]; then
  980. desktop="`binary_to_desktop_file "$binary"`"
  981. basename "$desktop"
  982. else
  983. # $op = "check"
  984. check="`desktop_file_to_binary "$1"`"
  985. if [ -z "$check" ]; then
  986. echo no
  987. exit_success
  988. fi
  989. if [ x"$binary" != x"$check" ]; then
  990. echo no
  991. exit_success
  992. fi
  993. echo yes
  994. fi
  995. exit_success
  996. }
  997. if [ x"$1" = x"--list" ]; then
  998. echo "Known properties:"
  999. # Extract the property names from dispatch_specific() above.
  1000. grep "^[ ]*[^)]*) # PROP:" "$0" | sed -e 's/^[ ]*\([^)]*\)) # PROP: \(.*\)$/ \1 \2/' | sort
  1001. exit_success
  1002. fi
  1003. [ x"$1" != x ] || exit_failure_syntax "no operation given"
  1004. [ x"$2" != x ] || exit_failure_syntax "no parameter name given"
  1005. [ x"$1" = x"get" -o x"$3" != x ] || exit_failure_syntax "no parameter value given"
  1006. op="$1"
  1007. parm="$2"
  1008. shift 2
  1009. if [ x"$op" != x"get" -a x"$op" != x"check" -a x"$op" != x"set" ]; then
  1010. exit_failure_syntax "invalid operation"
  1011. fi
  1012. detectDE
  1013. case "$DE" in
  1014. kde|gnome*|xfce)
  1015. dispatch_specific "$@"
  1016. ;;
  1017. generic)
  1018. dispatch_generic "$@"
  1019. ;;
  1020. *)
  1021. exit_failure_operation_impossible "unknown desktop environment"
  1022. ;;
  1023. esac