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