diff --git a/smsapp/conversationlistmodel.cpp b/smsapp/conversationlistmodel.cpp index 3f36bcd4..4f796478 100644 --- a/smsapp/conversationlistmodel.cpp +++ b/smsapp/conversationlistmodel.cpp @@ -1,156 +1,157 @@ /* * This file is part of KDE Telepathy Chat * * Copyright (C) 2018 Aleix Pol Gonzalez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "conversationlistmodel.h" #include #include "interfaces/conversationmessage.h" Q_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL, "kdeconnect.sms.conversations_list") ConversationListModel::ConversationListModel(QObject* parent) : QStandardItemModel(parent) , m_conversationsInterface(nullptr) { qCCritical(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "Constructing" << this; auto roles = roleNames(); roles.insert(FromMeRole, "fromMe"); roles.insert(PersonUriRole, "personUri"); roles.insert(ConversationIdRole, "conversationId"); roles.insert(DateRole, "date"); setItemRoleNames(roles); ConversationMessage::registerDbusType(); } ConversationListModel::~ConversationListModel() { - if (m_conversationsInterface) delete m_conversationsInterface; } void ConversationListModel::setDeviceId(const QString& deviceId) { - qCCritical(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "setDeviceId" << "of" << this; - if (m_conversationsInterface) delete m_conversationsInterface; - - m_deviceId = deviceId; - - m_conversationsInterface = new DeviceConversationsDbusInterface(deviceId); + qCCritical(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "setDeviceId" << deviceId << "of" << this; + if (deviceId == m_deviceId) + return; - connect(m_conversationsInterface, SIGNAL(conversationCreated(const QString&)), - this, SLOT(handleCreatedConversation(const QString&)), Qt::UniqueConnection); + if (m_conversationsInterface) { + disconnect(m_conversationsInterface, SIGNAL(conversationCreated(QString)), this, SLOT(handleCreatedConversation(QString))); + delete m_conversationsInterface; + } + m_deviceId = deviceId; + m_conversationsInterface = new DeviceConversationsDbusInterface(deviceId, this); + connect(m_conversationsInterface, SIGNAL(conversationCreated(QString)), this, SLOT(handleCreatedConversation(QString))); prepareConversationsList(); m_conversationsInterface->requestAllConversationThreads(); } void ConversationListModel::prepareConversationsList() { clear(); QDBusPendingReply validThreadIDsReply = m_conversationsInterface->activeConversations(); validThreadIDsReply.waitForFinished(); for (const QString& conversationId : validThreadIDsReply.value()) { handleCreatedConversation(conversationId); } } void ConversationListModel::handleCreatedConversation(const QString& conversationId) { QVariantList args; args.append(conversationId); m_conversationsInterface->callWithCallback("getFirstFromConversation", args, this, SLOT(createRowFromMessage(ConversationMessage)), SLOT(printDBusError(QDBusError))); } void ConversationListModel::printDBusError(const QDBusError& error) { qCWarning(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << error; } void ConversationListModel::createRowFromMessage(const ConversationMessage& message) { if (message.type() == -1) { // The Android side currently hacks in -1 if something weird comes up // TODO: Remove this hack when MMS support is implemented return; } QStandardItem* item = new QStandardItem(); KPeople::PersonData* personData = lookupPersonByAddress(message.address()); if (personData) { item->setText(personData->name()); item->setIcon(QIcon(personData->photo())); item->setData(personData->personUri(), PersonUriRole); } else { item->setText(message.address()); } item->setData(message.threadID(), ConversationIdRole); item->setData(message.type() == ConversationMessage::MessageTypeSent, FromMeRole); item->setData(message.date(), DateRole); appendRow(item); delete personData; } KPeople::PersonData* ConversationListModel::lookupPersonByAddress(const QString& address) { int rowIndex = 0; for (rowIndex = 0; rowIndex < m_people.rowCount(); rowIndex++) { const QString& uri = m_people.get(rowIndex, KPeople::PersonsModel::PersonUriRole).toString(); KPeople::PersonData* person = new KPeople::PersonData(uri); const QString& email = person->email(); const QString& phoneNumber = canonicalizePhoneNumber(person->contactCustomProperty("phoneNumber").toString()); if (address == email || canonicalizePhoneNumber(address) == phoneNumber) { qCDebug(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) << "Matched" << address << "to" << person->name(); return person; } delete person; } return nullptr; } QString ConversationListModel::canonicalizePhoneNumber(const QString& phoneNumber) { QString toReturn(phoneNumber); toReturn = toReturn.remove(' '); toReturn = toReturn.remove('-'); toReturn = toReturn.remove('('); toReturn = toReturn.remove(')'); toReturn = toReturn.remove('+'); return toReturn; } diff --git a/smsapp/qml/ConversationList.qml b/smsapp/qml/ConversationList.qml index b75bfaa6..ec26376d 100644 --- a/smsapp/qml/ConversationList.qml +++ b/smsapp/qml/ConversationList.qml @@ -1,101 +1,101 @@ /* * This file is part of KDE Telepathy Chat * * Copyright (C) 2015 Aleix Pol Gonzalez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import QtQuick 2.5 import QtQuick.Controls 2.1 import QtQuick.Layouts 1.1 import org.kde.people 1.0 import org.kde.plasma.core 2.0 as Core import org.kde.kirigami 2.2 as Kirigami import org.kde.kdeconnect 1.0 import org.kde.kdeconnect.sms 1.0 Kirigami.ScrollablePage { footer: ComboBox { id: devicesCombo enabled: count > 0 displayText: enabled ? undefined : i18n("No devices available") model: DevicesSortProxyModel { id: devicesModel //TODO: make it possible to sort only if they can do sms sourceModel: DevicesModel { displayFilter: DevicesModel.Paired | DevicesModel.Reachable } onRowsInserted: if (devicesCombo.currentIndex < 0) { devicesCombo.currentIndex = 0 } } textRole: "display" } readonly property QtObject device: devicesCombo.currentIndex >= 0 ? devicesModel.data(devicesModel.index(devicesCombo.currentIndex, 0), DevicesModel.DeviceRole) : null Component { id: chatView ConversationDisplay {} } ListView { id: view currentIndex: 0 model: ConversationListModel { - deviceId: device.id() + deviceId: device ? device.id() : "" } header: TextField { id: filter placeholderText: i18n("Filter...") width: parent.width onTextChanged: { view.model.filterRegExp = new RegExp(filter.text) view.currentIndex = 0 } Keys.onUpPressed: view.currentIndex = Math.max(view.currentIndex-1, 0) Keys.onDownPressed: view.currentIndex = Math.min(view.currentIndex+1, view.count-1) onAccepted: { view.currentItem.startChat() } Shortcut { sequence: "Ctrl+F" onActivated: filter.forceActiveFocus() } } delegate: Kirigami.BasicListItem { hoverEnabled: true readonly property var person: PersonData { personUri: model.personUri } label: display icon: decoration function startChat() { applicationWindow().pageStack.push(chatView, { person: person.person, conversationId: model.conversationId, device: device}) } onClicked: { startChat(); } } } }