1 /*****************************************************************************
2  * Copyright (C) 2006 Csaba Karai <krusader@users.sourceforge.net>           *
3  * Copyright (C) 2006-2019 Krusader Krew [https://krusader.org]              *
4  *                                                                           *
5  * This file is part of Krusader [https://krusader.org].                     *
6  *                                                                           *
7  * Krusader is free software: you can redistribute it and/or modify          *
8  * it under the terms of the GNU General Public License as published by      *
9  * the Free Software Foundation, either version 2 of the License, or         *
10  * (at your option) any later version.                                       *
11  *                                                                           *
12  * Krusader is distributed in the hope that it will be useful,               *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
15  * GNU General Public License for more details.                              *
16  *                                                                           *
17  * You should have received a copy of the GNU General Public License         *
18  * along with Krusader.  If not, see [http://www.gnu.org/licenses/].         *
19  *****************************************************************************/
20 
21 #include "feedtolistboxdialog.h"
22 #include "synchronizer.h"
23 #include "synchronizergui.h"
24 #include "../FileSystem/filesystem.h"
25 #include "../FileSystem/virtualfilesystem.h"
26 #include "../krglobal.h"
27 #include "../krusaderview.h"
28 #include "../panelmanager.h"
29 
30 // QtWidgets
31 #include <QCheckBox>
32 #include <QDialogButtonBox>
33 #include <QLineEdit>
34 #include <QLabel>
35 #include <QComboBox>
36 #include <QVBoxLayout>
37 
38 #include <KConfigCore/KConfig>
39 #include <KI18n/KLocalizedString>
40 #include <KWidgetsAddons/KMessageBox>
41 
42 #define  S_LEFT        0
43 #define  S_RIGHT       1
44 #define  S_BOTH        2
45 
FeedToListBoxDialog(QWidget * parent,Synchronizer * sync,QTreeWidget * syncL,bool equOK)46 FeedToListBoxDialog::FeedToListBoxDialog(QWidget *parent, Synchronizer *sync,
47         QTreeWidget *syncL, bool equOK) : QDialog(parent),
48         synchronizer(sync), syncList(syncL), equalAllowed(equOK), accepted(false)
49 {
50 
51     setWindowTitle(i18n("Krusader::Feed to listbox"));
52     setWindowModality(Qt::WindowModal);
53 
54     QVBoxLayout *mainLayout = new QVBoxLayout;
55     setLayout(mainLayout);
56 
57     // autodetecting the parameters
58 
59     int selectedNum = 0;
60     int itemNum = 0;
61     int leftExistingNum = 0;
62     int rightExistingNum = 0;
63 
64     QTreeWidgetItemIterator it(syncList);
65     while (*it) {
66         SynchronizerGUI::SyncViewItem *item = (SynchronizerGUI::SyncViewItem *) * it;
67         SynchronizerFileItem *syncItem = item->synchronizerItemRef();
68 
69         if (syncItem && syncItem->isMarked()) {
70             if (item->isSelected() || syncItem->task() != TT_EQUALS || equalAllowed) {
71                 itemNum++;
72                 if (item->isSelected())
73                     selectedNum++;
74 
75                 if (syncItem->existsInLeft())
76                     leftExistingNum++;
77                 if (syncItem->existsInRight())
78                     rightExistingNum++;
79             }
80         }
81         it++;
82     }
83 
84     if (itemNum == 0) {
85         hide();
86         KMessageBox::error(parent, i18n("No elements to feed."));
87         return;
88     }
89 
90     // guessing the collection name
91 
92     VirtualFileSystem virtFilesystem;
93     if (!virtFilesystem.scanDir(QUrl("virt:/")))
94         return;
95 
96     KConfigGroup group(krConfig, "Synchronize");
97     int listBoxNum = group.readEntry("Feed To Listbox Counter", 1);
98     QString queryName;
99     do {
100         queryName = i18n("Synchronize results") + QString(" %1").arg(listBoxNum++);
101     } while (virtFilesystem.getFileItem(queryName) != 0);
102     group.writeEntry("Feed To Listbox Counter", listBoxNum);
103 
104     // creating the widget
105 
106     QLabel *label = new QLabel(i18n("Here you can name the file collection"), this);
107     mainLayout->addWidget(label);
108 
109     lineEdit = new QLineEdit(this);
110     lineEdit->setText(queryName);
111     lineEdit->setClearButtonEnabled(true);
112     lineEdit->selectAll();
113     mainLayout->addWidget(lineEdit);
114 
115     QHBoxLayout * hbox = new QHBoxLayout;
116 
117     QLabel *label2 = new QLabel(i18n("Side to feed:"), this);
118     label2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
119     hbox->addWidget(label2);
120 
121     sideCombo = new QComboBox(this);
122     sideCombo->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
123     sideCombo->addItem(i18n("Left"));
124     sideCombo->addItem(i18n("Right"));
125     sideCombo->addItem(i18n("Both"));
126     hbox->addWidget(sideCombo);
127 
128     if (leftExistingNum == 0) {
129         sideCombo->setCurrentIndex(1);
130         sideCombo->setEnabled(false);
131     } else if (rightExistingNum == 0) {
132         sideCombo->setCurrentIndex(0);
133         sideCombo->setEnabled(false);
134     } else
135         sideCombo->setCurrentIndex(2);
136 
137     QFrame *line = new QFrame(this);
138     line->setFrameStyle(QFrame::VLine | QFrame::Sunken);
139     line->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
140     hbox->addWidget(line);
141 
142     cbSelected = new QCheckBox(i18n("Selected files only"), this);
143     cbSelected->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
144     cbSelected->setEnabled(selectedNum != 0);
145     cbSelected->setChecked(selectedNum != 0);
146     hbox->addWidget(cbSelected);
147 
148     mainLayout->addLayout(hbox);
149 
150     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
151     mainLayout->addWidget(buttonBox);
152 
153     QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
154     okButton->setDefault(true);
155     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
156 
157     connect(buttonBox, SIGNAL(accepted()), SLOT(slotOk()));
158     connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));
159 
160     exec();
161 }
162 
slotOk()163 void FeedToListBoxDialog::slotOk()
164 {
165     int side = sideCombo->currentIndex();
166     bool selected = cbSelected->isChecked();
167     QString name = lineEdit->text();
168     QList<QUrl> urlList;
169 
170     QTreeWidgetItemIterator it(syncList);
171     for (;*it; it++) {
172         SynchronizerGUI::SyncViewItem *item = (SynchronizerGUI::SyncViewItem *) * it;
173         SynchronizerFileItem *syncItem = item->synchronizerItemRef();
174 
175         if (!syncItem || !syncItem->isMarked())
176             continue;
177         if (selected && !item->isSelected())
178             continue;
179         if (!equalAllowed && syncItem->task() == TT_EQUALS && (!selected || !item->isSelected()))
180             continue;
181 
182         if ((side == S_BOTH || side == S_LEFT) && syncItem->existsInLeft()) {
183             QString leftDirName = syncItem->leftDirectory().isEmpty() ? "" : syncItem->leftDirectory() + '/';
184             QUrl leftURL = Synchronizer::fsUrl(synchronizer->leftBaseDirectory() + leftDirName + syncItem->leftName());
185             urlList.push_back(leftURL);
186         }
187 
188         if ((side == S_BOTH || side == S_RIGHT) && syncItem->existsInRight()) {
189             QString rightDirName = syncItem->rightDirectory().isEmpty() ? "" : syncItem->rightDirectory() + '/';
190             QUrl leftURL = Synchronizer::fsUrl(synchronizer->rightBaseDirectory() + rightDirName + syncItem->rightName());
191             urlList.push_back(leftURL);
192         }
193     }
194 
195     QUrl url = QUrl(QString("virt:/") + name);
196     VirtualFileSystem virtFilesystem;
197     if (!virtFilesystem.refresh(url)) { // create directory if it does not exist
198         KMessageBox::error(parentWidget(), i18n("Cannot open %1.", url.toDisplayString()));
199         return;
200     }
201     virtFilesystem.addFiles(urlList);
202     ACTIVE_MNG->slotNewTab(url);
203     accepted = true;
204     accept();
205 }
206 
207