/
Bookmarks.swift
568 lines (472 loc) · 18.5 KB
/
Bookmarks.swift
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import UIKit
import Shared
// A small structure to encapsulate all the possible data that we can get
// from an application sharing a web page or a URL.
public struct ShareItem {
public let url: String
public let title: String?
public let favicon: Favicon?
public init(url: String, title: String?, favicon: Favicon?) {
self.url = url
self.title = title
self.favicon = favicon
}
}
public protocol ShareToDestination {
func shareItem(item: ShareItem)
}
public protocol SearchableBookmarks {
func bookmarksByURL(url: NSURL) -> Deferred<Maybe<Cursor<BookmarkItem>>>
}
public struct BookmarkMirrorItem {
public let guid: GUID
public let type: BookmarkNodeType
let serverModified: Timestamp
let isDeleted: Bool
let hasDupe: Bool
let parentID: GUID?
let parentName: String?
// Livemarks.
public let feedURI: String?
public let siteURI: String?
// Separators.
let pos: Int?
// Folders, livemarks, bookmarks and queries.
let title: String?
let description: String?
// Bookmarks and queries.
let bookmarkURI: String?
let tags: String?
let keyword: String?
// Queries.
let folderName: String?
let queryID: String?
// Folders.
let children: [GUID]?
// The places root is a folder but has no parentName.
public static func folder(guid: GUID, modified: Timestamp, hasDupe: Bool, parentID: GUID, parentName: String?, title: String, description: String?, children: [GUID]) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
let parent = BookmarkRoots.translateIncomingRootGUID(parentID)
return BookmarkMirrorItem(guid: id, type: .Folder, serverModified: modified,
isDeleted: false, hasDupe: hasDupe, parentID: parent, parentName: parentName,
feedURI: nil, siteURI: nil,
pos: nil,
title: title, description: description,
bookmarkURI: nil, tags: nil, keyword: nil,
folderName: nil, queryID: nil,
children: children)
}
public static func livemark(guid: GUID, modified: Timestamp, hasDupe: Bool, parentID: GUID, parentName: String, title: String, description: String?, feedURI: String, siteURI: String) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
let parent = BookmarkRoots.translateIncomingRootGUID(parentID)
return BookmarkMirrorItem(guid: id, type: .Livemark, serverModified: modified,
isDeleted: false, hasDupe: hasDupe, parentID: parent, parentName: parentName,
feedURI: feedURI, siteURI: siteURI,
pos: nil,
title: title, description: description,
bookmarkURI: nil, tags: nil, keyword: nil,
folderName: nil, queryID: nil,
children: nil)
}
public static func separator(guid: GUID, modified: Timestamp, hasDupe: Bool, parentID: GUID, parentName: String, pos: Int) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
let parent = BookmarkRoots.translateIncomingRootGUID(parentID)
return BookmarkMirrorItem(guid: id, type: .Separator, serverModified: modified,
isDeleted: false, hasDupe: hasDupe, parentID: parent, parentName: parentName,
feedURI: nil, siteURI: nil,
pos: pos,
title: nil, description: nil,
bookmarkURI: nil, tags: nil, keyword: nil,
folderName: nil, queryID: nil,
children: nil)
}
public static func bookmark(guid: GUID, modified: Timestamp, hasDupe: Bool, parentID: GUID, parentName: String, title: String, description: String?, URI: String, tags: String, keyword: String?) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
let parent = BookmarkRoots.translateIncomingRootGUID(parentID)
return BookmarkMirrorItem(guid: id, type: .Bookmark, serverModified: modified,
isDeleted: false, hasDupe: hasDupe, parentID: parent, parentName: parentName,
feedURI: nil, siteURI: nil,
pos: nil,
title: title, description: description,
bookmarkURI: URI, tags: tags, keyword: keyword,
folderName: nil, queryID: nil,
children: nil)
}
public static func query(guid: GUID, modified: Timestamp, hasDupe: Bool, parentID: GUID, parentName: String, title: String, description: String?, URI: String, tags: String, keyword: String?, folderName: String?, queryID: String?) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
let parent = BookmarkRoots.translateIncomingRootGUID(parentID)
return BookmarkMirrorItem(guid: id, type: .Query, serverModified: modified,
isDeleted: false, hasDupe: hasDupe, parentID: parent, parentName: parentName,
feedURI: nil, siteURI: nil,
pos: nil,
title: title, description: description,
bookmarkURI: URI, tags: tags, keyword: keyword,
folderName: folderName, queryID: queryID,
children: nil)
}
public static func deleted(type: BookmarkNodeType, guid: GUID, modified: Timestamp) -> BookmarkMirrorItem {
let id = BookmarkRoots.translateIncomingRootGUID(guid)
return BookmarkMirrorItem(guid: id, type: type, serverModified: modified,
isDeleted: true, hasDupe: false, parentID: nil, parentName: nil,
feedURI: nil, siteURI: nil,
pos: nil,
title: nil, description: nil,
bookmarkURI: nil, tags: nil, keyword: nil,
folderName: nil, queryID: nil,
children: nil)
}
}
public protocol BookmarkMirrorStorage {
func applyRecords(records: [BookmarkMirrorItem]) -> Success
}
public struct BookmarkRoots {
// These match Places on desktop.
public static let RootGUID = "root________"
public static let MobileFolderGUID = "mobile______"
public static let MenuFolderGUID = "menu________"
public static let ToolbarFolderGUID = "toolbar_____"
public static let UnfiledFolderGUID = "unfiled_____"
public static let FakeDesktopFolderGUID = "desktop_____" // Pseudo. Never mentioned in a real record.
/**
* Sync records are a horrible mess of Places-native GUIDs and Sync-native IDs.
* For example:
* {"id":"places",
* "type":"folder",
* "title":"",
* "description":null,
* "children":["menu________","toolbar_____",
* "tags________","unfiled_____",
* "jKnyPDrBQSDg","T6XK5oJMU8ih"],
* "parentid":"2hYxKgBwvkEH"}"
*
* We thus normalize on the extended Places IDs (with underscores) for
* local storage, and translate to the Sync IDs when creating an outbound
* record.
* We translate the record's ID and also its parent. Evidence suggests that
* we don't need to translate children IDs.
*
* TODO: We don't create outbound records yet, so that's why there's no
* translation in that direction yet!
*/
public static func translateIncomingRootGUID(guid: GUID) -> GUID {
return [
"places": RootGUID,
"root": RootGUID,
"mobile": MobileFolderGUID,
"menu": MenuFolderGUID,
"toolbar": ToolbarFolderGUID,
"unfiled": UnfiledFolderGUID
][guid] ?? guid
}
/*
public static let TagsFolderGUID = "tags________"
public static let PinnedFolderGUID = "pinned______"
*/
static let RootID = 0
static let MobileID = 1
static let MenuID = 2
static let ToolbarID = 3
static let UnfiledID = 4
}
/**
* This partly matches Places's nsINavBookmarksService, just for sanity.
*
* It is further extended to support the types that exist in Sync, so we can use
* this to store mirrored rows.
*
* These are only used at the DB layer.
*/
public enum BookmarkNodeType: Int {
case Bookmark = 1
case Folder = 2
case Separator = 3
case DynamicContainer = 4
case Livemark = 5
case Query = 6
// No microsummary: those turn into bookmarks.
}
/**
* The immutable base interface for bookmarks and folders.
*/
public class BookmarkNode {
public var id: Int? = nil
public var guid: String
public var title: String
public var favicon: Favicon? = nil
init(guid: String, title: String) {
self.guid = guid
self.title = title
}
}
/**
* An immutable item representing a bookmark.
*
* To modify this, issue changes against the backing store and get an updated model.
*/
public class BookmarkItem: BookmarkNode {
public let url: String!
public init(guid: String, title: String, url: String) {
self.url = url
super.init(guid: guid, title: title)
}
}
/**
* A folder is an immutable abstraction over a named
* thing that can return its child nodes by index.
*/
public class BookmarkFolder: BookmarkNode {
public var count: Int { return 0 }
public subscript(index: Int) -> BookmarkNode? { return nil }
public func itemIsEditableAtIndex(index: Int) -> Bool {
return false
}
}
/**
* A model is a snapshot of the bookmarks store, suitable for backing a table view.
*
* Navigation through the folder hierarchy produces a sequence of models.
*
* Changes to the backing store implicitly invalidates a subset of models.
*
* 'Refresh' means requesting a new model from the store.
*/
public class BookmarksModel {
let modelFactory: BookmarksModelFactory
public let current: BookmarkFolder
public init(modelFactory: BookmarksModelFactory, root: BookmarkFolder) {
self.modelFactory = modelFactory
self.current = root
}
/**
* Produce a new model rooted at the appropriate folder. Fails if the folder doesn't exist.
*/
public func selectFolder(folder: BookmarkFolder) -> Deferred<Maybe<BookmarksModel>> {
return modelFactory.modelForFolder(folder)
}
/**
* Produce a new model rooted at the appropriate folder. Fails if the folder doesn't exist.
*/
public func selectFolder(guid: String) -> Deferred<Maybe<BookmarksModel>> {
return modelFactory.modelForFolder(guid)
}
/**
* Produce a new model rooted at the base of the hierarchy. Should never fail.
*/
public func selectRoot() -> Deferred<Maybe<BookmarksModel>> {
return modelFactory.modelForRoot()
}
/**
* Produce a new model rooted at the same place as this model. Can fail if
* the folder has been deleted from the backing store.
*/
public func reloadData() -> Deferred<Maybe<BookmarksModel>> {
return modelFactory.modelForFolder(current)
}
}
public protocol BookmarksModelFactory {
func modelForFolder(folder: BookmarkFolder) -> Deferred<Maybe<BookmarksModel>>
func modelForFolder(guid: GUID) -> Deferred<Maybe<BookmarksModel>>
func modelForFolder(guid: GUID, title: String) -> Deferred<Maybe<BookmarksModel>>
func modelForRoot() -> Deferred<Maybe<BookmarksModel>>
// Whenever async construction is necessary, we fall into a pattern of needing
// a placeholder that behaves correctly for the period between kickoff and set.
var nullModel: BookmarksModel { get }
func isBookmarked(url: String) -> Deferred<Maybe<Bool>>
func remove(bookmark: BookmarkNode) -> Success
func removeByURL(url: String) -> Success
func clearBookmarks() -> Success
}
/*
* A folder that contains an array of children.
*/
public class MemoryBookmarkFolder: BookmarkFolder, SequenceType {
let children: [BookmarkNode]
public init(guid: GUID, title: String, children: [BookmarkNode]) {
self.children = children
super.init(guid: guid, title: title)
}
public struct BookmarkNodeGenerator: GeneratorType {
public typealias Element = BookmarkNode
let children: [BookmarkNode]
var index: Int = 0
init(children: [BookmarkNode]) {
self.children = children
}
public mutating func next() -> BookmarkNode? {
return index < children.count ? children[index++] : nil
}
}
override public var favicon: Favicon? {
get {
if let path = NSBundle.mainBundle().pathForResource("bookmarkFolder", ofType: "png") {
let url = NSURL(fileURLWithPath: path)
return Favicon(url: url.absoluteString, date: NSDate(), type: IconType.Local)
}
return nil
}
set {
}
}
override public var count: Int {
return children.count
}
override public subscript(index: Int) -> BookmarkNode {
get {
return children[index]
}
}
override public func itemIsEditableAtIndex(index: Int) -> Bool {
return true
}
public func generate() -> BookmarkNodeGenerator {
return BookmarkNodeGenerator(children: self.children)
}
/**
* Return a new immutable folder that's just like this one,
* but also contains the new items.
*/
func append(items: [BookmarkNode]) -> MemoryBookmarkFolder {
if (items.isEmpty) {
return self
}
return MemoryBookmarkFolder(guid: self.guid, title: self.title, children: self.children + items)
}
}
public class MemoryBookmarksSink: ShareToDestination {
var queue: [BookmarkNode] = []
public init() { }
public func shareItem(item: ShareItem) {
let title = item.title == nil ? "Untitled" : item.title!
func exists(e: BookmarkNode) -> Bool {
if let bookmark = e as? BookmarkItem {
return bookmark.url == item.url;
}
return false;
}
// Don't create duplicates.
if (!queue.contains(exists)) {
queue.append(BookmarkItem(guid: Bytes.generateGUID(), title: title, url: item.url))
}
}
}
private extension SuggestedSite {
func asBookmark() -> BookmarkNode {
let b = BookmarkItem(guid: self.guid ?? Bytes.generateGUID(), title: self.title, url: self.url)
b.favicon = self.icon
return b
}
}
public class PrependedBookmarkFolder: BookmarkFolder {
private let main: BookmarkFolder
private let prepend: BookmarkNode
init(main: BookmarkFolder, prepend: BookmarkNode) {
self.main = main
self.prepend = prepend
super.init(guid: main.guid, title: main.guid)
}
override public var count: Int {
return self.main.count + 1
}
override public subscript(index: Int) -> BookmarkNode? {
if index == 0 {
return self.prepend
}
return self.main[index - 1]
}
override public func itemIsEditableAtIndex(index: Int) -> Bool {
return index > 0 && self.main.itemIsEditableAtIndex(index - 1)
}
}
public class BookmarkFolderWithDefaults: BookmarkFolder {
private let folder: BookmarkFolder
private let sites: SuggestedSitesCursor
init(folder: BookmarkFolder, sites: SuggestedSitesCursor) {
self.folder = folder
self.sites = sites
super.init(guid: folder.guid, title: folder.title)
}
override public var count: Int {
return self.folder.count + self.sites.count
}
override public subscript(index: Int) -> BookmarkNode? {
if index < self.folder.count {
return self.folder[index]
}
if let site = self.sites[index - self.folder.count] {
return site.asBookmark()
}
return nil
}
override public func itemIsEditableAtIndex(index: Int) -> Bool {
return index < self.folder.count
}
}
/**
* A trivial offline model factory that represents a simple hierarchy.
*/
public class MockMemoryBookmarksStore: BookmarksModelFactory, ShareToDestination {
let mobile: MemoryBookmarkFolder
let root: MemoryBookmarkFolder
var unsorted: MemoryBookmarkFolder
let sink: MemoryBookmarksSink
public init() {
let res = [BookmarkItem]()
mobile = MemoryBookmarkFolder(guid: BookmarkRoots.MobileFolderGUID, title: "Mobile Bookmarks", children: res)
unsorted = MemoryBookmarkFolder(guid: BookmarkRoots.UnfiledFolderGUID, title: "Unsorted Bookmarks", children: [])
sink = MemoryBookmarksSink()
root = MemoryBookmarkFolder(guid: BookmarkRoots.RootGUID, title: "Root", children: [mobile, unsorted])
}
public func modelForFolder(folder: BookmarkFolder) -> Deferred<Maybe<BookmarksModel>> {
return self.modelForFolder(folder.guid, title: folder.title)
}
public func modelForFolder(guid: GUID) -> Deferred<Maybe<BookmarksModel>> {
return self.modelForFolder(guid, title: "")
}
public func modelForFolder(guid: GUID, title: String) -> Deferred<Maybe<BookmarksModel>> {
var m: BookmarkFolder
switch (guid) {
case BookmarkRoots.MobileFolderGUID:
// Transparently merges in any queued items.
m = self.mobile.append(self.sink.queue)
break;
case BookmarkRoots.RootGUID:
m = self.root
break;
case BookmarkRoots.UnfiledFolderGUID:
m = self.unsorted
break;
default:
return deferMaybe(DatabaseError(description: "No such folder \(guid)."))
}
return deferMaybe(BookmarksModel(modelFactory: self, root: m))
}
public func modelForRoot() -> Deferred<Maybe<BookmarksModel>> {
return deferMaybe(BookmarksModel(modelFactory: self, root: self.root))
}
/**
* This class could return the full data immediately. We don't, because real DB-backed code won't.
*/
public var nullModel: BookmarksModel {
let f = MemoryBookmarkFolder(guid: BookmarkRoots.RootGUID, title: "Root", children: [])
return BookmarksModel(modelFactory: self, root: f)
}
public func shareItem(item: ShareItem) {
self.sink.shareItem(item)
}
public func isBookmarked(url: String) -> Deferred<Maybe<Bool>> {
return deferMaybe(DatabaseError(description: "Not implemented"))
}
public func remove(bookmark: BookmarkNode) -> Success {
return deferMaybe(DatabaseError(description: "Not implemented"))
}
public func removeByURL(url: String) -> Success {
return deferMaybe(DatabaseError(description: "Not implemented"))
}
public func clearBookmarks() -> Success {
return succeed()
}
}