From 0f928feff8e6b615b075fd8cd7d8bca2e5b171e6 Mon Sep 17 00:00:00 2001
From: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com>
Date: Fri, 16 Sep 2016 13:48:52 -0400
Subject: [PATCH] add clear method to delete chat history

This will delete all the .json chat files in the text dir. It also
clears all the TextRecording models, though the models themselves
remain to prevent nullptr errors. Also this does not remove the
ContactMethods which were generated from the chat history, the user
will have to re-launch ring for this to take effect.

Change-Id: Ifc3cf6289d19512bfc9acb5ea1c9de7b159f4473
Tuleap: #925
---
 src/localtextrecordingcollection.cpp | 22 ++++++++++++++--
 src/media/recordingmodel.cpp         | 18 ++++++++-----
 src/media/recordingmodel.h           |  1 +
 src/media/textrecording.cpp          | 39 ++++++++++++++++++++++++++++
 src/private/textrecording_p.h        |  4 +++
 5 files changed, 76 insertions(+), 8 deletions(-)

diff --git a/src/localtextrecordingcollection.cpp b/src/localtextrecordingcollection.cpp
index 9f680af3..711faf80 100644
--- a/src/localtextrecordingcollection.cpp
+++ b/src/localtextrecordingcollection.cpp
@@ -57,6 +57,8 @@ public:
    virtual bool addExisting( const Media::Recording* item ) override;
    QString fetch(const QByteArray& sha1);
 
+   void clearAll();
+
 private:
    virtual QVector<Media::Recording*> items() const override;
    //Attributes
@@ -106,6 +108,15 @@ bool LocalTextRecordingEditor::save(const Media::Recording* recording)
    return true;
 }
 
+void LocalTextRecordingEditor::clearAll()
+{
+    for (Media::Recording *recording : items()) {
+        auto textRecording = qobject_cast<Media::TextRecording*>(recording);
+        textRecording->d_ptr->clear();
+        save(recording);
+    }
+}
+
 bool LocalTextRecordingEditor::remove(const Media::Recording* item)
 {
    Q_UNUSED(item)
@@ -238,12 +249,19 @@ FlagPack<CollectionInterface::SupportedFeatures> LocalTextRecordingCollection::s
       CollectionInterface::SupportedFeatures::MANAGEABLE|
       CollectionInterface::SupportedFeatures::SAVE_ALL  |
       CollectionInterface::SupportedFeatures::LISTABLE  |
-      CollectionInterface::SupportedFeatures::REMOVE    ;
+      CollectionInterface::SupportedFeatures::REMOVE    |
+      CollectionInterface::SupportedFeatures::CLEAR     ;
 }
 
 bool LocalTextRecordingCollection::clear()
 {
-   return false;
+    static_cast<LocalTextRecordingEditor *>(editor<Media::Recording>())->clearAll();
+
+    QDir dir(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/text");
+
+    // TODO: the file deletion should be done on each individual file to be able to catch errors
+    // and to prevent us deleting files which are not the recordings
+    return dir.removeRecursively();
 }
 
 QByteArray LocalTextRecordingCollection::id() const
diff --git a/src/media/recordingmodel.cpp b/src/media/recordingmodel.cpp
index fc1eed58..99b6ea5a 100644
--- a/src/media/recordingmodel.cpp
+++ b/src/media/recordingmodel.cpp
@@ -313,12 +313,18 @@ bool Media::RecordingModel::removeItemCallback(const Recording* item)
 
 bool Media::RecordingModel::clearAllCollections() const
 {
-   foreach (CollectionInterface* backend, collections()) {
-      if (backend->supportedFeatures() & CollectionInterface::SupportedFeatures::ADD) {
-         backend->clear();
-      }
-   }
-   return true;
+    foreach (CollectionInterface* backend, collections(CollectionInterface::SupportedFeatures::CLEAR)) {
+        backend->clear();
+    }
+    return true;
+}
+
+///Deletes all recordings (which are possible to delete) and clears model
+void Media::RecordingModel::clear()
+{
+    beginResetModel();
+    clearAllCollections();
+    endResetModel();
 }
 
 void Media::RecordingModel::collectionAddedCallback(CollectionInterface* backend)
diff --git a/src/media/recordingmodel.h b/src/media/recordingmodel.h
index a257b96e..5e2a8469 100644
--- a/src/media/recordingmodel.h
+++ b/src/media/recordingmodel.h
@@ -78,6 +78,7 @@ public:
    //Setter
    void setAlwaysRecording( bool            record );
    void setRecordPath     ( const QString&  path   );
+   void clear             (                        );
 
    //Mutator
    TextRecording* createTextRecording(const ContactMethod* cm);
diff --git a/src/media/textrecording.cpp b/src/media/textrecording.cpp
index d80fa091..a5121efd 100644
--- a/src/media/textrecording.cpp
+++ b/src/media/textrecording.cpp
@@ -904,3 +904,42 @@ void InstantMessagingModel::addRowEnd()
 {
    endInsertRows();
 }
+
+void Media::TextRecordingPrivate::clear()
+{
+    m_pImModel->clear();
+
+    if (m_UnreadCount != 0) {
+        m_UnreadCount = 0;
+        emit q_ptr->unreadCountChange(0);
+    }
+}
+
+void InstantMessagingModel::clear()
+{
+    beginResetModel();
+
+    for ( TextMessageNode *node : m_pRecording->d_ptr->m_lNodes) {
+        for (Serializable::Payload *payload : node->m_pMessage->payloads) {
+            delete payload;
+        }
+        delete node->m_pMessage;
+        delete node;
+    }
+    m_pRecording->d_ptr->m_lNodes.clear();
+
+    for (Serializable::Peers *peers : m_pRecording->d_ptr->m_lAssociatedPeers) {
+        for (Serializable::Group *group : peers->groups) {
+            group->messages.clear();
+        }
+    }
+    m_pRecording->d_ptr->m_lAssociatedPeers.clear();
+
+    //TODO: holly memory leaks batman! what else do we need to delete?
+
+    m_pRecording->d_ptr->m_pCurrentGroup = nullptr;
+    m_pRecording->d_ptr->m_hMimeTypes.clear();
+    m_pRecording->d_ptr->m_lMimeTypes.clear();
+
+    endResetModel();
+}
diff --git a/src/private/textrecording_p.h b/src/private/textrecording_p.h
index a4d1d164..53818123 100644
--- a/src/private/textrecording_p.h
+++ b/src/private/textrecording_p.h
@@ -184,6 +184,8 @@ public:
    void accountMessageStatusChanged(const uint64_t id, DRing::Account::MessageStates status);
    bool updateMessageStatus(Serializable::Message* m, TextRecording::Status status);
 
+   void clear();
+
 private:
    TextRecording* q_ptr;
 };
@@ -243,6 +245,8 @@ public:
    virtual bool  setData  ( const QModelIndex& index, const QVariant &value, int role)       override;
    virtual QHash<int,QByteArray> roleNames() const override;
 
+   void clear();
+
    //Attributes
    Media::TextRecording* m_pRecording;
 
-- 
GitLab