xref: /glogg/src/session.cpp (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 #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     {
58         return openAlways( file_name, view_factory );
59     }
60     else {
61         // FIXME throw
62     }
63 
64     return view;
65 }
66 
67 void Session::close( const ViewInterface* view )
68 {
69     openFiles_.erase( openFiles_.find( view ) );
70 }
71 
72 void Session::save(
73         std::vector<std::pair<const ViewInterface*, uint64_t>> view_list )
74 {
75     LOG(logDEBUG) << "Session::save";
76 
77     std::vector<SessionInfo::OpenFile> session_files;
78     for ( auto view: view_list ) {
79         const OpenFile* file = findOpenFileFromView( view.first );
80         assert( file );
81 
82         LOG(logDEBUG) << "Saving " << file->fileName << " in session.";
83         session_files.push_back( { file->fileName, view.second } );
84     }
85 
86     SessionInfo& session = 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     SessionInfo& session = Persistent<SessionInfo>( "session" );
97 
98     std::vector<SessionInfo::OpenFile> session_files = session.openFiles();
99     std::vector<std::pair<std::string, ViewInterface*>> result;
100 
101     for ( auto file: session_files )
102     {
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 void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
113         uint32_t* fileNbLine, QDateTime* lastModified ) const
114 {
115     const OpenFile* file = findOpenFileFromView( view );
116 
117     assert( file );
118 
119     *fileSize = file->logData->getFileSize();
120     *fileNbLine = file->logData->getNbLine();
121     *lastModified = file->logData->getLastModifiedDate();
122 }
123 
124 
125 /*
126  * Private methods
127  */
128 
129 ViewInterface* Session::openAlways( const std::string& file_name,
130         std::function<ViewInterface*()> view_factory )
131 {
132     // Create the data objects
133     auto log_data          = std::make_shared<LogData>();
134     auto log_filtered_data =
135         std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
136 
137     ViewInterface* view = view_factory();
138     view->setData( log_data, log_filtered_data );
139     view->setQuickFindPattern( quickFindPattern_ );
140     view->setSavedSearches( savedSearches_ );
141 
142     // Insert in the hash
143     openFiles_.insert( { view,
144             { file_name,
145             log_data,
146             log_filtered_data,
147             view } } );
148 
149     // Start loading the file
150     log_data->attachFile( QString( file_name.c_str() ) );
151 
152     return view;
153 }
154 
155 Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
156 {
157     assert( view );
158 
159     OpenFile* file = &( openFiles_.at( view ) );
160 
161     // OpenfileMap::at might throw out_of_range but since a view MUST always
162     // be attached to a file, we don't handle it!
163 
164     return file;
165 }
166 
167 const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
168 {
169     assert( view );
170 
171     const OpenFile* file = &( openFiles_.at( view ) );
172 
173     // OpenfileMap::at might throw out_of_range but since a view MUST always
174     // be attached to a file, we don't handle it!
175 
176     return file;
177 }
178