File indexing completed on 2024-04-14 05:24:21

0001 /*
0002     SPDX-FileCopyrightText: 2020 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 // local
0007 #include "generictable.h"
0008 #include "activitydata.h"
0009 #include "appletdata.h"
0010 #include "errorinformationdata.h"
0011 #include "layoutdata.h"
0012 #include "screendata.h"
0013 #include "viewdata.h"
0014 
0015 // Qt
0016 #include <QDebug>
0017 
0018 namespace Latte {
0019 namespace Data {
0020 
0021 template <class T>
0022 GenericTable<T>::GenericTable()
0023 {
0024 }
0025 
0026 template <class T>
0027 GenericTable<T>::GenericTable(GenericTable<T> &&o)
0028     : m_list(o.m_list)
0029 {
0030 
0031 }
0032 
0033 template <class T>
0034 GenericTable<T>::GenericTable(const GenericTable<T> &o)
0035     : m_list(o.m_list)
0036 {
0037 
0038 }
0039 
0040 //! Operators
0041 template <class T>
0042 GenericTable<T> &GenericTable<T>::operator=(const GenericTable<T> &rhs)
0043 {
0044     m_list = rhs.m_list;
0045 
0046     return (*this);
0047 }
0048 
0049 template <class T>
0050 GenericTable<T> &GenericTable<T>::operator=(GenericTable<T> &&rhs)
0051 {
0052     m_list = rhs.m_list;
0053     return (*this);
0054 }
0055 
0056 template <class T>
0057 GenericTable<T> &GenericTable<T>::operator<<(const T &rhs)
0058 {
0059     if (!rhs.id.isEmpty()) {
0060         m_list << rhs;
0061     }
0062 
0063     return (*this);
0064 }
0065 
0066 template <class T>
0067 GenericTable<T> &GenericTable<T>::operator<<(const GenericTable<T> &rhs)
0068 {
0069     m_list << rhs.m_list;
0070     return (*this);
0071 }
0072 
0073 template <class T>
0074 GenericTable<T> &GenericTable<T>::insert(const int &pos, const T &rhs)
0075 {
0076     m_list.insert(pos, rhs);
0077     return (*this);
0078 }
0079 
0080 template <class T>
0081 GenericTable<T> &GenericTable<T>::insertBasedOnName(const T &rhs)
0082 {
0083     return insert(sortedPosForName(rhs.name), rhs);
0084 }
0085 
0086 template <class T>
0087 GenericTable<T> &GenericTable<T>::insertBasedOnId(const T &rhs)
0088 {
0089     return insert(sortedPosForId(rhs.id), rhs);
0090 }
0091 
0092 template <class T>
0093 bool GenericTable<T>::operator==(const GenericTable<T> &rhs) const
0094 {
0095     if (m_list.count() == 0 && rhs.m_list.count() == 0) {
0096         return true;
0097     }
0098 
0099     if (m_list.count() != rhs.m_list.count()) {
0100         return false;
0101     }
0102 
0103     for(int i=0; i<m_list.count(); ++i) {
0104         QString id = m_list[i].id;
0105 
0106         if (!rhs.containsId(id) || (*this)[id] != rhs[id]){
0107             return false;
0108         }
0109     }
0110 
0111     return true;
0112 }
0113 
0114 template <class T>
0115 bool GenericTable<T>::operator!=(const GenericTable<T> &rhs) const
0116 {
0117     return !(*this == rhs);
0118 }
0119 
0120 template <class T>
0121 T &GenericTable<T>::operator[](const QString &id)
0122 {
0123     int pos{-1};
0124 
0125     for(int i=0; i<m_list.count(); ++i) {
0126         if (m_list[i].id == id){
0127             pos = i;
0128             break;
0129         }
0130     }
0131 
0132     return m_list[pos];
0133 }
0134 
0135 template <class T>
0136 const T GenericTable<T>::operator[](const QString &id) const
0137 {
0138     int pos{-1};
0139 
0140     for(int i=0; i<m_list.count(); ++i) {
0141         if (m_list[i].id == id){
0142             pos = i;
0143             break;
0144         }
0145     }
0146 
0147     return m_list[pos];
0148 }
0149 
0150 template <class T>
0151 T &GenericTable<T>::operator[](const uint &index)
0152 {
0153     return m_list[index];
0154 }
0155 
0156 template <class T>
0157 const T GenericTable<T>::operator[](const uint &index) const
0158 {
0159     return m_list[index];
0160 }
0161 
0162 template <class T>
0163 GenericTable<T>::operator QString() const
0164 {
0165     QString result;
0166 
0167     for(int i=0; i<m_list.count(); ++i) {
0168         result += m_list[i].id;
0169         if (i<(m_list.count()-1)) {
0170             result += ", ";
0171         }
0172     }
0173 
0174     return result;
0175 }
0176 
0177 template <class T>
0178 bool GenericTable<T>::containsId(const QString &id) const
0179 {
0180     for(int i=0; i<m_list.count(); ++i) {
0181         if (m_list[i].id == id){
0182             return true;
0183         }
0184     }
0185 
0186     return false;
0187 }
0188 
0189 template <class T>
0190 bool GenericTable<T>::containsName(const QString &name) const
0191 {
0192     for(int i=0; i<m_list.count(); ++i) {
0193         if (m_list[i].name == name){
0194             return true;
0195         }
0196     }
0197 
0198     return false;
0199 }
0200 
0201 template <class T>
0202 bool GenericTable<T>::isEmpty() const
0203 {
0204     return m_list.count() <= 0;
0205 }
0206 
0207 template <class T>
0208 bool GenericTable<T>::rowExists(const int &row) const
0209 {
0210     return (m_list.count()>=0 && row>=0 && row<rowCount());
0211 }
0212 
0213 template <class T>
0214 int GenericTable<T>::indexOf(const QString &id) const
0215 {
0216     for(int i=0; i<m_list.count(); ++i) {
0217         if (m_list[i].id == id){
0218             return i;
0219         }
0220     }
0221 
0222     return -1;
0223 }
0224 
0225 template <class T>
0226 int GenericTable<T>::rowCount() const
0227 {
0228     return m_list.count();
0229 }
0230 
0231 template <class T>
0232 int GenericTable<T>::sortedPosForId(const QString &id) const
0233 {
0234     int pos{0};
0235 
0236     for(int i=0; i<m_list.count(); ++i) {
0237         if (QString::compare(m_list[i].id, id, Qt::CaseInsensitive) <= 0) {
0238             pos++;
0239         } else {
0240             break;
0241         }
0242     }
0243 
0244     return pos;
0245 }
0246 
0247 template <class T>
0248 int GenericTable<T>::sortedPosForName(const QString &name) const
0249 {
0250     int pos{0};
0251 
0252     for(int i=0; i<m_list.count(); ++i) {
0253         if (QString::compare(m_list[i].name, name, Qt::CaseInsensitive) <= 0) {
0254             pos++;
0255         } else {
0256             break;
0257         }
0258     }
0259 
0260     return pos;
0261 }
0262 
0263 template <class T>
0264 QString GenericTable<T>::idForName(const QString &name) const
0265 {
0266     for(int  i=0; i<m_list.count(); ++i) {
0267         if (m_list[i].name == name) {
0268             return m_list[i].id;
0269         }
0270     }
0271 
0272     return QString();
0273 }
0274 
0275 template <class T>
0276 QStringList GenericTable<T>::ids() const
0277 {
0278     QStringList idlist;
0279 
0280     for(int i=0; i<m_list.count(); ++i) {
0281         idlist << m_list[i].id;
0282     }
0283 
0284     return idlist;
0285 }
0286 
0287 template <class T>
0288 QStringList GenericTable<T>::names() const
0289 {
0290     QStringList nms;
0291 
0292     for(int i=0; i<m_list.count(); ++i) {
0293         nms << m_list[i].name;
0294     }
0295 
0296     return nms;
0297 }
0298 
0299 template <class T>
0300 void GenericTable<T>::clear()
0301 {
0302     m_list.clear();
0303 }
0304 
0305 template <class T>
0306 void GenericTable<T>::remove(const QString &id)
0307 {
0308     const int pos = indexOf(id);
0309 
0310     if (pos >= 0) {
0311         m_list.removeAt(pos);
0312     }
0313 }
0314 
0315 template <class T>
0316 void GenericTable<T>::remove(const int &row)
0317 {
0318     if (rowExists(row)) {
0319         m_list.removeAt(row);
0320     }
0321 }
0322 
0323 //! Make linker happy and provide which table instances will be used.
0324 //! The alternative would be to move functions definitions in the header file
0325 //! but that would drop readability
0326 template class GenericTable<Data::Activity>;
0327 template class GenericTable<Data::Applet>;
0328 template class GenericTable<Data::ErrorInformation>;
0329 template class GenericTable<Data::Generic>;
0330 template class GenericTable<Data::Layout>;
0331 template class GenericTable<Data::Screen>;
0332 template class GenericTable<Data::View>;
0333 
0334 }
0335 }