File indexing completed on 2024-05-12 17:07:26

0001 /*
0002     SPDX-FileCopyrightText: 2009 Ben Cooksley <ben@eclipse.endoftheinternet.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "PredicateItem.h"
0008 
0009 #include "ActionEditor.h"
0010 
0011 #include <QList>
0012 #include <QString>
0013 
0014 #include <KLocalizedString>
0015 
0016 class PredicateItem::Private
0017 {
0018 public:
0019     Private()
0020     {
0021     }
0022 
0023     PredicateItem *parent;
0024     QList<PredicateItem *> children;
0025 };
0026 
0027 PredicateItem::PredicateItem(Solid::Predicate item, PredicateItem *itsParent)
0028     : d(new Private())
0029 {
0030     d->parent = itsParent;
0031 
0032     if (d->parent) {
0033         d->parent->children().append(this);
0034     }
0035 
0036     // Read data from Solid::Predicate
0037     itemType = item.type();
0038     ifaceType = item.interfaceType();
0039     property = item.propertyName();
0040     value = item.matchingValue();
0041     compOperator = item.comparisonOperator();
0042 
0043     if (itemType == Solid::Predicate::Disjunction || itemType == Solid::Predicate::Conjunction) {
0044         PredicateItem *child = new PredicateItem(item.firstOperand(), this);
0045         PredicateItem *child2 = new PredicateItem(item.secondOperand(), this);
0046         Q_UNUSED(child)
0047         Q_UNUSED(child2)
0048     }
0049     // We're now ready, no need to keep the Solid::Predicate for now
0050 }
0051 
0052 PredicateItem::~PredicateItem()
0053 {
0054     qDeleteAll(d->children);
0055     d->children.clear();
0056     delete d;
0057 }
0058 
0059 PredicateItem *PredicateItem::child(int index) const
0060 {
0061     return d->children.at(index);
0062 }
0063 
0064 PredicateItem *PredicateItem::parent() const
0065 {
0066     return d->parent;
0067 }
0068 
0069 QList<PredicateItem *> &PredicateItem::children() const
0070 {
0071     return d->children;
0072 }
0073 
0074 Solid::Predicate PredicateItem::predicate() const
0075 {
0076     Solid::Predicate item;
0077 
0078     switch (itemType) {
0079     case Solid::Predicate::InterfaceCheck:
0080         item = Solid::Predicate(ifaceType);
0081         break;
0082     case Solid::Predicate::Conjunction:
0083         item = children().at(0)->predicate() & children().at(1)->predicate();
0084         break;
0085     case Solid::Predicate::Disjunction:
0086         item = children().at(0)->predicate() | children().at(1)->predicate();
0087         break;
0088     default:
0089         break;
0090     }
0091 
0092     if (itemType != Solid::Predicate::PropertyCheck) {
0093         return item;
0094     }
0095 
0096     switch (compOperator) {
0097     case Solid::Predicate::Equals:
0098         item = Solid::Predicate(ifaceType, property, value);
0099         break;
0100     case Solid::Predicate::Mask:
0101         item = Solid::Predicate(ifaceType, property, value, Solid::Predicate::Mask);
0102         break;
0103     default:
0104         break;
0105     }
0106 
0107     return item;
0108 }
0109 
0110 QString PredicateItem::prettyName() const
0111 {
0112     QString typeName;
0113     QString compName;
0114 
0115     QString deviceName;
0116     switch (itemType) {
0117     case Solid::Predicate::InterfaceCheck:
0118         deviceName = SolidActionData::instance()->nameFromInterface(ifaceType);
0119         typeName = i18n("The device must be of the type %1", deviceName);
0120         break;
0121     case Solid::Predicate::Disjunction:
0122         typeName = i18n("Any of the contained properties must match");
0123         break;
0124     case Solid::Predicate::Conjunction:
0125         typeName = i18n("All of the contained properties must match");
0126         break;
0127     default:
0128         break;
0129     }
0130 
0131     QString prettyProperty = SolidActionData::instance()->propertyName(ifaceType, property);
0132     switch (compOperator) {
0133     case Solid::Predicate::Equals:
0134         compName = i18n("The device property %1 must equal %2", prettyProperty, value.toString());
0135         break;
0136     case Solid::Predicate::Mask:
0137         compName = i18n("The device property %1 must contain %2", prettyProperty, value.toString());
0138         break;
0139     default:
0140         break;
0141     }
0142 
0143     if (itemType == Solid::Predicate::PropertyCheck) {
0144         return compName;
0145     }
0146     return typeName;
0147 }
0148 
0149 void PredicateItem::setTypeByInt(int item)
0150 {
0151     Solid::Predicate::Type iType = Solid::Predicate::InterfaceCheck;
0152     switch (item) {
0153     case Solid::Predicate::PropertyCheck:
0154         iType = Solid::Predicate::PropertyCheck;
0155         break;
0156     case Solid::Predicate::Conjunction:
0157         iType = Solid::Predicate::Conjunction;
0158         break;
0159     case Solid::Predicate::Disjunction:
0160         iType = Solid::Predicate::Disjunction;
0161         break;
0162     case Solid::Predicate::InterfaceCheck:
0163         iType = Solid::Predicate::InterfaceCheck;
0164         break;
0165     default:
0166         break;
0167     }
0168     itemType = iType;
0169 }
0170 
0171 void PredicateItem::setComparisonByInt(int item)
0172 {
0173     switch (item) {
0174     case Solid::Predicate::Equals:
0175         compOperator = Solid::Predicate::Equals;
0176         break;
0177     case Solid::Predicate::Mask:
0178         compOperator = Solid::Predicate::Mask;
0179         break;
0180     default:
0181         break;
0182     }
0183 }
0184 
0185 void PredicateItem::updateChildrenStatus()
0186 {
0187     if (itemType != Solid::Predicate::Disjunction && itemType != Solid::Predicate::Conjunction) {
0188         qDeleteAll(d->children);
0189         d->children.clear();
0190     } else if (d->children.count() == 0) {
0191         Solid::Predicate templItem = Solid::Predicate::fromString(QStringLiteral("IS StorageVolume"));
0192         new PredicateItem(templItem, this);
0193         new PredicateItem(templItem, this);
0194     }
0195 }