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 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
|
/*
Copyright (C) 2013 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "mainwindow.h"
#include <QLabel>
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QActionGroup>
#include <QWidgetAction>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QSplitter>
#include <QToolButton>
#include <QShortcut>
#include <QKeySequence>
#include <QSettings>
#include <QMimeData>
#include <QStandardPaths>
#include <QClipboard>
#include <QDebug>
#include "tabpage.h"
#include "launcher.h"
#include <libfm-qt6/filemenu.h>
#include <libfm-qt6/bookmarkaction.h>
#include <libfm-qt6/fileoperation.h>
#include <libfm-qt6/utilities.h>
#include <libfm-qt6/filepropsdialog.h>
#include <libfm-qt6/pathedit.h>
#include <libfm-qt6/pathbar.h>
#include <libfm-qt6/core/fileinfo.h>
#include <libfm-qt6/mountoperation.h>
#include "ui_about.h"
#include "ui_shortcuts.h"
#include "application.h"
#include "bulkrename.h"
using namespace Fm;
namespace PCManFM {
ViewFrame::ViewFrame(QWidget* parent):
QFrame(parent),
topBar_(nullptr) {
QVBoxLayout* vBox = new QVBoxLayout;
vBox->setContentsMargins(0, 0, 0, 0);
tabBar_ = new TabBar;
tabBar_->setFocusPolicy(Qt::NoFocus);
stackedWidget_ = new QStackedWidget;
vBox->addWidget(tabBar_);
vBox->addWidget(stackedWidget_, 1);
setLayout(vBox);
// tabbed browsing interface
tabBar_->setDocumentMode(true);
tabBar_->setExpanding(false);
tabBar_->setMovable(true); // reorder the tabs by dragging
// switch to the tab under the cursor during dnd.
tabBar_->setChangeCurrentOnDrag(true);
tabBar_->setAcceptDrops(true);
tabBar_->setContextMenuPolicy(Qt::CustomContextMenu);
}
void ViewFrame::createTopBar(bool usePathButtons) {
if(QVBoxLayout* vBox = qobject_cast<QVBoxLayout*>(layout())) {
if(usePathButtons) {
if (qobject_cast<Fm::PathEdit*>(topBar_)) {
delete topBar_;
topBar_ = nullptr;
}
if(topBar_ == nullptr) {
topBar_ = new Fm::PathBar();
vBox->insertWidget(0, topBar_);
}
}
else {
if(qobject_cast<Fm::PathBar*>(topBar_)) {
delete topBar_;
topBar_ = nullptr;
}
if(topBar_ == nullptr) {
topBar_ = new Fm::PathEdit();
vBox->insertWidget(0, topBar_);
}
}
}
}
void ViewFrame::removeTopBar() {
if(topBar_ != nullptr) {
if(QVBoxLayout* vBox = qobject_cast<QVBoxLayout*>(layout())) {
vBox->removeWidget(topBar_);
delete topBar_;
topBar_ = nullptr;
}
}
}
//======================================================================
// static
QPointer<MainWindow> MainWindow::lastActive_;
MainWindow::MainWindow(Fm::FilePath path):
QMainWindow(),
pathEntry_(nullptr),
pathBar_(nullptr),
bookmarks_{Fm::Bookmarks::globalInstance()},
fileLauncher_(this),
rightClickIndex_(-1),
updatingViewMenu_(false),
menuSpacer_(nullptr),
activeViewFrame_(nullptr),
splitTabsNum_(-1) {
Settings& settings = static_cast<Application*>(qApp)->settings();
setAttribute(Qt::WA_DeleteOnClose);
// setup user interface
ui.setupUi(this);
// add a warning label to the root instance
if(geteuid() == 0) {
QLabel *warningLabel = new QLabel(tr("Root Instance"));
warningLabel->setAlignment(Qt::AlignCenter);
warningLabel->setTextInteractionFlags(Qt::NoTextInteraction);
warningLabel->setStyleSheet(QLatin1String("QLabel {background-color: #7d0000; color: white; font-weight:bold; border-radius: 3px; margin: 2px; padding: 5px;}"));
ui.verticalLayout->addWidget(warningLabel);
ui.verticalLayout->setStretch(0, 1);
}
splitView_ = path && settings.splitView(); // splt view needs a path
// hide menu items that are not usable
//if(!uriExists("computer:///"))
// ui.actionComputer->setVisible(false);
if(!settings.supportTrash()) {
ui.actionTrash->setVisible(false);
}
// add a context menu for showing browse history to back and forward buttons
QToolButton* forwardButton = static_cast<QToolButton*>(ui.toolBar->widgetForAction(ui.actionGoForward));
forwardButton->setContextMenuPolicy(Qt::CustomContextMenu);
connect(forwardButton, &QToolButton::customContextMenuRequested, this, &MainWindow::onBackForwardContextMenu);
QToolButton* backButton = static_cast<QToolButton*>(ui.toolBar->widgetForAction(ui.actionGoBack));
backButton->setContextMenuPolicy(Qt::CustomContextMenu);
connect(backButton, &QToolButton::customContextMenuRequested, this, &MainWindow::onBackForwardContextMenu);
connect(ui.actionCloseRight, &QAction::triggered, this, &MainWindow::closeRightTabs);
connect(ui.actionCloseLeft, &QAction::triggered, this, &MainWindow::closeLeftTabs);
connect(ui.actionCloseOther, &QAction::triggered, this, &MainWindow::closeOtherTabs);
ui.actionFilter->setChecked(settings.showFilter());
ui.actionShowThumbnails->setChecked(settings.showThumbnails());
// menu
ui.actionDelete->setText(settings.useTrash() ? tr("&Move to Trash") : tr("&Delete"));
ui.actionDelete->setIcon(settings.useTrash() ? QIcon::fromTheme(QStringLiteral("user-trash")) : QIcon::fromTheme(QStringLiteral("edit-delete")));
ui.actionNetwork->setIcon(QIcon::fromTheme(QStringLiteral("network"), QIcon::fromTheme(QStringLiteral("folder-network"))));
ui.actionApplications->setIcon(QIcon::fromTheme(QStringLiteral("system-software-install"),
QIcon::fromTheme(QStringLiteral("applications-accessories"))));
// side pane
ui.sidePane->setVisible(settings.isSidePaneVisible());
ui.actionSidePane->setChecked(settings.isSidePaneVisible());
ui.sidePane->setIconSize(QSize(settings.sidePaneIconSize(), settings.sidePaneIconSize()));
ui.sidePane->setMode(settings.sidePaneMode());
ui.sidePane->restoreHiddenPlaces(settings.getHiddenPlaces());
connect(ui.sidePane, &Fm::SidePane::chdirRequested, this, &MainWindow::onSidePaneChdirRequested);
connect(ui.sidePane, &Fm::SidePane::openFolderInNewWindowRequested, this, &MainWindow::onSidePaneOpenFolderInNewWindowRequested);
connect(ui.sidePane, &Fm::SidePane::openFolderInNewTabRequested, this, &MainWindow::onSidePaneOpenFolderInNewTabRequested);
connect(ui.sidePane, &Fm::SidePane::openFolderInTerminalRequested, this, &MainWindow::onSidePaneOpenFolderInTerminalRequested);
connect(ui.sidePane, &Fm::SidePane::createNewFolderRequested, this, &MainWindow::onSidePaneCreateNewFolderRequested);
connect(ui.sidePane, &Fm::SidePane::modeChanged, this, &MainWindow::onSidePaneModeChanged);
connect(ui.sidePane, &Fm::SidePane::hiddenPlaceSet, this, &MainWindow::onSettingHiddenPlace);
// detect change of splitter position
connect(ui.splitter, &QSplitter::splitterMoved, this, &MainWindow::onSplitterMoved);
// add filesystem info to status bar
fsInfoLabel_ = new QLabel(ui.statusbar);
ui.statusbar->addPermanentWidget(fsInfoLabel_);
// setup the splitter
ui.splitter->setStretchFactor(1, 1); // only the right pane can be stretched
QList<int> sizes;
sizes.append(settings.splitterPos());
sizes.append(300);
ui.splitter->setSizes(sizes);
// load bookmark menu
connect(bookmarks_.get(), &Fm::Bookmarks::changed, this, &MainWindow::onBookmarksChanged);
loadBookmarksMenu();
// use generic icons for view actions only if theme icons don't exist
ui.actionIconView->setIcon(QIcon::fromTheme(QLatin1String("view-list-icons"), style()->standardIcon(QStyle::SP_FileDialogContentsView)));
ui.actionThumbnailView->setIcon(QIcon::fromTheme(QLatin1String("view-preview"), style()->standardIcon(QStyle::SP_FileDialogInfoView)));
ui.actionCompactView->setIcon(QIcon::fromTheme(QLatin1String("view-list-text"), style()->standardIcon(QStyle::SP_FileDialogListView)));
ui.actionDetailedList->setIcon(QIcon::fromTheme(QLatin1String("view-list-details"), style()->standardIcon(QStyle::SP_FileDialogDetailedView)));
// Fix the menu groups which is not done by Qt designer
// To my surprise, this was supported in Qt designer 3 :-(
QActionGroup* group = new QActionGroup(ui.menu_View);
group->setExclusive(true);
group->addAction(ui.actionIconView);
group->addAction(ui.actionCompactView);
group->addAction(ui.actionThumbnailView);
group->addAction(ui.actionDetailedList);
group = new QActionGroup(ui.menuSorting);
group->setExclusive(true);
group->addAction(ui.actionByFileName);
group->addAction(ui.actionByMTime);
group->addAction(ui.actionByCrTime);
group->addAction(ui.actionByDTime);
group->addAction(ui.actionByFileSize);
group->addAction(ui.actionByFileType);
group->addAction(ui.actionByOwner);
group->addAction(ui.actionByGroup);
group = new QActionGroup(ui.menuSorting);
group->setExclusive(true);
group->addAction(ui.actionAscending);
group->addAction(ui.actionDescending);
group = new QActionGroup(ui.menuPathBarStyle);
group->setExclusive(true);
group->addAction(ui.actionLocationBar);
group->addAction(ui.actionPathButtons);
// Add menubar actions to the main window this is necessary so that actions
// shortcuts are still working when the menubar is hidden.
addActions(ui.menubar->actions());
// Show or hide the menu bar
QMenu* menu = new QMenu(ui.toolBar);
menu->addMenu(ui.menu_File);
menu->addMenu(ui.menu_Edit);
menu->addMenu(ui.menu_View);
menu->addMenu(ui.menu_Go);
menu->addMenu(ui.menu_Bookmarks);
menu->addMenu(ui.menu_Tool);
menu->addMenu(ui.menu_Help);
ui.actionMenu->setMenu(menu);
if(ui.actionMenu->icon().isNull()) {
ui.actionMenu->setIcon(QIcon::fromTheme(QStringLiteral("applications-system")));
}
QToolButton* menuBtn = static_cast<QToolButton*>(ui.toolBar->widgetForAction(ui.actionMenu));
menuBtn->setPopupMode(QToolButton::InstantPopup);
menuSep_ = ui.toolBar->insertSeparator(ui.actionMenu);
menuSep_->setVisible(!settings.showMenuBar() && !splitView_);
ui.actionMenu->setVisible(!settings.showMenuBar());
ui.menubar->setVisible(settings.showMenuBar());
ui.actionMenu_bar->setChecked(settings.showMenuBar());
connect(ui.actionMenu_bar, &QAction::triggered, this, &MainWindow::toggleMenuBar);
// recent files
int recentNumber = settings.getRecentFilesNumber();
if(recentNumber == 0) {
ui.menuRecentFiles->setEnabled(false);
}
else {
QAction* recentAction = nullptr;
for(int i = 0; i < recentNumber; ++i) {
recentAction = new QAction(this);
recentAction->setVisible(false);
connect(recentAction, &QAction::triggered, this, &MainWindow::lanunchRecentFile);
ui.menuRecentFiles->addAction(recentAction);
}
ui.menuRecentFiles->addSeparator();
ui.menuRecentFiles->addAction(ui.actionClearRecent);
}
connect(ui.menuRecentFiles, &QMenu::aboutToShow, this, &MainWindow::updateRecenMenu);
connect(ui.actionClearRecent, &QAction::triggered, this, &MainWindow::clearRecentMenu);
// create shortcuts
QShortcut* shortcut;
shortcut = new QShortcut(QKeySequence(Qt::Key_Escape), this);
connect(shortcut, &QShortcut::activated, [this] {
if(currentPage()) {
currentPage()->clearFilter();
currentPage()->folderView()->childView()->setFocus();
}
});
shortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Escape), this);
connect(shortcut, &QShortcut::activated, [this] {
if(ui.sidePane->isVisible() && ui.sidePane->view()) {
ui.sidePane->view()->setFocus();
}
});
shortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_L), this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::focusPathEntry);
shortcut = new QShortcut(Qt::ALT | Qt::Key_D, this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::focusPathEntry);
shortcut = new QShortcut(Qt::CTRL | Qt::Key_Tab, this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::onShortcutNextTab);
shortcut = new QShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_Tab, this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::onShortcutPrevTab);
// Add Ctrl+PgUp and Ctrl+PgDown as well, because they are common in Firefox
// , Opera, Google Chromium/Google Chrome and most other tab-using
// applications.
shortcut = new QShortcut(Qt::CTRL | Qt::Key_PageDown, this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::onShortcutNextTab);
shortcut = new QShortcut(Qt::CTRL | Qt::Key_PageUp, this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::onShortcutPrevTab);
int i;
for(i = 0; i < 10; ++i) {
shortcut = new QShortcut(QKeySequence(Qt::ALT | (Qt::Key_0 + i)), this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::onShortcutJumpToTab);
shortcut = new QShortcut(QKeySequence(Qt::CTRL | (Qt::Key_0 + i)), this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::onShortcutJumpToTab);
}
shortcut = new QShortcut(QKeySequence(Qt::SHIFT | Qt::Key_Delete), this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::on_actionDelete_triggered);
// in addition to F3, for convenience
shortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_F), this);
connect(shortcut, &QShortcut::activated, ui.actionFindFiles, &QAction::trigger);
// in addition to Alt+Return, for convenience
shortcut = new QShortcut(Qt::ALT | Qt::Key_Enter, this);
connect(shortcut, &QShortcut::activated, this, &MainWindow::on_actionFileProperties_triggered);
addViewFrame(path);
if(splitView_) {
// put the menu button on the right (there's no path bar/entry on the toolbar)
QWidget* w = new QWidget(this);
w->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
menuSpacer_ = ui.toolBar->insertWidget(ui.actionMenu, w);
ui.actionSplitView->setChecked(true);
addViewFrame(path);
qApp->removeEventFilter(this); // precaution
qApp->installEventFilter(this);
}
else {
ui.actionSplitView->setChecked(false);
setAcceptDrops(true); // we want tab dnd in the simple mode
}
createPathBar(settings.pathBarButtons());
if(settings.pathBarButtons()) {
ui.actionPathButtons->setChecked(true);
}
else {
ui.actionLocationBar->setChecked(true);
}
// size from settings
resize(settings.windowWidth(), settings.windowHeight());
if(settings.rememberWindowSize() && settings.windowMaximized()) {
setWindowState(windowState() | Qt::WindowMaximized);
}
if(QApplication::layoutDirection() == Qt::RightToLeft) {
setRTLIcons(true);
}
if(static_cast<Application*>(qApp)->underWayland()) {
ui.actionOpenAsRoot->setEnabled(false);
}
}
MainWindow::~MainWindow() = default;
// Activate a view frame appropriately and give a special style to the inactive one(s).
// NOTE: This function is called only with the split mode.
bool MainWindow::eventFilter(QObject* watched, QEvent* event) {
if(qobject_cast<QWidget*>(watched)) {
if(event->type() == QEvent::FocusIn
// the event has happened inside the splitter
&& ui.viewSplitter->isAncestorOf(qobject_cast<QWidget*>(watched))) {
for(int i = 0; i < ui.viewSplitter->count(); ++i) {
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(i))) {
if(viewFrame->isAncestorOf(qobject_cast<QWidget*>(watched))) {
// a widget inside this view frame has gained focus; ensure the view is active
if(activeViewFrame_ != viewFrame) {
activeViewFrame_ = viewFrame;
updateUIForCurrentPage(false); // WARNING: never set focus here!
}
if(viewFrame->palette().color(QPalette::Base)
!= qApp->palette().color(QPalette::Base)) {
viewFrame->setPalette(qApp->palette()); // restore the main palette
}
}
else if (viewFrame->palette().color(QPalette::Base)
== qApp->palette().color(QPalette::Base)) {
// Change the text and base palettes of an inactive view frame a little.
// NOTE: Style-sheets aren't used because they can interfere with QStyle.
QPalette palette = viewFrame->palette();
// There are various ways of getting a distinct color near the base color
// but this one gives the best results with almost all palettes:
QColor txtCol = palette.color(QPalette::Text);
QColor baseCol = palette.color(QPalette::Base);
baseCol.setRgbF(0.9 * baseCol.redF() + 0.1 * txtCol.redF(),
0.9 * baseCol.greenF() + 0.1 * txtCol.greenF(),
0.9 * baseCol.blueF() + 0.1 * txtCol.blueF(),
baseCol.alphaF());
palette.setColor(QPalette::Base, baseCol);
// view text
txtCol.setAlphaF(txtCol.alphaF() * 0.7);
palette.setColor(QPalette::Text, txtCol);
// window text (used in tabs)
txtCol = palette.color(QPalette::WindowText);
txtCol.setAlphaF(txtCol.alphaF() * 0.7);
palette.setColor(QPalette::WindowText, txtCol);
// button text (the disabled text color isn't changed because it may be
// used by some styles for drawing disabled path-bar arrow)
txtCol = palette.color(QPalette::ButtonText);
txtCol.setAlphaF(txtCol.alphaF() * 0.7);
palette.setColor(QPalette::Active, QPalette::ButtonText, txtCol);
palette.setColor(QPalette::Inactive, QPalette::ButtonText, txtCol);
viewFrame->setPalette(palette);
}
}
}
}
// Use the Tab key for switching between view frames
else if (event->type() == QEvent::KeyPress) {
if(QKeyEvent *ke = static_cast<QKeyEvent*>(event)) {
if(ke->key() == Qt::Key_Tab && ke->modifiers() == Qt::NoModifier) {
if(!qobject_cast<QTextEdit*>(watched) // not during inline renaming
&& ui.viewSplitter->isAncestorOf(qobject_cast<QWidget*>(watched))) {
// wrap the focus
for(int i = 0; i < ui.viewSplitter->count(); ++i) {
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(i))) {
if(activeViewFrame_ == viewFrame) {
int n = i < ui.viewSplitter->count() - 1 ? i + 1 : 0;
activeViewFrame_ = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(n));
updateUIForCurrentPage(); // focuses the view and calls this function again
return true;
}
}
}
}
}
}
}
}
return QMainWindow::eventFilter(watched, event);
}
void MainWindow::addViewFrame(const Fm::FilePath& path) {
ui.actionGo->setVisible(false);
Application* app = static_cast<Application*>(qApp);
Settings& settings = app->settings();
ViewFrame* viewFrame = new ViewFrame();
viewFrame->getTabBar()->setDetachable(!splitView_); // no tab DND with the split view
viewFrame->getTabBar()->setTabsClosable(settings.showTabClose());
viewFrame->getTabBar()->setAutoHide(!settings.alwaysShowTabs());
ui.viewSplitter->addWidget(viewFrame); // the splitter takes ownership of viewFrame
if(ui.viewSplitter->count() == 1) {
activeViewFrame_ = viewFrame;
}
else { // give equal widths to all view frames
QTimer::singleShot(0, this, [this] {
QList<int> sizes;
for(int i = 0; i < ui.viewSplitter->count(); ++i) {
sizes << ui.viewSplitter->width() / ui.viewSplitter->count();
}
ui.viewSplitter->setSizes(sizes);
});
}
connect(viewFrame->getTabBar(), &QTabBar::currentChanged, this, &MainWindow::onTabBarCurrentChanged);
connect(viewFrame->getTabBar(), &QTabBar::tabCloseRequested, this, &MainWindow::onTabBarCloseRequested);
connect(viewFrame->getTabBar(), &QTabBar::tabMoved, this, &MainWindow::onTabBarTabMoved);
connect(viewFrame->getTabBar(), &QTabBar::tabBarClicked, this, &MainWindow::onTabBarClicked);
connect(viewFrame->getTabBar(), &QTabBar::customContextMenuRequested, this, &MainWindow::tabContextMenu);
connect(viewFrame->getTabBar(), &QTabBar::tabBarDoubleClicked, this, [this](int index) {
if(index == -1) {
on_actionNewTab_triggered();
}
});
connect(viewFrame->getStackedWidget(), &QStackedWidget::widgetRemoved, this, &MainWindow::onStackedWidgetWidgetRemoved);
// the tab will be detached only after the DND is finished
connect(viewFrame->getTabBar(), &TabBar::tabDetached, this, &MainWindow::detachTab, Qt::QueuedConnection);
if(path) {
addTab(path, viewFrame);
}
}
void MainWindow::on_actionSplitView_triggered(bool checked) {
if(splitView_ == checked) {
return;
}
Application* app = static_cast<Application*>(qApp);
Settings& settings = app->settings();
splitView_ = checked;
settings.setSplitView(splitView_);
if(splitView_) { // split the view
// remove the path bar/entry from the toolbar
ui.actionGo->setVisible(false);
menuSep_->setVisible(false);
if(pathBar_ != nullptr) {
delete pathBar_;
pathBar_ = nullptr;
}
else if(pathEntry_ != nullptr) {
delete pathEntry_;
pathEntry_ = nullptr;
}
// add a spacer before the menu action if not existing
if(menuSpacer_ == nullptr) {
QWidget* w = new QWidget(this);
w->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
menuSpacer_ = ui.toolBar->insertWidget(ui.actionMenu, w);
}
menuSpacer_->setVisible(true);
// disable tab DND
activeViewFrame_->getTabBar()->setDetachable(false);
setAcceptDrops(false);
// add the current path to a new view frame
Fm::FilePath path;
TabPage* page = currentPage();
if(page) {
path = page->path();
}
addViewFrame(path);
qApp->removeEventFilter(this); // precaution
qApp->installEventFilter(this);
createPathBar(settings.pathBarButtons());
// reset the focus for the inactive view frame(s) to be styled by MainWindow::eventFilter()
if(page) {
page->folderView()->childView()->clearFocus();
page->folderView()->childView()->setFocus();
}
}
else { // remove splitting
menuSep_->setVisible(!settings.showMenuBar());
qApp->removeEventFilter(this);
for(int i = 0; i < ui.viewSplitter->count(); ++i) {
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(i))) {
if(viewFrame != activeViewFrame_) {
viewFrame->deleteLater(); // this may be called by onStackedWidgetWidgetRemoved()
}
}
}
// enable tab DND
activeViewFrame_->getTabBar()->setDetachable(true);
setAcceptDrops(true);
activeViewFrame_->removeTopBar();
if(menuSpacer_ != nullptr) {
menuSpacer_->setVisible(false);
}
createPathBar(settings.pathBarButtons());
}
}
ViewFrame* MainWindow::viewFrameForTabPage(TabPage* page) {
if(page) {
if(QStackedWidget* sw = qobject_cast<QStackedWidget*>(page->parentWidget())) {
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(sw->parentWidget())) {
return viewFrame;
}
}
}
return nullptr;
}
void MainWindow::chdir(Fm::FilePath path, ViewFrame* viewFrame) {
// wait until queued events are processed
QTimer::singleShot(0, viewFrame, [this, path, viewFrame] {
if(TabPage* page = currentPage(viewFrame)) {
page->chdir(path, true);
setTabIcon(page);
if(viewFrame == activeViewFrame_) {
updateUIForCurrentPage();
}
else {
if(Fm::PathBar* pathBar = qobject_cast<Fm::PathBar*>(viewFrame->getTopBar())) {
pathBar->setPath(page->path());
}
else if(Fm::PathEdit* pathEntry = qobject_cast<Fm::PathEdit*>(viewFrame->getTopBar())) {
pathEntry->setText(page->pathName());
}
}
}
});
}
void MainWindow::createPathBar(bool usePathButtons) {
// NOTE: Path bars/entries may be created after tab pages; so, their paths/texts should be set.
if(splitView_) {
for(int i = 0; i < ui.viewSplitter->count(); ++i) {
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(i))) {
viewFrame->createTopBar(usePathButtons);
TabPage* curPage = currentPage(viewFrame);
if(Fm::PathBar* pathBar = qobject_cast<Fm::PathBar*>(viewFrame->getTopBar())) {
connect(pathBar, &Fm::PathBar::chdir, this, &MainWindow::onPathBarChdir);
connect(pathBar, &Fm::PathBar::middleClickChdir, this, &MainWindow::onPathBarMiddleClickChdir);
connect(pathBar, &Fm::PathBar::editingFinished, this, &MainWindow::onResetFocus);
if(curPage) {
pathBar->setPath(curPage->path());
}
}
else if(Fm::PathEdit* pathEntry = qobject_cast<Fm::PathEdit*>(viewFrame->getTopBar())) {
connect(pathEntry, &Fm::PathEdit::returnPressed, this, &MainWindow::onPathEntryReturnPressed);
if(curPage) {
pathEntry->setText(curPage->pathName());
}
}
}
}
}
else {
QWidget* bar = nullptr;
TabPage* curPage = currentPage();
if(usePathButtons) {
if(pathEntry_ != nullptr) {
delete pathEntry_;
pathEntry_ = nullptr;
}
if(pathBar_ == nullptr) {
bar = pathBar_ = new Fm::PathBar(this);
connect(pathBar_, &Fm::PathBar::chdir, this, &MainWindow::onPathBarChdir);
connect(pathBar_, &Fm::PathBar::middleClickChdir, this, &MainWindow::onPathBarMiddleClickChdir);
connect(pathBar_, &Fm::PathBar::editingFinished, this, &MainWindow::onResetFocus);
if(curPage) {
pathBar_->setPath(currentPage()->path());
}
}
}
else {
if(pathBar_ != nullptr) {
delete pathBar_;
pathBar_ = nullptr;
}
if(pathEntry_ == nullptr) {
bar = pathEntry_ = new Fm::PathEdit(this);
connect(pathEntry_, &Fm::PathEdit::returnPressed, this, &MainWindow::onPathEntryReturnPressed);
if(curPage) {
pathEntry_->setText(curPage->pathName());
}
}
}
if(bar != nullptr) {
ui.toolBar->insertWidget(ui.actionGo, bar);
ui.actionGo->setVisible(!usePathButtons);
}
}
}
int MainWindow::addTabWithPage(TabPage* page, ViewFrame* viewFrame, Fm::FilePath path) {
if(page == nullptr || viewFrame == nullptr) {
return -1;
}
page->setFileLauncher(&fileLauncher_);
int index = viewFrame->getStackedWidget()->addWidget(page);
connect(page, &TabPage::titleChanged, this, &MainWindow::onTabPageTitleChanged);
connect(page, &TabPage::statusChanged, this, &MainWindow::onTabPageStatusChanged);
connect(page, &TabPage::sortFilterChanged, this, &MainWindow::onTabPageSortFilterChanged);
connect(page, &TabPage::backwardRequested, this, &MainWindow::on_actionGoBack_triggered);
connect(page, &TabPage::forwardRequested, this, &MainWindow::on_actionGoForward_triggered);
connect(page, &TabPage::backspacePressed, this, &MainWindow::on_actionGoUp_triggered);
connect(page, &TabPage::folderUnmounted, this, &MainWindow::onFolderUnmounted);
if(path) {
page->chdir(path, true);
}
QString tabText = page->title();
// remove newline (not all styles can handle it) and distinguish ampersand from mnemonic
tabText.replace(QLatin1Char('\n'), QLatin1Char(' '))
.replace(QLatin1Char('&'), QLatin1String("&&"));
viewFrame->getTabBar()->insertTab(index, tabText);
Settings& settings = static_cast<Application*>(qApp)->settings();
if(settings.switchToNewTab()) {
viewFrame->getTabBar()->setCurrentIndex(index); // also focuses the view
if (isMinimized()) {
setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
show();
}
}
else if(TabPage* tabPage = currentPage()) {
tabPage->folderView()->childView()->setFocus();
}
// also set tab icon (if the folder is customized)
setTabIcon(page);
return index;
}
// add a new tab
void MainWindow::addTab(Fm::FilePath path, ViewFrame* viewFrame) {
TabPage* newPage = new TabPage(this);
addTabWithPage(newPage, viewFrame, path);
}
void MainWindow::addTab(Fm::FilePath path) {
if(splitView_ && static_cast<Application*>(qApp)->openingLastTabs()) {
int N = static_cast<Application*>(qApp)->settings().splitViewTabsNum();
if(N > 0) {
// Divide tabs between the first and second view frames appropriately.
// NOTE: It is assumed that reoprning of last tabs is started when the split view has
// two frames, each with a single tab created in the c-tor -- as is the case with our
// calling codes -- otherwise, the splitting index might not be respected.
if(splitTabsNum_ == -1) {
splitTabsNum_ = N - 1;
}
ViewFrame* firstFrame = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(0));
ViewFrame* secondFrame = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(1));
if(!firstFrame || !secondFrame) { // unlikely but logical
static_cast<Application*>(qApp)->settings().setSplitViewTabsNum(0);
splitTabsNum_ = -1;
addTab(path, activeViewFrame_);
}
else if(splitTabsNum_ > 0) {
--splitTabsNum_;
addTab(path, firstFrame);
}
else {
addTab(path, secondFrame);
// On reaching the single tab of the second frame, remove it after adding a tab.
if(splitTabsNum_ == 0 && secondFrame->getStackedWidget()->count() == 2) {
closeTab(0, secondFrame);
}
splitTabsNum_ = -2; // will not change again for the current window
}
return;
}
}
// add the tab to the active view frame
addTab(path, activeViewFrame_);
}
void MainWindow::toggleMenuBar(bool /*checked*/) {
Settings& settings = static_cast<Application*>(qApp)->settings();
bool showMenuBar = !settings.showMenuBar();
if(!showMenuBar) {
if(QMessageBox::Cancel == QMessageBox::warning(this,
tr("Hide menu bar"),
tr("This will hide the menu bar completely, use Ctrl+M to show it again."),
QMessageBox::Ok | QMessageBox::Cancel)) {
ui.actionMenu_bar->setChecked(true);
return;
}
}
ui.menubar->setVisible(showMenuBar);
ui.actionMenu_bar->setChecked(showMenuBar);
menuSep_->setVisible(!showMenuBar);
ui.actionMenu->setVisible(!showMenuBar);
settings.setShowMenuBar(showMenuBar);
}
void MainWindow::updateRecenMenu() {
Settings& settings = static_cast<Application*>(qApp)->settings();
int recentNumber = settings.getRecentFilesNumber();
auto actions = ui.menuRecentFiles->actions();
if(actions.size() < recentNumber + 2) { // there is a separator and a clear action
return; // not really needed because we guarantee that it doesn't happen
}
auto recentFiles = settings.getRecentFiles();
int recentSize = recentFiles.size();
QFontMetrics metrics(ui.menuRecentFiles->font());
int w = 150 * metrics.horizontalAdvance(QLatin1Char(' ')); // for eliding long texts
for(int i = 0; i < recentNumber; ++i) {
if(i < recentSize) {
actions.at(i)->setText(metrics.elidedText(recentFiles.value(i).replace(QLatin1Char('&'), QLatin1String("&&")).replace(QLatin1Char('\t'), QLatin1Char(' ')), Qt::ElideMiddle, w));
QIcon icon;
auto mimeType = Fm::MimeType::guessFromFileName(recentFiles.at(i).toLocal8Bit().constData());
if(!mimeType->isUnknownType()) {
if(auto icn = mimeType->icon()) {
icon = icn->qicon();
}
}
actions.at(i)->setIcon(icon);
actions.at(i)->setData(recentFiles.at(i));
actions.at(i)->setVisible(true);
}
else {
actions.at(i)->setText(QString());
actions.at(i)->setIcon(QIcon());
actions.at(i)->setData(QVariant());
actions.at(i)->setVisible(false);
}
}
ui.actionClearRecent->setEnabled(recentSize != 0);
}
void MainWindow::clearRecentMenu() {
Settings& settings = static_cast<Application*>(qApp)->settings();
settings.clearRecentFiles();
updateRecenMenu();
}
void MainWindow::lanunchRecentFile() {
if(QAction *action = qobject_cast<QAction*>(QObject::sender())) {
Settings& settings = static_cast<Application*>(qApp)->settings();
auto pathStr = action->data().toString();
settings.addRecentFile(pathStr);
auto pathArray = pathStr.toLocal8Bit();
auto path = Fm::FilePath::fromLocalPath(pathArray.constData());
Fm::FilePathList pathList;
pathList.push_back(std::move(path));
fileLauncher_.launchPaths(nullptr, pathList);
}
}
void MainWindow::onPathEntryReturnPressed() {
Fm::PathEdit* pathEntry = pathEntry_;
if(pathEntry == nullptr) {
pathEntry = static_cast<Fm::PathEdit*>(sender());
}
if(pathEntry != nullptr) {
QString text = pathEntry->text();
QByteArray utext = text.toLocal8Bit();
chdir(Fm::FilePath::fromPathStr(utext.constData()));
}
}
void MainWindow::onPathBarChdir(const Fm::FilePath& dirPath) {
TabPage* page = nullptr;
ViewFrame* viewFrame = nullptr;
if(pathBar_ != nullptr) {
page = currentPage();
viewFrame = activeViewFrame_;
}
else {
Fm::PathBar* pathBar = static_cast<Fm::PathBar*>(sender());
viewFrame = qobject_cast<ViewFrame*>(pathBar->parentWidget());
if(viewFrame != nullptr) {
page = currentPage(viewFrame);
}
}
if(page && dirPath != page->path()) {
chdir(dirPath, viewFrame);
}
}
void MainWindow::onPathBarMiddleClickChdir(const Fm::FilePath& dirPath) {
ViewFrame* viewFrame = nullptr;
if(pathBar_ != nullptr) {
viewFrame = activeViewFrame_;
}
else {
Fm::PathBar* pathBar = static_cast<Fm::PathBar*>(sender());
viewFrame = qobject_cast<ViewFrame*>(pathBar->parentWidget());
}
if(viewFrame) {
addTab(dirPath, viewFrame);
}
}
void MainWindow::on_actionGoUp_triggered() {
QTimer::singleShot(0, this, [this] {
if(TabPage* page = currentPage()) {
page->up();
setTabIcon(page);
updateUIForCurrentPage();
}
});
}
void MainWindow::on_actionGoBack_triggered() {
QTimer::singleShot(0, this, [this] {
if(TabPage* page = currentPage()) {
page->backward();
setTabIcon(page);
updateUIForCurrentPage();
}
});
}
void MainWindow::on_actionGoForward_triggered() {
QTimer::singleShot(0, this, [this] {
if(TabPage* page = currentPage()) {
page->forward();
setTabIcon(page);
updateUIForCurrentPage();
}
});
}
void MainWindow::on_actionHome_triggered() {
chdir(Fm::FilePath::homeDir());
}
void MainWindow::on_actionReload_triggered() {
currentPage()->reload();
if(pathEntry_ != nullptr) {
pathEntry_->setText(currentPage()->pathName());
}
}
void MainWindow::on_actionConnectToServer_triggered() {
Application* app = static_cast<Application*>(qApp);
app->connectToServer();
}
void MainWindow::on_actionGo_triggered() {
onPathEntryReturnPressed();
}
void MainWindow::on_actionNewTab_triggered() {
auto path = currentPage()->path();
addTab(path);
}
void MainWindow::on_actionNewWin_triggered() {
auto path = currentPage()->path();
(new MainWindow(path))->show();
}
void MainWindow::on_actionNewFolder_triggered() {
if(TabPage* tabPage = currentPage()) {
auto dirPath = tabPage->folderView()->path();
if(dirPath) {
createFileOrFolder(CreateNewFolder, dirPath, nullptr, this);
}
}
}
void MainWindow::on_actionNewBlankFile_triggered() {
if(TabPage* tabPage = currentPage()) {
auto dirPath = tabPage->folderView()->path();
if(dirPath) {
createFileOrFolder(CreateNewTextFile, dirPath, nullptr, this);
}
}
}
void MainWindow::on_actionCloseTab_triggered() {
closeTab(activeViewFrame_->getTabBar()->currentIndex());
}
void MainWindow::on_actionCloseWindow_triggered() {
// FIXME: should we save state here?
close();
// the window will be deleted automatically on close
}
void MainWindow::on_actionFileProperties_triggered() {
TabPage* page = currentPage();
if(page) {
auto files = page->selectedFiles();
if(!files.empty()) {
Fm::FilePropsDialog::showForFiles(files);
}
}
}
void MainWindow::on_actionFolderProperties_triggered() {
TabPage* page = currentPage();
if(page) {
auto folder = page->folder();
if(folder) {
auto info = folder->info();
if(info) {
Fm::FilePropsDialog::showForFile(info);
}
}
}
}
void MainWindow::on_actionShowHidden_triggered(bool checked) {
currentPage()->setShowHidden(checked);
// visibility of hidden folders in directory tree is toggled by onTabPageSortFilterChanged()
}
void MainWindow::on_actionShowThumbnails_triggered(bool checked) {
QWidgetList windows = qApp->topLevelWidgets();
QWidgetList::iterator it;
for(it = windows.begin(); it != windows.end(); ++it) {
QWidget* window = *it;
if(window->inherits("PCManFM::MainWindow")) {
MainWindow* mainWindow = static_cast<MainWindow*>(window);
mainWindow->ui.actionShowThumbnails->setChecked(checked); // doesn't call this function
for(int i = 0; i < mainWindow->ui.viewSplitter->count(); ++i) {
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(mainWindow->ui.viewSplitter->widget(i))) {
int n = viewFrame->getStackedWidget()->count();
for(int j = 0; j < n; ++j) {
if(TabPage* page = static_cast<TabPage*>(viewFrame->getStackedWidget()->widget(j))) {
page->setShowThumbnails(checked);
}
}
}
}
}
}
// this setting is shared by Desktop too
static_cast<Application*>(qApp)->updateDesktopsFromSettings(false);
}
void MainWindow::on_actionByFileName_triggered(bool /*checked*/) {
currentPage()->sort(Fm::FolderModel::ColumnFileName, currentPage()->sortOrder());
}
void MainWindow::on_actionByMTime_triggered(bool /*checked*/) {
currentPage()->sort(Fm::FolderModel::ColumnFileMTime, currentPage()->sortOrder());
}
void MainWindow::on_actionByCrTime_triggered(bool /*checked*/) {
currentPage()->sort(Fm::FolderModel::ColumnFileCrTime, currentPage()->sortOrder());
}
void MainWindow::on_actionByDTime_triggered(bool /*checked*/) {
currentPage()->sort(Fm::FolderModel::ColumnFileDTime, currentPage()->sortOrder());
}
void MainWindow::on_actionByOwner_triggered(bool /*checked*/) {
currentPage()->sort(Fm::FolderModel::ColumnFileOwner, currentPage()->sortOrder());
}
void MainWindow::on_actionByGroup_triggered(bool /*checked*/) {
currentPage()->sort(Fm::FolderModel::ColumnFileGroup, currentPage()->sortOrder());
}
void MainWindow::on_actionByFileSize_triggered(bool /*checked*/) {
currentPage()->sort(Fm::FolderModel::ColumnFileSize, currentPage()->sortOrder());
}
void MainWindow::on_actionByFileType_triggered(bool /*checked*/) {
currentPage()->sort(Fm::FolderModel::ColumnFileType, currentPage()->sortOrder());
}
void MainWindow::on_actionAscending_triggered(bool /*checked*/) {
currentPage()->sort(currentPage()->sortColumn(), Qt::AscendingOrder);
}
void MainWindow::on_actionDescending_triggered(bool /*checked*/) {
currentPage()->sort(currentPage()->sortColumn(), Qt::DescendingOrder);
}
void MainWindow::on_actionCaseSensitive_triggered(bool checked) {
currentPage()->setSortCaseSensitive(checked);
}
void MainWindow::on_actionFolderFirst_triggered(bool checked) {
currentPage()->setSortFolderFirst(checked);
}
void MainWindow::on_actionHiddenLast_triggered(bool checked) {
currentPage()->setSortHiddenLast(checked);
}
void MainWindow::on_actionPreserveView_triggered(bool checked) {
TabPage* page = currentPage();
page->setCustomizedView(checked);
if(checked) {
ui.actionPreserveViewRecursive->setChecked(false);
}
ui.actionGoToCustomizedViewSource->setVisible(page->hasInheritedCustomizedView());
setTabIcon(page);
}
void MainWindow::on_actionPreserveViewRecursive_triggered(bool checked) {
TabPage* page = currentPage();
page->setCustomizedView(checked, true);
if(checked) {
ui.actionPreserveView->setChecked(false);
}
ui.actionGoToCustomizedViewSource->setVisible(page->hasInheritedCustomizedView());
setTabIcon(page);
}
void MainWindow::on_actionGoToCustomizedViewSource_triggered() {
currentPage()->goToCustomizedViewSource();
updateUIForCurrentPage();
}
void MainWindow::on_actionFilter_triggered(bool checked) {
static_cast<Application*>(qApp)->settings().setShowFilter(checked);
// show/hide filter-bars and disable/enable their transience for all tabs
// (of all view frames) in all windows because this is a global setting
QWidgetList windows = qApp->topLevelWidgets();
QWidgetList::iterator it;
for(it = windows.begin(); it != windows.end(); ++it) {
QWidget* window = *it;
if(window->inherits("PCManFM::MainWindow")) {
MainWindow* mainWindow = static_cast<MainWindow*>(window);
mainWindow->ui.actionFilter->setChecked(checked); // doesn't call this function
for(int i = 0; i < mainWindow->ui.viewSplitter->count(); ++i) {
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(mainWindow->ui.viewSplitter->widget(i))) {
int n = viewFrame->getStackedWidget()->count();
for(int j = 0; j < n; ++j) {
if(TabPage* page = static_cast<TabPage*>(viewFrame->getStackedWidget()->widget(j))) {
page->transientFilterBar(!checked);
}
}
}
}
}
}
}
void MainWindow::on_actionUnfilter_triggered() {
// clear filters for all tabs (of all view frames)
for(int i = 0; i < ui.viewSplitter->count(); ++i) {
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(i))) {
int n = viewFrame->getStackedWidget()->count();
for(int j = 0; j < n; ++j) {
if(TabPage* page = static_cast<TabPage*>(viewFrame->getStackedWidget()->widget(j))) {
page->clearFilter();
}
}
}
}
}
void MainWindow::on_actionShowFilter_triggered() {
if(TabPage* page = currentPage()) {
page->showFilterBar();
}
}
void MainWindow::on_actionLocationBar_triggered(bool checked) {
if(checked) {
// show current path in a location bar entry
createPathBar(false);
static_cast<Application*>(qApp)->settings().setPathBarButtons(false);
}
}
void MainWindow::on_actionPathButtons_triggered(bool checked) {
if(checked) {
// show current path as buttons
createPathBar(true);
static_cast<Application*>(qApp)->settings().setPathBarButtons(true);
}
}
void MainWindow::on_actionComputer_triggered() {
chdir(Fm::FilePath::fromUri("computer:///"));
}
void MainWindow::on_actionApplications_triggered() {
chdir(Fm::FilePath::fromUri("menu://applications/"));
}
void MainWindow::on_actionTrash_triggered() {
chdir(Fm::FilePath::fromUri("trash:///"));
}
void MainWindow::on_actionNetwork_triggered() {
chdir(Fm::FilePath::fromUri("network:///"));
}
void MainWindow::on_actionDesktop_triggered() {
auto desktop = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation).toLocal8Bit();
chdir(Fm::FilePath::fromLocalPath(desktop.constData()));
}
void MainWindow::on_actionAddToBookmarks_triggered() {
TabPage* page = currentPage();
if(page) {
auto cwd = page->path();
if(cwd) {
QString bookmarkName;
auto parent = cwd.parent();
if(!parent.isValid() || parent == cwd) { // a root path
bookmarkName = QString::fromUtf8(cwd.displayName().get());
auto parts = bookmarkName.split(QLatin1Char('/'), Qt::SkipEmptyParts);
if(!parts.isEmpty()) {
bookmarkName = parts.last();
}
}
else {
bookmarkName = QString::fromUtf8(cwd.baseName().get());
}
bookmarks_->insert(cwd, bookmarkName, -1);
}
}
}
void MainWindow::on_actionEditBookmarks_triggered() {
Application* app = static_cast<Application*>(qApp);
app->editBookmarks();
}
void MainWindow::on_actionAbout_triggered() {
// the about dialog
class AboutDialog : public QDialog {
public:
explicit AboutDialog(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) : QDialog(parent, f) {
ui.setupUi(this);
ui.version->setText(tr("Version: %1").arg(QStringLiteral(PCMANFM_QT_VERSION)));
}
private:
Ui::AboutDialog ui;
};
AboutDialog dialog(this);
dialog.exec();
}
void MainWindow::on_actionHiddenShortcuts_triggered() {
class HiddenShortcutsDialog : public QDialog {
public:
explicit HiddenShortcutsDialog(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()) : QDialog(parent, f) {
ui.setupUi(this);
ui.treeWidget->setRootIsDecorated(false);
ui.treeWidget->header()->setSectionResizeMode(QHeaderView::Stretch);
ui.treeWidget->header()->setSectionsClickable(true);
ui.treeWidget->sortByColumn(0, Qt::AscendingOrder);
ui.treeWidget->setSortingEnabled(true);
}
private:
Ui::HiddenShortcutsDialog ui;
};
HiddenShortcutsDialog dialog(this);
dialog.exec();
}
void MainWindow::on_actionIconView_triggered() {
TabPage* page = currentPage();
page->setViewMode(Fm::FolderView::IconMode);
setTabIcon(page);
}
void MainWindow::on_actionCompactView_triggered() {
TabPage* page = currentPage();
page->setViewMode(Fm::FolderView::CompactMode);
setTabIcon(page);
}
void MainWindow::on_actionDetailedList_triggered() {
TabPage* page = currentPage();
page->setViewMode(Fm::FolderView::DetailedListMode);
setTabIcon(page);
}
void MainWindow::on_actionThumbnailView_triggered() {
TabPage* page = currentPage();
page->setViewMode(Fm::FolderView::ThumbnailMode);
setTabIcon(page);
}
void MainWindow::onTabBarCloseRequested(int index) {
TabBar* tabBar = static_cast<TabBar*>(sender());
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(tabBar->parentWidget())) {
closeTab(index, viewFrame);
}
}
void MainWindow::onResetFocus() {
if(TabPage* page = currentPage()) {
page->folderView()->childView()->setFocus();
}
}
void MainWindow::onTabBarTabMoved(int from, int to) {
TabBar* tabBar = static_cast<TabBar*>(sender());
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(tabBar->parentWidget())) {
// a tab in the tab bar is moved by the user, so we have to move the
// corredponding tab page in the stacked widget to the new position, too.
QWidget* page = viewFrame->getStackedWidget()->widget(from);
if(page) {
// we're not going to delete the tab page, so here we block signals
// to avoid calling the slot onStackedWidgetWidgetRemoved() before
// removing the page. Otherwise the page widget will be destroyed.
viewFrame->getStackedWidget()->blockSignals(true);
viewFrame->getStackedWidget()->removeWidget(page);
viewFrame->getStackedWidget()->insertWidget(to, page); // insert the page to the new position
viewFrame->getStackedWidget()->blockSignals(false); // unblock signals
viewFrame->getStackedWidget()->setCurrentWidget(page);
}
}
}
void MainWindow::onFolderUnmounted() {
TabPage* tabPage = static_cast<TabPage*>(sender());
if(ViewFrame* viewFrame = viewFrameForTabPage(tabPage)) {
const QList<MountOperation*> ops = ui.sidePane->findChildren<MountOperation*>();
if(ops.isEmpty()) { // unmounting is done somewhere else
Settings& settings = static_cast<Application*>(qApp)->settings();
if(settings.closeOnUnmount()) {
viewFrame->getStackedWidget()->removeWidget(tabPage);
// NOTE: Since Fm::Folder queues a folder reload after emitting the unmount signal,
// pending events may be waiting to be delivered at this very moment. Therefore,
// if the tab page is deleted immediately, a crash will be imminent for various reasons.
tabPage->deleteLater();
}
else {
tabPage->chdir(Fm::FilePath::homeDir(), true);
setTabIcon(tabPage);
updateUIForCurrentPage();
}
}
else { // wait for all (un-)mount operations to be finished (otherwise, they might be cancelled)
for(const MountOperation* op : ops) {
connect(op, &QObject::destroyed, tabPage, [this, tabPage, viewFrame] {
if(ui.sidePane->findChildren<MountOperation*>().isEmpty()) {
Settings& settings = static_cast<Application*>(qApp)->settings();
if(settings.closeOnUnmount()) {
viewFrame->getStackedWidget()->removeWidget(tabPage);
tabPage->deleteLater();
}
else {
tabPage->chdir(Fm::FilePath::homeDir(), true);
setTabIcon(tabPage);
updateUIForCurrentPage();
}
}
});
}
}
}
}
void MainWindow::closeTab(int index, ViewFrame* viewFrame) {
QWidget* page = viewFrame->getStackedWidget()->widget(index);
if(page) {
viewFrame->getStackedWidget()->removeWidget(page); // this does not delete the page widget
delete page;
// NOTE: we do not remove the tab here.
// it'll be done in onStackedWidgetWidgetRemoved()
}
}
void MainWindow::resizeEvent(QResizeEvent* event) {
QMainWindow::resizeEvent(event);
Settings& settings = static_cast<Application*>(qApp)->settings();
if(settings.rememberWindowSize()) {
settings.setLastWindowMaximized(isMaximized());
if(!isMaximized()) {
settings.setLastWindowWidth(width());
settings.setLastWindowHeight(height());
}
}
}
void MainWindow::closeEvent(QCloseEvent* event) {
if(lastActive_ == this) {
lastActive_ = nullptr;
}
QWidget::closeEvent(event);
Settings& settings = static_cast<Application*>(qApp)->settings();
if(settings.rememberWindowSize()) {
settings.setLastWindowMaximized(isMaximized());
if(!isMaximized()) {
settings.setLastWindowWidth(width());
settings.setLastWindowHeight(height());
}
}
// remember last tab paths only if this is the last window
QStringList tabPaths;
int splitNum = 0;
if(lastActive_ == nullptr && settings.reopenLastTabs()) {
for(int i = 0; i < ui.viewSplitter->count(); ++i) {
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(i))) {
int n = viewFrame->getStackedWidget()->count();
for(int j = 0; j < n; ++j) {
if(TabPage* page = static_cast<TabPage*>(viewFrame->getStackedWidget()->widget(j))) {
tabPaths.append(QString::fromUtf8(page->path().toString().get()));
}
}
}
if(i == 0 && ui.viewSplitter->count() > 1) {
splitNum = tabPaths.size();
}
}
}
settings.setTabPaths(tabPaths);
settings.setSplitViewTabsNum(splitNum);
}
void MainWindow::onTabBarCurrentChanged(int index) {
TabBar* tabBar = static_cast<TabBar*>(sender());
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(tabBar->parentWidget())) {
viewFrame->getStackedWidget()->setCurrentIndex(index);
if(viewFrame == activeViewFrame_) {
updateUIForCurrentPage();
}
else {
if(TabPage* page = currentPage(viewFrame)) {
if(Fm::PathBar* pathBar = qobject_cast<Fm::PathBar*>(viewFrame->getTopBar())) {
pathBar->setPath(page->path());
}
else if(Fm::PathEdit* pathEntry = qobject_cast<Fm::PathEdit*>(viewFrame->getTopBar())) {
pathEntry->setText(page->pathName());
}
}
}
}
}
void MainWindow::updateStatusBarForCurrentPage() {
TabPage* tabPage = currentPage();
QString text = tabPage->statusText(TabPage::StatusTextSelectedFiles);
if(text.isEmpty()) {
text = tabPage->statusText(TabPage::StatusTextNormal);
}
ui.statusbar->showMessage(text);
text = tabPage->statusText(TabPage::StatusTextFSInfo);
fsInfoLabel_->setText(text);
fsInfoLabel_->setVisible(!text.isEmpty());
}
void MainWindow::updateViewMenuForCurrentPage() {
if(updatingViewMenu_) { // prevent recursive calls
return;
}
updatingViewMenu_ = true;
TabPage* tabPage = currentPage();
if(tabPage) {
// update menus. FIXME: should we move this to another method?
ui.actionShowHidden->setChecked(tabPage->showHidden());
ui.actionPreserveView->setChecked(tabPage->hasCustomizedView() && !tabPage->hasRecursiveCustomizedView());
ui.actionPreserveViewRecursive->setChecked(tabPage->hasRecursiveCustomizedView());
ui.actionGoToCustomizedViewSource->setVisible(tabPage->hasInheritedCustomizedView());
// view mode
QAction* modeAction = nullptr;
switch(tabPage->viewMode()) {
case Fm::FolderView::IconMode:
modeAction = ui.actionIconView;
break;
case Fm::FolderView::CompactMode:
modeAction = ui.actionCompactView;
break;
case Fm::FolderView::DetailedListMode:
modeAction = ui.actionDetailedList;
break;
case Fm::FolderView::ThumbnailMode:
modeAction = ui.actionThumbnailView;
break;
}
Q_ASSERT(modeAction != nullptr);
modeAction->setChecked(true);
// sort menu
// WARNING: Since libfm-qt may have a column that is not handled here,
// we should prevent a crash by setting all actions to null first and
// check their action group later.
QAction* sortActions[Fm::FolderModel::NumOfColumns];
for(int i = 0; i < Fm::FolderModel::NumOfColumns; ++i) {
sortActions[i] = nullptr;
}
sortActions[Fm::FolderModel::ColumnFileName] = ui.actionByFileName;
sortActions[Fm::FolderModel::ColumnFileMTime] = ui.actionByMTime;
sortActions[Fm::FolderModel::ColumnFileCrTime] = ui.actionByCrTime;
sortActions[Fm::FolderModel::ColumnFileDTime] = ui.actionByDTime;
sortActions[Fm::FolderModel::ColumnFileSize] = ui.actionByFileSize;
sortActions[Fm::FolderModel::ColumnFileType] = ui.actionByFileType;
sortActions[Fm::FolderModel::ColumnFileOwner] = ui.actionByOwner;
sortActions[Fm::FolderModel::ColumnFileGroup] = ui.actionByGroup;
if (auto group = ui.actionByFileName->actionGroup()) {
const auto actions = group->actions();
auto action = sortActions[tabPage->sortColumn()];
if(actions.contains(action)) {
action->setChecked(true);
}
else {
for(auto a : actions) {
a->setChecked(false);
}
}
}
if(auto path = tabPage->path()) {
ui.actionByDTime->setVisible(strcmp(path.toString().get(), "trash:///") == 0);
}
if(tabPage->sortOrder() == Qt::AscendingOrder) {
ui.actionAscending->setChecked(true);
}
else {
ui.actionDescending->setChecked(true);
}
ui.actionCaseSensitive->setChecked(tabPage->sortCaseSensitive());
ui.actionFolderFirst->setChecked(tabPage->sortFolderFirst());
ui.actionHiddenLast->setChecked(tabPage->sortHiddenLast());
}
updatingViewMenu_ = false;
}
// Update the enabled state of File and Edit actions for selected files
void MainWindow::updateSelectedActions() {
bool hasAccessible = false;
bool hasDeletable = false;
int renamable = 0;
if(TabPage* page = currentPage()) {
auto files = page->selectedFiles();
for(auto& file: files) {
if(file->isAccessible()) {
hasAccessible = true;
}
if(file->isDeletable()) {
hasDeletable = true;
}
if(file->canSetName()) {
++renamable;
}
if (hasAccessible && hasDeletable && renamable > 1) {
break;
}
}
ui.actionFileProperties->setEnabled(files.size() > 0);
ui.actionCopyFullPath->setEnabled(files.size() == 1);
}
ui.actionCopy->setEnabled(hasAccessible);
ui.actionCut->setEnabled(hasDeletable);
ui.actionDelete->setEnabled(hasDeletable);
ui.actionRename->setEnabled(renamable > 0);
ui.actionBulkRename->setEnabled(renamable > 1);
}
void MainWindow::updateUIForCurrentPage(bool setFocus) {
TabPage* tabPage = currentPage();
if(tabPage) {
setWindowTitle(tabPage->title());
if(splitView_) {
if(Fm::PathBar* pathBar = qobject_cast<Fm::PathBar*>(activeViewFrame_->getTopBar())) {
pathBar->setPath(tabPage->path());
}
else if(Fm::PathEdit* pathEntry = qobject_cast<Fm::PathEdit*>(activeViewFrame_->getTopBar())) {
pathEntry->setText(tabPage->pathName());
}
}
else {
if(pathEntry_ != nullptr) {
pathEntry_->setText(tabPage->pathName());
}
else if(pathBar_ != nullptr) {
pathBar_->setPath(tabPage->path());
}
}
ui.statusbar->showMessage(tabPage->statusText());
fsInfoLabel_->setText(tabPage->statusText(TabPage::StatusTextFSInfo));
if(setFocus) {
tabPage->folderView()->childView()->setFocus();
}
// update side pane
ui.sidePane->setCurrentPath(tabPage->path());
ui.sidePane->setShowHidden(tabPage->showHidden());
// update back/forward/up toolbar buttons
ui.actionGoUp->setEnabled(tabPage->canUp());
ui.actionGoBack->setEnabled(tabPage->canBackward());
ui.actionGoForward->setEnabled(tabPage->canForward());
ui.actionOpenAsAdmin->setEnabled(tabPage->path() && tabPage->path().isNative());
updateViewMenuForCurrentPage();
updateStatusBarForCurrentPage();
}
// also update the enabled state of File and Edit actions
updateSelectedActions();
bool isWritable(false);
bool isNative(false);
if(tabPage && tabPage->folder()) {
if(auto info = tabPage->folder()->info()) {
isWritable = info->isWritable();
isNative = info->isNative();
}
}
ui.actionPaste->setEnabled(isWritable);
ui.menuCreateNew->setEnabled(isWritable);
// disable creation shortcuts too
ui.actionNewFolder->setEnabled(isWritable);
ui.actionNewBlankFile->setEnabled(isWritable);
ui.actionCreateLauncher->setEnabled(isWritable && isNative);
}
void MainWindow::onStackedWidgetWidgetRemoved(int index) {
QStackedWidget* sw = static_cast<QStackedWidget*>(sender());
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(sw->parentWidget())) {
// qDebug("onStackedWidgetWidgetRemoved: %d", index);
// need to remove associated tab from tabBar
viewFrame->getTabBar()->removeTab(index);
if(viewFrame->getTabBar()->count() == 0) { // this is the last one
if(!splitView_) {
deleteLater(); // destroy the whole window
// qDebug("delete window");
}
else {
// if we are in the split mode and the last tab of a view frame is closed,
// remove that view frame and go to the simple mode
for(int i = 0; i < ui.viewSplitter->count(); ++i) {
// first find and activate the next view frame
if(ViewFrame* thisViewFrame = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(i))) {
if(thisViewFrame == viewFrame) {
int n = i < ui.viewSplitter->count() - 1 ? i + 1 : 0;
if(ViewFrame* nextViewFrame = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(n))) {
if(activeViewFrame_ != nextViewFrame) {
activeViewFrame_ = nextViewFrame;
updateUIForCurrentPage();
// if the window isn't active, eventFilter() won't be called,
// so we should revert to the main palette here
if(activeViewFrame_->palette().color(QPalette::Base)
!= qApp->palette().color(QPalette::Base)) {
activeViewFrame_->setPalette(qApp->palette());
}
}
break;
}
}
}
}
ui.actionSplitView->setChecked(false);
on_actionSplitView_triggered(false);
}
}
}
}
void MainWindow::onTabPageTitleChanged() {
TabPage* tabPage = static_cast<TabPage*>(sender());
if(ViewFrame* viewFrame = viewFrameForTabPage(tabPage)) {
int index = viewFrame->getStackedWidget()->indexOf(tabPage);
if(index >= 0) {
QString tabText = tabPage->title();
// remove newline and distinguish ampersand from mnemonic
tabText.replace(QLatin1Char('\n'), QLatin1Char(' '))
.replace(QLatin1Char('&'), QLatin1String("&&"));
viewFrame->getTabBar()->setTabText(index, tabText);
}
if(viewFrame == activeViewFrame_) {
if(tabPage == currentPage()) {
setWindowTitle(tabPage->title());
// Since TabPage::titleChanged is emitted on changing directory,
// the enabled state of some actions should be updated here
bool isNative(tabPage->path() && tabPage->path().isNative());
ui.actionOpenAsAdmin->setEnabled(isNative);
bool isWritable(false);
if(tabPage && tabPage->folder()) {
if(auto info = tabPage->folder()->info()) {
isWritable = info->isWritable();
}
}
ui.actionPaste->setEnabled(isWritable);
ui.menuCreateNew->setEnabled(isWritable);
ui.actionNewFolder->setEnabled(isWritable);
ui.actionNewBlankFile->setEnabled(isWritable);
ui.actionCreateLauncher->setEnabled(isWritable && isNative);
}
}
}
}
void MainWindow::onTabPageStatusChanged(int type, QString statusText) {
TabPage* tabPage = static_cast<TabPage*>(sender());
if(tabPage == currentPage()) {
switch(type) {
case TabPage::StatusTextNormal:
case TabPage::StatusTextSelectedFiles: {
// although the status text may change very frequently,
// the text of PCManFM::StatusBar is updated with a delay
QString text = tabPage->statusText(TabPage::StatusTextSelectedFiles);
if(text.isEmpty()) {
ui.statusbar->showMessage(tabPage->statusText(TabPage::StatusTextNormal));
}
else {
ui.statusbar->showMessage(text);
}
break;
}
case TabPage::StatusTextFSInfo:
fsInfoLabel_->setText(tabPage->statusText(TabPage::StatusTextFSInfo));
fsInfoLabel_->setVisible(!statusText.isEmpty());
break;
}
}
// Since TabPage::statusChanged is always emitted after View::selChanged,
// there is no need to connect a separate slot to the latter signal
updateSelectedActions();
}
void MainWindow::onTabPageSortFilterChanged() { // NOTE: This may be called from context menu too.
TabPage* tabPage = static_cast<TabPage*>(sender());
if(tabPage == currentPage()) {
updateViewMenuForCurrentPage();
ui.sidePane->setShowHidden(tabPage->showHidden());
if(!tabPage->hasCustomizedView() && !tabPage->hasInheritedCustomizedView()) { // remember sort settings globally
Settings& settings = static_cast<Application*>(qApp)->settings();
settings.setSortColumn(static_cast<Fm::FolderModel::ColumnId>(tabPage->sortColumn()));
settings.setSortOrder(tabPage->sortOrder());
settings.setSortFolderFirst(tabPage->sortFolderFirst());
settings.setSortHiddenLast(tabPage->sortHiddenLast());
settings.setSortCaseSensitive(tabPage->sortCaseSensitive());
settings.setShowHidden(tabPage->showHidden());
}
}
}
void MainWindow::onSidePaneChdirRequested(int type, const Fm::FilePath &path) {
// FIXME: use enum for type value or change it to button.
if(type == 0) { // left button (default)
chdir(path);
}
else if(type == 1) { // middle button
addTab(path);
}
else if(type == 2) { // new window
(new MainWindow(path))->show();
}
}
void MainWindow::onSidePaneOpenFolderInNewWindowRequested(const Fm::FilePath &path) {
(new MainWindow(path))->show();
}
void MainWindow::onSidePaneOpenFolderInNewTabRequested(const Fm::FilePath &path) {
addTab(path);
}
void MainWindow::onSidePaneOpenFolderInTerminalRequested(const Fm::FilePath &path) {
Application* app = static_cast<Application*>(qApp);
app->openFolderInTerminal(path);
}
void MainWindow::onSidePaneCreateNewFolderRequested(const Fm::FilePath &path) {
createFileOrFolder(CreateNewFolder, path, nullptr, this);
}
void MainWindow::onSidePaneModeChanged(Fm::SidePane::Mode mode) {
static_cast<Application*>(qApp)->settings().setSidePaneMode(mode);
}
void MainWindow::onSettingHiddenPlace(const QString& str, bool hide) {
static_cast<Application*>(qApp)->settings().setHiddenPlace(str, hide);
}
void MainWindow::on_actionSidePane_triggered(bool checked) {
Application* app = static_cast<Application*>(qApp);
app->settings().showSidePane(checked);
ui.sidePane->setVisible(checked);
}
void MainWindow::onSplitterMoved(int pos, int /*index*/) {
Application* app = static_cast<Application*>(qApp);
app->settings().setSplitterPos(pos);
}
void MainWindow::loadBookmarksMenu() {
QAction* before = ui.actionAddToBookmarks;
for(auto& item: bookmarks_->items()) {
BookmarkAction* action = new BookmarkAction(item, ui.menu_Bookmarks);
connect(action, &QAction::triggered, this, &MainWindow::onBookmarkActionTriggered);
ui.menu_Bookmarks->insertAction(before, action);
}
ui.menu_Bookmarks->insertSeparator(before);
}
void MainWindow::onBookmarksChanged() {
// delete existing items
QList<QAction*> actions = ui.menu_Bookmarks->actions();
QList<QAction*>::const_iterator it = actions.constBegin();
QList<QAction*>::const_iterator last_it = actions.constEnd() - 2;
while(it != last_it) {
QAction* action = *it;
++it;
ui.menu_Bookmarks->removeAction(action);
}
loadBookmarksMenu();
}
void MainWindow::onBookmarkActionTriggered() {
BookmarkAction* action = static_cast<BookmarkAction*>(sender());
auto path = action->path();
if(path) {
Application* app = static_cast<Application*>(qApp);
Settings& settings = app->settings();
switch(settings.bookmarkOpenMethod()) {
case OpenInCurrentTab: /* current tab */
default:
chdir(path);
break;
case OpenInNewTab: /* new tab */
addTab(path);
break;
case OpenInNewWindow: /* new window */
(new MainWindow(path))->show();
break;
}
}
}
void MainWindow::on_actionCopy_triggered() {
TabPage* page = currentPage();
auto paths = page->selectedFilePaths();
copyFilesToClipboard(paths);
}
void MainWindow::on_actionCut_triggered() {
TabPage* page = currentPage();
auto paths = page->selectedFilePaths();
cutFilesToClipboard(paths);
}
void MainWindow::on_actionPaste_triggered() {
pasteFilesFromClipboard(currentPage()->path());
}
void MainWindow::on_actionDelete_triggered() {
Application* app = static_cast<Application*>(qApp);
Settings& settings = app->settings();
TabPage* page = currentPage();
auto paths = page->selectedFilePaths();
auto path_it = paths.cbegin();
bool trashed(path_it != paths.cend() && (*path_it).hasUriScheme("trash"));
bool shiftPressed = (qApp->keyboardModifiers() & Qt::ShiftModifier ? true : false);
if(settings.useTrash() && !shiftPressed
// trashed files should be deleted
&& !trashed) {
FileOperation::trashFiles(paths, settings.confirmTrash(), this);
}
else {
FileOperation::deleteFiles(paths, settings.confirmDelete(), this);
}
}
void MainWindow::on_actionRename_triggered() {
// do inline renaming if only one item is selected,
// otherwise use the renaming dialog
TabPage* page = currentPage();
auto files = page->selectedFiles();
if(files.size() == 1) {
QAbstractItemView* view = page->folderView()->childView();
QModelIndexList selIndexes = view->selectionModel()->selectedIndexes();
if(selIndexes.size() > 1) { // in the detailed list mode, only the first index is editable
view->setCurrentIndex(selIndexes.at(0));
}
QModelIndex cur = view->currentIndex();
if (cur.isValid()) {
view->scrollTo(cur);
view->edit(cur);
return;
}
}
if(!files.empty()) {
for(auto& file: files) {
if(!Fm::renameFile(file, this)) {
break;
}
}
}
}
void MainWindow::on_actionBulkRename_triggered() {
BulkRenamer(currentPage()->selectedFiles(), this);
}
void MainWindow::on_actionSelectAll_triggered() {
currentPage()->selectAll();
}
void MainWindow::on_actionDeselectAll_triggered() {
currentPage()->deselectAll();
}
void MainWindow::on_actionInvertSelection_triggered() {
currentPage()->invertSelection();
}
void MainWindow::on_actionPreferences_triggered() {
Application* app = reinterpret_cast<Application*>(qApp);
app->preferences(QString());
}
// change some icons according to layout direction
void MainWindow::setRTLIcons(bool isRTL) {
QIcon nxtIcn = QIcon::fromTheme(QStringLiteral("go-next"));
QIcon prevIcn = QIcon::fromTheme(QStringLiteral("go-previous"));
if(isRTL) {
ui.actionGoBack->setIcon(nxtIcn);
ui.actionCloseLeft->setIcon(nxtIcn);
ui.actionGoForward->setIcon(prevIcn);
ui.actionCloseRight->setIcon(prevIcn);
}
else {
ui.actionGoBack->setIcon(prevIcn);
ui.actionCloseLeft->setIcon(prevIcn);
ui.actionGoForward->setIcon(nxtIcn);
ui.actionCloseRight->setIcon(nxtIcn);
}
}
bool MainWindow::event(QEvent* event) {
switch(event->type()) {
case QEvent::WindowActivate:
lastActive_ = this;
default:
break;
}
return QMainWindow::event(event);
}
void MainWindow::changeEvent(QEvent* event) {
switch(event->type()) {
case QEvent::LayoutDirectionChange:
setRTLIcons(QApplication::layoutDirection() == Qt::RightToLeft);
break;
default:
break;
}
QWidget::changeEvent(event);
}
void MainWindow::onBackForwardContextMenu(QPoint pos) {
// show a popup menu for browsing history here.
QToolButton* btn = static_cast<QToolButton*>(sender());
TabPage* page = currentPage();
Fm::BrowseHistory& history = page->browseHistory();
int current = history.currentIndex();
QMenu menu(this);
for(size_t i = 0; i < history.size(); ++i) {
const BrowseHistoryItem& item = history.at(i);
auto path = item.path();
auto name = path.displayName();
QAction* action = menu.addAction(QString::fromUtf8(name.get()));
if(i == static_cast<size_t>(current)) {
// make the current path bold and checked
action->setCheckable(true);
action->setChecked(true);
QFont font = menu.font();
font.setBold(true);
action->setFont(font);
}
}
QAction* selectedAction = menu.exec(btn->mapToGlobal(pos));
if(selectedAction) {
int index = menu.actions().indexOf(selectedAction);
page->jumpToHistory(index);
setTabIcon(page);
updateUIForCurrentPage();
}
}
void MainWindow::onTabBarClicked(int /*index*/) {
TabBar* tabBar = static_cast<TabBar*>(sender());
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(tabBar->parentWidget())) {
// focus the view on clicking the tab bar
if(TabPage* page = currentPage(viewFrame)) {
page->folderView()->childView()->setFocus();
}
}
}
void MainWindow::tabContextMenu(const QPoint& pos) {
TabBar* tabBar = static_cast<TabBar*>(sender());
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(tabBar->parentWidget())) {
int tabNum = viewFrame->getTabBar()->count();
if(tabNum < 1) {
return;
}
rightClickIndex_ = viewFrame->getTabBar()->tabAt(pos);
if(rightClickIndex_ < 0) {
return;
}
QMenu menu(this);
// tab closing actions
if(rightClickIndex_ > 0) {
menu.addAction(ui.actionCloseLeft);
}
if(rightClickIndex_ < tabNum - 1) {
menu.addAction(ui.actionCloseRight);
if(rightClickIndex_ > 0) {
menu.addSeparator();
menu.addAction(ui.actionCloseOther);
}
}
// per-folder actions for the current tab
if(viewFrame->getTabBar()->currentIndex() == rightClickIndex_) {
menu.addSeparator();
QWidgetAction* labelAction = new QWidgetAction(&menu);
QLabel *label = new QLabel(QStringLiteral("<center><b>")
+ tr("Customized View Settings")
+ QStringLiteral("</b></center>"));
label->setMargin(5);
labelAction->setDefaultWidget(label);
menu.addAction(labelAction);
menu.addAction(ui.actionPreserveView);
menu.addAction(ui.actionPreserveViewRecursive);
menu.addSeparator();
menu.addAction(ui.actionGoToCustomizedViewSource);
menu.addAction(ui.actionCleanPerFolderConfig);
}
menu.exec(viewFrame->getTabBar()->mapToGlobal(pos));
}
}
void MainWindow::closeLeftTabs() {
while(rightClickIndex_ > 0) {
closeTab(rightClickIndex_ - 1);
--rightClickIndex_;
}
}
void MainWindow::closeRightTabs() {
if(rightClickIndex_ < 0) {
return;
}
while(rightClickIndex_ < activeViewFrame_->getTabBar()->count() - 1) {
closeTab(rightClickIndex_ + 1);
}
}
void MainWindow::focusPathEntry() {
// use text entry for the path bar
if(splitView_) {
if(Fm::PathBar* pathBar = qobject_cast<Fm::PathBar*>(activeViewFrame_->getTopBar())) {
pathBar->openEditor();
}
else if(Fm::PathEdit* pathEntry = qobject_cast<Fm::PathEdit*>(activeViewFrame_->getTopBar())) {
pathEntry->setFocus();
pathEntry->selectAll();
}
}
else{
if(pathEntry_ != nullptr) {
pathEntry_->setFocus();
pathEntry_->selectAll();
}
else if(pathBar_ != nullptr) { // use button-style path bar
pathBar_->openEditor();
}
}
}
void MainWindow::dragEnterEvent(QDragEnterEvent* event) {
if(event->mimeData()->hasFormat(QStringLiteral("application/pcmanfm-qt-tab"))
// ensure that the tab drag source is ours (and not a root window, for example)
&& event->source() != nullptr) {
event->acceptProposedAction();
}
}
void MainWindow::dropEvent(QDropEvent* event) {
if(event->mimeData()->hasFormat(QStringLiteral("application/pcmanfm-qt-tab"))) {
if(QObject *sourseObject = event->source()) {
// announce that the tab drop is accepted by us (see TabBar::mouseMoveEvent)
sourseObject->setProperty(TabBar::tabDropped, true);
// the tab will be dropped (moved) after the DND is finished
QTimer::singleShot(0, sourseObject, [this, sourseObject]() {
dropTab(sourseObject);
});
}
}
event->acceptProposedAction();
}
void MainWindow::dropTab(QObject* source) {
QWidget* w = qobject_cast<QWidget*>(source);
MainWindow* dragSource = (w == nullptr ? nullptr : qobject_cast<MainWindow*>(w->window()));
if (dragSource == this // drop on itself
|| dragSource == nullptr) {
activeViewFrame_->getTabBar()->finishMouseMoveEvent();
return;
}
// first close the tab in the drag window;
// then add its page to a new tab in the drop window
TabPage* dropPage = dragSource->currentPage();
if(dropPage) {
disconnect(dropPage, nullptr, dragSource, nullptr);
// release mouse before tab removal because otherwise, the source tabbar
// might not be updated properly with tab reordering during a fast drag-and-drop
dragSource->activeViewFrame_->getTabBar()->releaseMouse();
dragSource->activeViewFrame_->getStackedWidget()->removeWidget(dropPage);
int index = addTabWithPage(dropPage, activeViewFrame_);
activeViewFrame_->getTabBar()->setCurrentIndex(index);
}
else {
activeViewFrame_->getTabBar()->finishMouseMoveEvent(); // impossible
}
}
void MainWindow::detachTab() {
if (activeViewFrame_->getStackedWidget()->count() == 1 // don't detach a single tab
|| static_cast<Application*>(qApp)->settings().splitView()) { // may have changed elsewhere
activeViewFrame_->getTabBar()->finishMouseMoveEvent();
return;
}
// close the tab and move its page to a new window
TabPage* dropPage = currentPage();
if(dropPage) {
disconnect(dropPage, nullptr, this, nullptr);
activeViewFrame_->getTabBar()->releaseMouse(); // as in dropTab()
activeViewFrame_->getStackedWidget()->removeWidget(dropPage);
MainWindow* newWin = new MainWindow();
newWin->addTabWithPage(dropPage, newWin->activeViewFrame_);
newWin->show();
}
else {
activeViewFrame_->getTabBar()->finishMouseMoveEvent(); // impossible
}
}
void MainWindow::setTabIcon(TabPage* tabPage) {
ViewFrame* viewFrame = viewFrameForTabPage(tabPage);
if(viewFrame == nullptr) {
return;
}
bool isCustomized = tabPage->hasCustomizedView() || tabPage->hasInheritedCustomizedView();
int index = viewFrame->getStackedWidget()->indexOf(tabPage);
auto tabBar = viewFrame->getTabBar();
if(!isCustomized) {
if(!tabBar->tabIcon(index).isNull()) {
tabBar->setTabIcon(index, QIcon());
}
return;
}
// set the tab icon of a customized folder to its view mode
switch(tabPage->viewMode()) {
case Fm::FolderView::IconMode:
tabBar->setTabIcon(index, QIcon::fromTheme(QLatin1String("view-list-icons"), style()->standardIcon(QStyle::SP_FileDialogContentsView)));
break;
case Fm::FolderView::CompactMode:
tabBar->setTabIcon(index, QIcon::fromTheme(QLatin1String("view-list-text"), style()->standardIcon(QStyle::SP_FileDialogListView)));
break;
case Fm::FolderView::DetailedListMode:
tabBar->setTabIcon(index, QIcon::fromTheme(QLatin1String("view-list-details"), style()->standardIcon(QStyle::SP_FileDialogDetailedView)));
break;
case Fm::FolderView::ThumbnailMode:
tabBar->setTabIcon(index, QIcon::fromTheme(QLatin1String("view-preview"), style()->standardIcon(QStyle::SP_FileDialogInfoView)));
break;
}
}
void MainWindow::updateFromSettings(Settings& settings) {
// apply settings
// menu
ui.actionDelete->setText(settings.useTrash() ? tr("&Move to Trash") : tr("&Delete"));
ui.actionDelete->setIcon(settings.useTrash() ? QIcon::fromTheme(QStringLiteral("user-trash")) : QIcon::fromTheme(QStringLiteral("edit-delete")));
// side pane
ui.sidePane->setIconSize(QSize(settings.sidePaneIconSize(), settings.sidePaneIconSize()));
// recent files
int recentNumber = settings.getRecentFilesNumber();
auto actions = ui.menuRecentFiles->actions();
int N = actions.isEmpty() ? 0 : actions.size() - 2; // there is a separator and a clear action
if(recentNumber > N) {
ui.menuRecentFiles->setEnabled(true);
QAction* sep = nullptr;
if(actions.size() >= 2) {
ui.menuRecentFiles->removeAction(ui.actionClearRecent);
sep = ui.menuRecentFiles->actions().last();
ui.menuRecentFiles->removeAction(sep);
}
QAction* recentAction = nullptr;
for(int i = N; i < recentNumber; ++i) {
recentAction = new QAction(this);
recentAction->setVisible(false);
connect(recentAction, &QAction::triggered, this, &MainWindow::lanunchRecentFile);
ui.menuRecentFiles->addAction(recentAction);
}
if(sep) {
ui.menuRecentFiles->addAction(sep);
}
else {
ui.menuRecentFiles->addSeparator();
}
ui.menuRecentFiles->addAction(ui.actionClearRecent);
}
else if(recentNumber < N) {
for(int i = 0; i < N - recentNumber; ++i) {
auto lastAction = ui.menuRecentFiles->actions().at(ui.menuRecentFiles->actions().size() - 3);
ui.menuRecentFiles->removeAction(lastAction);
delete lastAction;
}
if(recentNumber == 0) {
ui.menuRecentFiles->clear(); // also deletes the separator
ui.menuRecentFiles->setEnabled(false);
}
}
// tabs
for(int i = 0; i < ui.viewSplitter->count(); ++i) {
if(ViewFrame* viewFrame = qobject_cast<ViewFrame*>(ui.viewSplitter->widget(i))) {
viewFrame->getTabBar()->setTabsClosable(settings.showTabClose());
viewFrame->getTabBar()->setAutoHide(!settings.alwaysShowTabs());
// all tab pages
int n = viewFrame->getStackedWidget()->count();
for(int j = 0; j < n; ++j) {
TabPage* page = static_cast<TabPage*>(viewFrame->getStackedWidget()->widget(j));
page->updateFromSettings(settings);
}
}
}
}
void MainWindow::on_actionOpenAsAdmin_triggered() {
if(TabPage* page = currentPage()) {
if(auto path = page->path()) {
if(path.isNative()) {
CStrPtr admin{g_strconcat("admin://", path.localPath().get(), nullptr)};
chdir(Fm::FilePath::fromPathStr(admin.get()));
}
}
}
}
void MainWindow::on_actionOpenAsRoot_triggered() {
TabPage* page = currentPage();
if(page) {
Application* app = static_cast<Application*>(qApp);
Settings& settings = app->settings();
if(!settings.suCommand().isEmpty()) {
// run the su command
// FIXME: it's better to get the filename of the current process rather than hard-code pcmanfm-qt here.
QByteArray suCommand = settings.suCommand().toLocal8Bit();
QByteArray programCommand = app->applicationFilePath().toLocal8Bit();
programCommand += " %U";
// if %s exists in the su command, substitute it with the program
int substPos = suCommand.indexOf("%s");
if(substPos != -1) {
// replace %s with program
suCommand.replace(substPos, 2, programCommand);
}
else {
/* no %s found so just append to it */
suCommand += programCommand;
}
Fm::GAppInfoPtr appInfo{g_app_info_create_from_commandline(suCommand.constData(), nullptr, GAppInfoCreateFlags(0), nullptr), false};
if(appInfo) {
auto cwd = page->path();
Fm::GErrorPtr err;
auto uri = cwd.uri();
GList* uris = g_list_prepend(nullptr, uri.get());
if(!g_app_info_launch_uris(appInfo.get(), uris, nullptr, &err)) {
QMessageBox::critical(this, tr("Error"), QString::fromUtf8(err->message));
}
g_list_free(uris);
}
}
else {
// show an error message and ask the user to set the command
QMessageBox::critical(this, tr("Error"), tr("Switch user command is not set."));
app->preferences(QStringLiteral("advanced"));
}
}
}
void MainWindow::on_actionFindFiles_triggered() {
Application* app = static_cast<Application*>(qApp);
const auto files = currentPage()->selectedFiles();
QStringList paths;
if(!files.empty()) {
for(const auto& file: files) {
// FIXME: is it ok to use display name here?
// This might be broken on filesystems with non-UTF-8 filenames.
if(file->isDir()) {
paths.append(QString::fromUtf8(file->path().displayName().get()));
}
}
}
if(paths.isEmpty()) {
paths.append(currentPage()->pathName());
}
app->findFiles(paths);
}
void MainWindow::on_actionOpenTerminal_triggered() {
TabPage* page = currentPage();
if(page) {
Application* app = static_cast<Application*>(qApp);
app->openFolderInTerminal(page->path());
}
}
void MainWindow::on_actionCreateLauncher_triggered() {
TabPage* page = currentPage();
if(page) {
page->createShortcut();
}
}
void MainWindow::on_actionCopyFullPath_triggered() {
TabPage* page = currentPage();
if(page) {
auto paths = page->selectedFilePaths();
if(paths.size() == 1) {
QApplication::clipboard()->setText(QString::fromUtf8(paths.front().toString().get()), QClipboard::Clipboard);
}
}
}
void MainWindow::onShortcutNextTab() {
int current = activeViewFrame_->getTabBar()->currentIndex();
if(current < activeViewFrame_->getTabBar()->count() - 1) {
activeViewFrame_->getTabBar()->setCurrentIndex(current + 1);
}
else {
activeViewFrame_->getTabBar()->setCurrentIndex(0);
}
}
void MainWindow::onShortcutPrevTab() {
int current = activeViewFrame_->getTabBar()->currentIndex();
if(current > 0) {
activeViewFrame_->getTabBar()->setCurrentIndex(current - 1);
}
else {
activeViewFrame_->getTabBar()->setCurrentIndex(activeViewFrame_->getTabBar()->count() - 1);
}
}
// Switch to nth tab when Alt+n or Ctrl+n is pressed
void MainWindow::onShortcutJumpToTab() {
QShortcut* shortcut = reinterpret_cast<QShortcut*>(sender());
QKeySequence seq = shortcut->key();
QKeyCombination keyComb = seq[0];
Qt::Key keyValue = keyComb.key();
int index;
if(keyValue == '0') {
index = 9;
}
else {
index = keyValue - '1';
}
if(index < activeViewFrame_->getTabBar()->count()) {
activeViewFrame_->getTabBar()->setCurrentIndex(index);
}
}
void MainWindow::on_actionCleanPerFolderConfig_triggered() {
QMessageBox::StandardButton r = QMessageBox::question(this,
tr("Cleaning Folder Settings"),
tr("Do you want to remove settings of nonexistent folders?\nThey might be useful if those folders are created again."),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if(r == QMessageBox::Yes) {
Application* app = static_cast<Application*>(qApp);
app->cleanPerFolderConfig();
}
}
void MainWindow::openFolderAndSelectFiles(const Fm::FilePathList& files, bool inNewTab) {
if(!files.empty()) {
if(auto path = files.front().parent()) {
if(!inNewTab) {
auto win = new MainWindow(path);
win->show();
if(auto page = win->currentPage()) {
page->setFilesToSelect(files);
}
}
else {
TabPage* newPage = new TabPage(this);
addTabWithPage(newPage, activeViewFrame_, std::move(path));
newPage->setFilesToSelect(files);
}
}
}
}
}
|