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