1 /**
2  * Functions for building and retrieving paths to shared MIME database directories.
3  * Authors:
4  *  $(LINK2 https://github.com/FreeSlave, Roman Chistokhodov)
5  * License:
6  *  $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
7  * Copyright:
8  *  Roman Chistokhodov, 2015-2016
9  */
10 
11 module mime.paths;
12 
13 private {
14     import std.algorithm;
15     import std.path;
16     import std.range;
17     import std.traits;
18 }
19 
20 package {
21     import isfreedesktop;
22     import xdgpaths;
23 }
24 
25 /**
26  * Get shared MIME database paths based on dataPaths.
27  * Returns:
28  *  Range of paths where MIME database files are stored.
29  *
30  */
31 auto mimePaths(Range)(Range dataPaths) if(isInputRange!Range && is(ElementType!Range : string)) {
32     return dataPaths.map!(p => buildPath(p, "mime"));
33 }
34 
35 ///
36 unittest
37 {
38     auto dataPaths = ["share", buildPath("local", "share")];
39     assert(equal(mimePaths(dataPaths), [buildPath("share", "mime"), buildPath("local", "share", "mime")]));
40 }
41 
42 
43 static if (isFreedesktop) {
44     version(unittest) {
45         import std.process : environment;
46 
47         package struct EnvGuard
48         {
49             this(string env) {
50                 envVar = env;
51                 envValue = environment.get(env);
52             }
53 
54             ~this() {
55                 if (envValue is null) {
56                     environment.remove(envVar);
57                 } else {
58                     environment[envVar] = envValue;
59                 }
60             }
61 
62             string envVar;
63             string envValue;
64         }
65     }
66 
67     /**
68      * Get shared MIME database paths in system.
69      *
70      * $(BLUE This function is Freedesktop only).
71      * Note: This function does not check if paths exist and appear to be directories.
72      * Returns:
73      *  Range of MIME paths in the order of preference from the most preferable to the least.
74      * Usually it's the same as $HOME/.local/share/mime, /usr/local/share/mime and /usr/share/mime.
75      */
76     @safe auto mimePaths() {
77         return xdgAllDataDirs("mime");
78     }
79 
80     ///
81     unittest
82     {
83         auto dataHomeGuard = EnvGuard("XDG_DATA_HOME");
84         auto dataDirsGuard = EnvGuard("XDG_DATA_DIRS");
85 
86         environment["XDG_DATA_HOME"] = "/home/user/data";
87         environment["XDG_DATA_DIRS"] = "/usr/local/data:/usr/data";
88 
89         assert(mimePaths() == [
90             "/home/user/data/mime", "/usr/local/data/mime", "/usr/data/mime"
91         ]);
92     }
93 
94     /**
95      * Get writable path where shared MIME database is stored.
96      *
97      * $(BLUE This function is Freedesktop only).
98      * Returns:
99      *  Writable MIME path for the current user ($HOME/.local/share/mime).
100      * Note: this function does not check if the path exist and appears to be directory.
101      */
102     @safe string writableMimePath() nothrow {
103         return xdgDataHome("mime");
104     }
105 
106     ///
107     unittest
108     {
109         auto dataHomeGuard = EnvGuard("XDG_DATA_HOME");
110 
111         environment["XDG_DATA_HOME"] = "/home/user/data";
112 
113         assert(writableMimePath() == "/home/user/data/mime");
114     }
115 }