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