1 /* 2 * Copyright (C) 2013 Nicolas Bonnefon and other contributors 3 * 4 * This file is part of glogg. 5 * 6 * glogg is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * glogg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "session.h" 21 22 #include <cassert> 23 #include <QFileInfo> 24 25 #include "viewinterface.h" 26 #include "data/logdata.h" 27 #include "data/logfiltereddata.h" 28 29 Session::Session() 30 { 31 } 32 33 Session::~Session() 34 { 35 // FIXME Clean up all the data objects... 36 } 37 38 ViewInterface* Session::open( const std::string& file_name, 39 std::function<ViewInterface*()> view_factory ) 40 { 41 ViewInterface* view = nullptr; 42 43 QFileInfo fileInfo( file_name.c_str() ); 44 if ( fileInfo.isReadable() ) 45 { 46 // Create the data objects 47 auto log_data = std::make_shared<LogData>(); 48 auto log_filtered_data = 49 std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() ); 50 51 view = view_factory(); 52 view->setData( log_data, log_filtered_data ); 53 54 // Insert in the hash 55 open_files_.insert( { view, 56 { file_name, 57 log_data, 58 log_filtered_data, 59 view } } ); 60 61 // Start loading the file 62 log_data->attachFile( QString( file_name.c_str() ) ); 63 } 64 else { 65 // throw 66 } 67 68 return view; 69 } 70 71 void Session::stopLoading( const ViewInterface* view ) 72 { 73 OpenFile* file = findOpenFileFromView( view ); 74 if ( file ) { 75 file->logFilteredData->interruptSearch(); 76 file->logData->interruptLoading(); 77 } 78 } 79 80 void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize, 81 uint32_t* fileNbLine, QDateTime* lastModified ) const 82 { 83 const OpenFile* file = findOpenFileFromView( view ); 84 85 assert( file ); 86 87 *fileSize = file->logData->getFileSize(); 88 *fileNbLine = file->logData->getNbLine(); 89 *lastModified = file->logData->getLastModifiedDate(); 90 } 91 92 93 /* 94 * Private methods 95 */ 96 97 Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) 98 { 99 assert( view ); 100 101 OpenFile* file = &( open_files_.at( view ) ); 102 103 // OpenfileMap::at might throw out_of_range but since a view MUST always 104 // be attached to a file, we don't handle it! 105 106 return file; 107 } 108 109 const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const 110 { 111 assert( view ); 112 113 const OpenFile* file = &( open_files_.at( view ) ); 114 115 // OpenfileMap::at might throw out_of_range but since a view MUST always 116 // be attached to a file, we don't handle it! 117 118 return file; 119 } 120