1/*
2 * This file is part of gitg
3 *
4 * Copyright (C) 2012 - Jesse van den Kieboom
5 *
6 * gitg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * gitg 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with gitg. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20namespace Gitg
21{
22
23[GtkTemplate (ui = "/org/gnome/gitg/ui/gitg-preferences-history.ui")]
24public class PreferencesHistory : Gtk.Grid, GitgExt.Preferences
25{
26	// Do this to pull in config.h before glib.h (for gettext...)
27	private const string version = Gitg.Config.VERSION;
28	private bool d_block;
29
30	[GtkChild (name = "collapse_inactive_lanes_enabled")]
31	private Gtk.CheckButton d_collapse_inactive_lanes_enabled;
32
33	[GtkChild (name = "adjustment_collapse")]
34	private Gtk.Adjustment d_adjustment_collapse;
35	[GtkChild (name = "collapse_inactive_lanes")]
36	private Gtk.Scale d_collapse_inactive_lanes;
37
38	[GtkChild (name = "topological_order")]
39	private Gtk.CheckButton d_topological_order;
40
41	[GtkChild (name = "mainline_head")]
42	private Gtk.CheckButton d_mainline_head;
43
44	[GtkChild (name = "select_current_branch" )]
45	private Gtk.RadioButton d_select_current_branch;
46
47	[GtkChild (name = "select_all_branches" )]
48	private Gtk.RadioButton d_select_all_branches;
49
50	[GtkChild (name = "select_all_commits" )]
51	private Gtk.RadioButton d_select_all_commits;
52
53	[GtkChild (name = "sort_references_by_activity")]
54	private Gtk.CheckButton d_sort_references_by_activity;
55
56	[GtkChild (name = "show_upstream_with_branch")]
57	private Gtk.CheckButton d_show_upstream_with_branch;
58
59	private Gtk.RadioButton[] d_select_buttons;
60	private string[] d_select_names;
61
62	private static int round_val(double val)
63	{
64		int ival = (int)val;
65
66		return ival + (int)(val - ival > 0.5);
67	}
68
69	construct
70	{
71		var settings = new Settings(Gitg.Config.APPLICATION_ID + ".preferences.history");
72
73		settings.bind("collapse-inactive-lanes-enabled",
74		              d_collapse_inactive_lanes_enabled,
75		              "active",
76		              SettingsBindFlags.GET | SettingsBindFlags.SET);
77
78		settings.bind("topological-order",
79		              d_topological_order,
80		              "active",
81		              SettingsBindFlags.GET | SettingsBindFlags.SET);
82
83		settings.bind("mainline-head",
84		              d_mainline_head,
85		              "active",
86		              SettingsBindFlags.GET | SettingsBindFlags.SET);
87
88		d_adjustment_collapse.value_changed.connect((adj) => {
89			if (d_block)
90			{
91				return;
92			}
93
94			var nval = round_val(adj.get_value());
95			var val = settings.get_int("collapse-inactive-lanes");
96
97			if (val != nval)
98			{
99				settings.set_int("collapse-inactive-lanes", nval);
100			}
101
102			d_block = true;
103			adj.set_value(nval);
104			d_block = false;
105		});
106
107		var monsig = settings.changed["collapse-inactive-lanes"].connect((s, k) => {
108			d_block = true;
109			update_collapse_inactive_lanes(settings);
110			d_block = false;
111		});
112
113		destroy.connect((w) => {
114			settings.disconnect(monsig);
115		});
116
117		update_collapse_inactive_lanes(settings);
118
119		d_select_buttons = new Gtk.RadioButton[] {
120			d_select_current_branch,
121			d_select_all_branches,
122			d_select_all_commits
123		};
124
125		d_select_names = new string[] {
126			"current-branch",
127			"all-branches",
128			"all-commits"
129		};
130
131		settings.bind("default-selection",
132		              this,
133		              "default-selection",
134		              SettingsBindFlags.GET | SettingsBindFlags.SET);
135
136		for (var i = 0; i < d_select_buttons.length; i++) {
137			d_select_buttons[i].notify["active"].connect(() => {
138				notify_property("default-selection");
139			});
140		}
141
142		settings.bind_with_mapping("reference-sort-order",
143		                           d_sort_references_by_activity,
144		                           "active",
145		                           SettingsBindFlags.GET | SettingsBindFlags.SET,
146			(value, variant) => {
147				value.set_boolean(variant.get_string() == "last-activity");
148				return true;
149			},
150
151		    (value, expected_type) => {
152		    	return new Variant.string(value.get_boolean() ? "last-activity" : "name");
153		    },
154
155		    null, null
156		);
157
158		settings.bind("show-upstream-with-branch",
159		              d_show_upstream_with_branch,
160		              "active",
161		              SettingsBindFlags.GET | SettingsBindFlags.SET);
162	}
163
164	public string default_selection
165	{
166		get
167		{
168			for (var i = 0; i < d_select_buttons.length; i++)
169			{
170				if (d_select_buttons[i].active)
171				{
172					return d_select_names[i];
173				}
174			}
175
176			return d_select_names[0];
177		}
178
179		set
180		{
181			for (var i = 0; i < d_select_buttons.length; i++)
182			{
183				if (d_select_names[i] == value)
184				{
185					d_select_buttons[i].active = true;
186					return;
187				}
188			}
189		}
190	}
191
192	private void update_collapse_inactive_lanes(Settings settings)
193	{
194		var val = round_val(d_collapse_inactive_lanes.get_value());
195		var nval = settings.get_int("collapse-inactive-lanes");
196
197		if (val != nval)
198		{
199			d_collapse_inactive_lanes.set_value((double)nval);
200		}
201	}
202
203	public Gtk.Widget widget
204	{
205		owned get
206		{
207			return this;
208		}
209	}
210
211	public string id
212	{
213		owned get { return "/org/gnome/gitg/Preferences/History"; }
214	}
215
216	public string display_name
217	{
218		owned get { return _("History"); }
219	}
220}
221
222}
223
224// vi:ts=4
225