File indexing completed on 2024-04-28 16:44:52

0001 /*
0002    SPDX-FileCopyrightText: 1999-2001 Lubos Lunak <l.lunak@kde.org>
0003    SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "conditions/conditions_list_base.h"
0009 #include "conditions/conditions_visitor.h"
0010 
0011 #include <KConfig>
0012 #include <KConfigGroup>
0013 
0014 namespace KHotKeys
0015 {
0016 Condition_list_base::Condition_list_base(Condition_list_base *parent_P)
0017     : Condition(parent_P)
0018     , QList<Condition *>()
0019 {
0020 }
0021 
0022 Condition_list_base::Condition_list_base(const QList<Condition *> &children_P, Condition_list_base *parent_P)
0023     : Condition(parent_P)
0024     , QList<Condition *>(children_P)
0025 {
0026 }
0027 
0028 Condition_list_base::Condition_list_base(KConfigGroup &cfg_P, Condition_list_base *parent_P)
0029     : Condition(parent_P)
0030 {
0031     int cnt = cfg_P.readEntry("ConditionsCount", 0);
0032     for (int i = 0; i < cnt; ++i) {
0033         KConfigGroup conditionConfig(cfg_P.config(), cfg_P.name() + QString::number(i));
0034         (void)Condition::create_cfg_read(conditionConfig, this);
0035     }
0036 }
0037 
0038 Condition_list_base::~Condition_list_base()
0039 {
0040     // Carefully delete conditions in a manual loop. Something like qDeleteAll
0041     // would crash because the conditions remove themselves from this list in
0042     // their destructors.
0043     while (!isEmpty()) {
0044         Condition *c = first();
0045         removeAll(c);
0046         delete c;
0047     }
0048 }
0049 
0050 bool Condition_list_base::accepts_children() const
0051 {
0052     return true;
0053 }
0054 
0055 void Condition_list_base::append(Condition *cond)
0056 {
0057     if (cond->parent() == this) {
0058         return;
0059     }
0060 
0061     cond->reparent(this);
0062     QList<Condition *>::append(cond);
0063 }
0064 
0065 Condition_list_base::Iterator Condition_list_base::begin()
0066 {
0067     return QList<Condition *>::begin();
0068 }
0069 
0070 Condition_list_base::ConstIterator Condition_list_base::begin() const
0071 {
0072     return QList<Condition *>::begin();
0073 }
0074 
0075 void Condition_list_base::clear()
0076 {
0077     return QList<Condition *>::clear();
0078 }
0079 
0080 void Condition_list_base::cfg_write(KConfigGroup &cfg_P) const
0081 {
0082     int i = 0;
0083     for (ConstIterator it(begin()); it != end(); ++it, ++i) {
0084         KConfigGroup conditionConfig(cfg_P.config(), cfg_P.name() + QString::number(i));
0085         (*it)->cfg_write(conditionConfig);
0086     }
0087     cfg_P.writeEntry("ConditionsCount", i);
0088 }
0089 
0090 int Condition_list_base::count() const
0091 {
0092     return QList<Condition *>::count();
0093 }
0094 
0095 Condition *Condition_list_base::first()
0096 {
0097     return QList<Condition *>::first();
0098 }
0099 
0100 Condition const *Condition_list_base::first() const
0101 {
0102     return QList<Condition *>::first();
0103 }
0104 
0105 Condition_list_base::Iterator Condition_list_base::end()
0106 {
0107     return QList<Condition *>::end();
0108 }
0109 
0110 Condition_list_base::ConstIterator Condition_list_base::end() const
0111 {
0112     return QList<Condition *>::end();
0113 }
0114 
0115 bool Condition_list_base::isEmpty() const
0116 {
0117     return QList<Condition *>::isEmpty();
0118 }
0119 
0120 int Condition_list_base::removeAll(Condition *const &cond)
0121 {
0122     return QList<Condition *>::removeAll(cond);
0123 }
0124 
0125 void Condition_list_base::visit(ConditionsVisitor *visitor)
0126 {
0127     visitor->visitConditionsListBase(this);
0128 }
0129 
0130 } // namespace KHotKeys