public
anonymous / zotfile.js
Created

Slightly improved ZotFile.js (with file move error handling)

  • Download Gist
zotfile.js
JavaScript
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 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
Zotero.ZotFile = {
prefs: null,
wm: null,
fileMap: {}, //maps collections to their file objects
folderSep:null,
projectNr: new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15"),
projectPath: new Array("","","","","","","","","","","","","","",""),
projectMax:15,
zotfileURL:"http://www.columbia.edu/~jpl2136/zotfile.html",
temp:"",
 
 
// ========================= //
// FUNCTIONS: INIT FUNCTIONS //
// ========================= //
firstRun: function() {
// transfer preferences and uninstall zotreader
if(!Zotero.isFx36) AddonManager.getAddonByID("zotreader@columbia.edu",function(aAddon) {
// transfer preferences
Zotero.ZotFile.prefs.setCharPref("tablet.dest_dir",Zotero.ZotReader.prefs.getCharPref("dest_dir"));
Zotero.ZotFile.prefs.setCharPref("tablet.subfolderFormat",Zotero.ZotReader.prefs.getCharPref("subfolderFormat"));
Zotero.ZotFile.prefs.setBoolPref("tablet.subfolder",Zotero.ZotReader.prefs.getBoolPref("subfolder"));
Zotero.ZotFile.prefs.setIntPref("tablet.mode",Zotero.ZotReader.prefs.getIntPref("mode"));
Zotero.ZotFile.prefs.setIntPref("tablet.projectFolders",Zotero.ZotReader.prefs.getIntPref("projectFolders"));
Zotero.ZotFile.prefs.setIntPref("tablet.mode",Zotero.ZotReader.prefs.getIntPref("mode"));
 
for (i=0;i<Zotero.ZotFile.projectMax;i++) {
Zotero.ZotFile.prefs.setBoolPref("tablet.projectFolders"+Zotero.ZotFile.projectNr[i],Zotero.ZotReader.prefs.getBoolPref("projectFolders"+Zotero.ZotFile.projectNr[i]));
Zotero.ZotFile.prefs.setCharPref("tablet.projectFolders"+Zotero.ZotFile.projectNr[i]+"_folder",Zotero.ZotReader.prefs.getCharPref("projectFolders"+Zotero.ZotFile.projectNr[i]+"_folder"));
Zotero.ZotFile.prefs.setCharPref("tablet.projectFolders"+Zotero.ZotFile.projectNr[i]+"_label",Zotero.ZotReader.prefs.getCharPref("projectFolders"+Zotero.ZotFile.projectNr[i]+"_label"));
}
Zotero.ZotFile.prefs.setBoolPref("tablet",true);
//uninstall zotreader
aAddon.uninstall();
// prompt for restart
if (confirm("ZotFile has transfered most of your preferences from ZotFile Reader to the new version of ZotFile and uninstalled the ZotFile Reader add-on.\n\nPlease restart Firefox now!")) {
var boot = Components.classes["@mozilla.org/toolkit/app-startup;1"].getService(Components.interfaces.nsIAppStartup);
boot.quit(Components.interfaces.nsIAppStartup.eForceQuit|Components.interfaces.nsIAppStartup.eRestart);
}
});
},
 
versionChanges: function (currentVersion) {
// open webpage
if(this.prefs.getCharPref("version")==="" || currentVersion=="2.0") {
if(!Zotero.isStandalone) this.futureRun(function(){gBrowser.selectedTab = gBrowser.addTab(Zotero.ZotFile.zotfileURL); });
if( Zotero.isStandalone) this.futureRun(function(){ZoteroPane_Local.loadURI(Zotero.ZotFile.zotfileURL); });
}
 
// add saved search and change tag when upgrading to 2.1
if(currentVersion=="2.1" && this.prefs.getBoolPref("tablet")) {
// create saved search for modified tablet items
this.createSavedSearch("tablet_modified");
 
if(Zotero.Tags.getID("_READ",0)!==false) {
try {
// change tablet tag
Zotero.Tags.rename(Zotero.Tags.getID("_READ",0), "_tablet");
 
// show message
this.infoWindow("ZotFile Warning","In ZotFile 2.1, the tag for tablet attachments was changed from '_READ' to '_tablet'. Please do not use or change the '_tablet' tag manually!",8000);
 
} catch (ex) {
alert("Warning: ZotFile has changed the tag for attachments on the tablet from '_READ' to '_tablet' but was unable to automatically change the existing tag. Please make the changes manually or ask for help in the zotfile thread on the zotero forum.");
}
}
 
// change saved searches
var searches=Zotero.Searches.getAll();
for(var i=0; i<searches.length;i++ ) {
var conditions=searches[i].getSearchConditions();
for(var j=1; j<conditions.length;j++ ) {
if(conditions[j].condition=="tag" && conditions[j].value=="_READ") {
searches[i].updateCondition(conditions[j].id,'tag',conditions[j].operator,'_tablet');
searches[i].save();
}
}
}
}
// set current version
this.prefs.setCharPref("version",currentVersion);
 
// code for specific version upgrades
// if(currentVersion=="2.1" && oldVersion!="2.1")
 
},
init: function () {
 
// only do this stuff for the first run
if(this.prefs===null) {
//get preference objects
this.prefs = Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefService);
this.prefs = this.prefs.getBranch("extensions.zotfile.");
 
this.ffPrefs = Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefService).getBranch("browser.download.");
this.wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
// set source dir to custom folder if zotero standalone
if(Zotero.isStandalone && this.prefs.getBoolPref('source_dir_ff')) this.prefs.setBoolPref('source_dir_ff',false);
 
// version handeling
var oldVersion=this.prefs.getCharPref("version");
if(!Zotero.isFx36) Components.utils.import("resource://gre/modules/AddonManager.jsm");
// if first run, check for zotfile reader and transfer preferences
if (oldVersion=="") this.firstRun();
 
// update current version
if(!Zotero.isFx36) AddonManager.getAddonByID("zotfile@columbia.edu",function(aAddon) {
var currentVersion=aAddon.version;
// if different version then previously
if(currentVersion!=oldVersion) Zotero.ZotFile.versionChanges(currentVersion);
});
 
if(Zotero.isFx36) {
var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
var addon = em.getItemForID("zotfile@columbia.edu");
if(addon.version!=oldVersion) this.versionChanges(addon.version);
}
// run in future to not burden start-up
this.futureRun(function(){
// determine folder seperator depending on OS
Zotero.ZotFile.folderSep="/";
if (Zotero.isWin) Zotero.ZotFile.folderSep="\\";
 
// check whether extraction of annotations is supported
if (Zotero.ZotFile.pdfAnnotations.popplerSupportedPlatforms.join().indexOf(Zotero.platform)!=-1) Zotero.ZotFile.pdfAnnotations.popplerExtractorSupported=true;
 
// set path and check whether installed
if (Zotero.ZotFile.pdfAnnotations.popplerExtractorSupported) {
// set Extractor Path
Zotero.ZotFile.pdfAnnotations.popplerExtractorSetPath();
 
// check whether tool is installed
Zotero.ZotFile.pdfAnnotations.popplerExtractorTool=Zotero.ZotFile.pdfAnnotations.popplerExtractorCheckInstalled();
// set to pdf.js if poppler is not installed
if(!Zotero.ZotFile.pdfAnnotations.popplerExtractorTool) Zotero.ZotFile.prefs.setBoolPref("pdfExtraction.UsePDFJS",true);
}
 
// set to pdf.js if poppler is not supported
if(!Zotero.ZotFile.pdfAnnotations.popplerExtractorSupported) Zotero.ZotFile.prefs.setBoolPref("pdfExtraction.UsePDFJS",true);
 
});
}
 
// add event listener for zotfile menu items
var win = this.wm.getMostRecentWindow("navigator:browser");
win.document.getElementById('zotero-itemmenu').addEventListener('popupshowing', this.showMenu, false);
 
 
// add event listener for selecting the 'modified tablet attachments' saved search
if(this.prefs.getBoolPref("tablet")) this.savedSearchEventListener(true);
// show items in right-click menu conditional on options using an event listener
// CODE NOT IMPLEMENTED (just to remember how it works if needed)
/*var cm = document.getElementById('zotero-itemmenu');
cm.addEventListener("popupshowing", this.showMenu, false);
// Register the callback in Zotero as an item observer
var notifierID = Zotero.Notifier.registerObserver(this.notifierCallback, ['item']);*/
},
 
savedSearchEventListener: function(activate) {
var win = this.wm.getMostRecentWindow("navigator:browser");
if(activate) win.document.getElementById('zotero-collections-tree').addEventListener('click', this.updateModifiedAttachmentsSearch, false);
if(!activate) win.document.getElementById('zotero-collections-tree').removeEventListener('click', this.updateModifiedAttachmentsSearch, false);
 
},
 
/*showMenu: function(event) {
Zotero.ZotFile.infoWindow("ZotFile Report","event fired!",8000);
},*/
// ============================ //
// FUNCTIONS: HELPER FUNCTIONS //
// ============================ //
// detect duplicates in array
removeDuplicates: function (x) {
x = x.sort();
var y = [];
 
y.push(x[0]);
for (var i=1; i < (x.length); i++) {
if (x[i-1] != x[i]) y.push(x[i]);
}
return(y);
},
 
// search and replace in array
arrayReplace: function (x,search, replace) {
for(var i=0; i<x.length;i++ ) {
if(x[i]==search) x.splice(i,1,replace);
}
return(x);
},
 
getSelectedAttachments: function () {
// get selected items
var win = this.wm.getMostRecentWindow("navigator:browser");
var items = win.ZoteroPane.getSelectedItems();
 
// create array of attachments to pull
var attIDs=[];
for (var i=0; i < items.length; i++) {
var item = items[i];
// regular item
if(item.isRegularItem()) {
// get all attachments
var attachments = item.getAttachments();
 
// go through all attachments and add those with a tag
for (var j=0; j < attachments.length; j++) attIDs.push(attachments[j]);
 
}
// attachment item
if(item.isAttachment()) attIDs.push(item.getID());
}
// remove duplicate elements
attIDs=this.removeDuplicates(attIDs);
 
return(attIDs);
},
 
futureRun: function(aFunc) {
var tm = Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager);
tm.mainThread.dispatch({run: function(){aFunc();}},Components.interfaces.nsIThread.DISPATCH_NORMAL);
},
createSavedSearch: function(which) {
if(which=="tablet" || which=="both") {
var search = new Zotero.Search();
search.addCondition('tag', 'is', this.prefs.getCharPref("tablet.tag"));
search.addCondition('includeParentsAndChildren', 'true');
search.addCondition('noChildren', 'true');
search.setName("Tablet Files");
search.save();
}
 
if(which=="tablet_modified" || which=="both") {
var search_modified = new Zotero.Search();
search_modified.addCondition('tag', 'is', this.prefs.getCharPref("tablet.tagModified"));
search_modified.setName("Tablet Files (modified)");
search_modified.save();
}
},
// ================== //
// FUNCTIONS: WINDOWS //
// ================== //
openPreferenceWindow: function (paneID, action) {
var io = {
pane: paneID,
action: action
};
window.openDialog('chrome://zotfile/content/options.xul',
'zotfile-options',
'chrome,titlebar,toolbar,centerscreen'+ Zotero.Prefs.get('browser.preferences.instantApply', true) ? 'dialog=no' : 'modal',io
);
},
 
openSubfolderWindow: function (paneID, action) {
var io = {
pane: paneID,
action: action
};
var prefWindow=window.openDialog('chrome://zotfile/content/options-projects.xul',
'zotfile-tablet-subfolders',
'chrome,titlebar,toolbar,centerscreen'+ Zotero.Prefs.get('browser.preferences.instantApply', true) ? 'dialog=no' : 'modal',io
);
},
 
chooseDirectory: function () {
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var win = wm.getMostRecentWindow('navigator:browser');
 
var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);
 
var nsIFilePicker = Components.interfaces.nsIFilePicker;
while (true) {
var fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(win, Zotero.getString('dataDir.selectDir'), nsIFilePicker.modeGetFolder);
fp.appendFilters(nsIFilePicker.filterAll);
if (fp.show() == nsIFilePicker.returnOK) {
var file = fp.file;
 
// Set preference
//Zotero.ZotFile.prefs.setCharPref(pref,file.path);
return(file.path);
}
else {
return("");
}
}
},
 
infoWindow: function(main, message, time){
var pw = new (Zotero.ProgressWindow);
pw.changeHeadline(main);
if (main=="error") pw.changeHeadline(Zotero.getString("general.errorHasOccurred")); pw.addDescription(message);
pw.show();
pw.startCloseTimer(time);
 
},
 
promptUser: function(message,option1,option2,option3) {
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);
 
var check = {value: false}; // default the checkbox to false
 
var flags = prompts.BUTTON_POS_0 * prompts.BUTTON_TITLE_IS_STRING +
prompts.BUTTON_POS_1 * prompts.BUTTON_TITLE_IS_STRING +
prompts.BUTTON_POS_2 * prompts.BUTTON_TITLE_IS_STRING;
 
var button = prompts.confirmEx(null, "ZotFile Dialog", message,
flags, option1,option2,option3, null, check);
 
// this.infoWindow("ZotReader Report","button " + button,8000);
 
return(button);
 
},
 
// ======================= //
// FUNCTIONS: ZOTFILE MENU //
// ======================= //
 
showMenu: function() {
var showMenu=false;
 
// get selected items
var win = Zotero.ZotFile.wm.getMostRecentWindow("navigator:browser");
var items = win.ZoteroPane.getSelectedItems();
 
//iterate through selected items
for (var i=0; i < items.length; i++) {
if(items[i].isAttachment() || items[i].isRegularItem()) {
showMenu=true;
break;
}
}
 
// show or hide zotfile menu items
win.document.getElementById("id-zotfile-separator").hidden = !showMenu;
win.document.getElementById("id-zotfile-attach-file").hidden = !showMenu;
win.document.getElementById("id-zotfile-manage-attachments").hidden = !showMenu;
},
 
buildZotFileMenu: function () {
var menuItemExtract=true;
 
// get selected items
var win = this.wm.getMostRecentWindow("navigator:browser");
var items = win.ZoteroPane.getSelectedItems();
var item;
 
// get menu and recreate structure of child items
var menu = win.ZoteroPane.document.getElementById('id-zotfile-menu');
var m = {
warning1:0,
rename: 1,
extractanno: 2,
sep1: 3,
warning2: 4,
push2reader: 5,
updatefile: 6,
pullreader: 7,
sep2: 8,
subfolders: 9,
warning3: 10,
push2readerFolder:new Array(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25),
sep3: 26,
menuConfigure: 27,
length:28
};
 
// list of disabled and show menu-items
var disable = [m.subfolders,m.warning1,m.warning2,m.warning3], show = [];
 
// check selected items
var groupLib=1;
if (!items[0].libraryID) groupLib=0;
var oneItem=0;
var oneAtt=0;
var onePushed=0;
for (var i=0; i < items.length; i++) {
item=items[i];
if(item.isRegularItem()) {
oneItem=1;
// get all attachments
var attachments = item.getAttachments();
 
// go through all attachments
for (var j=0; j < attachments.length; j++) {
oneAtt=1;
// get current attachments
var att = Zotero.Items.get(attachments[j]);
if(att.hasTag(Zotero.Tags.getID(this.prefs.getCharPref("tablet.tag"),0))) onePushed=1;
}
}
 
// attachment item
if(item.isAttachment()) {
oneAtt=1;
oneItem=1;
if(item.hasTag(Zotero.Tags.getID(this.prefs.getCharPref("tablet.tag"),0))) onePushed=1;
}
if(onePushed==1) break;
 
}
 
// check whether destitination folder is defined (and valid)
var dest_dir_valid=this.fileExists(this.prefs.getCharPref("tablet.dest_dir"));
// if(this.prefs.getCharPref("tablet.dest_dir")!="") var dest_dir_valid=1;
 
// warnings
if(!oneItem) {
show.push(m.warning1);
menu.childNodes[m.warning1].setAttribute('label',"No item or attachment selected.");
}
 
 
// at least one item and one attachment
if(oneItem) {
// add 'new att' and 'rename'
show = [
m.rename
];
// warning
if(!oneAtt) {
disable.push(m.rename, m.extractanno);
show.push(m.sep1,m.warning2);
menu.childNodes[m.warning2].setAttribute('label',"Selected item(s) have no attachments.");
}
 
// add 'Extract annotations'
if(this.prefs.getBoolPref("pdfExtraction.MenuItem")) show.push(m.extractanno);
 
// tablet menu part
if(this.prefs.getBoolPref("tablet") && oneAtt) {
// add sep
show.push(m.sep1);
// warnings
if(!dest_dir_valid) {
show.push(m.warning2);
menu.childNodes[m.warning2].setAttribute('label',"Location for tablet files is not defined or invalid.");
}
if(groupLib) {
show.push(m.warning2);
menu.childNodes[m.warning2].setAttribute('label',"Selected item is in group library.");
}
if(dest_dir_valid && !groupLib) {
show.push(m.push2reader,m.pullreader);
 
// set tooltip for base folder
menu.childNodes[m.push2reader].setAttribute('tooltiptext',"Send Attachment File to \'" + this.prefs.getCharPref("tablet.dest_dir") + "\'");
 
if(!onePushed) disable.push(m.pullreader);
 
// add update menu item
if(this.checkSelectedSearch() || this.prefs.getBoolPref("tablet.updateAlwaysShow")) {
show.push(m.updatefile);
if(!onePushed) disable.push(m.updatefile);
}
 
// Collection based project folders
var projectsSet=0;
if(this.prefs.getIntPref("tablet.projectFolders")==1) {
show.push(m.sep2,m.subfolders);
 
// get first selected item
item=items[0];
if(item.isAttachment()) if(item.getSource()) item=Zotero.Items.get(item.getSource());
 
// create folders from collections
var folders = [];
var collections=item.getCollections();
for (i=0;i<collections.length;i++) {
var collection=Zotero.Collections.get(collections[i]);
var folder=this.folderSep + collection.getName();
var parent=collection.getParent();
while (parent) {
parent=Zotero.Collections.get(parent);
folder=this.folderSep + parent.getName() + folder;
parent=parent.getParent();
}
folders.push(folder);
}
 
// add folders to menu
if(folders.length) {
projectsSet=1;
folders=folders.sort();
for (i=0;i<folders.length;i++) {
show.push(m.push2readerFolder[i]);
menu.childNodes[m.push2readerFolder[i]].setAttribute('label',folders[i]);
menu.childNodes[m.push2readerFolder[i]].setAttribute('tooltiptext',"Send Attachment File to \'..." + folders[i] + "\'");
this.projectPath[i]=folders[i];
if(i>9) break;
}
}
}
 
// User defined project folders
if(this.prefs.getIntPref("tablet.projectFolders")==2) {
show.push(m.sep2,m.subfolders,m.sep3,m.menuConfigure);
for (i=0;i<this.projectMax;i++) {
if(this.prefs.getBoolPref("tablet.projectFolders" + this.projectNr[i])) {
show.push(m.push2readerFolder[i]);
menu.childNodes[m.push2readerFolder[i]].setAttribute('label', this.prefs.getCharPref("tablet.projectFolders" + this.projectNr[i] +"_label"));
menu.childNodes[m.push2readerFolder[i]].setAttribute('tooltiptext',"Send Attachment File to \'..." + this.prefs.getCharPref("tablet.projectFolders" + this.projectNr[i] +"_folder") + "\'");
projectsSet=1;
}
}
}
 
// message that no folders are defined
if(!projectsSet && this.prefs.getIntPref("tablet.projectFolders")!=0) {
var warning;
show.push(m.warning3);
if(this.prefs.getIntPref("tablet.projectFolders")==1) warning="Item is in no collection.";
if(this.prefs.getIntPref("tablet.projectFolders")==2) warning="No subfolders defined.";
menu.childNodes[m.warning3].setAttribute('label', warning);
}
}
}
}
 
 
// enable all items by default
for (i=0;i<m.length;i++) menu.childNodes[i].setAttribute('disabled', false);
// disable menu items
for (i in disable) menu.childNodes[disable[i]].setAttribute('disabled', true);
// Hide all items by default
for (i=0;i<m.length;i++) menu.childNodes[i].setAttribute('hidden', true);
// Show items
for (i in show) menu.childNodes[show[i]].setAttribute('hidden', false);
 
},
 
 
// =================================== //
// FUNCTIONS: GET FILE- & FOLDER NAME //
// =================================== //
addUserInput: function(filename, original_filename){
var default_str = this.prefs.getCharPref("userInput_Default");
if (default_str=="[original filename]") default_str=original_filename;
var filesuffix = prompt("Enter file suffix (press Cancel to add nothing)\n\nOriginal Filename\n"+original_filename+"\n\nNew Filename\n"+filename + " (YOUR INPUT)", default_str);
if (filesuffix != '' && filesuffix != null) {
// add file type to the file name
filename = filename + " (" + filesuffix + ")";
}
return(filename);
},
truncateTitle: function(title){
// truncnate title after : . and ?
if(this.prefs.getBoolPref("truncate_title")) {
var truncate = title.search(/:|\.|\?/);
if(truncate!=-1) title = title.substr(0,truncate);
}
// truncate if to long
var title_length = title.length;
if (title_length>this.prefs.getIntPref("max_titlelength")) {
var max_titlelength=this.prefs.getIntPref("max_titlelength");
var before_trunc_char = title.substr(max_titlelength,1);
// truncate title at max length
title = title.substr(0,max_titlelength);
// remove the last word until a space is found
if(this.prefs.getBoolPref("truncate_smart") && title.search(" ")!=-1 && before_trunc_char.search(/[a-zA-Z0-9]/!=-1)) {
while (title.substring(title.length-1, title.length) != ' ') title = title.substring(0, title.length-1);
title = title.substring(0, title.length-1);
}
} else {
// remove some non letter characters if they apear at the end of the title that was not truncated
var endchar = title.substring(title.length-1, title.length);
if (endchar == ':' || endchar == '?' || endchar == '.' || endchar == '/' || endchar == '\\' || endchar == '>' || endchar == '<' || endchar == '*' || endchar == '|') {
title = title.substring(0, title.length-1);
}
}
// replace forbidden characters with meaningful alternatives (they can only apear in the middle of the text at this point)
title = title.replace(/[\/\\]/g, '-');
title = title.replace(/[\*|"<>]/g, '');
title = title.replace(/[\?:]/g, ' -');
return(title);
},
// Function replaces wildcard both for filename and subfolder definition
replaceWildcard: function(zitem, rule){
// get item type
var item_type = zitem.getType();
var item_type_string = Zotero.ItemTypes.getLocalizedString(item_type);
// get title of selected item
var title = zitem.getField('title');
// truncnate title
title = this.truncateTitle(title);
// get journal
var journal = zitem.getField('publicationTitle');
 
// get journal abbreviation
var journal_abb = zitem.getField('journalAbbreviation');
 
// get publisher
var publisher = zitem.getField('publisher');
 
// get volume and issue
var volume = zitem.getField('volume');
var issue = zitem.getField('issue');
 
// get patent stuff
// var inventor
var assignee = zitem.getField('assignee');
var patentnr = zitem.getField('patentNumber');
var priority_date = patentnr.substr(2,4);
 
// get creator and create authors string
// creator types: author/editor(1,3) for book(2), inventor(14) for patent(19),programmer(24) for computer prog.(27),presenter(21) for presentation(32)
var creatorType=[1];
if (zitem.getType()==2) creatorType=[1,3];
if (zitem.getType()==19) creatorType=[14];
if (zitem.getType()==32) creatorType=[21];
if (zitem.getType()==27) creatorType=[24];
var add_etal=this.prefs.getBoolPref("add_etal");
var author = "";
var creators = zitem.getCreators();
var numauthors = creators.length;
for (var i=0; i < creators.length; i++) {
if(creatorType.indexOf(creators[i].creatorTypeID)==-1) numauthors=numauthors-1;
}
var max_authors=(this.prefs.getBoolPref("truncate_authors")) ? this.prefs.getIntPref("max_authors") : 500;
if (numauthors<=max_authors) add_etal=0;
if (numauthors>max_authors) numauthors = 1;
var j=0;
for (i=0; i < creators.length; i++) {
if (j<numauthors && creatorType.indexOf(creators[i].creatorTypeID)!=-1) {
if (author!="") author = author + "_" + creators[i].ref.lastName;
if (author=="") author = creators[i].ref.lastName;
j=j+1;
}
}
if (add_etal==1) author = author + this.prefs.getCharPref("etal");
 
// date
var year_issue="";
var year = zitem.getField('date', true).substr(0,4);
if(item_type==19) {
year_issue = zitem.getField('issueDate', true).substr(0,4);
year = year_issue;
}
// create output from rule
var field=0;
var output='';
for (i=0; i<rule.length; i++) {
var char=rule.charAt(i);
switch (char) {
case '%':
field=1;
break;
 
case 'a':
if (field==1) output = output + author;
field=0;
break;
case 'A':
if (field==1) output = output + author.substr(0,1).toUpperCase();
field=0;
break;
 
case 't':
if (field==1) output = output + title;
field=0;
break;
 
case 'y':
if (field==1) output = output + year;
field=0;
break;
 
case 'j':
if (field==1) output = output + journal;
field=0;
break;
 
case 'p':
if (field==1) output = output + publisher;
field=0;
break;
 
case 'n':
if (field==1) output = output + patentnr;
field=0;
break;
 
case 'i':
if (field==1) output = output + assignee;
field=0;
break;
 
case 'u':
if (field==1) output = output + year_issue;
field=0;
break;
 
case 'w':
if (field==1) {
output = output + journal;
if(journal=="") output = output + publisher;
}
field=0;
break;
 
case 's':
if (field==1) output = output + journal_abb;
field=0;
break;
 
case 'v':
if (field==1) output = output + volume;
field=0;
break;
 
case 'e':
if (field==1) output = output + issue;
field=0;
break;
case 'T':
if (field==1) output = output + item_type_string;
field=0;
break;
 
default: output = output + char;
}
}
return(output);
},
getFiletype: function(fname){
if(fname) {
var temp = [];
temp = fname.split('.');
return(temp[temp.length-1].toLowerCase());
}
else {
return("");
}
},
checkFileType: function (file) {
if(!this.prefs.getBoolPref("useFileTypes")) return(true);
// check
var filetype=this.getFiletype(file.leafName);
var type=filetype.search(new RegExp(this.prefs.getCharPref("filetypes").replace(/,/gi,"|")));
if (type>=0) {
return(true);
}
else {
return(false);
}
},
 
completePath: function(location,filename) {
return (location + this.folderSep + filename);
},
 
addSuffix: function(filename,k) {
var temp = [];
temp = filename.split('.');
return(temp[0] + k + "." + this.getFiletype(filename));
},
getFilename: function(item,filename_org){
var filename;
// create the new filename from the selected item
var item_type = item.getType();
var rename_rule=this.prefs.getCharPref("renameFormat");
if(item_type==19) rename_rule=this.prefs.getCharPref("renameFormat_patent");
if (!this.prefs.getBoolPref("useZoteroToRename")) {
filename=this.replaceWildcard(item, rename_rule);
//var filename = author + "_" + year + "_" + title;
 
// Strip potentially invalid characters
// (code line adopted from Zotero)
filename = filename.replace(/[\/\\\?\*:|"<>\.]/g, '');
 
// replace multiple blanks in filename with single blank & remove whitespace
//var filename = filename.replace(/ {2,}/g, ' ');
filename = Zotero.Utilities.trimInternal(filename);
 
// replace blanks with '_' if option selected
if (this.prefs.getBoolPref("replace_blanks")) filename = filename.replace(/ /g, '_');
 
// remove all the accents and other strange characters from filename
if (Zotero.version[0]>=3 && this.prefs.getBoolPref("removeDiacritics")) filename = Zotero.Utilities.removeDiacritics(filename);
}
if (this.prefs.getBoolPref("useZoteroToRename")) filename=Zotero.Attachments.getFileBaseNameFromItem(item.itemID);
if(this.prefs.getBoolPref("userInput")) filename=this.addUserInput(filename,filename_org);
// add filetype to filename
if(filename_org!="") filename = filename + "." + this.getFiletype(filename_org);
// return
filename = Zotero.File.getValidFileName(filename);
return(filename);
},
 
getLocation: function(zitem, dest_dir,subfolder, rule) {
var subfolderFormat="";
if(subfolder) {
subfolderFormat=this.replaceWildcard(zitem, rule);
if (!Zotero.isWin) subfolderFormat=subfolderFormat.replace('//','/undefined/');
if ( Zotero.isWin) subfolderFormat=subfolderFormat.replace('\\\\','\\undefined\\');
}
 
// var journal = zitem.getField('publicationTitle');
var folder = dest_dir + subfolderFormat;
return(folder);
//return(folder.replace(/[\?%\*:|"<>]/g, ''));
},
 
// ================ //
// FUNCTIONS: FILES //
// ================ //
 
createFile: function(path) {
try {
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path);
return(file);
}
catch (err) {
return(-1);
}
},
 
// function to check whether a file exists
// argument: path as string (with optional filename), zotero att, or file obj
fileExists: function (arg, filename) {
var file;
// when string is passed
if(typeof(arg)=='string') {
if(filename!=null) arg=this.completePath(arg,filename);
file=this.createFile(arg);
}
// when object (i.e. zotero attachment item) is passed
if(typeof(arg)=='object') {
if( arg.getFile) file=arg.getFile();
if(!arg.getFile) file=arg;
}
// check whether the file exsists
try {
return(file.exists());
}
catch (err) {
return(false);
}
},
 
moveFile: function(file, destination, filename, att_name){
/* moves 'file' to 'destination' path an renames it to 'filename'; 'att_name' is the attachment title used in error messages
* -> returns the path to the new (created) file, in case of an error: path="NULL"
*/
// file.path!= this.createFile(this.completePath(location, filename)).path
if(file.path!=this.completePath(destination,filename)) {
var filename_temp=filename;
var k=2;
while(this.fileExists(destination, filename_temp)) {
filename_temp = this.addSuffix(filename,k);
k++;
if(k>99)
//TODO There should be a prompt window which let the user choose a name
// If not, it would create an error like file exists or more severe: it will override the existing file
break;
}
filename=filename_temp;
try {
// create a nslFile Object of the destination folder
var dir = this.createFile(destination);
 
// move file to new location
file.moveTo(dir, filename);
}
catch(err) {
if(err.name == "NS_ERROR_FILE_IS_LOCKED")
this.infoWindow("ZotFile Report",
"ZotFile was unable to move the attachment with name '" + att_name + "' because it is locked. " +
"Probably it is opened in a program, so please close it.",
100000);
else
this.infoWindow("ZotFile Report",
"ZotFile gets an untreated error while moving the attachment with name '" + att_name + ". \n\n" +
"Error details: " + err,
100000);
file.path = "NULL";
}
}
return(file.path);
},
 
copyFile: function(file, destination, filename){
 
// create a nslFile Object of the destination folder
var dir = this.createFile(destination);
 
// check whether already exists and add name if it does
if(file.path!=this.completePath(destination,filename)) {
 
var filename_temp=filename;
var k=2;
while(this.fileExists(destination,filename_temp)) {
filename_temp = this.addSuffix(filename,k);
k++;
if(k>99) break;
}
filename=filename_temp;
 
// copy file
file.copyTo(dir, filename);
}
 
// return file
return(this.createFile(this.completePath(dir.path,filename)));
 
},
 
removeFile: function(file) {
if(file.exists()) {
try {
file.remove(false);
}
catch(err){
if(file.isDirectory()) this.infoWindow("ZotFile Report","ZotFile was unable delete the old folder probably because other files are in the folder.",8000);
}
}
},
showFolder: function(folderFile) {
// create folder if it does not exsist
Zotero.File.createDirectoryIfMissing(folderFile);
 
// open folder in file system
folderFile.QueryInterface(Components.interfaces.nsILocalFile);
try {
folderFile.reveal();
}
catch (e) {
// On platforms that don't support nsILocalFile.reveal() (e.g. Linux), we
// open a small window with a selected read-only textbox containing the
// file path, so the user can open it, Control-c, Control-w, Alt-Tab, and
// Control-v the path into another app
var io = {alertText: folderFile.path};
window.openDialog('chrome://zotero/content/selectableAlert.xul', "zotero-reveal-window", "chrome", io);
}
},
// ============================== //
// FUNCTIONS: ATTACHING NEW FILES //
// ============================== //
getAllFilesInFolder: function(dir_path){
var return_files=[];
// create a nslFile Object for the dir
try {
var dir = this.createFile(dir_path);
var success=0;
 
// go through all the files in the dir
var files = dir.directoryEntries;
while (files.hasMoreElements()) {
// get one after the other file
var file = files.getNext();
file.QueryInterface(Components.interfaces.nsIFile);
// only look at files which are neither folders nor hidden
if(!file.isDirectory() && !file.isHidden()) {
// is this a file we want to work with?
if (this.checkFileType(file)) {
return_files[success]=file;
success=success+1;
}
}
}
if (success>0) return(return_files);
else return(-1);
 
} catch (e) {
Components.utils.reportError(e);
return (-2);
}
 
},
 
getLastFileInFolder: function(dir_path){
var return_files=[];
// create a nslFile Object for the dir
try {
var dir = this.createFile(dir_path);
var lastfile_date=0;
var lastfile_path="";
var success=0;
 
// go through all the files in the dir
var i=0;
var files = dir.directoryEntries;
while (files.hasMoreElements()) {
// get one after the other file
var file = files.getNext();
file.QueryInterface(Components.interfaces.nsIFile);
// only look at files which are neither folders nor hidden
if(!file.isDirectory() && !file.isHidden()) {
// now we want to check which filetype we are looking at
// we only want to consider pdfs, docs, ...
if (this.checkFileType(file)) {
var modtime = file.lastModifiedTime;
i=i+1;
// finally, we set return_files to the file with the most recent modification
if (modtime>lastfile_date){
lastfile_date=modtime;
return_files[0]=file;
// lastfile=file;
success=1;
}
}
}
}
if (success==1) return(return_files);
else return(-1);
} catch (e) {
Components.utils.reportError(e);
return (-2);
}
 
},
 
getFFDownloadFolder: function () {
var path="";
try {
if(this.ffPrefs.getBoolPref('useDownloadDir')) {
var downloadManager = Components.classes["@mozilla.org/download-manager;1"]
.getService(Components.interfaces.nsIDownloadManager);
path=downloadManager.userDownloadsDirectory.path;
}
if(!this.ffPrefs.getBoolPref('useDownloadDir') && this.ffPrefs.prefHasUserValue('lastDir') ) {
path=this.ffPrefs.getCharPref('lastDir');
}
}
catch (err) {
path="";
}
return(path);
},
getSourceDir: function(message) {
var source_dir="";
if ( this.prefs.getBoolPref("source_dir_ff")) source_dir=this.getFFDownloadFolder();
if (!this.prefs.getBoolPref("source_dir_ff")) source_dir=this.prefs.getCharPref("source_dir");
// test whether valid source dir
if (source_dir!="" && this.fileExists(source_dir)) {
return (source_dir);
} else {
if(message) this.infoWindow("ZotFile Error","The source folder is not valid. Please change the the source folder under Zotero-Actions-Zotfile Preferences. You might have to use a custom folder.",8000);
return(-1);
}
 
},
// FUNCTION: Attach New File(s) from Download Folder
attachNewFile: function(){
var win = this.wm.getMostRecentWindow("navigator:browser");
var items = win.ZoteroPane.getSelectedItems();
// var items = ZoteroPane.getSelectedItems();
 
var item = items[0];
 
//check whether it really is an bibliographic item (no Attachment, note or collection)
if (item.isRegularItem()) {
 
// check whether valid FF default download folder
if(this.prefs.getBoolPref('source_dir_ff') && this.getSourceDir(false)==-1) {
this.prefs.setBoolPref('source_dir_ff',false);
this.prefs.setCharPref('source_dir',prompt("ZotFile Settings\n\nZotfile is not able to determine your default FF download folder. Please enter a custom source dir. The source dir is the directory where ZotFile looks for the most recently modified file when you use the ZotFile function 'Attach New File'."));
return;
}
 
// get source dir
var source_dir=this.getSourceDir(true);
 
// exit if getting source dir was not successful
if (source_dir==-1) return;
 
// get files from source dir
if (!this.prefs.getBoolPref("allFiles")) file=this.getLastFileInFolder(source_dir);
if ( this.prefs.getBoolPref("allFiles")) file=this.getAllFilesInFolder(source_dir);
 
// attach them
if(file!=-1 && file!=-2) {
for (var i=0; i < file.length; i++) {
 
// confirmation from user
var file_oldpath=file[i].leafName;
var confirmed=1;
if (this.prefs.getBoolPref("confirmation")) confirmed=confirm("Do you want to rename and attach/link the file \'" + file_oldpath + "\' to the currently selected Zotero item?");
if(confirmed){
var attID;
// create linked attachment if local library
if (!item.libraryID) attID=Zotero.Attachments.linkFromFile(file[i], item.itemID,item.libraryID);
 
// import attachment if cloud library
if (item.libraryID) {
attID=Zotero.Attachments.importFromFile(file[i], item.itemID,item.libraryID);
this.removeFile(file[i]);
}
 
// Rename and Move Attachment
var att = Zotero.Items.get(attID);
this.renameAttachment(item, att,this.prefs.getBoolPref("import"),this.prefs.getCharPref("dest_dir"),this.prefs.getBoolPref("subfolder"),this.prefs.getCharPref("subfolderFormat"),true);
 
}
}
 
}
else this.infoWindow("Zotfile Error","Unable to find file(s) in " + source_dir,8000);
 
}
else this.infoWindow("Zotfile Error","Selected item is either an Attachment, a note, or a collection.",8000);
// else this.infoWindow("Zotfile Error","Selected item is in a Group Library.",8000);
 
},
// ============================ //
// FUNCTIONS: TABLET FUNCTIONS //
// =========================== //
clearInfo: function (att) {
att.setNote("");
att.save();
},
 
getInfo: function (att,tagname) {
try {
var note = att.getNote();
var search=note.search(tagname);
var content=note.substring(search);
content=content.substring(content.search("{")+1,content.search("}"));
 
// for location tag: replace [BaseFolder] with destination folder
if(tagname=="location") content=content.replace("[BaseFolder]",this.prefs.getCharPref("tablet.dest_dir"));
 
return(content);
}
catch (err) {
return("");
}
},
 
addInfo: function(att,tagname,value) {
 
var note = att.getNote();
var tag_content=this.getInfo(att,tagname).replace(this.prefs.getCharPref("tablet.dest_dir"),"[BaseFolder]");
 
// for location tag: replace destination folder with [BaseFolder]
if(tagname=="location" && this.prefs.getBoolPref("tablet.dest_dir_relativePath")) value=value.replace(this.prefs.getCharPref("tablet.dest_dir"),"[BaseFolder]");
 
// check whether tag already exists
var search=note.search(tagname);
 
// tag already exists
if (search!=-1) {
att.setNote(note.replace(tagname +"{"+tag_content+"}",tagname +"{"+value+"}"));
}
// tag does not exists
else {
if(note=="") att.setNote(note + tagname +"{"+value+"}");
if(note!="") att.setNote(note + "; " + tagname +"{"+value+"}");
}
att.save();
},
 
getTabletStatus: function(att) {
var tagID=Zotero.Tags.getID(this.prefs.getCharPref("tablet.tag"),0);
return(att.hasTag(tagID));
},
 
getTabletStatusModified: function(item) {
var modified=false;
if (this.getTabletStatus(item)) {
var file=this.getTabletFile(item);
 
if(file!==false) if (file.exists()) {
// get last modified time from att note and add att to list if file was modified
var lastmod=this.getInfo(item,"lastmod");
if(file.lastModifiedTime + ""!=lastmod) if (lastmod!="") modified=true;
}
}
return modified;
},
 
getTabletFile: function(att) {
try {
if(this.getTabletStatus(att)) {
// get file depending on mode
var file=this.getInfo(att,"mode")==1 ? this.createFile(this.getInfo(att,"location")) : att.getFile();
return(file);
}
return(false);
}
catch (err) {
return(false);
}
},
getTabletLocationFile: function(subfolder) {
if(subfolder==null) subfolder="";
return(this.createFile(this.prefs.getCharPref("tablet.dest_dir")+subfolder));
},
getAttachmentsOnTablet: function(subfolder) {
// search for attachments with tag
var search = new Zotero.Search();
search.addCondition('tag', 'is', this.prefs.getCharPref("tablet.tag"));
var results = search.search();
var items = Zotero.Items.get(results);
var atts = [];
// iterate through attachment items
for (var i=0; i < items.length; i++) {
var item = items[i];
 
// show warning if regular item with tablet tag
if(item.isRegularItem()) this.infoWindow("ZotFile Warning","A regular Zotero item has the \'" + this.prefs.getCharPref("tablet.tag") + "\' tag. This tag should only be used by zotfile and not assigned manually.",8000);
 
// check whether non-top level attachment
if(!item.isTopLevelItem() && item.isAttachment()) {
 
// show warning if no information in note
if(this.getInfo(item,"mode")==="") this.infoWindow("ZotFile Warning","The information stored in attachment notes is missing for an attachment on the tablet. Make sure that you do not delete this information manually.",8000);
if(this.getInfo(item,"mode")!="") {
if(subfolder===undefined) atts.push(item);
if(subfolder!==undefined) if(this.getInfo(item,"projectFolder").toLowerCase()==subfolder.toLowerCase()) atts.push(item);
}
}
}
// return attachments
return(atts);
},
 
getModifiedAttachmentsOnTablet: function (subfolder) {
var items=this.getAttachmentsOnTablet(subfolder);
var atts=[];
 
// iterate through attachment items
for (var i=0; i < items.length; i++) {
// get attachment item, parent and file
var item = items[i];
 
if(this.getTabletStatusModified(item)) atts.push(item);
}
// return attachments
return(atts);
},
 
setTabletFolder:function (items,projectFolder) {
for (var i=0; i < items.length; i++) {
var item = items[i];
if(item.getSource()) {
 
// get parent item
var parent=Zotero.Items.get(item.getSource());
 
// first pull if background mode
var att_mode=this.getInfo(item,"mode");
if(att_mode==1 || att_mode!=this.prefs.getIntPref("tablet.mode")) {
var itemID=this.getAttachmentFromTablet(parent,item,true);
item = Zotero.Items.get(itemID);
}
// now push
if(parent.isRegularItem()) {
if(projectFolder!==null) this.sendAttachmentToTablet(parent,item,projectFolder,false);
if(projectFolder===null) this.sendAttachmentToTablet(parent,item,this.getInfo(item,"projectFolder"),false);
}
}
}
// report
var mess_loc=(projectFolder!=="" && projectFolder!==null) ? ("'..." + projectFolder + "'.") : "the base folder.";
Zotero.ZotFile.infoWindow("ZotFile Report","ZotFile has moved " + items.length + " attachments to " + mess_loc,8000);
},
 
checkSelectedSearch: function() {
// get selected saved search
var win = this.wm.getMostRecentWindow("navigator:browser");
var savedSearch = win.ZoteroPane.getSelectedSavedSearch();
// returns false if no saved search is selected (e.g. collection)
 
// check whether saved search 'tablet files (modified)' is selected based on search conditions
var searchModifiedTabletFiles=false;
if(savedSearch!==false) {
var savedSearchConditions=savedSearch.getSearchConditions();
for (var i=1; i < savedSearchConditions.length; i++) {
if(savedSearchConditions[i].condition=="tag" && savedSearchConditions[i].value==this.prefs.getCharPref("tablet.tagModified")) searchModifiedTabletFiles=true;
}
}
return searchModifiedTabletFiles;
},
 
updateModifiedAttachmentsSearch: function(event) {
// debug output
if(Zotero.ZotFile.prefs.getBoolPref("debug")) Zotero.debug("zotfile.updateModifiedAttachmentsSearch - event fired");
// update saved search only if 'tablet files (modified)' saved search is selected
if(Zotero.ZotFile.checkSelectedSearch()) {
var items = Zotero.ZotFile.getModifiedAttachmentsOnTablet();
if(Zotero.ZotFile.prefs.getBoolPref("debug")) Zotero.debug("zotfile.updateModifiedAttachmentsSearch - saved search selected with " + items.length + " modified atts.");
for (var j=0; j < items.length; j++) items[j].addTag(Zotero.ZotFile.prefs.getCharPref("tablet.tagModified"));
 
// expand all rows in when search is selected
//var win = this.wm.getMostRecentWindow("navigator:browser");
//win.ZoteroPane.itemsView.expandAllRows();
}
},
scanTabletFiles: function() {
// get items on tablet
var items = this.getModifiedAttachmentsOnTablet();
if(items.length===0) this.infoWindow("ZotFile Report","Scan Tablet did not find any updated items in the destination folder.",8000);
// iterate through attachment items
for (var i=0; i < items.length; i++) {
// get attachment item, parent and file
var item = items[i];
var parent=Zotero.Items.get(item.getSource());
var file=this.getTabletFile(item);
 
// ask user
var userInput=this.promptUser("Attachment \'" + file.leafName + "\' was modified. What do you want to do?","Get Attachment from Tablet","Update Zotero File","Cancel");
 
// Pull attachment
if(userInput===0) this.getAttachmentFromTablet(parent,item,false);
 
// change modification date of attachment and update file
if(userInput==1) {
if(this.getInfo(item,"mode")==2) this.addInfo(item,"lastmod",file.lastModifiedTime);
if(this.getInfo(item,"mode")==1) {
var projectFolder=this.getInfo(item,"projectFolder");
 
// first pull if already on reader
// this.getAttachmentFromTablet(parent,item,true);
var att_mode=this.getInfo(item,"mode");
if(att_mode==1 || att_mode!=this.prefs.getIntPref("tablet.mode")) {
var itemID=this.getAttachmentFromTablet(parent,item,true);
item = Zotero.Items.get(itemID);
}
// now push
var newAttID=this.sendAttachmentToTablet(parent,item,projectFolder);
}
 
}
}
},
sendAttachmentToTablet: function(item, att, projectFolder, verbose) {
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.sendAttachmentToTablet - sending attachment " + att.getID() + " (mode is " + this.prefs.getIntPref("tablet.mode") + ")");
var newFile;
verbose = (typeof verbose == 'undefined') ? true : verbose;
var newAttID=null;
var file = att.getFile();
 
if(this.fileExists(att) && this.checkFileType(att.getFile())) {
// background mode: Rename and Move Attachment
if(this.prefs.getIntPref("tablet.mode")==1) {
 
// change name of file
if (this.prefs.getBoolPref("tablet.rename")) {
var filename=this.getFilename(item,file.leafName);
if(filename!=file.leafName) {
att.renameAttachmentFile(filename);
att.setField('title', filename);
att.save();
file = att.getFile();
}
}
newAttID=att.getID();
 
// add tags and catch error if it does not work
try {
if(!this.getTabletStatus(att)) att.addTag(this.prefs.getCharPref("tablet.tag"));
if (this.prefs.getBoolPref("tablet.tagParentPush")) item.addTag(this.prefs.getCharPref("tablet.tagParentPush_tag"));
}
catch (err) {
this.infoWindow("ZotFile Error","Zotfile was unable to send the file \'" + file.leafName + "\' to the tablet (error: tag assignment). Please try again and report the problem if it happens again.",8000);
return(null);
}
 
// create copy of file on tablet and catch errors
try {
// create copy on tablet
var folder=this.getLocation(item,this.prefs.getCharPref("tablet.dest_dir")+projectFolder,this.prefs.getBoolPref("tablet.subfolder"),this.prefs.getCharPref("tablet.subfolderFormat"));
newFile=this.copyFile(file,folder,file.leafName);
}
catch (err) {
if(this.getTabletStatus(att)) att.removeTag(Zotero.Tags.getID(this.prefs.getCharPref("tablet.tag"),0));
this.infoWindow("ZotFile Error","Zotfile was unable to send the file \'" + file.leafName + "\' to the tablet (error: moving the file). Please try again and report the problem if it happens again.",8000);
return(null);
}
}
 
// foreground mode: Rename and Move Attachment
if(this.prefs.getIntPref("tablet.mode")==2) {
newAttID=this.renameAttachment(item, att,false,this.prefs.getCharPref("tablet.dest_dir")+projectFolder,this.prefs.getBoolPref("tablet.subfolder"),this.prefs.getCharPref("tablet.subfolderFormat"),false);
 
// get new attachment and file
att = Zotero.Items.get(newAttID);
newFile = att.getFile();
 
// add tag to attachment
if(!this.getTabletStatus(att)) att.addTag(this.prefs.getCharPref("tablet.tag"));
if (this.prefs.getBoolPref("tablet.tagParentPush")) item.addTag(this.prefs.getCharPref("tablet.tagParentPush_tag"));
}
// add info to note (date of modification to attachment, location, and mode)
this.addInfo(att,"lastmod",newFile.lastModifiedTime);
this.addInfo(att,"mode",this.prefs.getIntPref("tablet.mode"));
this.addInfo(att,"location",newFile.path);
this.addInfo(att,"projectFolder",projectFolder);
// notification
if(verbose) this.infoWindow("ZotFile Report","The attachment \'" + newFile.leafName + "\' was sent to the tablet.",8000); // at \'" + projectFolder + "\'.
}
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.sendAttachmentToTablet - attachment send with new ID " + newAttID);
return(newAttID);
},
sendSelectedAttachmentsToTablet: function(project) {
// save current selection
var win = this.wm.getMostRecentWindow("navigator:browser");
var selection=win.ZoteroPane.itemsView.saveSelection();
// get selected attachments
var attIDs=this.getSelectedAttachments();
var attID;
 
// debug
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.sendSelectedAttachmentsToTablet - sending " + attIDs.length + " attachments to tablet");
// get projectFolder
var projectFolder="";
if (project!='') {
if(this.prefs.getIntPref("tablet.projectFolders")==1) projectFolder=this.projectPath[parseInt(project,10)-1];
if(this.prefs.getIntPref("tablet.projectFolders")==2) projectFolder=this.prefs.getCharPref("tablet.projectFolders" + project + "_folder");
}
 
// debug
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.sendSelectedAttachmentsToTablet - projectFolder set to '" + projectFolder +"'");
// Check which attachments are already on the reader
var attOnReader=[];
var attOnReaderCount=0;
for (var i=0; i < attIDs.length; i++) {
var hasTag=this.getTabletStatus(Zotero.Items.get(attIDs[i]));
attOnReader.push(hasTag);
if (hasTag) attOnReaderCount++;
}
 
// debug
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.sendSelectedAttachmentsToTablet - attachments on tablet: " + attOnReaderCount);
var repush=!this.prefs.getBoolPref("tablet.confirmRepush");
// Push attachments
var confirmed=1;
if (this.prefs.getBoolPref("confirmation_batch_ask") && attIDs.length>=this.prefs.getIntPref("confirmation_batch")) confirmed=confirm("Do you want to send " + attIDs.length + " attachments to the tablet?");
if(confirmed) {
if (!repush && attOnReaderCount>0) repush=confirm(attOnReaderCount + " of the selected attachments are already on the tablet. Do you want to replace these files on the tablet?");
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.sendSelectedAttachmentsToTablet - iterating through " + attIDs.length + " attachments...");
for (i=0; i < attIDs.length; i++) {
var att = Zotero.Items.get(attIDs[i]);
var item= Zotero.Items.get(att.getSource());
if(!attOnReader[i] || (attOnReader[i] && repush)) {
// first pull if already on reader
if (attOnReader[i]) {
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.sendSelectedAttachmentsToTablet - get attachment " + i + " from tablet before sending it");
var att_mode=this.getInfo(att,"mode");
if(att_mode==1 || att_mode!=this.prefs.getIntPref("tablet.mode")) {
attID=this.getAttachmentFromTablet(item,att,true);
att = Zotero.Items.get(attID);
}
}
// now push
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.sendSelectedAttachmentsToTablet - send attachment " + i);
attID=this.sendAttachmentToTablet(item,att,projectFolder);
if(attID!==null && attIDs[i]!=attID) selection=this.arrayReplace(selection,attIDs[i],attID);
}
}
// restore selection
if(Zotero.version>="3") win.ZoteroPane.itemsView.selectItems(selection);
 
// debug
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.sendSelectedAttachmentsToTablet - attachments sent to tablet");
}
if(confirmed===0 && this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.sendSelectedAttachmentsToTablet - sending attachments canceled by user");
},
 
updateSelectedTabletAttachments: function () {
// get selected attachments
var itemIDs=this.getSelectedAttachments();
var attID, newAttID;
 
// iterate through selected attachments
for (i=0; i < itemIDs.length; i++) {
var item = Zotero.Items.get(itemIDs[i]);
 
if(this.getTabletStatusModified(item)) {
// get parent and file
var parent=Zotero.Items.get(item.getSource());
var file=this.getTabletFile(item);
var filename=file.leafName;
 
var att_mode=this.getInfo(item,"mode");
if(att_mode==2) {
this.addInfo(item,"lastmod",file.lastModifiedTime);
var tagIDModified=Zotero.Tags.getID(this.prefs.getCharPref("tablet.tagModified"),0);
if(item.hasTag(tagIDModified)) item.removeTag(tagIDModified);
newAttID=item.getID();
}
if(att_mode==1) {
var projectFolder=this.getInfo(item,"projectFolder");
// first get from tablet
var itemID=this.getAttachmentFromTablet(parent,item,true);
item = Zotero.Items.get(itemID);
// now send back to reader
newAttID=this.sendAttachmentToTablet(parent,item,projectFolder,false);
}
 
// extract annotations
if (this.prefs.getBoolPref("tablet.updateExtractAnnotations")) this.pdfAnnotations.getAnnotations([newAttID]);
 
// show message
this.infoWindow("ZotFile Report","The attachment \'" + filename + "\' was synced between Zotero and the tablet folder.",8000);
}
}
},
getAttachmentFromTablet: function (item, att,fakeRemove) {
var attID=att.getID();
var option=2;
var itemPulled=false;
var att_mode=this.getInfo(att,"mode");
 
//debug
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.getAttachmentFromTablet - begin with mode " + att_mode);
 
// get files
var file_zotero=att.getFile();
var file_reader=this.getTabletFile(att);
 
// get modification times for files
var time_reader = file_reader.exists() ? parseInt(file_reader.lastModifiedTime+"",10) : 0;
var time_saved = parseInt(this.getInfo(att,"lastmod"),10);
var time_zotero = (file_zotero!=false) ? parseInt(file_zotero.lastModifiedTime+"",10) : 0;
 
//debug
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.getAttachmentFromTablet - modification times: tablet=" + time_reader + "; saved=" + time_saved + "; zotero=" + time_zotero);
 
// background mode
if(att_mode==1) {
if (time_reader!=0 || time_zotero!=0) {
// set options
if (time_reader>time_saved && time_zotero<=time_saved) option=0;
if (time_reader<=time_saved && time_zotero<=time_saved) option=1;
if (time_reader<=time_saved && time_zotero>time_saved) option=1;
if (time_reader>time_saved && time_zotero>time_saved) option=2;
// if attachment gets replaced
if (!this.prefs.getBoolPref("tablet.storeCopyOfFile")) {
// prompt if both file have been modified
if (option==2) option=this.promptUser("Both copies of the attachment file \'" + file_zotero.leafName + "\' have been modified. What do you want to do?\n\nRemoving the tablet file discards all changes made to the file on the tablet.","Replace Zotero File","Remove Tablet File","Cancel");
 
// Replace Zotero file
if(option==0) {
file_reader.moveTo(file_zotero.parent,file_zotero.leafName);
itemPulled=true;
}
}
// if saving a copy of the file as a new attachment with suffix
if (this.prefs.getBoolPref("tablet.storeCopyOfFile")) {
// only if reader file was modified
if(option!=1) {
var filename=this.addSuffix(file_zotero.leafName,this.prefs.getCharPref("tablet.storeCopyOfFile_suffix"));
//add linked attachment
if (!item.libraryID && !this.prefs.getBoolPref("import")) {
file_reader.moveTo(file_zotero.parent,filename);
attID=Zotero.Attachments.linkFromFile(file_reader, item.itemID,item.libraryID);
itemPulled=true;
}
//imports attachment
if (item.libraryID || this.prefs.getBoolPref("import")) {
// import file on reader
attID=Zotero.Attachments.importFromFile(file_reader, item.itemID,item.libraryID);
var attAnnotated = Zotero.Items.get(attID);
 
// rename file associated with attachment
attAnnotated.renameAttachmentFile(filename);
 
// change title of attachment item
attAnnotated.setField('title', filename);
attAnnotated.save();
// remove file on reader
this.removeFile(file_reader);
itemPulled=true;
}
}
}
// Pull without replacement (i.e. remove file on tablet)
if(option==1) {
this.removeFile(file_reader);
itemPulled=true;
}
}
}
// foreground mode
if(att_mode==2) {
attID=this.renameAttachment(item, att,this.prefs.getBoolPref("import"),this.prefs.getCharPref("dest_dir"),this.prefs.getBoolPref("subfolder"),this.prefs.getCharPref("subfolderFormat"),false);
att = Zotero.Items.get(attID);
itemPulled=true;
option = time_zotero>time_saved ? 0 : 1;
}
// post-processing if attachment has been removed & it's not a fake-pull
if (itemPulled && !fakeRemove) {
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.getAttachmentFromTablet - post-processing after attachment was removed");
// remove info (tag and att note)
var tagID=Zotero.Tags.getID(this.prefs.getCharPref("tablet.tag"),0);
att.removeTag(tagID);
this.clearInfo(att);
// extract annotations from attachment and add note
if (this.prefs.getBoolPref("pdfExtraction.Pull") && option!=1) this.pdfAnnotations.getAnnotations([attID]);
// remove tag from parent item
tagID=Zotero.Tags.getID(this.prefs.getCharPref("tablet.tagParentPush_tag"),0);
if(item.hasTag(tagID)) item.removeTag(tagID);
// add tag to parent item
if (this.prefs.getBoolPref("tablet.tagParentPull")) item.addTag(this.prefs.getCharPref("tablet.tagParentPull_tag"));
// notification
this.infoWindow("ZotFile Report","The attachment \'" + att.getFile().leafName + "\' was removed from the tablet.",8000);
}
 
// remove modified tag from attachment
if (itemPulled) {
var tagIDModified=Zotero.Tags.getID(this.prefs.getCharPref("tablet.tagModified"),0);
if(att.hasTag(tagIDModified)) att.removeTag(tagIDModified);
}
 
//debug
if(this.prefs.getBoolPref("debug")) Zotero.debug("zotfile.getAttachmentFromTablet - end");
// return new id
return(attID);
},
getSelectedAttachmentsFromTablet: function() {
// save current selection
var win = this.wm.getMostRecentWindow("navigator:browser");
var selection=win.ZoteroPane.itemsView.saveSelection();
 
// get selected attachments
var attIDs=this.getSelectedAttachments();
// Pull attachments
var tagID=Zotero.Tags.getID(this.prefs.getCharPref("tablet.tag"),0);
var confirmed=1;
if (this.prefs.getBoolPref("confirmation_batch_ask") && attIDs.length>=this.prefs.getIntPref("confirmation_batch")) confirmed=confirm("Do you want to get the " + attIDs.length + " selected attachments from your tablet?");
if(confirmed) for (var i=0; i < attIDs.length; i++) {
var att = Zotero.Items.get(attIDs[i]);
var item= Zotero.Items.get(att.getSource());
if(att.hasTag(tagID)) {
var attID=this.getAttachmentFromTablet(item,att,false);
if(attID!==null && attIDs[i]!=attID) selection=this.arrayReplace(selection,attIDs[i],attID);
}
}
// restore selection
if(Zotero.version>="3") win.ZoteroPane.itemsView.selectItems(selection);
},
// ============================= //
// FUNCTIONS: RENAME ATTACHMENTS //
// ============================ //
// Rename & Move Existing Attachments
renameAttachment: function(item, att,import_att,dest_dir,subfolder,subfolderFormat,notification) {
var file;
var newAttID=null;
// get link mode and item ID
var linkmode = att.attachmentLinkMode;
var itemID = item.id;
// only proceed if linked or imported attachment
if(att.isImportedAttachment() || linkmode==Zotero.Attachments.LINK_MODE_LINKED_FILE) {
// get object of attached file
file = att.getFile();
 
// create file name using ZotFile rules
var filename = this.getFilename(item, file.leafName);
var location = this.getLocation(item,dest_dir,subfolder,subfolderFormat);
 
if (import_att || item.libraryID) {
// rename file associated with attachment
att.renameAttachmentFile(filename);
 
// change title of attachment item
att.setField('title', filename);
att.save();
// get object of attached file
file = att.getFile();
// output
if (linkmode!=Zotero.Attachments.LINK_MODE_LINKED_FILE && notification) this.infoWindow("Zotfile Report","Imported Attachment renamed to \'" + filename + "\'.",8000);
}
// (a) LINKED ATTACHMENT TO IMPORTED ATTACHMENT
if (linkmode==Zotero.Attachments.LINK_MODE_LINKED_FILE && import_att) {
// Attach file to selected Zotero item
newAttID=Zotero.Attachments.importFromFile(file, itemID,item.libraryID);
// remove file from hard-drive
file.remove(false);
 
// erase old attachment
att.erase();
// output
if(notification) this.infoWindow("Zotfile Report","Imported Attachment \'" + filename + "\'.",8000);
// return id of attachment
return newAttID;
}
// (b) TO LINKED ATTACHMENT (only if library is local and not cloud)
// if (linkmode==Zotero.Attachments.LINK_MODE_IMPORTED_FILE && !import_att) {
if (!import_att && !item.libraryID) {
// move pdf file
var newfile_path=this.moveFile(file,location, filename, att.getDisplayTitle());
if (newfile_path!="NULL") {
// recreate the outfile nslFile Object
file = this.createFile(newfile_path);
// create linked attachment
newAttID=Zotero.Attachments.linkFromFile(file, itemID,item.libraryID);
// erase old attachment
att.erase();
if(notification) this.infoWindow("Zotfile Report","Linked Attachment \'" + file.leafName + "\'.",8000);
// return id of attachment
return newAttID;
}
}
}
// return id of attachment
return newAttID;
},
// FUNCTION: Rename & Move Existing Attachments
renameSelectedAttachments: function(){
// save current selection
var win = this.wm.getMostRecentWindow("navigator:browser");
var selection=win.ZoteroPane.itemsView.saveSelection();
// get selected attachments
var attIDs=this.getSelectedAttachments();
// Pull attachments
var confirmed=1;
if (this.prefs.getBoolPref("confirmation_batch_ask") && attIDs.length>=this.prefs.getIntPref("confirmation_batch")) confirmed=confirm("Do you want to move and rename " + attIDs.length + " attachments?");
if(confirmed) {
for (var i=0; i < attIDs.length; i++) {
// get attachment and item
var att = Zotero.Items.get(attIDs[i]);
var item= Zotero.Items.get(att.getSource());
 
// preserve attachment note and tags
var att_note=att.getNote();
var att_tags=att.getTags();
if(att_tags.length>0) for (var j=0; j < att_tags.length; j++) att_tags[j]= att_tags[j]._get('name');
// Rename and Move Attachment
var file = att.getFile();
if(this.fileExists(att) && this.checkFileType(file) && !this.getTabletStatus(att)) {
// move & rename
var attID=this.renameAttachment(item, att,this.prefs.getBoolPref("import"),this.prefs.getCharPref("dest_dir"),this.prefs.getBoolPref("subfolder"),this.prefs.getCharPref("subfolderFormat"),true);
//update list of selected item
if(attID!==null && attIDs[i]!=attID) selection=this.arrayReplace(selection,attIDs[i],attID);
 
// restore attachments note and tags
if(att_note!="" || att_tags.length>0) {
att = Zotero.Items.get(attID);
if(att_note!="") att.setNote(att_note);
if(att_tags) for each(var tag in att_tags) att.addTag(tag);
att.save();
}
}
if(this.getTabletStatus(att)) this.infoWindow("Zotfile Error","Attachment could not be renamed because it is on the tablet.",8000);
 
}
// restore selection
if(Zotero.version>="3") win.ZoteroPane.itemsView.selectItems(selection);
}
},
// =========================================== //
// FUNCTIONS: PDF ANNOTATION EXTRACTION CLASS //
// ========================================== //
// class to extract pdf annotations
pdfAnnotations : {
popplerExtractorFileName: 'ExtractPDFAnnotations',
popplerExtractorPath:null,
popplerExtractorVersion:1.0,
popplerSupportedPlatforms:['MacIntel'],
pdfExtraction:false,
popplerExtractorTool:false,
popplerExtractorSupported:false,
popplerExtractorBaseURL:'http://www.columbia.edu/~jpl2136/PDFTools/',
 
/** The list of PDFs we should extract annotations from. Each
element is an object with the following fields:
attachment: the Zotero object representing the attachment
path: an absolute path to the attachment file
item: the Zotero item containing the attachment
*/
pdfAttachmentsForExtraction: [],
numTotalPdfAttachments: 0,
errorExtractingAnnotations: false,
/** The hidden browser where PDFs get rendered by pdf.js. */
pdfHiddenBrowser: null,
PDF_EXTRACT_URL: 'chrome://zotfile/content/pdfextract/extract.html',
 
popplerExtractorSetPath: function() {
// extractor filename
this.popplerExtractorFileName += '-' + Zotero.platform;
if (Zotero.isWin) this.popplerExtractorFileName+='.exe';
//“pdftotext-{platform}”, where {platform} is “Win32”, “MacIntel”, “MacPPC”, “Linux-i686”, etc. (To determine your current platform, type javascript:alert(navigator.platform) in the Firefox URL bar and hit Enter.)
 
// extractor path
this.popplerExtractorPath = Zotero.getZoteroDirectory().path + "/ExtractPDFAnnotations/" + this.popplerExtractorFileName;
if (Zotero.isWin) this.popplerExtractorPath.replace(/\\\//g,"\\").replace(/\//g,"\\");
},
 
popplerExtractorCheckInstalled: function () {
// str = toolIsRegistered ? "Installed..." : "Download Tool to Extract PDF Annotations";
try {
var fileobj = Zotero.ZotFile.createFile(this.popplerExtractorPath);
if (fileobj.exists()) return(1);
if (!fileobj.exists()) return(0);
}
catch (err) {
return(0);
}
 
},
 
openFileStream: function (file) {
var istream = Components.classes["@mozilla.org/network/file-input-stream;1"].
createInstance(Components.interfaces.nsIFileInputStream);
istream.init(file, 0x01, 0444, 0);
istream.QueryInterface(Components.interfaces.nsILineInputStream);
 
/* Need to find out what the character encoding is. Using UTF-8 for this example: */
var charset = "UTF-8";
var is = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
.createInstance(Components.interfaces.nsIConverterInputStream);
// This assumes that fis is the template.Interface("nsIInputStream") you want to read from
is.init(istream, charset, 1024, 0xFFFD);
is.QueryInterface(Components.interfaces.nsIUnicharLineInputStream);
 
return(is);
},
 
popplerExtractorCall: function (pdfFilePath,outputFile) {
// set up process
var extractorFile=Zotero.ZotFile.createFile(this.popplerExtractorPath);
var proc = Components.classes["@mozilla.org/process/util;1"].
createInstance(Components.interfaces.nsIProcess);
proc.init(extractorFile);
 
// define arguments
var args = [pdfFilePath,outputFile];
 
// run process
if (!Zotero.isFx36) {
proc.runw(true, args, args.length);
}
else {
proc.run(true, args, args.length);
}
 
},
 
getAnnotations: function(attIDs) {
// Zotero.debug("ZotFile - pdfAnnotations - getAnnotations() - called");
// get selected attachments if no att ids are passed
if(attIDs==null) attIDs=Zotero.ZotFile.getSelectedAttachments();
 
// Zotero.debug("ZotFile - pdfAnnotations - getAnnotations() - " + attIDs.length + " attachments");
// iterate through attachment items
var file;
if(attIDs!=null) for (var i=0; i < attIDs.length; i++) {
// get attachment item, parent and file
var att = Zotero.Items.get(attIDs[i]);
var item = Zotero.Items.get(att.getSource());
// if file is on tablet in background mode, take the one which was modified
if(Zotero.ZotFile.getTabletStatus(att) && Zotero.ZotFile.getInfo(att,"mode")==1) {
var file_zotero=att.getFile();
var file_reader=Zotero.ZotFile.getTabletFile(att);
// get times
var time_reader = file_reader.exists() ? parseInt(file_reader.lastModifiedTime+"",10) : 0;
var time_saved = parseInt(Zotero.ZotFile.getInfo(att,"lastmod"),10);
var time_zotero = (file_zotero!=false) ? parseInt(file_zotero.lastModifiedTime+"",10) : 0;
 
if (time_reader!=0 || time_zotero!=0) {
 
// set options
var option;
if (time_reader>time_saved && time_zotero<=time_saved) option=0;
if (time_reader<=time_saved && time_zotero<=time_saved) option=1;
if (time_reader<=time_saved && time_zotero>time_saved) option=1;
if (time_reader>time_saved && time_zotero>time_saved) option=2;
 
// prompt if both file have been modified
if(option==2) option =Zotero.ZotFile.promptUser("Both copies of the attachment file \'" + file_zotero.leafName + "\' have been modified. From which copy do you want to extract annotations?","Use Copy on Tablet","Use Copy in Zotero","Cancel");
if(option==0) file =file_reader;
if(option==1) file =file_zotero;
if(option==2) return(false);
}
}
else {
file = att.getFile();
}
 
// extract annotations from pdf and create note with annotations
if(Zotero.ZotFile.getFiletype(file.leafName)=="pdf") {
if (Zotero.ZotFile.prefs.getBoolPref("pdfExtraction.UsePDFJS") || Zotero.ZotFile.prefs.getBoolPref("pdfExtraction.UsePDFJSandPoppler")) {
var a = {};
a.attachment = att;
a.path = file.path;
a.item = item;
this.pdfAttachmentsForExtraction.push(a);
}
if (this.popplerExtractorTool && (
!Zotero.ZotFile.prefs.getBoolPref("pdfExtraction.UsePDFJS") ||
Zotero.ZotFile.prefs.getBoolPref("pdfExtraction.UsePDFJSandPoppler"))) {
var outputFile=file.path.replace(".pdf",".txt");
this.popplerExtractorCall(file.path,outputFile);
var annotations = this.popplerExtractorGetAnnotationsFromFile(outputFile);
if(annotations.length!=0) this.createNote(annotations, item, "poppler");
 
// delete output text file
if(Zotero.ZotFile.prefs.getBoolPref("pdfExtraction.popplerDeleteTxtFile")) Zotero.ZotFile.removeFile(Zotero.ZotFile.createFile(outputFile));
}
}
}
if (this.pdfAttachmentsForExtraction.length > 0 &&
(Zotero.ZotFile.prefs.getBoolPref("pdfExtraction.UsePDFJS") || Zotero.ZotFile.prefs.getBoolPref("pdfExtraction.UsePDFJSandPoppler"))) {
if (!Zotero.isFx36) {
// setup extraction process
this.errorExtractingAnnotations = false;
this.numTotalPdfAttachments = this.pdfAttachmentsForExtraction.length;
Zotero.showZoteroPaneProgressMeter("Extract PDF annotations (press ESC to cancel)",true);
var win = Zotero.ZotFile.wm.getMostRecentWindow("navigator:browser");
win.ZoteroPane.document.addEventListener('keypress', this.cancellationListener,false);
this.pdfHiddenBrowser = Zotero.Browser.createHiddenBrowser();
this.pdfHiddenBrowser.loadURI(this.PDF_EXTRACT_URL);
}
else Zotero.ZotFile.infoWindow("ZotFile Error","The extraction of pdf annotations with pdf.js is not supported on Firefox 3.6. Install the most recent Firefox version to use this feature. Mac users can also switch to poppler for the extraction of pdf annotations (in 'ZotFile Preferences' under 'Advanced Settings').",8000);
}
 
// Zotero.debug("ZotFile - pdfAnnotations - getAnnotations() - end - done");
},
 
popplerExtractorGetAnnotationsFromFile: function(outputFile) {
var annotations = [];
var file=Zotero.ZotFile.createFile(outputFile);
if(file.exists()) {
// open an input stream from file
var istream=this.openFileStream(file);
 
// read lines into array
var line = {};
do {
// get line
cont = istream.readLine(line);
var line_split = line.value.split(' ; ');
var strMarkUp = (line_split[5]) ? this.trim(line_split[5].replace(/\\n/g,"<br>")) : "";
var strText = (line_split[4]) ? this.trim(line_split[4].replace(/\\n/g,"<br>")) : "";
 
// if(strText!="") var strText=this.removeHyphens(strText);
 
// create annotation object
/* structure: filename ; page ; ID ; type ; text ; textMarkUp */
if(strMarkUp!="" || strText!="") {
a = {
filename:line_split[0],
page:parseInt(line_split[1],10),
ID:parseInt(line_split[2],10),
type:line_split[3],
// date:line_split[7],
// creator:line_split[11],
content:strText,
markup:strMarkUp
};
annotations.push(a);
}
 
} while (cont);
 
istream.close();
}
else Zotero.ZotFile.infoWindow("ZotFile Error","Annotation extraction failed.",8000);
return annotations;
},
 
createNote: function(annotations, item, method) {
var note_content=this.getNoteContent(annotations, item, method);
var note = new Zotero.Item("note");
note.libraryID = item._libraryID;
// note.setNote(Zotero.Utilities.text2html(note_content));
note.setNote(note_content);
note.setSource(item.getID());
var noteID = note.save();
 
// Zotero.ZotFile.infoWindow("ZotFile Report","TAB:" + prefWindow.document.getElementById('zotfile-tabbox').selectedTab,8000);
},
 
getNoteContent: function(annotations, item, method) {
// get current date
var date_str=new Date().toUTCString();
 
// set note title
var note="<b>Extracted Annotations (" + date_str;
if (Zotero.ZotFile.prefs.getBoolPref("pdfExtraction.UsePDFJSandPoppler")) note += ", " + method;
note += ")</b><br><br>";
 
// get html tags for notes and highlights
var htmlTagNoteStart=Zotero.ZotFile.prefs.getCharPref("pdfExtraction.NoteHtmlTagStart");
var htmlTagNoteEnd=Zotero.ZotFile.prefs.getCharPref("pdfExtraction.NoteHtmlTagEnd");
var htmlTagHighlightStart=Zotero.ZotFile.prefs.getCharPref("pdfExtraction.HighlightHtmlTagStart");
var htmlTagHighlightEnd=Zotero.ZotFile.prefs.getCharPref("pdfExtraction.HighlightHtmlTagEnd");
var htmlTagUnderlineStart=Zotero.ZotFile.prefs.getCharPref("pdfExtraction.UnderlineHtmlTagStart");
var htmlTagUnderlineEnd=Zotero.ZotFile.prefs.getCharPref("pdfExtraction.UnderlineHtmlTagEnd");
 
// iterature through annotations
for (var i=0; i < annotations.length; i++) {
var anno=annotations[i];
 
// get page
var page=anno.page;
if(Zotero.ZotFile.prefs.getBoolPref("pdfExtraction.NoteTruePage")) {
try {
var itemPages=item.getField('pages');
if(itemPages) page=parseInt(itemPages.split('-')[0],10)+page-1;
}
catch(err) {}
}
 
// get citation
var cite="p. ";
if(Zotero.ZotFile.prefs.getBoolPref("pdfExtraction.NoteFullCite")) cite=Zotero.ZotFile.replaceWildcard(item, "%a %y:").replace(/_(?!.*_)/," and ").replace(/_/g,", ");
 
// add to note text pdfExtractionNoteRemoveHtmlNote
if(anno.content && anno.content != "" &&
(!anno.markup || this.strDistance(anno.content,anno.markup)>0.15 )) {
var content = anno.content.replace(/(\r\n|\n|\r)/gm,"<br/>");
note += "<p>"+htmlTagNoteStart+content+" (note on p." + page + ")"+htmlTagNoteEnd+"</p><br>";
}
 
if(anno.markup && anno.markup != "") {
var markup = this.trim(anno.markup);
if(Zotero.ZotFile.prefs.getBoolPref("pdfExtraction.NoteRemoveHyphens")) markup = this.removeHyphens(markup);
var tagStart = htmlTagHighlightStart;
var tagEnd = htmlTagHighlightEnd;
if (anno.type == "Highlight") {
tagStart = htmlTagHighlightStart;
tagEnd = htmlTagHighlightEnd;
} else if (anno.type == "Underline") {
tagStart = htmlTagUnderlineStart;
tagEnd = htmlTagUnderlineEnd;
}
note += "<p>"+tagStart+"\""+markup+"\" (" + cite + page + ")" +tagEnd+"</p>";
}
}
return note;
 
},
 
trim: function(str) {
//return str.replace (/^\s+/, '').replace (/\s+$/, '');
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
},
 
strDistance: function (s1,s2) {
s1=Zotero.Utilities.trimInternal(s1).replace(/ /g,"");
s2=Zotero.Utilities.trimInternal(s2).replace(/ /g,"");
var l = (s1.length > s2.length) ? s1.length : s2.length;
return Zotero.Utilities.levenshtein(s1,s2)/l;
},
 
removeHyphens: function(str) {
while (true) {
var pos = str.search(/[a-zA-Z]- [a-zA-Z]/g);
if (pos == -1) break;
str = str.substring(0,pos+1) + str.substring(pos+3,str.length);
}
return str;
},
 
/* Runs the annotation extraction code in extract.html/extract.js,
* to extract annotations from a single PDF. */
extractAnnotationsFromFiles: function() {
var attachment = this.pdfAttachmentsForExtraction.shift();
var args = {};
args.url = 'file://'+attachment.path;
args.item = attachment.item;
args.callbackObj = this;
args.callback = this.extractionComplete;
Zotero.ZotFile.PdfExtractor.extractAnnotations(args);
},
 
/** Called from extract.js whenever a page is processed. */
pageExtractionComplete: function(pagesProcessed, totalPages) {
// update progress bar
var fractionDone = (this.numTotalPdfAttachments - this.pdfAttachmentsForExtraction.length - 1) /
this.numTotalPdfAttachments;
fractionDone += ((pagesProcessed / totalPages) * (1.0 / this.numTotalPdfAttachments));
Zotero.updateZoteroPaneProgressMeter(fractionDone * 100.0);
},
 
/** Keypress listener that cancels the extraction if the user presses escape. */
cancellationListener: function(keyEvent) {
if (keyEvent.keyCode == KeyboardEvent.DOM_VK_ESCAPE) {
var zzpa = Zotero.ZotFile.pdfAnnotations;
zzpa.pdfAttachmentsForExtraction = [];
zzpa.extractionComplete([], null);
keyEvent.currentTarget.removeEventListener('keypress', zzpa.cancellationListener);
}
},
 
/* Called from extract.js when all annotations for a single PDF have
* been extracted.
* @param annotations An array of annotation objects. Each element
* contains the following fields: url (a url pointing to the file
* this annotation came from), page (the page number within the
* document where this annotation appears), type (the type of
* annotation, e.g. "Highlight", or "Text"), content (the text of
* any pop-up note in this annotation), and markup (the words from
* the document, if any, that were highlighted/underlined).
* @param item The Zotero item these annotations came from */
extractionComplete: function(annotations, item) {
// put annotations into a Zotero note
if (annotations.length > 0) this.createNote(annotations, item, "pdf.js");
// move on to the next pdf, if there is one
if (this.pdfAttachmentsForExtraction.length > 0) {
this.extractAnnotationsFromFiles();
} else { // we're done
if (this.errorExtractingAnnotations) {
Zotero.ZotFile.infoWindow("ZotFile Report","ZotFile was unable to extract all annotations because pdf.js does not support certain PDF standards yet. Please see the JavaScript error console for more details.",8000);
}
this.errorExtractingAnnotations = false;
Zotero.Browser.deleteHiddenBrowser(this.pdfHiddenBrowser);
this.pdfHiddenBrowser = null;
this.numTotalPdfAttachments = 0;
Zotero.hideZoteroPaneOverlay(); // hide progress bar
}
}
 
}
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.