1 | /*************************************************************************** |
2 | * Copyright 2009 Ben Cooksley <ben@eclipse.endoftheinternet.org> * |
3 | * * |
4 | * This program is free software; you can redistribute it and/or modify * |
5 | * it under the terms of the GNU General Public License as published by * |
6 | * the Free Software Foundation; either version 2 of the License, or * |
7 | * (at your option) any later version. * |
8 | * * |
9 | * This program is distributed in the hope that it will be useful, * |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
12 | * GNU General Public License for more details. * |
13 | * * |
14 | * You should have received a copy of the GNU General Public License * |
15 | * along with this program; if not, write to the * |
16 | * Free Software Foundation, Inc., * |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * |
18 | ***************************************************************************/ |
19 | |
20 | #include "PredicateItem.h" |
21 | |
22 | #include "ActionEditor.h" |
23 | |
24 | #include <QList> |
25 | #include <QString> |
26 | |
27 | #include <KLocale> |
28 | #include <Solid/Predicate> |
29 | |
30 | class PredicateItem::Private { |
31 | |
32 | public: |
33 | Private() {} |
34 | |
35 | PredicateItem * parent; |
36 | QList<PredicateItem*> children; |
37 | |
38 | }; |
39 | |
40 | PredicateItem::PredicateItem( Solid::Predicate item, PredicateItem * itsParent ) |
41 | : d( new Private() ) |
42 | { |
43 | d->parent = itsParent; |
44 | |
45 | if ( d->parent ) { |
46 | d->parent->children().append( this ); |
47 | } |
48 | |
49 | // Read data from Solid::Predicate |
50 | itemType = item.type(); |
51 | ifaceType = item.interfaceType(); |
52 | property = item.propertyName(); |
53 | value = item.matchingValue(); |
54 | compOperator = item.comparisonOperator(); |
55 | |
56 | if( itemType == Solid::Predicate::Disjunction || itemType == Solid::Predicate::Conjunction ) { |
57 | PredicateItem * child = new PredicateItem( item.firstOperand(), this ); |
58 | PredicateItem * child2 = new PredicateItem( item.secondOperand(), this ); |
59 | Q_UNUSED( child ) |
60 | Q_UNUSED( child2 ) |
61 | } |
62 | // We're now ready, no need to keep the Solid::Predicate for now |
63 | } |
64 | |
65 | PredicateItem::~PredicateItem() |
66 | { |
67 | qDeleteAll( d->children ); |
68 | d->children.clear(); |
69 | delete d; |
70 | } |
71 | |
72 | PredicateItem * PredicateItem::child( int index ) const |
73 | { |
74 | return d->children.at(index); |
75 | } |
76 | |
77 | PredicateItem * PredicateItem::parent() const |
78 | { |
79 | return d->parent; |
80 | } |
81 | |
82 | QList<PredicateItem*>& PredicateItem::children() const |
83 | { |
84 | return d->children; |
85 | } |
86 | |
87 | Solid::Predicate PredicateItem::predicate() const |
88 | { |
89 | Solid::Predicate item; |
90 | |
91 | switch( itemType ) { |
92 | case Solid::Predicate::InterfaceCheck: |
93 | item = Solid::Predicate( ifaceType ); |
94 | break; |
95 | case Solid::Predicate::Conjunction: |
96 | item = children().at(0)->predicate() & children().at(1)->predicate(); |
97 | break; |
98 | case Solid::Predicate::Disjunction: |
99 | item = children().at(0)->predicate() | children().at(1)->predicate(); |
100 | break; |
101 | default: |
102 | break; |
103 | } |
104 | |
105 | if( itemType != Solid::Predicate::PropertyCheck ) { |
106 | return item; |
107 | } |
108 | |
109 | switch( compOperator ) { |
110 | case Solid::Predicate::Equals: |
111 | item = Solid::Predicate( ifaceType, property, value ); |
112 | break; |
113 | case Solid::Predicate::Mask: |
114 | item = Solid::Predicate( ifaceType, property, value, Solid::Predicate::Mask ); |
115 | break; |
116 | default: |
117 | break; |
118 | } |
119 | |
120 | return item; |
121 | } |
122 | |
123 | QString PredicateItem::prettyName() const |
124 | { |
125 | QString typeName; |
126 | QString compName; |
127 | |
128 | QString deviceName; |
129 | switch( itemType ) { |
130 | case Solid::Predicate::InterfaceCheck: |
131 | deviceName = SolidActionData::instance()->nameFromInterface(ifaceType); |
132 | typeName = i18n("The device must be of the type %1" , deviceName); |
133 | break; |
134 | case Solid::Predicate::Disjunction: |
135 | typeName = i18n("Any of the contained properties must match" ); |
136 | break; |
137 | case Solid::Predicate::Conjunction: |
138 | typeName = i18n("All of the contained properties must match" ); |
139 | break; |
140 | default: |
141 | break; |
142 | } |
143 | |
144 | QString prettyProperty = SolidActionData::instance()->propertyName( ifaceType, property ); |
145 | switch( compOperator ) { |
146 | case Solid::Predicate::Equals: |
147 | compName = i18n("The device property %1 must equal %2" , prettyProperty, value.toString()); |
148 | break; |
149 | case Solid::Predicate::Mask: |
150 | compName = i18n("The device property %1 must contain %2" , prettyProperty, value.toString()); |
151 | break; |
152 | default: |
153 | break; |
154 | } |
155 | |
156 | if( itemType == Solid::Predicate::PropertyCheck ) { |
157 | return compName; |
158 | } |
159 | return typeName; |
160 | } |
161 | |
162 | void PredicateItem::setTypeByInt( int item ) |
163 | { |
164 | Solid::Predicate::Type iType = Solid::Predicate::InterfaceCheck; |
165 | switch( item ) { |
166 | case Solid::Predicate::PropertyCheck: |
167 | iType = Solid::Predicate::PropertyCheck; |
168 | break; |
169 | case Solid::Predicate::Conjunction: |
170 | iType = Solid::Predicate::Conjunction; |
171 | break; |
172 | case Solid::Predicate::Disjunction: |
173 | iType = Solid::Predicate::Disjunction; |
174 | break; |
175 | case Solid::Predicate::InterfaceCheck: |
176 | iType = Solid::Predicate::InterfaceCheck; |
177 | break; |
178 | default: |
179 | break; |
180 | } |
181 | itemType = iType; |
182 | } |
183 | |
184 | void PredicateItem::setComparisonByInt( int item ) |
185 | { |
186 | switch( item ) { |
187 | case Solid::Predicate::Equals: |
188 | compOperator = Solid::Predicate::Equals; |
189 | break; |
190 | case Solid::Predicate::Mask: |
191 | compOperator = Solid::Predicate::Mask; |
192 | break; |
193 | default: |
194 | break; |
195 | } |
196 | } |
197 | |
198 | void PredicateItem::updateChildrenStatus() |
199 | { |
200 | if( itemType != Solid::Predicate::Disjunction && itemType != Solid::Predicate::Conjunction ) { |
201 | qDeleteAll( d->children ); |
202 | d->children.clear(); |
203 | } else if( d->children.count() == 0 ) { |
204 | Solid::Predicate templItem = Solid::Predicate::fromString("IS StorageVolume" ); |
205 | new PredicateItem( templItem, this ); |
206 | new PredicateItem( templItem, this ); |
207 | } |
208 | } |
209 | |