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