xref: /glogg/src/session.h (revision d96f3f211e1e2a3a5b219145986c18e10b0f96aa)
1*d96f3f21SNicolas Bonnefon /*
2*d96f3f21SNicolas Bonnefon  * Copyright (C) 2013 Nicolas Bonnefon and other contributors
3*d96f3f21SNicolas Bonnefon  *
4*d96f3f21SNicolas Bonnefon  * This file is part of glogg.
5*d96f3f21SNicolas Bonnefon  *
6*d96f3f21SNicolas Bonnefon  * glogg is free software: you can redistribute it and/or modify
7*d96f3f21SNicolas Bonnefon  * it under the terms of the GNU General Public License as published by
8*d96f3f21SNicolas Bonnefon  * the Free Software Foundation, either version 3 of the License, or
9*d96f3f21SNicolas Bonnefon  * (at your option) any later version.
10*d96f3f21SNicolas Bonnefon  *
11*d96f3f21SNicolas Bonnefon  * glogg is distributed in the hope that it will be useful,
12*d96f3f21SNicolas Bonnefon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*d96f3f21SNicolas Bonnefon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*d96f3f21SNicolas Bonnefon  * GNU General Public License for more details.
15*d96f3f21SNicolas Bonnefon  *
16*d96f3f21SNicolas Bonnefon  * You should have received a copy of the GNU General Public License
17*d96f3f21SNicolas Bonnefon  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18*d96f3f21SNicolas Bonnefon  */
19*d96f3f21SNicolas Bonnefon 
20*d96f3f21SNicolas Bonnefon #ifndef SESSION_H
21*d96f3f21SNicolas Bonnefon #define SESSION_H
22*d96f3f21SNicolas Bonnefon 
23*d96f3f21SNicolas Bonnefon #include <memory>
24*d96f3f21SNicolas Bonnefon #include <unordered_map>
25*d96f3f21SNicolas Bonnefon #include <string>
26*d96f3f21SNicolas Bonnefon 
27*d96f3f21SNicolas Bonnefon class ViewInterface;
28*d96f3f21SNicolas Bonnefon class LogData;
29*d96f3f21SNicolas Bonnefon class LogFilteredData;
30*d96f3f21SNicolas Bonnefon 
31*d96f3f21SNicolas Bonnefon // The session is responsible for maintaining the list of open log files
32*d96f3f21SNicolas Bonnefon // and their association with Views.
33*d96f3f21SNicolas Bonnefon // It also maintains the domain objects which are common to all log files
34*d96f3f21SNicolas Bonnefon // (SearchHistory, FileHistory, QFPattern...)
35*d96f3f21SNicolas Bonnefon class Session {
36*d96f3f21SNicolas Bonnefon   public:
37*d96f3f21SNicolas Bonnefon     Session();
38*d96f3f21SNicolas Bonnefon     ~Session();
39*d96f3f21SNicolas Bonnefon 
40*d96f3f21SNicolas Bonnefon     // No copy/assignment please
41*d96f3f21SNicolas Bonnefon     Session( const Session& ) = delete;
42*d96f3f21SNicolas Bonnefon     Session& operator =( const Session& ) = delete;
43*d96f3f21SNicolas Bonnefon 
44*d96f3f21SNicolas Bonnefon     // Return the view associated to a file if it is open
45*d96f3f21SNicolas Bonnefon     // The filename must be strictly identical to trigger a match
46*d96f3f21SNicolas Bonnefon     // (no match in case of e.g. relative vs. absolute pathname.
47*d96f3f21SNicolas Bonnefon     ViewInterface* getViewIfOpen( const char* file_name ) const;
48*d96f3f21SNicolas Bonnefon     // Open a new file, and construct a new view for it (the caller passes a factory
49*d96f3f21SNicolas Bonnefon     // to build the concrete view)
50*d96f3f21SNicolas Bonnefon     // The ownership of the view is given to the caller
51*d96f3f21SNicolas Bonnefon     // Throw exceptions if the file is already open or if it cannot be open.
52*d96f3f21SNicolas Bonnefon     ViewInterface* open( const char* file_name, std::function<ViewInterface*()> view_factory );
53*d96f3f21SNicolas Bonnefon     // Close the file identified by the view passed
54*d96f3f21SNicolas Bonnefon     // Throw an exception if it does not exist.
55*d96f3f21SNicolas Bonnefon     void close( const ViewInterface* view );
56*d96f3f21SNicolas Bonnefon 
57*d96f3f21SNicolas Bonnefon   private:
58*d96f3f21SNicolas Bonnefon     // Open a file without checking if it is existing/readable
59*d96f3f21SNicolas Bonnefon     ViewInterface* openAlways( const char* file_name, std::function<ViewInterface*()> view_factory );
60*d96f3f21SNicolas Bonnefon 
61*d96f3f21SNicolas Bonnefon     struct OpenFile {
62*d96f3f21SNicolas Bonnefon         std::string fileName;
63*d96f3f21SNicolas Bonnefon         std::shared_ptr<LogData> logData;
64*d96f3f21SNicolas Bonnefon         std::shared_ptr<LogFilteredData> logFilteredData;
65*d96f3f21SNicolas Bonnefon         ViewInterface* view;
66*d96f3f21SNicolas Bonnefon     };
67*d96f3f21SNicolas Bonnefon 
68*d96f3f21SNicolas Bonnefon     // List of open files
69*d96f3f21SNicolas Bonnefon     typedef std::unordered_map<std::string, OpenFile> OpenFileMap;
70*d96f3f21SNicolas Bonnefon     OpenFileMap open_files;
71*d96f3f21SNicolas Bonnefon };
72*d96f3f21SNicolas Bonnefon 
73*d96f3f21SNicolas Bonnefon #endif
74