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
|
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace macdoc
{
public enum BookmarkEventType {
Added,
Deleted,
Modified
}
public class BookmarkManagerEventsArgs : EventArgs
{
public BookmarkManager.Entry Entry { get; set; }
public BookmarkEventType EventType { get; set; }
}
public class BookmarkManager
{
public class Entry
{
public string Name { get; set; }
public string Url { get; set; }
public string Notes { get; set; }
}
readonly string storagePath;
List<Entry> bookmarks;
IList<Entry> readonlyVersion;
XmlSerializer serializer = new XmlSerializer (typeof (List<Entry>));
public event EventHandler<BookmarkManagerEventsArgs> BookmarkListChanged;
public BookmarkManager (string storagePath)
{
this.storagePath = storagePath;
LoadBookmarks ();
}
public IList<Entry> GetAllBookmarks ()
{
return readonlyVersion;
}
public void AddBookmark (Entry entry)
{
if (entry == null)
throw new ArgumentNullException ("entry");
RenameIfDuplicated (entry);
bookmarks.Add (entry);
FireChangedEvent (entry, BookmarkEventType.Added);
}
void RenameIfDuplicated (Entry entry)
{
var duplicatedNames = new HashSet<string> (bookmarks.Where (b => b != entry && b.Name.StartsWith (entry.Name)).Select (b => b.Name));
if (duplicatedNames.Count == 0)
return;
var nameTry = entry.Name;
var suffixCount = 1;
while (duplicatedNames.Contains (nameTry))
nameTry = entry.Name + '_' + (suffixCount++);
entry.Name = nameTry;
}
public bool DeleteBookmark (Entry entry)
{
var result = bookmarks.Remove (entry);
if (result)
FireChangedEvent (entry, BookmarkEventType.Deleted);
return result;
}
public bool LoadBookmarks ()
{
var path = Path.Combine (storagePath, "bookmarks.xml");
if (!File.Exists (path)) {
bookmarks = new List<Entry> ();
readonlyVersion = bookmarks.AsReadOnly ();
return false;
}
using (var file = File.OpenRead (path))
bookmarks = (List<Entry>)serializer.Deserialize (file);
// Sanitize input to possibly remove buggy elements (i.e. missing url, name, etc..)
bookmarks = bookmarks.Where (b => b != null && !string.IsNullOrWhiteSpace (b.Name) && !string.IsNullOrWhiteSpace (b.Url)).ToList ();
readonlyVersion = bookmarks.AsReadOnly ();
return true;
}
public void SaveBookmarks ()
{
if (!Directory.Exists (storagePath))
Directory.CreateDirectory (storagePath);
var path = Path.Combine (storagePath, "bookmarks.xml");
try {
using (var file = File.Create (path))
serializer.Serialize (file, bookmarks);
} catch (UnauthorizedAccessException) {}
}
public void CommitBookmarkChange (Entry entry)
{
RenameIfDuplicated (entry);
FireChangedEvent (entry, BookmarkEventType.Modified);
}
public int FindIndexOfBookmarkFromUrl (string url)
{
return bookmarks.FindIndex (entry => entry.Url.Equals (url, StringComparison.InvariantCultureIgnoreCase));
}
void FireChangedEvent (Entry entry, BookmarkEventType evtType)
{
var temp = BookmarkListChanged;
if (temp != null)
temp (this, new BookmarkManagerEventsArgs () { Entry = entry, EventType = evtType });
}
}
}
|