xref: /glogg/src/session.cpp (revision b76966a660b57dd0cbec7739fa98087bc18b357f)
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 
3811582726SNicolas Bonnefon     // Get the global search history (it remains the property
3911582726SNicolas Bonnefon     // of the Persistent)
4011582726SNicolas 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 
503b4aad7fSNicolas Bonnefon ViewInterface* Session::getViewIfOpen( const std::string& file_name ) const
513b4aad7fSNicolas Bonnefon {
523b4aad7fSNicolas Bonnefon     auto result = find_if( openFiles_.begin(), openFiles_.end(),
533b4aad7fSNicolas Bonnefon             [&](const std::pair<const ViewInterface*, OpenFile>& o)
543b4aad7fSNicolas Bonnefon             { return ( o.second.fileName == file_name ); } );
553b4aad7fSNicolas Bonnefon 
563b4aad7fSNicolas Bonnefon     if ( result != openFiles_.end() )
573b4aad7fSNicolas Bonnefon         return result->second.view;
583b4aad7fSNicolas Bonnefon     else
593b4aad7fSNicolas Bonnefon         return nullptr;
603b4aad7fSNicolas Bonnefon }
613b4aad7fSNicolas Bonnefon 
62f0708ca8SNicolas Bonnefon ViewInterface* Session::open( const std::string& file_name,
63f0708ca8SNicolas Bonnefon         std::function<ViewInterface*()> view_factory )
64f0708ca8SNicolas Bonnefon {
65039481acSNicolas Bonnefon     ViewInterface* view = nullptr;
66039481acSNicolas Bonnefon 
67039481acSNicolas Bonnefon     QFileInfo fileInfo( file_name.c_str() );
68a3b56311SNicolas Bonnefon     if ( fileInfo.isReadable() ) {
69a44d09bcSNicolas Bonnefon         return openAlways( file_name, view_factory, nullptr );
70039481acSNicolas Bonnefon     }
71039481acSNicolas Bonnefon     else {
72a3b56311SNicolas Bonnefon         throw FileUnreadableErr();
73039481acSNicolas Bonnefon     }
74f0708ca8SNicolas Bonnefon 
75f0708ca8SNicolas Bonnefon     return view;
76f0708ca8SNicolas Bonnefon }
7778b9e3f6SNicolas Bonnefon 
78cdd89779SNicolas Bonnefon void Session::close( const ViewInterface* view )
79cdd89779SNicolas Bonnefon {
80cdd89779SNicolas Bonnefon     openFiles_.erase( openFiles_.find( view ) );
81cdd89779SNicolas Bonnefon }
82cdd89779SNicolas Bonnefon 
83a44d09bcSNicolas Bonnefon void Session::save( std::vector<
84a44d09bcSNicolas Bonnefon         std::tuple<const ViewInterface*,
85a44d09bcSNicolas Bonnefon             uint64_t,
86a44d09bcSNicolas Bonnefon             std::shared_ptr<const ViewContextInterface>>
87*b76966a6SNicolas Bonnefon         > view_list,
88*b76966a6SNicolas Bonnefon         const QByteArray& geometry )
89b57881faSNicolas Bonnefon {
90b57881faSNicolas Bonnefon     LOG(logDEBUG) << "Session::save";
91b57881faSNicolas Bonnefon 
92b57881faSNicolas Bonnefon     std::vector<SessionInfo::OpenFile> session_files;
93b57881faSNicolas Bonnefon     for ( auto view: view_list ) {
94a44d09bcSNicolas Bonnefon         const ViewInterface* view_object;
95a44d09bcSNicolas Bonnefon         uint64_t top_line;
96a44d09bcSNicolas Bonnefon         std::shared_ptr<const ViewContextInterface> view_context;
97a44d09bcSNicolas Bonnefon 
98a44d09bcSNicolas Bonnefon         std::tie( view_object, top_line, view_context ) = view;
99a44d09bcSNicolas Bonnefon 
100a44d09bcSNicolas Bonnefon         const OpenFile* file = findOpenFileFromView( view_object );
101b57881faSNicolas Bonnefon         assert( file );
102b57881faSNicolas Bonnefon 
103b57881faSNicolas Bonnefon         LOG(logDEBUG) << "Saving " << file->fileName << " in session.";
104a44d09bcSNicolas Bonnefon         session_files.push_back( { file->fileName, top_line, view_context->toString() } );
105b57881faSNicolas Bonnefon     }
106b57881faSNicolas Bonnefon 
10711582726SNicolas Bonnefon     std::shared_ptr<SessionInfo> session =
10811582726SNicolas Bonnefon         Persistent<SessionInfo>( "session" );
10911582726SNicolas Bonnefon     session->setOpenFiles( session_files );
110*b76966a6SNicolas Bonnefon     session->setGeometry( geometry );
111b57881faSNicolas Bonnefon     GetPersistentInfo().save( QString( "session" ) );
112b57881faSNicolas Bonnefon }
113b57881faSNicolas Bonnefon 
1148964a9adSNicolas Bonnefon std::vector<std::pair<std::string, ViewInterface*>> Session::restore(
1158964a9adSNicolas Bonnefon         std::function<ViewInterface*()> view_factory,
1168964a9adSNicolas Bonnefon         int *current_file_index )
1178964a9adSNicolas Bonnefon {
118b57881faSNicolas Bonnefon     GetPersistentInfo().retrieve( QString( "session" ) );
11911582726SNicolas Bonnefon     std::shared_ptr<SessionInfo> session =
12011582726SNicolas Bonnefon         Persistent<SessionInfo>( "session" );
121b57881faSNicolas Bonnefon 
12211582726SNicolas Bonnefon     std::vector<SessionInfo::OpenFile> session_files = session->openFiles();
123*b76966a6SNicolas Bonnefon     LOG(logDEBUG) << "Session returned " << session_files.size();
1248964a9adSNicolas Bonnefon     std::vector<std::pair<std::string, ViewInterface*>> result;
1258964a9adSNicolas Bonnefon 
1268964a9adSNicolas Bonnefon     for ( auto file: session_files )
1278964a9adSNicolas Bonnefon     {
12860864ff5SNicolas Bonnefon         LOG(logDEBUG) << "Create view for " << file.fileName;
129a44d09bcSNicolas Bonnefon         ViewInterface* view = openAlways( file.fileName, view_factory, file.viewContext.c_str() );
130b57881faSNicolas Bonnefon         result.push_back( { file.fileName, view } );
1318964a9adSNicolas Bonnefon     }
1328964a9adSNicolas Bonnefon 
1338964a9adSNicolas Bonnefon     *current_file_index = -1;
1348964a9adSNicolas Bonnefon 
1358964a9adSNicolas Bonnefon     return result;
1368964a9adSNicolas Bonnefon }
1378964a9adSNicolas Bonnefon 
138*b76966a6SNicolas Bonnefon void Session::storedGeometry( QByteArray* geometry ) const
139*b76966a6SNicolas Bonnefon {
140*b76966a6SNicolas Bonnefon     GetPersistentInfo().retrieve( QString( "session" ) );
141*b76966a6SNicolas Bonnefon     std::shared_ptr<SessionInfo> session =
142*b76966a6SNicolas Bonnefon         Persistent<SessionInfo>( "session" );
143*b76966a6SNicolas Bonnefon 
144*b76966a6SNicolas Bonnefon     *geometry = session->geometry();
145*b76966a6SNicolas Bonnefon }
146*b76966a6SNicolas Bonnefon 
1470e97f16dSNicolas Bonnefon std::string Session::getFilename( const ViewInterface* view ) const
1480e97f16dSNicolas Bonnefon {
1490e97f16dSNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
1500e97f16dSNicolas Bonnefon 
1510e97f16dSNicolas Bonnefon     assert( file );
1520e97f16dSNicolas Bonnefon 
1530e97f16dSNicolas Bonnefon     return file->fileName;
1540e97f16dSNicolas Bonnefon }
1550e97f16dSNicolas Bonnefon 
15678b9e3f6SNicolas Bonnefon void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
15778b9e3f6SNicolas Bonnefon         uint32_t* fileNbLine, QDateTime* lastModified ) const
158039481acSNicolas Bonnefon {
15978b9e3f6SNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
16078b9e3f6SNicolas Bonnefon 
16178b9e3f6SNicolas Bonnefon     assert( file );
16278b9e3f6SNicolas Bonnefon 
16378b9e3f6SNicolas Bonnefon     *fileSize = file->logData->getFileSize();
16478b9e3f6SNicolas Bonnefon     *fileNbLine = file->logData->getNbLine();
16578b9e3f6SNicolas Bonnefon     *lastModified = file->logData->getLastModifiedDate();
166039481acSNicolas Bonnefon }
16778b9e3f6SNicolas Bonnefon 
16878b9e3f6SNicolas Bonnefon 
16978b9e3f6SNicolas Bonnefon /*
17078b9e3f6SNicolas Bonnefon  * Private methods
171039481acSNicolas Bonnefon  */
17278b9e3f6SNicolas Bonnefon 
1738964a9adSNicolas Bonnefon ViewInterface* Session::openAlways( const std::string& file_name,
174a44d09bcSNicolas Bonnefon         std::function<ViewInterface*()> view_factory,
175a44d09bcSNicolas Bonnefon         const char* view_context )
1768964a9adSNicolas Bonnefon {
1778964a9adSNicolas Bonnefon     // Create the data objects
1788964a9adSNicolas Bonnefon     auto log_data          = std::make_shared<LogData>();
1798964a9adSNicolas Bonnefon     auto log_filtered_data =
1808964a9adSNicolas Bonnefon         std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
1818964a9adSNicolas Bonnefon 
1828964a9adSNicolas Bonnefon     ViewInterface* view = view_factory();
1838964a9adSNicolas Bonnefon     view->setData( log_data, log_filtered_data );
1848964a9adSNicolas Bonnefon     view->setQuickFindPattern( quickFindPattern_ );
1858964a9adSNicolas Bonnefon     view->setSavedSearches( savedSearches_ );
1868964a9adSNicolas Bonnefon 
187a44d09bcSNicolas Bonnefon     if ( view_context )
188a44d09bcSNicolas Bonnefon         view->setViewContext( view_context );
189a44d09bcSNicolas Bonnefon 
1908964a9adSNicolas Bonnefon     // Insert in the hash
1918964a9adSNicolas Bonnefon     openFiles_.insert( { view,
1928964a9adSNicolas Bonnefon             { file_name,
1938964a9adSNicolas Bonnefon             log_data,
1948964a9adSNicolas Bonnefon             log_filtered_data,
1958964a9adSNicolas Bonnefon             view } } );
1968964a9adSNicolas Bonnefon 
1978964a9adSNicolas Bonnefon     // Start loading the file
1988964a9adSNicolas Bonnefon     log_data->attachFile( QString( file_name.c_str() ) );
1998964a9adSNicolas Bonnefon 
2008964a9adSNicolas Bonnefon     return view;
2018964a9adSNicolas Bonnefon }
2028964a9adSNicolas Bonnefon 
20378b9e3f6SNicolas Bonnefon Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
20478b9e3f6SNicolas Bonnefon {
20578b9e3f6SNicolas Bonnefon     assert( view );
20678b9e3f6SNicolas Bonnefon 
2071b5e406eSNicolas Bonnefon     OpenFile* file = &( openFiles_.at( view ) );
20878b9e3f6SNicolas Bonnefon 
20978b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
21078b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
21178b9e3f6SNicolas Bonnefon 
21278b9e3f6SNicolas Bonnefon     return file;
21378b9e3f6SNicolas Bonnefon }
21478b9e3f6SNicolas Bonnefon 
21578b9e3f6SNicolas Bonnefon const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
21678b9e3f6SNicolas Bonnefon {
21778b9e3f6SNicolas Bonnefon     assert( view );
21878b9e3f6SNicolas Bonnefon 
2191b5e406eSNicolas Bonnefon     const OpenFile* file = &( openFiles_.at( view ) );
22078b9e3f6SNicolas Bonnefon 
22178b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
22278b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
22378b9e3f6SNicolas Bonnefon 
22478b9e3f6SNicolas Bonnefon     return file;
22578b9e3f6SNicolas Bonnefon }
226