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
|
/* Copyright 2016 Software Freedom Conservancy Inc.
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
public class Folders.Branch : Sidebar.Branch {
private Gee.HashMap<File, Folders.SidebarEntry> entries =
new Gee.HashMap<File, Folders.SidebarEntry>(file_hash, file_equal);
private File home_dir;
public Branch() {
base (new Folders.Root(),
Sidebar.Branch.Options.STARTUP_OPEN_GROUPING
| Sidebar.Branch.Options.HIDE_IF_EMPTY,
comparator);
home_dir = File.new_for_path(Environment.get_home_dir());
foreach (MediaSourceCollection sources in MediaCollectionRegistry.get_instance().get_all()) {
// seed
on_media_contents_altered(sources.get_all(), null);
// monitor
sources.contents_altered.connect(on_media_contents_altered);
}
}
~Branch() {
foreach (MediaSourceCollection sources in MediaCollectionRegistry.get_instance().get_all())
sources.contents_altered.disconnect(on_media_contents_altered);
}
private static int comparator(Sidebar.Entry a, Sidebar.Entry b) {
if (a == b)
return 0;
int coll_key_equality = strcmp(((Folders.SidebarEntry) a).collation,
((Folders.SidebarEntry) b).collation);
if (coll_key_equality == 0) {
// Collation keys were the same, double-check that
// these really are the same string...
return strcmp(((Folders.SidebarEntry) a).get_sidebar_name(),
((Folders.SidebarEntry) b).get_sidebar_name());
}
return coll_key_equality;
}
private void on_master_source_replaced(MediaSource media_source, File old_file, File new_file) {
remove_entry(old_file);
add_entry(media_source);
}
private void on_media_contents_altered(Gee.Iterable<DataObject>? added, Gee.Iterable<DataObject>? removed) {
if (added != null) {
foreach (DataObject object in added) {
add_entry((MediaSource) object);
((MediaSource) object).master_replaced.connect(on_master_source_replaced);
}
}
if (removed != null) {
foreach (DataObject object in removed) {
remove_entry(((MediaSource) object).get_file());
((MediaSource) object).master_replaced.disconnect(on_master_source_replaced);
}
}
}
void add_entry(MediaSource media) {
File file = media.get_file();
Gee.ArrayList<File> elements = new Gee.ArrayList<File>();
// add the path elements in reverse order up to home directory
File? parent = file.get_parent();
while (parent != null && parent.get_path() != null) {
// don't process paths above the user's home directory
if (parent.equal(home_dir.get_parent()))
break;
elements.add(parent);
parent = parent.get_parent();
}
// walk path elements in order from home directory down, building needed sidebar entries
// along the way
Folders.SidebarEntry? parent_entry = null;
for (int ctr = elements.size - 1; ctr >= 0; ctr--) {
File parent_dir = elements[ctr];
// save current parent, needed if this entry needs to be grafted
Folders.SidebarEntry? old_parent_entry = parent_entry;
parent_entry = entries.get(parent_dir);
if (parent_entry == null) {
parent_entry = new Folders.SidebarEntry(parent_dir);
entries.set(parent_dir, parent_entry);
graft((old_parent_entry == null) ? get_root() : old_parent_entry, parent_entry);
}
// only increment entry's file count if File is going in this folder
if (ctr == 0)
parent_entry.count++;
}
}
private void remove_entry(File file) {
Folders.SidebarEntry? folder_entry = entries.get(file.get_parent());
if (folder_entry == null)
return;
assert(folder_entry.count > 0);
// decrement file count for folder of photo
if (--folder_entry.count > 0 || get_child_count(folder_entry) > 0)
return;
// empty folder so prune tree
Folders.SidebarEntry? prune_point = folder_entry;
assert(prune_point != null);
for (;;) {
bool removed = entries.unset(prune_point.dir);
assert(removed);
Folders.SidebarEntry? parent = get_parent(prune_point) as Folders.SidebarEntry;
if (parent == null || parent.count != 0 || get_child_count(parent) > 1)
break;
prune_point = parent;
}
prune(prune_point);
}
}
private class Folders.Root : Sidebar.Header {
public Root() {
base (_("Folders"), _("Browse the library’s folder structure"));
}
}
public class Folders.SidebarEntry : Sidebar.SimplePageEntry, Sidebar.ExpandableEntry {
public File dir { get; private set; }
public string collation { get; private set; }
private int _count = 0;
public int count {
get {
return _count;
}
set {
int prev_count = _count;
_count = value;
// when count change 0->1 and 1->0 may need refresh icon
if ((prev_count == 0 && _count == 1) || (prev_count == 1 && _count == 0))
sidebar_icon_changed(get_sidebar_icon());
}
}
public SidebarEntry(File dir) {
this.dir = dir;
collation = to_string().collate_key_for_filename();
}
public override string get_sidebar_name() {
try {
return dir.query_info(FileAttribute.STANDARD_DISPLAY_NAME, FileQueryInfoFlags.NONE, null).get_display_name();
} catch (Error err) {
return this.to_string();
}
}
public override string? get_sidebar_icon() {
return count == 0 ? icon : have_photos_icon;
}
public override string to_string() {
return Filename.display_name(dir.get_path());
}
public bool expand_on_select() {
return true;
}
protected override global::Page create_page() {
return new Folders.Page(dir);
}
}
|