xref: /glogg/src/session.cpp (revision a3b56311c687757f717bf682aba24d01106ac57e)
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
39     savedSearches_ = std::shared_ptr<SavedSearches>(
40             &(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     SessionInfo& session = Persistent<SessionInfo>( "session" );
86     session.setOpenFiles( session_files );
87     GetPersistentInfo().save( QString( "session" ) );
88 }
89 
90 std::vector<std::pair<std::string, ViewInterface*>> Session::restore(
91         std::function<ViewInterface*()> view_factory,
92         int *current_file_index )
93 {
94     GetPersistentInfo().retrieve( QString( "session" ) );
95     SessionInfo& session = Persistent<SessionInfo>( "session" );
96 
97     std::vector<SessionInfo::OpenFile> session_files = session.openFiles();
98     std::vector<std::pair<std::string, ViewInterface*>> result;
99 
100     for ( auto file: session_files )
101     {
102         LOG(logDEBUG) << "Create view for " << file.fileName;
103         ViewInterface* view = openAlways( file.fileName, view_factory );
104         result.push_back( { file.fileName, view } );
105     }
106 
107     *current_file_index = -1;
108 
109     return result;
110 }
111 
112 std::string Session::getFilename( const ViewInterface* view ) const
113 {
114     const OpenFile* file = findOpenFileFromView( view );
115 
116     assert( file );
117 
118     return file->fileName;
119 }
120 
121 void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
122         uint32_t* fileNbLine, QDateTime* lastModified ) const
123 {
124     const OpenFile* file = findOpenFileFromView( view );
125 
126     assert( file );
127 
128     *fileSize = file->logData->getFileSize();
129     *fileNbLine = file->logData->getNbLine();
130     *lastModified = file->logData->getLastModifiedDate();
131 }
132 
133 
134 /*
135  * Private methods
136  */
137 
138 ViewInterface* Session::openAlways( const std::string& file_name,
139         std::function<ViewInterface*()> view_factory )
140 {
141     // Create the data objects
142     auto log_data          = std::make_shared<LogData>();
143     auto log_filtered_data =
144         std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
145 
146     ViewInterface* view = view_factory();
147     view->setData( log_data, log_filtered_data );
148     view->setQuickFindPattern( quickFindPattern_ );
149     view->setSavedSearches( savedSearches_ );
150 
151     // Insert in the hash
152     openFiles_.insert( { view,
153             { file_name,
154             log_data,
155             log_filtered_data,
156             view } } );
157 
158     // Start loading the file
159     log_data->attachFile( QString( file_name.c_str() ) );
160 
161     return view;
162 }
163 
164 Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
165 {
166     assert( view );
167 
168     OpenFile* file = &( openFiles_.at( view ) );
169 
170     // OpenfileMap::at might throw out_of_range but since a view MUST always
171     // be attached to a file, we don't handle it!
172 
173     return file;
174 }
175 
176 const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
177 {
178     assert( view );
179 
180     const OpenFile* file = &( openFiles_.at( view ) );
181 
182     // OpenfileMap::at might throw out_of_range but since a view MUST always
183     // be attached to a file, we don't handle it!
184 
185     return file;
186 }
187