xref: /glogg/src/session.h (revision d96f3f211e1e2a3a5b219145986c18e10b0f96aa)
1 /*
2  * Copyright (C) 2013 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 
27 class ViewInterface;
28 class LogData;
29 class LogFilteredData;
30 
31 // The session is responsible for maintaining the list of open log files
32 // and their association with Views.
33 // It also maintains the domain objects which are common to all log files
34 // (SearchHistory, FileHistory, QFPattern...)
35 class Session {
36   public:
37     Session();
38     ~Session();
39 
40     // No copy/assignment please
41     Session( const Session& ) = delete;
42     Session& operator =( const Session& ) = delete;
43 
44     // Return the view associated to a file if it is open
45     // The filename must be strictly identical to trigger a match
46     // (no match in case of e.g. relative vs. absolute pathname.
47     ViewInterface* getViewIfOpen( const char* file_name ) const;
48     // Open a new file, and construct a new view for it (the caller passes a factory
49     // to build the concrete view)
50     // The ownership of the view is given to the caller
51     // Throw exceptions if the file is already open or if it cannot be open.
52     ViewInterface* open( const char* file_name, std::function<ViewInterface*()> view_factory );
53     // Close the file identified by the view passed
54     // Throw an exception if it does not exist.
55     void close( const ViewInterface* view );
56 
57   private:
58     // Open a file without checking if it is existing/readable
59     ViewInterface* openAlways( const char* file_name, std::function<ViewInterface*()> view_factory );
60 
61     struct OpenFile {
62         std::string fileName;
63         std::shared_ptr<LogData> logData;
64         std::shared_ptr<LogFilteredData> logFilteredData;
65         ViewInterface* view;
66     };
67 
68     // List of open files
69     typedef std::unordered_map<std::string, OpenFile> OpenFileMap;
70     OpenFileMap open_files;
71 };
72 
73 #endif
74