xdg-mime 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. #!/bin/sh
  2. #---------------------------------------------
  3. # xdg-mime
  4. #
  5. # Utility script to manipulate MIME related information
  6. # on XDG compliant systems.
  7. #
  8. # Refer to the usage() function below for usage.
  9. #
  10. # Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
  11. # Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
  12. # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
  13. # Copyright 2006, Jeremy White <jwhite@codeweavers.com>
  14. #
  15. # LICENSE:
  16. #
  17. # Permission is hereby granted, free of charge, to any person obtaining a
  18. # copy of this software and associated documentation files (the "Software"),
  19. # to deal in the Software without restriction, including without limitation
  20. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  21. # and/or sell copies of the Software, and to permit persons to whom the
  22. # Software is furnished to do so, subject to the following conditions:
  23. #
  24. # The above copyright notice and this permission notice shall be included
  25. # in all copies or substantial portions of the Software.
  26. #
  27. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  28. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  31. # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  32. # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  33. # OTHER DEALINGS IN THE SOFTWARE.
  34. #
  35. #---------------------------------------------
  36. manualpage()
  37. {
  38. cat << _MANUALPAGE
  39. Name
  40. xdg-mime -- command line tool for querying information about
  41. file type handling and adding descriptions for new file types
  42. Synopsis
  43. xdg-mime query { filetype | default } ...
  44. xdg-mime default application mimetype(s)
  45. xdg-mime install [--mode mode] [--novendor] mimetypes-file
  46. xdg-mime uninstall [--mode mode] mimetypes-file
  47. xdg-mime { --help | --manual | --version }
  48. Description
  49. The xdg-mime program can be used to query information about
  50. file types and to add descriptions for new file types.
  51. Commands
  52. query
  53. Returns information related to file types.
  54. The query option is for use inside a desktop session
  55. only. It is not recommended to use xdg-mime query as
  56. root.
  57. The following queries are supported:
  58. query filetype FILE: Returns the file type of FILE in
  59. the form of a MIME type.
  60. query default mimetype: Returns the default application
  61. that the desktop environment uses for opening files of
  62. type mimetype. The default application is identified by
  63. its *.desktop file.
  64. default
  65. Ask the desktop environment to make application the
  66. default application for opening files of type mimetype.
  67. An application can be made the default for several file
  68. types by specifying multiple mimetypes.
  69. application is the desktop file id of the application
  70. and has the form vendor-name.desktop. application must
  71. already be installed in the desktop menu before it can
  72. be made the default handler. The application's desktop
  73. file must list support for all the MIME types that it
  74. wishes to be the default handler for.
  75. Requests to make an application a default handler may be
  76. subject to system policy or approval by the end-user.
  77. xdg-mime query can be used to verify whether an
  78. application is the actual default handler for a specific
  79. file type.
  80. The default option is for use inside a desktop session
  81. only. It is not recommended to use xdg-mime default as
  82. root.
  83. install
  84. Adds the file type descriptions provided in
  85. mimetypes-file to the desktop environment.
  86. mimetypes-file must be a XML file that follows the
  87. freedesktop.org Shared MIME-info Database specification
  88. and that has a mime-info element as its document root.
  89. For each new file type one or more icons with name
  90. type-subtype must be installed with the
  91. xdg-icon-resource command in the mimetypes context. For
  92. example the filetype
  93. application/vnd.oasis.opendocument.text requires an icon
  94. named application-vnd.oasis.opendocument.text to be
  95. installed (unless the file type recommends another icon
  96. name).
  97. uninstall
  98. Removes the file type descriptions provided in
  99. mimetypes-file and previously added with xdg-mime
  100. install from the desktop environment. mimetypes-file
  101. must be a XML file that follows the freedesktop.org
  102. Shared MIME-info Database specification and that has a
  103. mime-info element as its document root.
  104. Options
  105. --mode mode
  106. mode can be user or system. In user mode the file is
  107. (un)installed for the current user only. In system mode
  108. the file is (un)installed for all users on the system.
  109. Usually only root is allowed to install in system mode.
  110. The default is to use system mode when called by root
  111. and to use user mode when called by a non-root user.
  112. --novendor
  113. Normally, xdg-mime checks to ensure that the
  114. mimetypes-file to be installed has a proper vendor
  115. prefix. This option can be used to disable that check.
  116. A vendor prefix consists of alpha characters ([a-zA-Z])
  117. and is terminated with a dash ("-"). Companies and
  118. organizations are encouraged to use a word or phrase,
  119. preferably the organizations name, for which they hold a
  120. trademark as their vendor prefix. The purpose of the
  121. vendor prefix is to prevent name conflicts.
  122. --help
  123. Show command synopsis.
  124. --manual
  125. Show this manual page.
  126. --version
  127. Show the xdg-utils version information.
  128. Environment Variables
  129. xdg-mime honours the following environment variables:
  130. XDG_UTILS_DEBUG_LEVEL
  131. Setting this environment variable to a non-zero
  132. numerical value makes xdg-mime do more verbose reporting
  133. on stderr. Setting a higher value increases the
  134. verbosity.
  135. XDG_UTILS_INSTALL_MODE
  136. This environment variable can be used by the user or
  137. administrator to override the installation mode. Valid
  138. values are user and system.
  139. Exit Codes
  140. An exit code of 0 indicates success while a non-zero exit code
  141. indicates failure. The following failure codes can be returned:
  142. 1
  143. Error in command line syntax.
  144. 2
  145. One of the files passed on the command line did not
  146. exist.
  147. 3
  148. A required tool could not be found.
  149. 4
  150. The action failed.
  151. 5
  152. No permission to read one of the files passed on the
  153. command line.
  154. See Also
  155. xdg-icon-resource(1), xdg-desktop-menu(1), Shared MIME database
  156. specification, MIME applications associations specification
  157. Examples
  158. xdg-mime query filetype /tmp/foobar.png
  159. Prints the MIME type of the file /tmp/foobar.png, in this case
  160. image/png
  161. xdg-mime query default image/png
  162. Prints the .desktop filename of the application which is
  163. registered to open PNG files.
  164. xdg-mime install shinythings-shiny.xml
  165. Adds a file type description for "shiny"-files. "shinythings-"
  166. is used as the vendor prefix. The file type description could
  167. look as follows.
  168. shinythings-shiny.xml:
  169. <?xml version="1.0"?>
  170. <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'
  171. >
  172. <mime-type type="text/x-shiny">
  173. <comment>Shiny new file type</comment>
  174. <glob pattern="*.shiny"/>
  175. <glob pattern="*.shi"/>
  176. </mime-type>
  177. </mime-info>
  178. An icon for this new file type must also be installed, for
  179. example with:
  180. xdg-icon-resource install --context mimetypes --size 64 shiny-file-icon.
  181. png text-x-shiny
  182. _MANUALPAGE
  183. }
  184. usage()
  185. {
  186. cat << _USAGE
  187. xdg-mime -- command line tool for querying information about
  188. file type handling and adding descriptions for new file types
  189. Synopsis
  190. xdg-mime query { filetype | default } ...
  191. xdg-mime default application mimetype(s)
  192. xdg-mime install [--mode mode] [--novendor] mimetypes-file
  193. xdg-mime uninstall [--mode mode] mimetypes-file
  194. xdg-mime { --help | --manual | --version }
  195. _USAGE
  196. }
  197. #@xdg-utils-common@
  198. #----------------------------------------------------------------------------
  199. # Common utility functions included in all XDG wrapper scripts
  200. #----------------------------------------------------------------------------
  201. DEBUG()
  202. {
  203. [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
  204. [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
  205. shift
  206. echo "$@" >&2
  207. }
  208. # This handles backslashes but not quote marks.
  209. first_word()
  210. {
  211. read first rest
  212. echo "$first"
  213. }
  214. #-------------------------------------------------------------
  215. # map a binary to a .desktop file
  216. binary_to_desktop_file()
  217. {
  218. search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
  219. binary="`which "$1"`"
  220. binary="`readlink -f "$binary"`"
  221. base="`basename "$binary"`"
  222. IFS=:
  223. for dir in $search; do
  224. unset IFS
  225. [ "$dir" ] || continue
  226. [ -d "$dir/applications" ] || [ -d "$dir/applnk" ] || continue
  227. for file in "$dir"/applications/*.desktop "$dir"/applications/*/*.desktop "$dir"/applnk/*.desktop "$dir"/applnk/*/*.desktop; do
  228. [ -r "$file" ] || continue
  229. # Check to make sure it's worth the processing.
  230. grep -q "^Exec.*$base" "$file" || continue
  231. # Make sure it's a visible desktop file (e.g. not "preferred-web-browser.desktop").
  232. grep -Eq "^(NoDisplay|Hidden)=true" "$file" && continue
  233. command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
  234. command="`which "$command"`"
  235. if [ x"`readlink -f "$command"`" = x"$binary" ]; then
  236. # Fix any double slashes that got added path composition
  237. echo "$file" | sed -e 's,//*,/,g'
  238. return
  239. fi
  240. done
  241. done
  242. }
  243. #-------------------------------------------------------------
  244. # map a .desktop file to a binary
  245. desktop_file_to_binary()
  246. {
  247. search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
  248. desktop="`basename "$1"`"
  249. IFS=:
  250. for dir in $search; do
  251. unset IFS
  252. [ "$dir" ] && [ -d "$dir/applications" ] || [ -d "$dir/applnk" ] || continue
  253. # Check if desktop file contains -
  254. if [ "${desktop#*-}" != "$desktop" ]; then
  255. vendor=${desktop%-*}
  256. app=${desktop#*-}
  257. if [ -r $dir/applications/$vendor/$app ]; then
  258. file_path=$dir/applications/$vendor/$app
  259. elif [ -r $dir/applnk/$vendor/$app ]; then
  260. file_path=$dir/applnk/$vendor/$app
  261. fi
  262. fi
  263. if test -z "$file_path" ; then
  264. for indir in "$dir"/applications/ "$dir"/applications/*/ "$dir"/applnk/ "$dir"/applnk/*/; do
  265. file="$indir/$desktop"
  266. if [ -r "$file" ]; then
  267. file_path=$file
  268. break
  269. fi
  270. done
  271. fi
  272. if [ -r "$file_path" ]; then
  273. # Remove any arguments (%F, %f, %U, %u, etc.).
  274. command="`grep -E "^Exec(\[[^]=]*])?=" "$file_path" | cut -d= -f 2- | first_word`"
  275. command="`which "$command"`"
  276. readlink -f "$command"
  277. return
  278. fi
  279. done
  280. }
  281. #-------------------------------------------------------------
  282. # Exit script on successfully completing the desired operation
  283. exit_success()
  284. {
  285. if [ $# -gt 0 ]; then
  286. echo "$@"
  287. echo
  288. fi
  289. exit 0
  290. }
  291. #-----------------------------------------
  292. # Exit script on malformed arguments, not enough arguments
  293. # or missing required option.
  294. # prints usage information
  295. exit_failure_syntax()
  296. {
  297. if [ $# -gt 0 ]; then
  298. echo "xdg-mime: $@" >&2
  299. echo "Try 'xdg-mime --help' for more information." >&2
  300. else
  301. usage
  302. echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info."
  303. fi
  304. exit 1
  305. }
  306. #-------------------------------------------------------------
  307. # Exit script on missing file specified on command line
  308. exit_failure_file_missing()
  309. {
  310. if [ $# -gt 0 ]; then
  311. echo "xdg-mime: $@" >&2
  312. fi
  313. exit 2
  314. }
  315. #-------------------------------------------------------------
  316. # Exit script on failure to locate necessary tool applications
  317. exit_failure_operation_impossible()
  318. {
  319. if [ $# -gt 0 ]; then
  320. echo "xdg-mime: $@" >&2
  321. fi
  322. exit 3
  323. }
  324. #-------------------------------------------------------------
  325. # Exit script on failure returned by a tool application
  326. exit_failure_operation_failed()
  327. {
  328. if [ $# -gt 0 ]; then
  329. echo "xdg-mime: $@" >&2
  330. fi
  331. exit 4
  332. }
  333. #------------------------------------------------------------
  334. # Exit script on insufficient permission to read a specified file
  335. exit_failure_file_permission_read()
  336. {
  337. if [ $# -gt 0 ]; then
  338. echo "xdg-mime: $@" >&2
  339. fi
  340. exit 5
  341. }
  342. #------------------------------------------------------------
  343. # Exit script on insufficient permission to write a specified file
  344. exit_failure_file_permission_write()
  345. {
  346. if [ $# -gt 0 ]; then
  347. echo "xdg-mime: $@" >&2
  348. fi
  349. exit 6
  350. }
  351. check_input_file()
  352. {
  353. if [ ! -e "$1" ]; then
  354. exit_failure_file_missing "file '$1' does not exist"
  355. fi
  356. if [ ! -r "$1" ]; then
  357. exit_failure_file_permission_read "no permission to read file '$1'"
  358. fi
  359. }
  360. check_vendor_prefix()
  361. {
  362. file_label="$2"
  363. [ -n "$file_label" ] || file_label="filename"
  364. file=`basename "$1"`
  365. case "$file" in
  366. [[:alpha:]]*-*)
  367. return
  368. ;;
  369. esac
  370. echo "xdg-mime: $file_label '$file' does not have a proper vendor prefix" >&2
  371. echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
  372. echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
  373. echo "Use --novendor to override or 'xdg-mime --manual' for additional info." >&2
  374. exit 1
  375. }
  376. check_output_file()
  377. {
  378. # if the file exists, check if it is writeable
  379. # if it does not exists, check if we are allowed to write on the directory
  380. if [ -e "$1" ]; then
  381. if [ ! -w "$1" ]; then
  382. exit_failure_file_permission_write "no permission to write to file '$1'"
  383. fi
  384. else
  385. DIR=`dirname "$1"`
  386. if [ ! -w "$DIR" ] || [ ! -x "$DIR" ]; then
  387. exit_failure_file_permission_write "no permission to create file '$1'"
  388. fi
  389. fi
  390. }
  391. #----------------------------------------
  392. # Checks for shared commands, e.g. --help
  393. check_common_commands()
  394. {
  395. while [ $# -gt 0 ] ; do
  396. parm="$1"
  397. shift
  398. case "$parm" in
  399. --help)
  400. usage
  401. echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info."
  402. exit_success
  403. ;;
  404. --manual)
  405. manualpage
  406. exit_success
  407. ;;
  408. --version)
  409. echo "xdg-mime 1.1.3"
  410. exit_success
  411. ;;
  412. esac
  413. done
  414. }
  415. check_common_commands "$@"
  416. [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
  417. if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
  418. # Be silent
  419. xdg_redirect_output=" > /dev/null 2> /dev/null"
  420. else
  421. # All output to stderr
  422. xdg_redirect_output=" >&2"
  423. fi
  424. #--------------------------------------
  425. # Checks for known desktop environments
  426. # set variable DE to the desktop environments name, lowercase
  427. detectDE()
  428. {
  429. # see https://bugs.freedesktop.org/show_bug.cgi?id=34164
  430. unset GREP_OPTIONS
  431. if [ -n "${XDG_CURRENT_DESKTOP}" ]; then
  432. case "${XDG_CURRENT_DESKTOP}" in
  433. # only recently added to menu-spec, pre-spec X- still in use
  434. Cinnamon|X-Cinnamon)
  435. DE=cinnamon;
  436. ;;
  437. ENLIGHTENMENT)
  438. DE=enlightenment;
  439. ;;
  440. # GNOME, GNOME-Classic:GNOME, or GNOME-Flashback:GNOME
  441. GNOME*)
  442. DE=gnome;
  443. ;;
  444. KDE)
  445. DE=kde;
  446. ;;
  447. # Deepin Desktop Environments
  448. DEEPIN|Deepin|deepin)
  449. DE=dde;
  450. ;;
  451. LXDE)
  452. DE=lxde;
  453. ;;
  454. LXQt)
  455. DE=lxqt;
  456. ;;
  457. MATE)
  458. DE=mate;
  459. ;;
  460. XFCE)
  461. DE=xfce
  462. ;;
  463. X-Generic)
  464. DE=generic
  465. ;;
  466. esac
  467. fi
  468. if [ x"$DE" = x"" ]; then
  469. # classic fallbacks
  470. if [ x"$KDE_FULL_SESSION" != x"" ]; then DE=kde;
  471. elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
  472. elif [ x"$MATE_DESKTOP_SESSION_ID" != x"" ]; then DE=mate;
  473. 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;
  474. elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
  475. elif xprop -root 2> /dev/null | grep -i '^xfce_desktop_window' >/dev/null 2>&1; then DE=xfce
  476. elif echo $DESKTOP | grep -q '^Enlightenment'; then DE=enlightenment;
  477. elif [ x"$LXQT_SESSION_CONFIG" != x"" ]; then DE=lxqt;
  478. fi
  479. fi
  480. if [ x"$DE" = x"" ]; then
  481. # fallback to checking $DESKTOP_SESSION
  482. case "$DESKTOP_SESSION" in
  483. gnome)
  484. DE=gnome;
  485. ;;
  486. LXDE|Lubuntu)
  487. DE=lxde;
  488. ;;
  489. MATE)
  490. DE=mate;
  491. ;;
  492. xfce|xfce4|'Xfce Session')
  493. DE=xfce;
  494. ;;
  495. esac
  496. fi
  497. if [ x"$DE" = x"" ]; then
  498. # fallback to uname output for other platforms
  499. case "$(uname 2>/dev/null)" in
  500. CYGWIN*)
  501. DE=cygwin;
  502. ;;
  503. Darwin)
  504. DE=darwin;
  505. ;;
  506. esac
  507. fi
  508. if [ x"$DE" = x"gnome" ]; then
  509. # gnome-default-applications-properties is only available in GNOME 2.x
  510. # but not in GNOME 3.x
  511. which gnome-default-applications-properties > /dev/null 2>&1 || DE="gnome3"
  512. fi
  513. if [ -f "$XDG_RUNTIME_DIR/flatpak-info" ]; then
  514. DE="flatpak"
  515. fi
  516. }
  517. #----------------------------------------------------------------------------
  518. # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
  519. # It also always returns 1 in KDE 3.4 and earlier
  520. # Simply return 0 in such case
  521. kfmclient_fix_exit_code()
  522. {
  523. version=`LC_ALL=C.UTF-8 kde-config --version 2>/dev/null | grep '^KDE'`
  524. major=`echo $version | sed 's/KDE.*: \([0-9]\).*/\1/'`
  525. minor=`echo $version | sed 's/KDE.*: [0-9]*\.\([0-9]\).*/\1/'`
  526. release=`echo $version | sed 's/KDE.*: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
  527. test "$major" -gt 3 && return $1
  528. test "$minor" -gt 5 && return $1
  529. test "$release" -gt 4 && return $1
  530. return 0
  531. }
  532. #----------------------------------------------------------------------------
  533. # Returns true if there is a graphical display attached.
  534. has_display()
  535. {
  536. if [ -n "$DISPLAY" ] || [ -n "$WAYLAND_DISPLAY" ]; then
  537. return 0
  538. else
  539. return 1
  540. fi
  541. }
  542. update_mime_database()
  543. {
  544. if [ "$mode" = user ] && has_display; then
  545. detectDE
  546. if [ x"$DE" = x"kde" ] ; then
  547. DEBUG 1 "Running kbuildsycoca"
  548. eval 'kbuildsycoca${KDE_SESSION_VERSION}'$xdg_redirect_output
  549. fi
  550. fi
  551. for x in `echo "$PATH:/opt/gnome/bin" | sed 's/:/ /g'`; do
  552. if [ -x $x/update-mime-database ] ; then
  553. DEBUG 1 "Running $x/update-mime-database $1"
  554. eval '$x/update-mime-database $1'$xdg_redirect_output
  555. return
  556. fi
  557. done
  558. }
  559. info_kde()
  560. {
  561. if [ -n "${KDE_SESSION_VERSION}" ]; then
  562. case "${KDE_SESSION_VERSION}" in
  563. 4)
  564. DEBUG 1 "Running kmimetypefinder \"$1\""
  565. kmimetypefinder "$1" 2>/dev/null | head -n 1
  566. ;;
  567. 5)
  568. DEBUG 1 "Running kmimetypefinder${KDE_SESSION_VERSION} \"$1\""
  569. kmimetypefinder${KDE_SESSION_VERSION} "$1" 2>/dev/null | head -n 1
  570. ;;
  571. esac
  572. else
  573. DEBUG 1 "Running kfile \"$1\""
  574. kfile "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1
  575. fi
  576. if [ $? -eq 0 ]; then
  577. exit_success
  578. else
  579. exit_failure_operation_failed
  580. fi
  581. }
  582. info_gnome()
  583. {
  584. if gio help info 2>/dev/null 1>&2; then
  585. DEBUG 1 "Running gio info \"$1\""
  586. gio info "$1" 2> /dev/null | grep standard::content-type | cut -d' ' -f4
  587. elif gvfs-info --help 2>/dev/null 1>&2; then
  588. DEBUG 1 "Running gvfs-info \"$1\""
  589. gvfs-info "$1" 2> /dev/null | grep standard::content-type | cut -d' ' -f4
  590. elif gnomevfs-info --help 2>/dev/null 1>&2; then
  591. DEBUG 1 "Running gnomevfs-info \"$1\""
  592. gnomevfs-info --slow-mime "$1" 2> /dev/null | grep "^MIME" | cut -d ":" -f 2 | sed s/"^ "//
  593. fi
  594. if [ $? -eq 0 ]; then
  595. exit_success
  596. else
  597. exit_failure_operation_failed
  598. fi
  599. }
  600. info_generic()
  601. {
  602. if mimetype --version >/dev/null 2>&1; then
  603. DEBUG 1 "Running mimetype --brief --dereference \"$1\""
  604. mimetype --brief --dereference "$1"
  605. else
  606. DEBUG 1 "Running file --brief --dereference --mime-type \"$1\""
  607. /usr/bin/file --brief --dereference --mime-type "$1" 2> /dev/null
  608. fi
  609. if [ $? -eq 0 ]; then
  610. exit_success
  611. else
  612. exit_failure_operation_failed
  613. fi
  614. }
  615. make_default_kde()
  616. {
  617. # $1 is vendor-name.desktop
  618. # $2 is mime/type
  619. #
  620. # On KDE 3, add to $KDE_CONFIG_PATH/profilerc:
  621. # [$2 - 1]
  622. # Application=$1
  623. #
  624. # Remove all [$2 - *] sections, or even better,
  625. # renumber [$2 - *] sections and remove duplicate
  626. #
  627. # On KDE 4, add $2=$1 to $XDG_DATA_APPS/mimeapps.list
  628. #
  629. # Example file:
  630. #
  631. # [Added Associations]
  632. # text/plain=kde4-kate.desktop;kde4-kwrite.desktop;
  633. #
  634. # [Removed Associations]
  635. # text/plain=gnome-gedit.desktop;gnu-emacs.desktop;
  636. vendor="$1"
  637. mimetype="$2"
  638. if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
  639. default_dir=`kde4-config --path xdgdata-apps 2> /dev/null | cut -d ':' -f 1`
  640. default_file="$default_dir/mimeapps.list"
  641. else
  642. default_dir=`kde-config --path config 2> /dev/null | cut -d ':' -f 1`
  643. default_file="$default_dir/profilerc"
  644. fi
  645. if [ -z "$default_dir" ]; then
  646. DEBUG 2 "make_default_kde: No kde runtime detected"
  647. return
  648. fi
  649. DEBUG 2 "make_default_kde $vendor $mimetype"
  650. DEBUG 1 "Updating $default_file"
  651. mkdir -p "$default_dir"
  652. [ -f "$default_file" ] || touch "$default_file"
  653. if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
  654. [ -f "$default_file" ] || touch "$default_file"
  655. awk -v application="$vendor" -v mimetype="$mimetype" '
  656. BEGIN {
  657. prefix=mimetype "="
  658. associations=0
  659. found=0
  660. blanks=0
  661. }
  662. {
  663. suppress=0
  664. if (index($0, "[Added Associations]") == 1) {
  665. associations=1
  666. } else if (index($0, "[") == 1) {
  667. if (associations && !found) {
  668. print prefix application
  669. found=1
  670. }
  671. associations=0
  672. } else if ($0 == "") {
  673. blanks++
  674. suppress=1
  675. } else if (associations && index($0, prefix) == 1) {
  676. value=substr($0, length(prefix) + 1, length())
  677. split(value, apps, ";")
  678. value=application ";"
  679. count=0
  680. for (i in apps) {
  681. count++
  682. }
  683. for (i=0; i < count; i++) {
  684. if (apps[i] != application && apps[i] != "") {
  685. value=value apps[i] ";"
  686. }
  687. }
  688. $0=prefix value
  689. found=1
  690. }
  691. if (!suppress) {
  692. while (blanks > 0) {
  693. print ""
  694. blanks--
  695. }
  696. print $0
  697. }
  698. }
  699. END {
  700. if (!found) {
  701. if (!associations) {
  702. print "[Added Associations]"
  703. }
  704. print prefix application
  705. }
  706. while (blanks > 0) {
  707. print ""
  708. blanks--
  709. }
  710. }
  711. ' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_file"
  712. eval 'kbuildsycoca4'$xdg_redirect_output
  713. else
  714. awk -v application="$vendor" -v mimetype="$mimetype" '
  715. BEGIN {
  716. header_start="[" mimetype " - "
  717. suppress=0
  718. }
  719. {
  720. if (index($0, header_start) == 1 )
  721. suppress=1
  722. else
  723. if (/^\[/) { suppress=0 }
  724. if (!suppress) {
  725. print $0
  726. }
  727. }
  728. END {
  729. print ""
  730. print "[" mimetype " - 1]"
  731. print "Application=" application
  732. print "AllowAsDefault=true"
  733. print "GenericServiceType=Application"
  734. print "Preference=1"
  735. print "ServiceType=" mimetype
  736. }
  737. ' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_file"
  738. fi
  739. }
  740. make_default_generic()
  741. {
  742. # $1 is vendor-name.desktop
  743. # $2 is mime/type
  744. # Add $2=$1 to XDG_CONFIG_HOME/mimeapps.list
  745. xdg_config_home="$XDG_CONFIG_HOME"
  746. [ -n "$xdg_config_home" ] || xdg_config_home="$HOME/.config"
  747. default_file="$xdg_config_home/mimeapps.list"
  748. DEBUG 2 "make_default_generic $1 $2"
  749. DEBUG 1 "Updating $default_file"
  750. [ -f "$default_file" ] || touch "$default_file"
  751. awk -v mimetype="$2" -v application="$1" '
  752. BEGIN {
  753. prefix=mimetype "="
  754. indefault=0
  755. added=0
  756. blanks=0
  757. found=0
  758. }
  759. {
  760. suppress=0
  761. if (index($0, "[Default Applications]") == 1) {
  762. indefault=1
  763. found=1
  764. } else if (index($0, "[") == 1) {
  765. if (!added && indefault) {
  766. print prefix application
  767. added=1
  768. }
  769. indefault=0
  770. } else if ($0 == "") {
  771. suppress=1
  772. blanks++
  773. } else if (indefault && !added && index($0, prefix) == 1) {
  774. $0=prefix application
  775. added=1
  776. }
  777. if (!suppress) {
  778. while (blanks > 0) {
  779. print ""
  780. blanks--
  781. }
  782. print $0
  783. }
  784. }
  785. END {
  786. if (!added) {
  787. if (!found) {
  788. print ""
  789. print "[Default Applications]"
  790. }
  791. print prefix application
  792. }
  793. while (blanks > 0) {
  794. print ""
  795. blanks--
  796. }
  797. }
  798. ' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_file"
  799. }
  800. search_desktop_file()
  801. {
  802. local MIME="$1"
  803. local dir="$2"
  804. grep -l "$MIME;" "$dir/"*.desktop 2>/dev/null
  805. for f in $dir/*/; do
  806. [ -d "$f" ] && search_desktop_file "$MIME" "$f"
  807. done
  808. }
  809. defapp_fallback()
  810. {
  811. MIME="$1"
  812. xdg_user_dir="$XDG_DATA_HOME"
  813. [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
  814. xdg_system_dirs="$XDG_DATA_DIRS"
  815. [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
  816. preference=-1
  817. desktop_file=""
  818. for d in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
  819. for x in `search_desktop_file "$MIME" "$d/applications"`; do
  820. pref=0`awk -F"=" '/InitialPreference=/ {print($2)}' "$x"`
  821. DEBUG 2 " Checking $x"
  822. if [ $pref -gt $preference ]; then
  823. DEBUG 2 " Select $x [ $preference => $pref ]"
  824. preference=$pref
  825. desktop_file=$x
  826. fi
  827. done
  828. done
  829. if [ -n "$desktop_file" ] ; then
  830. echo `basename $desktop_file`
  831. exit_success
  832. fi
  833. }
  834. check_mimeapps_list()
  835. {
  836. local mimetype="$1" dir="$2"
  837. local desktop oldifs="$IFS"
  838. IFS=:
  839. for desktop in $XDG_CURRENT_DESKTOP ''; do
  840. IFS="$oldifs"
  841. if [ -n "$desktop" ]; then
  842. local prefix=$(echo "$desktop-" | tr '[:upper:]' '[:lower:]')
  843. else
  844. local prefix=
  845. fi
  846. local mimeapps_list="$dir/${prefix}mimeapps.list"
  847. if [ -f "$mimeapps_list" ] ; then
  848. DEBUG 2 "Checking $mimeapps_list"
  849. local result=$(awk -v mimetype="$mimetype" '
  850. BEGIN {
  851. prefix=mimetype "="
  852. indefault=0
  853. found=0
  854. }
  855. {
  856. if (index($0, "[Default Applications]") == 1) {
  857. indefault=1
  858. } else if (index($0, "[") == 1) {
  859. indefault=0
  860. } else if (!found && indefault && index($0, prefix) == 1) {
  861. print substr($0, length(prefix) +1, length())
  862. found=1
  863. }
  864. }
  865. ' "$mimeapps_list" | cut -d ';' -f 1)
  866. if [ -n "$result" ]; then
  867. # $result could be a ; separated list of .desktop files
  868. # use the first on the system
  869. IFS=\;
  870. for app in $result; do
  871. IFS="$oldifs"
  872. exists=$(desktop_file_to_binary "$app")
  873. if [ -n "$exists" ]; then
  874. echo "$app"
  875. exit_success
  876. fi
  877. done
  878. fi
  879. fi
  880. done
  881. }
  882. defapp_generic()
  883. {
  884. MIME="$1"
  885. xdg_config_home="$XDG_CONFIG_HOME"
  886. [ -n "$xdg_config_home" ] || xdg_config_home="$HOME/.config"
  887. xdg_config_dirs="$XDG_CONFIG_DIRS"
  888. [ -n "$xdg_config_dirs" ] || xdg_config_dirs="/etc/xdg"
  889. xdg_user_dir="$XDG_DATA_HOME"
  890. [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
  891. xdg_system_dirs="$XDG_DATA_DIRS"
  892. [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
  893. local oldifs="$IFS" dir
  894. IFS=:
  895. for dir in $xdg_config_home $xdg_config_dirs; do
  896. IFS="$oldifs"
  897. check_mimeapps_list "$MIME" "$dir"
  898. done
  899. IFS=:
  900. for dir in $xdg_user_dir $xdg_system_dirs; do
  901. IFS="$oldifs"
  902. check_mimeapps_list "$MIME" "$dir/applications"
  903. done
  904. for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
  905. for prefix in "$XDG_MENU_PREFIX" ""; do
  906. DEBUG 2 "Checking $x/applications/${prefix}defaults.list and $x/applications/${prefix}mimeinfo.cache"
  907. trader_result=`grep "$MIME=" $x/applications/${prefix}defaults.list $x/applications/${prefix}mimeinfo.cache 2> /dev/null | head -n 1 | cut -d '=' -f 2 | cut -d ';' -f 1`
  908. if [ -n "$trader_result" ] ; then
  909. echo $trader_result
  910. exit_success
  911. fi
  912. done
  913. done
  914. defapp_fallback $MIME
  915. exit_success
  916. }
  917. defapp_kde()
  918. {
  919. MIME="$1"
  920. if [ -n "${KDE_SESSION_VERSION}" ]; then
  921. case "${KDE_SESSION_VERSION}" in
  922. 4)
  923. KTRADER=`which ktraderclient 2> /dev/null`
  924. ;;
  925. 5)
  926. KTRADER=`which ktraderclient${KDE_SESSION_VERSION} 2> /dev/null`
  927. ;;
  928. esac
  929. else
  930. KTRADER=`which ktradertest 2> /dev/null`
  931. fi
  932. if [ -n "$KTRADER" ] ; then
  933. DEBUG 1 "Running KDE trader query \"$MIME\" mimetype and \"Application\" servicetype"
  934. trader_result=`$KTRADER --mimetype "$MIME" --servicetype Application 2>/dev/null \
  935. | grep -E "^DesktopEntryPath : |\.desktop$" | head -n1 | sed "s/^DesktopEntryPath : '\(.*\.desktop\)'\$/\1/"`
  936. if [ -n "$trader_result" ] ; then
  937. basename "$trader_result"
  938. exit_success
  939. else
  940. exit_failure_operation_failed
  941. fi
  942. else
  943. defapp_generic "$1"
  944. fi
  945. }
  946. [ x"$1" != x"" ] || exit_failure_syntax
  947. mode=
  948. action=
  949. filename=
  950. mimetype=
  951. case $1 in
  952. install)
  953. action=install
  954. ;;
  955. uninstall)
  956. action=uninstall
  957. ;;
  958. query)
  959. shift
  960. if [ -z "$1" ] ; then
  961. exit_failure_syntax "query type argument missing"
  962. fi
  963. case $1 in
  964. filetype)
  965. action=info
  966. filename="$2"
  967. if [ -z "$filename" ] ; then
  968. exit_failure_syntax "FILE argument missing"
  969. fi
  970. case $filename in
  971. -*)
  972. exit_failure_syntax "unexpected option '$filename'"
  973. ;;
  974. esac
  975. check_input_file "$filename"
  976. filename=`readlink -f -- "$filename"`
  977. ;;
  978. default)
  979. action=defapp
  980. mimetype="$2"
  981. if [ -z "$mimetype" ] ; then
  982. exit_failure_syntax "mimetype argument missing"
  983. fi
  984. case $mimetype in
  985. -*)
  986. exit_failure_syntax "unexpected option '$mimetype'"
  987. ;;
  988. */*)
  989. # Ok
  990. ;;
  991. *)
  992. exit_failure_syntax "mimetype '$mimetype' is not in the form 'minor/major'"
  993. ;;
  994. esac
  995. ;;
  996. *)
  997. exit_failure_syntax "unknown query type '$1'"
  998. ;;
  999. esac
  1000. ;;
  1001. default)
  1002. action=makedefault
  1003. shift
  1004. if [ -z "$1" ] ; then
  1005. exit_failure_syntax "application argument missing"
  1006. fi
  1007. case $1 in
  1008. -*)
  1009. exit_failure_syntax "unexpected option '$1'"
  1010. ;;
  1011. *.desktop)
  1012. filename="$1"
  1013. ;;
  1014. *)
  1015. exit_failure_syntax "malformed argument '$1', expected *.desktop"
  1016. ;;
  1017. esac
  1018. ;;
  1019. *)
  1020. exit_failure_syntax "unknown command '$1'"
  1021. ;;
  1022. esac
  1023. shift
  1024. if [ "$action" = "makedefault" ]; then
  1025. if [ -z "$1" ] ; then
  1026. exit_failure_syntax "mimetype argument missing"
  1027. fi
  1028. while [ $# -gt 0 ] ; do
  1029. case $1 in
  1030. -*)
  1031. exit_failure_syntax "unexpected option '$1'"
  1032. ;;
  1033. esac
  1034. mimetype="$1"
  1035. shift
  1036. make_default_kde "$filename" "$mimetype"
  1037. make_default_generic "$filename" "$mimetype"
  1038. done
  1039. exit_success
  1040. fi
  1041. if [ "$action" = "info" ]; then
  1042. detectDE
  1043. if [ x"$DE" = x"" ]; then
  1044. if [ -x /usr/bin/file ]; then
  1045. DE=generic
  1046. fi
  1047. fi
  1048. case "$DE" in
  1049. kde)
  1050. info_kde "$filename"
  1051. ;;
  1052. gnome*|cinnamon|lxde|mate|xfce)
  1053. info_gnome "$filename"
  1054. ;;
  1055. *)
  1056. info_generic "$filename"
  1057. ;;
  1058. esac
  1059. exit_failure_operation_impossible "no method available for quering MIME type of '$filename'"
  1060. fi
  1061. if [ "$action" = "defapp" ]; then
  1062. detectDE
  1063. case "$DE" in
  1064. kde)
  1065. defapp_kde "$mimetype"
  1066. ;;
  1067. *)
  1068. defapp_generic "$mimetype"
  1069. ;;
  1070. esac
  1071. exit_failure_operation_impossible "no method available for quering default application for '$mimetype'"
  1072. fi
  1073. vendor=true
  1074. while [ $# -gt 0 ] ; do
  1075. parm="$1"
  1076. shift
  1077. case $parm in
  1078. --mode)
  1079. if [ -z "$1" ] ; then
  1080. exit_failure_syntax "mode argument missing for --mode"
  1081. fi
  1082. case "$1" in
  1083. user)
  1084. mode="user"
  1085. ;;
  1086. system)
  1087. mode="system"
  1088. ;;
  1089. *)
  1090. exit_failure_syntax "unknown mode '$1'"
  1091. ;;
  1092. esac
  1093. shift
  1094. ;;
  1095. --novendor)
  1096. vendor=false
  1097. ;;
  1098. -*)
  1099. exit_failure_syntax "unexpected option '$parm'"
  1100. ;;
  1101. *)
  1102. if [ -n "$filename" ] ; then
  1103. exit_failure_syntax "unexpected argument '$parm'"
  1104. fi
  1105. filename="$parm"
  1106. check_input_file "$filename"
  1107. ;;
  1108. esac
  1109. done
  1110. if [ -z "$action" ] ; then
  1111. exit_failure_syntax "command argument missing"
  1112. fi
  1113. if [ -n "$XDG_UTILS_INSTALL_MODE" ] ; then
  1114. if [ "$XDG_UTILS_INSTALL_MODE" = "system" ] ; then
  1115. mode="system"
  1116. elif [ "$XDG_UTILS_INSTALL_MODE" = "user" ] ; then
  1117. mode="user"
  1118. fi
  1119. fi
  1120. if [ -z "$mode" ] ; then
  1121. if [ `whoami` = "root" ] ; then
  1122. mode="system"
  1123. else
  1124. mode="user"
  1125. fi
  1126. fi
  1127. if [ -z "$filename" ] ; then
  1128. exit_failure_syntax "mimetypes-file argument missing"
  1129. fi
  1130. if [ "$vendor" = "true" -a "$action" = "install" ] ; then
  1131. check_vendor_prefix "$filename"
  1132. fi
  1133. xdg_base_dir=
  1134. xdg_dir_name=mime/packages/
  1135. xdg_user_dir="$XDG_DATA_HOME"
  1136. [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
  1137. [ x"$mode" = x"user" ] && xdg_base_dir="$xdg_user_dir/mime"
  1138. xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
  1139. xdg_system_dirs="$XDG_DATA_DIRS"
  1140. [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
  1141. for x in `echo $xdg_system_dirs | sed 's/:/ /g'`; do
  1142. if [ -w $x/$xdg_dir_name ] ; then
  1143. [ x"$mode" = x"system" ] && xdg_base_dir="$x/mime"
  1144. xdg_global_dir="$x/$xdg_dir_name"
  1145. break
  1146. fi
  1147. done
  1148. [ -w $xdg_global_dir ] || xdg_global_dir=
  1149. DEBUG 3 "xdg_user_dir: $xdg_user_dir"
  1150. DEBUG 3 "xdg_global_dir: $xdg_global_dir"
  1151. # Find KDE3 mimelnk directory
  1152. kde_user_dir=
  1153. kde_global_dir=
  1154. kde_global_dirs=`kde${KDE_SESSION_VERSION}-config --path mime 2> /dev/null`
  1155. DEBUG 3 "kde_global_dirs: $kde_global_dirs"
  1156. first=
  1157. for x in `echo $kde_global_dirs | sed 's/:/ /g'` ; do
  1158. if [ -z "$first" ] ; then
  1159. first=false
  1160. kde_user_dir="$x"
  1161. elif [ -w $x ] ; then
  1162. kde_global_dir="$x"
  1163. fi
  1164. done
  1165. DEBUG 3 "kde_user_dir: $kde_user_dir"
  1166. DEBUG 3 "kde_global_dir: $kde_global_dir"
  1167. # TODO: Gnome legacy support
  1168. # See http://forums.fedoraforum.org/showthread.php?t=26875
  1169. gnome_user_dir="$HOME/.gnome/apps"
  1170. gnome_global_dir=/usr/share/gnome/apps
  1171. [ -w $gnome_global_dir ] || gnome_global_dir=
  1172. DEBUG 3 "gnome_user_dir: $gnome_user_dir"
  1173. DEBUG 3 "gnome_global_dir: $gnome_global_dir"
  1174. if [ x"$mode" = x"user" ] ; then
  1175. xdg_dir="$xdg_user_dir"
  1176. kde_dir="$kde_user_dir"
  1177. gnome_dir="$gnome_user_dir"
  1178. my_umask=077
  1179. else
  1180. xdg_dir="$xdg_global_dir"
  1181. kde_dir="$kde_global_dir"
  1182. gnome_dir="$gnome_global_dir"
  1183. my_umask=022
  1184. if [ -z "${xdg_dir}${kde_dir}${gnome_dir}" ] ; then
  1185. exit_failure_operation_impossible "No writable system mimetype directory found."
  1186. fi
  1187. fi
  1188. # echo "[xdg|$xdg_user_dir|$xdg_global_dir]"
  1189. # echo "[kde|$kde_user_dir|$kde_global_dir]"
  1190. # echo "[gnome|$gnome_user_dir|$gnome_global_dir]"
  1191. # echo "[using|$xdg_dir|$kde_dir|$gnome_dir]"
  1192. basefile=`basename "$filename"`
  1193. #[ -z $vendor ] || basefile="$vendor-$basefile"
  1194. mimetypes=
  1195. if [ -n "$kde_dir" ] ; then
  1196. DEBUG 2 "KDE3 mimelnk directory found, extracting mimetypes from XML file"
  1197. mimetypes=`awk < "$filename" '
  1198. # Strip XML comments
  1199. BEGIN {
  1200. suppress=0
  1201. }
  1202. {
  1203. do
  1204. if (suppress) {
  1205. if (match($0,/-->/)) {
  1206. $0=substr($0,RSTART+RLENGTH)
  1207. suppress=0
  1208. }
  1209. else {
  1210. break
  1211. }
  1212. }
  1213. else {
  1214. if (match($0,/<!--/)) {
  1215. if (RSTART>1) print substr($0,0,RSTART)
  1216. $0=substr($0,RSTART+RLENGTH)
  1217. suppress=1
  1218. }
  1219. else {
  1220. if ($0) print $0
  1221. break
  1222. }
  1223. }
  1224. while(1)
  1225. }
  1226. ' | awk '
  1227. # List MIME types listed in <mime-type> tags
  1228. BEGIN {
  1229. RS="<"
  1230. }
  1231. /^mime-info/, /^\/mime-info/ {
  1232. if (match($0,/^mime-type/)) {
  1233. if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
  1234. print substr($0,RSTART+6,RLENGTH-6)
  1235. }
  1236. }
  1237. }'`
  1238. fi
  1239. DEBUG 1 "$action mimetype in $xdg_dir"
  1240. case $action in
  1241. install)
  1242. save_umask=`umask`
  1243. umask $my_umask
  1244. for x in $xdg_dir ; do
  1245. mkdir -p $x
  1246. eval 'cp $filename $x/$basefile'$xdg_redirect_output
  1247. done
  1248. if [ -n "$mimetypes" ] ; then
  1249. # No quotes around $mimetypes
  1250. for x in $mimetypes ; do
  1251. DEBUG 1 "Installing $kde_dir/$x.desktop (KDE 3.x support)"
  1252. mkdir -p `dirname $kde_dir/$x.desktop`
  1253. awk < "$filename" '
  1254. # Strip XML comments
  1255. BEGIN {
  1256. suppress=0
  1257. }
  1258. {
  1259. do
  1260. if (suppress) {
  1261. if (match($0,/-->/)) {
  1262. $0=substr($0,RSTART+RLENGTH)
  1263. suppress=0
  1264. }
  1265. else {
  1266. break
  1267. }
  1268. }
  1269. else {
  1270. if (match($0,/<!--/)) {
  1271. if (RSTART>1) print substr($0,0,RSTART)
  1272. $0=substr($0,RSTART+RLENGTH)
  1273. suppress=1
  1274. }
  1275. else {
  1276. if ($0) print $0
  1277. break
  1278. }
  1279. }
  1280. while(1)
  1281. }
  1282. ' | awk > $kde_dir/$x.desktop '
  1283. # Extract mimetype $x from the XML file $filename
  1284. # Note that bash requires us to escape a single quote as '"'"'
  1285. BEGIN {
  1286. the_type=ARGV[1]
  1287. the_source=ARGV[2]
  1288. ARGC=1
  1289. RS="<"
  1290. found=0
  1291. glob_patterns=""
  1292. }
  1293. /^mime-info/, /^\/mime-info/ {
  1294. if (match($0,/^mime-type/)) {
  1295. if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
  1296. if (substr($0,RSTART+6,RLENGTH-6) == the_type) {
  1297. found=1
  1298. print "[Desktop Entry]"
  1299. print "# Installed by xdg-mime from " the_source
  1300. print "Type=MimeType"
  1301. print "MimeType=" the_type
  1302. the_icon=the_type
  1303. sub("/", "-", the_icon)
  1304. print "Icon=" the_icon
  1305. }
  1306. }
  1307. }
  1308. else if (found) {
  1309. if (match($0,/^\/mime-type/)) {
  1310. if (glob_patterns)
  1311. print "Patterns=" glob_patterns
  1312. exit 0
  1313. }
  1314. if (match($0,/^sub-class-of/)) {
  1315. if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
  1316. print "X-KDE-IsAlso=" substr($0,RSTART+6,RLENGTH-6)
  1317. }
  1318. else {
  1319. print "Error: '"'"'type'"'"' argument missing in " RS $0
  1320. exit 1
  1321. }
  1322. }
  1323. if (match($0,/^glob/)) {
  1324. if (match($0,/pattern="[^"]*/) || match($0,/pattern='"'"'[^'"'"']*/)) {
  1325. glob_patterns = glob_patterns substr($0,RSTART+9,RLENGTH-9) ";"
  1326. }
  1327. else {
  1328. print "Error: '"'"'pattern'"'"' argument missing in " RS $0
  1329. exit 1
  1330. }
  1331. }
  1332. if (match($0,/^comment/)) {
  1333. if (match($0,/xml:lang="[^"]*/) || match($0,/xml:lang='"'"'[^'"'"']*/)) {
  1334. lang=substr($0,RSTART+10,RLENGTH-10)
  1335. }
  1336. else {
  1337. lang=""
  1338. }
  1339. if (match($0,/>/)) {
  1340. comment=substr($0,RSTART+1)
  1341. sub("&lt;", "<", comment)
  1342. sub("&gt;", ">", comment)
  1343. sub("&amp;", "\\&", comment)
  1344. if (lang)
  1345. print "Comment[" lang "]=" comment
  1346. else
  1347. print "Comment=" comment
  1348. }
  1349. }
  1350. }
  1351. }
  1352. END {
  1353. if (!found) {
  1354. print "Error: Mimetype '"'"'" the_type "'"'"' not found"
  1355. exit 1
  1356. }
  1357. }
  1358. ' $x $basefile
  1359. if [ "$?" = "1" ] ; then
  1360. grep -A 10 "^Error:" $kde_dir/$x.desktop >&2
  1361. rm $kde_dir/$x.desktop
  1362. exit 1
  1363. fi
  1364. done
  1365. fi
  1366. umask $save_umask
  1367. ;;
  1368. uninstall)
  1369. for x in $xdg_dir ; do
  1370. rm -f $x/$basefile
  1371. done
  1372. # No quotes around $mimetypes
  1373. for x in $mimetypes ; do
  1374. if grep '^# Installed by xdg-mime' $kde_dir/$x.desktop >/dev/null 2>&1; then
  1375. DEBUG 1 "Removing $kde_dir/$x.desktop (KDE 3.x support)"
  1376. rm -f $kde_dir/$x.desktop
  1377. fi
  1378. done
  1379. ;;
  1380. esac
  1381. update_mime_database $xdg_base_dir
  1382. exit_success