summaryrefslogtreecommitdiff
path: root/src/utilities/paths.vala
blob: a6e01efef00aa46334124c900ac40734c8bd6bd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/////////////////////////////////////////////////////////////////////////
// Copyright 2011-2018 Simon Schneegans
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////

namespace GnomePie {

/////////////////////////////////////////////////////////////////////////
/// A static class which stores all relevant paths used by Gnome-Pie.
/// These depend upon the location from which the program was launched.
/////////////////////////////////////////////////////////////////////////

public class Paths : GLib.Object {

    /////////////////////////////////////////////////////////////////////
    /// The config directory,
    /// usually ~/.config/gnome-pie/.
    /////////////////////////////////////////////////////////////////////

    public static string config_directory { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The log file,
    /// usually ~/.config/gnome-pie/gnome-pie.log.
    /////////////////////////////////////////////////////////////////////

    public static string log { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The settings file,
    /// usually ~/.config/gnome-pie/gnome-pie.conf.
    /////////////////////////////////////////////////////////////////////

    public static string settings { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The pie configuration file
    /// usually ~/.config/gnome-pie/pies.conf.
    /////////////////////////////////////////////////////////////////////

    public static string pie_config { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The directory containing themes installed by the user
    /// usually ~/.config/gnome-pie/themes.
    /////////////////////////////////////////////////////////////////////

    public static string local_themes { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The directory containing pre-installed themes
    /// usually /usr/share/gnome-pie/themes.
    /////////////////////////////////////////////////////////////////////

    public static string global_themes { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The directory containing locale files
    /// usually /usr/share/locale.
    /////////////////////////////////////////////////////////////////////

    public static string locales { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The directory containing UI declaration files
    /// usually /usr/share/gnome-pie/ui/.
    /////////////////////////////////////////////////////////////////////

    public static string ui_files { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The autostart file of gnome-pie_config
    /// usually ~/.config/autostart/gnome-pie.desktop.
    /////////////////////////////////////////////////////////////////////

    public static string autostart { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The path where all pie-launchers are stored
    /// usually ~/.config/gnome-pie/launchers.
    /////////////////////////////////////////////////////////////////////

    public static string launchers { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The path to the executable.
    /////////////////////////////////////////////////////////////////////

    public static string executable { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// Deletes a directory recursively from disk. Use with care :)
    /////////////////////////////////////////////////////////////////////

    public static void delete_directory(string directory) {
        try {
            var d = Dir.open(directory);
            string name;
            while ((name = d.read_name()) != null) {
                string path = Path.build_filename(directory, name);
                if (FileUtils.test(path, FileTest.IS_DIR)) {
                    delete_directory(path);
                } else {
                    FileUtils.remove(path);
                }
            }
            DirUtils.remove(directory);
        } catch (Error e) {
            warning (e.message);
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Initializes all values above.
    /////////////////////////////////////////////////////////////////////

    public static void init() {

        // get path of executable
        try {
            executable = GLib.File.new_for_path(GLib.FileUtils.read_link("/proc/self/exe")).get_path();
        } catch (GLib.FileError e) {
            warning("Failed to get path of executable!");
        }

        // append resources to icon search path to icon theme, if neccasary
        var icon_dir = GLib.File.new_for_path(GLib.Path.get_dirname(executable)).get_child("resources");

        if (icon_dir.query_exists()) {
            string path = icon_dir.get_path();
            Gtk.IconTheme.get_default().append_search_path(path);
        }

        Gtk.IconTheme.get_default().append_search_path(GLib.Environment.get_home_dir() + ".icons");

        // get global paths
        var default_dir = GLib.File.new_for_path("/usr/share/gnome-pie/");
        if(!default_dir.query_exists()) {
            default_dir = GLib.File.new_for_path("/usr/local/share/gnome-pie/");

            if(!default_dir.query_exists()) {
                default_dir = GLib.File.new_for_path(GLib.Path.get_dirname(
                    executable)).get_child("resources");
            }
        }

        global_themes = default_dir.get_path() + "/themes";
        ui_files = default_dir.get_path() + "/ui";

        // get locales path
        var locale_dir = GLib.File.new_for_path("/usr/share/locale/de/LC_MESSAGES/gnomepie.mo");
        if(locale_dir.query_exists()) {
            locale_dir = GLib.File.new_for_path("/usr/share/locale");
        } else {
            locale_dir = GLib.File.new_for_path("/usr/local/share/locale/de/LC_MESSAGES/gnomepie.mo");
            if(locale_dir.query_exists()) {
                locale_dir = GLib.File.new_for_path("/usr/local/share/locale");
            } else {
                locale_dir = GLib.File.new_for_path(GLib.Path.get_dirname(
                    executable)).get_child("resources/locale/de/LC_MESSAGES/gnomepie.mo");

                if(locale_dir.query_exists()) {
                    locale_dir = GLib.File.new_for_path(GLib.Path.get_dirname(
                        executable)).get_child("resources/locale");
                }
            }
        }

        locales = locale_dir.get_path();

        // get local paths
        var config_dir = GLib.File.new_for_path(
            GLib.Environment.get_user_config_dir()).get_child("gnome-pie");

        // create config_dir if neccasary
        if(!config_dir.query_exists()) {
            try {
                config_dir.make_directory();
            } catch (GLib.Error e) {
                error(e.message);
            }
        }

        config_directory = config_dir.get_path();

        // create local themes directory if neccasary
        var themes_dir = config_dir.get_child("themes");
        if(!themes_dir.query_exists()) {
            try {
                themes_dir.make_directory();
            } catch (GLib.Error e) {
                error(e.message);
            }
        }

        local_themes = themes_dir.get_path();

        // create launchers directory if neccasary
        var launchers_dir = config_dir.get_child("launchers");
        if(!launchers_dir.query_exists()) {
            try {
                launchers_dir.make_directory();
            } catch (GLib.Error e) {
                error(e.message);
            }
        }

        launchers = launchers_dir.get_path();

        // check for config file
        var config_file = config_dir.get_child("pies.conf");

        pie_config = config_file.get_path();
        settings = config_dir.get_path() + "/gnome-pie.conf";
        log = config_dir.get_path() + "/gnome-pie.log";

        if (!GLib.File.new_for_path(log).query_exists()) {
            try {
                FileUtils.set_contents(log, "");
            } catch (GLib.FileError e) {
                error(e.message);
            }
        }

        // autostart file name
        autostart = GLib.Path.build_filename(GLib.Environment.get_user_config_dir(),
                                             "autostart", "gnome-pie.desktop", null);

        // print results
        if (!GLib.File.new_for_path(pie_config).query_exists())
            warning("Failed to find pie configuration file \"pies.conf\"! (This should only happen when Gnome-Pie is started for the first time...)");

        if (!GLib.File.new_for_path(settings).query_exists())
            warning("Failed to find settings file \"gnome-pie.conf\"! (This should only happen when Gnome-Pie is started for the first time...)");

        if (!GLib.File.new_for_path(log).query_exists())
            warning("Failed to find log file \"gnome-pie.log\"!");

        if (!GLib.File.new_for_path(local_themes).query_exists())
            warning("Failed to find local themes directory!");

        if (!GLib.File.new_for_path(launchers).query_exists())
            warning("Failed to find launchers directory!");

        if (!GLib.File.new_for_path(global_themes).query_exists())
            warning("Failed to find global themes directory!");

        if (!GLib.File.new_for_path(ui_files).query_exists())
            warning("Failed to find UI files directory!");
    }
}

}