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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/ForgetAboutSite.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm");
// XXXmano: we should move most/all of these constants to PlacesUtils
const ORGANIZER_ROOT_BOOKMARKS = "place:folder=BOOKMARKS_MENU&excludeItems=1&queryType=1";
// No change to the view, preserve current selection
const RELOAD_ACTION_NOTHING = 0;
// Inserting items new to the view, select the inserted rows
const RELOAD_ACTION_INSERT = 1;
// Removing items from the view, select the first item after the last selected
const RELOAD_ACTION_REMOVE = 2;
// Moving items within a view, don't treat the dropped items as additional
// rows.
const RELOAD_ACTION_MOVE = 3;
// When removing a bunch of pages we split them in chunks to give some breath
// to the main-thread.
const REMOVE_PAGES_CHUNKLEN = 300;
/**
* Represents an insertion point within a container where we can insert
* items.
* @param aItemId
* The identifier of the parent container
* @param aIndex
* The index within the container where we should insert
* @param aOrientation
* The orientation of the insertion. NOTE: the adjustments to the
* insertion point to accommodate the orientation should be done by
* the person who constructs the IP, not the user. The orientation
* is provided for informational purposes only!
* @param [optional] aIsTag
* Indicates if parent container is a tag
* @param [optional] aDropNearItemId
* When defined we will calculate index based on this itemId
* @constructor
*/
function InsertionPoint(aItemId, aIndex, aOrientation, aIsTag,
aDropNearItemId) {
this.itemId = aItemId;
this._index = aIndex;
this.orientation = aOrientation;
this.isTag = aIsTag;
this.dropNearItemId = aDropNearItemId;
}
InsertionPoint.prototype = {
set index(val) {
return this._index = val;
},
get index() {
if (this.dropNearItemId > 0) {
// If dropNearItemId is set up we must calculate the real index of
// the item near which we will drop.
var index = PlacesUtils.bookmarks.getItemIndex(this.dropNearItemId);
return this.orientation == Ci.nsITreeView.DROP_BEFORE ? index : index + 1;
}
return this._index;
}
};
/**
* Places Controller
*/
function PlacesController(aView) {
this._view = aView;
XPCOMUtils.defineLazyServiceGetter(this, "clipboard",
"@mozilla.org/widget/clipboard;1",
"nsIClipboard");
XPCOMUtils.defineLazyGetter(this, "profileName", function () {
return Services.dirsvc.get("ProfD", Ci.nsIFile).leafName;
});
this._cachedLivemarkInfoObjects = new Map();
}
PlacesController.prototype = {
/**
* The places view.
*/
_view: null,
QueryInterface: XPCOMUtils.generateQI([
Ci.nsIClipboardOwner
]),
// nsIClipboardOwner
LosingOwnership: function PC_LosingOwnership (aXferable) {
this.cutNodes = [];
},
terminate: function PC_terminate() {
this._releaseClipboardOwnership();
},
supportsCommand: function PC_supportsCommand(aCommand) {
// Non-Places specific commands that we also support
switch (aCommand) {
case "cmd_undo":
case "cmd_redo":
case "cmd_cut":
case "cmd_copy":
case "cmd_paste":
case "cmd_delete":
case "cmd_selectAll":
return true;
}
// All other Places Commands are prefixed with "placesCmd_" ... this
// filters out other commands that we do _not_ support (see 329587).
const CMD_PREFIX = "placesCmd_";
return (aCommand.substr(0, CMD_PREFIX.length) == CMD_PREFIX);
},
isCommandEnabled: function PC_isCommandEnabled(aCommand) {
switch (aCommand) {
case "cmd_undo":
return PlacesUtils.transactionManager.numberOfUndoItems > 0;
case "cmd_redo":
return PlacesUtils.transactionManager.numberOfRedoItems > 0;
case "cmd_cut":
case "placesCmd_cut":
case "placesCmd_moveBookmarks":
for (let node of this._view.selectedNodes) {
// If selection includes history nodes or tags-as-bookmark, disallow
// cutting.
if (node.itemId == -1 ||
(node.parent && PlacesUtils.nodeIsTagQuery(node.parent))) {
return false;
}
}
// Otherwise fall through to the cmd_delete check.
case "cmd_delete":
case "placesCmd_delete":
case "placesCmd_deleteDataHost":
return this._hasRemovableSelection();
case "cmd_copy":
case "placesCmd_copy":
return this._view.hasSelection;
case "cmd_paste":
case "placesCmd_paste":
return this._canInsert(true) && this._isClipboardDataPasteable();
case "cmd_selectAll":
if (this._view.selType != "single") {
let rootNode = this._view.result.root;
if (rootNode.containerOpen && rootNode.childCount > 0)
return true;
}
return false;
case "placesCmd_open":
case "placesCmd_open:window":
case "placesCmd_open:privatewindow":
case "placesCmd_open:tab":
var selectedNode = this._view.selectedNode;
return selectedNode && PlacesUtils.nodeIsURI(selectedNode);
case "placesCmd_new:folder":
case "placesCmd_new:livemark":
return this._canInsert();
case "placesCmd_new:bookmark":
return this._canInsert();
case "placesCmd_new:separator":
return this._canInsert() &&
!PlacesUtils.asQuery(this._view.result.root).queryOptions.excludeItems &&
this._view.result.sortingMode ==
Ci.nsINavHistoryQueryOptions.SORT_BY_NONE;
case "placesCmd_show:info":
var selectedNode = this._view.selectedNode;
return selectedNode && PlacesUtils.getConcreteItemId(selectedNode) != -1
case "placesCmd_reload":
// Livemark containers
var selectedNode = this._view.selectedNode;
return selectedNode && this.hasCachedLivemarkInfo(selectedNode);
case "placesCmd_sortBy:name":
var selectedNode = this._view.selectedNode;
return selectedNode &&
PlacesUtils.nodeIsFolder(selectedNode) &&
!PlacesUIUtils.isContentsReadOnly(selectedNode) &&
this._view.result.sortingMode ==
Ci.nsINavHistoryQueryOptions.SORT_BY_NONE;
case "placesCmd_createBookmark":
var node = this._view.selectedNode;
return node && PlacesUtils.nodeIsURI(node) && node.itemId == -1;
case "placesCmd_openParentFolder":
return true;
default:
return false;
}
},
doCommand: function PC_doCommand(aCommand) {
switch (aCommand) {
case "cmd_undo":
PlacesUtils.transactionManager.undoTransaction();
break;
case "cmd_redo":
PlacesUtils.transactionManager.redoTransaction();
break;
case "cmd_cut":
case "placesCmd_cut":
this.cut();
break;
case "cmd_copy":
case "placesCmd_copy":
this.copy();
break;
case "cmd_paste":
case "placesCmd_paste":
this.paste();
break;
case "cmd_delete":
case "placesCmd_delete":
this.remove("Remove Selection");
break;
case "placesCmd_deleteDataHost":
var host;
if (PlacesUtils.nodeIsHost(this._view.selectedNode)) {
var queries = this._view.selectedNode.getQueries();
host = queries[0].domain;
}
else
host = NetUtil.newURI(this._view.selectedNode.uri).host;
ForgetAboutSite.removeDataFromDomain(host)
.catch(Components.utils.reportError);
break;
case "cmd_selectAll":
this.selectAll();
break;
case "placesCmd_open":
PlacesUIUtils.openNodeIn(this._view.selectedNode, "current", this._view);
break;
case "placesCmd_open:window":
PlacesUIUtils.openNodeIn(this._view.selectedNode, "window", this._view);
break;
case "placesCmd_open:privatewindow":
PlacesUIUtils.openNodeIn(this._view.selectedNode, "window", this._view, true);
break;
case "placesCmd_open:tab":
PlacesUIUtils.openNodeIn(this._view.selectedNode, "tab", this._view);
break;
case "placesCmd_new:folder":
this.newItem("folder");
break;
case "placesCmd_new:bookmark":
this.newItem("bookmark");
break;
case "placesCmd_new:livemark":
this.newItem("livemark");
break;
case "placesCmd_new:separator":
this.newSeparator();
break;
case "placesCmd_show:info":
this.showBookmarkPropertiesForSelection();
break;
case "placesCmd_moveBookmarks":
this.moveSelectedBookmarks();
break;
case "placesCmd_reload":
this.reloadSelectedLivemark();
break;
case "placesCmd_sortBy:name":
this.sortFolderByName();
break;
case "placesCmd_createBookmark":
let node = this._view.selectedNode;
PlacesUIUtils.showBookmarkDialog({ action: "add"
, type: "bookmark"
, hiddenRows: [ "description"
, "keyword"
, "location"
, "loadInSidebar" ]
, uri: NetUtil.newURI(node.uri)
, title: node.title
}, window.top);
break;
case "placesCmd_openParentFolder":
this.openParentFolder();
break;
}
},
onEvent: function PC_onEvent(eventName) { },
/**
* Determine whether or not the selection can be removed, either by the
* delete or cut operations based on whether or not any of its contents
* are non-removable. We don't need to worry about recursion here since it
* is a policy decision that a removable item not be placed inside a non-
* removable item.
* @returns true if all nodes in the selection can be removed,
* false otherwise.
*/
_hasRemovableSelection() {
var ranges = this._view.removableSelectionRanges;
if (!ranges.length)
return false;
var root = this._view.result.root;
for (var j = 0; j < ranges.length; j++) {
var nodes = ranges[j];
for (var i = 0; i < nodes.length; ++i) {
// Disallow removing the view's root node
if (nodes[i] == root)
return false;
if (!PlacesUIUtils.canUserRemove(nodes[i]))
return false;
}
}
return true;
},
/**
* Determines whether or not nodes can be inserted relative to the selection.
*/
_canInsert: function PC__canInsert(isPaste) {
var ip = this._view.insertionPoint;
return ip != null && (isPaste || ip.isTag != true);
},
/**
* Determines whether or not the root node for the view is selected
*/
rootNodeIsSelected: function PC_rootNodeIsSelected() {
var nodes = this._view.selectedNodes;
var root = this._view.result.root;
for (var i = 0; i < nodes.length; ++i) {
if (nodes[i] == root)
return true;
}
return false;
},
/**
* Looks at the data on the clipboard to see if it is paste-able.
* Paste-able data is:
* - in a format that the view can receive
* @returns true if: - clipboard data is of a TYPE_X_MOZ_PLACE_* flavor,
- clipboard data is of type TEXT_UNICODE and
is a valid URI.
*/
_isClipboardDataPasteable: function PC__isClipboardDataPasteable() {
// if the clipboard contains TYPE_X_MOZ_PLACE_* data, it is definitely
// pasteable, with no need to unwrap all the nodes.
var flavors = PlacesControllerDragHelper.placesFlavors;
var clipboard = this.clipboard;
var hasPlacesData =
clipboard.hasDataMatchingFlavors(flavors, flavors.length,
Ci.nsIClipboard.kGlobalClipboard);
if (hasPlacesData)
return this._view.insertionPoint != null;
// if the clipboard doesn't have TYPE_X_MOZ_PLACE_* data, we also allow
// pasting of valid "text/unicode" and "text/x-moz-url" data
var xferable = Cc["@mozilla.org/widget/transferable;1"].
createInstance(Ci.nsITransferable);
xferable.init(null);
xferable.addDataFlavor(PlacesUtils.TYPE_X_MOZ_URL);
xferable.addDataFlavor(PlacesUtils.TYPE_UNICODE);
clipboard.getData(xferable, Ci.nsIClipboard.kGlobalClipboard);
try {
// getAnyTransferData will throw if no data is available.
var data = { }, type = { };
xferable.getAnyTransferData(type, data, { });
data = data.value.QueryInterface(Ci.nsISupportsString).data;
if (type.value != PlacesUtils.TYPE_X_MOZ_URL &&
type.value != PlacesUtils.TYPE_UNICODE)
return false;
// unwrapNodes() will throw if the data blob is malformed.
var unwrappedNodes = PlacesUtils.unwrapNodes(data, type.value);
return this._view.insertionPoint != null;
}
catch (e) {
// getAnyTransferData or unwrapNodes failed
return false;
}
},
/**
* Gathers information about the selected nodes according to the following
* rules:
* "link" node is a URI
* "bookmark" node is a bookamrk
* "livemarkChild" node is a child of a livemark
* "tagChild" node is a child of a tag
* "folder" node is a folder
* "query" node is a query
* "separator" node is a separator line
* "host" node is a host
*
* @returns an array of objects corresponding the selected nodes. Each
* object has each of the properties above set if its corresponding
* node matches the rule. In addition, the annotations names for each
* node are set on its corresponding object as properties.
* Notes:
* 1) This can be slow, so don't call it anywhere performance critical!
* 2) A single-object array corresponding the root node is returned if
* there's no selection.
*/
_buildSelectionMetadata: function PC__buildSelectionMetadata() {
var metadata = [];
var root = this._view.result.root;
var nodes = this._view.selectedNodes;
if (nodes.length == 0)
nodes.push(root); // See the second note above
for (var i = 0; i < nodes.length; i++) {
var nodeData = {};
var node = nodes[i];
var nodeType = node.type;
var uri = null;
// We don't use the nodeIs* methods here to avoid going through the type
// property way too often
switch (nodeType) {
case Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY:
nodeData["query"] = true;
if (node.parent) {
switch (PlacesUtils.asQuery(node.parent).queryOptions.resultType) {
case Ci.nsINavHistoryQueryOptions.RESULTS_AS_SITE_QUERY:
nodeData["host"] = true;
break;
case Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_SITE_QUERY:
case Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_QUERY:
nodeData["day"] = true;
break;
}
}
break;
case Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER:
case Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT:
nodeData["folder"] = true;
break;
case Ci.nsINavHistoryResultNode.RESULT_TYPE_SEPARATOR:
nodeData["separator"] = true;
break;
case Ci.nsINavHistoryResultNode.RESULT_TYPE_URI:
nodeData["link"] = true;
uri = NetUtil.newURI(node.uri);
if (PlacesUtils.nodeIsBookmark(node)) {
nodeData["bookmark"] = true;
PlacesUtils.nodeIsTagQuery(node.parent)
var parentNode = node.parent;
if (parentNode) {
if (PlacesUtils.nodeIsTagQuery(parentNode))
nodeData["tagChild"] = true;
else if (this.hasCachedLivemarkInfo(parentNode))
nodeData["livemarkChild"] = true;
}
}
break;
}
// annotations
if (uri) {
let names = PlacesUtils.annotations.getPageAnnotationNames(uri);
for (let j = 0; j < names.length; ++j)
nodeData[names[j]] = true;
}
// For items also include the item-specific annotations
if (node.itemId != -1) {
let names = PlacesUtils.annotations
.getItemAnnotationNames(node.itemId);
for (let j = 0; j < names.length; ++j)
nodeData[names[j]] = true;
}
metadata.push(nodeData);
}
return metadata;
},
/**
* Determines if a context-menu item should be shown
* @param aMenuItem
* the context menu item
* @param aMetaData
* meta data about the selection
* @returns true if the conditions (see buildContextMenu) are satisfied
* and the item can be displayed, false otherwise.
*/
_shouldShowMenuItem: function PC__shouldShowMenuItem(aMenuItem, aMetaData) {
var selectiontype = aMenuItem.getAttribute("selectiontype");
if (selectiontype == "multiple" && aMetaData.length == 1)
return false;
if (selectiontype == "single" && aMetaData.length != 1)
return false;
var forceHideAttr = aMenuItem.getAttribute("forcehideselection");
if (forceHideAttr) {
var forceHideRules = forceHideAttr.split("|");
for (let i = 0; i < aMetaData.length; ++i) {
for (let j = 0; j < forceHideRules.length; ++j) {
if (forceHideRules[j] in aMetaData[i])
return false;
}
}
}
var selectionAttr = aMenuItem.getAttribute("selection");
if (!selectionAttr) {
return !aMenuItem.hidden;
}
if (selectionAttr == "any")
return true;
var showRules = selectionAttr.split("|");
var anyMatched = false;
function metaDataNodeMatches(metaDataNode, rules) {
for (var i = 0; i < rules.length; i++) {
if (rules[i] in metaDataNode)
return true;
}
return false;
}
for (var i = 0; i < aMetaData.length; ++i) {
if (metaDataNodeMatches(aMetaData[i], showRules))
anyMatched = true;
else
return false;
}
return anyMatched;
},
/**
* Detects information (meta-data rules) about the current selection in the
* view (see _buildSelectionMetadata) and sets the visibility state for each
* of the menu-items in the given popup with the following rules applied:
* 1) The "selectiontype" attribute may be set on a menu-item to "single"
* if the menu-item should be visible only if there is a single node
* selected, or to "multiple" if the menu-item should be visible only if
* multiple nodes are selected. If the attribute is not set or if it is
* set to an invalid value, the menu-item may be visible for both types of
* selection.
* 2) The "selection" attribute may be set on a menu-item to the various
* meta-data rules for which it may be visible. The rules should be
* separated with the | character.
* 3) A menu-item may be visible only if at least one of the rules set in
* its selection attribute apply to each of the selected nodes in the
* view.
* 4) The "forcehideselection" attribute may be set on a menu-item to rules
* for which it should be hidden. This attribute takes priority over the
* selection attribute. A menu-item would be hidden if at least one of the
* given rules apply to one of the selected nodes. The rules should be
* separated with the | character.
* 5) The "hideifnoinsertionpoint" attribute may be set on a menu-item to
* true if it should be hidden when there's no insertion point
* 6) The visibility state of a menu-item is unchanged if none of these
* attribute are set.
* 7) These attributes should not be set on separators for which the
* visibility state is "auto-detected."
* 8) The "hideifprivatebrowsing" attribute may be set on a menu-item to
* true if it should be hidden inside the private browsing mode
* @param aPopup
* The menupopup to build children into.
* @return true if at least one item is visible, false otherwise.
*/
buildContextMenu: function PC_buildContextMenu(aPopup) {
var metadata = this._buildSelectionMetadata();
var ip = this._view.insertionPoint;
var noIp = !ip || ip.isTag;
var separator = null;
var visibleItemsBeforeSep = false;
var anyVisible = false;
for (var i = 0; i < aPopup.childNodes.length; ++i) {
var item = aPopup.childNodes[i];
if (item.localName != "menuseparator") {
// We allow pasting into tag containers, so special case that.
var hideIfNoIP = item.getAttribute("hideifnoinsertionpoint") == "true" &&
noIp && !(ip && ip.isTag && item.id == "placesContext_paste");
// Show the "Open Containing Folder" menu-item only when the context is
// in the Library or in the Sidebar, and only when there's no insertion
// point.
var hideParentFolderItem = item.id == "placesContext_openParentFolder" &&
(!/tree/i.test(this._view.localName) || ip);
var hideIfPrivate = item.getAttribute("hideifprivatebrowsing") == "true" &&
PrivateBrowsingUtils.isWindowPrivate(window);
item.hidden = hideIfNoIP || hideIfPrivate || hideParentFolderItem ||
!this._shouldShowMenuItem(item, metadata);
if (!item.hidden) {
visibleItemsBeforeSep = true;
anyVisible = true;
// Show the separator above the menu-item if any
if (separator) {
separator.hidden = false;
separator = null;
}
}
}
else { // menuseparator
// Initially hide it. It will be unhidden if there will be at least one
// visible menu-item above and below it.
item.hidden = true;
// We won't show the separator at all if no items are visible above it
if (visibleItemsBeforeSep)
separator = item;
// New separator, count again:
visibleItemsBeforeSep = false;
}
}
// Set Open Folder/Links In Tabs items enabled state if they're visible
if (anyVisible) {
var openContainerInTabsItem = document.getElementById("placesContext_openContainer:tabs");
if (!openContainerInTabsItem.hidden && this._view.selectedNode &&
PlacesUtils.nodeIsContainer(this._view.selectedNode)) {
openContainerInTabsItem.disabled =
!PlacesUtils.hasChildURIs(this._view.selectedNode);
}
else {
// see selectiontype rule in the overlay
var openLinksInTabsItem = document.getElementById("placesContext_openLinks:tabs");
openLinksInTabsItem.disabled = openLinksInTabsItem.hidden;
}
}
return anyVisible;
},
/**
* Select all links in the current view.
*/
selectAll: function PC_selectAll() {
this._view.selectAll();
},
/**
* Opens the bookmark properties for the selected URI Node.
*/
showBookmarkPropertiesForSelection:
function PC_showBookmarkPropertiesForSelection() {
var node = this._view.selectedNode;
if (!node)
return;
var itemType = PlacesUtils.nodeIsFolder(node) ||
PlacesUtils.nodeIsTagQuery(node) ? "folder" : "bookmark";
var concreteId = PlacesUtils.getConcreteItemId(node);
var isRootItem = PlacesUtils.isRootItem(concreteId);
var itemId = node.itemId;
if (isRootItem || PlacesUtils.nodeIsTagQuery(node)) {
// If this is a root or the Tags query we use the concrete itemId to catch
// the correct title for the node.
itemId = concreteId;
}
PlacesUIUtils.showBookmarkDialog({ action: "edit"
, type: itemType
, itemId: itemId
, readOnly: isRootItem
, hiddenRows: [ "folderPicker" ]
}, window.top);
},
/**
* This method can be run on a URI parameter to ensure that it didn't
* receive a string instead of an nsIURI object.
*/
_assertURINotString: function PC__assertURINotString(value) {
NS_ASSERT((typeof(value) == "object") && !(value instanceof String),
"This method should be passed a URI as a nsIURI object, not as a string.");
},
/**
* Reloads the selected livemark if any.
*/
reloadSelectedLivemark: function PC_reloadSelectedLivemark() {
var selectedNode = this._view.selectedNode;
if (selectedNode) {
let itemId = selectedNode.itemId;
PlacesUtils.livemarks.getLivemark({ id: itemId })
.then(aLivemark => {
aLivemark.reload(true);
}, Components.utils.reportError);
}
},
/**
* Opens the links in the selected folder, or the selected links in new tabs.
*/
openSelectionInTabs: function PC_openLinksInTabs(aEvent) {
var node = this._view.selectedNode;
if (node && PlacesUtils.nodeIsContainer(node))
PlacesUIUtils.openContainerNodeInTabs(this._view.selectedNode, aEvent, this._view);
else
PlacesUIUtils.openURINodesInTabs(this._view.selectedNodes, aEvent, this._view);
},
/**
* Shows the Add Bookmark UI for the current insertion point.
*
* @param aType
* the type of the new item (bookmark/livemark/folder)
*/
newItem: function PC_newItem(aType) {
let ip = this._view.insertionPoint;
if (!ip)
throw Cr.NS_ERROR_NOT_AVAILABLE;
let performed =
PlacesUIUtils.showBookmarkDialog({ action: "add"
, type: aType
, defaultInsertionPoint: ip
, hiddenRows: [ "folderPicker" ]
}, window.top);
if (performed) {
// Select the new item.
let insertedNodeId = PlacesUtils.bookmarks
.getIdForItemAt(ip.itemId, ip.index);
this._view.selectItems([insertedNodeId], false);
}
},
/**
* Create a new Bookmark separator somewhere.
*/
newSeparator: function PC_newSeparator() {
var ip = this._view.insertionPoint;
if (!ip)
throw Cr.NS_ERROR_NOT_AVAILABLE;
var txn = new PlacesCreateSeparatorTransaction(ip.itemId, ip.index);
PlacesUtils.transactionManager.doTransaction(txn);
// select the new item
var insertedNodeId = PlacesUtils.bookmarks
.getIdForItemAt(ip.itemId, ip.index);
this._view.selectItems([insertedNodeId], false);
},
/**
* Opens a dialog for moving the selected nodes.
*/
moveSelectedBookmarks: function PC_moveBookmarks() {
window.openDialog("chrome://browser/content/places/moveBookmarks.xul",
"", "chrome, modal",
this._view.selectedNodes);
},
/**
* Sort the selected folder by name.
*/
sortFolderByName: function PC_sortFolderByName() {
var itemId = PlacesUtils.getConcreteItemId(this._view.selectedNode);
var txn = new PlacesSortFolderByNameTransaction(itemId);
PlacesUtils.transactionManager.doTransaction(txn);
},
/**
* Open the parent folder for the selected bookmarks search result.
*/
openParentFolder: function PC_openParentFolder() {
var view;
if (!document.popupNode) {
view = document.commandDispatcher.focusedElement;
} else {
view = PlacesUIUtils.getViewForNode(document.popupNode); // XULElement
}
if (!view || view.getAttribute("type") != "places")
return;
var node = view.selectedNode; // nsINavHistoryResultNode
var aItemId = node.itemId;
var aFolderItemId = this.getParentFolderByItemId(aItemId);
if (aFolderItemId)
this.selectFolderByItemId(view, aFolderItemId, aItemId);
},
getParentFolderByItemId: function PC_getParentFolderByItemId(aItemId) {
var bmsvc = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"].
getService(Components.interfaces.nsINavBookmarksService);
var parentFolderId = bmsvc.getFolderIdForItem(aItemId);
return parentFolderId;
},
selectItems2: function PC_selectItems2(view, aIDs) {
var ids = aIDs; // Don't manipulate the caller's array.
// Array of nodes found by findNodes which are to be selected
var nodes = [];
// Array of nodes found by findNodes which should be opened
var nodesToOpen = [];
// A set of URIs of container-nodes that were previously searched,
// and thus shouldn't be searched again. This is empty at the initial
// start of the recursion and gets filled in as the recursion
// progresses.
var nodesURIChecked = [];
/**
* Recursively search through a node's children for items
* with the given IDs. When a matching item is found, remove its ID
* from the IDs array, and add the found node to the nodes dictionary.
*
* NOTE: This method will leave open any node that had matching items
* in its subtree.
*/
function findNodes(node) {
var foundOne = false;
// See if node matches an ID we wanted; add to results.
// For simple folder queries, check both itemId and the concrete
// item id.
var index = ids.indexOf(node.itemId);
if (index == -1 &&
node.type == Components.interfaces.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT) {
index = ids.indexOf(PlacesUtils.asQuery(node).folderItemId); //xxx Bug 556739 3.7a5pre
}
if (index != -1) {
nodes.push(node);
foundOne = true;
ids.splice(index, 1);
}
if (ids.length == 0 || !PlacesUtils.nodeIsContainer(node) ||
nodesURIChecked.indexOf(node.uri) != -1)
return foundOne;
nodesURIChecked.push(node.uri);
PlacesUtils.asContainer(node); // xxx Bug 556739 3.7a6pre
// Remember the beginning state so that we can re-close
// this node if we don't find any additional results here.
var previousOpenness = node.containerOpen;
node.containerOpen = true;
for (var child = 0; child < node.childCount && ids.length > 0;
child++) {
var childNode = node.getChild(child);
var found = findNodes(childNode);
if (!foundOne)
foundOne = found;
}
// If we didn't find any additional matches in this node's
// subtree, revert the node to its previous openness.
if (foundOne)
nodesToOpen.unshift(node);
node.containerOpen = previousOpenness;
return foundOne;
} // findNodes
// Disable notifications while looking for nodes.
let result = view.result;
let didSuppressNotifications = result.suppressNotifications;
if (!didSuppressNotifications)
result.suppressNotifications = true
try {
findNodes(view.result.root);
}
finally {
if (!didSuppressNotifications)
result.suppressNotifications = false;
}
// For all the nodes we've found, highlight the corresponding
// index in the tree.
var resultview = view.view;
var selection = resultview.selection;
selection.selectEventsSuppressed = true;
selection.clearSelection();
// Open nodes containing found items.
for (var i = 0; i < nodesToOpen.length; i++) {
nodesToOpen[i].containerOpen = true;
}
for (var i = 0; i < nodes.length; i++) {
if (PlacesUtils.nodeIsContainer(nodes[i]))
continue;
var index = resultview.treeIndexForNode(nodes[i]);
selection.rangedSelect(index, index, true);
}
selection.selectEventsSuppressed = false;
},
selectFolderByItemId: function PC_selectFolderByItemId(view, aFolderItemId, aItemId) {
// Library
if (view.getAttribute("id") == "placeContent") {
view = document.getElementById("placesList");
// Select a folder node in folder pane.
this.selectItems2(view, [aFolderItemId]);
view.selectItems([aFolderItemId]);
if (view.currentIndex)
view.treeBoxObject.ensureRowIsVisible(view.currentIndex);
// Reselect child node.
setTimeout(function(aItemId, view) {
var aView = view.ownerDocument.getElementById("placeContent");
aView.selectItems([aItemId]);
if (aView.currentIndex)
aView.treeBoxObject.ensureRowIsVisible(aView.currentIndex);
}, 0, aItemId, view);
return;
}
// Bookmarks Sidebar
if (!view)
return;
view.place = view.place;
if ('FlatBookmarksOverlay' in window) {
var sidebarwin = view.ownerDocument.defaultView;
var searchBox = sidebarwin.document.getElementById("search-box");
searchBox.value = "";
searchBox.doCommand();
sidebarwin.FlatBookmarks._setTreePlace(sidebarwin.FlatBookmarks._makePlaceForFolder(aFolderItemId));
view.selectItems([aItemId]);
var tbo = view.treeBoxObject;
tbo.ensureRowIsVisible(view.currentIndex);
view.focus();
return;
}
view.findNode = function flatChildNodes(node, aIDs) {
var ids = aIDs; // Don't manipulate the caller's array.
// Array of nodes found by findNodes which are to be selected
var nodes = [];
// Array of nodes found by findNodes which should be opened
var nodesToOpen = [];
// A set of URIs of container-nodes that were previously searched,
// and thus shouldn't be searched again. This is empty at the initial
// start of the recursion and gets filled in as the recursion
// progresses.
var nodesURIChecked = [];
/**
* Recursively search through a node's children for items
* with the given IDs. When a matching item is found, remove its ID
* from the IDs array, and add the found node to the nodes dictionary.
*
* NOTE: This method will leave open any node that had matching items
* in its subtree.
*/
function findNodes(node) {
var foundOne = false;
// See if node matches an ID we wanted; add to results.
// For simple folder queries, check both itemId and the concrete
// item id.
var index = ids.indexOf(node.itemId);
if (index == -1 &&
node.type == Components.interfaces.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT) {
index = ids.indexOf(PlacesUtils.asQuery(node).folderItemId); // xxx Bug 556739 3.7a5pre
}
if (index != -1) {
nodes.push(node);
foundOne = true;
ids.splice(index, 1);
}
if (ids.length == 0 || !PlacesUtils.nodeIsContainer(node) ||
nodesURIChecked.indexOf(node.uri) != -1)
return foundOne;
nodesURIChecked.push(node.uri);
PlacesUtils.asContainer(node); // xxx Bug 556739 3.7a6pre
// Remember the beginning state so that we can re-close
// this node if we don't find any additional results here.
var previousOpenness = node.containerOpen;
node.containerOpen = true;
for (var child = 0; child < node.childCount && ids.length > 0;
child++) {
var childNode = node.getChild(child);
if (PlacesUtils.nodeIsQuery(childNode))
continue;
var found = findNodes(childNode);
if (!foundOne)
foundOne = found;
}
// If we didn't find any additional matches in this node's
// subtree, revert the node to its previous openness.
if (foundOne)
nodesToOpen.unshift(node);
node.containerOpen = previousOpenness;
return foundOne;
} // findNodes
// Disable notifications while looking for nodes.
let result = this.result;
let didSuppressNotifications = result.suppressNotifications;
if (!didSuppressNotifications)
result.suppressNotifications = true
try {
findNodes(this.result.root);
}
finally {
if (!didSuppressNotifications)
result.suppressNotifications = false;
}
// Open nodes containing found items.
for (var i = 0; i < nodesToOpen.length; i++) {
nodesToOpen[i].containerOpen = true;
}
return nodes;
}; // findNode
// For all the nodes we've found, highlight the corresponding
// index in the tree.
var resultview = view.view;
var selection = view.view.selection;
selection.selectEventsSuppressed = true;
selection.clearSelection();
var nodes = view.findNode(view.result.root, [aFolderItemId]);
if (nodes.length > 0) {
var index = resultview.treeIndexForNode(nodes[0]);
nodes = view.findNode(nodes[0], [aItemId]);
if (nodes.length > 0) {
index = resultview.treeIndexForNode(nodes[0]);
selection.rangedSelect(index, index, true);
}
}
selection.selectEventsSuppressed = false;
var tbo = view.treeBoxObject;
tbo.ensureRowIsVisible(view.currentIndex);
view.focus();
return;
},
/**
* Walk the list of folders we're removing in this delete operation, and
* see if the selected node specified is already implicitly being removed
* because it is a child of that folder.
* @param node
* Node to check for containment.
* @param pastFolders
* List of folders the calling function has already traversed
* @returns true if the node should be skipped, false otherwise.
*/
_shouldSkipNode: function PC_shouldSkipNode(node, pastFolders) {
/**
* Determines if a node is contained by another node within a resultset.
* @param node
* The node to check for containment for
* @param parent
* The parent container to check for containment in
* @returns true if node is a member of parent's children, false otherwise.
*/
function isContainedBy(node, parent) {
var cursor = node.parent;
while (cursor) {
if (cursor == parent)
return true;
cursor = cursor.parent;
}
return false;
}
for (var j = 0; j < pastFolders.length; ++j) {
if (isContainedBy(node, pastFolders[j]))
return true;
}
return false;
},
/**
* Creates a set of transactions for the removal of a range of items.
* A range is an array of adjacent nodes in a view.
* @param [in] range
* An array of nodes to remove. Should all be adjacent.
* @param [out] transactions
* An array of transactions.
* @param [optional] removedFolders
* An array of folder nodes that have already been removed.
*/
_removeRange: function PC__removeRange(range, transactions, removedFolders) {
NS_ASSERT(transactions instanceof Array, "Must pass a transactions array");
if (!removedFolders)
removedFolders = [];
for (var i = 0; i < range.length; ++i) {
var node = range[i];
if (this._shouldSkipNode(node, removedFolders))
continue;
if (PlacesUtils.nodeIsTagQuery(node.parent)) {
// This is a uri node inside a tag container. It needs a special
// untag transaction.
var tagItemId = PlacesUtils.getConcreteItemId(node.parent);
var uri = NetUtil.newURI(node.uri);
let txn = new PlacesUntagURITransaction(uri, [tagItemId]);
transactions.push(txn);
}
else if (PlacesUtils.nodeIsTagQuery(node) && node.parent &&
PlacesUtils.nodeIsQuery(node.parent) &&
PlacesUtils.asQuery(node.parent).queryOptions.resultType ==
Ci.nsINavHistoryQueryOptions.RESULTS_AS_TAG_QUERY) {
// This is a tag container.
// Untag all URIs tagged with this tag only if the tag container is
// child of the "Tags" query in the library, in all other places we
// must only remove the query node.
var tag = node.title;
var URIs = PlacesUtils.tagging.getURIsForTag(tag);
for (var j = 0; j < URIs.length; j++) {
let txn = new PlacesUntagURITransaction(URIs[j], [tag]);
transactions.push(txn);
}
}
else if (PlacesUtils.nodeIsURI(node) &&
PlacesUtils.nodeIsQuery(node.parent) &&
PlacesUtils.asQuery(node.parent).queryOptions.queryType ==
Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY) {
// This is a uri node inside an history query.
PlacesUtils.bhistory.removePage(NetUtil.newURI(node.uri));
// History deletes are not undoable, so we don't have a transaction.
}
else if (node.itemId == -1 &&
PlacesUtils.nodeIsQuery(node) &&
PlacesUtils.asQuery(node).queryOptions.queryType ==
Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY) {
// This is a dynamically generated history query, like queries
// grouped by site, time or both. Dynamically generated queries don't
// have an itemId even if they are descendants of a bookmark.
this._removeHistoryContainer(node);
// History deletes are not undoable, so we don't have a transaction.
}
else {
// This is a common bookmark item.
if (PlacesUtils.nodeIsFolder(node)) {
// If this is a folder we add it to our array of folders, used
// to skip nodes that are children of an already removed folder.
removedFolders.push(node);
}
let txn = new PlacesRemoveItemTransaction(node.itemId);
transactions.push(txn);
}
}
},
/**
* Removes the set of selected ranges from bookmarks.
* @param txnName
* See |remove|.
*/
_removeRowsFromBookmarks: function PC__removeRowsFromBookmarks(txnName) {
var ranges = this._view.removableSelectionRanges;
var transactions = [];
var removedFolders = [];
for (var i = 0; i < ranges.length; i++)
this._removeRange(ranges[i], transactions, removedFolders);
if (transactions.length > 0) {
var txn = new PlacesAggregatedTransaction(txnName, transactions);
PlacesUtils.transactionManager.doTransaction(txn);
}
},
/**
* Removes the set of selected ranges from history.
*
* @note history deletes are not undoable.
*/
_removeRowsFromHistory: function PC__removeRowsFromHistory() {
let nodes = this._view.selectedNodes;
let URIs = [];
for (let i = 0; i < nodes.length; ++i) {
let node = nodes[i];
if (PlacesUtils.nodeIsURI(node)) {
let uri = NetUtil.newURI(node.uri);
// Avoid duplicates.
if (URIs.indexOf(uri) < 0) {
URIs.push(uri);
}
}
else if (PlacesUtils.nodeIsQuery(node) &&
PlacesUtils.asQuery(node).queryOptions.queryType ==
Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY) {
this._removeHistoryContainer(node);
}
}
// Do removal in chunks to give some breath to main-thread.
function pagesChunkGenerator(aURIs) {
while (aURIs.length) {
let URIslice = aURIs.splice(0, REMOVE_PAGES_CHUNKLEN);
PlacesUtils.bhistory.removePages(URIslice, URIslice.length);
Services.tm.mainThread.dispatch(function() {
try {
gen.next();
} catch (ex if ex instanceof StopIteration) {}
}, Ci.nsIThread.DISPATCH_NORMAL);
yield;
}
}
let gen = pagesChunkGenerator(URIs);
gen.next();
},
/**
* Removes history visits for an history container node.
* @param [in] aContainerNode
* The container node to remove.
*
* @note history deletes are not undoable.
*/
_removeHistoryContainer: function PC__removeHistoryContainer(aContainerNode) {
if (PlacesUtils.nodeIsHost(aContainerNode)) {
// Site container.
PlacesUtils.bhistory.removePagesFromHost(aContainerNode.title, true);
}
else if (PlacesUtils.nodeIsDay(aContainerNode)) {
// Day container.
let query = aContainerNode.getQueries()[0];
let beginTime = query.beginTime;
let endTime = query.endTime;
NS_ASSERT(query && beginTime && endTime,
"A valid date container query should exist!");
// We want to exclude beginTime from the removal because
// removePagesByTimeframe includes both extremes, while date containers
// exclude the lower extreme. So, if we would not exclude it, we would
// end up removing more history than requested.
PlacesUtils.bhistory.removePagesByTimeframe(beginTime + 1, endTime);
}
},
/**
* Removes the selection
* @param aTxnName
* A name for the transaction if this is being performed
* as part of another operation.
*/
remove: function PC_remove(aTxnName) {
if (!this._hasRemovableSelection())
return;
NS_ASSERT(aTxnName !== undefined, "Must supply Transaction Name");
var root = this._view.result.root;
if (PlacesUtils.nodeIsFolder(root))
this._removeRowsFromBookmarks(aTxnName);
else if (PlacesUtils.nodeIsQuery(root)) {
var queryType = PlacesUtils.asQuery(root).queryOptions.queryType;
if (queryType == Ci.nsINavHistoryQueryOptions.QUERY_TYPE_BOOKMARKS)
this._removeRowsFromBookmarks(aTxnName);
else if (queryType == Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY)
this._removeRowsFromHistory();
else
NS_ASSERT(false, "implement support for QUERY_TYPE_UNIFIED");
}
else
NS_ASSERT(false, "unexpected root");
},
/**
* Fills a DataTransfer object with the content of the selection that can be
* dropped elsewhere.
* @param aEvent
* The dragstart event.
*/
setDataTransfer: function PC_setDataTransfer(aEvent) {
let dt = aEvent.dataTransfer;
let doCopy = ["copyLink", "copy", "link"].indexOf(dt.effectAllowed) != -1;
let result = this._view.result;
let didSuppressNotifications = result.suppressNotifications;
if (!didSuppressNotifications)
result.suppressNotifications = true;
function addData(type, index, overrideURI) {
let wrapNode = PlacesUtils.wrapNode(node, type, overrideURI, doCopy);
dt.mozSetDataAt(type, wrapNode, index);
}
function addURIData(index, overrideURI) {
addData(PlacesUtils.TYPE_X_MOZ_URL, index, overrideURI);
addData(PlacesUtils.TYPE_UNICODE, index, overrideURI);
addData(PlacesUtils.TYPE_HTML, index, overrideURI);
}
try {
let nodes = this._view.draggableSelection;
for (let i = 0; i < nodes.length; ++i) {
var node = nodes[i];
// This order is _important_! It controls how this and other
// applications select data to be inserted based on type.
addData(PlacesUtils.TYPE_X_MOZ_PLACE, i);
// Drop the feed uri for livemark containers
let livemarkInfo = this.getCachedLivemarkInfo(node);
if (livemarkInfo) {
addURIData(i, livemarkInfo.feedURI.spec);
}
else if (node.uri) {
addURIData(i);
}
}
}
finally {
if (!didSuppressNotifications)
result.suppressNotifications = false;
}
},
get clipboardAction () {
let action = {};
let actionOwner;
try {
let xferable = Cc["@mozilla.org/widget/transferable;1"].
createInstance(Ci.nsITransferable);
xferable.init(null);
xferable.addDataFlavor(PlacesUtils.TYPE_X_MOZ_PLACE_ACTION)
this.clipboard.getData(xferable, Ci.nsIClipboard.kGlobalClipboard);
xferable.getTransferData(PlacesUtils.TYPE_X_MOZ_PLACE_ACTION, action, {});
[action, actionOwner] =
action.value.QueryInterface(Ci.nsISupportsString).data.split(",");
} catch(ex) {
// Paste from external sources don't have any associated action, just
// fallback to a copy action.
return "copy";
}
// For cuts also check who inited the action, since cuts across different
// instances should instead be handled as copies (The sources are not
// available for this instance).
if (action == "cut" && actionOwner != this.profileName)
action = "copy";
return action;
},
_releaseClipboardOwnership: function PC__releaseClipboardOwnership() {
if (this.cutNodes.length > 0) {
// This clears the logical clipboard, doesn't remove data.
this.clipboard.emptyClipboard(Ci.nsIClipboard.kGlobalClipboard);
}
},
_clearClipboard: function PC__clearClipboard() {
let xferable = Cc["@mozilla.org/widget/transferable;1"].
createInstance(Ci.nsITransferable);
xferable.init(null);
// Empty transferables may cause crashes, so just add an unknown type.
const TYPE = "text/x-moz-place-empty";
xferable.addDataFlavor(TYPE);
xferable.setTransferData(TYPE, PlacesUtils.toISupportsString(""), 0);
this.clipboard.setData(xferable, null, Ci.nsIClipboard.kGlobalClipboard);
},
_populateClipboard: function PC__populateClipboard(aNodes, aAction) {
// This order is _important_! It controls how this and other applications
// select data to be inserted based on type.
let contents = [
{ type: PlacesUtils.TYPE_X_MOZ_PLACE, entries: [] },
{ type: PlacesUtils.TYPE_X_MOZ_URL, entries: [] },
{ type: PlacesUtils.TYPE_HTML, entries: [] },
{ type: PlacesUtils.TYPE_UNICODE, entries: [] },
];
// Avoid handling descendants of a copied node, the transactions take care
// of them automatically.
let copiedFolders = [];
aNodes.forEach(function (node) {
if (this._shouldSkipNode(node, copiedFolders))
return;
if (PlacesUtils.nodeIsFolder(node))
copiedFolders.push(node);
let livemarkInfo = this.getCachedLivemarkInfo(node);
let overrideURI = livemarkInfo ? livemarkInfo.feedURI.spec : null;
let resolveShortcuts = !PlacesControllerDragHelper.canMoveNode(node);
contents.forEach(function (content) {
content.entries.push(
PlacesUtils.wrapNode(node, content.type, overrideURI, resolveShortcuts)
);
});
}, this);
function addData(type, data) {
xferable.addDataFlavor(type);
xferable.setTransferData(type, PlacesUtils.toISupportsString(data),
data.length * 2);
}
let xferable = Cc["@mozilla.org/widget/transferable;1"].
createInstance(Ci.nsITransferable);
xferable.init(null);
let hasData = false;
// This order matters here! It controls how this and other applications
// select data to be inserted based on type.
contents.forEach(function (content) {
if (content.entries.length > 0) {
hasData = true;
let glue =
content.type == PlacesUtils.TYPE_X_MOZ_PLACE ? "," : PlacesUtils.endl;
addData(content.type, content.entries.join(glue));
}
});
// Track the exected action in the xferable. This must be the last flavor
// since it's the least preferred one.
// Enqueue a unique instance identifier to distinguish operations across
// concurrent instances of the application.
addData(PlacesUtils.TYPE_X_MOZ_PLACE_ACTION, aAction + "," + this.profileName);
if (hasData) {
this.clipboard.setData(xferable,
this.cutNodes.length > 0 ? this : null,
Ci.nsIClipboard.kGlobalClipboard);
}
},
_cutNodes: [],
get cutNodes() this._cutNodes,
set cutNodes(aNodes) {
let self = this;
function updateCutNodes(aValue) {
self._cutNodes.forEach(function (aNode) {
self._view.toggleCutNode(aNode, aValue);
});
}
updateCutNodes(false);
this._cutNodes = aNodes;
updateCutNodes(true);
return aNodes;
},
/**
* Copy Bookmarks and Folders to the clipboard
*/
copy: function PC_copy() {
let result = this._view.result;
let didSuppressNotifications = result.suppressNotifications;
if (!didSuppressNotifications)
result.suppressNotifications = true;
try {
this._populateClipboard(this._view.selectedNodes, "copy");
}
finally {
if (!didSuppressNotifications)
result.suppressNotifications = false;
}
},
/**
* Cut Bookmarks and Folders to the clipboard
*/
cut: function PC_cut() {
let result = this._view.result;
let didSuppressNotifications = result.suppressNotifications;
if (!didSuppressNotifications)
result.suppressNotifications = true;
try {
this._populateClipboard(this._view.selectedNodes, "cut");
this.cutNodes = this._view.selectedNodes;
}
finally {
if (!didSuppressNotifications)
result.suppressNotifications = false;
}
},
/**
* Paste Bookmarks and Folders from the clipboard
*/
paste: function PC_paste() {
// No reason to proceed if there isn't a valid insertion point.
let ip = this._view.insertionPoint;
if (!ip)
throw Cr.NS_ERROR_NOT_AVAILABLE;
let action = this.clipboardAction;
let xferable = Cc["@mozilla.org/widget/transferable;1"].
createInstance(Ci.nsITransferable);
xferable.init(null);
// This order matters here! It controls the preferred flavors for this
// paste operation.
[ PlacesUtils.TYPE_X_MOZ_PLACE,
PlacesUtils.TYPE_X_MOZ_URL,
PlacesUtils.TYPE_UNICODE,
].forEach(function (type) xferable.addDataFlavor(type));
this.clipboard.getData(xferable, Ci.nsIClipboard.kGlobalClipboard);
// Now get the clipboard contents, in the best available flavor.
let data = {}, type = {}, items = [];
try {
xferable.getAnyTransferData(type, data, {});
data = data.value.QueryInterface(Ci.nsISupportsString).data;
type = type.value;
items = PlacesUtils.unwrapNodes(data, type);
} catch(ex) {
// No supported data exists or nodes unwrap failed, just bail out.
return;
}
let transactions = [];
let insertionIndex = ip.index;
for (let i = 0; i < items.length; ++i) {
if (ip.isTag) {
// Pasting into a tag container means tagging the item, regardless of
// the requested action.
let tagTxn = new PlacesTagURITransaction(NetUtil.newURI(items[i].uri),
[ip.itemId]);
transactions.push(tagTxn);
continue;
}
// Adjust index to make sure items are pasted in the correct position.
// If index is DEFAULT_INDEX, items are just appended.
if (ip.index != PlacesUtils.bookmarks.DEFAULT_INDEX)
insertionIndex = ip.index + i;
transactions.push(
PlacesUIUtils.makeTransaction(items[i], type, ip.itemId,
insertionIndex, action == "copy")
);
}
let aggregatedTxn = new PlacesAggregatedTransaction("Paste", transactions);
PlacesUtils.transactionManager.doTransaction(aggregatedTxn);
// Cut/past operations are not repeatable, so clear the clipboard.
if (action == "cut") {
this._clearClipboard();
}
// Select the pasted items, they should be consecutive.
let insertedNodeIds = [];
for (let i = 0; i < transactions.length; ++i) {
insertedNodeIds.push(
PlacesUtils.bookmarks.getIdForItemAt(ip.itemId, ip.index + i)
);
}
if (insertedNodeIds.length > 0)
this._view.selectItems(insertedNodeIds, false);
},
/**
* Cache the livemark info for a node. This allows the controller and the
* views to treat the given node as a livemark.
* @param aNode
* a places result node.
* @param aLivemarkInfo
* a mozILivemarkInfo object.
*/
cacheLivemarkInfo: function PC_cacheLivemarkInfo(aNode, aLivemarkInfo) {
this._cachedLivemarkInfoObjects.set(aNode, aLivemarkInfo);
},
/**
* Returns whether or not there's cached mozILivemarkInfo object for a node.
* @param aNode
* a places result node.
* @return true if there's a cached mozILivemarkInfo object for
* aNode, false otherwise.
*/
hasCachedLivemarkInfo: function PC_hasCachedLivemarkInfo(aNode)
this._cachedLivemarkInfoObjects.has(aNode),
/**
* Returns the cached livemark info for a node, if set by cacheLivemarkInfo,
* null otherwise.
* @param aNode
* a places result node.
* @return the mozILivemarkInfo object for aNode, if set, null otherwise.
*/
getCachedLivemarkInfo: function PC_getCachedLivemarkInfo(aNode)
this._cachedLivemarkInfoObjects.get(aNode, null)
};
/**
* Handles drag and drop operations for views. Note that this is view agnostic!
* You should not use PlacesController._view within these methods, since
* the view that the item(s) have been dropped on was not necessarily active.
* Drop functions are passed the view that is being dropped on.
*/
var PlacesControllerDragHelper = {
/**
* DOM Element currently being dragged over
*/
currentDropTarget: null,
/**
* Determines if the mouse is currently being dragged over a child node of
* this menu. This is necessary so that the menu doesn't close while the
* mouse is dragging over one of its submenus
* @param node
* The container node
* @returns true if the user is dragging over a node within the hierarchy of
* the container, false otherwise.
*/
draggingOverChildNode: function PCDH_draggingOverChildNode(node) {
let currentNode = this.currentDropTarget;
while (currentNode) {
if (currentNode == node)
return true;
currentNode = currentNode.parentNode;
}
return false;
},
/**
* @returns The current active drag session. Returns null if there is none.
*/
getSession: function PCDH__getSession() {
return this.dragService.getCurrentSession();
},
/**
* Extract the first accepted flavor from a list of flavors.
* @param aFlavors
* The flavors list of type nsIDOMDOMStringList.
*/
getFirstValidFlavor: function PCDH_getFirstValidFlavor(aFlavors) {
for (let i = 0; i < aFlavors.length; i++) {
if (this.GENERIC_VIEW_DROP_TYPES.indexOf(aFlavors[i]) != -1)
return aFlavors[i];
}
// If no supported flavor is found, check if data includes text/plain
// contents. If so, request them as text/unicode, a conversion will happen
// automatically.
if (aFlavors.contains("text/plain")) {
return PlacesUtils.TYPE_UNICODE;
}
return null;
},
/**
* Determines whether or not the data currently being dragged can be dropped
* on a places view.
* @param ip
* The insertion point where the items should be dropped.
*/
canDrop: function PCDH_canDrop(ip, dt) {
let dropCount = dt.mozItemCount;
// Check every dragged item.
for (let i = 0; i < dropCount; i++) {
let flavor = this.getFirstValidFlavor(dt.mozTypesAt(i));
if (!flavor)
return false;
// Urls can be dropped on any insertionpoint.
// XXXmano: remember that this method is called for each dragover event!
// Thus we shouldn't use unwrapNodes here at all if possible.
// I think it would be OK to accept bogus data here (e.g. text which was
// somehow wrapped as TAB_DROP_TYPE, this is not in our control, and
// will just case the actual drop to be a no-op), and only rule out valid
// expected cases, which are either unsupported flavors, or items which
// cannot be dropped in the current insertionpoint. The last case will
// likely force us to use unwrapNodes for the private data types of
// places.
if (flavor == TAB_DROP_TYPE)
continue;
let data = dt.mozGetDataAt(flavor, i);
let dragged;
try {
dragged = PlacesUtils.unwrapNodes(data, flavor)[0];
}
catch (e) {
return false;
}
// Only bookmarks and urls can be dropped into tag containers.
if (ip.isTag && ip.orientation == Ci.nsITreeView.DROP_ON &&
dragged.type != PlacesUtils.TYPE_X_MOZ_URL &&
(dragged.type != PlacesUtils.TYPE_X_MOZ_PLACE ||
(dragged.uri && dragged.uri.startsWith("place:")) ))
return false;
// The following loop disallows the dropping of a folder on itself or
// on any of its descendants.
if (dragged.type == PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER ||
(dragged.uri && dragged.uri.startsWith("place:")) ) {
let parentId = ip.itemId;
while (parentId != PlacesUtils.placesRootId) {
if (dragged.concreteId == parentId || dragged.id == parentId)
return false;
parentId = PlacesUtils.bookmarks.getFolderIdForItem(parentId);
}
}
}
return true;
},
/**
* Determines if a node can be moved.
*
* @param aNode
* A nsINavHistoryResultNode node.
* @returns True if the node can be moved, false otherwise.
*/
canMoveNode:
function PCDH_canMoveNode(aNode) {
// Only bookmark items are movable.
if (aNode.itemId == -1)
return false;
// Once tags and bookmarked are divorced, the tag-query check should be
// removed.
let parentNode = aNode.parent;
return parentNode != null &&
!(PlacesUtils.nodeIsFolder(parentNode) &&
PlacesUIUtils.isContentsReadOnly(parentNode)) &&
!PlacesUtils.nodeIsTagQuery(parentNode);
},
/**
* Handles the drop of one or more items onto a view.
* @param insertionPoint
* The insertion point where the items should be dropped
*/
onDrop: function PCDH_onDrop(insertionPoint, dt) {
let doCopy = ["copy", "link"].indexOf(dt.dropEffect) != -1;
let transactions = [];
let dropCount = dt.mozItemCount;
let movedCount = 0;
for (let i = 0; i < dropCount; ++i) {
let flavor = this.getFirstValidFlavor(dt.mozTypesAt(i));
if (!flavor)
return;
let data = dt.mozGetDataAt(flavor, i);
let unwrapped;
if (flavor != TAB_DROP_TYPE) {
// There's only ever one in the D&D case.
unwrapped = PlacesUtils.unwrapNodes(data, flavor)[0];
}
else if (data instanceof XULElement && data.localName == "tab" &&
data.ownerDocument.defaultView instanceof ChromeWindow) {
let uri = data.linkedBrowser.currentURI;
let spec = uri ? uri.spec : "about:blank";
let title = data.label;
unwrapped = { uri: spec,
title: data.label,
type: PlacesUtils.TYPE_X_MOZ_URL};
}
else
throw("bogus data was passed as a tab");
let index = insertionPoint.index;
// Adjust insertion index to prevent reversal of dragged items. When you
// drag multiple elts upward: need to increment index or each successive
// elt will be inserted at the same index, each above the previous.
let dragginUp = insertionPoint.itemId == unwrapped.parent &&
index < PlacesUtils.bookmarks.getItemIndex(unwrapped.id);
if (index != -1 && dragginUp)
index += movedCount++;
// If dragging over a tag container we should tag the item.
if (insertionPoint.isTag &&
insertionPoint.orientation == Ci.nsITreeView.DROP_ON) {
let uri = NetUtil.newURI(unwrapped.uri);
let tagItemId = insertionPoint.itemId;
let tagTxn = new PlacesTagURITransaction(uri, [tagItemId]);
transactions.push(tagTxn);
}
else {
transactions.push(PlacesUIUtils.makeTransaction(unwrapped,
flavor, insertionPoint.itemId,
index, doCopy));
}
}
let txn = new PlacesAggregatedTransaction("DropItems", transactions);
PlacesUtils.transactionManager.doTransaction(txn);
},
/**
* Checks if we can insert into a container.
* @param aContainer
* The container were we are want to drop
*/
disallowInsertion: function(aContainer) {
NS_ASSERT(aContainer, "empty container");
// Allow dropping into Tag containers and editable folders.
return !PlacesUtils.nodeIsTagQuery(aContainer) &&
(!PlacesUtils.nodeIsFolder(aContainer) ||
PlacesUIUtils.isContentsReadOnly(aContainer));
},
placesFlavors: [PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER,
PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR,
PlacesUtils.TYPE_X_MOZ_PLACE],
// The order matters.
GENERIC_VIEW_DROP_TYPES: [PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER,
PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR,
PlacesUtils.TYPE_X_MOZ_PLACE,
PlacesUtils.TYPE_X_MOZ_URL,
TAB_DROP_TYPE,
PlacesUtils.TYPE_UNICODE],
};
XPCOMUtils.defineLazyServiceGetter(PlacesControllerDragHelper, "dragService",
"@mozilla.org/widget/dragservice;1",
"nsIDragService");
function goUpdatePlacesCommands() {
// Get the controller for one of the places commands.
var placesController = doGetPlacesControllerForCommand("placesCmd_open");
function updatePlacesCommand(aCommand) {
goSetCommandEnabled(aCommand, placesController &&
placesController.isCommandEnabled(aCommand));
}
updatePlacesCommand("placesCmd_open");
updatePlacesCommand("placesCmd_open:window");
updatePlacesCommand("placesCmd_open:privatewindow");
updatePlacesCommand("placesCmd_open:tab");
updatePlacesCommand("placesCmd_new:folder");
updatePlacesCommand("placesCmd_new:bookmark");
updatePlacesCommand("placesCmd_new:livemark");
updatePlacesCommand("placesCmd_new:separator");
updatePlacesCommand("placesCmd_show:info");
updatePlacesCommand("placesCmd_moveBookmarks");
updatePlacesCommand("placesCmd_reload");
updatePlacesCommand("placesCmd_sortBy:name");
updatePlacesCommand("placesCmd_openParentFolder");
updatePlacesCommand("placesCmd_cut");
updatePlacesCommand("placesCmd_copy");
updatePlacesCommand("placesCmd_paste");
updatePlacesCommand("placesCmd_delete");
}
function doGetPlacesControllerForCommand(aCommand)
{
// A context menu may be built for non-focusable views. Thus, we first try
// to look for a view associated with document.popupNode
let popupNode;
try {
popupNode = document.popupNode;
} catch (e) {
// The document went away (bug 797307).
return null;
}
if (popupNode) {
let view = PlacesUIUtils.getViewForNode(popupNode);
if (view && view._contextMenuShown)
return view.controllers.getControllerForCommand(aCommand);
}
// When we're not building a context menu, only focusable views
// are possible. Thus, we can safely use the command dispatcher.
let controller = top.document.commandDispatcher
.getControllerForCommand(aCommand);
if (controller)
return controller;
return null;
}
function goDoPlacesCommand(aCommand)
{
let controller = doGetPlacesControllerForCommand(aCommand);
if (controller && controller.isCommandEnabled(aCommand))
controller.doCommand(aCommand);
}
|