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