/*! * * Copyright (c) 2021-2023 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file VSDCardFilesModel.cpp * \author (last) Vy * \date (last) 16-May-2023 * \author (original) Behrouz NematiPour * \date (original) 11-May-2021 * */ // Qt #include // Project #include "VSDCardFilesModel.h" #include "Logger.h" #include "StorageGlobals.h" #include "DeviceController.h" using namespace View; VSDCardFilesModel::VSDCardFilesModel(QAbstractListModel *parent) : QAbstractListModel(parent) { // startTimer(_interval); initConnections(); doInit(); } /*! * \brief VSDCardFilesModel::initConnections * Makes the necessary connections. Called inside VIEW_DEF_CLASS */ void VSDCardFilesModel::initConnections() { connect(&_DeviceController , SIGNAL(didWatchDirectoryChange (const QString &)), this , SLOT( onWatchDirectoryChange (const QString &))); // The model gets a total refresh when Logger class emits didRemoveLogs connect(&_Logger , SIGNAL( didRemoveLogs (bool)), this , SLOT(onTotalModelRefresh (bool))); } /*! * \brief VSDCardFilesModel::doInit * \details Initializes the view or what needs to be done when for example getting into the WiFi setting screen (do scan for now). */ void VSDCardFilesModel::doInit() { // Do the first population populateLogModel(); } /*! * \brief VSDCardFilesModel::rowCount * Gets the number of networks * \param parent - (QModelIndex) the parent QModelIndex * \return (int) - the number of networks */ int VSDCardFilesModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(parent); return _sdCardFiles.count(); } /*! * \brief VSDCardFilesModel::data * Returns the sd card file properties at the specified index * \param index (QModelIndex) contains the row of data to lookup * \param role - (int) the property index to return. See SDCardFileDataRole * \return (QVariant) - the value for the specified network property */ QVariant VSDCardFilesModel::data(const QModelIndex & index, int role) const { if (index.row() < 0 || index.row() >= _sdCardFiles.count()) return QVariant(); const SDCardFileData &sdCardFile = _sdCardFiles[index.row()]; switch (role) { case FilePathRole: return sdCardFile.filePath(); case FileNameRole: return sdCardFile.fileName(); case FileSizeRole: return sdCardFile.fileSize_bytes(); case FileLogTypeRole : return sdCardFile.logType(); case FileModificationDateTimeRole: return sdCardFile.lastModificationDateTime(); } return QVariant(); } /*! * \brief VSDCardFilesModel::roleNames * Translates how to access specific properties of the data for QML from the NetworkDataRole enum * \return (QHash) - maps enums to property names */ QHash VSDCardFilesModel::roleNames() const { QHash roles; roles[FilePathRole] = "filePath"; roles[FileNameRole] = "fileName"; roles[FileSizeRole] = "fileSize_bytes"; roles[FileLogTypeRole] = "fileLogType"; roles[FileModificationDateTimeRole] = "fileModDateTime"; return roles; } /*! * \brief VSDCardFilesModel::timerEvent * \details The overloaded parent QObject timer event handler function */ void VSDCardFilesModel::timerEvent(QTimerEvent *) { // No-Op - DO NOT do a totalModelReset/resetModel on a timeout! } void VSDCardFilesModel::onWatchDirectoryChange(const QString &vDirectory) { qDebug()<< "onWatchDirectoryChange " << vDirectory; //TODO need to investigate why doing single insertion into model does not work as expected and also // whether there is a performance gain if we do non-resetModel approach // For a general case, this change in any logging directory does not happen often, maybe once a day or every x hours resetModel(); } void VSDCardFilesModel::resetModel() { beginResetModel(); _sdCardFiles.clear(); populateLogModel (); // repopulate it back with refreshed directory info endResetModel(); } void VSDCardFilesModel::onTotalModelRefresh(bool vIsStrillProcessing) { qDebug()<< Q_FUNC_INFO; if ( !vIsStrillProcessing ){ resetModel(); } } void VSDCardFilesModel::populateLogModel() { const QList logTypes = Storage::Logger::getLogTypeList(); foreach(Storage::Logger::LogType type , logTypes) { QString folderPath = QString("%1%2").arg(Storage::Log_Folder_Base).arg(_Logger.getPathOfLogType(type)); // Add to systemWatcher to update for changes _DeviceController.doAddDirectoryWatch(folderPath); QDirIterator dirIterator(folderPath, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); QString absolutePath; absolutePath = dirIterator.path(); while(dirIterator.hasNext()) { absolutePath = dirIterator.next(); if(!dirIterator.fileName().isEmpty()) { //DEBUG: qDebug() << " FOUND " << absolutePath + dirIterator.fileName(); _sdCardFiles.append(SDCardFileData(type, dirIterator.fileInfo())); } } } }