xref: /glogg/src/session.cpp (revision 6a12446ea5d0b31e9ae87ae6561fe4e6f45da437)
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>
26*6a12446eSNicolas Bonnefon #include <algorithm>
27039481acSNicolas Bonnefon 
28d96f3f21SNicolas Bonnefon #include "viewinterface.h"
291b5e406eSNicolas Bonnefon #include "persistentinfo.h"
301b5e406eSNicolas Bonnefon #include "savedsearches.h"
31b57881faSNicolas Bonnefon #include "sessioninfo.h"
32039481acSNicolas Bonnefon #include "data/logdata.h"
33039481acSNicolas Bonnefon #include "data/logfiltereddata.h"
34d96f3f21SNicolas Bonnefon 
Session()35d96f3f21SNicolas Bonnefon Session::Session()
36d96f3f21SNicolas Bonnefon {
371b5e406eSNicolas Bonnefon     GetPersistentInfo().retrieve( QString( "savedSearches" ) );
381b5e406eSNicolas Bonnefon 
3911582726SNicolas Bonnefon     // Get the global search history (it remains the property
4011582726SNicolas Bonnefon     // of the Persistent)
4111582726SNicolas Bonnefon     savedSearches_ = Persistent<SavedSearches>( "savedSearches" );
42b423cd88SNicolas Bonnefon 
43b423cd88SNicolas Bonnefon     quickFindPattern_ = std::make_shared<QuickFindPattern>();
44d96f3f21SNicolas Bonnefon }
45d96f3f21SNicolas Bonnefon 
~Session()46d96f3f21SNicolas Bonnefon Session::~Session()
47d96f3f21SNicolas Bonnefon {
48d96f3f21SNicolas Bonnefon     // FIXME Clean up all the data objects...
49d96f3f21SNicolas Bonnefon }
50f0708ca8SNicolas Bonnefon 
getViewIfOpen(const std::string & file_name) const513b4aad7fSNicolas Bonnefon ViewInterface* Session::getViewIfOpen( const std::string& file_name ) const
523b4aad7fSNicolas Bonnefon {
53*6a12446eSNicolas Bonnefon     auto result = std::find_if( openFiles_.begin(), openFiles_.end(),
543b4aad7fSNicolas Bonnefon             [&](const std::pair<const ViewInterface*, OpenFile>& o)
553b4aad7fSNicolas Bonnefon             { return ( o.second.fileName == file_name ); } );
563b4aad7fSNicolas Bonnefon 
573b4aad7fSNicolas Bonnefon     if ( result != openFiles_.end() )
583b4aad7fSNicolas Bonnefon         return result->second.view;
593b4aad7fSNicolas Bonnefon     else
603b4aad7fSNicolas Bonnefon         return nullptr;
613b4aad7fSNicolas Bonnefon }
623b4aad7fSNicolas Bonnefon 
open(const std::string & file_name,std::function<ViewInterface * ()> view_factory)63f0708ca8SNicolas Bonnefon ViewInterface* Session::open( const std::string& file_name,
64f0708ca8SNicolas Bonnefon         std::function<ViewInterface*()> view_factory )
65f0708ca8SNicolas Bonnefon {
66039481acSNicolas Bonnefon     ViewInterface* view = nullptr;
67039481acSNicolas Bonnefon 
68039481acSNicolas Bonnefon     QFileInfo fileInfo( file_name.c_str() );
69a3b56311SNicolas Bonnefon     if ( fileInfo.isReadable() ) {
70a44d09bcSNicolas Bonnefon         return openAlways( file_name, view_factory, nullptr );
71039481acSNicolas Bonnefon     }
72039481acSNicolas Bonnefon     else {
73a3b56311SNicolas Bonnefon         throw FileUnreadableErr();
74039481acSNicolas Bonnefon     }
75f0708ca8SNicolas Bonnefon 
76f0708ca8SNicolas Bonnefon     return view;
77f0708ca8SNicolas Bonnefon }
7878b9e3f6SNicolas Bonnefon 
close(const ViewInterface * view)79cdd89779SNicolas Bonnefon void Session::close( const ViewInterface* view )
80cdd89779SNicolas Bonnefon {
81cdd89779SNicolas Bonnefon     openFiles_.erase( openFiles_.find( view ) );
82cdd89779SNicolas Bonnefon }
83cdd89779SNicolas Bonnefon 
save(std::vector<std::tuple<const ViewInterface *,uint64_t,std::shared_ptr<const ViewContextInterface>>> view_list,const QByteArray & geometry)84a44d09bcSNicolas Bonnefon void Session::save( std::vector<
85a44d09bcSNicolas Bonnefon         std::tuple<const ViewInterface*,
86a44d09bcSNicolas Bonnefon             uint64_t,
87a44d09bcSNicolas Bonnefon             std::shared_ptr<const ViewContextInterface>>
88b76966a6SNicolas Bonnefon         > view_list,
89b76966a6SNicolas Bonnefon         const QByteArray& geometry )
90b57881faSNicolas Bonnefon {
91b57881faSNicolas Bonnefon     LOG(logDEBUG) << "Session::save";
92b57881faSNicolas Bonnefon 
93b57881faSNicolas Bonnefon     std::vector<SessionInfo::OpenFile> session_files;
94b57881faSNicolas Bonnefon     for ( auto view: view_list ) {
95a44d09bcSNicolas Bonnefon         const ViewInterface* view_object;
96a44d09bcSNicolas Bonnefon         uint64_t top_line;
97a44d09bcSNicolas Bonnefon         std::shared_ptr<const ViewContextInterface> view_context;
98a44d09bcSNicolas Bonnefon 
99a44d09bcSNicolas Bonnefon         std::tie( view_object, top_line, view_context ) = view;
100a44d09bcSNicolas Bonnefon 
101a44d09bcSNicolas Bonnefon         const OpenFile* file = findOpenFileFromView( view_object );
102b57881faSNicolas Bonnefon         assert( file );
103b57881faSNicolas Bonnefon 
104b57881faSNicolas Bonnefon         LOG(logDEBUG) << "Saving " << file->fileName << " in session.";
105a44d09bcSNicolas Bonnefon         session_files.push_back( { file->fileName, top_line, view_context->toString() } );
106b57881faSNicolas Bonnefon     }
107b57881faSNicolas Bonnefon 
10811582726SNicolas Bonnefon     std::shared_ptr<SessionInfo> session =
10911582726SNicolas Bonnefon         Persistent<SessionInfo>( "session" );
11011582726SNicolas Bonnefon     session->setOpenFiles( session_files );
111b76966a6SNicolas Bonnefon     session->setGeometry( geometry );
112b57881faSNicolas Bonnefon     GetPersistentInfo().save( QString( "session" ) );
113b57881faSNicolas Bonnefon }
114b57881faSNicolas Bonnefon 
restore(std::function<ViewInterface * ()> view_factory,int * current_file_index)1158964a9adSNicolas Bonnefon std::vector<std::pair<std::string, ViewInterface*>> Session::restore(
1168964a9adSNicolas Bonnefon         std::function<ViewInterface*()> view_factory,
1178964a9adSNicolas Bonnefon         int *current_file_index )
1188964a9adSNicolas Bonnefon {
119b57881faSNicolas Bonnefon     GetPersistentInfo().retrieve( QString( "session" ) );
12011582726SNicolas Bonnefon     std::shared_ptr<SessionInfo> session =
12111582726SNicolas Bonnefon         Persistent<SessionInfo>( "session" );
122b57881faSNicolas Bonnefon 
12311582726SNicolas Bonnefon     std::vector<SessionInfo::OpenFile> session_files = session->openFiles();
124b76966a6SNicolas Bonnefon     LOG(logDEBUG) << "Session returned " << session_files.size();
1258964a9adSNicolas Bonnefon     std::vector<std::pair<std::string, ViewInterface*>> result;
1268964a9adSNicolas Bonnefon 
1278964a9adSNicolas Bonnefon     for ( auto file: session_files )
1288964a9adSNicolas Bonnefon     {
12960864ff5SNicolas Bonnefon         LOG(logDEBUG) << "Create view for " << file.fileName;
130a44d09bcSNicolas Bonnefon         ViewInterface* view = openAlways( file.fileName, view_factory, file.viewContext.c_str() );
131b57881faSNicolas Bonnefon         result.push_back( { file.fileName, view } );
1328964a9adSNicolas Bonnefon     }
1338964a9adSNicolas Bonnefon 
1348964a9adSNicolas Bonnefon     *current_file_index = -1;
1358964a9adSNicolas Bonnefon 
1368964a9adSNicolas Bonnefon     return result;
1378964a9adSNicolas Bonnefon }
1388964a9adSNicolas Bonnefon 
storedGeometry(QByteArray * geometry) const139b76966a6SNicolas Bonnefon void Session::storedGeometry( QByteArray* geometry ) const
140b76966a6SNicolas Bonnefon {
141b76966a6SNicolas Bonnefon     GetPersistentInfo().retrieve( QString( "session" ) );
142b76966a6SNicolas Bonnefon     std::shared_ptr<SessionInfo> session =
143b76966a6SNicolas Bonnefon         Persistent<SessionInfo>( "session" );
144b76966a6SNicolas Bonnefon 
145b76966a6SNicolas Bonnefon     *geometry = session->geometry();
146b76966a6SNicolas Bonnefon }
147b76966a6SNicolas Bonnefon 
getFilename(const ViewInterface * view) const1480e97f16dSNicolas Bonnefon std::string Session::getFilename( const ViewInterface* view ) const
1490e97f16dSNicolas Bonnefon {
1500e97f16dSNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
1510e97f16dSNicolas Bonnefon 
1520e97f16dSNicolas Bonnefon     assert( file );
1530e97f16dSNicolas Bonnefon 
1540e97f16dSNicolas Bonnefon     return file->fileName;
1550e97f16dSNicolas Bonnefon }
1560e97f16dSNicolas Bonnefon 
getFileInfo(const ViewInterface * view,uint64_t * fileSize,uint32_t * fileNbLine,QDateTime * lastModified) const15778b9e3f6SNicolas Bonnefon void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
15878b9e3f6SNicolas Bonnefon         uint32_t* fileNbLine, QDateTime* lastModified ) const
159039481acSNicolas Bonnefon {
16078b9e3f6SNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
16178b9e3f6SNicolas Bonnefon 
16278b9e3f6SNicolas Bonnefon     assert( file );
16378b9e3f6SNicolas Bonnefon 
16478b9e3f6SNicolas Bonnefon     *fileSize = file->logData->getFileSize();
16578b9e3f6SNicolas Bonnefon     *fileNbLine = file->logData->getNbLine();
16678b9e3f6SNicolas Bonnefon     *lastModified = file->logData->getLastModifiedDate();
167039481acSNicolas Bonnefon }
16878b9e3f6SNicolas Bonnefon 
16978b9e3f6SNicolas Bonnefon 
17078b9e3f6SNicolas Bonnefon /*
17178b9e3f6SNicolas Bonnefon  * Private methods
172039481acSNicolas Bonnefon  */
17378b9e3f6SNicolas Bonnefon 
openAlways(const std::string & file_name,std::function<ViewInterface * ()> view_factory,const char * view_context)1748964a9adSNicolas Bonnefon ViewInterface* Session::openAlways( const std::string& file_name,
175a44d09bcSNicolas Bonnefon         std::function<ViewInterface*()> view_factory,
176a44d09bcSNicolas Bonnefon         const char* view_context )
1778964a9adSNicolas Bonnefon {
1788964a9adSNicolas Bonnefon     // Create the data objects
1798964a9adSNicolas Bonnefon     auto log_data          = std::make_shared<LogData>();
1808964a9adSNicolas Bonnefon     auto log_filtered_data =
1818964a9adSNicolas Bonnefon         std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
1828964a9adSNicolas Bonnefon 
1838964a9adSNicolas Bonnefon     ViewInterface* view = view_factory();
1848964a9adSNicolas Bonnefon     view->setData( log_data, log_filtered_data );
1858964a9adSNicolas Bonnefon     view->setQuickFindPattern( quickFindPattern_ );
1868964a9adSNicolas Bonnefon     view->setSavedSearches( savedSearches_ );
1878964a9adSNicolas Bonnefon 
188a44d09bcSNicolas Bonnefon     if ( view_context )
189a44d09bcSNicolas Bonnefon         view->setViewContext( view_context );
190a44d09bcSNicolas Bonnefon 
1918964a9adSNicolas Bonnefon     // Insert in the hash
1928964a9adSNicolas Bonnefon     openFiles_.insert( { view,
1938964a9adSNicolas Bonnefon             { file_name,
1948964a9adSNicolas Bonnefon             log_data,
1958964a9adSNicolas Bonnefon             log_filtered_data,
1968964a9adSNicolas Bonnefon             view } } );
1978964a9adSNicolas Bonnefon 
1988964a9adSNicolas Bonnefon     // Start loading the file
1998964a9adSNicolas Bonnefon     log_data->attachFile( QString( file_name.c_str() ) );
2008964a9adSNicolas Bonnefon 
2018964a9adSNicolas Bonnefon     return view;
2028964a9adSNicolas Bonnefon }
2038964a9adSNicolas Bonnefon 
findOpenFileFromView(const ViewInterface * view)20478b9e3f6SNicolas Bonnefon Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
20578b9e3f6SNicolas Bonnefon {
20678b9e3f6SNicolas Bonnefon     assert( view );
20778b9e3f6SNicolas Bonnefon 
2081b5e406eSNicolas Bonnefon     OpenFile* file = &( openFiles_.at( view ) );
20978b9e3f6SNicolas Bonnefon 
21078b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
21178b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
21278b9e3f6SNicolas Bonnefon 
21378b9e3f6SNicolas Bonnefon     return file;
21478b9e3f6SNicolas Bonnefon }
21578b9e3f6SNicolas Bonnefon 
findOpenFileFromView(const ViewInterface * view) const21678b9e3f6SNicolas Bonnefon const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
21778b9e3f6SNicolas Bonnefon {
21878b9e3f6SNicolas Bonnefon     assert( view );
21978b9e3f6SNicolas Bonnefon 
2201b5e406eSNicolas Bonnefon     const OpenFile* file = &( openFiles_.at( view ) );
22178b9e3f6SNicolas Bonnefon 
22278b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
22378b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
22478b9e3f6SNicolas Bonnefon 
22578b9e3f6SNicolas Bonnefon     return file;
22678b9e3f6SNicolas Bonnefon }
227