/*! * * 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 VLogFilesModel.cpp * \author (last) Vy * \date (last) 16-May-2023 * \author (original) Behrouz NematiPour * \date (original) 11-May-2021 * */ // Qt #include // Project #include "VLogFilesModel.h" #include "Logger.h" #include "StorageGlobals.h" #include "DeviceController.h" using namespace View; VLogFilesModel::VLogFilesModel(QAbstractListModel *parent) : QAbstractListModel(parent) { initConnections(); } /*! * \brief VLogFilesModel::initConnections * Makes the necessary connections. Called inside VIEW_DEF_CLASS */ void VLogFilesModel::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 VLogFilesModel::doInit * \details Initializes the view or what needs to be done */ void VLogFilesModel::doInit() { refreshModel(); } /*! * \brief VLogFilesModel::rowCount * Gets the number of networks * \param parent - (QModelIndex) the parent QModelIndex * \return (int) - the number of networks */ int VLogFilesModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(parent); return _logFiles.count(); } /*! * \brief VLogFilesModel::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 VLogFilesModel::data(const QModelIndex & index, int role) const { if (index.row() < 0 || index.row() >= _logFiles.count()) return QVariant(); const LogFileData &logFile = _logFiles[index.row()]; switch (role) { case FilePathRole: return logFile.filePath(); case FileNameRole: return logFile.fileName(); case FileSizeRole: return logFile.fileSize_bytes(); case FileLogTypeRole : return logFile.logType(); case FileModificationDateTimeRole: return logFile.lastModificationDateTime(); } return QVariant(); } /*! * \brief VLogFilesModel::roleNames * Translates how to access specific properties of the data for QML from the NetworkDataRole enum * \return (QHash) - maps enums to property names */ QHash VLogFilesModel::roleNames() const { QHash roles; roles[FilePathRole] = "filePath"; roles[FileNameRole] = "fileName"; roles[FileSizeRole] = "fileSize_bytes"; roles[FileLogTypeRole] = "fileLogType"; roles[FileModificationDateTimeRole] = "fileModDateTime"; return roles; } QList VLogFilesModel::getLogListFromDir(Storage::Logger::LogType vType, const QString &vDirectory, bool vCheckPreInclude){ QList temp; QDirIterator dirIterator(vDirectory, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); QString absolutePath; while(dirIterator.hasNext()) { absolutePath = dirIterator.next(); if(!dirIterator.fileName().isEmpty()) { LogFileData currentLog(vType, dirIterator.fileInfo()); if(!vCheckPreInclude || !_logFiles.contains(currentLog)) { //DEBUG qDebug() << " adding " << absolutePath + dirIterator.fileName(); temp.append(currentLog); } } } return temp; } void VLogFilesModel::updateLogModel(const QString &vDirectory) { Storage::Logger::LogType type = _Logger.getLogType(vDirectory); //DEBUG: qDebug() << Q_FUNC_INFO << " dir changed : " << vDirectory << " type " << type; QList temp = getLogListFromDir(type, vDirectory, true); if(temp.count() == 0) return; // No new files // Update the model beginInsertRows(QModelIndex(), _logFiles.count(), _logFiles.count() + temp.count()); _logFiles.append(temp); endInsertRows(); } void VLogFilesModel::refreshModel() { qDebug()< 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); _logFiles.append(getLogListFromDir(type, folderPath)); } } // ============================== VUSBLogFilesModel VUSBLogFilesModel::VUSBLogFilesModel(QAbstractListModel *parent) : VLogFilesModel(parent) { // add some additional connections initConnections(); } void VUSBLogFilesModel::initConnections() { connect(&_DeviceController , SIGNAL( didUSBDriveMount ()), this , SLOT( refreshModel ())); connect(&_DeviceController , SIGNAL( didUSBDriveUmount ()), this , SLOT( removeAllRows ())); connect(&_DeviceController , SIGNAL( didUSBDriveRemove ()), this , SLOT( removeAllRows ())); } void VUSBLogFilesModel::updateModelOnDirChange(const QString &vDirectory) { if(vDirectory.contains(Storage::USB_Mount_Point)) { // Indicates that the folder change is related to the USB mount folder updateLogModel(vDirectory); } } void VUSBLogFilesModel::populateLogModel() { const QList logTypes = Storage::Logger::getLogTypeList(); foreach(Storage::Logger::LogType type , logTypes) { QString folderPath = QString("%1%2").arg(Storage::USB_Mount_Point).arg(_Logger.getPathOfLogType(type)); // Add to systemWatcher to update for changes _DeviceController.doAddDirectoryWatch(folderPath); _logFiles.append(getLogListFromDir(type, folderPath)); } }