1/* embedded.vala
2 *
3 * Copyright (C) 2008-2009 Didier Villevalois
4 * Copyright (C) 2008-2012 Florian Brosch
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19 *
20 * Author:
21 * 	Didier 'Ptitjes Villevalois <ptitjes@free.fr>
22 */
23
24
25public class Valadoc.Content.Embedded : ContentElement, Inline, StyleAttributes {
26	public string url {
27		get;
28		set;
29	}
30
31	public string? caption {
32		get;
33		set;
34	}
35
36	public HorizontalAlign horizontal_align {
37		get;
38		set;
39	}
40
41	public VerticalAlign vertical_align {
42		get;
43		set;
44	}
45
46	public string? style {
47		get;
48		set;
49	}
50
51	public Api.Package package;
52
53	private ResourceLocator _locator;
54
55	internal Embedded () {
56		base ();
57	}
58
59	public override void configure (Settings settings, ResourceLocator locator) {
60		_locator = locator;
61	}
62
63	public override void check (Api.Tree api_root, Api.Node container, string file_path,
64								ErrorReporter reporter, Settings settings)
65	{
66		// search relative to our file
67		if (!Path.is_absolute (url)) {
68			string relative_to_file = Path.build_path (Path.DIR_SEPARATOR_S,
69													   Path.get_dirname (file_path),
70													   url);
71			if (FileUtils.test (relative_to_file, FileTest.EXISTS | FileTest.IS_REGULAR)) {
72				url = (owned) relative_to_file;
73				package = container.package;
74				return ;
75			}
76		}
77
78		// search relative to the current directory / absolute path
79		if (!FileUtils.test (url, FileTest.EXISTS | FileTest.IS_REGULAR)) {
80			string base_name = Path.get_basename (url);
81
82			foreach (unowned string dir in settings.alternative_resource_dirs) {
83				string alternative_path = Path.build_path (Path.DIR_SEPARATOR_S,
84														 dir,
85														 base_name);
86				if (FileUtils.test (alternative_path, FileTest.EXISTS | FileTest.IS_REGULAR)) {
87					url = (owned) alternative_path;
88					package = container.package;
89					return ;
90				}
91			}
92
93			string node_segment = (container is Api.Package)? "" : container.get_full_name () + ": ";
94			reporter.simple_error ("%s: %s{{".printf (file_path, node_segment),
95								   "'%s' does not exist", url);
96		} else {
97			package = container.package;
98		}
99	}
100
101	public override void accept (ContentVisitor visitor) {
102		visitor.visit_embedded (this);
103	}
104
105	public override bool is_empty () {
106		return false;
107	}
108
109	public override ContentElement copy (ContentElement? new_parent = null) {
110		Embedded embedded = new Embedded ();
111		embedded.parent = new_parent;
112
113		embedded.horizontal_align = horizontal_align;
114		embedded.vertical_align = vertical_align;
115		embedded._locator = _locator;
116		embedded.caption = caption;
117		embedded.package = package;
118		embedded.style = style;
119		embedded.url = url;
120
121		return embedded;
122	}
123}
124