File indexing completed on 2024-05-12 17:16:14

0001 /***************************************************************************
0002  *   Copyright (C) 2006-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 
0021 #include "propertiesdlg.h"
0022 #include "ui_propertiesdlg.h"
0023 
0024 #include "svnfrontend/fronthelpers/propertyitem.h"
0025 #include "svnfrontend/fronthelpers/propertylist.h"
0026 #include "editpropsdlg.h"
0027 #include "svnitem.h"
0028 #include "svnqt/client.h"
0029 
0030 #include <KLocalizedString>
0031 #include <KMessageBox>
0032 
0033 PropertiesDlg::PropertiesDlg(SvnItem *which, const svn::ClientP &aClient, const svn::Revision &aRev, QWidget *parent)
0034     : KSvnDialog(QLatin1String("properties_dlg"), parent)
0035     , m_Item(which)
0036     , m_Client(aClient)
0037     , m_Rev(aRev)
0038     , m_ui(new Ui::PropertiesDlg)
0039 {
0040     m_ui->setupUi(this);
0041     setDefaultButton(m_ui->buttonBox->button(QDialogButtonBox::Ok));
0042     connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0043     connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0044     connect(m_ui->buttonBox, &QDialogButtonBox::helpRequested, this, &PropertiesDlg::slotHelp);
0045 
0046     m_ui->tvPropertyList->setAllColumnsShowFocus(true);
0047     m_ui->tvPropertyList->setCommitchanges(false);
0048 
0049     // signals and slots connections
0050     connect(m_ui->pbAdd, &QAbstractButton::clicked, this, &PropertiesDlg::slotAdd);
0051     connect(m_ui->pbModify, &QAbstractButton::clicked, this, &PropertiesDlg::slotModify);
0052     connect(m_ui->pbDelete, &QAbstractButton::clicked, this, &PropertiesDlg::slotDelete);
0053 
0054     connect(m_ui->tvPropertyList,
0055             &QTreeWidget::currentItemChanged,
0056             this,
0057             &PropertiesDlg::slotCurrentItemChanged);
0058     if (!m_Client) {
0059         m_ui->tvPropertyList->setEnabled(false);
0060     }
0061     slotCurrentItemChanged(nullptr);
0062     initItem();
0063 }
0064 
0065 PropertiesDlg::~PropertiesDlg()
0066 {
0067     delete m_ui;
0068 }
0069 
0070 void PropertiesDlg::slotHelp()
0071 {
0072     qWarning("PropertiesDlg::slotHelp(): Not implemented yet");
0073 }
0074 
0075 void PropertiesDlg::slotCurrentItemChanged(QTreeWidgetItem *item)
0076 {
0077     m_ui->pbDelete->setEnabled(item != nullptr);
0078     m_ui->pbModify->setEnabled(item != nullptr);
0079     if (!item || item->type() != PropertyListViewItem::_RTTI_) {
0080         return;
0081     }
0082     PropertyListViewItem *ki = static_cast<PropertyListViewItem *>(item);
0083     if (PropertyListViewItem::protected_Property(ki->currentName())) {
0084         m_ui->pbDelete->setEnabled(false);
0085         m_ui->pbModify->setEnabled(false);
0086         return;
0087     }
0088     if (ki->deleted()) {
0089         m_ui->pbDelete->setText(i18n("Undelete property"));
0090     } else {
0091         m_ui->pbDelete->setText(i18n("Delete property"));
0092     }
0093 }
0094 
0095 void PropertiesDlg::initItem()
0096 {
0097     if (!m_Client) {
0098         QString ex = i18n("Missing SVN link");
0099         emit clientException(ex);
0100         return;
0101     }
0102     svn::Path what(m_Item->fullName());
0103     svn::PathPropertiesMapListPtr propList;
0104     try {
0105         propList = m_Client->proplist(what, m_Rev, m_Rev);
0106     } catch (const svn::ClientException &e) {
0107         emit clientException(e.msg());
0108         return;
0109     }
0110     m_ui->tvPropertyList->displayList(propList, true, m_Item->isDir(), m_Item->fullName());
0111 }
0112 
0113 void PropertiesDlg::slotAdd()
0114 {
0115     QPointer<EditPropsDlg> dlg(new EditPropsDlg(true, this));
0116     dlg->setDir(m_Item->isDir());
0117 
0118     if (dlg->exec() == QDialog::Accepted) {
0119         if (PropertyListViewItem::protected_Property(dlg->propName())) {
0120             KMessageBox::error(this, i18n("This property may not set by users.\nRejecting it."), i18n("Protected property"));
0121             return;
0122         }
0123         if (m_ui->tvPropertyList->checkExisting(dlg->propName())) {
0124             KMessageBox::error(this, i18n("A property with that name exists.\nRejecting it."), i18n("Double property"));
0125             return;
0126         }
0127         if (!dlg->propName().isEmpty()) {
0128             PropertyListViewItem *item = new PropertyListViewItem(m_ui->tvPropertyList);
0129             item->setName(dlg->propName());
0130             item->setValue(dlg->propValue());
0131         }
0132     }
0133     delete dlg;
0134 }
0135 
0136 void PropertiesDlg::slotDelete()
0137 {
0138     QTreeWidgetItem *qi = m_ui->tvPropertyList->currentItem();
0139     if (!qi) {
0140         return;
0141     }
0142     PropertyListViewItem *ki = static_cast<PropertyListViewItem *>(qi);
0143     if (PropertyListViewItem::protected_Property(ki->currentName())) {
0144         return;
0145     }
0146     if (ki->deleted()) {
0147         ki->unDeleteIt();
0148     } else {
0149         ki->deleteIt();
0150     }
0151     slotCurrentItemChanged(qi);
0152 }
0153 
0154 void PropertiesDlg::slotModify()
0155 {
0156     QTreeWidgetItem *qi = m_ui->tvPropertyList->currentItem();
0157     if (!qi) {
0158         return;
0159     }
0160     PropertyListViewItem *ki = static_cast<PropertyListViewItem *>(qi);
0161     if (PropertyListViewItem::protected_Property(ki->currentName())) {
0162         return;
0163     }
0164     QPointer<EditPropsDlg> dlg(new EditPropsDlg(false, this));
0165     dlg->setDir(m_Item->isDir());
0166     dlg->setPropName(ki->currentName());
0167     dlg->setPropValue(ki->currentValue());
0168 
0169     if (dlg->exec() == QDialog::Accepted) {
0170         if (PropertyListViewItem::protected_Property(dlg->propName())) {
0171             KMessageBox::error(this, i18n("This property may not set by users.\nRejecting it."), i18n("Protected property"));
0172             return;
0173         }
0174         if (m_ui->tvPropertyList->checkExisting(dlg->propName(), qi)) {
0175             KMessageBox::error(this, i18n("A property with that name exists.\nRejecting it."), i18n("Double property"));
0176             return;
0177         }
0178         ki->setName(dlg->propName());
0179         ki->setValue(dlg->propValue());
0180     }
0181     delete dlg;
0182 }
0183 
0184 void PropertiesDlg::changedItems(svn::PropertiesMap &toSet, QStringList &toDelete)
0185 {
0186     toSet.clear();
0187     toDelete.clear();
0188     QTreeWidgetItemIterator iter(m_ui->tvPropertyList);
0189     while (*iter) {
0190         PropertyListViewItem *ki = static_cast<PropertyListViewItem *>((*iter));
0191         ++iter;
0192         if (PropertyListViewItem::protected_Property(ki->currentName()) ||
0193                 PropertyListViewItem::protected_Property(ki->startName())) {
0194             continue;
0195         }
0196         if (ki->deleted()) {
0197             toDelete.push_back(ki->currentName());
0198         } else if (ki->currentName() != ki->startName()) {
0199             toDelete.push_back(ki->startName());
0200             toSet[ki->currentName()] = ki->currentValue();
0201         } else if (ki->currentValue() != ki->startValue()) {
0202             toSet[ki->currentName()] = ki->currentValue();
0203         }
0204     }
0205 }