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 #ifndef SESSION_H 21 #define SESSION_H 22 23 #include <memory> 24 #include <unordered_map> 25 #include <string> 26 #include <vector> 27 #include <utility> 28 29 #include <QDateTime> 30 31 #include "quickfindpattern.h" 32 33 class ViewInterface; 34 class LogData; 35 class LogFilteredData; 36 class SavedSearches; 37 38 // The session is responsible for maintaining the list of open log files 39 // and their association with Views. 40 // It also maintains the domain objects which are common to all log files 41 // (SavedSearches, FileHistory, QFPattern...) 42 class Session { 43 public: 44 Session(); 45 ~Session(); 46 47 // No copy/assignment please 48 Session( const Session& ) = delete; 49 Session& operator =( const Session& ) = delete; 50 51 // Return the view associated to a file if it is open 52 // The filename must be strictly identical to trigger a match 53 // (no match in case of e.g. relative vs. absolute pathname. 54 ViewInterface* getViewIfOpen( const std::string& file_name ) const; 55 // Open a new file, starts its asynchronous loading, and construct a new 56 // view for it (the caller passes a factory to build the concrete view) 57 // The ownership of the view is given to the caller 58 // Throw exceptions if the file is already open or if it cannot be open. 59 ViewInterface* open( const std::string& file_name, 60 std::function<ViewInterface*()> view_factory ); 61 // Close the file identified by the view passed 62 // Throw an exception if it does not exist. 63 void close( const ViewInterface* view ); 64 65 // Open all the files listed in the stored session 66 // (see ::open) 67 // returns a vector of pairs (file_name, view) and the index of the 68 // current file (or -1 if none). 69 std::vector<std::pair<std::string, ViewInterface*>> restore( 70 std::function<ViewInterface*()> view_factory, 71 int *current_file_index ); 72 // Save the session to persistent storage. An ordered list of 73 // (view, topLine) is passed, this is because only the main window 74 // know the order in which the views are presented to the user (it might 75 // have changed since file were opened). 76 void save( 77 std::vector<std::pair<const ViewInterface*, uint64_t>> view_list ); 78 79 // Get the file name for the passed view. 80 std::string getFilename( const ViewInterface* view ) const; 81 // Get the size (in bytes) and number of lines in the current file. 82 // The file is identified by the view attached to it. 83 void getFileInfo( const ViewInterface* view, uint64_t* fileSize, 84 uint32_t* fileNbLine, QDateTime* lastModified ) const; 85 // Get a (non-const) reference to the QuickFind pattern. 86 std::shared_ptr<QuickFindPattern> getQuickFindPattern() const 87 { return quickFindPattern_; } 88 89 private: 90 struct OpenFile { 91 std::string fileName; 92 std::shared_ptr<LogData> logData; 93 std::shared_ptr<LogFilteredData> logFilteredData; 94 ViewInterface* view; 95 }; 96 97 // Open a file without checking if it is existing/readable 98 ViewInterface* openAlways( const std::string& file_name, 99 std::function<ViewInterface*()> view_factory ); 100 // Find an open file from its associated view 101 OpenFile* findOpenFileFromView( const ViewInterface* view ); 102 const OpenFile* findOpenFileFromView( const ViewInterface* view ) const; 103 104 // List of open files 105 typedef std::unordered_map<const ViewInterface*, OpenFile> OpenFileMap; 106 OpenFileMap openFiles_; 107 108 // Global search history 109 std::shared_ptr<SavedSearches> savedSearches_; 110 111 // Global quickfind pattern 112 std::shared_ptr<QuickFindPattern> quickFindPattern_; 113 }; 114 115 #endif 116