1 /* 2 * Copyright (C) 2013, 2014 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 "log.h" 23 24 #include <cassert> 25 #include <QFileInfo> 26 27 #include "viewinterface.h" 28 #include "persistentinfo.h" 29 #include "savedsearches.h" 30 #include "sessioninfo.h" 31 #include "data/logdata.h" 32 #include "data/logfiltereddata.h" 33 34 Session::Session() 35 { 36 GetPersistentInfo().retrieve( QString( "savedSearches" ) ); 37 38 // Get the global search history 39 savedSearches_ = std::shared_ptr<SavedSearches>( 40 &(Persistent<SavedSearches>( "savedSearches" )) ); 41 42 quickFindPattern_ = std::make_shared<QuickFindPattern>(); 43 } 44 45 Session::~Session() 46 { 47 // FIXME Clean up all the data objects... 48 } 49 50 ViewInterface* Session::open( const std::string& file_name, 51 std::function<ViewInterface*()> view_factory ) 52 { 53 ViewInterface* view = nullptr; 54 55 QFileInfo fileInfo( file_name.c_str() ); 56 if ( fileInfo.isReadable() ) 57 { 58 return openAlways( file_name, view_factory ); 59 } 60 else { 61 // FIXME throw 62 } 63 64 return view; 65 } 66 67 void Session::close( const ViewInterface* view ) 68 { 69 openFiles_.erase( openFiles_.find( view ) ); 70 } 71 72 void Session::save( 73 std::vector<std::pair<const ViewInterface*, uint64_t>> view_list ) 74 { 75 LOG(logDEBUG) << "Session::save"; 76 77 std::vector<SessionInfo::OpenFile> session_files; 78 for ( auto view: view_list ) { 79 const OpenFile* file = findOpenFileFromView( view.first ); 80 assert( file ); 81 82 LOG(logDEBUG) << "Saving " << file->fileName << " in session."; 83 session_files.push_back( { file->fileName, view.second } ); 84 } 85 86 SessionInfo& session = Persistent<SessionInfo>( "session" ); 87 session.setOpenFiles( session_files ); 88 GetPersistentInfo().save( QString( "session" ) ); 89 } 90 91 std::vector<std::pair<std::string, ViewInterface*>> Session::restore( 92 std::function<ViewInterface*()> view_factory, 93 int *current_file_index ) 94 { 95 GetPersistentInfo().retrieve( QString( "session" ) ); 96 SessionInfo& session = Persistent<SessionInfo>( "session" ); 97 98 std::vector<SessionInfo::OpenFile> session_files = session.openFiles(); 99 std::vector<std::pair<std::string, ViewInterface*>> result; 100 101 for ( auto file: session_files ) 102 { 103 LOG(logDEBUG) << "Create view for " << file.fileName; 104 ViewInterface* view = openAlways( file.fileName, view_factory ); 105 result.push_back( { file.fileName, view } ); 106 } 107 108 *current_file_index = -1; 109 110 return result; 111 } 112 113 std::string Session::getFilename( const ViewInterface* view ) const 114 { 115 const OpenFile* file = findOpenFileFromView( view ); 116 117 assert( file ); 118 119 return file->fileName; 120 } 121 122 void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize, 123 uint32_t* fileNbLine, QDateTime* lastModified ) const 124 { 125 const OpenFile* file = findOpenFileFromView( view ); 126 127 assert( file ); 128 129 *fileSize = file->logData->getFileSize(); 130 *fileNbLine = file->logData->getNbLine(); 131 *lastModified = file->logData->getLastModifiedDate(); 132 } 133 134 135 /* 136 * Private methods 137 */ 138 139 ViewInterface* Session::openAlways( const std::string& file_name, 140 std::function<ViewInterface*()> view_factory ) 141 { 142 // Create the data objects 143 auto log_data = std::make_shared<LogData>(); 144 auto log_filtered_data = 145 std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() ); 146 147 ViewInterface* view = view_factory(); 148 view->setData( log_data, log_filtered_data ); 149 view->setQuickFindPattern( quickFindPattern_ ); 150 view->setSavedSearches( savedSearches_ ); 151 152 // Insert in the hash 153 openFiles_.insert( { view, 154 { file_name, 155 log_data, 156 log_filtered_data, 157 view } } ); 158 159 // Start loading the file 160 log_data->attachFile( QString( file_name.c_str() ) ); 161 162 return view; 163 } 164 165 Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) 166 { 167 assert( view ); 168 169 OpenFile* file = &( openFiles_.at( view ) ); 170 171 // OpenfileMap::at might throw out_of_range but since a view MUST always 172 // be attached to a file, we don't handle it! 173 174 return file; 175 } 176 177 const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const 178 { 179 assert( view ); 180 181 const OpenFile* file = &( openFiles_.at( view ) ); 182 183 // OpenfileMap::at might throw out_of_range but since a view MUST always 184 // be attached to a file, we don't handle it! 185 186 return file; 187 } 188