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" )) ); 38*b423cd88SNicolas Bonnefon 39*b423cd88SNicolas Bonnefon quickFindPattern_ = std::make_shared<QuickFindPattern>(); 40d96f3f21SNicolas Bonnefon } 41d96f3f21SNicolas Bonnefon 42d96f3f21SNicolas Bonnefon Session::~Session() 43d96f3f21SNicolas Bonnefon { 44d96f3f21SNicolas Bonnefon // FIXME Clean up all the data objects... 45d96f3f21SNicolas Bonnefon } 46f0708ca8SNicolas Bonnefon 47f0708ca8SNicolas Bonnefon ViewInterface* Session::open( const std::string& file_name, 48f0708ca8SNicolas Bonnefon std::function<ViewInterface*()> view_factory ) 49f0708ca8SNicolas Bonnefon { 50039481acSNicolas Bonnefon ViewInterface* view = nullptr; 51039481acSNicolas Bonnefon 52039481acSNicolas Bonnefon QFileInfo fileInfo( file_name.c_str() ); 53039481acSNicolas Bonnefon if ( fileInfo.isReadable() ) 54039481acSNicolas Bonnefon { 55039481acSNicolas Bonnefon // Create the data objects 5678b9e3f6SNicolas Bonnefon auto log_data = std::make_shared<LogData>(); 5778b9e3f6SNicolas Bonnefon auto log_filtered_data = 5878b9e3f6SNicolas Bonnefon std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() ); 59039481acSNicolas Bonnefon 60039481acSNicolas Bonnefon view = view_factory(); 6178b9e3f6SNicolas Bonnefon view->setData( log_data, log_filtered_data ); 62*b423cd88SNicolas Bonnefon view->setQuickFindPattern( quickFindPattern_ ); 631b5e406eSNicolas Bonnefon view->setSavedSearches( savedSearches_ ); 6478b9e3f6SNicolas Bonnefon 6578b9e3f6SNicolas Bonnefon // Insert in the hash 661b5e406eSNicolas Bonnefon openFiles_.insert( { view, 6778b9e3f6SNicolas Bonnefon { file_name, 6878b9e3f6SNicolas Bonnefon log_data, 6978b9e3f6SNicolas Bonnefon log_filtered_data, 7078b9e3f6SNicolas Bonnefon view } } ); 71039481acSNicolas Bonnefon 72039481acSNicolas Bonnefon // Start loading the file 7378b9e3f6SNicolas Bonnefon log_data->attachFile( QString( file_name.c_str() ) ); 74039481acSNicolas Bonnefon } 75039481acSNicolas Bonnefon else { 76039481acSNicolas Bonnefon // throw 77039481acSNicolas Bonnefon } 78f0708ca8SNicolas Bonnefon 79f0708ca8SNicolas Bonnefon return view; 80f0708ca8SNicolas Bonnefon } 8178b9e3f6SNicolas Bonnefon 82cdd89779SNicolas Bonnefon void Session::close( const ViewInterface* view ) 83cdd89779SNicolas Bonnefon { 84cdd89779SNicolas Bonnefon openFiles_.erase( openFiles_.find( view ) ); 85cdd89779SNicolas Bonnefon } 86cdd89779SNicolas Bonnefon 8778b9e3f6SNicolas Bonnefon void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize, 8878b9e3f6SNicolas Bonnefon uint32_t* fileNbLine, QDateTime* lastModified ) const 89039481acSNicolas Bonnefon { 9078b9e3f6SNicolas Bonnefon const OpenFile* file = findOpenFileFromView( view ); 9178b9e3f6SNicolas Bonnefon 9278b9e3f6SNicolas Bonnefon assert( file ); 9378b9e3f6SNicolas Bonnefon 9478b9e3f6SNicolas Bonnefon *fileSize = file->logData->getFileSize(); 9578b9e3f6SNicolas Bonnefon *fileNbLine = file->logData->getNbLine(); 9678b9e3f6SNicolas Bonnefon *lastModified = file->logData->getLastModifiedDate(); 97039481acSNicolas Bonnefon } 9878b9e3f6SNicolas Bonnefon 9978b9e3f6SNicolas Bonnefon 10078b9e3f6SNicolas Bonnefon /* 10178b9e3f6SNicolas Bonnefon * Private methods 102039481acSNicolas Bonnefon */ 10378b9e3f6SNicolas Bonnefon 10478b9e3f6SNicolas Bonnefon Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) 10578b9e3f6SNicolas Bonnefon { 10678b9e3f6SNicolas Bonnefon assert( view ); 10778b9e3f6SNicolas Bonnefon 1081b5e406eSNicolas Bonnefon OpenFile* file = &( openFiles_.at( view ) ); 10978b9e3f6SNicolas Bonnefon 11078b9e3f6SNicolas Bonnefon // OpenfileMap::at might throw out_of_range but since a view MUST always 11178b9e3f6SNicolas Bonnefon // be attached to a file, we don't handle it! 11278b9e3f6SNicolas Bonnefon 11378b9e3f6SNicolas Bonnefon return file; 11478b9e3f6SNicolas Bonnefon } 11578b9e3f6SNicolas Bonnefon 11678b9e3f6SNicolas Bonnefon const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const 11778b9e3f6SNicolas Bonnefon { 11878b9e3f6SNicolas Bonnefon assert( view ); 11978b9e3f6SNicolas Bonnefon 1201b5e406eSNicolas Bonnefon const OpenFile* file = &( openFiles_.at( view ) ); 12178b9e3f6SNicolas Bonnefon 12278b9e3f6SNicolas Bonnefon // OpenfileMap::at might throw out_of_range but since a view MUST always 12378b9e3f6SNicolas Bonnefon // be attached to a file, we don't handle it! 12478b9e3f6SNicolas Bonnefon 12578b9e3f6SNicolas Bonnefon return file; 12678b9e3f6SNicolas Bonnefon } 127