File indexing completed on 2024-04-21 04:18:48
0001 // vim: set tabstop=4 shiftwidth=4 expandtab: 0002 /* 0003 Gwenview: an image viewer 0004 Copyright 2012 Aurélien Gâteau <agateau@kde.org> 0005 0006 This program is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU General Public License 0008 as published by the Free Software Foundation; either version 2 0009 of the License, or (at your option) any later version. 0010 0011 This program is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with this program; if not, write to the Free Software 0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA. 0019 0020 */ 0021 // Self 0022 #include <serializedurlmap.h> 0023 0024 // Local 0025 0026 // KF 0027 #include <KConfigGroup> 0028 0029 // Qt 0030 #include <QUrl> 0031 0032 namespace Gwenview 0033 { 0034 static const char *KEY_SUFFIX = "key"; 0035 static const char *VALUE_SUFFIX = "value"; 0036 0037 static QUrl stripPass(const QUrl &url_) 0038 { 0039 QUrl url = url_; 0040 url.setPassword(QString()); 0041 return url; 0042 } 0043 0044 struct SerializedUrlMapPrivate { 0045 KConfigGroup mGroup; 0046 QMap<QUrl, QUrl> mMap; 0047 0048 void read() 0049 { 0050 mMap.clear(); 0051 for (int idx = 0;; ++idx) { 0052 const QString idxString = QString::number(idx); 0053 const QString key = idxString + QLatin1String(KEY_SUFFIX); 0054 if (!mGroup.hasKey(key)) { 0055 break; 0056 } 0057 const QVariant keyUrl = mGroup.readEntry(key, QVariant()); 0058 const QVariant valueUrl = mGroup.readEntry(idxString + QLatin1String(VALUE_SUFFIX), QVariant()); 0059 mMap.insert(keyUrl.toUrl(), valueUrl.toUrl()); 0060 } 0061 } 0062 0063 void write() 0064 { 0065 mGroup.deleteGroup(); 0066 QMap<QUrl, QUrl>::ConstIterator it = mMap.constBegin(), end = mMap.constEnd(); 0067 int idx = 0; 0068 for (; it != end; ++it, ++idx) { 0069 const QString idxString = QString::number(idx); 0070 mGroup.writeEntry(idxString + QLatin1String(KEY_SUFFIX), QVariant(it.key())); 0071 mGroup.writeEntry(idxString + QLatin1String(VALUE_SUFFIX), QVariant(it.value())); 0072 } 0073 mGroup.sync(); 0074 } 0075 }; 0076 0077 SerializedUrlMap::SerializedUrlMap() 0078 : d(new SerializedUrlMapPrivate) 0079 { 0080 } 0081 0082 SerializedUrlMap::~SerializedUrlMap() 0083 { 0084 delete d; 0085 } 0086 0087 void SerializedUrlMap::setConfigGroup(const KConfigGroup &group) 0088 { 0089 d->mGroup = group; 0090 d->read(); 0091 } 0092 0093 QUrl SerializedUrlMap::value(const QUrl &key_) const 0094 { 0095 const QString pass = key_.password(); 0096 const QUrl key = stripPass(key_); 0097 QUrl url = d->mMap.value(key); 0098 url.setPassword(pass); 0099 return url; 0100 } 0101 0102 void SerializedUrlMap::insert(const QUrl &key, const QUrl &value) 0103 { 0104 d->mMap.insert(stripPass(key), stripPass(value)); 0105 d->write(); 0106 } 0107 0108 } // namespace