File indexing completed on 2024-06-16 04:29:46
0001 #include "contactsmodel.h" 0002 #include "abstractinterface.h" 0003 0004 #include <QDebug> 0005 0006 #ifdef Q_OS_ANDROID 0007 #include "androidinterface.h" 0008 #else 0009 #include "linuxinterface.h" 0010 #endif 0011 0012 #include <MauiKit3/Core/fmh.h> 0013 0014 #ifdef Q_OS_ANDROID 0015 ContactsModel::ContactsModel(QObject *parent) 0016 : MauiList(parent) 0017 , syncer(AndroidInterface::getInstance()) 0018 #else 0019 ContactsModel::ContactsModel(QObject *parent) 0020 : MauiList(parent) 0021 , syncer(new LinuxInterface(this)) 0022 #endif 0023 { 0024 connect(syncer, &AbstractInterface::contactsReady, [this](FMH::MODEL_LIST contacts) { 0025 qDebug() << "CONATCTS READY AT MODEL 1" << contacts; 0026 emit this->preListChanged(); 0027 this->list = contacts; 0028 this->filter(); 0029 emit this->postListChanged(); 0030 emit this->countChanged(); 0031 }); 0032 0033 this->getList(); 0034 } 0035 0036 const FMH::MODEL_LIST &ContactsModel::items() const 0037 { 0038 return this->list; 0039 } 0040 0041 void ContactsModel::setQuery(const QString &query) 0042 { 0043 if (this->m_query == query || query.isEmpty()) 0044 return; 0045 0046 this->m_query = query; 0047 0048 emit this->preListChanged(); 0049 this->filter(); 0050 emit this->postListChanged(); 0051 0052 emit this->queryChanged(); 0053 } 0054 0055 QString ContactsModel::getQuery() const 0056 { 0057 return this->m_query; 0058 } 0059 0060 void ContactsModel::getList() 0061 { 0062 qDebug() << "TRYING TO SET FULL LIST"; 0063 this->syncer->getContacts(); 0064 } 0065 0066 bool ContactsModel::insert(const QVariantMap &map) 0067 { 0068 qDebug() << "INSERTING NEW CONTACT" << map; 0069 0070 if (map.isEmpty()) 0071 return false; 0072 0073 const auto model = FMH::toModel(map); 0074 if (!this->syncer->insertContact(model)) 0075 return false; 0076 0077 qDebug() << "inserting new contact count" << this->list.count(); 0078 emit this->preItemAppended(); 0079 this->list << model; 0080 emit this->postItemAppended(); 0081 0082 qDebug() << "inserting new contact count" << this->list.count(); 0083 0084 return true; 0085 } 0086 0087 bool ContactsModel::update(const QVariantMap &map, const int &index) 0088 { 0089 if (index >= this->list.size() || index < 0) 0090 return false; 0091 0092 const auto newItem = FMH::toModel(map); 0093 const auto oldItem = this->list[index]; 0094 0095 auto updatedItem = FMH::MODEL(); 0096 updatedItem[FMH::MODEL_KEY::ID] = oldItem[FMH::MODEL_KEY::ID]; 0097 0098 QVector<int> roles; 0099 for (const auto &key : newItem.keys()) { 0100 if (newItem[key] != oldItem[key]) { 0101 updatedItem.insert(key, newItem[key]); 0102 roles << key; 0103 } 0104 } 0105 0106 qDebug() << "trying to update contact:" << oldItem << "\n\n" << newItem << "\n\n" << updatedItem; 0107 0108 this->syncer->updateContact(oldItem[FMH::MODEL_KEY::ID], newItem); 0109 this->list[index] = newItem; 0110 emit this->updateModel(index, roles); 0111 0112 return true; 0113 } 0114 0115 bool ContactsModel::remove(const int &index) 0116 { 0117 if (index >= this->list.size() || index < 0) 0118 return false; 0119 0120 qDebug() << "trying to remove :" << this->list[index][FMH::MODEL_KEY::ID]; 0121 if (this->syncer->removeContact(this->list[index][FMH::MODEL_KEY::ID])) { 0122 emit this->preItemRemoved(index); 0123 this->list.removeAt(index); 0124 emit this->postItemRemoved(); 0125 return true; 0126 } 0127 0128 return false; 0129 } 0130 0131 void ContactsModel::filter() 0132 { 0133 FMH::MODEL_LIST res; 0134 0135 if (this->m_query.contains("=")) { 0136 auto q = this->m_query.split("=", Qt::SkipEmptyParts); 0137 if (q.size() == 2) { 0138 for (auto item : this->list) { 0139 if (item[FMH::MODEL_NAME_KEY[q.first().trimmed()]].replace(" ", "").contains(q.last().trimmed())) 0140 res << item; 0141 } 0142 } 0143 0144 this->list = res; 0145 } 0146 } 0147 0148 void ContactsModel::append(const QVariantMap &item) 0149 { 0150 if (item.isEmpty()) 0151 return; 0152 0153 emit this->preItemAppended(); 0154 0155 FMH::MODEL model; 0156 for (auto key : item.keys()) 0157 model.insert(FMH::MODEL_NAME_KEY[key], item[key].toString()); 0158 0159 qDebug() << "Appending item to list" << item; 0160 this->list << model; 0161 0162 qDebug() << this->list; 0163 0164 emit this->postItemAppended(); 0165 } 0166 0167 void ContactsModel::append(const QVariantMap &item, const int &at) 0168 { 0169 if (item.isEmpty()) 0170 return; 0171 0172 if (at > this->list.size() || at < 0) 0173 return; 0174 0175 qDebug() << "trying to append at" << at << item["title"]; 0176 0177 emit this->preItemAppendedAt(at); 0178 this->list.insert(at, FMH::toModel(item)); 0179 emit this->postItemAppended(); 0180 } 0181 0182 void ContactsModel::clear() 0183 { 0184 emit this->preListChanged(); 0185 this->list.clear(); 0186 emit this->postListChanged(); 0187 } 0188 0189 void ContactsModel::reset() 0190 { 0191 this->m_query.clear(); 0192 this->getList(); 0193 } 0194 0195 void ContactsModel::refresh() 0196 { 0197 this->getList(); 0198 } 0199 0200 QVariantList ContactsModel::getAccounts() 0201 { 0202 return FMH::toMapList(syncer->getAccounts()); 0203 }