xref: /glogg/src/session.cpp (revision 0e97f16d31c1c1df556dcf53653f7efc157a778a)
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 
22b57881faSNicolas Bonnefon #include "log.h"
23b57881faSNicolas 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"
30b57881faSNicolas 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 
72b57881faSNicolas Bonnefon void Session::save(
73b57881faSNicolas Bonnefon         std::vector<std::pair<const ViewInterface*, uint64_t>> view_list )
74b57881faSNicolas Bonnefon {
75b57881faSNicolas Bonnefon     LOG(logDEBUG) << "Session::save";
76b57881faSNicolas Bonnefon 
77b57881faSNicolas Bonnefon     std::vector<SessionInfo::OpenFile> session_files;
78b57881faSNicolas Bonnefon     for ( auto view: view_list ) {
79b57881faSNicolas Bonnefon         const OpenFile* file = findOpenFileFromView( view.first );
80b57881faSNicolas Bonnefon         assert( file );
81b57881faSNicolas Bonnefon 
82b57881faSNicolas Bonnefon         LOG(logDEBUG) << "Saving " << file->fileName << " in session.";
83b57881faSNicolas Bonnefon         session_files.push_back( { file->fileName, view.second } );
84b57881faSNicolas Bonnefon     }
85b57881faSNicolas Bonnefon 
86b57881faSNicolas Bonnefon     SessionInfo& session = Persistent<SessionInfo>( "session" );
87b57881faSNicolas Bonnefon     session.setOpenFiles( session_files );
88b57881faSNicolas Bonnefon     GetPersistentInfo().save( QString( "session" ) );
89b57881faSNicolas Bonnefon }
90b57881faSNicolas 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 {
95b57881faSNicolas Bonnefon     GetPersistentInfo().retrieve( QString( "session" ) );
96b57881faSNicolas Bonnefon     SessionInfo& session = Persistent<SessionInfo>( "session" );
97b57881faSNicolas Bonnefon 
98b57881faSNicolas 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     {
103b57881faSNicolas Bonnefon         ViewInterface* view = openAlways( file.fileName, view_factory );
104b57881faSNicolas 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 
112*0e97f16dSNicolas Bonnefon std::string Session::getFilename( const ViewInterface* view ) const
113*0e97f16dSNicolas Bonnefon {
114*0e97f16dSNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
115*0e97f16dSNicolas Bonnefon 
116*0e97f16dSNicolas Bonnefon     assert( file );
117*0e97f16dSNicolas Bonnefon 
118*0e97f16dSNicolas Bonnefon     return file->fileName;
119*0e97f16dSNicolas Bonnefon }
120*0e97f16dSNicolas Bonnefon 
12178b9e3f6SNicolas Bonnefon void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
12278b9e3f6SNicolas Bonnefon         uint32_t* fileNbLine, QDateTime* lastModified ) const
123039481acSNicolas Bonnefon {
12478b9e3f6SNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
12578b9e3f6SNicolas Bonnefon 
12678b9e3f6SNicolas Bonnefon     assert( file );
12778b9e3f6SNicolas Bonnefon 
12878b9e3f6SNicolas Bonnefon     *fileSize = file->logData->getFileSize();
12978b9e3f6SNicolas Bonnefon     *fileNbLine = file->logData->getNbLine();
13078b9e3f6SNicolas Bonnefon     *lastModified = file->logData->getLastModifiedDate();
131039481acSNicolas Bonnefon }
13278b9e3f6SNicolas Bonnefon 
13378b9e3f6SNicolas Bonnefon 
13478b9e3f6SNicolas Bonnefon /*
13578b9e3f6SNicolas Bonnefon  * Private methods
136039481acSNicolas Bonnefon  */
13778b9e3f6SNicolas Bonnefon 
1388964a9adSNicolas Bonnefon ViewInterface* Session::openAlways( const std::string& file_name,
1398964a9adSNicolas Bonnefon         std::function<ViewInterface*()> view_factory )
1408964a9adSNicolas Bonnefon {
1418964a9adSNicolas Bonnefon     // Create the data objects
1428964a9adSNicolas Bonnefon     auto log_data          = std::make_shared<LogData>();
1438964a9adSNicolas Bonnefon     auto log_filtered_data =
1448964a9adSNicolas Bonnefon         std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
1458964a9adSNicolas Bonnefon 
1468964a9adSNicolas Bonnefon     ViewInterface* view = view_factory();
1478964a9adSNicolas Bonnefon     view->setData( log_data, log_filtered_data );
1488964a9adSNicolas Bonnefon     view->setQuickFindPattern( quickFindPattern_ );
1498964a9adSNicolas Bonnefon     view->setSavedSearches( savedSearches_ );
1508964a9adSNicolas Bonnefon 
1518964a9adSNicolas Bonnefon     // Insert in the hash
1528964a9adSNicolas Bonnefon     openFiles_.insert( { view,
1538964a9adSNicolas Bonnefon             { file_name,
1548964a9adSNicolas Bonnefon             log_data,
1558964a9adSNicolas Bonnefon             log_filtered_data,
1568964a9adSNicolas Bonnefon             view } } );
1578964a9adSNicolas Bonnefon 
1588964a9adSNicolas Bonnefon     // Start loading the file
1598964a9adSNicolas Bonnefon     log_data->attachFile( QString( file_name.c_str() ) );
1608964a9adSNicolas Bonnefon 
1618964a9adSNicolas Bonnefon     return view;
1628964a9adSNicolas Bonnefon }
1638964a9adSNicolas Bonnefon 
16478b9e3f6SNicolas Bonnefon Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
16578b9e3f6SNicolas Bonnefon {
16678b9e3f6SNicolas Bonnefon     assert( view );
16778b9e3f6SNicolas Bonnefon 
1681b5e406eSNicolas Bonnefon     OpenFile* file = &( openFiles_.at( view ) );
16978b9e3f6SNicolas Bonnefon 
17078b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
17178b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
17278b9e3f6SNicolas Bonnefon 
17378b9e3f6SNicolas Bonnefon     return file;
17478b9e3f6SNicolas Bonnefon }
17578b9e3f6SNicolas Bonnefon 
17678b9e3f6SNicolas Bonnefon const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
17778b9e3f6SNicolas Bonnefon {
17878b9e3f6SNicolas Bonnefon     assert( view );
17978b9e3f6SNicolas Bonnefon 
1801b5e406eSNicolas Bonnefon     const OpenFile* file = &( openFiles_.at( view ) );
18178b9e3f6SNicolas Bonnefon 
18278b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
18378b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
18478b9e3f6SNicolas Bonnefon 
18578b9e3f6SNicolas Bonnefon     return file;
18678b9e3f6SNicolas Bonnefon }
187