xref: /glogg/src/session.cpp (revision b57881fa285fa8240ff86bc3a7c4f5766388e84d)
1d96f3f21SNicolas Bonnefon /*
28964a9adSNicolas Bonnefon  * Copyright (C) 2013, 2014 Nicolas Bonnefon and other contributors
3d96f3f21SNicolas Bonnefon  *
4d96f3f21SNicolas Bonnefon  * This file is part of glogg.
5d96f3f21SNicolas Bonnefon  *
6d96f3f21SNicolas Bonnefon  * glogg is free software: you can redistribute it and/or modify
7d96f3f21SNicolas Bonnefon  * it under the terms of the GNU General Public License as published by
8d96f3f21SNicolas Bonnefon  * the Free Software Foundation, either version 3 of the License, or
9d96f3f21SNicolas Bonnefon  * (at your option) any later version.
10d96f3f21SNicolas Bonnefon  *
11d96f3f21SNicolas Bonnefon  * glogg is distributed in the hope that it will be useful,
12d96f3f21SNicolas Bonnefon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13d96f3f21SNicolas Bonnefon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14d96f3f21SNicolas Bonnefon  * GNU General Public License for more details.
15d96f3f21SNicolas Bonnefon  *
16d96f3f21SNicolas Bonnefon  * You should have received a copy of the GNU General Public License
17d96f3f21SNicolas Bonnefon  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18d96f3f21SNicolas Bonnefon  */
19d96f3f21SNicolas Bonnefon 
20d96f3f21SNicolas Bonnefon #include "session.h"
21d96f3f21SNicolas Bonnefon 
22*b57881faSNicolas Bonnefon #include "log.h"
23*b57881faSNicolas Bonnefon 
2478b9e3f6SNicolas Bonnefon #include <cassert>
25039481acSNicolas Bonnefon #include <QFileInfo>
26039481acSNicolas Bonnefon 
27d96f3f21SNicolas Bonnefon #include "viewinterface.h"
281b5e406eSNicolas Bonnefon #include "persistentinfo.h"
291b5e406eSNicolas Bonnefon #include "savedsearches.h"
30*b57881faSNicolas Bonnefon #include "sessioninfo.h"
31039481acSNicolas Bonnefon #include "data/logdata.h"
32039481acSNicolas Bonnefon #include "data/logfiltereddata.h"
33d96f3f21SNicolas Bonnefon 
34d96f3f21SNicolas Bonnefon Session::Session()
35d96f3f21SNicolas Bonnefon {
361b5e406eSNicolas Bonnefon     GetPersistentInfo().retrieve( QString( "savedSearches" ) );
371b5e406eSNicolas Bonnefon 
381b5e406eSNicolas Bonnefon     // Get the global search history
391b5e406eSNicolas Bonnefon     savedSearches_ = std::shared_ptr<SavedSearches>(
401b5e406eSNicolas Bonnefon             &(Persistent<SavedSearches>( "savedSearches" )) );
41b423cd88SNicolas Bonnefon 
42b423cd88SNicolas Bonnefon     quickFindPattern_ = std::make_shared<QuickFindPattern>();
43d96f3f21SNicolas Bonnefon }
44d96f3f21SNicolas Bonnefon 
45d96f3f21SNicolas Bonnefon Session::~Session()
46d96f3f21SNicolas Bonnefon {
47d96f3f21SNicolas Bonnefon     // FIXME Clean up all the data objects...
48d96f3f21SNicolas Bonnefon }
49f0708ca8SNicolas Bonnefon 
50f0708ca8SNicolas Bonnefon ViewInterface* Session::open( const std::string& file_name,
51f0708ca8SNicolas Bonnefon         std::function<ViewInterface*()> view_factory )
52f0708ca8SNicolas Bonnefon {
53039481acSNicolas Bonnefon     ViewInterface* view = nullptr;
54039481acSNicolas Bonnefon 
55039481acSNicolas Bonnefon     QFileInfo fileInfo( file_name.c_str() );
56039481acSNicolas Bonnefon     if ( fileInfo.isReadable() )
57039481acSNicolas Bonnefon     {
588964a9adSNicolas Bonnefon         return openAlways( file_name, view_factory );
59039481acSNicolas Bonnefon     }
60039481acSNicolas Bonnefon     else {
618964a9adSNicolas Bonnefon         // FIXME throw
62039481acSNicolas Bonnefon     }
63f0708ca8SNicolas Bonnefon 
64f0708ca8SNicolas Bonnefon     return view;
65f0708ca8SNicolas Bonnefon }
6678b9e3f6SNicolas Bonnefon 
67cdd89779SNicolas Bonnefon void Session::close( const ViewInterface* view )
68cdd89779SNicolas Bonnefon {
69cdd89779SNicolas Bonnefon     openFiles_.erase( openFiles_.find( view ) );
70cdd89779SNicolas Bonnefon }
71cdd89779SNicolas Bonnefon 
72*b57881faSNicolas Bonnefon void Session::save(
73*b57881faSNicolas Bonnefon         std::vector<std::pair<const ViewInterface*, uint64_t>> view_list )
74*b57881faSNicolas Bonnefon {
75*b57881faSNicolas Bonnefon     LOG(logDEBUG) << "Session::save";
76*b57881faSNicolas Bonnefon 
77*b57881faSNicolas Bonnefon     std::vector<SessionInfo::OpenFile> session_files;
78*b57881faSNicolas Bonnefon     for ( auto view: view_list ) {
79*b57881faSNicolas Bonnefon         const OpenFile* file = findOpenFileFromView( view.first );
80*b57881faSNicolas Bonnefon         assert( file );
81*b57881faSNicolas Bonnefon 
82*b57881faSNicolas Bonnefon         LOG(logDEBUG) << "Saving " << file->fileName << " in session.";
83*b57881faSNicolas Bonnefon         session_files.push_back( { file->fileName, view.second } );
84*b57881faSNicolas Bonnefon     }
85*b57881faSNicolas Bonnefon 
86*b57881faSNicolas Bonnefon     SessionInfo& session = Persistent<SessionInfo>( "session" );
87*b57881faSNicolas Bonnefon     session.setOpenFiles( session_files );
88*b57881faSNicolas Bonnefon     GetPersistentInfo().save( QString( "session" ) );
89*b57881faSNicolas Bonnefon }
90*b57881faSNicolas Bonnefon 
918964a9adSNicolas Bonnefon std::vector<std::pair<std::string, ViewInterface*>> Session::restore(
928964a9adSNicolas Bonnefon         std::function<ViewInterface*()> view_factory,
938964a9adSNicolas Bonnefon         int *current_file_index )
948964a9adSNicolas Bonnefon {
95*b57881faSNicolas Bonnefon     GetPersistentInfo().retrieve( QString( "session" ) );
96*b57881faSNicolas Bonnefon     SessionInfo& session = Persistent<SessionInfo>( "session" );
97*b57881faSNicolas Bonnefon 
98*b57881faSNicolas Bonnefon     std::vector<SessionInfo::OpenFile> session_files = session.openFiles();
998964a9adSNicolas Bonnefon     std::vector<std::pair<std::string, ViewInterface*>> result;
1008964a9adSNicolas Bonnefon 
1018964a9adSNicolas Bonnefon     for ( auto file: session_files )
1028964a9adSNicolas Bonnefon     {
103*b57881faSNicolas Bonnefon         ViewInterface* view = openAlways( file.fileName, view_factory );
104*b57881faSNicolas Bonnefon         result.push_back( { file.fileName, view } );
1058964a9adSNicolas Bonnefon     }
1068964a9adSNicolas Bonnefon 
1078964a9adSNicolas Bonnefon     *current_file_index = -1;
1088964a9adSNicolas Bonnefon 
1098964a9adSNicolas Bonnefon     return result;
1108964a9adSNicolas Bonnefon }
1118964a9adSNicolas Bonnefon 
11278b9e3f6SNicolas Bonnefon void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
11378b9e3f6SNicolas Bonnefon         uint32_t* fileNbLine, QDateTime* lastModified ) const
114039481acSNicolas Bonnefon {
11578b9e3f6SNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
11678b9e3f6SNicolas Bonnefon 
11778b9e3f6SNicolas Bonnefon     assert( file );
11878b9e3f6SNicolas Bonnefon 
11978b9e3f6SNicolas Bonnefon     *fileSize = file->logData->getFileSize();
12078b9e3f6SNicolas Bonnefon     *fileNbLine = file->logData->getNbLine();
12178b9e3f6SNicolas Bonnefon     *lastModified = file->logData->getLastModifiedDate();
122039481acSNicolas Bonnefon }
12378b9e3f6SNicolas Bonnefon 
12478b9e3f6SNicolas Bonnefon 
12578b9e3f6SNicolas Bonnefon /*
12678b9e3f6SNicolas Bonnefon  * Private methods
127039481acSNicolas Bonnefon  */
12878b9e3f6SNicolas Bonnefon 
1298964a9adSNicolas Bonnefon ViewInterface* Session::openAlways( const std::string& file_name,
1308964a9adSNicolas Bonnefon         std::function<ViewInterface*()> view_factory )
1318964a9adSNicolas Bonnefon {
1328964a9adSNicolas Bonnefon     // Create the data objects
1338964a9adSNicolas Bonnefon     auto log_data          = std::make_shared<LogData>();
1348964a9adSNicolas Bonnefon     auto log_filtered_data =
1358964a9adSNicolas Bonnefon         std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
1368964a9adSNicolas Bonnefon 
1378964a9adSNicolas Bonnefon     ViewInterface* view = view_factory();
1388964a9adSNicolas Bonnefon     view->setData( log_data, log_filtered_data );
1398964a9adSNicolas Bonnefon     view->setQuickFindPattern( quickFindPattern_ );
1408964a9adSNicolas Bonnefon     view->setSavedSearches( savedSearches_ );
1418964a9adSNicolas Bonnefon 
1428964a9adSNicolas Bonnefon     // Insert in the hash
1438964a9adSNicolas Bonnefon     openFiles_.insert( { view,
1448964a9adSNicolas Bonnefon             { file_name,
1458964a9adSNicolas Bonnefon             log_data,
1468964a9adSNicolas Bonnefon             log_filtered_data,
1478964a9adSNicolas Bonnefon             view } } );
1488964a9adSNicolas Bonnefon 
1498964a9adSNicolas Bonnefon     // Start loading the file
1508964a9adSNicolas Bonnefon     log_data->attachFile( QString( file_name.c_str() ) );
1518964a9adSNicolas Bonnefon 
1528964a9adSNicolas Bonnefon     return view;
1538964a9adSNicolas Bonnefon }
1548964a9adSNicolas Bonnefon 
15578b9e3f6SNicolas Bonnefon Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
15678b9e3f6SNicolas Bonnefon {
15778b9e3f6SNicolas Bonnefon     assert( view );
15878b9e3f6SNicolas Bonnefon 
1591b5e406eSNicolas Bonnefon     OpenFile* file = &( openFiles_.at( view ) );
16078b9e3f6SNicolas Bonnefon 
16178b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
16278b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
16378b9e3f6SNicolas Bonnefon 
16478b9e3f6SNicolas Bonnefon     return file;
16578b9e3f6SNicolas Bonnefon }
16678b9e3f6SNicolas Bonnefon 
16778b9e3f6SNicolas Bonnefon const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
16878b9e3f6SNicolas Bonnefon {
16978b9e3f6SNicolas Bonnefon     assert( view );
17078b9e3f6SNicolas Bonnefon 
1711b5e406eSNicolas Bonnefon     const OpenFile* file = &( openFiles_.at( view ) );
17278b9e3f6SNicolas Bonnefon 
17378b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
17478b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
17578b9e3f6SNicolas Bonnefon 
17678b9e3f6SNicolas Bonnefon     return file;
17778b9e3f6SNicolas Bonnefon }
178