xref: /glogg/src/session.h (revision f0708ca81489bc71275a0d193395638d59f0a661)
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 std::string& 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 std::string& file_name,
53             std::function<ViewInterface*()> view_factory );
54     // Close the file identified by the view passed
55     // Throw an exception if it does not exist.
56     void close( const ViewInterface* view );
57 
58   private:
59     // Open a file without checking if it is existing/readable
60     ViewInterface* openAlways( const std::string& file_name,
61             std::function<ViewInterface*()> view_factory );
62 
63     struct OpenFile {
64         std::string fileName;
65         std::shared_ptr<LogData> logData;
66         std::shared_ptr<LogFilteredData> logFilteredData;
67         ViewInterface* view;
68     };
69 
70     // List of open files
71     typedef std::unordered_map<std::string, OpenFile> OpenFileMap;
72     OpenFileMap open_files;
73 };
74 
75 #endif
76