xref: /glogg/src/session.h (revision a44d09bc709514bfc9e2b6b4e435e83e8a36868a)
1d96f3f21SNicolas Bonnefon /*
28964a9adSNicolas Bonnefon  * Copyright (C) 2013, 2014 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>
268964a9adSNicolas Bonnefon #include <vector>
278964a9adSNicolas Bonnefon #include <utility>
28d96f3f21SNicolas Bonnefon 
2978b9e3f6SNicolas Bonnefon #include <QDateTime>
3078b9e3f6SNicolas Bonnefon 
31b423cd88SNicolas Bonnefon #include "quickfindpattern.h"
32b423cd88SNicolas Bonnefon 
33d96f3f21SNicolas Bonnefon class ViewInterface;
34*a44d09bcSNicolas Bonnefon class ViewContextInterface;
35d96f3f21SNicolas Bonnefon class LogData;
36d96f3f21SNicolas Bonnefon class LogFilteredData;
371b5e406eSNicolas Bonnefon class SavedSearches;
38d96f3f21SNicolas Bonnefon 
39a3b56311SNicolas Bonnefon // File unreadable error
40a3b56311SNicolas Bonnefon class FileUnreadableErr {};
41a3b56311SNicolas Bonnefon 
42d96f3f21SNicolas Bonnefon // The session is responsible for maintaining the list of open log files
43d96f3f21SNicolas Bonnefon // and their association with Views.
44d96f3f21SNicolas Bonnefon // It also maintains the domain objects which are common to all log files
451b5e406eSNicolas Bonnefon // (SavedSearches, FileHistory, QFPattern...)
46d96f3f21SNicolas Bonnefon class Session {
47d96f3f21SNicolas Bonnefon   public:
48d96f3f21SNicolas Bonnefon     Session();
49d96f3f21SNicolas Bonnefon     ~Session();
50d96f3f21SNicolas Bonnefon 
51d96f3f21SNicolas Bonnefon     // No copy/assignment please
52d96f3f21SNicolas Bonnefon     Session( const Session& ) = delete;
53d96f3f21SNicolas Bonnefon     Session& operator =( const Session& ) = delete;
54d96f3f21SNicolas Bonnefon 
55d96f3f21SNicolas Bonnefon     // Return the view associated to a file if it is open
56d96f3f21SNicolas Bonnefon     // The filename must be strictly identical to trigger a match
57d96f3f21SNicolas Bonnefon     // (no match in case of e.g. relative vs. absolute pathname.
58f0708ca8SNicolas Bonnefon     ViewInterface* getViewIfOpen( const std::string& file_name ) const;
5978b9e3f6SNicolas Bonnefon     // Open a new file, starts its asynchronous loading, and construct a new
6078b9e3f6SNicolas Bonnefon     // view for it (the caller passes a factory to build the concrete view)
61d96f3f21SNicolas Bonnefon     // The ownership of the view is given to the caller
62d96f3f21SNicolas Bonnefon     // Throw exceptions if the file is already open or if it cannot be open.
63f0708ca8SNicolas Bonnefon     ViewInterface* open( const std::string& file_name,
64f0708ca8SNicolas Bonnefon             std::function<ViewInterface*()> view_factory );
65d96f3f21SNicolas Bonnefon     // Close the file identified by the view passed
66d96f3f21SNicolas Bonnefon     // Throw an exception if it does not exist.
67d96f3f21SNicolas Bonnefon     void close( const ViewInterface* view );
68b57881faSNicolas Bonnefon 
698964a9adSNicolas Bonnefon     // Open all the files listed in the stored session
708964a9adSNicolas Bonnefon     // (see ::open)
718964a9adSNicolas Bonnefon     // returns a vector of pairs (file_name, view) and the index of the
728964a9adSNicolas Bonnefon     // current file (or -1 if none).
738964a9adSNicolas Bonnefon     std::vector<std::pair<std::string, ViewInterface*>> restore(
748964a9adSNicolas Bonnefon             std::function<ViewInterface*()> view_factory,
758964a9adSNicolas Bonnefon             int *current_file_index );
76b57881faSNicolas Bonnefon     // Save the session to persistent storage. An ordered list of
77*a44d09bcSNicolas Bonnefon     // (view, topline, ViewContextInterface) is passed, this is because only
78*a44d09bcSNicolas Bonnefon     // the main window // know the order in which the views are presented to
79*a44d09bcSNicolas Bonnefon     // the user (it might // have changed since file were opened).
80*a44d09bcSNicolas Bonnefon     void save( std::vector<
81*a44d09bcSNicolas Bonnefon                    std::tuple<const ViewInterface*, uint64_t, std::shared_ptr<const ViewContextInterface>>
82*a44d09bcSNicolas Bonnefon                > view_list );
83b57881faSNicolas Bonnefon 
840e97f16dSNicolas Bonnefon     // Get the file name for the passed view.
850e97f16dSNicolas Bonnefon     std::string getFilename( const ViewInterface* view ) const;
8678b9e3f6SNicolas Bonnefon     // Get the size (in bytes) and number of lines in the current file.
8778b9e3f6SNicolas Bonnefon     // The file is identified by the view attached to it.
8878b9e3f6SNicolas Bonnefon     void getFileInfo( const ViewInterface* view, uint64_t* fileSize,
8978b9e3f6SNicolas Bonnefon             uint32_t* fileNbLine, QDateTime* lastModified ) const;
90b423cd88SNicolas Bonnefon     // Get a (non-const) reference to the QuickFind pattern.
91b423cd88SNicolas Bonnefon     std::shared_ptr<QuickFindPattern> getQuickFindPattern() const
92b423cd88SNicolas Bonnefon     { return quickFindPattern_; }
93d96f3f21SNicolas Bonnefon 
94d96f3f21SNicolas Bonnefon   private:
95d96f3f21SNicolas Bonnefon     struct OpenFile {
96d96f3f21SNicolas Bonnefon         std::string fileName;
97d96f3f21SNicolas Bonnefon         std::shared_ptr<LogData> logData;
98d96f3f21SNicolas Bonnefon         std::shared_ptr<LogFilteredData> logFilteredData;
99d96f3f21SNicolas Bonnefon         ViewInterface* view;
100d96f3f21SNicolas Bonnefon     };
101d96f3f21SNicolas Bonnefon 
10278b9e3f6SNicolas Bonnefon     // Open a file without checking if it is existing/readable
10378b9e3f6SNicolas Bonnefon     ViewInterface* openAlways( const std::string& file_name,
104*a44d09bcSNicolas Bonnefon             std::function<ViewInterface*()> view_factory,
105*a44d09bcSNicolas Bonnefon             const char* view_context );
10678b9e3f6SNicolas Bonnefon     // Find an open file from its associated view
10778b9e3f6SNicolas Bonnefon     OpenFile* findOpenFileFromView( const ViewInterface* view );
10878b9e3f6SNicolas Bonnefon     const OpenFile* findOpenFileFromView( const ViewInterface* view ) const;
109039481acSNicolas Bonnefon 
11078b9e3f6SNicolas Bonnefon     // List of open files
11178b9e3f6SNicolas Bonnefon     typedef std::unordered_map<const ViewInterface*, OpenFile> OpenFileMap;
1121b5e406eSNicolas Bonnefon     OpenFileMap openFiles_;
1131b5e406eSNicolas Bonnefon 
1141b5e406eSNicolas Bonnefon     // Global search history
1151b5e406eSNicolas Bonnefon     std::shared_ptr<SavedSearches> savedSearches_;
116b423cd88SNicolas Bonnefon 
117b423cd88SNicolas Bonnefon     // Global quickfind pattern
118b423cd88SNicolas Bonnefon     std::shared_ptr<QuickFindPattern> quickFindPattern_;
119d96f3f21SNicolas Bonnefon };
120d96f3f21SNicolas Bonnefon 
121d96f3f21SNicolas Bonnefon #endif
122