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
|
/*
SPDX-FileCopyrightText: 2004 Jonas Bähr <jonas.baehr@web.de>
SPDX-FileCopyrightText: 2004 Shie Erlich <erlich@users.sourceforge.net>
SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "expander.h"
#include "../FileSystem/filesystemprovider.h"
#include "../GUI/profilemanager.h"
#include "../KViewer/krviewer.h"
#include "../Panel/PanelView/krview.h"
#include "../Panel/listpanel.h"
#include "../Panel/panelfunc.h"
#include "../Search/krsearchdialog.h"
#include "../krservices.h"
#include "../krusader.h"
#include "../krusaderview.h"
#include "../panelmanager.h"
#ifdef SYNCHRONIZER_ENABLED
#include "../Synchronizer/synchronizergui.h"
#endif
// QtCore
#include <QDebug>
#include <QList>
#include <QStringList>
#include <QTemporaryFile>
#include <QTextStream>
// QtGui
#include <QClipboard>
// QtWidgets
#include <QApplication>
#include <QInputDialog>
#include <KLocalizedString>
#include <KMessageBox>
#include <algorithm>
#include <functional>
using namespace std;
#define NEED_PANEL \
if (panel == nullptr) { \
panelMissingError(_expression, exp); \
return QString(); \
}
inline void exp_placeholder::setError(Expander &exp, const Error &e)
{
exp.setError(e);
}
inline QStringList exp_placeholder::splitEach(const TagString &s)
{
return Expander::splitEach(s);
}
inline exp_placeholder::exp_placeholder() = default;
void exp_placeholder::panelMissingError(const QString &s, Expander &exp)
{
exp.setError(Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Needed panel specification missing in expander %1", s)));
}
QStringList exp_placeholder::fileList(const KrPanel *const panel,
const QString &type,
const QString &mask,
const bool omitPath,
const bool useUrl,
Expander &exp,
const QString &error)
{
QStringList items;
if (type.isEmpty() || type == "all")
panel->view->getItemsByMask(mask, &items);
else if (type == "files")
panel->view->getItemsByMask(mask, &items, false, true);
else if (type == "dirs")
panel->view->getItemsByMask(mask, &items, true, false);
else if (type == "selected")
panel->view->getSelectedItems(&items);
else {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: Bad argument to %1: %2 is not valid item specifier", error, type)));
return QStringList();
}
if (!omitPath) { // add the current path
// translate to urls using filesystem
const QList<QUrl> list = panel->func->files()->getUrls(items);
items.clear();
// parse everything to a single qstring
for (const QUrl &url : list) {
items.push_back(useUrl ? url.url() : url.path());
}
}
return items;
}
namespace
{
class exp_simpleplaceholder : public exp_placeholder
{
public:
EXP_FUNC override;
virtual TagString expFunc(const KrPanel *, const QStringList &, const bool &, Expander &) const = 0;
};
#define PLACEHOLDER_CLASS(name) \
class name : public exp_placeholder \
{ \
public: \
name(); \
virtual TagString expFunc(const KrPanel *, const TagStringList &, const bool &, Expander &) const override; \
};
#define SIMPLE_PLACEHOLDER_CLASS(name) \
class name : public exp_simpleplaceholder \
{ \
public: \
using exp_simpleplaceholder::expFunc; \
name(); \
virtual TagString expFunc(const KrPanel *, const QStringList &, const bool &, Expander &) const override; \
};
/**
* expands %_Path% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the path of the specified panel
*/
SIMPLE_PLACEHOLDER_CLASS(exp_Path)
/**
* expands %_Count% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the number of items, which type is
* specified by the first Parameter
*/
SIMPLE_PLACEHOLDER_CLASS(exp_Count)
/**
* expands %_Filter% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the correspondent filter (ie: "*.cpp")
*/
SIMPLE_PLACEHOLDER_CLASS(exp_Filter)
/**
* expands %_Current% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the current item ( != the selected ones)
*/
SIMPLE_PLACEHOLDER_CLASS(exp_Current)
/**
* expands %_List% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with a list of items, which type is specified by
* the first Parameter
*/
SIMPLE_PLACEHOLDER_CLASS(exp_List)
/**
* expands %_ListFile% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate
* the active, other, right or left panel) with the name of a temporary file,
* containing a list of items, which type is specified by the first Parameter
*/
SIMPLE_PLACEHOLDER_CLASS(exp_ListFile)
/**
* expands %_Ask% ('_' is necessary because there is no panel needed)
* with the return of an input-dialog
*/
SIMPLE_PLACEHOLDER_CLASS(exp_Ask)
/**
* This copies it's first Parameter to the clipboard
*/
PLACEHOLDER_CLASS(exp_Clipboard)
/**
* This selects all items by the mask given with the first Parameter
*/
SIMPLE_PLACEHOLDER_CLASS(exp_Select)
/**
* This changes the panel'spath to the value given with the first Parameter.
*/
SIMPLE_PLACEHOLDER_CLASS(exp_Goto)
/**
* This is equal to 'cp <first Parameter> <second Parameter>'.
*/
PLACEHOLDER_CLASS(exp_Copy)
/**
* This is equal to 'mv <first Parameter> <second Parameter>'.
*/
PLACEHOLDER_CLASS(exp_Move)
#ifdef SYNCHRONIZER_ENABLED
/**
* This opens the synchronizer with a given profile
*/
SIMPLE_PLACEHOLDER_CLASS(exp_Sync)
#endif
/**
* This opens the searchmodule with a given profile
*/
SIMPLE_PLACEHOLDER_CLASS(exp_NewSearch)
/**
* This loads the panel-profile with a given name
*/
SIMPLE_PLACEHOLDER_CLASS(exp_Profile)
/**
* This is setting marks in the string where he is later split up for each {all, selected, files, dirs}
*/
SIMPLE_PLACEHOLDER_CLASS(exp_Each)
/**
* This sets the sorting on a specific column
*/
SIMPLE_PLACEHOLDER_CLASS(exp_ColSort)
/**
* This sets relation between the left and right panel
*/
SIMPLE_PLACEHOLDER_CLASS(exp_PanelSize)
/**
* This loads a file in the internal viewer
*/
SIMPLE_PLACEHOLDER_CLASS(exp_View)
////////////////////////////////////////////////////////////
//////////////////////// utils ////////////////////////
////////////////////////////////////////////////////////////
/**
* escapes everything that confuses bash in filenames
* @param s String to manipulate
* @return escaped string
*/
QString bashquote(QString s)
{
/*
// we _can_not_ use this function because it _encloses_ the sting in single-quotes!
// In this case quotes strings could not be concatenated anymore
return KrServices::quote(s);
*/
static const QString evilstuff = "\\\"'`()[]{}!?;$&<>| \t\r\n"; // stuff that should get escaped
for (auto i : evilstuff)
s.replace(i, ('\\' + i));
return s;
}
QString separateAndQuote(QStringList list, const QString &separator, const bool quote)
{
if (quote)
transform(list.begin(), list.end(), list.begin(), bashquote);
// QLineEdit::text() always escapes special characters, revert this for newline and tab
QString decodedSeparator = separator;
decodedSeparator.replace("\\n", "\n").replace("\\t", "\t");
return list.join(decodedSeparator);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// expander classes ////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
exp_Path::exp_Path()
{
_expression = "Path";
_description = i18n("Panel's Path...");
_needPanel = true;
addParameter(exp_parameter(i18n("Automatically escape spaces"), "__yes", false));
}
TagString exp_Path::expFunc(const KrPanel *panel, const QStringList ¶meter, const bool &useUrl, Expander &exp) const
{
NEED_PANEL
QString result;
if (useUrl)
result = panel->func->files()->currentDirectory().url() + '/';
else
result = panel->func->files()->currentDirectory().path() + '/';
if (parameter.count() > 0 && parameter[0].toLower() == "no") // don't escape spaces
return TagString(result);
else
return TagString(bashquote(result));
}
exp_Count::exp_Count()
{
_expression = "Count";
_description = i18n("Number of...");
_needPanel = true;
addParameter(exp_parameter(i18n("Count:"), "__choose:All;Files;Dirs;Selected", false));
}
TagString exp_Count::expFunc(const KrPanel *panel, const QStringList ¶meter, const bool &, Expander &exp) const
{
NEED_PANEL
int n = -1;
if (parameter.count() == 0 || parameter[0].isEmpty() || parameter[0].toLower() == "all")
n = panel->view->numDirs() + panel->view->numFiles();
else if (parameter[0].toLower() == "files")
n = panel->view->numFiles();
else if (parameter[0].toLower() == "dirs")
n = panel->view->numDirs();
else if (parameter[0].toLower() == "selected")
n = panel->view->numSelected();
else {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: Bad argument to Count: %1 is not valid item specifier", parameter[0])));
return QString();
}
return TagString(QString("%1").arg(n));
}
exp_Filter::exp_Filter()
{
_expression = "Filter";
_description = i18n("Filter Mask (*.h, *.cpp, etc.)");
_needPanel = true;
}
TagString exp_Filter::expFunc(const KrPanel *panel, const QStringList &, const bool &, Expander &exp) const
{
NEED_PANEL
return panel->view->filterMask().nameFilter();
}
exp_Current::exp_Current()
{
_expression = "Current";
_description = i18n("Current File (!= Selected File)...");
_needPanel = true;
addParameter(exp_parameter(i18n("Omit the current path (optional)"), "__no", false));
addParameter(exp_parameter(i18n("Automatically escape spaces"), "__yes", false));
}
TagString exp_Current::expFunc(const KrPanel *panel, const QStringList ¶meter, const bool &useUrl, Expander &exp) const
{
NEED_PANEL
QString item = panel->view->getCurrentItem();
if (item == "..") {
// if ".." is current, treat this as nothing is current
return QString();
}
QString result;
if (parameter.count() > 0 && parameter[0].toLower() == "yes") // omit the current path
result = item;
else {
const QUrl url = panel->func->files()->getUrl(item);
result = useUrl ? url.url() : url.path();
}
const bool escapeSpaces = parameter.count() < 2 || parameter[1].toLower() != "no";
return escapeSpaces ? bashquote(result) : result;
}
exp_List::exp_List()
{
_expression = "List";
_description = i18n("Item List of...");
_needPanel = true;
addParameter(exp_parameter(i18n("Which items:"), "__choose:All;Files;Dirs;Selected", false));
addParameter(exp_parameter(i18n("Separator between the items (optional):"), " ", false));
addParameter(exp_parameter(i18n("Omit the current path (optional)"), "__no", false));
addParameter(exp_parameter(i18n("Mask (optional, all but 'Selected'):"), "__select", false));
addParameter(exp_parameter(i18n("Automatically escape spaces"), "__yes", false));
}
TagString exp_List::expFunc(const KrPanel *panel, const QStringList ¶meter, const bool &useUrl, Expander &exp) const
{
NEED_PANEL
// get selected items from view
QStringList items;
QString mask;
if (parameter.count() <= 3 || parameter[3].isEmpty())
mask = '*';
else
mask = parameter[3];
return separateAndQuote(fileList(panel,
parameter.isEmpty() ? QString() : parameter[0].toLower(),
mask,
parameter.count() > 2 ? parameter[2].toLower() == "yes" : false,
useUrl,
exp,
"List"),
parameter.count() > 1 ? parameter[1] : " ",
parameter.count() > 4 ? parameter[4].toLower() == "yes" : true);
}
exp_ListFile::exp_ListFile()
{
_expression = "ListFile";
_description = i18n("Filename of an Item List...");
_needPanel = true;
addParameter(exp_parameter(i18n("Which items:"), "__choose:All;Files;Dirs;Selected", false));
addParameter(exp_parameter(i18n("Separator between the items (optional)"), "\n", false));
addParameter(exp_parameter(i18n("Omit the current path (optional)"), "__no", false));
addParameter(exp_parameter(i18n("Mask (optional, all but 'Selected'):"), "__select", false));
addParameter(exp_parameter(i18n("Automatically escape spaces"), "__no", false));
}
TagString exp_ListFile::expFunc(const KrPanel *panel, const QStringList ¶meter, const bool &useUrl, Expander &exp) const
{
NEED_PANEL
// get selected items from view
QStringList items;
QString mask;
if (parameter.count() <= 3 || parameter[3].isEmpty())
mask = '*';
else
mask = parameter[3];
QTemporaryFile tmpFile(QDir::tempPath() + QLatin1String("/krusader_XXXXXX.itemlist"));
tmpFile.setAutoRemove(false);
if (!tmpFile.open()) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_WORLD, i18n("Expander: temporary file could not be opened (%1)", tmpFile.errorString())));
return QString();
}
QTextStream stream(&tmpFile);
stream << separateAndQuote(fileList(panel,
parameter.isEmpty() ? QString() : parameter[0].toLower(),
mask,
parameter.count() > 2 ? parameter[2].toLower() == "yes" : false,
useUrl,
exp,
"ListFile"),
parameter.count() > 1 ? parameter[1] : "\n",
parameter.count() > 4 ? parameter[4].toLower() == "yes" : true)
<< "\n";
tmpFile.close();
return tmpFile.fileName();
}
exp_Select::exp_Select()
{
_expression = "Select";
_description = i18n("Manipulate the Selection...");
_needPanel = true;
addParameter(exp_parameter(i18n("Selection mask:"), "__select", true));
addParameter(exp_parameter(i18n("Manipulate in which way:"), "__choose:Set;Add;Remove", false));
}
TagString exp_Select::expFunc(const KrPanel *panel, const QStringList ¶meter, const bool &, Expander &exp) const
{
NEED_PANEL
KrQuery mask;
if (parameter.count() <= 0 || parameter[0].isEmpty())
mask = KrQuery("*");
else
mask = KrQuery(parameter[0]);
if (parameter.count() > 1 && parameter[1].toLower() == "list-add")
panel->view->select(mask);
else if (parameter.count() > 1 && parameter[1].toLower() == "list-remove")
panel->view->unselect(mask);
else { // parameter[1].toLower() == "set" or isEmpty() or whatever
panel->view->unselect(KrQuery("*"));
panel->view->select(mask);
}
return QString(); // this doesn't return anything, that's normal!
}
exp_Goto::exp_Goto()
{
_expression = "Goto";
_description = i18n("Jump to a Location...");
_needPanel = true;
addParameter(exp_parameter(i18n("Choose a path:"), "__goto", true));
addParameter(exp_parameter(i18n("Open location in a new tab"), "__no", false));
}
TagString exp_Goto::expFunc(const KrPanel *panel, const QStringList ¶meter, const bool &, Expander &exp) const
{
NEED_PANEL
bool newTab = false;
if (parameter.count() > 1 && parameter[1].toLower() == "yes")
newTab = true;
if (parameter.count() == 0) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: at least 1 parameter is required for Goto.")));
return QString();
}
QUrl url = QUrl::fromUserInput(parameter[0], QString(), QUrl::AssumeLocalFile);
if (newTab) {
if (panel == LEFT_PANEL)
MAIN_VIEW->leftManager()->slotNewTab(url);
else
MAIN_VIEW->rightManager()->slotNewTab(url);
} else {
panel->func->openUrl(url, "");
panel->gui->slotFocusOnMe();
}
return QString(); // this doesn't return anything, that's normal!
}
/*
exp_Search::exp_Search() {
_expression = "Search";
_description = i18n("Search for files");
_needPanel = true;
addParameter( new exp_parameter( i18n("please choose the setting"), "__searchprofile", true ) );
addParameter( new exp_parameter( i18n("open the search in a new tab"), "__yes", false ) ); //TODO: add this also to panel-dependent as soon as filesystem
support the display of search-results
}
*/
exp_Ask::exp_Ask()
{
_expression = "Ask";
_description = i18n("Ask Parameter from User...");
_needPanel = false;
addParameter(exp_parameter(i18n("Question:"), "Where do you want do go today?", true));
addParameter(exp_parameter(i18n("Preset (optional):"), "", false));
addParameter(exp_parameter(i18n("Caption (optional):"), "", false));
}
TagString exp_Ask::expFunc(const KrPanel *, const QStringList ¶meter, const bool &, Expander &exp) const
{
QString caption, preset, result;
if (parameter.count() == 0) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: at least 1 parameter is required for Ask.")));
return QString();
}
if (parameter.count() <= 2 || parameter[2].isEmpty())
caption = i18n("User Action");
else
caption = parameter[2];
if (parameter.count() <= 1 || parameter[1].isEmpty())
preset.clear();
else
preset = parameter[1];
bool ok;
result = QInputDialog::getText(krMainWindow, caption, parameter[0], QLineEdit::Normal, preset, &ok);
if (ok)
return result;
else {
// user cancelled
setError(exp, Error(Error::exp_S_ERROR, Error::exp_C_USER));
return QString();
}
}
exp_Clipboard::exp_Clipboard()
{
_expression = "Clipboard";
_description = i18n("Copy to Clipboard...");
_needPanel = false;
addParameter(exp_parameter(i18n("What to copy:"), "__placeholder", true));
addParameter(exp_parameter(i18n("Append to current clipboard content with this separator (optional):"), "", false));
}
TagString exp_Clipboard::expFunc(const KrPanel *, const TagStringList ¶meter, const bool &, Expander &exp) const
{
// qDebug() << "Expander::exp_Clipboard, parameter[0]: '" << parameter[0] << "', Clipboard: " << QApplication::clipboard()->text();
if (parameter.count() == 0) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: at least 1 parameter is required for Clipboard.")));
return QString();
}
QStringList lst = splitEach(parameter[0]);
if (parameter.count() > 1 && !parameter[1].isSimple()) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_SYNTAX, i18n("Expander: %Each% may not be in the second argument of %Clipboard%")));
return QString();
}
if (parameter.count() <= 1 || parameter[1].string().isEmpty() || QApplication::clipboard()->text().isEmpty())
QApplication::clipboard()->setText(lst.join("\n"));
else
QApplication::clipboard()->setText(QApplication::clipboard()->text() + parameter[1].string() + lst.join("\n"));
return QString(); // this doesn't return anything, that's normal!
}
exp_Copy::exp_Copy()
{
_expression = "Copy";
_description = i18n("Copy a File/Folder...");
_needPanel = false;
addParameter(exp_parameter(i18n("What to copy:"), "__placeholder", true));
addParameter(exp_parameter(i18n("Where to copy:"), "__placeholder", true));
}
TagString exp_Copy::expFunc(const KrPanel *, const TagStringList ¶meter, const bool &, Expander &exp) const
{
if (parameter.count() < 2) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: at least 2 parameter is required for Copy.")));
return QString();
}
// basically the parameter can already be used as URL, but since QUrl has problems with ftp-proxy-urls (like ftp://username@proxyusername@url...) this is
// necessary:
const QStringList sourceList = splitEach(parameter[0]);
QList<QUrl> sourceURLs;
for (const QString &source : sourceList) {
sourceURLs.append(QUrl::fromUserInput(source, QString(), QUrl::AssumeLocalFile));
}
if (!parameter[1].isSimple()) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_SYNTAX, i18n("Expander: %Each% may not be in the second argument of %Copy%")));
return QString();
}
// or transform(...) ?
const QUrl dest = QUrl::fromUserInput(parameter[1].string(), QString(), QUrl::AssumeLocalFile);
if (!dest.isValid() || find_if(sourceURLs.constBegin(), sourceURLs.constEnd(), std::not_fn(std::bind(std::mem_fn(&QUrl::isValid), std::placeholders::_1))) != sourceURLs.constEnd()) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: invalid URLs in %_Copy(\"src\", \"dest\")%")));
return QString();
}
FileSystemProvider::instance().startCopyFiles(sourceURLs, dest);
return QString(); // this doesn't return everything, that's normal!
}
exp_Move::exp_Move()
{
_expression = "Move";
_description = i18n("Move/Rename a File/Folder...");
_needPanel = false;
addParameter(exp_parameter(i18n("What to move/rename:"), "__placeholder", true));
addParameter(exp_parameter(i18n("New target/name:"), "__placeholder", true));
}
TagString exp_Move::expFunc(const KrPanel *, const TagStringList ¶meter, const bool &, Expander &exp) const
{
if (parameter.count() < 2) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: at least 2 parameter is required for Move.")));
return QString();
}
// basically the parameter can already be used as URL, but since QUrl has problems with ftp-proxy-urls (like ftp://username@proxyusername@url...) this is
// necessary:
QStringList lst = splitEach(parameter[0]);
if (!parameter[1].isSimple()) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_SYNTAX, i18n("%Each% may not be in the second argument of %Move%")));
return QString();
}
QList<QUrl> src;
for (QStringList::const_iterator it = lst.constBegin(), end = lst.constEnd(); it != end; ++it)
src.push_back(QUrl::fromUserInput(*it, QString(), QUrl::AssumeLocalFile));
// or transform(...) ?
QUrl dest = QUrl::fromUserInput(parameter[1].string(), QString(), QUrl::AssumeLocalFile);
if (!dest.isValid() || find_if(src.constBegin(), src.constEnd(), std::not_fn(std::bind(std::mem_fn(&QUrl::isValid), std::placeholders::_1))) != src.constEnd()) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: invalid URLs in %_Move(\"src\", \"dest\")%")));
return QString();
}
FileSystemProvider::instance().startCopyFiles(src, dest, KIO::CopyJob::Move);
return QString(); // this doesn't return anything, that's normal!
}
#ifdef SYNCHRONIZER_ENABLED
exp_Sync::exp_Sync()
{
_expression = "Sync";
_description = i18n("Load a Synchronizer Profile...");
_needPanel = false;
addParameter(exp_parameter(i18n("Choose a profile:"), "__syncprofile", true));
}
TagString exp_Sync::expFunc(const KrPanel *, const QStringList ¶meter, const bool &, Expander &exp) const
{
if (parameter.count() == 0 || parameter[0].isEmpty()) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: no profile specified for %_Sync(profile)%")));
return QString();
}
SynchronizerGUI *synchronizerDialog = new SynchronizerGUI(MAIN_VIEW, parameter[0]);
synchronizerDialog->show(); // destroyed on close
return QString(); // this doesn't return everything, that's normal!
}
#endif
exp_NewSearch::exp_NewSearch()
{
_expression = "NewSearch";
_description = i18n("Load a Searchmodule Profile...");
_needPanel = false;
addParameter(exp_parameter(i18n("Choose a profile:"), "__searchprofile", true));
}
TagString exp_NewSearch::expFunc(const KrPanel *, const QStringList ¶meter, const bool &, Expander &exp) const
{
if (parameter.count() == 0 || parameter[0].isEmpty()) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: no profile specified for %_NewSearch(profile)%")));
return QString();
}
new KrSearchDialog(parameter[0], krApp);
return QString(); // this doesn't return everything, that's normal!
}
exp_Profile::exp_Profile()
{
_expression = "Profile";
_description = i18n("Load a Panel Profile...");
_needPanel = false;
addParameter(exp_parameter(i18n("Choose a profile:"), "__panelprofile", true));
}
TagString exp_Profile::expFunc(const KrPanel *, const QStringList ¶meter, const bool &, Expander &exp) const
{
if (parameter.count() == 0 || parameter[0].isEmpty()) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: no profile specified for %_Profile(profile)%; abort...")));
return QString();
}
MAIN_VIEW->profiles(parameter[0]);
return QString(); // this doesn't return everything, that's normal!
}
exp_Each::exp_Each()
{
_expression = "Each";
_description = i18n("Separate Program Call for Each...");
_needPanel = true;
addParameter(exp_parameter(i18n("Which items:"), "__choose:All;Files;Dirs;Selected", false));
addParameter(exp_parameter(i18n("Omit the current path (optional)"), "__no", false));
addParameter(exp_parameter(i18n("Mask (optional, all but 'Selected'):"), "__select", false));
addParameter(exp_parameter(i18n("Automatically escape spaces"), "__yes", false));
}
TagString exp_Each::expFunc(const KrPanel *panel, const QStringList ¶meter, const bool &useUrl, Expander &exp) const
{
NEED_PANEL
QString mask;
if (parameter.count() <= 2 || parameter[2].isEmpty())
mask = '*';
else
mask = parameter[2];
TagString ret;
QStringList l = fileList(panel,
parameter.empty() ? QString() : parameter[0].toLower(),
mask,
parameter.count() > 1 && parameter[1].toLower() == "yes",
useUrl,
exp,
"Each");
if (!(parameter.count() <= 3 || parameter[3].toLower() != "yes"))
transform(l.begin(), l.end(), l.begin(), bashquote);
ret.insertTag(0, l);
return ret;
}
exp_ColSort::exp_ColSort()
{
_expression = "ColSort";
_description = i18n("Set Sorting for This Panel...");
_needPanel = true;
addParameter(exp_parameter(i18n("Choose a column:"), "__choose:Name;Ext;Type;Size;Modified;Perms;rwx;Owner;Group", true));
addParameter(exp_parameter(i18n("Choose a sort sequence:"), "__choose:Toggle;Asc;Desc", false));
}
TagString exp_ColSort::expFunc(const KrPanel *panel, const QStringList ¶meter, const bool &, Expander &exp) const
{
NEED_PANEL
if (parameter.count() == 0 || parameter[0].isEmpty()) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: no column specified for %_ColSort(column)%")));
return QString();
}
KrViewProperties::ColumnType oldColumn = panel->view->properties()->sortColumn;
KrViewProperties::ColumnType column = oldColumn;
if (parameter[0].toLower() == "name") {
column = KrViewProperties::Name;
} else if (parameter[0].toLower() == "ext") {
column = KrViewProperties::Ext;
} else if (parameter[0].toLower() == "type") {
column = KrViewProperties::Type;
} else if (parameter[0].toLower() == "size") {
column = KrViewProperties::Size;
} else if (parameter[0].toLower() == "modified") {
column = KrViewProperties::Modified;
} else if (parameter[0].toLower() == "changed") {
column = KrViewProperties::Changed;
} else if (parameter[0].toLower() == "accessed") {
column = KrViewProperties::Accessed;
} else if (parameter[0].toLower() == "perms") {
column = KrViewProperties::Permissions;
} else if (parameter[0].toLower() == "rwx") {
column = KrViewProperties::KrPermissions;
} else if (parameter[0].toLower() == "owner") {
column = KrViewProperties::Owner;
} else if (parameter[0].toLower() == "group") {
column = KrViewProperties::Group;
} else {
setError(exp, Error(Error::exp_S_WARNING, Error::exp_C_ARGUMENT, i18n("Expander: unknown column specified for %_ColSort(%1)%", parameter[0])));
return QString();
}
bool descending = panel->view->properties()->sortOptions & KrViewProperties::Descending;
if (parameter.count() <= 1 || (parameter[1].toLower() != "asc" && parameter[1].toLower() != "desc")) { // no sortdir parameter
if (column == oldColumn) // reverse direction if column is unchanged
descending = !descending;
else // otherwise set to ascending
descending = false;
} else { // sortdir specified
if (parameter[1].toLower() == "asc")
descending = false;
else // == desc
descending = true;
}
panel->view->setSortMode(column, descending);
return QString(); // this doesn't return anything, that's normal!
}
exp_PanelSize::exp_PanelSize()
{
_expression = "PanelSize";
_description = i18n("Set Relation Between the Panels...");
_needPanel = true;
addParameter(exp_parameter(i18n("Set the new size in percent:"), "__int:0;100;5;50", true));
}
TagString exp_PanelSize::expFunc(const KrPanel *panel, const QStringList ¶meter, const bool &, Expander &exp) const
{
NEED_PANEL
int newSize;
if (parameter.count() == 0 || parameter[0].isEmpty())
newSize = 50; // default is 50%
else
newSize = parameter[0].toInt();
if (newSize < 0 || newSize > 100) {
setError(exp,
Error(Error::exp_S_FATAL,
Error::exp_C_ARGUMENT,
i18n("Expander: Value %1 out of range for %_PanelSize(percent)%. The first parameter has to be >0 and <100", newSize)));
return QString();
}
MAIN_VIEW->setPanelSize(panel->isLeft(), newSize);
return QString(); // this doesn't return everything, that's normal!
}
exp_View::exp_View()
{
_expression = "View";
_description = i18n("View File with Krusader's Internal Viewer...");
_needPanel = false;
addParameter(exp_parameter(i18n("Which file to view (normally '%aCurrent%'):"), "__placeholder", true));
addParameter(exp_parameter(i18n("Choose a view mode:"), "__choose:generic;text;hex", false));
// addParameter( exp_parameter( i18n("Choose a window-mode"), "__choose:tab;window;panel", false ) );
// TODO: window-mode 'panel' should open the file in the third-hand viewer
addParameter(exp_parameter(i18n("Choose a window mode:"), "__choose:tab;window", false));
}
TagString exp_View::expFunc(const KrPanel *, const QStringList ¶meter, const bool &, Expander &exp) const
{
if (parameter.count() == 0 || parameter[0].isEmpty()) {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_ARGUMENT, i18n("Expander: no file to view in %_View(filename)%")));
return QString();
}
QString viewMode, windowMode;
if (parameter.count() <= 1 || parameter[1].isEmpty())
viewMode = "generic";
else
viewMode = parameter[1];
if (parameter.count() <= 2 || parameter[2].isEmpty())
windowMode = "tab";
else
windowMode = parameter[2];
KrViewer::Mode mode = KrViewer::Generic;
if (viewMode == "text")
mode = KrViewer::Text;
else if (viewMode == "hex")
mode = KrViewer::Hex;
QUrl url = QUrl::fromUserInput(parameter[0], QString(), QUrl::AssumeLocalFile);
KrViewer::view(url, mode, (windowMode == "window"));
// TODO: Call the viewer with viewMode and windowMode. Filename is in parameter[0].
// It would be nice if parameter[0] could also be a space-separated filename-list (provided if the first parameter is %aList(selected)%)
return QString(); // this doesn't return everything, that's normal!
}
/////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////// end of expander classes ////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
TagString exp_simpleplaceholder::expFunc(const KrPanel *p, const TagStringList ¶meter, const bool &useUrl, Expander &exp) const
{
QStringList lst;
for (const auto &it : parameter)
if (it.isSimple())
lst.push_back(it.string());
else {
setError(exp, Error(Error::exp_S_FATAL, Error::exp_C_SYNTAX, i18n("%Each% is not allowed in parameter to %1", description())));
return QString();
}
return expFunc(p, lst, useUrl, exp);
}
}
KrPanel *Expander::getPanel(const char panelIndicator, const exp_placeholder *pl, Expander &exp)
{
switch (panelIndicator) {
case 'a':
return ACTIVE_PANEL;
case 'o':
return OTHER_PANEL;
case 'l':
return LEFT_PANEL;
case 'r':
return RIGHT_PANEL;
case '_':
return nullptr;
default:
exp.setError(
Error(Error::exp_S_FATAL, Error::exp_C_SYNTAX, i18n("Expander: Bad panel specifier %1 in placeholder %2", panelIndicator, pl->description())));
return nullptr;
}
}
void Expander::expand(const QString &stringToExpand, bool useUrl)
{
TagString result = expandCurrent(stringToExpand, useUrl);
if (error())
return;
if (!result.isSimple())
resultList = splitEach(result);
else
resultList.append(result.string());
// qWarning() << resultList[0];
}
TagString Expander::expandCurrent(const QString &stringToExpand, bool useUrl)
{
TagString result;
QString exp;
TagString tmpResult;
qsizetype begin, end, i;
// int brackets = 0;
// bool inQuotes = false;
qsizetype idx = 0;
while (idx < stringToExpand.length()) {
if ((begin = stringToExpand.indexOf('%', idx)) == -1)
break;
if ((end = findEnd(stringToExpand, begin)) == -1) {
// xgettext:no-c-format
setError(Error(Error::exp_S_FATAL, Error::exp_C_SYNTAX, i18n("Error: unterminated % in Expander")));
return QString();
}
result += stringToExpand.mid(idx, begin - idx); // copy until the start of %exp%
// get the expression, and expand it using the correct expander function
exp = stringToExpand.mid(begin + 1, end - begin - 1);
// qDebug() << "------------- exp: '" << exp << "'";
if (exp.isEmpty())
result += QString(QChar('%'));
else {
TagStringList parameter = separateParameter(&exp, useUrl);
if (error())
return QString();
char panelIndicator = exp.toLower()[0].toLatin1();
exp.replace(0, 1, "");
for (i = 0; i < placeholderCount(); ++i)
if (exp == placeholder(i)->expression()) {
// qDebug() << "---------------------------------------";
tmpResult = placeholder(i)->expFunc(getPanel(panelIndicator, placeholder(i), *this), parameter, useUrl, *this);
if (error()) {
return QString();
} else
result += tmpResult;
// qDebug() << "---------------------------------------";
break;
}
if (i == placeholderCount()) { // didn't find an expander
setError(Error(Error::exp_S_FATAL, Error::exp_C_SYNTAX, i18n("Error: unrecognized %%%1%2%% in Expander", panelIndicator, exp)));
return QString();
}
} // else
idx = end + 1;
}
// copy the rest of the string
result += stringToExpand.mid(idx);
// qDebug() << "============== result '" << result << "'";
return result;
}
QStringList Expander::splitEach(TagString stringToSplit)
{
if (stringToSplit.isSimple()) {
// qWarning() << stringToSplit.string();
QStringList l;
l << stringToSplit.string();
return l;
}
pair<uint, QStringList> pl = *stringToSplit.tagsBegin();
stringToSplit.eraseTag(stringToSplit.tagsBegin());
QStringList ret;
for (QStringList::const_iterator it = pl.second.constBegin(), end = pl.second.constEnd(); it != end; ++it) {
TagString s = stringToSplit;
s.insert(pl.first, *it);
ret += splitEach(s);
}
return ret;
// qDebug() << "stringToSplit: " << stringToSplit;
}
TagStringList Expander::separateParameter(QString *const exp, bool useUrl)
{
TagStringList parameter;
QStringList parameter1;
QString result;
qsizetype begin, end;
if ((begin = exp->indexOf('(')) != -1) {
if ((end = exp->lastIndexOf(')')) == -1) {
setError(Error(Error::exp_S_FATAL, Error::exp_C_SYNTAX, i18n("Error: missing ')' in Expander")));
return TagStringList();
}
result = exp->mid(begin + 1, end - begin - 1);
*exp = exp->left(begin);
bool inQuotes = false;
int idx = 0;
begin = 0;
while (idx < result.length()) {
if (result[idx].toLatin1() == '\\') {
if (result[idx + 1].toLatin1() == '"')
result.replace(idx, 1, "");
}
if (result[idx].toLatin1() == '"')
inQuotes = !inQuotes;
if (result[idx].toLatin1() == ',' && !inQuotes) {
parameter1.append(result.mid(begin, idx - begin));
begin = idx + 1;
// qWarning() << " ---- parameter: " << parameter.join(";");
}
idx++;
}
parameter1.append(result.mid(begin, idx - begin)); // don't forget the last one
for (auto &it : parameter1) {
it = it.trimmed();
if (it.left(1) == "\"")
it = it.mid(1, it.length() - 2);
parameter.push_back(expandCurrent(it, useUrl));
if (error())
return TagStringList();
}
}
// qWarning() << "------- exp: " << *exp << " ---- parameter: " << parameter.join(";");
return parameter;
}
qsizetype Expander::findEnd(const QString &str, qsizetype start)
{
qsizetype end = str.indexOf('%', start + 1);
if (end == -1)
return end;
qsizetype bracket = str.indexOf('(', start + 1);
if (end < bracket || bracket == -1)
return end;
qsizetype idx = bracket + 1;
bool inQuotes = false;
int depth = 1;
while (idx < str.length()) {
switch (str[idx].toLatin1()) {
case '\\':
idx++;
break;
case '"':
inQuotes = !inQuotes;
break;
case '(':
if (!inQuotes)
depth++;
break;
case ')':
if (!inQuotes)
--depth;
break;
case '%':
if (depth == 0)
return idx;
} // switch
idx++;
} // while
// failsafe
return -1;
}
QList<const exp_placeholder *> &Expander::_placeholder()
{
static QList<const exp_placeholder *> ret;
if (!ret.count()) {
ret << new exp_View;
ret << new exp_PanelSize;
ret << new exp_ColSort;
ret << new exp_Each;
ret << new exp_Profile;
ret << new exp_NewSearch;
#ifdef SYNCHRONIZER_ENABLED
ret << new exp_Sync;
#endif
ret << new exp_Move;
ret << new exp_Copy;
ret << new exp_Goto;
ret << new exp_Select;
ret << new exp_Clipboard;
ret << new exp_Ask;
ret << new exp_ListFile;
ret << new exp_List;
ret << new exp_Current;
ret << new exp_Filter;
ret << new exp_Count;
ret << new exp_Path;
}
return ret;
}
|