File indexing completed on 2024-04-21 04:04:44
0001 /* 0002 * privacylist.cpp 0003 * Copyright (C) 2006 Remko Troncon 0004 * 0005 * This program is free software; you can redistribute it and/or 0006 * modify it under the terms of the GNU General Public License 0007 * as published by the Free Software Foundation; either version 2 0008 * of the License, or (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 library; if not, write to the Free Software 0017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 0018 * 02110-1301, USA 0019 * 0020 */ 0021 0022 #include <algorithm> 0023 0024 #include <QDomElement> 0025 #include <QStringList> 0026 0027 #include "jabber_protocol_debug.h" 0028 // #include "jabberprotocol.h" 0029 0030 #include "privacylist.h" 0031 0032 #define ORDER_INCREMENT 10 0033 0034 PrivacyList::PrivacyList(const QString& name, const QList<PrivacyListItem>& items) : name_(name), items_(items) 0035 { 0036 std::sort(items_.begin(), items_.end()); 0037 } 0038 0039 PrivacyList::PrivacyList(const QDomElement& e) 0040 { 0041 fromXml(e); 0042 } 0043 0044 void PrivacyList::updateItem(int index, const PrivacyListItem& item) 0045 { 0046 unsigned int order = items_[index].order(); 0047 items_[index] = item; 0048 items_[index].setOrder(order); 0049 } 0050 0051 void PrivacyList::insertItem(int index, const PrivacyListItem& item) 0052 { 0053 items_.insert(index,item); 0054 0055 reNumber(); 0056 } 0057 0058 void PrivacyList::reNumber() 0059 { 0060 unsigned int order = 100; 0061 for (int i = 0; i < items_.size(); ++i) { 0062 items_[i].setOrder(order); 0063 order += ORDER_INCREMENT; 0064 } 0065 } 0066 0067 0068 bool PrivacyList::moveItemUp(int index) 0069 { 0070 if (index < items().count() && index > 0) { 0071 unsigned int order =items_[index].order(); 0072 if (order == items_[index-1].order()) { 0073 reNumber(); 0074 return true; 0075 } 0076 items_[index].setOrder(items_[index-1].order()); 0077 items_[index-1].setOrder(order); 0078 items_.swapItemsAt(index,index-1); 0079 return true; 0080 } 0081 else { 0082 return false; 0083 } 0084 } 0085 0086 bool PrivacyList::moveItemDown(int index) 0087 { 0088 if (index >= 0 && index < items().count()-1) { 0089 unsigned int order =items_[index].order(); 0090 if (order == items_[index+1].order()) { 0091 reNumber(); 0092 return true; 0093 } 0094 items_[index].setOrder(items_[index+1].order()); 0095 items_[index+1].setOrder(order); 0096 items_.swapItemsAt(index,index+1); 0097 return true; 0098 } 0099 else { 0100 return false; 0101 } 0102 } 0103 0104 bool PrivacyList::onlyBlockItems() const 0105 { 0106 bool allBlocked = true; 0107 bool fallThrough = false; 0108 QList<PrivacyListItem>::ConstIterator it; 0109 for (it = items_.begin(); it != items_.end() && allBlocked; ++it ) { 0110 if ((*it).type() == PrivacyListItem::FallthroughType && (*it).action() == PrivacyListItem::Allow && (*it).all()) { 0111 fallThrough = true; 0112 } 0113 else if ((*it).isBlock()) { 0114 if (fallThrough) 0115 allBlocked = false; 0116 } 0117 else { 0118 allBlocked = false; 0119 } 0120 } 0121 return allBlocked; 0122 } 0123 0124 QDomElement PrivacyList::toXml(QDomDocument& doc) const 0125 { 0126 QDomElement list = doc.createElement("list"); 0127 list.setAttribute("name",name()); 0128 0129 for (QList<PrivacyListItem>::ConstIterator it = items_.begin() ; it != items_.end(); it++) { 0130 list.appendChild((*it).toXml(doc)); 0131 } 0132 0133 return list; 0134 } 0135 0136 void PrivacyList::fromXml(const QDomElement& el) 0137 { 0138 //qCDebug(JABBER_PROTOCOL_LOG) << "Parsing privacy list"; 0139 if (el.isNull() || el.tagName() != "list") { 0140 qCWarning(JABBER_PROTOCOL_LOG) << "Invalid root tag for privacy list."; 0141 return; 0142 } 0143 0144 setName(el.attribute("name")); 0145 for(QDomNode n = el.firstChild(); !n.isNull(); n = n.nextSibling()) { 0146 QDomElement e = n.toElement(); 0147 if (!e.isNull()) 0148 items_.append(PrivacyListItem(e)); 0149 } 0150 0151 std::sort(items_.begin(), items_.end()); 0152 } 0153 0154 QString PrivacyList::toString() const 0155 { 0156 QString s; 0157 for (QList<PrivacyListItem>::ConstIterator it = items_.begin() ; it != items_.end(); it++) { 0158 s += QString("%1 (%2)\n").arg((*it).toString()).arg((*it).order()); 0159 } 0160 return s; 0161 }