diff --git a/src/localtextrecordingcollection.cpp b/src/localtextrecordingcollection.cpp index 9f680af354f3b6883c7a0f68945ef2c8374b35a4..711faf80249e8cd7d43f345ceab37a94c7dbafe5 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 fc1eed58b8233de4ec93d11dd835a1b6f9897c07..99b6ea5aee38411d1d452abccc5c9e0a9d09b86a 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 a257b96e8c130a5269c10ad25869933e1e456a72..5e2a846947376ff3be9f9b662fa4b901f0806727 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 d80fa091d4bba45fa23771a735649110c2b4dfe7..a5121efdb3f6bc7bf11e490fe5d707b5b17ffb1f 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 a4d1d164e91408f51db075961cad163635f52171..53818123cdd32216ef1f418d8f2a4c6ca7c2ade9 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;