1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * d-dazzle 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with d-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.Path;
20 
21 private import dazzle.PathElement;
22 private import dazzle.c.functions;
23 public  import dazzle.c.types;
24 private import glib.ConstructionException;
25 private import glib.ListG;
26 private import glib.Str;
27 private import gobject.ObjectG;
28 
29 
30 /** */
31 public class Path : ObjectG
32 {
33 	/** the main Gtk struct */
34 	protected DzlPath* dzlPath;
35 
36 	/** Get the main Gtk struct */
37 	public DzlPath* getPathStruct(bool transferOwnership = false)
38 	{
39 		if (transferOwnership)
40 			ownedRef = false;
41 		return dzlPath;
42 	}
43 
44 	/** the main Gtk struct as a void* */
45 	protected override void* getStruct()
46 	{
47 		return cast(void*)dzlPath;
48 	}
49 
50 	/**
51 	 * Sets our main struct and passes it to the parent class.
52 	 */
53 	public this (DzlPath* dzlPath, bool ownedRef = false)
54 	{
55 		this.dzlPath = dzlPath;
56 		super(cast(GObject*)dzlPath, ownedRef);
57 	}
58 
59 
60 	/** */
61 	public static GType getType()
62 	{
63 		return dzl_path_get_type();
64 	}
65 
66 	/** */
67 	public this()
68 	{
69 		auto p = dzl_path_new();
70 
71 		if(p is null)
72 		{
73 			throw new ConstructionException("null returned by new");
74 		}
75 
76 		this(cast(DzlPath*) p, true);
77 	}
78 
79 	/** */
80 	public void append(PathElement element)
81 	{
82 		dzl_path_append(dzlPath, (element is null) ? null : element.getPathElementStruct());
83 	}
84 
85 	/**
86 	 * Gets the path element found at @index.
87 	 *
88 	 * Indexes start from zero.
89 	 *
90 	 * Returns: An #DzlPathElement.
91 	 */
92 	public PathElement getElement(uint index)
93 	{
94 		auto p = dzl_path_get_element(dzlPath, index);
95 
96 		if(p is null)
97 		{
98 			return null;
99 		}
100 
101 		return ObjectG.getDObject!(PathElement)(cast(DzlPathElement*) p);
102 	}
103 
104 	/**
105 	 * Returns: The elements of the path.
106 	 */
107 	public ListG getElements()
108 	{
109 		auto p = dzl_path_get_elements(dzlPath);
110 
111 		if(p is null)
112 		{
113 			return null;
114 		}
115 
116 		return new ListG(cast(GList*) p);
117 	}
118 
119 	/** */
120 	public uint getLength()
121 	{
122 		return dzl_path_get_length(dzlPath);
123 	}
124 
125 	/** */
126 	public bool hasPrefix(Path prefix)
127 	{
128 		return dzl_path_has_prefix(dzlPath, (prefix is null) ? null : prefix.getPathStruct()) != 0;
129 	}
130 
131 	/** */
132 	public bool isEmpty()
133 	{
134 		return dzl_path_is_empty(dzlPath) != 0;
135 	}
136 
137 	/** */
138 	public void prepend(PathElement element)
139 	{
140 		dzl_path_prepend(dzlPath, (element is null) ? null : element.getPathElementStruct());
141 	}
142 
143 	/** */
144 	public string printf()
145 	{
146 		auto retStr = dzl_path_printf(dzlPath);
147 
148 		scope(exit) Str.freeString(retStr);
149 		return Str.toString(retStr);
150 	}
151 }