xref: /glogg/src/session.cpp (revision 11582726a85c08832d009bfe179074b8d1152d21)
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 
38*11582726SNicolas Bonnefon     // Get the global search history (it remains the property
39*11582726SNicolas Bonnefon     // of the Persistent)
40*11582726SNicolas Bonnefon     savedSearches_ = 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() );
56a3b56311SNicolas Bonnefon     if ( fileInfo.isReadable() ) {
578964a9adSNicolas Bonnefon         return openAlways( file_name, view_factory );
58039481acSNicolas Bonnefon     }
59039481acSNicolas Bonnefon     else {
60a3b56311SNicolas Bonnefon         throw FileUnreadableErr();
61039481acSNicolas Bonnefon     }
62f0708ca8SNicolas Bonnefon 
63f0708ca8SNicolas Bonnefon     return view;
64f0708ca8SNicolas Bonnefon }
6578b9e3f6SNicolas Bonnefon 
66cdd89779SNicolas Bonnefon void Session::close( const ViewInterface* view )
67cdd89779SNicolas Bonnefon {
68cdd89779SNicolas Bonnefon     openFiles_.erase( openFiles_.find( view ) );
69cdd89779SNicolas Bonnefon }
70cdd89779SNicolas Bonnefon 
71b57881faSNicolas Bonnefon void Session::save(
72b57881faSNicolas Bonnefon         std::vector<std::pair<const ViewInterface*, uint64_t>> view_list )
73b57881faSNicolas Bonnefon {
74b57881faSNicolas Bonnefon     LOG(logDEBUG) << "Session::save";
75b57881faSNicolas Bonnefon 
76b57881faSNicolas Bonnefon     std::vector<SessionInfo::OpenFile> session_files;
77b57881faSNicolas Bonnefon     for ( auto view: view_list ) {
78b57881faSNicolas Bonnefon         const OpenFile* file = findOpenFileFromView( view.first );
79b57881faSNicolas Bonnefon         assert( file );
80b57881faSNicolas Bonnefon 
81b57881faSNicolas Bonnefon         LOG(logDEBUG) << "Saving " << file->fileName << " in session.";
82b57881faSNicolas Bonnefon         session_files.push_back( { file->fileName, view.second } );
83b57881faSNicolas Bonnefon     }
84b57881faSNicolas Bonnefon 
85*11582726SNicolas Bonnefon     std::shared_ptr<SessionInfo> session =
86*11582726SNicolas Bonnefon         Persistent<SessionInfo>( "session" );
87*11582726SNicolas 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" ) );
96*11582726SNicolas Bonnefon     std::shared_ptr<SessionInfo> session =
97*11582726SNicolas Bonnefon         Persistent<SessionInfo>( "session" );
98b57881faSNicolas Bonnefon 
99*11582726SNicolas Bonnefon     std::vector<SessionInfo::OpenFile> session_files = session->openFiles();
1008964a9adSNicolas Bonnefon     std::vector<std::pair<std::string, ViewInterface*>> result;
1018964a9adSNicolas Bonnefon 
1028964a9adSNicolas Bonnefon     for ( auto file: session_files )
1038964a9adSNicolas Bonnefon     {
10460864ff5SNicolas Bonnefon         LOG(logDEBUG) << "Create view for " << file.fileName;
105b57881faSNicolas Bonnefon         ViewInterface* view = openAlways( file.fileName, view_factory );
106b57881faSNicolas Bonnefon         result.push_back( { file.fileName, view } );
1078964a9adSNicolas Bonnefon     }
1088964a9adSNicolas Bonnefon 
1098964a9adSNicolas Bonnefon     *current_file_index = -1;
1108964a9adSNicolas Bonnefon 
1118964a9adSNicolas Bonnefon     return result;
1128964a9adSNicolas Bonnefon }
1138964a9adSNicolas Bonnefon 
1140e97f16dSNicolas Bonnefon std::string Session::getFilename( const ViewInterface* view ) const
1150e97f16dSNicolas Bonnefon {
1160e97f16dSNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
1170e97f16dSNicolas Bonnefon 
1180e97f16dSNicolas Bonnefon     assert( file );
1190e97f16dSNicolas Bonnefon 
1200e97f16dSNicolas Bonnefon     return file->fileName;
1210e97f16dSNicolas Bonnefon }
1220e97f16dSNicolas Bonnefon 
12378b9e3f6SNicolas Bonnefon void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
12478b9e3f6SNicolas Bonnefon         uint32_t* fileNbLine, QDateTime* lastModified ) const
125039481acSNicolas Bonnefon {
12678b9e3f6SNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
12778b9e3f6SNicolas Bonnefon 
12878b9e3f6SNicolas Bonnefon     assert( file );
12978b9e3f6SNicolas Bonnefon 
13078b9e3f6SNicolas Bonnefon     *fileSize = file->logData->getFileSize();
13178b9e3f6SNicolas Bonnefon     *fileNbLine = file->logData->getNbLine();
13278b9e3f6SNicolas Bonnefon     *lastModified = file->logData->getLastModifiedDate();
133039481acSNicolas Bonnefon }
13478b9e3f6SNicolas Bonnefon 
13578b9e3f6SNicolas Bonnefon 
13678b9e3f6SNicolas Bonnefon /*
13778b9e3f6SNicolas Bonnefon  * Private methods
138039481acSNicolas Bonnefon  */
13978b9e3f6SNicolas Bonnefon 
1408964a9adSNicolas Bonnefon ViewInterface* Session::openAlways( const std::string& file_name,
1418964a9adSNicolas Bonnefon         std::function<ViewInterface*()> view_factory )
1428964a9adSNicolas Bonnefon {
1438964a9adSNicolas Bonnefon     // Create the data objects
1448964a9adSNicolas Bonnefon     auto log_data          = std::make_shared<LogData>();
1458964a9adSNicolas Bonnefon     auto log_filtered_data =
1468964a9adSNicolas Bonnefon         std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
1478964a9adSNicolas Bonnefon 
1488964a9adSNicolas Bonnefon     ViewInterface* view = view_factory();
1498964a9adSNicolas Bonnefon     view->setData( log_data, log_filtered_data );
1508964a9adSNicolas Bonnefon     view->setQuickFindPattern( quickFindPattern_ );
1518964a9adSNicolas Bonnefon     view->setSavedSearches( savedSearches_ );
1528964a9adSNicolas Bonnefon 
1538964a9adSNicolas Bonnefon     // Insert in the hash
1548964a9adSNicolas Bonnefon     openFiles_.insert( { view,
1558964a9adSNicolas Bonnefon             { file_name,
1568964a9adSNicolas Bonnefon             log_data,
1578964a9adSNicolas Bonnefon             log_filtered_data,
1588964a9adSNicolas Bonnefon             view } } );
1598964a9adSNicolas Bonnefon 
1608964a9adSNicolas Bonnefon     // Start loading the file
1618964a9adSNicolas Bonnefon     log_data->attachFile( QString( file_name.c_str() ) );
1628964a9adSNicolas Bonnefon 
1638964a9adSNicolas Bonnefon     return view;
1648964a9adSNicolas Bonnefon }
1658964a9adSNicolas Bonnefon 
16678b9e3f6SNicolas Bonnefon Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
16778b9e3f6SNicolas Bonnefon {
16878b9e3f6SNicolas Bonnefon     assert( view );
16978b9e3f6SNicolas Bonnefon 
1701b5e406eSNicolas Bonnefon     OpenFile* file = &( openFiles_.at( view ) );
17178b9e3f6SNicolas Bonnefon 
17278b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
17378b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
17478b9e3f6SNicolas Bonnefon 
17578b9e3f6SNicolas Bonnefon     return file;
17678b9e3f6SNicolas Bonnefon }
17778b9e3f6SNicolas Bonnefon 
17878b9e3f6SNicolas Bonnefon const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
17978b9e3f6SNicolas Bonnefon {
18078b9e3f6SNicolas Bonnefon     assert( view );
18178b9e3f6SNicolas Bonnefon 
1821b5e406eSNicolas Bonnefon     const OpenFile* file = &( openFiles_.at( view ) );
18378b9e3f6SNicolas Bonnefon 
18478b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
18578b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
18678b9e3f6SNicolas Bonnefon 
18778b9e3f6SNicolas Bonnefon     return file;
18878b9e3f6SNicolas Bonnefon }
189