Warning, file /utilities/krusader/app/Synchronizer/feedtolistboxdialog.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2006 Csaba Karai <krusader@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2006-2022 Krusader Krew <https://krusader.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "feedtolistboxdialog.h"
0009 #include "../FileSystem/filesystem.h"
0010 #include "../FileSystem/virtualfilesystem.h"
0011 #include "../krglobal.h"
0012 #include "../krusaderview.h"
0013 #include "../panelmanager.h"
0014 #include "synchronizer.h"
0015 #include "synchronizergui.h"
0016 
0017 // QtWidgets
0018 #include <QCheckBox>
0019 #include <QComboBox>
0020 #include <QDialogButtonBox>
0021 #include <QLabel>
0022 #include <QLineEdit>
0023 #include <QVBoxLayout>
0024 
0025 #include <KConfigCore/KConfig>
0026 #include <KI18n/KLocalizedString>
0027 #include <KWidgetsAddons/KMessageBox>
0028 
0029 #define S_LEFT 0
0030 #define S_RIGHT 1
0031 #define S_BOTH 2
0032 
0033 FeedToListBoxDialog::FeedToListBoxDialog(QWidget *parent, Synchronizer *sync, QTreeWidget *syncL, bool equOK)
0034     : QDialog(parent)
0035     , synchronizer(sync)
0036     , syncList(syncL)
0037     , equalAllowed(equOK)
0038     , accepted(false)
0039 {
0040     setWindowTitle(i18n("Krusader::Feed to listbox"));
0041     setWindowModality(Qt::WindowModal);
0042 
0043     auto *mainLayout = new QVBoxLayout;
0044     setLayout(mainLayout);
0045 
0046     // autodetecting the parameters
0047 
0048     int selectedNum = 0;
0049     int itemNum = 0;
0050     int leftExistingNum = 0;
0051     int rightExistingNum = 0;
0052 
0053     QTreeWidgetItemIterator it(syncList);
0054     while (*it) {
0055         auto *item = dynamic_cast<SynchronizerGUI::SyncViewItem *>(*it);
0056         SynchronizerFileItem *syncItem = item->synchronizerItemRef();
0057 
0058         if (syncItem && syncItem->isMarked()) {
0059             if (item->isSelected() || syncItem->task() != TT_EQUALS || equalAllowed) {
0060                 itemNum++;
0061                 if (item->isSelected())
0062                     selectedNum++;
0063 
0064                 if (syncItem->existsInLeft())
0065                     leftExistingNum++;
0066                 if (syncItem->existsInRight())
0067                     rightExistingNum++;
0068             }
0069         }
0070         it++;
0071     }
0072 
0073     if (itemNum == 0) {
0074         hide();
0075         KMessageBox::error(parent, i18n("No elements to feed."));
0076         return;
0077     }
0078 
0079     // guessing the collection name
0080 
0081     VirtualFileSystem virtFilesystem;
0082     if (!virtFilesystem.scanDir(QUrl("virt:/")))
0083         return;
0084 
0085     KConfigGroup group(krConfig, "Synchronize");
0086     int listBoxNum = group.readEntry("Feed To Listbox Counter", 1);
0087     QString queryName;
0088     do {
0089         queryName = i18n("Synchronize results") + QString(" %1").arg(listBoxNum++);
0090     } while (virtFilesystem.getFileItem(queryName) != nullptr);
0091     group.writeEntry("Feed To Listbox Counter", listBoxNum);
0092 
0093     // creating the widget
0094 
0095     QLabel *label = new QLabel(i18n("Here you can name the file collection"), this);
0096     mainLayout->addWidget(label);
0097 
0098     lineEdit = new QLineEdit(this);
0099     lineEdit->setText(queryName);
0100     lineEdit->setClearButtonEnabled(true);
0101     lineEdit->selectAll();
0102     mainLayout->addWidget(lineEdit);
0103 
0104     auto *hbox = new QHBoxLayout;
0105 
0106     QLabel *label2 = new QLabel(i18n("Side to feed:"), this);
0107     label2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0108     hbox->addWidget(label2);
0109 
0110     sideCombo = new QComboBox(this);
0111     sideCombo->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0112     sideCombo->addItem(i18n("Left"));
0113     sideCombo->addItem(i18n("Right"));
0114     sideCombo->addItem(i18n("Both"));
0115     hbox->addWidget(sideCombo);
0116 
0117     if (leftExistingNum == 0) {
0118         sideCombo->setCurrentIndex(1);
0119         sideCombo->setEnabled(false);
0120     } else if (rightExistingNum == 0) {
0121         sideCombo->setCurrentIndex(0);
0122         sideCombo->setEnabled(false);
0123     } else
0124         sideCombo->setCurrentIndex(2);
0125 
0126     QFrame *line = new QFrame(this);
0127     line->setFrameStyle(QFrame::VLine | QFrame::Sunken);
0128     line->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
0129     hbox->addWidget(line);
0130 
0131     cbSelected = new QCheckBox(i18n("Selected files only"), this);
0132     cbSelected->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
0133     cbSelected->setEnabled(selectedNum != 0);
0134     cbSelected->setChecked(selectedNum != 0);
0135     hbox->addWidget(cbSelected);
0136 
0137     mainLayout->addLayout(hbox);
0138 
0139     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0140     mainLayout->addWidget(buttonBox);
0141 
0142     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0143     okButton->setDefault(true);
0144     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0145 
0146     connect(buttonBox, &QDialogButtonBox::accepted, this, &FeedToListBoxDialog::slotOk);
0147     connect(buttonBox, &QDialogButtonBox::rejected, this, &FeedToListBoxDialog::reject);
0148 
0149     exec();
0150 }
0151 
0152 void FeedToListBoxDialog::slotOk()
0153 {
0154     int side = sideCombo->currentIndex();
0155     bool selected = cbSelected->isChecked();
0156     QString name = lineEdit->text();
0157     QList<QUrl> urlList;
0158 
0159     QTreeWidgetItemIterator it(syncList);
0160     for (; *it; it++) {
0161         auto *item = dynamic_cast<SynchronizerGUI::SyncViewItem *>(*it);
0162         SynchronizerFileItem *syncItem = item->synchronizerItemRef();
0163 
0164         if (!syncItem || !syncItem->isMarked())
0165             continue;
0166         if (selected && !item->isSelected())
0167             continue;
0168         if (!equalAllowed && syncItem->task() == TT_EQUALS && (!selected || !item->isSelected()))
0169             continue;
0170 
0171         if ((side == S_BOTH || side == S_LEFT) && syncItem->existsInLeft()) {
0172             QString leftDirName = syncItem->leftDirectory().isEmpty() ? "" : syncItem->leftDirectory() + '/';
0173             QUrl leftURL = Synchronizer::fsUrl(synchronizer->leftBaseDirectory() + leftDirName + syncItem->leftName());
0174             urlList.push_back(leftURL);
0175         }
0176 
0177         if ((side == S_BOTH || side == S_RIGHT) && syncItem->existsInRight()) {
0178             QString rightDirName = syncItem->rightDirectory().isEmpty() ? "" : syncItem->rightDirectory() + '/';
0179             QUrl leftURL = Synchronizer::fsUrl(synchronizer->rightBaseDirectory() + rightDirName + syncItem->rightName());
0180             urlList.push_back(leftURL);
0181         }
0182     }
0183 
0184     QUrl url = QUrl(QString("virt:/") + name);
0185     VirtualFileSystem virtFilesystem;
0186     if (!virtFilesystem.refresh(url)) { // create directory if it does not exist
0187         KMessageBox::error(parentWidget(), i18n("Cannot open %1.", url.toDisplayString()));
0188         return;
0189     }
0190     virtFilesystem.addFiles(urlList);
0191     ACTIVE_MNG->slotNewTab(url);
0192     accepted = true;
0193     accept();
0194 }