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