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
|
// Directory list. Taken from the FOX library and slightly modified.
#include "config.h"
#include "i18n.h"
#include <fx.h>
#include <FXPNGIcon.h>
#if defined(linux)
#include <mntent.h>
#endif
#include "xfedefs.h"
#include "icons.h"
#include "xfeutils.h"
#include "File.h"
#include "FileDict.h"
#include "InputDialog.h"
#include "MessageBox.h"
#include "XFileExplorer.h"
#include "DirList.h"
// Interval between updevices and mtdevices read (s)
#define UPDEVICES_INTERVAL 300
#define MTDEVICES_INTERVAL 5
// Interval between refreshes (ms)
#define REFRESH_INTERVAL 1000
// File systems not supporting mod-time, refresh every nth time
#define REFRESH_FREQUENCY 30
// Time interval before expanding a folder (ms)
#define EXPAND_INTERVAL 500
// Global variables
#if defined(linux)
extern FXStringDict* fsdevices;
extern FXStringDict* mtdevices;
extern FXStringDict* updevices;
#endif
extern FXbool allowPopupScroll;
extern FXString xdgdatahome;
// Object implementation
FXIMPLEMENT(DirItem,FXTreeItem,NULL,0)
// Map
FXDEFMAP(DirList) DirListMap[]=
{
FXMAPFUNC(SEL_DRAGGED,0,DirList::onDragged),
FXMAPFUNC(SEL_TIMEOUT,DirList::ID_REFRESH_TIMER,DirList::onCmdRefreshTimer),
#if defined(linux)
FXMAPFUNC(SEL_TIMEOUT,DirList::ID_MTDEVICES_REFRESH,DirList::onMtdevicesRefresh),
FXMAPFUNC(SEL_TIMEOUT,DirList::ID_UPDEVICES_REFRESH,DirList::onUpdevicesRefresh),
#endif
FXMAPFUNC(SEL_TIMEOUT,DirList::ID_EXPAND_TIMER,DirList::onExpandTimer),
FXMAPFUNC(SEL_DND_ENTER,0,DirList::onDNDEnter),
FXMAPFUNC(SEL_DND_LEAVE,0,DirList::onDNDLeave),
FXMAPFUNC(SEL_DND_DROP,0,DirList::onDNDDrop),
FXMAPFUNC(SEL_DND_MOTION,0,DirList::onDNDMotion),
FXMAPFUNC(SEL_DND_REQUEST,0,DirList::onDNDRequest),
FXMAPFUNC(SEL_BEGINDRAG,0,DirList::onBeginDrag),
FXMAPFUNC(SEL_ENDDRAG,0,DirList::onEndDrag),
FXMAPFUNC(SEL_OPENED,0,DirList::onOpened),
FXMAPFUNC(SEL_CLOSED,0,DirList::onClosed),
FXMAPFUNC(SEL_EXPANDED,0,DirList::onExpanded),
FXMAPFUNC(SEL_COLLAPSED,0,DirList::onCollapsed),
FXMAPFUNC(SEL_UPDATE,DirList::ID_SHOW_HIDDEN,DirList::onUpdShowHidden),
FXMAPFUNC(SEL_UPDATE,DirList::ID_HIDE_HIDDEN,DirList::onUpdHideHidden),
FXMAPFUNC(SEL_UPDATE,DirList::ID_TOGGLE_HIDDEN,DirList::onUpdToggleHidden),
FXMAPFUNC(SEL_UPDATE,DirList::ID_SHOW_FILES,DirList::onUpdShowFiles),
FXMAPFUNC(SEL_UPDATE,DirList::ID_HIDE_FILES,DirList::onUpdHideFiles),
FXMAPFUNC(SEL_UPDATE,DirList::ID_TOGGLE_FILES,DirList::onUpdToggleFiles),
FXMAPFUNC(SEL_UPDATE,DirList::ID_SET_PATTERN,DirList::onUpdSetPattern),
FXMAPFUNC(SEL_UPDATE,DirList::ID_SORT_REVERSE,DirList::onUpdSortReverse),
FXMAPFUNC(SEL_COMMAND,DirList::ID_SHOW_HIDDEN,DirList::onCmdShowHidden),
FXMAPFUNC(SEL_COMMAND,DirList::ID_DRAG_COPY,DirList::onCmdDragCopy),
FXMAPFUNC(SEL_COMMAND,DirList::ID_DRAG_MOVE,DirList::onCmdDragMove),
FXMAPFUNC(SEL_COMMAND,DirList::ID_DRAG_LINK,DirList::onCmdDragLink),
FXMAPFUNC(SEL_COMMAND,DirList::ID_DRAG_REJECT,DirList::onCmdDragReject),
FXMAPFUNC(SEL_COMMAND,DirList::ID_HIDE_HIDDEN,DirList::onCmdHideHidden),
FXMAPFUNC(SEL_COMMAND,DirList::ID_TOGGLE_HIDDEN,DirList::onCmdToggleHidden),
FXMAPFUNC(SEL_COMMAND,DirList::ID_SHOW_FILES,DirList::onCmdShowFiles),
FXMAPFUNC(SEL_COMMAND,DirList::ID_HIDE_FILES,DirList::onCmdHideFiles),
FXMAPFUNC(SEL_COMMAND,DirList::ID_TOGGLE_FILES,DirList::onCmdToggleFiles),
FXMAPFUNC(SEL_COMMAND,DirList::ID_SET_PATTERN,DirList::onCmdSetPattern),
FXMAPFUNC(SEL_COMMAND,DirList::ID_SORT_REVERSE,DirList::onCmdSortReverse),
FXMAPFUNC(SEL_COMMAND,DirList::ID_REFRESH,DirList::onCmdRefresh),
FXMAPFUNC(SEL_COMMAND,DirList::ID_SORT_CASE,DirList::onCmdSortCase),
FXMAPFUNC(SEL_UPDATE,DirList::ID_SORT_CASE,DirList::onUpdSortCase)
};
// Object implementation
FXIMPLEMENT(DirList,FXTreeList,DirListMap,ARRAYNUMBER(DirListMap))
// Directory List Widget
DirList::DirList(FXWindow *focuswin,FXComposite *p,FXObject* tgt,FXSelector sel,FXuint opts,FXint x,FXint y,FXint w,FXint h):
FXTreeList(p,tgt,sel,opts,x,y,w,h),pattern("*")
{
flags|=FLAG_ENABLED|FLAG_DROPTARGET;
matchmode=FILEMATCH_FILE_NAME|FILEMATCH_NOESCAPE;
associations=NULL;
if(!(options&DIRLIST_NO_OWN_ASSOC))
associations=new FileDict(getApp());
list=NULL;
sortfunc=(FXTreeListSortFunc)ascendingCase;
dropaction=DRAG_MOVE;
counter=0;
prevSelItem=NULL;
focuswindow=focuswin;
#if defined(linux)
// Initialize the fsdevices, mtdevices and updevices lists
struct mntent *mnt;
if (fsdevices==NULL)
{
// To list file system devices
fsdevices=new FXStringDict();
FILE *fstab=setmntent(FSTAB_PATH,"r");
if(fstab)
{
while((mnt=getmntent(fstab)))
{
if(!streq(mnt->mnt_type,MNTTYPE_IGNORE) && !streq(mnt->mnt_type,MNTTYPE_SWAP)
&& !streq(mnt->mnt_dir,"/"))
{
if(!strncmp(mnt->mnt_fsname,"/dev/fd",7))
fsdevices->insert(mnt->mnt_dir,"floppy");
else if (!strncmp(mnt->mnt_type,"iso",3))
fsdevices->insert(mnt->mnt_dir,"cdrom");
else if (!strncmp(mnt->mnt_fsname,"/dev/zip",8))
fsdevices->insert(mnt->mnt_dir,"zip");
else if (streq(mnt->mnt_type,"nfs"))
fsdevices->insert(mnt->mnt_dir,"nfsdisk");
else if (streq(mnt->mnt_type,"smbfs"))
fsdevices->insert(mnt->mnt_dir,"smbdisk");
else
fsdevices->insert(mnt->mnt_dir,"harddisk");
}
}
endmntent(fstab);
}
}
if (mtdevices==NULL)
{
// To list mounted devices
mtdevices=new FXStringDict();
FILE *mtab=setmntent(MTAB_PATH,"r");
if(mtab)
{
while((mnt=getmntent(mtab)))
{
// To fix an issue with some Linux distributions
FXString mntdir=mnt->mnt_dir;
if (mntdir!="/dev/.static/dev" && mntdir.rfind(".gvfs",5,mntdir.length())==-1)
mtdevices->insert(mnt->mnt_dir,mnt->mnt_type);
}
endmntent(mtab);
}
}
if (updevices==NULL)
{
// To mark mount points that are up or down
updevices=new FXStringDict();
struct stat statbuf;
FXString mtstate;
FILE *mtab=setmntent(MTAB_PATH,"r");
if(mtab)
{
while((mnt=getmntent(mtab)))
{
// To fix an issue with some Linux distributions
FXString mntdir=mnt->mnt_dir;
if (mntdir!="/dev/.static/dev" && mntdir.rfind(".gvfs",5,mntdir.length())==-1)
{
if (lstatmt(mnt->mnt_dir,&statbuf)==-1)
mtstate="down";
else
mtstate="up";
updevices->insert(mnt->mnt_dir,mtstate.text());
}
}
endmntent(mtab);
}
}
#endif
// Trahscan location
trashfileslocation=xdgdatahome + PATHSEPSTRING TRASHFILESPATH;
trashinfolocation=xdgdatahome + PATHSEPSTRING TRASHINFOPATH;
}
// Create the directory list
void DirList::create()
{
FXTreeList::create();
if(!deleteType)
deleteType=getApp()->registerDragType(deleteTypeName);
if(!urilistType)
urilistType=getApp()->registerDragType(urilistTypeName);
getApp()->addTimeout(this,ID_REFRESH_TIMER,REFRESH_INTERVAL);
#if defined(linux)
getApp()->addTimeout(this,ID_MTDEVICES_REFRESH,MTDEVICES_INTERVAL*1000);
getApp()->addTimeout(this,ID_UPDEVICES_REFRESH,UPDEVICES_INTERVAL*1000);
#endif
dropEnable();
// Scan root directory
scan(FALSE);
}
// Expand folder tree when hovering long over a folder
long DirList::onExpandTimer(FXObject* sender,FXSelector sel,void* ptr)
{
FXint xx,yy;
FXuint state;
DirItem *item;
getCursorPosition(xx,yy,state);
item=(DirItem*)getItemAt(xx,yy);
if(!(item->state&DirItem::FOLDER))
return 0;
// Expand tree item
expandTree((TreeItem*)item,TRUE);
scan(TRUE);
// Set open timer
getApp()->addTimeout(this,ID_EXPAND_TIMER,EXPAND_INTERVAL);
return 1;
}
// Create item
TreeItem* DirList::createItem(const FXString& text,FXIcon* oi,FXIcon* ci,void* ptr)
{
return (TreeItem*) new DirItem(text,oi,ci,ptr);
}
// Sort ascending order, keeping directories first
FXint DirList::ascending(const FXTreeItem* pa,const FXTreeItem* pb)
{
register const DirItem *a=(DirItem*)pa;
register const DirItem *b=(DirItem*)pb;
register FXint diff=(FXint)b->isDirectory() - (FXint)a->isDirectory();
return diff ? diff : compare(a->label,b->label);
}
// Sort descending order, keeping directories first
FXint DirList::descending(const FXTreeItem* pa,const FXTreeItem* pb)
{
register const DirItem *a=(DirItem*)pa;
register const DirItem *b=(DirItem*)pb;
register FXint diff=(FXint)b->isDirectory() - (FXint)a->isDirectory();
return diff ? diff : compare(b->label,a->label);
}
// Sort ascending order, case insensitive, keeping directories first
FXint DirList::ascendingCase(const FXTreeItem* pa,const FXTreeItem* pb)
{
register const DirItem *a=(DirItem*)pa;
register const DirItem *b=(DirItem*)pb;
register FXint diff=(FXint)b->isDirectory() - (FXint)a->isDirectory();
return diff ? diff : comparecase(a->label,b->label);
}
// Sort descending order, case insensitive, keeping directories first
FXint DirList::descendingCase(const FXTreeItem* pa,const FXTreeItem* pb)
{
register const DirItem *a=(DirItem*)pa;
register const DirItem *b=(DirItem*)pb;
register FXint diff=(FXint)b->isDirectory() - (FXint)a->isDirectory();
return diff ? diff : comparecase(b->label,a->label);
}
// Handle drag-and-drop enter
long DirList::onDNDEnter(FXObject* sender,FXSelector sel,void* ptr)
{
FXTreeList::onDNDEnter(sender,sel,ptr);
return 1;
}
// Handle drag-and-drop leave
long DirList::onDNDLeave(FXObject* sender,FXSelector sel,void* ptr)
{
// Cancel open up timer
getApp()->removeTimeout(this,ID_EXPAND_TIMER);
stopAutoScroll();
FXTreeList::onDNDLeave(sender,sel,ptr);
if(prevSelItem)
{
if(!isItemCurrent(prevSelItem))
closeItem(prevSelItem);
prevSelItem = NULL;
}
return 1;
}
// Handle drag-and-drop motion
long DirList::onDNDMotion(FXObject* sender,FXSelector sel,void* ptr)
{
FXEvent *event=(FXEvent*)ptr;
TreeItem *item;
// Cancel open up timer
getApp()->removeTimeout(this,ID_EXPAND_TIMER);
// Start autoscrolling
if(startAutoScroll(event,FALSE))
return 1;
// Give base class a shot
if(FXTreeList::onDNDMotion(sender,sel,ptr))
return 1;
// Dropping list of filenames
if(offeredDNDType(FROM_DRAGNDROP,urilistType))
{
// Locate drop place
item=(TreeItem*)getItemAt(event->win_x,event->win_y);
// We can drop in a directory
if(item && isItemDirectory(item))
{
// Get drop directory
dropdirectory=getItemPathname(item);
// What is being done (move,copy,link)
dropaction=inquireDNDAction();
// Set open up timer
getApp()->addTimeout(this,ID_EXPAND_TIMER,EXPAND_INTERVAL);
// Set icon to open folder icon
setItemOpenIcon(item,minifolderopenicon);
// See if this is writable
if(::isWritable(dropdirectory))
{
acceptDrop(DRAG_ACCEPT);
FXint x,y;
FXuint state;
getCursorPosition(x,y,state);
TreeItem* item=(TreeItem*)getItemAt(x,y);
if(prevSelItem && prevSelItem != item)
{
if(!isItemCurrent(prevSelItem))
closeItem(prevSelItem);
prevSelItem = NULL;
}
if(item && prevSelItem != item)
{
openItem(item);
prevSelItem = item;
}
}
}
return 1;
}
return 0;
}
// Set drag type to copy
long DirList::onCmdDragCopy(FXObject* sender,FXSelector sel,void* ptr)
{
dropaction=DRAG_COPY;
return 1;
}
// Set drag type to move
long DirList::onCmdDragMove(FXObject* sender,FXSelector sel,void* ptr)
{
dropaction=DRAG_MOVE;
return 1;
}
// Set drag type to symlink
long DirList::onCmdDragLink(FXObject* sender,FXSelector sel,void* ptr)
{
dropaction=DRAG_LINK;
return 1;
}
// Cancel drag action
long DirList::onCmdDragReject(FXObject* sender,FXSelector sel,void* ptr)
{
dropaction=DRAG_REJECT;
return 1;
}
// Handle drag-and-drop drop
long DirList::onDNDDrop(FXObject* sender,FXSelector sel,void* ptr)
{
FXuchar *data;
FXuint len;
FXbool showdialog=TRUE;
FXint ret;
File *f=NULL;
FXbool ask_before_copy=getApp()->reg().readUnsignedEntry("OPTIONS","ask_before_copy",TRUE);
FXbool confirm_dnd=getApp()->reg().readUnsignedEntry("OPTIONS","confirm_drag_and_drop",TRUE);
// Cancel open up timer
getApp()->removeTimeout(this,ID_EXPAND_TIMER);
// Stop scrolling
stopAutoScroll();
// Perhaps target wants to deal with it
if(FXTreeList::onDNDDrop(sender,sel,ptr))
return 1;
// Check if control key or shift key were pressed
FXbool ctrlshiftkey=FALSE;
if (ptr!=NULL)
{
FXEvent* event=(FXEvent*)ptr;
if (event->state&CONTROLMASK)
ctrlshiftkey=TRUE;
if (event->state&SHIFTMASK)
ctrlshiftkey=TRUE;
}
// Get DND data
// This is done before displaying the popup menu to fix a drag and drop problem with konqueror and dolphin file managers
FXbool dnd=getDNDData(FROM_DRAGNDROP,urilistType,data,len);
// Display the dnd dialog if the control or shift key were not pressed
if (confirm_dnd & !ctrlshiftkey)
{
// Display a popup to select the drag type
dropaction=DRAG_REJECT;
FXMenuPane menu(this);
FXint x,y;
FXuint state;
getRoot()->getCursorPosition(x,y,state);
new FXMenuCommand(&menu,_("Copy here"),copy_clpicon,this,DirList::ID_DRAG_COPY);
new FXMenuCommand(&menu,_("Move here"),moveiticon,this,DirList::ID_DRAG_MOVE);
new FXMenuCommand(&menu,_("Link here"),minilinkicon,this,DirList::ID_DRAG_LINK);
new FXMenuSeparator(&menu);
new FXMenuCommand(&menu,_("Cancel"),NULL,this,DirList::ID_DRAG_REJECT);
menu.create();
allowPopupScroll=TRUE; // Allow keyboard scrolling
menu.popup(NULL,x,y);
getApp()->runModalWhileShown(&menu);
allowPopupScroll=FALSE;
}
// Close item
if(prevSelItem)
{
if(!isItemCurrent(prevSelItem))
closeItem(prevSelItem);
prevSelItem = NULL;
}
// Get uri-list of files being dropped
//if(getDNDData(FROM_DRAGNDROP,urilistType,data,len))
if(dnd) // See comment upper
{
FXRESIZE(&data,FXuchar,len+1);
data[len]='\0';
FXchar *p,*q;
p=q=(FXchar*)data;
// Number of selected items
FXString buf=p;
int num=buf.contains('\n')+1;
// Eventually correct the number of selected items
// because sometimes there is another '\n' at the end of the string
FXint pos=buf.rfind('\n');
if (pos==buf.length()-1)
num=num-1;
// File object
if (dropaction==DRAG_COPY)
f=new File(this,_("File copy"),COPY);
else if (dropaction==DRAG_MOVE)
f=new File(this,_("File move"),MOVE);
else if (dropaction==DRAG_LINK)
f=new File(this,_("File symlink"),SYMLINK);
else
{
FXFREE(&data);
return 0;
}
// Target directory
FXString targetdir=dropdirectory;
while(*p)
{
while(*q && *q!='\r')
q++;
FXString url(p,q-p);
FXString source(FXURL::fileFromURL(url));
FXString target(targetdir);
FXString sourcedir=FXPath::directory(source);
// File operation dialog, if needed
if ((!confirm_dnd | ctrlshiftkey) & ask_before_copy & showdialog)
{
FXIcon *icon=NULL;
FXString title,message;
if (dropaction==DRAG_COPY)
{
title=_("Copy ");
icon = copy_bigicon;
if (num==1)
message=title+source;
else
title.format(_("Copy %s files/folders.\nFrom: %s"),FXStringVal(num).text(),sourcedir.text());
}
else if (dropaction==DRAG_MOVE)
{
title=_("Move ");
icon = move_bigicon;
if (num==1)
message=title+source;
else
title.format(_("Move %s files/folders.\nFrom: %s"),FXStringVal(num).text(),sourcedir.text());
}
else if ((dropaction==DRAG_LINK) && (num==1))
{
title=_("Symlink ");
icon=link_bigicon;
message=title+source;
}
InputDialog* dialog = new InputDialog(this,targetdir,message,title,_("To:"),icon);
dialog->CursorEnd();
int rc=1;
rc=dialog->execute();
target=dialog->getText();
target=::filePath(target);
if (num>1)
showdialog=FALSE;
delete dialog;
if (!rc)
return 0;
}
// Move the source file
if(dropaction==DRAG_MOVE)
{
// Move file
f->create();
// If target file is located at trash location, also create the corresponding trashinfo file
// Do it silently and don't report any error if it fails
FXbool use_trash_can=getApp()->reg().readUnsignedEntry("OPTIONS","use_trash_can",TRUE);
if (use_trash_can && FXPath::directory(target)==trashfileslocation )
{
// Trash files path name
FXString trashpathname=createTrashpathname(source,trashfileslocation);
// Adjust target name to get the _N suffix if any
FXString trashtarget=FXPath::directory(target)+PATHSEPSTRING+FXPath::name(trashpathname);
// Create trashinfo file
createTrashinfo(source,trashpathname,trashfileslocation,trashinfolocation);
// Move source to trash target
ret=f->move(source,trashtarget);
}
// Move source to target
else
{
//target=FXPath::directory(target);
ret=f->move(source,target);
}
// If source file is located at trash location, try to also remove the corresponding trashinfo if it exists
// Do it silently and don't report any error if it fails
if (use_trash_can && ret && (source.left(trashfileslocation.length())==trashfileslocation) )
{
FXString trashinfopathname=trashinfolocation+PATHSEPSTRING+FXPath::name(source)+".trashinfo";
::unlink(trashinfopathname.text());
}
// An error has occurred
if (ret==0 && !f->isCancelled())
{
f->hideProgressDialog();
MessageBox::error(this,BOX_OK,_("Error"),_("An error has occurred during the move file operation!"));
break;
}
// If action is cancelled in progress dialog
if (f->isCancelled())
{
f->hideProgressDialog();
MessageBox::error(this,BOX_OK,_("Error"),_("Move file operation cancelled!"));
break;
}
// Set directory to the source parent
setDirectory(sourcedir,FALSE);
}
// Copy the source file
else if(dropaction==DRAG_COPY)
{
// Copy file
f->create();
// If target file is located at trash location, also create the corresponding trashinfo file
// Do it silently and don't report any error if it fails
FXbool use_trash_can=getApp()->reg().readUnsignedEntry("OPTIONS","use_trash_can",TRUE);
if (use_trash_can && FXPath::directory(target)==trashfileslocation )
{
// Trash files path name
FXString trashpathname=createTrashpathname(source,trashfileslocation);
// Adjust target name to get the _N suffix if any
FXString trashtarget=FXPath::directory(target)+PATHSEPSTRING+FXPath::name(trashpathname);
// Create trashinfo file
createTrashinfo(source,trashpathname,trashfileslocation,trashinfolocation);
// Copy source to trash target
ret=f->copy(source,trashtarget);
}
// Copy source to target
else
{
//target=FXPath::directory(target);
ret=f->copy(source,target);
}
// An error has occurred
if (ret==0 && !f->isCancelled())
{
f->hideProgressDialog();
MessageBox::error(this,BOX_OK,_("Error"),_("An error has occurred during the copy file operation!"));
break;
}
// If action is cancelled in progress dialog
if (f->isCancelled())
{
f->hideProgressDialog();
MessageBox::error(this,BOX_OK,_("Error"),_("Copy file operation cancelled!"));
break;
}
}
// Link the source file (no progress dialog in this case)
else if(dropaction==DRAG_LINK)
{
// Link file
f->create();
f->symlink(source,target);
}
if(*q=='\r')
q+=2;
p=q;
}
delete f;
FXFREE(&data);
// Force a refresh of the DirList
onCmdRefresh(0,0,0);
return 1;
}
return 0;
}
// Somebody wants our dragged data
long DirList::onDNDRequest(FXObject* sender,FXSelector sel,void* ptr)
{
FXEvent *event=(FXEvent*)ptr;
FXuchar *data;
FXuint len;
// Perhaps the target wants to supply its own data
if(FXTreeList::onDNDRequest(sender,sel,ptr))
return 1;
// Return list of filenames as a uri-list
if(event->target==urilistType)
{
if(!dragfiles.empty())
{
len=dragfiles.length();
FXMEMDUP(&data,dragfiles.text(),FXuchar,len);
setDNDData(FROM_DRAGNDROP,event->target,data,len);
}
return 1;
}
// Delete selected files
if(event->target==deleteType)
return 1;
return 0;
}
// Start a drag operation
long DirList::onBeginDrag(FXObject* sender,FXSelector sel,void* ptr)
{
register TreeItem *item;
if(FXTreeList::onBeginDrag(sender,sel,ptr))
return 1;
if(beginDrag(&urilistType,1))
{
dragfiles=FXString::null;
item=(TreeItem*)firstitem;
while(item)
{
if(item->isSelected())
{
if(!dragfiles.empty())
dragfiles+="\r\n";
dragfiles+=::fileToURI(getItemPathname(item));
}
if(item->first)
item=(TreeItem*)item->first;
else
{
while(!item->next && item->parent)
item=(TreeItem*)item->parent;
item=(TreeItem*)item->next;
}
}
return 1;
}
return 0;
}
// End drag operation
long DirList::onEndDrag(FXObject* sender,FXSelector sel,void* ptr)
{
if(FXTreeList::onEndDrag(sender,sel,ptr))
return 1;
endDrag((didAccept()!=DRAG_REJECT));
setDragCursor(getDefaultCursor());
return 1;
}
// Dragged stuff around
long DirList::onDragged(FXObject* sender,FXSelector sel,void* ptr)
{
FXEvent* event=(FXEvent*)ptr;
FXDragAction action;
if(FXTreeList::onDragged(sender,sel,ptr))
return 1;
action=DRAG_MOVE;
if(event->state&CONTROLMASK)
action=DRAG_COPY;
if(event->state&SHIFTMASK)
action=DRAG_MOVE;
if((event->state&CONTROLMASK) && (event->state&SHIFTMASK))
action=DRAG_LINK;
handleDrag(event->root_x,event->root_y,action);
if(didAccept()!=DRAG_REJECT)
{
if(action==DRAG_MOVE)
setDragCursor(getApp()->getDefaultCursor(DEF_DNDMOVE_CURSOR));
else if(action==DRAG_LINK)
setDragCursor(getApp()->getDefaultCursor(DEF_DNDLINK_CURSOR));
else
setDragCursor(getApp()->getDefaultCursor(DEF_DNDCOPY_CURSOR));
}
else
setDragCursor(getApp()->getDefaultCursor(DEF_DNDSTOP_CURSOR));
return 1;
}
// Toggle hidden files
long DirList::onCmdToggleHidden(FXObject*,FXSelector,void*)
{
showHiddenFiles(!shownHiddenFiles());
return 1;
}
// Update toggle hidden files widget
long DirList::onUpdToggleHidden(FXObject* sender,FXSelector,void*)
{
if(shownHiddenFiles())
sender->handle(this,FXSEL(SEL_COMMAND,ID_CHECK),NULL);
else
sender->handle(this,FXSEL(SEL_COMMAND,ID_UNCHECK),NULL);
return 1;
}
// Show hidden files
long DirList::onCmdShowHidden(FXObject*,FXSelector,void*)
{
showHiddenFiles(TRUE);
return 1;
}
// Update show hidden files widget
long DirList::onUpdShowHidden(FXObject* sender,FXSelector,void*)
{
if(shownHiddenFiles())
sender->handle(this,FXSEL(SEL_COMMAND,ID_CHECK),NULL);
else
sender->handle(this,FXSEL(SEL_COMMAND,ID_UNCHECK),NULL);
return 1;
}
// Hide hidden files
long DirList::onCmdHideHidden(FXObject*,FXSelector,void*)
{
showHiddenFiles(FALSE);
return 1;
}
// Update hide hidden files widget
long DirList::onUpdHideHidden(FXObject* sender,FXSelector,void*)
{
if(!shownHiddenFiles())
sender->handle(this,FXSEL(SEL_COMMAND,ID_CHECK),NULL);
else
sender->handle(this,FXSEL(SEL_COMMAND,ID_UNCHECK),NULL);
return 1;
}
// Toggle files display
long DirList::onCmdToggleFiles(FXObject*,FXSelector,void*)
{
showFiles(!showFiles());
return 1;
}
// Update toggle files widget
long DirList::onUpdToggleFiles(FXObject* sender,FXSelector,void*)
{
if(showFiles())
sender->handle(this,FXSEL(SEL_COMMAND,ID_CHECK),NULL);
else
sender->handle(this,FXSEL(SEL_COMMAND,ID_UNCHECK),NULL);
return 1;
}
// Show files
long DirList::onCmdShowFiles(FXObject*,FXSelector,void*)
{
showFiles(TRUE);
return 1;
}
// Update show files widget
long DirList::onUpdShowFiles(FXObject* sender,FXSelector,void*)
{
if(showFiles())
sender->handle(this,FXSEL(SEL_COMMAND,ID_CHECK),NULL);
else
sender->handle(this,FXSEL(SEL_COMMAND,ID_UNCHECK),NULL);
return 1;
}
// Hide files
long DirList::onCmdHideFiles(FXObject*,FXSelector,void*)
{
showFiles(FALSE);
return 1;
}
// Update hide files widget
long DirList::onUpdHideFiles(FXObject* sender,FXSelector,void*)
{
if(!showFiles())
sender->handle(this,FXSEL(SEL_COMMAND,ID_CHECK),NULL);
else
sender->handle(this,FXSEL(SEL_COMMAND,ID_UNCHECK),NULL);
return 1;
}
// Change pattern
long DirList::onCmdSetPattern(FXObject*,FXSelector,void* ptr)
{
if(!ptr)
return 0;
setPattern((const char*)ptr);
return 1;
}
// Update pattern
long DirList::onUpdSetPattern(FXObject* sender,FXSelector,void*)
{
sender->handle(this,FXSEL(SEL_COMMAND,FXWindow::ID_SETVALUE),(void*)pattern.text());
return 1;
}
// Reverse sort order
long DirList::onCmdSortReverse(FXObject*,FXSelector,void*)
{
if(sortfunc==(FXTreeListSortFunc)ascending)
sortfunc=(FXTreeListSortFunc)descending;
else if(sortfunc==(FXTreeListSortFunc)descending)
sortfunc=(FXTreeListSortFunc)ascending;
else if(sortfunc==(FXTreeListSortFunc)ascendingCase)
sortfunc=(FXTreeListSortFunc)descendingCase;
else if(sortfunc==(FXTreeListSortFunc)descendingCase)
sortfunc=(FXTreeListSortFunc)ascendingCase;
scan(TRUE);
return 1;
}
// Update sender
long DirList::onUpdSortReverse(FXObject* sender,FXSelector,void* ptr)
{
sender->handle(this,(sortfunc==(FXTreeListSortFunc)descending || sortfunc==(FXTreeListSortFunc)descendingCase) ? FXSEL(SEL_COMMAND,ID_CHECK) : FXSEL(SEL_COMMAND,ID_UNCHECK),ptr);
return 1;
}
// Toggle case sensitivity
long DirList::onCmdSortCase(FXObject*,FXSelector,void*)
{
if(sortfunc==(FXTreeListSortFunc)ascending)
sortfunc=(FXTreeListSortFunc)ascendingCase;
else if(sortfunc==(FXTreeListSortFunc)descending)
sortfunc=(FXTreeListSortFunc)descendingCase;
else if(sortfunc==(FXTreeListSortFunc)ascendingCase)
sortfunc=(FXTreeListSortFunc)ascending;
else if(sortfunc==(FXTreeListSortFunc)descendingCase)
sortfunc=(FXTreeListSortFunc)descending;
scan(TRUE);
return 1;
}
// Check if case sensitive
long DirList::onUpdSortCase(FXObject* sender,FXSelector,void* ptr)
{
sender->handle(this,(sortfunc==(FXTreeListSortFunc)ascendingCase || sortfunc==(FXTreeListSortFunc)descendingCase) ? FXSEL(SEL_COMMAND,ID_CHECK) : FXSEL(SEL_COMMAND,ID_UNCHECK),ptr);
return 1;
}
// Close directory
long DirList::onClosed(FXObject*,FXSelector,void* ptr)
{
DirItem *item=(DirItem*)ptr;
if(item->state&DirItem::FOLDER)
return target && target->handle(this,FXSEL(SEL_CLOSED,message),ptr);
return 1;
}
// Open directory
long DirList::onOpened(FXObject*,FXSelector,void* ptr)
{
DirItem *item=(DirItem*)ptr;
if(item->state&DirItem::FOLDER)
return target && target->handle(this,FXSEL(SEL_OPENED,message),ptr);
return 1;
}
// Item opened
long DirList::onExpanded(FXObject* sender,FXSelector sel,void* ptr)
{
DirItem *item=(DirItem*)ptr;
if(!(item->state&DirItem::FOLDER))
return 0;
// Expand tree item
expandTree((TreeItem*)item,TRUE);
listChildItems(item);
// Now we know for sure whether we really have subitems or not
if(!item->first)
item->state&=~DirItem::HASITEMS;
else
item->state|=DirItem::HASITEMS;
sortChildItems(item);
return 1;
}
// Item closed
long DirList::onCollapsed(FXObject* sender,FXSelector sel,void* ptr)
{
DirItem *item=(DirItem*)ptr;
if(!(item->state&DirItem::FOLDER))
return 0;
// Collapse tree item
collapseTree((TreeItem*)item,TRUE);
return 1;
}
// Expand tree
FXbool DirList::expandTree(TreeItem* tree,FXbool notify)
{
if(FXTreeList::expandTree(tree,notify))
{
if(isItemDirectory(tree))
{
listChildItems((DirItem*)tree);
sortChildItems(tree);
}
return TRUE;
}
return FALSE;
}
// Collapse tree
FXbool DirList::collapseTree(TreeItem* tree,FXbool notify)
{
if(FXTreeList::collapseTree(tree,notify))
{
if(isItemDirectory(tree))
{
// As a memory saving feature, all knowledge below this item
// is deleted; we'll just recreate it when its reexpanded!
removeItems(tree->first,tree->last);
recalc();
}
return TRUE;
}
return FALSE;
}
#if defined(linux)
// To periodically scan /proc/mounts and refresh the mtdevices list
long DirList::onMtdevicesRefresh(FXObject*,FXSelector,void*)
{
// Do the refresh only if xfe has the focus
//if (focuswindow->hasFocus())
//{
struct mntent *mnt;
FXStringDict* tmpdict = new FXStringDict();
FILE *mtab=setmntent(MTAB_PATH,"r");
if(mtab)
{
while((mnt=getmntent(mtab)))
{
// To fix an issue with some Linux distributions
FXString mntdir=mnt->mnt_dir;
if (mntdir!="/dev/.static/dev" && mntdir.rfind(".gvfs",5,mntdir.length())==-1)
{
tmpdict->insert(mnt->mnt_dir,"");
if (mtdevices->find(mnt->mnt_dir))
mtdevices->remove(mnt->mnt_dir);
mtdevices->insert(mnt->mnt_dir,mnt->mnt_type);
}
}
endmntent(mtab);
}
// Remove mount points that don't exist anymore
FXint s;
const FXchar *key, *data;
for (s = mtdevices->first(); s < mtdevices->size(); s = mtdevices->next(s))
{
key = mtdevices->key(s);
data = mtdevices->data(s);
if (!tmpdict->find(mtdevices->key(s)))
mtdevices->remove(mtdevices->key(s));
}
delete tmpdict;
//}
// Reset timer again
getApp()->addTimeout(this,ID_MTDEVICES_REFRESH,MTDEVICES_INTERVAL*1000);
return 0;
}
// To periodically scan /proc/mounts and detect up and down mounted devices
// NB : the refresh period is much longer than for onMtdevicesRefresh
long DirList::onUpdevicesRefresh(FXObject*,FXSelector,void*)
{
struct mntent *mnt;
struct stat statbuf;
FXString mtstate;
FXbool mount_warn=getApp()->reg().readUnsignedEntry("OPTIONS","mount_warn",FALSE);
FXStringDict* tmpdict = new FXStringDict();
FILE *mtab=setmntent(MTAB_PATH,"r");
if(mtab)
{
while((mnt=getmntent(mtab)))
{
// To fix an issue with some Linux distributions
FXString mntdir=mnt->mnt_dir;
if (mntdir!="/dev/.static/dev" && mntdir.rfind(".gvfs",5,mntdir.length())==-1)
{
tmpdict->insert(mnt->mnt_dir,"");
if (lstatmt(mnt->mnt_dir,&statbuf)==-1)
{
mtstate="down";
if (mount_warn)
MessageBox::warning(this,BOX_OK,_("Warning"),_("Mount point %s is not responding..."),mnt->mnt_dir);
}
else
mtstate="up";
if (updevices->find(mnt->mnt_dir))
updevices->remove(mnt->mnt_dir);
updevices->insert(mnt->mnt_dir,mtstate.text());
}
}
endmntent(mtab);
}
// Remove mount points that don't exist anymore
FXint s;
const FXchar *key, *data;
for (s = updevices->first(); s < updevices->size(); s = updevices->next(s))
{
key = updevices->key(s);
data = updevices->data(s);
if (!tmpdict->find(updevices->key(s)))
updevices->remove(updevices->key(s));
}
delete tmpdict;
// Reset timer again
getApp()->addTimeout(this,ID_UPDEVICES_REFRESH,UPDEVICES_INTERVAL*1000);
return 0;
}
#endif
// Refresh with timer
long DirList::onCmdRefreshTimer(FXObject*,FXSelector,void*)
{
if(flags&FLAG_UPDATE)
{
scan(FALSE);
counter=(counter+1)%REFRESH_FREQUENCY;
}
// Reset timer again
getApp()->addTimeout(this,ID_REFRESH_TIMER,REFRESH_INTERVAL);
return 0;
}
// Force refresh
long DirList::onCmdRefresh(FXObject*,FXSelector,void*)
{
scan(TRUE);
return 0;
}
// Scan items to see if listing is necessary
void DirList::scan(FXbool force)
{
FXString pathname;
struct stat info;
DirItem *item;
// Do root first time
if(!firstitem || force)
{
listRootItems();
sortRootItems();
}
// Check all items
item=(DirItem*)firstitem;
while(item)
{
// Is expanded directory?
if(item->isDirectory() && item->isExpanded())
{
// Get the full path of the item
pathname=getItemPathname((TreeItem*)item);
// Stat this directory
if (statrep(pathname.text(),&info)==0)
{
// Get the mod date of the item
FXTime newdate=(FXTime)FXMAX(info.st_mtime,info.st_ctime);
// Forced, date was changed, or failed to get proper date and counter expired
if(force || (item->date!=newdate) || (counter==0))
{
// And do the refresh
listChildItems(item);
sortChildItems(item);
// Remember when we did this
item->date=newdate;
}
// Go deeper
if(item->first)
{
item=(DirItem*)item->first;
continue;
}
}
// Directory does not exist
else
{
// Go to parent and rescan
setDirectory(FXPath::directory(pathname),FALSE);
scan(TRUE);
break;
}
}
// Go up
while(!item->next && item->parent)
{
item=(DirItem*)item->parent;
}
// Go to next
item=(DirItem*)item->next;
}
}
// List root directories
void DirList::listRootItems()
{
DirItem *item=(DirItem*)firstitem;
FXIcon *openicon, *closedicon;
FileAssoc *fileassoc;
// First time, make root node
if(!item)
item=list=(DirItem*)appendItem(NULL,PATHSEPSTRING,harddiskicon,harddiskicon,NULL,TRUE);
// Root is a directory, has items under it, and is searchable
item->state|=DirItem::FOLDER|DirItem::HASITEMS;
item->state&=~(DirItem::CHARDEV|DirItem::BLOCKDEV|DirItem::FIFO|DirItem::SOCK|DirItem::SYMLINK|DirItem::EXECUTABLE);
// Determine associations, icons and type
fileassoc=NULL;
openicon=harddiskicon;
closedicon=harddiskicon;
if(associations)
fileassoc=associations->findDirBinding(PATHSEPSTRING);
// If association is found, use it
if(fileassoc)
{
if(fileassoc->miniicon)
closedicon=fileassoc->miniicon;
if(fileassoc->miniiconopen)
openicon=fileassoc->miniiconopen;
}
// Update item information
item->openIcon=openicon;
item->closedIcon=closedicon;
item->size=0L;
item->assoc=fileassoc;
item->date=0;
// Create item
if(id())
item->create();
// Need to layout
recalc();
}
// List child items
void DirList::listChildItems(DirItem *par)
{
DirItem *oldlist, *newlist, **po, **pp, **pn, *item, *link;
FXIcon *openicon, *closedicon;
FileAssoc *fileassoc;
DIR *dirp;
struct dirent *dp;
struct stat info;
FXString pathname, directory, name;
FXString type, mod, usrid, grpid, atts, del;
FXint islink;
long deldate;
// Path to parent node
directory=getItemPathname((TreeItem*)par);
// Build new insert-order list
oldlist=par->list;
newlist=NULL;
// Assemble lists
po=&oldlist;
pn=&newlist;
// Get directory stream pointer
dirp=opendir(directory.text());
// Managed to open directory
if(dirp)
{
// Process directory entries
#ifdef FOX_THREAD_SAFE
struct fxdirent dirresult;
while(!readdir_r(dirp,&dirresult,&dp) && dp)
{
#else
while((dp=readdir(dirp))!=NULL)
{
#endif
// Get name of entry
name=dp->d_name;
// A dot special file?
if(name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0)))
continue;
// Hidden file or directory normally not shown
if(name[0]=='.' && !(options&DIRLIST_SHOWHIDDEN))
continue;
// Build full pathname of entry
pathname=directory;
if(!ISPATHSEP(pathname[pathname.length()-1]))
pathname+=PATHSEPSTRING;
pathname+=name;
// Get file/link info
if(lstatrep(pathname.text(),&info)!=0)
continue;
// If its a link, get the info on file itself
islink=S_ISLNK(info.st_mode);
if(islink && statrep(pathname.text(),&info)!=0)
continue;
// If it is not a directory, and not showing files and matching pattern skip it
if(!S_ISDIR(info.st_mode) && !((options&DIRLIST_SHOWFILES) && FXPath::match(pattern,name,matchmode)))
continue;
// Find it, and take it out from the old list if found
for(pp=po; (item=*pp)!=NULL; pp=&item->link)
{
if(compare(item->label,name)==0)
{
*pp=item->link;
item->link=NULL;
po=pp;
goto fnd;
}
}
// Not found; prepend before list
item=(DirItem*)appendItem(par,name,minifolderopenicon,minifolderclosedicon,NULL,TRUE);
// Next gets hung after this one
fnd:
*pn=item;
pn=&item->link;
// Item flags
if(info.st_mode&(S_IXUSR|S_IXGRP|S_IXOTH))
item->state|=DirItem::EXECUTABLE;
else
item->state&=~DirItem::EXECUTABLE;
if(S_ISDIR(info.st_mode))
{
item->state|=DirItem::FOLDER;
item->state&=~DirItem::EXECUTABLE;
}
else
item->state&=~(DirItem::FOLDER|DirItem::HASITEMS);
if(S_ISCHR(info.st_mode))
{
item->state|=DirItem::CHARDEV;
item->state&=~DirItem::EXECUTABLE;
}
else
item->state&=~DirItem::CHARDEV;
if(S_ISBLK(info.st_mode))
{
item->state|=DirItem::BLOCKDEV;
item->state&=~DirItem::EXECUTABLE;
}
else
item->state&=~DirItem::BLOCKDEV;
if(S_ISFIFO(info.st_mode))
{
item->state|=DirItem::FIFO;
item->state&=~DirItem::EXECUTABLE;
}
else
item->state&=~DirItem::FIFO;
if(S_ISSOCK(info.st_mode))
{
item->state|=DirItem::SOCK;
item->state&=~DirItem::EXECUTABLE;
}
else
item->state&=~DirItem::SOCK;
if(islink)
item->state|=DirItem::SYMLINK;
else
{
item->state&=~DirItem::SYMLINK;
}
// We can drag items
item->state|=DirItem::DRAGGABLE;
// Assume no associations
fileassoc=NULL;
// Determine icons and type
if(item->state&DirItem::FOLDER)
{
if(!::isReadExecutable(pathname))
{
openicon=minifolderlockedicon;
closedicon=minifolderlockedicon;
}
else
{
openicon=minifolderopenicon;
closedicon=minifolderclosedicon;
}
if(associations)
fileassoc=associations->findDirBinding(pathname.text());
}
else if(item->state&DirItem::EXECUTABLE)
{
openicon=miniappicon;
closedicon=miniappicon;
if(associations)
fileassoc=associations->findExecBinding(pathname.text());
}
else
{
openicon=minidocicon;
closedicon=minidocicon;
if(associations)
fileassoc=associations->findFileBinding(pathname.text());
}
// If association is found, use it
if(fileassoc)
{
if(fileassoc->miniicon)
closedicon=fileassoc->miniicon;
if(fileassoc->miniiconopen)
openicon=fileassoc->miniiconopen;
}
// Update item information
item->openIcon=openicon;
item->closedIcon=closedicon;
item->size=(unsigned long)info.st_size;
item->assoc=fileassoc;
item->date=info.st_mtime;
// Set the HASITEMS flag
(hasSubDirs(pathname.text())==1 ? item->setHasItems(TRUE) : item->setHasItems(FALSE));
// Default folder type
type=_("Folder");
// Obtain user name
FXString usrid=FXSystem::userName(info.st_uid);
// Obtain group name
FXString grpid=FXSystem::groupName(info.st_gid);
// Permissions (caution : we don't use the FXSystem::modeString() function because
// it seems to be incompatible with the info.st_mode format)
FXString atts=::permissions(info.st_mode);
// Modification time
mod=FXSystem::time("%x %X",item->date);
// If we are in trash can, obtain the deletion time
deldate=0;
del="";
if (FXPath::directory(pathname)==trashfileslocation)
{
char *endptr;
FXString str;
str=pathname.rafter('_');
str=str.rbefore('-');
deldate=strtol(str.text(),&endptr,10);
if (deldate!=0)
del=FXSystem::time("%x %X",deldate);
}
#if defined(linux)
// Mounted devices may have a specific icon
if(mtdevices->find(pathname.text()))
{
type=_("Mount point");
if(streq(mtdevices->find(pathname.text()),"cifs"))
{
item->closedIcon=nfsdriveicon;
item->openIcon=nfsdriveicon;
}
else
{
item->closedIcon=harddiskicon;
item->openIcon=harddiskicon;
}
}
// Devices found in fstab may have a specific icon
if(fsdevices->find(pathname.text()))
{
type=_("Mount point");
if(streq(fsdevices->find(pathname.text()),"harddisk"))
{
item->closedIcon=harddiskicon;
item->openIcon=harddiskicon;
}
else if(streq(fsdevices->find(pathname.text()),"nfsdisk"))
{
item->closedIcon=nfsdriveicon;
item->openIcon=nfsdriveicon;
}
else if(streq(fsdevices->find(pathname.text()),"smbdisk"))
{
item->closedIcon=nfsdriveicon;
item->openIcon=nfsdriveicon;
}
else if(streq(fsdevices->find(pathname.text()),"floppy"))
{
item->closedIcon=floppyicon;
item->openIcon=floppyicon;
}
else if(streq(fsdevices->find(pathname.text()),"cdrom"))
{
item->closedIcon=cdromicon;
item->openIcon=cdromicon;
}
else if(streq(fsdevices->find(pathname.text()),"zip"))
{
item->closedIcon=zipicon;
item->openIcon=zipicon;
}
}
#endif
// Symbolic links have a specific icon
if(islink)
{
type=_("Link to Folder");
item->closedIcon=minilinkicon;
item->openIcon=minilinkicon;
}
// Data used to update the tooltip
item->tdata=item->label+"\t"+type+"\t"+mod+"\t"+usrid+"\t"+grpid+"\t"+atts+"\t"+del+"\t"+pathname;
item->setData(&item->tdata);
// Create item
if(id())
item->create();
}
// Close it
closedir(dirp);
}
// Wipe items remaining in list:- they have disappeared!!
for(item=oldlist; item; item=link)
{
link=item->link;
removeItem(item,TRUE);
}
// Now we know for sure whether we really have subitems or not
if(par->first)
par->state|=DirItem::HASITEMS;
else
par->state&=~DirItem::HASITEMS;
// Remember new list
par->list=newlist;
// Need to layout
recalc();
}
// Is directory
FXbool DirList::isItemDirectory(const TreeItem* item) const
{
if(item==NULL)
fxerror("%s::isItemDirectory: item is NULL.\n",getClassName());
return (item->state&DirItem::FOLDER)!=0;
}
// Is file
FXbool DirList::isItemFile(const TreeItem* item) const
{
if(item==NULL)
fxerror("%s::isItemFile: item is NULL.\n",getClassName());
return (item->state&(DirItem::FOLDER|DirItem::CHARDEV|DirItem::BLOCKDEV|DirItem::FIFO|DirItem::SOCK))==0;
}
// Is executable
FXbool DirList::isItemExecutable(const TreeItem* item) const
{
if(item==NULL)
fxerror("%s::isItemExecutable: item is NULL.\n",getClassName());
return (item->state&DirItem::EXECUTABLE)!=0;
}
// Return absolute pathname of item
FXString DirList::getItemPathname(const TreeItem* item) const
{
FXString pathname;
if(item)
{
while(1)
{
pathname.prepend(item->getText());
item=(TreeItem*)item->parent;
if(!item)
break;
if(item->parent)
pathname.prepend(PATHSEP);
}
}
return pathname;
}
// Return the item from the absolute pathname
TreeItem* DirList::getPathnameItem(const FXString& path)
{
register TreeItem *item,*it;
register FXint beg=0,end=0;
FXString name;
if(!path.empty())
{
if(ISPATHSEP(path[0]))
end++;
if(beg<end)
{
name=path.mid(beg,end-beg);
for(it=(TreeItem*)firstitem; it; it=(TreeItem*)it->next)
{
if(compare(name,it->getText())==0)
goto x;
}
listRootItems();
sortRootItems();
for(it=(TreeItem*)firstitem; it; it=(TreeItem*)it->next)
{
if(compare(name,it->getText())==0)
goto x;
}
return NULL;
x:
item=it;
FXASSERT(item);
while(end<path.length())
{
beg=end;
while(end<path.length() && !ISPATHSEP(path[end]))
end++;
name=path.mid(beg,end-beg);
for(it=(TreeItem*)item->first; it; it=(TreeItem*)it->next)
{
if(compare(name,it->getText())==0)
goto y;
}
listChildItems((DirItem*)item);
sortChildItems(item);
for(it=(TreeItem*)item->first; it; it=(TreeItem*)it->next)
{
if(compare(name,it->getText())==0)
goto y;
}
return item;
y:
item=it;
FXASSERT(item);
if(end<path.length() && ISPATHSEP(path[end]))
end++;
}
FXASSERT(item);
return item;
}
}
return NULL;
}
// Obtain item's file name only
FXString DirList::getItemFilename(const TreeItem* item) const
{
if(item==NULL)
fxerror("%s::getItemFilename: item is NULL.\n",getClassName());
return item->label;
}
// Open all intermediate directories down toward given one
void DirList::setDirectory(const FXString& pathname,FXbool notify)
{
if(!pathname.empty())
{
FXString path=FXPath::absolute(getItemPathname((TreeItem*)currentitem),pathname);
while(!FXPath::isTopDirectory(path) && !::isDirectory(path))
path=FXPath::upLevel(path);
TreeItem *item=getPathnameItem(path);
if(id())
layout();
makeItemVisible(item);
setCurrentItem(item,notify);
}
}
// Return directory part of path to current item
FXString DirList::getDirectory() const
{
const TreeItem* item=(TreeItem*)currentitem;
while(item)
{
if(item->state&DirItem::FOLDER)
return getItemPathname(item);
item=(TreeItem*)item->parent;
}
return "";
}
// Set current (dir/file) name path
void DirList::setCurrentFile(const FXString& pathname,FXbool notify)
{
if(!pathname.empty())
{
FXString path=FXPath::absolute(getItemPathname((TreeItem*)currentitem),pathname);
while(!FXPath::isTopDirectory(path) && !::exists(path))
{
path=FXPath::upLevel(path);
}
TreeItem *item=getPathnameItem(path);
if(id())
layout();
makeItemVisible(item);
setCurrentItem(item,notify);
}
}
// Get current (dir/file) name path
FXString DirList::getCurrentFile() const
{
return getItemPathname((TreeItem*)currentitem);
}
// Get list style
FXbool DirList::showFiles() const
{
return (options&DIRLIST_SHOWFILES)!=0;
}
// Change list style
void DirList::showFiles(FXbool showing)
{
FXuint opts=options;
if(showing)
opts|=DIRLIST_SHOWFILES;
else
opts&=~DIRLIST_SHOWFILES;
if(options!=opts)
{
options=opts;
scan(TRUE);
}
}
// Return TRUE if showing hidden files
FXbool DirList::shownHiddenFiles() const
{
return (options&DIRLIST_SHOWHIDDEN)!=0;
}
// Change show hidden files mode
void DirList::showHiddenFiles(FXbool showing)
{
FXuint opts=options;
if(showing)
opts|=DIRLIST_SHOWHIDDEN;
else
opts&=~DIRLIST_SHOWHIDDEN;
if(opts!=options)
{
options=opts;
scan(TRUE);
}
}
// Set associations
void DirList::setAssociations(FileDict* assoc)
{
associations=assoc;
scan(TRUE);
}
// Set the pattern to filter
void DirList::setPattern(const FXString& ptrn)
{
if(ptrn.empty())
return;
if(pattern!=ptrn)
{
pattern=ptrn;
scan(TRUE);
}
}
// Change file match mode
void DirList::setMatchMode(FXuint mode)
{
if(matchmode!=mode)
{
matchmode=mode;
scan(TRUE);
}
}
// Cleanup
DirList::~DirList()
{
clearItems();
getApp()->removeTimeout(this,ID_REFRESH_TIMER);
getApp()->removeTimeout(this,ID_EXPAND_TIMER);
#if defined(linux)
getApp()->removeTimeout(this,ID_MTDEVICES_REFRESH);
getApp()->removeTimeout(this,ID_UPDEVICES_REFRESH);
#endif
if(!(options&DIRLIST_NO_OWN_ASSOC))
delete associations;
associations=(FileDict*)-1;
}
|