File indexing completed on 2024-05-12 05:44:24

0001 /***************************************************************************
0002  *   Copyright (C) 2007 by Rajko Albrecht  ral@alwins-world.de             *
0003  *   https://kde.org/applications/development/org.kde.kdesvn               *
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 #include "propertylist.h"
0021 #include "kmultilinedelegate.h"
0022 #include "svnfrontend/fronthelpers/propertyitem.h"
0023 
0024 #include <KLocalizedString>
0025 #include <KMessageBox>
0026 #include <QTimer>
0027 
0028 Propertylist::Propertylist(QWidget *parent)
0029     : QTreeWidget(parent)
0030     , m_commitit(false)
0031     , m_Dir(false)
0032 {
0033     setItemDelegate(new KMultilineDelegate(this));
0034     QTimer::singleShot(0, this, &Propertylist::init);
0035 }
0036 
0037 Propertylist::~Propertylist()
0038 {
0039 }
0040 
0041 void Propertylist::init()
0042 {
0043     headerItem()->setText(0, i18n("Property"));
0044     headerItem()->setText(1, i18n("Value"));
0045     setAllColumnsShowFocus(true);
0046     setRootIsDecorated(false);
0047     sortItems(0, Qt::AscendingOrder);
0048     setAcceptDrops(false);
0049     setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
0050     setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
0051     setContextMenuPolicy(Qt::ActionsContextMenu);
0052     connect(this, &QTreeWidget::itemChanged, this, &Propertylist::slotItemChanged, Qt::UniqueConnection);
0053     resizeColumnToContents(0);
0054 }
0055 
0056 void Propertylist::displayList(const svn::PathPropertiesMapListPtr &propList, bool editable, bool isDir, const QString &aCur)
0057 {
0058     disconnect(this, &QTreeWidget::itemChanged, this, &Propertylist::slotItemChanged);
0059     viewport()->setUpdatesEnabled(false);
0060     clear();
0061     m_Dir = isDir;
0062     if (propList) {
0063         m_current = aCur;
0064         if (!propList->isEmpty()) {
0065             /* just want the first one */
0066             const svn::PropertiesMap pmap = propList->at(0).second;
0067             for (auto pit = pmap.constBegin(); pit != pmap.constEnd(); ++pit) {
0068                 PropertyListViewItem *ki = new PropertyListViewItem(this, pit.key(), pit.value());
0069                 if (editable && !PropertyListViewItem::protected_Property(ki->currentName())) {
0070                     ki->setFlags(ki->flags() | Qt::ItemIsEditable);
0071                 }
0072             }
0073         }
0074     }
0075     viewport()->setUpdatesEnabled(true);
0076     viewport()->repaint();
0077     connect(this, &QTreeWidget::itemChanged, this, &Propertylist::slotItemChanged, Qt::UniqueConnection);
0078     resizeColumnToContents(0);
0079 }
0080 
0081 void Propertylist::clear()
0082 {
0083     QTreeWidget::clear();
0084 }
0085 
0086 /*!
0087     \fn PropertiesDlg::slotItemRenamed(QListViewItem*item,const QString & str,int col )
0088  */
0089 void Propertylist::slotItemChanged(QTreeWidgetItem *_item, int col)
0090 {
0091     if (!_item || _item->type() != PropertyListViewItem::_RTTI_) {
0092         return;
0093     }
0094     PropertyListViewItem *item = static_cast<PropertyListViewItem *>(_item);
0095     QString text = item->text(col);
0096 
0097     if (text.isEmpty() && col == 0) {
0098         item->setText(0, item->currentName());
0099         return;
0100     }
0101     bool fail = false;
0102     disconnect(this, &QTreeWidget::itemChanged, this, &Propertylist::slotItemChanged);
0103     if (PropertyListViewItem::protected_Property(item->text(0)) || PropertyListViewItem::protected_Property(item->currentName())) {
0104         KMessageBox::error(this, i18n("This property may not set by users.\nRejecting it."), i18n("Protected property"));
0105         item->setText(0, item->currentName());
0106         item->setText(1, item->currentValue());
0107         fail = true;
0108     } else if (checkExisting(item->text(0), item)) {
0109         KMessageBox::error(this, i18n("A property with that name exists.\nRejecting it."), i18n("Double property"));
0110         item->setText(0, item->currentName());
0111         item->setText(1, item->currentValue());
0112         fail = true;
0113     }
0114     connect(this, &QTreeWidget::itemChanged, this, &Propertylist::slotItemChanged);
0115     if (fail) {
0116         return;
0117     }
0118 
0119     if (col == 0) {
0120         item->checkName();
0121     } else {
0122         item->checkValue();
0123     }
0124     if (commitchanges() && item->different()) {
0125         svn::PropertiesMap pm;
0126         QStringList dels;
0127         pm[item->currentName()] = item->currentValue();
0128         if (item->currentName() != item->startName()) {
0129             dels.push_back(item->startName());
0130         }
0131         emit sigSetProperty(pm, dels, m_current);
0132     }
0133 }
0134 
0135 bool Propertylist::checkExisting(const QString &aName, QTreeWidgetItem *it)
0136 {
0137     if (!it) {
0138         return !findItems(aName, Qt::MatchExactly | Qt::MatchRecursive, 0).isEmpty();
0139     }
0140     QTreeWidgetItemIterator iter(this);
0141     while (*iter) {
0142         if ((*iter) == it) {
0143             ++iter;
0144             continue;
0145         }
0146         if ((*iter)->text(0) == aName) {
0147             return true;
0148         }
0149         ++iter;
0150     }
0151     return false;
0152 }
0153 
0154 #include "moc_propertylist.cpp"