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