-
Notifications
You must be signed in to change notification settings - Fork 2
/
sshconf-entry-model.vala
285 lines (269 loc) · 8.84 KB
/
sshconf-entry-model.vala
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* sshconf-entry-model.vala
*
* Copyright (C) 2011 Qball Cow <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author:
* Qball Cow <[email protected]>
*/
using Gtk;
namespace SSHConf
{
class EntryModel : GLib.Object, Gtk.TreeModel, Gtk.TreeSortable
{
public enum Columns
{
ENTRY = 0,
NAME,
HOSTNAME,
NUM_COLUMNS
}
private GLib.List<Entry> entries;
private int stamp = 1;
~EntryModel()
{
entries = null;
stdout.printf("~EntryModel\n");
}
public GLib.Type get_column_type(int index)
{
switch((EntryModel.Columns)index)
{
case Columns.ENTRY:
return typeof(SSHConf.Entry);
case Columns.NAME:
case Columns.HOSTNAME:
return typeof(string);
default:
GLib.error("Unknown column");
}
}
public Gtk.TreeModelFlags get_flags()
{
return Gtk.TreeModelFlags.LIST_ONLY;
}
public bool get_iter(out Gtk.TreeIter iter, Gtk.TreePath path)
{
int[] indices = path.get_indices ();
unowned GLib.List<Entry> en = entries.nth(indices[0]);
if(en != null)
{
iter = Gtk.TreeIter();
iter.stamp = this.stamp;
iter.user_data = en;
iter.user_data2 = indices[0].to_pointer();
return true;
}
return false;
}
/**
* Get the number of columns in this model
*/
public int get_n_columns()
{
return (int)Columns.NUM_COLUMNS;
}
/**
* Get path from iter
*/
public Gtk.TreePath? get_path(Gtk.TreeIter iter)
{
int index = (int)(iter.user_data2);
Gtk.TreePath path = new Gtk.TreePath.from_indices(index, -1);
return path;
}
/**
* get value
*/
public void get_value(Gtk.TreeIter iter, int column,out Value val)
{
val = Value(this.get_column_type(column));
Entry? en = ((List<Entry>)iter.user_data).data;
switch((EntryModel.Columns)column)
{
case Columns.ENTRY:
val.set_object(en);
return;
case Columns.NAME:
val.set_string(en.name);
return;
case Columns.HOSTNAME:
val.set_string(en.hostname);
return;
default:
GLib.error("Unknown column");
}
}
/**
* Get the children of iter %parent (always false)
* if parent is null return first node
*/
public bool iter_children(out Gtk.TreeIter iter, Gtk.TreeIter? parent)
{
if(parent != null) return false;
return this.iter_first(out iter);
}
public bool iter_has_child(Gtk.TreeIter iter)
{
return false;
}
public int iter_n_children(Gtk.TreeIter? iter)
{
if(iter != null) return 0;
return (int)entries.length();
}
public bool iter_next(ref Gtk.TreeIter iter)
{
unowned List<Entry> entry = ((List<Entry>)iter.user_data);
if(entry.next == null) return false;
iter.stamp = this.stamp;
iter.user_data = entry.next;
iter.user_data2 = ((int)(iter.user_data2)+1).to_pointer();
return true;
}
public bool iter_nth_child(out Gtk.TreeIter iter, Gtk.TreeIter? parent, int n)
{
// no children
if(parent != null) return false;
unowned List<Entry>? en = entries.nth(n);
if(en == null) return false;
iter.stamp = this.stamp;
iter.user_data = (void*)en;
iter.user_data2 = n.to_pointer();
return true;
}
public bool iter_parent(out Gtk.TreeIter iter, Gtk.TreeIter child)
{
return false;
}
public void ref_node(Gtk.TreeIter iter)
{
}
public void unref_node(Gtk.TreeIter iter)
{
}
public bool iter_first(out Gtk.TreeIter iter)
{
if(entries.length() == 0) return false;
iter.stamp = this.stamp;
iter.user_data = (void*)entries.first();
iter.user_data2 = (1).to_pointer();
return true;
}
private void entry_changed(Entry en)
{
int index = entries.index(en);
Gtk.TreePath rpath = new Gtk.TreePath.from_indices(index, -1);
Gtk.TreeIter riter;
if(this.get_iter(out riter, rpath))
{
row_changed(rpath, riter);
sort_columns();
}
}
public Gtk.TreePath add_entry (Entry entry)
{
this.stamp++;
entries.append(entry);
Gtk.TreePath path = new Gtk.TreePath.from_indices(entries.length()-1,-1);
Gtk.TreeIter iter;
if(this.get_iter(out iter, path))
{
row_inserted(path, iter);
}
else
{
GLib.error("Failed to get iter for new node");
}
entry.notify["name"].connect((source,spec)=>{
entry_changed((source as SSHConf.Entry));
});
sort_columns();
path = new Gtk.TreePath.from_indices(entries.index(entry),-1);
return path;
}
public void remove_entry (Entry entry)
{
this.stamp++;
unowned List<Entry> en = entries.find(entry);
if(en != null)
{
int index = entries.index(entry);
Gtk.TreePath path = new Gtk.TreePath.from_indices(index, -1);
Entry e = (owned)en.data;
entries.delete_link(en);
e = null;
/* signal row deleted */
row_deleted(path);
}
}
/** Sortable interface
*/
private static int sort_entries_by_name(Entry? a, Entry? b)
{
if(a == null && b == null) return 0;
else if(a == null) return -1;
else if(b == null) return 1;
return a.name.collate(b.name);
}
private void sort_columns()
{
List<Entry> list_copy = null;//entries.copy();
foreach(Entry a in entries) {
list_copy.append(a);
}
list_copy.sort(sort_entries_by_name);
int[] new_order = new int[list_copy.length()];
int i =0;
foreach(Entry a in list_copy)
{
new_order[i] = entries.index(a);
i++;
}
entries = (owned)list_copy;
this.stamp++;
GLib.debug("reordered rows");
var path = new Gtk.TreePath();
Glue.rows_reordered(this,path,null, new_order);
}
private Columns sort_column = Columns.NAME;
private Gtk.SortType sort_order = Gtk.SortType.ASCENDING;
public bool has_default_sort_func()
{
return true;
}
public bool get_sort_column_id (out int column_id, out Gtk.SortType column_sort_order)
{
if(sort_column != Columns.NAME) return false;
column_id = sort_column;
column_sort_order = sort_order;
return true;
}
public void set_sort_column_id(int column_id, Gtk.SortType type)
{
/* Do nothing, we can only sort on name */
}
public void set_sort_func(int column_id, owned Gtk.TreeIterCompareFunc func)
{
/* Setting sort func is not supported */
GLib.error("Setting sort func is not supported");
}
public void set_default_sort_func(owned Gtk.TreeIterCompareFunc func)
{
/* Do nothing, we can only do our sort */
GLib.error("Default sort is not supported");
}
}
}