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
|
/***
Copyright (c) 1999, 2000 Eazel, Inc.
2015-2017 elementary LLC (http://launchpad.net/elementary)
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License version 3, as published
by the Free Software Foundation, Inc.,.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
Authors : John Sullivan <sullivan@eazel.com>
ammonkey <am.monkeyd@gmail.com>
Jeremy Wootten <jeremy@elementaryos.org>
***/
namespace Marlin {
public class BookmarkList : GLib.Object{
enum JobType {
LOAD = 1,
SAVE = 2
}
public unowned GLib.List<Marlin.Bookmark> list { get; private set; }
private GLib.FileMonitor monitor;
private GLib.Queue<JobType> pending_ops;
private static GLib.File bookmarks_file;
private GOF.CallWhenReady call_when_ready;
private static BookmarkList instance = null;
public signal void contents_changed ();
public signal void deleted ();
private BookmarkList () {
list = new GLib.List<Marlin.Bookmark> ();
pending_ops = new GLib.Queue<JobType> ();
/* Get the user config directory
* When running under pkexec determine real user from PKEXEC_UID
*/
string? user_home = Eel.get_real_user_home ();
string config_dir;
if (user_home != null) {
config_dir = GLib.Path.build_filename (user_home, ".config");
} else {
config_dir = GLib.Environment.get_user_config_dir ();
}
/*Check bookmarks file exists and in right place */
string filename = GLib.Path.build_filename (config_dir,
"gtk-3.0",
"bookmarks",
null);
var file = GLib.File.new_for_path (filename);
if (!file.query_exists (null)) {
/* Bookmarks file does not exist in right place ... create a new one */
try {
file.get_parent ().make_directory_with_parents (null);
}
catch (GLib.Error error) {
/* Probably already exists */
warning ("Could not create bookmarks directory: %s", error.message);
}
try {
file.create (GLib.FileCreateFlags.NONE, null);
}
catch (GLib.Error error){
critical ("Could not create bookmarks file: %s", error.message);
}
/* load existing bookmarks from the old location if it exists */
var old_filename = GLib.Path.build_filename (GLib.Environment.get_home_dir (),
".gtk-bookmarks",
null);
var old_file = GLib.File.new_for_path (old_filename);
if (old_file.query_exists (null)) {
/* If there is a legacy bookmark file we copy it to the new location */
Marlin.BookmarkList.bookmarks_file = old_file;
load_bookmarks_file ();
Marlin.BookmarkList.bookmarks_file = file;
} else {
/* Else populate the new file with default bookmarks */
Marlin.BookmarkList.bookmarks_file = file;
add_special_directories ();
}
save_bookmarks_file ();
} else {
Marlin.BookmarkList.bookmarks_file = file;
load_bookmarks_file ();
}
}
private void add_special_directory (GLib.UserDirectory user_dir, ref GLib.List<string> uris) {
string? dir_s = Environment.get_user_special_dir (user_dir);
if (dir_s != null)
uris.prepend ("file://" + dir_s);
}
private void add_special_directories () {
GLib.List<string> uris = null;
add_special_directory (UserDirectory.DOCUMENTS, ref uris);
add_special_directory (UserDirectory.DOWNLOAD, ref uris);
add_special_directory (UserDirectory.MUSIC, ref uris);
add_special_directory (UserDirectory.PUBLIC_SHARE, ref uris);
add_special_directory (UserDirectory.PICTURES, ref uris);
add_special_directory (UserDirectory.TEMPLATES, ref uris);
add_special_directory (UserDirectory.VIDEOS, ref uris);
uris.reverse ();
insert_uris_at_end (uris);
}
public static BookmarkList get_instance () {
if (instance == null)
instance = new BookmarkList ();
return instance;
}
public void insert_uri (string uri, uint index, string? label = null) {
insert_item_internal (new Bookmark.from_uri (uri, label), index);
save_bookmarks_file ();
}
public void insert_uri_at_end (string uri, string? label = null) {
append_internal (new Bookmark.from_uri (uri, label));
save_bookmarks_file ();
}
public void insert_uris (GLib.List<string> uris, uint index) {
if (index > list.length ()) {
critical ("Bookmarklist: Attempt to insert uri at out of range index");
return;
}
uris.@foreach ((uri) => {
insert_item_internal (new Bookmark.from_uri (uri, null), index);
index++;
});
save_bookmarks_file ();
}
public void insert_uris_at_end (GLib.List<string> uris) {
uris.@foreach ((uri) => {
append_internal (new Bookmark.from_uri (uri, null));
});
save_bookmarks_file ();
}
public bool contains (Marlin.Bookmark bm) {
return (list.find_custom (bm, Marlin.Bookmark.compare_with) != null);
}
public void delete_item_at (uint index) {
assert (index < list.length ());
unowned GLib.List<Marlin.Bookmark> node = list.nth (index);
list.remove_link (node);
stop_monitoring_bookmark (node.data);
save_bookmarks_file ();
}
public void delete_items_with_uri (string uri) {
bool list_changed = false;
unowned GLib.List<Marlin.Bookmark> node = list;
unowned GLib.List<Marlin.Bookmark> next = node.next;
for (node = list; node != null; node = next) {
next = node.next;
if (uri == node.data.get_uri ()) {
list.remove_link (node);
stop_monitoring_bookmark (node.data);
list_changed = true;
}
}
if (list_changed)
save_bookmarks_file ();
}
public uint length () {
return list.length ();
}
public unowned Marlin.Bookmark? item_at (uint index) {
assert (index < list.length ());
return list.nth_data (index);
}
public void move_item (uint index, uint destination) {
if (index > list.length ()) {
critical ("Bookmarklist: Attempt to move bookmark from out of range index");
return;
}
if (destination > list.length ()) {
critical ("Bookmarklist: Attempt to move bookmark to out of range index");
return;
}
if (index == destination) {
return;
}
unowned GLib.List<Marlin.Bookmark> link = list.nth (index);
list.remove_link (link);
list.insert (link.data, (int)destination);
save_bookmarks_file ();
}
private void append_internal (Marlin.Bookmark bookmark) {
insert_item_internal (bookmark,-1);
}
private void insert_item_internal (Marlin.Bookmark bm, uint index) {
if (this.contains (bm)) {
return;
}
/* Do not insert bookmark for home or filesystem root (already have builtins) */
var path = bm.gof_file.location.get_path ();
if ((path == Eel.get_real_user_home () || path == Path.DIR_SEPARATOR_S)) {
return;
}
list.insert (bm, (int)index);
start_monitoring_bookmark (bm);
}
private void load_bookmarks_file () {
schedule_job (JobType.LOAD);
}
private void save_bookmarks_file () {
schedule_job (JobType.SAVE);
}
private void schedule_job (JobType job) {
pending_ops.push_head (job);
if (pending_ops.length == 1)
process_next_op ();
}
private void load_bookmarks_file_async () {
GLib.File file = get_bookmarks_file ();
file.load_contents_async.begin (null, (obj, res) => {
try {
uint8[] contents;
file.load_contents_async.end (res, out contents, null);
if (contents != null) {
bookmark_list_from_string ((string)contents);
this.call_when_ready = new GOF.CallWhenReady (get_gof_file_list (), files_ready);
contents_changed (); /* Call now to ensure sidebar is updated even if call_when_ready blocks */
}
}
catch (GLib.Error error) {
critical ("Error loadinging bookmark file %s", error.message);
}
op_processed_call_back ();
});
}
private unowned GLib.List<GOF.File> get_gof_file_list () {
unowned GLib.List<GOF.File> files = null;
list.@foreach ((bm) => {
files.prepend (bm.gof_file);
});
return files;
}
private void files_ready (GLib.List<GOF.File> files) {
/* Sidebar does not use file.info when updating display so do not signal contents changed */
call_when_ready = null;
}
private void bookmark_list_from_string (string contents) {
list.@foreach (stop_monitoring_bookmark);
uint count = 0;
string [] lines = contents.split ("\n");
foreach (string line in lines) {
if (line[0] == '\0' || line[0] == ' ')
continue; /* ignore blank lines */
string [] parts = line.split (" ", 2);
if (parts.length == 2)
append_internal (new Marlin.Bookmark.from_uri (parts [0], parts [1]));
else
append_internal (new Marlin.Bookmark.from_uri (parts [0]));
count++;
}
list.@foreach (start_monitoring_bookmark);
if (list.length () > count)
/* renew bookmark that was deleted when bookmarks file was changed externally */
save_bookmarks_file ();
}
private void save_bookmarks_file_async () {
GLib.File file = get_bookmarks_file ();
StringBuilder sb = new StringBuilder ();
list.@foreach ((bookmark) => {
sb.append (bookmark.get_uri ());
sb.append (" " + bookmark.label);
sb.append ("\n");
});
file.replace_contents_async.begin (sb.data,
null,
false,
GLib.FileCreateFlags.NONE,
null,
(obj, res) => {
try {
file.replace_contents_async.end (res, null);
contents_changed ();
}
catch (GLib.Error error) {
warning ("Error replacing bookmarks file contents %s", error.message);
}
op_processed_call_back ();
});
}
private static GLib.File get_bookmarks_file () {
return Marlin.BookmarkList.bookmarks_file;
}
private void bookmarks_file_changed_call_back (GLib.File file,
GLib.File? other_file,
GLib.FileMonitorEvent event_type) {
if (event_type == GLib.FileMonitorEvent.CHANGED
|| event_type == GLib.FileMonitorEvent.CREATED)
load_bookmarks_file ();
}
private void bookmark_in_list_changed_callback (Marlin.Bookmark bookmark) {
save_bookmarks_file ();
}
private void bookmark_in_list_to_be_deleted_callback (Marlin.Bookmark bookmark) {
delete_items_with_uri (bookmark.get_uri ());
}
private void start_monitoring_bookmarks_file () {
GLib.File file = get_bookmarks_file ();
try {
monitor = file.monitor (GLib.FileMonitorFlags.SEND_MOVED, null);
monitor.set_rate_limit (1000);
monitor.changed.connect (bookmarks_file_changed_call_back);
}
catch (GLib.Error error) {
warning ("Error starting to monitor bookmarks file: %s", error.message);
}
}
private void stop_monitoring_bookmarks_file () {
if (monitor == null)
return;
monitor.cancel ();
monitor.changed.disconnect (bookmarks_file_changed_call_back);
monitor = null;
}
private void start_monitoring_bookmark (Marlin.Bookmark bookmark) {
bookmark.contents_changed.connect (bookmark_in_list_changed_callback);
bookmark.deleted.connect (bookmark_in_list_to_be_deleted_callback);
}
private void stop_monitoring_bookmark (Marlin.Bookmark bookmark) {
bookmark.contents_changed.disconnect (bookmark_in_list_changed_callback);
bookmark.deleted.disconnect (bookmark_in_list_to_be_deleted_callback);
}
private void process_next_op () {
stop_monitoring_bookmarks_file ();
var pending = pending_ops.pop_tail ();
/* if job is SAVE then subsequent pending saves and loads are redundant
* if job is LOAD then any pending changes requiring saving will be lost
* so we can clear pending jobs */
pending_ops.clear();
/* block queue until job processed */
pending_ops.push_head (pending);
switch (pending) {
case JobType.LOAD:
load_bookmarks_file_async ();
break;
case JobType.SAVE:
save_bookmarks_file_async ();
break;
default:
warning (@"Invalid booklist operation");
op_processed_call_back ();
break;
}
}
private void op_processed_call_back () {
pending_ops.pop_tail (); /* remove job just completed */
if (!pending_ops.is_empty ())
process_next_op ();
else
start_monitoring_bookmarks_file ();
}
}
}
|