1/*
2 *
3 * Copyright (C) 2015 - Jesse van den Kieboom
4 *
5 * gitg is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * gitg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with gitg. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19namespace Gitg
20{
21
22public class Utils
23{
24	public static string replace_home_dir_with_tilde(File file)
25	{
26		var name = file.get_parse_name();
27		var homedir = Environment.get_home_dir();
28
29		if (homedir != null)
30		{
31			try
32			{
33				var hd = Filename.to_utf8(homedir, -1, null, null);
34
35				if (hd == name)
36				{
37					name = "~/";
38				}
39				else
40				{
41					if (name.has_prefix(hd + "/"))
42					{
43						name = "~" + name[hd.length:name.length];
44					}
45				}
46			} catch {}
47		}
48
49		return name;
50	}
51
52	public static string expand_home_dir(string path)
53	{
54		string? homedir = null;
55		int pos = -1;
56
57		if (path.has_prefix("~/"))
58		{
59			homedir = PlatformSupport.get_user_home_dir();
60			pos = 1;
61		}
62		else if (path.has_prefix("~"))
63		{
64			pos = path.index_of_char('/');
65			var user = path[1:pos];
66
67			homedir = PlatformSupport.get_user_home_dir(user);
68		}
69
70		if (homedir != null)
71		{
72			return Path.build_filename(homedir, path.substring(pos + 1));
73		}
74
75		return path;
76	}
77}
78
79}
80
81// ex:ts=4 noet
82