xref: /glogg/src/session.h (revision b57881fa285fa8240ff86bc3a7c4f5766388e84d)
1 /*
2  * Copyright (C) 2013, 2014 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 #include <vector>
27 #include <utility>
28 
29 #include <QDateTime>
30 
31 #include "quickfindpattern.h"
32 
33 class ViewInterface;
34 class LogData;
35 class LogFilteredData;
36 class SavedSearches;
37 
38 // The session is responsible for maintaining the list of open log files
39 // and their association with Views.
40 // It also maintains the domain objects which are common to all log files
41 // (SavedSearches, FileHistory, QFPattern...)
42 class Session {
43   public:
44     Session();
45     ~Session();
46 
47     // No copy/assignment please
48     Session( const Session& ) = delete;
49     Session& operator =( const Session& ) = delete;
50 
51     // Return the view associated to a file if it is open
52     // The filename must be strictly identical to trigger a match
53     // (no match in case of e.g. relative vs. absolute pathname.
54     ViewInterface* getViewIfOpen( const std::string& file_name ) const;
55     // Open a new file, starts its asynchronous loading, and construct a new
56     // view for it (the caller passes a factory to build the concrete view)
57     // The ownership of the view is given to the caller
58     // Throw exceptions if the file is already open or if it cannot be open.
59     ViewInterface* open( const std::string& file_name,
60             std::function<ViewInterface*()> view_factory );
61     // Close the file identified by the view passed
62     // Throw an exception if it does not exist.
63     void close( const ViewInterface* view );
64 
65     // Open all the files listed in the stored session
66     // (see ::open)
67     // returns a vector of pairs (file_name, view) and the index of the
68     // current file (or -1 if none).
69     std::vector<std::pair<std::string, ViewInterface*>> restore(
70             std::function<ViewInterface*()> view_factory,
71             int *current_file_index );
72     // Save the session to persistent storage. An ordered list of
73     // (view, topLine) is passed, this is because only the main window
74     // know the order in which the views are presented to the user (it might
75     // have changed since file were opened).
76     void save(
77             std::vector<std::pair<const ViewInterface*, uint64_t>> view_list );
78 
79     // Get the size (in bytes) and number of lines in the current file.
80     // The file is identified by the view attached to it.
81     void getFileInfo( const ViewInterface* view, uint64_t* fileSize,
82             uint32_t* fileNbLine, QDateTime* lastModified ) const;
83     // Get a (non-const) reference to the QuickFind pattern.
84     std::shared_ptr<QuickFindPattern> getQuickFindPattern() const
85     { return quickFindPattern_; }
86 
87   private:
88     struct OpenFile {
89         std::string fileName;
90         std::shared_ptr<LogData> logData;
91         std::shared_ptr<LogFilteredData> logFilteredData;
92         ViewInterface* view;
93     };
94 
95     // Open a file without checking if it is existing/readable
96     ViewInterface* openAlways( const std::string& file_name,
97             std::function<ViewInterface*()> view_factory );
98     // Find an open file from its associated view
99     OpenFile* findOpenFileFromView( const ViewInterface* view );
100     const OpenFile* findOpenFileFromView( const ViewInterface* view ) const;
101 
102     // List of open files
103     typedef std::unordered_map<const ViewInterface*, OpenFile> OpenFileMap;
104     OpenFileMap openFiles_;
105 
106     // Global search history
107     std::shared_ptr<SavedSearches> savedSearches_;
108 
109     // Global quickfind pattern
110     std::shared_ptr<QuickFindPattern> quickFindPattern_;
111 };
112 
113 #endif
114