1d96f3f21SNicolas Bonnefon /* 28964a9adSNicolas Bonnefon * Copyright (C) 2013, 2014 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 22b57881faSNicolas Bonnefon #include "log.h" 23b57881faSNicolas Bonnefon 2478b9e3f6SNicolas Bonnefon #include <cassert> 25039481acSNicolas Bonnefon #include <QFileInfo> 26039481acSNicolas Bonnefon 27d96f3f21SNicolas Bonnefon #include "viewinterface.h" 281b5e406eSNicolas Bonnefon #include "persistentinfo.h" 291b5e406eSNicolas Bonnefon #include "savedsearches.h" 30b57881faSNicolas Bonnefon #include "sessioninfo.h" 31039481acSNicolas Bonnefon #include "data/logdata.h" 32039481acSNicolas Bonnefon #include "data/logfiltereddata.h" 33d96f3f21SNicolas Bonnefon 34d96f3f21SNicolas Bonnefon Session::Session() 35d96f3f21SNicolas Bonnefon { 361b5e406eSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "savedSearches" ) ); 371b5e406eSNicolas Bonnefon 381b5e406eSNicolas Bonnefon // Get the global search history 391b5e406eSNicolas Bonnefon savedSearches_ = std::shared_ptr<SavedSearches>( 401b5e406eSNicolas Bonnefon &(Persistent<SavedSearches>( "savedSearches" )) ); 41b423cd88SNicolas Bonnefon 42b423cd88SNicolas Bonnefon quickFindPattern_ = std::make_shared<QuickFindPattern>(); 43d96f3f21SNicolas Bonnefon } 44d96f3f21SNicolas Bonnefon 45d96f3f21SNicolas Bonnefon Session::~Session() 46d96f3f21SNicolas Bonnefon { 47d96f3f21SNicolas Bonnefon // FIXME Clean up all the data objects... 48d96f3f21SNicolas Bonnefon } 49f0708ca8SNicolas Bonnefon 50f0708ca8SNicolas Bonnefon ViewInterface* Session::open( const std::string& file_name, 51f0708ca8SNicolas Bonnefon std::function<ViewInterface*()> view_factory ) 52f0708ca8SNicolas Bonnefon { 53039481acSNicolas Bonnefon ViewInterface* view = nullptr; 54039481acSNicolas Bonnefon 55039481acSNicolas Bonnefon QFileInfo fileInfo( file_name.c_str() ); 56039481acSNicolas Bonnefon if ( fileInfo.isReadable() ) 57039481acSNicolas Bonnefon { 588964a9adSNicolas Bonnefon return openAlways( file_name, view_factory ); 59039481acSNicolas Bonnefon } 60039481acSNicolas Bonnefon else { 618964a9adSNicolas Bonnefon // FIXME throw 62039481acSNicolas Bonnefon } 63f0708ca8SNicolas Bonnefon 64f0708ca8SNicolas Bonnefon return view; 65f0708ca8SNicolas Bonnefon } 6678b9e3f6SNicolas Bonnefon 67cdd89779SNicolas Bonnefon void Session::close( const ViewInterface* view ) 68cdd89779SNicolas Bonnefon { 69cdd89779SNicolas Bonnefon openFiles_.erase( openFiles_.find( view ) ); 70cdd89779SNicolas Bonnefon } 71cdd89779SNicolas Bonnefon 72b57881faSNicolas Bonnefon void Session::save( 73b57881faSNicolas Bonnefon std::vector<std::pair<const ViewInterface*, uint64_t>> view_list ) 74b57881faSNicolas Bonnefon { 75b57881faSNicolas Bonnefon LOG(logDEBUG) << "Session::save"; 76b57881faSNicolas Bonnefon 77b57881faSNicolas Bonnefon std::vector<SessionInfo::OpenFile> session_files; 78b57881faSNicolas Bonnefon for ( auto view: view_list ) { 79b57881faSNicolas Bonnefon const OpenFile* file = findOpenFileFromView( view.first ); 80b57881faSNicolas Bonnefon assert( file ); 81b57881faSNicolas Bonnefon 82b57881faSNicolas Bonnefon LOG(logDEBUG) << "Saving " << file->fileName << " in session."; 83b57881faSNicolas Bonnefon session_files.push_back( { file->fileName, view.second } ); 84b57881faSNicolas Bonnefon } 85b57881faSNicolas Bonnefon 86b57881faSNicolas Bonnefon SessionInfo& session = Persistent<SessionInfo>( "session" ); 87b57881faSNicolas Bonnefon session.setOpenFiles( session_files ); 88b57881faSNicolas Bonnefon GetPersistentInfo().save( QString( "session" ) ); 89b57881faSNicolas Bonnefon } 90b57881faSNicolas Bonnefon 918964a9adSNicolas Bonnefon std::vector<std::pair<std::string, ViewInterface*>> Session::restore( 928964a9adSNicolas Bonnefon std::function<ViewInterface*()> view_factory, 938964a9adSNicolas Bonnefon int *current_file_index ) 948964a9adSNicolas Bonnefon { 95b57881faSNicolas Bonnefon GetPersistentInfo().retrieve( QString( "session" ) ); 96b57881faSNicolas Bonnefon SessionInfo& session = Persistent<SessionInfo>( "session" ); 97b57881faSNicolas Bonnefon 98b57881faSNicolas Bonnefon std::vector<SessionInfo::OpenFile> session_files = session.openFiles(); 998964a9adSNicolas Bonnefon std::vector<std::pair<std::string, ViewInterface*>> result; 1008964a9adSNicolas Bonnefon 1018964a9adSNicolas Bonnefon for ( auto file: session_files ) 1028964a9adSNicolas Bonnefon { 103*60864ff5SNicolas Bonnefon LOG(logDEBUG) << "Create view for " << file.fileName; 104b57881faSNicolas Bonnefon ViewInterface* view = openAlways( file.fileName, view_factory ); 105b57881faSNicolas Bonnefon result.push_back( { file.fileName, view } ); 1068964a9adSNicolas Bonnefon } 1078964a9adSNicolas Bonnefon 1088964a9adSNicolas Bonnefon *current_file_index = -1; 1098964a9adSNicolas Bonnefon 1108964a9adSNicolas Bonnefon return result; 1118964a9adSNicolas Bonnefon } 1128964a9adSNicolas Bonnefon 1130e97f16dSNicolas Bonnefon std::string Session::getFilename( const ViewInterface* view ) const 1140e97f16dSNicolas Bonnefon { 1150e97f16dSNicolas Bonnefon const OpenFile* file = findOpenFileFromView( view ); 1160e97f16dSNicolas Bonnefon 1170e97f16dSNicolas Bonnefon assert( file ); 1180e97f16dSNicolas Bonnefon 1190e97f16dSNicolas Bonnefon return file->fileName; 1200e97f16dSNicolas Bonnefon } 1210e97f16dSNicolas Bonnefon 12278b9e3f6SNicolas Bonnefon void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize, 12378b9e3f6SNicolas Bonnefon uint32_t* fileNbLine, QDateTime* lastModified ) const 124039481acSNicolas Bonnefon { 12578b9e3f6SNicolas Bonnefon const OpenFile* file = findOpenFileFromView( view ); 12678b9e3f6SNicolas Bonnefon 12778b9e3f6SNicolas Bonnefon assert( file ); 12878b9e3f6SNicolas Bonnefon 12978b9e3f6SNicolas Bonnefon *fileSize = file->logData->getFileSize(); 13078b9e3f6SNicolas Bonnefon *fileNbLine = file->logData->getNbLine(); 13178b9e3f6SNicolas Bonnefon *lastModified = file->logData->getLastModifiedDate(); 132039481acSNicolas Bonnefon } 13378b9e3f6SNicolas Bonnefon 13478b9e3f6SNicolas Bonnefon 13578b9e3f6SNicolas Bonnefon /* 13678b9e3f6SNicolas Bonnefon * Private methods 137039481acSNicolas Bonnefon */ 13878b9e3f6SNicolas Bonnefon 1398964a9adSNicolas Bonnefon ViewInterface* Session::openAlways( const std::string& file_name, 1408964a9adSNicolas Bonnefon std::function<ViewInterface*()> view_factory ) 1418964a9adSNicolas Bonnefon { 1428964a9adSNicolas Bonnefon // Create the data objects 1438964a9adSNicolas Bonnefon auto log_data = std::make_shared<LogData>(); 1448964a9adSNicolas Bonnefon auto log_filtered_data = 1458964a9adSNicolas Bonnefon std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() ); 1468964a9adSNicolas Bonnefon 1478964a9adSNicolas Bonnefon ViewInterface* view = view_factory(); 1488964a9adSNicolas Bonnefon view->setData( log_data, log_filtered_data ); 1498964a9adSNicolas Bonnefon view->setQuickFindPattern( quickFindPattern_ ); 1508964a9adSNicolas Bonnefon view->setSavedSearches( savedSearches_ ); 1518964a9adSNicolas Bonnefon 1528964a9adSNicolas Bonnefon // Insert in the hash 1538964a9adSNicolas Bonnefon openFiles_.insert( { view, 1548964a9adSNicolas Bonnefon { file_name, 1558964a9adSNicolas Bonnefon log_data, 1568964a9adSNicolas Bonnefon log_filtered_data, 1578964a9adSNicolas Bonnefon view } } ); 1588964a9adSNicolas Bonnefon 1598964a9adSNicolas Bonnefon // Start loading the file 1608964a9adSNicolas Bonnefon log_data->attachFile( QString( file_name.c_str() ) ); 1618964a9adSNicolas Bonnefon 1628964a9adSNicolas Bonnefon return view; 1638964a9adSNicolas Bonnefon } 1648964a9adSNicolas Bonnefon 16578b9e3f6SNicolas Bonnefon Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) 16678b9e3f6SNicolas Bonnefon { 16778b9e3f6SNicolas Bonnefon assert( view ); 16878b9e3f6SNicolas Bonnefon 1691b5e406eSNicolas Bonnefon OpenFile* file = &( openFiles_.at( view ) ); 17078b9e3f6SNicolas Bonnefon 17178b9e3f6SNicolas Bonnefon // OpenfileMap::at might throw out_of_range but since a view MUST always 17278b9e3f6SNicolas Bonnefon // be attached to a file, we don't handle it! 17378b9e3f6SNicolas Bonnefon 17478b9e3f6SNicolas Bonnefon return file; 17578b9e3f6SNicolas Bonnefon } 17678b9e3f6SNicolas Bonnefon 17778b9e3f6SNicolas Bonnefon const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const 17878b9e3f6SNicolas Bonnefon { 17978b9e3f6SNicolas Bonnefon assert( view ); 18078b9e3f6SNicolas Bonnefon 1811b5e406eSNicolas Bonnefon const OpenFile* file = &( openFiles_.at( view ) ); 18278b9e3f6SNicolas Bonnefon 18378b9e3f6SNicolas Bonnefon // OpenfileMap::at might throw out_of_range but since a view MUST always 18478b9e3f6SNicolas Bonnefon // be attached to a file, we don't handle it! 18578b9e3f6SNicolas Bonnefon 18678b9e3f6SNicolas Bonnefon return file; 18778b9e3f6SNicolas Bonnefon } 188