File: gitg-repository-list-box.vala

package info (click to toggle)
gitg 41-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,876 kB
  • sloc: ansic: 1,636; ruby: 1,445; sh: 314; python: 261; xml: 57; makefile: 15
file content (657 lines) | stat: -rw-r--r-- 13,994 bytes parent folder | download
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
 * This file is part of gitg
 *
 * Copyright (C) 2012-2016 - Ignacio Casal Quinteiro
 *
 * gitg 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 2 of the License, or
 * (at your option) any later version.
 *
 * gitg 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 gitg. If not, see <http://www.gnu.org/licenses/>.
 */

namespace Gitg
{
	public enum SelectionMode
	{
		NORMAL,
		SELECTION
	}

	public class RepositoryListBox : Gtk.ListBox
	{
		private string? d_filter_text;

		public signal void repository_activated(Repository repository);
		public signal void show_error(string primary_message, string secondary_message);

		[GtkTemplate (ui = "/org/gnome/gitg/ui/gitg-repository-list-box-row.ui")]
		public class Row : Gtk.ListBoxRow
		{
			private Repository? d_repository;
			private DateTime d_time = new DateTime.now_local();
			private bool d_loading;
			[GtkChild]
			private unowned ProgressBin d_progress_bin;
			[GtkChild]
			private unowned Gtk.Label d_repository_label;
			[GtkChild]
			private unowned Gtk.Label d_description_label;
			[GtkChild]
			private unowned Gtk.Label d_branch_label;
			[GtkChild]
			private unowned Gtk.Spinner d_spinner;
			[GtkChild]
			private unowned Gtk.CheckButton d_remove_check_button;
			[GtkChild]
			private unowned Gtk.Revealer d_remove_revealer;
			[GtkChild]
			private unowned Gtk.Box d_languages_box;

			public signal void request_remove();

			private SelectionMode d_mode;
			private string? d_dirname;
			private string? d_branch_name;

			public SelectionMode mode
			{
				get { return d_mode; }

				set
				{
					if (d_mode != value)
					{
						d_mode = value;

						d_remove_revealer.reveal_child = (d_mode == SelectionMode.SELECTION);

						d_remove_check_button.active = false;
					}
				}
			}

			public new bool selected
			{
				get; set;
			}

			construct
			{
				d_remove_check_button.bind_property("active",
				                                    this,
				                                    "selected",
				                                    BindingFlags.BIDIRECTIONAL |
				                                    BindingFlags.SYNC_CREATE);
			}

			public Repository? repository
			{
				get { return d_repository; }
				set
				{
					d_repository = value;
					update_repository_data();
				}
			}

			public bool can_remove
			{
				get { return d_remove_check_button.sensitive; }
				set { d_remove_check_button.sensitive = value; }
			}

			public DateTime time
			{
				get { return d_time; }
				set { d_time = value; }
			}

			public double fraction
			{
				set { d_progress_bin.fraction = value; }
			}

			public string? repository_name
			{
				get { return d_repository_label.get_text(); }
				set { d_repository_label.label = value; }
			}

			public string? dirname
			{
				get { return d_dirname; }
				set
				{
					d_dirname = value;
					update_branch_label();
				}
			}

			public string? branch_name
			{
				get { return d_branch_name; }
				set
				{
					d_branch_name = value;
					update_branch_label();
				}
			}

			private void update_branch_label()
			{
				if (d_branch_name == null || d_branch_name == "")
				{
					// Translators: this is used to construct: "at <directory>", to indicate where the repository is at.
					d_branch_label.label = _("at %s").printf(d_dirname);
				}
				else if (d_dirname == null || d_dirname == "")
				{
					d_branch_label.label = d_branch_name;
				}
				else
				{
					// Translators: this is used to construct: "<branch-name> at <directory>"
					d_branch_label.label = _("%s at %s").printf(d_branch_name, d_dirname);
				}
			}

			private void update_repository_data()
			{
				string head_name = "";
				string head_description = "";

				if (d_repository != null)
				{
					try
					{
						var head = d_repository.get_head();
						head_name = head.parsed_name.shortname;

						var commit = (Ggit.Commit)head.lookup();
						var tree = commit.get_tree();

						Ggit.OId? entry_id = null;

						for (var i = 0; i < tree.size(); i++)
						{
							var entry = tree.get(i);
							var name = entry.get_name();

							if (name != null && name.has_suffix(".doap"))
							{
								entry_id = entry.get_id();
								break;
							}
						}

						if (entry_id != null)
						{
							var blob = d_repository.lookup<Ggit.Blob>(entry_id);

							unowned uint8[] content = blob.get_raw_content();
							var doap = new Ide.Doap();
							doap.load_from_data((string)content, -1);

							head_description = doap.get_shortdesc();

							foreach (var lang in doap.get_languages())
							{
								var frame = new Gtk.Frame(null);
								frame.shadow_type = Gtk.ShadowType.NONE;
								frame.get_style_context().add_class("language-frame");
								frame.show();

								var label = new Gtk.Label(lang);
								var attr_list = new Pango.AttrList();
								attr_list.insert(Pango.attr_scale_new(Pango.Scale.SMALL));
								label.set_attributes(attr_list);
								label.show();

								frame.add(label);
								d_languages_box.add(frame);
							}
						}
					} catch {}
				}

				repository_name = d_repository != null ? d_repository.name : "";

				d_description_label.label = head_description;
				d_description_label.visible = head_description != "";

				branch_name = head_name;
			}

			public bool loading
			{
				get { return d_loading; }
				set
				{
					d_loading = value;

					if (!d_loading)
					{
						d_spinner.stop();
						d_spinner.hide();
						d_progress_bin.fraction = 0;
					}
					else
					{
						d_spinner.show();
						d_spinner.start();
					}
				}
			}

			public Row(Repository? repository, string dirname)
			{
				Object(repository: repository, dirname: dirname);
			}
		}

		public SelectionMode mode { get; set; }

		public bool bookmarks_from_recent_files { get; set; default = true; }

		private File? d_location;
		private uint d_save_repository_bookmarks_id;
		private BookmarkFile d_bookmark_file;

		public File? location
		{
			get
			{
				return d_location;
			}

			set
			{
				if (d_save_repository_bookmarks_id != 0)
				{
					Source.remove(d_save_repository_bookmarks_id);
					save_repository_bookmarks();
				}

				d_location = value;
				d_bookmark_file = new BookmarkFile();

				try
				{
					d_bookmark_file.load_from_file(value.get_path());
				}
				catch (FileError e)
				{
					if (bookmarks_from_recent_files)
					{
						// First time create, copy over from recent file manager
						copy_bookmarks_from_recent_files();
					}
				}
				catch (Error e)
				{
					stderr.printf(@"Failed to read repository bookmarks: $(e.message)\n");
				}
			}
		}

		private void copy_bookmarks_from_recent_files()
		{
			var manager = Gtk.RecentManager.get_default();
			var items = manager.get_items();

			foreach (var item in items)
			{
				if (!item.has_group("gitg"))
				{
					continue;
				}

				var uri = item.get_uri();

				d_bookmark_file.set_mime_type(uri, item.get_mime_type());
				d_bookmark_file.set_groups(uri, item.get_groups());
				d_bookmark_file.set_visited(uri, (time_t)item.get_modified());

				var app_name = Environment.get_application_name();
				var app_exec = string.join(" ", Environment.get_prgname(), "%f");

				try { d_bookmark_file.set_app_info(uri, app_name, app_exec, 1, -1); } catch {}
			}

			save_repository_bookmarks_timeout();
		}

		protected override bool button_press_event(Gdk.EventButton event)
		{
			Gdk.Event *ev = (Gdk.Event *)event;

			if (ev->triggers_context_menu() && mode == SelectionMode.NORMAL)
			{
				mode = SelectionMode.SELECTION;

				var row = get_row_at_y((int)event.y) as Row;

				if (row != null)
				{
					row.selected = true;
				}

				return true;
			}

			return base.button_press_event(event);
		}

		protected override void row_activated(Gtk.ListBoxRow row)
		{
			var r = (Row)row;

			if (mode == SelectionMode.SELECTION)
			{
				r.selected = !r.selected;
				return;
			}

			if (r.repository != null)
			{
				repository_activated(r.repository);
			}
		}

		construct
		{
			set_header_func(update_header);
			set_filter_func(filter);
			set_sort_func(compare_widgets);
			show();

			set_selection_mode(Gtk.SelectionMode.NONE);

			d_bookmark_file = new BookmarkFile();
		}

		~RepositoryListBox()
		{
			if (d_save_repository_bookmarks_id != 0)
			{
				Source.remove(d_save_repository_bookmarks_id);
				save_repository_bookmarks();
			}
		}

		private void update_header(Gtk.ListBoxRow row, Gtk.ListBoxRow? before)
		{
			row.set_header(before != null ? new Gtk.Separator(Gtk.Orientation.HORIZONTAL) : null);
		}

		private string normalize(string s)
		{
			return s.normalize(-1, NormalizeMode.ALL).casefold();
		}

		private bool filter(Gtk.ListBoxRow row)
		{
			return d_filter_text != null ? normalize(((Row)row).repository_name).contains(normalize(d_filter_text)) : true;
		}

		private int compare_widgets(Gtk.ListBoxRow a, Gtk.ListBoxRow b)
		{
			return ((Row)b).time.compare(((Row)a).time);
		}

		public void populate_bookmarks()
		{
			var uris = d_bookmark_file.get_uris();

			foreach (var uri in uris)
			{
				try {
					if (!d_bookmark_file.has_group(uri, "gitg"))
					{
						continue;
					}
				} catch { continue; }

				File repo_file = File.new_for_uri(uri);
				Repository repo;

				try
				{
					repo = new Repository(repo_file, null);
				}
				catch
				{
					try
					{
						d_bookmark_file.remove_item(uri);
					} catch {}

					continue;
				}

				DateTime? visited = null;

				try
				{
					visited = new DateTime.from_unix_utc(d_bookmark_file.get_visited(uri));
				} catch {};

				add_repository(repo, visited);
			}
		}

		private Row get_row_for_repository(Repository repository)
		{
			Row? row = null;

			foreach (var child in get_children())
			{
				var d = (Row)child;

				if (d.repository.get_location().equal(repository.get_location()))
				{
					row = d;
					break;
				}
			}

			return row;
		}

		private bool save_repository_bookmarks()
		{
			d_save_repository_bookmarks_id = 0;

			if (location == null)
			{
				return false;
			}

			try
			{
				var dir = location.get_parent();
				dir.make_directory_with_parents(null);
			} catch {}

			try
			{
				d_bookmark_file.to_file(location.get_path());
			}
			catch (Error e)
			{
				stderr.printf(@"Failed to save repository bookmarks: $(e.message)\n");
			}

			return false;
		}

		private void add_repository_to_bookmarks(string uri, DateTime? visited = null)
		{
			d_bookmark_file.set_mime_type(uri, "inode/directory");
			d_bookmark_file.set_groups(uri, new string[] { "gitg" });
			d_bookmark_file.set_visited(uri, visited == null ? -1 : (time_t)visited.to_unix());

			var app_name = Environment.get_application_name();
			var app_exec = string.join(" ", Environment.get_prgname(), "%f");

			try { d_bookmark_file.set_app_info(uri, app_name, app_exec, 1, -1); } catch {}

			save_repository_bookmarks_timeout();
		}

		private void save_repository_bookmarks_timeout()
		{
			if (d_save_repository_bookmarks_id != 0)
			{
				return;
			}

			d_save_repository_bookmarks_id = Timeout.add(300, save_repository_bookmarks);
		}

		public void end_cloning(Row row, Repository? repository)
		{
			if (repository != null)
			{
				File? workdir = repository.get_workdir();
				File? repo_file = repository.get_location();

				var uri = (workdir != null) ? workdir.get_uri() : repo_file.get_uri();
				add_repository_to_bookmarks(uri);

				row.repository = repository;
				row.loading = false;

				connect_repository_row(row);
			}
			else
			{
				remove(row);
			}
		}

		public Row? begin_cloning(File location)
		{
			var row = new Row(null, Utils.replace_home_dir_with_tilde(location.get_parent()));
			row.repository_name = location.get_basename();
			row.branch_name = _("Cloning…");

			row.loading = true;
			row.show();

			add(row);
			return row;
		}

		private void connect_repository_row(Row row)
		{
			var repository = row.repository;
			var workdir = repository.workdir != null ? repository.workdir : repository.location;

			if (workdir != null)
			{
				bind_property("mode", row, "mode");

				row.notify["selected"].connect(() => {
					notify_property("has-selection");
				});

				row.request_remove.connect(() => {
					try
					{
						d_bookmark_file.remove_item(workdir.get_uri());
					} catch {}

					remove(row);
				});

				row.can_remove = true;
			}
			else
			{
				row.can_remove = false;
			}

		}

		public Row? add_repository(Repository repository, DateTime? visited = null)
		{
			Row? row = get_row_for_repository(repository);

			var f = repository.workdir != null ? repository.workdir : repository.location;

			if (row == null)
			{
				var dirname = Utils.replace_home_dir_with_tilde((repository.workdir != null ? repository.workdir : repository.location).get_parent());
				row = new Row(repository, dirname);
				row.show();

				connect_repository_row(row);

				add(row);
			}

			row.time = visited != null ? visited : new DateTime.now_local();
			invalidate_sort();

			if (f != null)
			{
				add_repository_to_bookmarks(f.get_uri(), visited);
			}

			return row;
		}

		public Row[] get_selection()
		{
			var ret = new Row[0];

			foreach (var row in get_children())
			{
				var r = (Row)row;

				if (r.selected)
				{
					ret += r;
				}
			}

			return ret;
		}

		public bool has_selection
		{
			get
			{
				foreach (var row in get_children())
				{
					var r = (Row)row;

					if (r.selected)
					{
						return true;
					}
				}

				return false;
			}
		}

		public void filter_text(string? text)
		{
			d_filter_text = text;

			invalidate_filter();
		}
	}
}

// ex:ts=4 noet