xref: /glogg/src/session.cpp (revision 3b4aad7fa1a2fa4b52cfbc49dd32459dee0fdf56)
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::getViewIfOpen( const std::string& file_name ) const
51 {
52     auto result = find_if( openFiles_.begin(), openFiles_.end(),
53             [&](const std::pair<const ViewInterface*, OpenFile>& o)
54             { return ( o.second.fileName == file_name ); } );
55 
56     if ( result != openFiles_.end() )
57         return result->second.view;
58     else
59         return nullptr;
60 }
61 
62 ViewInterface* Session::open( const std::string& file_name,
63         std::function<ViewInterface*()> view_factory )
64 {
65     ViewInterface* view = nullptr;
66 
67     QFileInfo fileInfo( file_name.c_str() );
68     if ( fileInfo.isReadable() ) {
69         return openAlways( file_name, view_factory );
70     }
71     else {
72         throw FileUnreadableErr();
73     }
74 
75     return view;
76 }
77 
78 void Session::close( const ViewInterface* view )
79 {
80     openFiles_.erase( openFiles_.find( view ) );
81 }
82 
83 void Session::save(
84         std::vector<std::pair<const ViewInterface*, uint64_t>> view_list )
85 {
86     LOG(logDEBUG) << "Session::save";
87 
88     std::vector<SessionInfo::OpenFile> session_files;
89     for ( auto view: view_list ) {
90         const OpenFile* file = findOpenFileFromView( view.first );
91         assert( file );
92 
93         LOG(logDEBUG) << "Saving " << file->fileName << " in session.";
94         session_files.push_back( { file->fileName, view.second } );
95     }
96 
97     std::shared_ptr<SessionInfo> session =
98         Persistent<SessionInfo>( "session" );
99     session->setOpenFiles( session_files );
100     GetPersistentInfo().save( QString( "session" ) );
101 }
102 
103 std::vector<std::pair<std::string, ViewInterface*>> Session::restore(
104         std::function<ViewInterface*()> view_factory,
105         int *current_file_index )
106 {
107     GetPersistentInfo().retrieve( QString( "session" ) );
108     std::shared_ptr<SessionInfo> session =
109         Persistent<SessionInfo>( "session" );
110 
111     std::vector<SessionInfo::OpenFile> session_files = session->openFiles();
112     std::vector<std::pair<std::string, ViewInterface*>> result;
113 
114     for ( auto file: session_files )
115     {
116         LOG(logDEBUG) << "Create view for " << file.fileName;
117         ViewInterface* view = openAlways( file.fileName, view_factory );
118         result.push_back( { file.fileName, view } );
119     }
120 
121     *current_file_index = -1;
122 
123     return result;
124 }
125 
126 std::string Session::getFilename( const ViewInterface* view ) const
127 {
128     const OpenFile* file = findOpenFileFromView( view );
129 
130     assert( file );
131 
132     return file->fileName;
133 }
134 
135 void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
136         uint32_t* fileNbLine, QDateTime* lastModified ) const
137 {
138     const OpenFile* file = findOpenFileFromView( view );
139 
140     assert( file );
141 
142     *fileSize = file->logData->getFileSize();
143     *fileNbLine = file->logData->getNbLine();
144     *lastModified = file->logData->getLastModifiedDate();
145 }
146 
147 
148 /*
149  * Private methods
150  */
151 
152 ViewInterface* Session::openAlways( const std::string& file_name,
153         std::function<ViewInterface*()> view_factory )
154 {
155     // Create the data objects
156     auto log_data          = std::make_shared<LogData>();
157     auto log_filtered_data =
158         std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
159 
160     ViewInterface* view = view_factory();
161     view->setData( log_data, log_filtered_data );
162     view->setQuickFindPattern( quickFindPattern_ );
163     view->setSavedSearches( savedSearches_ );
164 
165     // Insert in the hash
166     openFiles_.insert( { view,
167             { file_name,
168             log_data,
169             log_filtered_data,
170             view } } );
171 
172     // Start loading the file
173     log_data->attachFile( QString( file_name.c_str() ) );
174 
175     return view;
176 }
177 
178 Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
179 {
180     assert( view );
181 
182     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 
190 const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
191 {
192     assert( view );
193 
194     const OpenFile* file = &( openFiles_.at( view ) );
195 
196     // OpenfileMap::at might throw out_of_range but since a view MUST always
197     // be attached to a file, we don't handle it!
198 
199     return file;
200 }
201