File indexing completed on 2024-04-28 16:42:52

0001 // SPDX-FileCopyrightText: 2019 Nicolas Fella <nicolas.fella@gmx.de>
0002 // SPDX-FileCopyrightText: 2021 Alexey Andreyev <aa13q@ya.ru>
0003 //
0004 // SPDX-License-Identifier: LGPL-2.1-or-later
0005 
0006 #include "call-history-model.h"
0007 
0008 #include <QDateTime>
0009 #include <QDebug>
0010 #include <QSqlError>
0011 #include <QSqlQuery>
0012 #include <QStandardPaths>
0013 
0014 CallHistoryModel::CallHistoryModel(QObject *parent)
0015     : CallModel(parent)
0016 {
0017     _databaseInterface = new org::kde::telephony::CallHistoryDatabase(QString::fromLatin1(_databaseInterface->staticInterfaceName()),
0018                                                                       QStringLiteral("/org/kde/telephony/CallHistoryDatabase/tel/mm"),
0019                                                                       QDBusConnection::sessionBus(),
0020                                                                       this);
0021 
0022     if (!_databaseInterface->isValid()) {
0023         qDebug() << Q_FUNC_INFO << "Could not initiate CallHistoryDatabase interface";
0024         return;
0025     }
0026 
0027     beginResetModel();
0028     _fetchCalls();
0029     endResetModel();
0030 
0031     /*
0032     connect(&ContactPhoneNumberMapper::instance(), &ContactPhoneNumberMapper::contactsChanged, this, [this] {
0033         Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1), {DisplayNameRole, PhotoRole});
0034     });
0035     */
0036 
0037     connect(_databaseInterface, &org::kde::telephony::CallHistoryDatabase::callsChanged, this, [this] {
0038         beginResetModel();
0039         _fetchCalls();
0040         endResetModel();
0041     });
0042 }
0043 
0044 void CallHistoryModel::addCall(const DialerTypes::CallData &callData)
0045 {
0046     QDBusPendingReply<int> reply = _databaseInterface->lastId();
0047     reply.waitForFinished();
0048     int databaseLastId;
0049     if (reply.isValid()) {
0050         databaseLastId = reply.value();
0051     } else {
0052         qDebug() << Q_FUNC_INFO << reply.error();
0053         return;
0054     }
0055 
0056     beginInsertRows(QModelIndex(), _calls.size(), _calls.size());
0057     _databaseInterface->addCall(callData);
0058 
0059     DialerTypes::CallData data;
0060     data.id = QString::number(databaseLastId);
0061 
0062     _calls.push_front(callData); // insert latest calls at the top of the list
0063 
0064     endInsertRows();
0065 }
0066 
0067 void CallHistoryModel::clear()
0068 {
0069     auto reply = _databaseInterface->clear();
0070     reply.waitForFinished();
0071     if (!reply.isValid()) {
0072         qDebug() << Q_FUNC_INFO << reply.error();
0073         return;
0074     }
0075     beginResetModel();
0076     _calls.clear();
0077     endResetModel();
0078 }
0079 
0080 bool CallHistoryModel::removeRows(int row, int count, const QModelIndex &parent)
0081 {
0082     Q_UNUSED(count)
0083     auto reply = _databaseInterface->remove(_calls[row].id);
0084     reply.waitForFinished();
0085     if (!reply.isValid()) {
0086         qDebug() << Q_FUNC_INFO << reply.error();
0087         return false;
0088     }
0089 
0090     beginRemoveRows(parent, row, row);
0091     _fetchCalls();
0092     endRemoveRows();
0093 
0094     return true;
0095 }
0096 
0097 void CallHistoryModel::_fetchCalls()
0098 {
0099     QDBusPendingReply<DialerTypes::CallDataVector> reply = _databaseInterface->fetchCalls();
0100     reply.waitForFinished();
0101     if (reply.isError()) {
0102         qDebug() << Q_FUNC_INFO << reply.error();
0103     }
0104     _calls = reply;
0105 }
0106 
0107 void CallHistoryModel::remove(int index)
0108 {
0109     removeRow(index);
0110 }
0111 
0112 QVariant CallHistoryModel::data(const QModelIndex &index, int role) const
0113 {
0114     int row = index.row();
0115     switch (role) {
0116     case Roles::EventRole:
0117         return _calls[row].id;
0118     case Roles::ProtocolRole:
0119         return _calls[row].protocol;
0120     case Roles::AccountRole:
0121         return _calls[row].account;
0122     case Roles::ProviderRole:
0123         return _calls[row].provider;
0124     case Roles::CommunicationWithRole:
0125         return _calls[row].communicationWith;
0126     case Roles::DirectionRole:
0127         return QVariant::fromValue(_calls[row].direction);
0128     case Roles::StateRole:
0129         return QVariant::fromValue(_calls[row].state);
0130     case Roles::StateReasonRole:
0131         return QVariant::fromValue(_calls[row].stateReason);
0132     case Roles::CallAttemptDurationRole:
0133         return _calls[row].callAttemptDuration;
0134     case Roles::StartedAtRole:
0135         return _calls[row].startedAt;
0136     case Roles::DurationRole:
0137         return _calls[row].duration;
0138     }
0139     return {};
0140 }
0141 
0142 int CallHistoryModel::rowCount(const QModelIndex &parent) const
0143 {
0144     Q_UNUSED(parent)
0145     return _calls.size();
0146 }