File indexing completed on 2024-11-24 04:44:30
0001 /* 0002 This file is part of oxaccess. 0003 0004 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "users.h" 0010 0011 #include <QDataStream> 0012 #include <QFile> 0013 #include <QStandardPaths> 0014 0015 using namespace OXA; 0016 0017 Users *Users::mSelf = nullptr; 0018 0019 Users::Users() 0020 : mCurrentUserId(-1) 0021 { 0022 } 0023 0024 Users::~Users() = default; 0025 0026 Users *Users::self() 0027 { 0028 if (!mSelf) { 0029 mSelf = new Users(); 0030 } 0031 0032 return mSelf; 0033 } 0034 0035 void Users::init(const QString &identifier) 0036 { 0037 mIdentifier = identifier; 0038 0039 loadFromCache(); 0040 } 0041 0042 qlonglong Users::currentUserId() const 0043 { 0044 return mCurrentUserId; 0045 } 0046 0047 User Users::lookupUid(qlonglong uid) const 0048 { 0049 return mUsers.value(uid); 0050 } 0051 0052 User Users::lookupEmail(const QString &email) const 0053 { 0054 QMapIterator<qlonglong, User> it(mUsers); 0055 while (it.hasNext()) { 0056 it.next(); 0057 0058 if (it.value().email() == email) { 0059 return it.value(); 0060 } 0061 } 0062 0063 return {}; 0064 } 0065 0066 QString Users::cacheFilePath() const 0067 { 0068 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + QLatin1StringView("openxchangeresource_") + mIdentifier; 0069 } 0070 0071 void Users::setCurrentUserId(qlonglong id) 0072 { 0073 mCurrentUserId = id; 0074 0075 saveToCache(); 0076 } 0077 0078 void Users::setUsers(const User::List &users) 0079 { 0080 mUsers.clear(); 0081 for (const User &user : users) { 0082 mUsers.insert(user.uid(), user); 0083 } 0084 0085 saveToCache(); 0086 } 0087 0088 void Users::loadFromCache() 0089 { 0090 QFile cacheFile(cacheFilePath()); 0091 if (!cacheFile.open(QIODevice::ReadOnly)) { 0092 return; 0093 } 0094 0095 QDataStream stream(&cacheFile); 0096 stream.setVersion(QDataStream::Qt_4_6); 0097 0098 mUsers.clear(); 0099 0100 stream >> mCurrentUserId; 0101 0102 qulonglong count; 0103 stream >> count; 0104 0105 qlonglong uid; 0106 QString name; 0107 QString email; 0108 for (qulonglong i = 0; i < count; ++i) { 0109 stream >> uid >> name >> email; 0110 0111 User user; 0112 user.setUid(uid); 0113 user.setName(name); 0114 user.setEmail(email); 0115 mUsers.insert(user.uid(), user); 0116 } 0117 } 0118 0119 void Users::saveToCache() 0120 { 0121 QFile cacheFile(cacheFilePath()); 0122 if (!cacheFile.open(QIODevice::WriteOnly)) { 0123 return; 0124 } 0125 0126 QDataStream stream(&cacheFile); 0127 stream.setVersion(QDataStream::Qt_4_6); 0128 0129 // write current user id 0130 stream << mCurrentUserId; 0131 0132 // write number of users 0133 stream << (qulonglong)mUsers.count(); 0134 0135 // write uid, name and email address for each user 0136 QMapIterator<qlonglong, User> it(mUsers); 0137 while (it.hasNext()) { 0138 it.next(); 0139 0140 stream << it.value().uid() << it.value().name() << it.value().email(); 0141 } 0142 } 0143 0144 #include "moc_users.cpp"