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