1 /* 2 * %kadu copyright begin% 3 * Copyright 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com) 4 * %kadu copyright end% 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "roster-service-tasks.h" 21 22 #include "contacts/contact.h" 23 #include "roster/roster-entry-state.h" 24 #include "roster/roster-entry.h" 25 #include "roster/roster-service.h" 26 #include "roster/roster-task-type.h" 27 updateTasksForContacts(const QVector<Contact> & contacts)28QVector<RosterTask> RosterServiceTasks::updateTasksForContacts(const QVector<Contact> &contacts) 29 { 30 auto result = QVector<RosterTask>{}; 31 for (auto &&contact : contacts) 32 { 33 if (!contact.rosterEntry()) 34 continue; 35 // add also synchronizing and detached contacts, roster service will take care 36 // of whether it should really do the merging 37 if (contact.rosterEntry()->state() != RosterEntryState::Synchronized) 38 result.append(RosterTask{RosterTaskType::Update, contact.id()}); 39 } 40 return result; 41 } 42 RosterServiceTasks(QObject * parent)43RosterServiceTasks::RosterServiceTasks(QObject *parent) : 44 QObject{parent} 45 { 46 } 47 ~RosterServiceTasks()48RosterServiceTasks::~RosterServiceTasks() 49 { 50 } 51 containsTask(const QString & id) const52bool RosterServiceTasks::containsTask(const QString &id) const 53 { 54 return m_idToTask.contains(id); 55 } 56 tasks()57QVector<RosterTask> RosterServiceTasks::tasks() 58 { 59 return m_tasks.toVector(); 60 } 61 shouldReplaceTask(RosterTaskType taskType,RosterTaskType replacementType)62bool RosterServiceTasks::shouldReplaceTask(RosterTaskType taskType, RosterTaskType replacementType) 63 { 64 Q_ASSERT(RosterTaskType::None != taskType); 65 Q_ASSERT(RosterTaskType::None != replacementType); 66 67 if (RosterTaskType::Delete == taskType) 68 return true; 69 70 if (RosterTaskType::Add == taskType) 71 return RosterTaskType::Delete == replacementType; 72 73 return RosterTaskType::Update != replacementType; 74 } 75 addTask(const RosterTask & task)76void RosterServiceTasks::addTask(const RosterTask &task) 77 { 78 if (!m_idToTask.contains(task.id())) 79 { 80 m_tasks.enqueue(task); 81 return; 82 } 83 84 auto existingTask = m_idToTask.value(task.id()); 85 if (shouldReplaceTask(existingTask.type(), task.type())) 86 { 87 m_tasks.removeAll(existingTask); 88 m_idToTask.remove(task.id()); 89 m_idToTask.insert(task.id(), task); 90 m_tasks.enqueue(task); 91 } 92 } 93 addTasks(const QVector<RosterTask> & tasks)94void RosterServiceTasks::addTasks(const QVector<RosterTask> &tasks) 95 { 96 for (auto const &task : tasks) 97 addTask(task); 98 } 99 taskType(const QString & id)100RosterTaskType RosterServiceTasks::taskType(const QString &id) 101 { 102 if (!m_idToTask.contains(id)) 103 return RosterTaskType::None; 104 else 105 return m_idToTask.value(id).type(); 106 } 107 isEmpty() const108bool RosterServiceTasks::isEmpty() const 109 { 110 return m_tasks.isEmpty(); 111 } 112 dequeue()113RosterTask RosterServiceTasks::dequeue() 114 { 115 Q_ASSERT(!isEmpty()); 116 117 auto result = m_tasks.dequeue(); 118 m_idToTask.remove(result.id()); 119 return result; 120 } 121 rosterServiceTasks(RosterService * rs)122RosterServiceTasks * rosterServiceTasks(RosterService *rs) 123 { 124 return rs 125 ? rs->tasks() 126 : nullptr; 127 } 128 129 #include "moc_roster-service-tasks.cpp" 130