File indexing completed on 2023-12-03 07:53:38
0001 /*************************************************************************** 0002 csaveablelist.cpp - ancestor of alias/trigger handler 0003 This file is a part of KMuddy distribution. 0004 ------------------- 0005 begin : Ne sep 8 2002 0006 copyright : (C) 2002 by Tomas Mecir 0007 email : kmuddy@kmuddy.com 0008 ***************************************************************************/ 0009 0010 /*************************************************************************** 0011 * * 0012 * This program is free software; you can redistribute it and/or modify * 0013 * it under the terms of the GNU General Public License as published by * 0014 * the Free Software Foundation; either version 2 of the License, or * 0015 * (at your option) any later version. * 0016 * * 0017 ***************************************************************************/ 0018 0019 #include "csaveablelist.h" 0020 0021 #include <kconfig.h> 0022 #include <kconfiggroup.h> 0023 0024 cSaveableList::cSaveableList (QString file, QString _objName, cSaveableField *proto) 0025 { 0026 _count = 0; 0027 first = nullptr; 0028 last = nullptr; 0029 cur = nullptr; 0030 marker = nullptr; 0031 objName = _objName; 0032 prototype = proto; 0033 0034 config = new KConfig (file); 0035 load (); 0036 } 0037 0038 cSaveableList::~cSaveableList () 0039 { 0040 clear (); 0041 if (config != nullptr) 0042 delete config; 0043 } 0044 0045 bool cSaveableList::addToBegin (cSaveableField *newitem) 0046 { 0047 if (newitem == nullptr) 0048 return false; 0049 if (first == nullptr) //list is still empty 0050 { 0051 first = newitem; 0052 last = newitem; 0053 newitem->prev = nullptr; 0054 newitem->next = nullptr; 0055 cur = nullptr; 0056 _count = 1; 0057 } 0058 else 0059 { 0060 first->prev = newitem; 0061 newitem->next = first; 0062 newitem->prev = nullptr; 0063 first = newitem; 0064 _count++; 0065 } 0066 return true; 0067 } 0068 0069 bool cSaveableList::addToEnd (cSaveableField *newitem) 0070 { 0071 if (newitem == nullptr) 0072 return false; 0073 if (last == nullptr) //list is still empty 0074 { 0075 first = newitem; 0076 last = newitem; 0077 newitem->prev = nullptr; 0078 newitem->next = nullptr; 0079 cur = nullptr; 0080 _count = 1; 0081 } 0082 else 0083 { 0084 last->next = newitem; 0085 newitem->next = nullptr; 0086 newitem->prev = last; 0087 last = newitem; 0088 _count++; 0089 } 0090 return true; 0091 } 0092 0093 bool cSaveableList::addAfterCurrent (cSaveableField *newitem) 0094 { 0095 if (newitem == nullptr) 0096 return false; 0097 if (cur == nullptr) //there is no current entry!!! 0098 return false; 0099 else 0100 { 0101 newitem->next = cur->next; 0102 newitem->prev = cur; 0103 if (cur->next != nullptr) 0104 cur->next->prev = newitem; 0105 cur->next = newitem; 0106 _count++; 0107 } 0108 return true; 0109 } 0110 0111 bool cSaveableList::addAfterMarker (cSaveableField *newitem) 0112 { 0113 if (newitem == nullptr) 0114 return false; 0115 if (marker == nullptr) //there is no marked entry!!! 0116 return false; 0117 else 0118 { 0119 newitem->next = marker->next; 0120 newitem->prev = marker; 0121 if (marker->next != nullptr) 0122 marker->next->prev = newitem; 0123 marker->next = newitem; 0124 _count++; 0125 } 0126 return true; 0127 } 0128 0129 bool cSaveableList::replaceCurrent (cSaveableField *newitem) 0130 { 0131 if (newitem == nullptr) 0132 return false; 0133 if (cur == nullptr) //there is no current entry!!! 0134 return false; 0135 else 0136 { 0137 //bind new item 0138 newitem->next = cur->next; 0139 newitem->prev = cur->prev; 0140 if (cur->next != nullptr) 0141 cur->next->prev = newitem; 0142 if (cur->prev != nullptr) 0143 cur->prev->next = newitem; 0144 if (first == cur) 0145 first = newitem; 0146 if (last == cur) 0147 last = newitem; 0148 if (marker == cur) 0149 marker = newitem; //marked item gets updated as well...7 0150 //get rid of the old one 0151 delete cur; 0152 cur = newitem; 0153 //_count is NOT modified 0154 } 0155 return true; 0156 } 0157 0158 bool cSaveableList::replaceMarker (cSaveableField *newitem) 0159 { 0160 if (newitem == nullptr) 0161 return false; 0162 if (marker == nullptr) //there is no marked entry!!! 0163 return false; 0164 else 0165 { 0166 //bind new item 0167 newitem->next = marker->next; 0168 newitem->prev = marker->prev; 0169 if (marker->next != nullptr) 0170 marker->next->prev = newitem; 0171 if (marker->prev != nullptr) 0172 marker->prev->next = newitem; 0173 if (first == marker) 0174 first = newitem; 0175 if (last == marker) 0176 last = newitem; 0177 if (cur == marker) 0178 cur = newitem; //current item gets updated as well... 0179 //get rid of the old one 0180 delete marker; 0181 marker = newitem; 0182 //_count is NOT modified 0183 } 0184 return true; 0185 } 0186 0187 bool cSaveableList::moveCurrentToFront () 0188 //only moves by one item 0189 { 0190 if (cur == nullptr) 0191 return false; 0192 if (cur->prev == nullptr) 0193 return true; //this is the begin - there's nothing to do 0194 0195 //store pointers 0196 cSaveableField *prev = cur->prev; 0197 cSaveableField *prev2 = cur->prev->prev; 0198 cSaveableField *next = cur->next; 0199 0200 //bind cur 0201 cur->next = prev; 0202 cur->prev = prev2; 0203 //bind next 0204 if (next != nullptr) 0205 next->prev = prev; 0206 //bind prev 0207 prev->next = next; 0208 prev->prev = cur; 0209 //bind prev2 0210 if (prev2 != nullptr) 0211 prev2->next = cur; 0212 0213 //update first/last if needed 0214 if (first == prev) 0215 first = cur; 0216 if (last == cur) 0217 last = prev; 0218 0219 return true; 0220 } 0221 0222 bool cSaveableList::moveCurrentToBack () 0223 //only moves by one item 0224 { 0225 if (cur == nullptr) 0226 return false; 0227 if (cur->next == nullptr) 0228 return true; //this is the end - there's nothing to do 0229 0230 //store pointers 0231 cSaveableField *prev = cur->prev; 0232 cSaveableField *next = cur->next; 0233 cSaveableField *next2 = cur->next->next; 0234 0235 //bind cur 0236 cur->next = next2; 0237 cur->prev = next; 0238 //bind prev 0239 if (prev != nullptr) 0240 prev->next = next; 0241 //bind next 0242 next->prev = prev; 0243 next->next = cur; 0244 //bind next2 0245 if (next2 != nullptr) 0246 next2->prev = cur; 0247 0248 //update first/last if needed 0249 if (first == cur) 0250 first = next; 0251 if (last == next) 0252 last = cur; 0253 0254 return true; 0255 } 0256 0257 bool cSaveableList::removeFirst (bool dontDelete) 0258 { 0259 if (first == nullptr) //there's nothing to remove! 0260 return false; 0261 if (first->next != nullptr) 0262 first->next->prev = nullptr; 0263 cSaveableField *newfirst = first->next; 0264 if (cur == first) 0265 cur = newfirst; 0266 if (last == first) 0267 last = nullptr; //newfirst==NULL in this case, but I want to be sure... 0268 if (marker == first) 0269 marker = newfirst; 0270 if (!dontDelete) 0271 delete first; 0272 first = newfirst; 0273 _count--; 0274 return true; 0275 } 0276 0277 bool cSaveableList::removeLast (bool dontDelete) 0278 { 0279 if (last == nullptr) //there's nothing to remove! 0280 return false; 0281 if (last->prev != nullptr) 0282 last->prev->next = nullptr; 0283 cSaveableField *newlast = last->prev; 0284 if (cur == last) 0285 cur = newlast; 0286 if (first == last) 0287 first = nullptr; //newlast==NULL in this case, but I want to be sure... 0288 if (marker == last) 0289 marker = newlast; 0290 if (!dontDelete) 0291 delete last; 0292 last = newlast; 0293 _count--; 0294 return true; 0295 } 0296 0297 bool cSaveableList::removeCurrent (bool dontDelete) 0298 { 0299 if (cur == nullptr) //there's nothing to remove! 0300 return false; 0301 0302 if (cur->prev != nullptr) 0303 cur->prev->next = cur->next; 0304 if (cur->next != nullptr) 0305 cur->next->prev = cur->prev; 0306 if (marker == cur) 0307 marker = nullptr; 0308 if (first == cur) 0309 first = cur->next; 0310 if (last == cur) 0311 last = cur->prev; 0312 if (!dontDelete) 0313 delete cur; 0314 cur = nullptr; 0315 _count--; 0316 return true; 0317 } 0318 0319 bool cSaveableList::removeMarked (bool dontDelete) 0320 { 0321 if (marker == nullptr) //there's nothing to remove! 0322 return false; 0323 0324 if (marker->prev != nullptr) 0325 marker->prev->next = marker->next; 0326 if (marker->next != nullptr) 0327 marker->next->prev = marker->prev; 0328 if (cur == marker) 0329 cur = nullptr; 0330 if (first == marker) 0331 first = marker->next; 0332 if (last == marker) 0333 last = marker->prev; 0334 if (!dontDelete) 0335 delete marker; 0336 marker = nullptr; 0337 _count--; 0338 return true; 0339 } 0340 0341 void cSaveableList::clear (bool dontDelete) 0342 { 0343 while (last != nullptr) 0344 removeFirst (dontDelete); 0345 _count = 0; 0346 } 0347 0348 void cSaveableList::load () 0349 { 0350 if (config == nullptr) 0351 return; 0352 clear (); 0353 KConfigGroup g = config->group ("General"); 0354 int num = g.readEntry ("Count", 0); 0355 for (int i = 1; i <= num; i++) 0356 { 0357 QString groupname = objName + " " + QString::number (i); 0358 cSaveableField *field = prototype->newInstance (); 0359 field->load (config, groupname); 0360 addToEnd (field); 0361 } 0362 } 0363 0364