1d96f3f21SNicolas Bonnefon /* 2d96f3f21SNicolas Bonnefon * Copyright (C) 2013 Nicolas Bonnefon and other contributors 3d96f3f21SNicolas Bonnefon * 4d96f3f21SNicolas Bonnefon * This file is part of glogg. 5d96f3f21SNicolas Bonnefon * 6d96f3f21SNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7d96f3f21SNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8d96f3f21SNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9d96f3f21SNicolas Bonnefon * (at your option) any later version. 10d96f3f21SNicolas Bonnefon * 11d96f3f21SNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12d96f3f21SNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13d96f3f21SNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14d96f3f21SNicolas Bonnefon * GNU General Public License for more details. 15d96f3f21SNicolas Bonnefon * 16d96f3f21SNicolas Bonnefon * You should have received a copy of the GNU General Public License 17d96f3f21SNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18d96f3f21SNicolas Bonnefon */ 19d96f3f21SNicolas Bonnefon 20d96f3f21SNicolas Bonnefon #include "session.h" 21d96f3f21SNicolas Bonnefon 2278b9e3f6SNicolas Bonnefon #include <cassert> 23039481acSNicolas Bonnefon #include <QFileInfo> 24039481acSNicolas Bonnefon 25d96f3f21SNicolas Bonnefon #include "viewinterface.h" 261b5e406eSNicolas Bonnefon #include "persistentinfo.h" 271b5e406eSNicolas Bonnefon #include "savedsearches.h" 28039481acSNicolas Bonnefon #include "data/logdata.h" 29039481acSNicolas Bonnefon #include "data/logfiltereddata.h" 30d96f3f21SNicolas Bonnefon 31d96f3f21SNicolas Bonnefon Session::Session() 32d96f3f21SNicolas Bonnefon { 331b5e406eSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "savedSearches" ) ); 341b5e406eSNicolas Bonnefon 351b5e406eSNicolas Bonnefon // Get the global search history 361b5e406eSNicolas Bonnefon savedSearches_ = std::shared_ptr<SavedSearches>( 371b5e406eSNicolas Bonnefon &(Persistent<SavedSearches>( "savedSearches" )) ); 38d96f3f21SNicolas Bonnefon } 39d96f3f21SNicolas Bonnefon 40d96f3f21SNicolas Bonnefon Session::~Session() 41d96f3f21SNicolas Bonnefon { 42d96f3f21SNicolas Bonnefon // FIXME Clean up all the data objects... 43d96f3f21SNicolas Bonnefon } 44f0708ca8SNicolas Bonnefon 45f0708ca8SNicolas Bonnefon ViewInterface* Session::open( const std::string& file_name, 46f0708ca8SNicolas Bonnefon std::function<ViewInterface*()> view_factory ) 47f0708ca8SNicolas Bonnefon { 48039481acSNicolas Bonnefon ViewInterface* view = nullptr; 49039481acSNicolas Bonnefon 50039481acSNicolas Bonnefon QFileInfo fileInfo( file_name.c_str() ); 51039481acSNicolas Bonnefon if ( fileInfo.isReadable() ) 52039481acSNicolas Bonnefon { 53039481acSNicolas Bonnefon // Create the data objects 5478b9e3f6SNicolas Bonnefon auto log_data = std::make_shared<LogData>(); 5578b9e3f6SNicolas Bonnefon auto log_filtered_data = 5678b9e3f6SNicolas Bonnefon std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() ); 57039481acSNicolas Bonnefon 58039481acSNicolas Bonnefon view = view_factory(); 5978b9e3f6SNicolas Bonnefon view->setData( log_data, log_filtered_data ); 601b5e406eSNicolas Bonnefon view->setSavedSearches( savedSearches_ ); 6178b9e3f6SNicolas Bonnefon 6278b9e3f6SNicolas Bonnefon // Insert in the hash 631b5e406eSNicolas Bonnefon openFiles_.insert( { view, 6478b9e3f6SNicolas Bonnefon { file_name, 6578b9e3f6SNicolas Bonnefon log_data, 6678b9e3f6SNicolas Bonnefon log_filtered_data, 6778b9e3f6SNicolas Bonnefon view } } ); 68039481acSNicolas Bonnefon 69039481acSNicolas Bonnefon // Start loading the file 7078b9e3f6SNicolas Bonnefon log_data->attachFile( QString( file_name.c_str() ) ); 71039481acSNicolas Bonnefon } 72039481acSNicolas Bonnefon else { 73039481acSNicolas Bonnefon // throw 74039481acSNicolas Bonnefon } 75f0708ca8SNicolas Bonnefon 76f0708ca8SNicolas Bonnefon return view; 77f0708ca8SNicolas Bonnefon } 7878b9e3f6SNicolas Bonnefon 79*cdd89779SNicolas Bonnefon void Session::close( const ViewInterface* view ) 80*cdd89779SNicolas Bonnefon { 81*cdd89779SNicolas Bonnefon openFiles_.erase( openFiles_.find( view ) ); 82*cdd89779SNicolas Bonnefon } 83*cdd89779SNicolas Bonnefon 8478b9e3f6SNicolas Bonnefon void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize, 8578b9e3f6SNicolas Bonnefon uint32_t* fileNbLine, QDateTime* lastModified ) const 86039481acSNicolas Bonnefon { 8778b9e3f6SNicolas Bonnefon const OpenFile* file = findOpenFileFromView( view ); 8878b9e3f6SNicolas Bonnefon 8978b9e3f6SNicolas Bonnefon assert( file ); 9078b9e3f6SNicolas Bonnefon 9178b9e3f6SNicolas Bonnefon *fileSize = file->logData->getFileSize(); 9278b9e3f6SNicolas Bonnefon *fileNbLine = file->logData->getNbLine(); 9378b9e3f6SNicolas Bonnefon *lastModified = file->logData->getLastModifiedDate(); 94039481acSNicolas Bonnefon } 9578b9e3f6SNicolas Bonnefon 9678b9e3f6SNicolas Bonnefon 9778b9e3f6SNicolas Bonnefon /* 9878b9e3f6SNicolas Bonnefon * Private methods 99039481acSNicolas Bonnefon */ 10078b9e3f6SNicolas Bonnefon 10178b9e3f6SNicolas Bonnefon Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) 10278b9e3f6SNicolas Bonnefon { 10378b9e3f6SNicolas Bonnefon assert( view ); 10478b9e3f6SNicolas Bonnefon 1051b5e406eSNicolas Bonnefon OpenFile* file = &( openFiles_.at( view ) ); 10678b9e3f6SNicolas Bonnefon 10778b9e3f6SNicolas Bonnefon // OpenfileMap::at might throw out_of_range but since a view MUST always 10878b9e3f6SNicolas Bonnefon // be attached to a file, we don't handle it! 10978b9e3f6SNicolas Bonnefon 11078b9e3f6SNicolas Bonnefon return file; 11178b9e3f6SNicolas Bonnefon } 11278b9e3f6SNicolas Bonnefon 11378b9e3f6SNicolas Bonnefon const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const 11478b9e3f6SNicolas Bonnefon { 11578b9e3f6SNicolas Bonnefon assert( view ); 11678b9e3f6SNicolas Bonnefon 1171b5e406eSNicolas Bonnefon const OpenFile* file = &( openFiles_.at( view ) ); 11878b9e3f6SNicolas Bonnefon 11978b9e3f6SNicolas Bonnefon // OpenfileMap::at might throw out_of_range but since a view MUST always 12078b9e3f6SNicolas Bonnefon // be attached to a file, we don't handle it! 12178b9e3f6SNicolas Bonnefon 12278b9e3f6SNicolas Bonnefon return file; 12378b9e3f6SNicolas Bonnefon } 124