xref: /glogg/src/session.cpp (revision 11582726a85c08832d009bfe179074b8d1152d21)
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 #include "session.h"
21 
22 #include "log.h"
23 
24 #include <cassert>
25 #include <QFileInfo>
26 
27 #include "viewinterface.h"
28 #include "persistentinfo.h"
29 #include "savedsearches.h"
30 #include "sessioninfo.h"
31 #include "data/logdata.h"
32 #include "data/logfiltereddata.h"
33 
34 Session::Session()
35 {
36     GetPersistentInfo().retrieve( QString( "savedSearches" ) );
37 
38     // Get the global search history (it remains the property
39     // of the Persistent)
40     savedSearches_ = Persistent<SavedSearches>( "savedSearches" );
41 
42     quickFindPattern_ = std::make_shared<QuickFindPattern>();
43 }
44 
45 Session::~Session()
46 {
47     // FIXME Clean up all the data objects...
48 }
49 
50 ViewInterface* Session::open( const std::string& file_name,
51         std::function<ViewInterface*()> view_factory )
52 {
53     ViewInterface* view = nullptr;
54 
55     QFileInfo fileInfo( file_name.c_str() );
56     if ( fileInfo.isReadable() ) {
57         return openAlways( file_name, view_factory );
58     }
59     else {
60         throw FileUnreadableErr();
61     }
62 
63     return view;
64 }
65 
66 void Session::close( const ViewInterface* view )
67 {
68     openFiles_.erase( openFiles_.find( view ) );
69 }
70 
71 void Session::save(
72         std::vector<std::pair<const ViewInterface*, uint64_t>> view_list )
73 {
74     LOG(logDEBUG) << "Session::save";
75 
76     std::vector<SessionInfo::OpenFile> session_files;
77     for ( auto view: view_list ) {
78         const OpenFile* file = findOpenFileFromView( view.first );
79         assert( file );
80 
81         LOG(logDEBUG) << "Saving " << file->fileName << " in session.";
82         session_files.push_back( { file->fileName, view.second } );
83     }
84 
85     std::shared_ptr<SessionInfo> session =
86         Persistent<SessionInfo>( "session" );
87     session->setOpenFiles( session_files );
88     GetPersistentInfo().save( QString( "session" ) );
89 }
90 
91 std::vector<std::pair<std::string, ViewInterface*>> Session::restore(
92         std::function<ViewInterface*()> view_factory,
93         int *current_file_index )
94 {
95     GetPersistentInfo().retrieve( QString( "session" ) );
96     std::shared_ptr<SessionInfo> session =
97         Persistent<SessionInfo>( "session" );
98 
99     std::vector<SessionInfo::OpenFile> session_files = session->openFiles();
100     std::vector<std::pair<std::string, ViewInterface*>> result;
101 
102     for ( auto file: session_files )
103     {
104         LOG(logDEBUG) << "Create view for " << file.fileName;
105         ViewInterface* view = openAlways( file.fileName, view_factory );
106         result.push_back( { file.fileName, view } );
107     }
108 
109     *current_file_index = -1;
110 
111     return result;
112 }
113 
114 std::string Session::getFilename( const ViewInterface* view ) const
115 {
116     const OpenFile* file = findOpenFileFromView( view );
117 
118     assert( file );
119 
120     return file->fileName;
121 }
122 
123 void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
124         uint32_t* fileNbLine, QDateTime* lastModified ) const
125 {
126     const OpenFile* file = findOpenFileFromView( view );
127 
128     assert( file );
129 
130     *fileSize = file->logData->getFileSize();
131     *fileNbLine = file->logData->getNbLine();
132     *lastModified = file->logData->getLastModifiedDate();
133 }
134 
135 
136 /*
137  * Private methods
138  */
139 
140 ViewInterface* Session::openAlways( const std::string& file_name,
141         std::function<ViewInterface*()> view_factory )
142 {
143     // Create the data objects
144     auto log_data          = std::make_shared<LogData>();
145     auto log_filtered_data =
146         std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
147 
148     ViewInterface* view = view_factory();
149     view->setData( log_data, log_filtered_data );
150     view->setQuickFindPattern( quickFindPattern_ );
151     view->setSavedSearches( savedSearches_ );
152 
153     // Insert in the hash
154     openFiles_.insert( { view,
155             { file_name,
156             log_data,
157             log_filtered_data,
158             view } } );
159 
160     // Start loading the file
161     log_data->attachFile( QString( file_name.c_str() ) );
162 
163     return view;
164 }
165 
166 Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
167 {
168     assert( view );
169 
170     OpenFile* file = &( openFiles_.at( view ) );
171 
172     // OpenfileMap::at might throw out_of_range but since a view MUST always
173     // be attached to a file, we don't handle it!
174 
175     return file;
176 }
177 
178 const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
179 {
180     assert( view );
181 
182     const OpenFile* file = &( openFiles_.at( view ) );
183 
184     // OpenfileMap::at might throw out_of_range but since a view MUST always
185     // be attached to a file, we don't handle it!
186 
187     return file;
188 }
189