xref: /glogg/src/session.cpp (revision a44d09bc709514bfc9e2b6b4e435e83e8a36868a)
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() ) {
69*a44d09bcSNicolas 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 
83*a44d09bcSNicolas Bonnefon void Session::save( std::vector<
84*a44d09bcSNicolas Bonnefon         std::tuple<const ViewInterface*,
85*a44d09bcSNicolas Bonnefon             uint64_t,
86*a44d09bcSNicolas Bonnefon             std::shared_ptr<const ViewContextInterface>>
87*a44d09bcSNicolas Bonnefon         > view_list )
88b57881faSNicolas Bonnefon {
89b57881faSNicolas Bonnefon     LOG(logDEBUG) << "Session::save";
90b57881faSNicolas Bonnefon 
91b57881faSNicolas Bonnefon     std::vector<SessionInfo::OpenFile> session_files;
92b57881faSNicolas Bonnefon     for ( auto view: view_list ) {
93*a44d09bcSNicolas Bonnefon         const ViewInterface* view_object;
94*a44d09bcSNicolas Bonnefon         uint64_t top_line;
95*a44d09bcSNicolas Bonnefon         std::shared_ptr<const ViewContextInterface> view_context;
96*a44d09bcSNicolas Bonnefon 
97*a44d09bcSNicolas Bonnefon         std::tie( view_object, top_line, view_context ) = view;
98*a44d09bcSNicolas Bonnefon 
99*a44d09bcSNicolas Bonnefon         const OpenFile* file = findOpenFileFromView( view_object );
100b57881faSNicolas Bonnefon         assert( file );
101b57881faSNicolas Bonnefon 
102b57881faSNicolas Bonnefon         LOG(logDEBUG) << "Saving " << file->fileName << " in session.";
103*a44d09bcSNicolas Bonnefon         session_files.push_back( { file->fileName, top_line, view_context->toString() } );
104b57881faSNicolas Bonnefon     }
105b57881faSNicolas Bonnefon 
10611582726SNicolas Bonnefon     std::shared_ptr<SessionInfo> session =
10711582726SNicolas Bonnefon         Persistent<SessionInfo>( "session" );
10811582726SNicolas Bonnefon     session->setOpenFiles( session_files );
109b57881faSNicolas Bonnefon     GetPersistentInfo().save( QString( "session" ) );
110b57881faSNicolas Bonnefon }
111b57881faSNicolas Bonnefon 
1128964a9adSNicolas Bonnefon std::vector<std::pair<std::string, ViewInterface*>> Session::restore(
1138964a9adSNicolas Bonnefon         std::function<ViewInterface*()> view_factory,
1148964a9adSNicolas Bonnefon         int *current_file_index )
1158964a9adSNicolas Bonnefon {
116b57881faSNicolas Bonnefon     GetPersistentInfo().retrieve( QString( "session" ) );
11711582726SNicolas Bonnefon     std::shared_ptr<SessionInfo> session =
11811582726SNicolas Bonnefon         Persistent<SessionInfo>( "session" );
119b57881faSNicolas Bonnefon 
12011582726SNicolas Bonnefon     std::vector<SessionInfo::OpenFile> session_files = session->openFiles();
1218964a9adSNicolas Bonnefon     std::vector<std::pair<std::string, ViewInterface*>> result;
1228964a9adSNicolas Bonnefon 
1238964a9adSNicolas Bonnefon     for ( auto file: session_files )
1248964a9adSNicolas Bonnefon     {
12560864ff5SNicolas Bonnefon         LOG(logDEBUG) << "Create view for " << file.fileName;
126*a44d09bcSNicolas Bonnefon         ViewInterface* view = openAlways( file.fileName, view_factory, file.viewContext.c_str() );
127b57881faSNicolas Bonnefon         result.push_back( { file.fileName, view } );
1288964a9adSNicolas Bonnefon     }
1298964a9adSNicolas Bonnefon 
1308964a9adSNicolas Bonnefon     *current_file_index = -1;
1318964a9adSNicolas Bonnefon 
1328964a9adSNicolas Bonnefon     return result;
1338964a9adSNicolas Bonnefon }
1348964a9adSNicolas Bonnefon 
1350e97f16dSNicolas Bonnefon std::string Session::getFilename( const ViewInterface* view ) const
1360e97f16dSNicolas Bonnefon {
1370e97f16dSNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
1380e97f16dSNicolas Bonnefon 
1390e97f16dSNicolas Bonnefon     assert( file );
1400e97f16dSNicolas Bonnefon 
1410e97f16dSNicolas Bonnefon     return file->fileName;
1420e97f16dSNicolas Bonnefon }
1430e97f16dSNicolas Bonnefon 
14478b9e3f6SNicolas Bonnefon void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
14578b9e3f6SNicolas Bonnefon         uint32_t* fileNbLine, QDateTime* lastModified ) const
146039481acSNicolas Bonnefon {
14778b9e3f6SNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
14878b9e3f6SNicolas Bonnefon 
14978b9e3f6SNicolas Bonnefon     assert( file );
15078b9e3f6SNicolas Bonnefon 
15178b9e3f6SNicolas Bonnefon     *fileSize = file->logData->getFileSize();
15278b9e3f6SNicolas Bonnefon     *fileNbLine = file->logData->getNbLine();
15378b9e3f6SNicolas Bonnefon     *lastModified = file->logData->getLastModifiedDate();
154039481acSNicolas Bonnefon }
15578b9e3f6SNicolas Bonnefon 
15678b9e3f6SNicolas Bonnefon 
15778b9e3f6SNicolas Bonnefon /*
15878b9e3f6SNicolas Bonnefon  * Private methods
159039481acSNicolas Bonnefon  */
16078b9e3f6SNicolas Bonnefon 
1618964a9adSNicolas Bonnefon ViewInterface* Session::openAlways( const std::string& file_name,
162*a44d09bcSNicolas Bonnefon         std::function<ViewInterface*()> view_factory,
163*a44d09bcSNicolas Bonnefon         const char* view_context )
1648964a9adSNicolas Bonnefon {
1658964a9adSNicolas Bonnefon     // Create the data objects
1668964a9adSNicolas Bonnefon     auto log_data          = std::make_shared<LogData>();
1678964a9adSNicolas Bonnefon     auto log_filtered_data =
1688964a9adSNicolas Bonnefon         std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
1698964a9adSNicolas Bonnefon 
1708964a9adSNicolas Bonnefon     ViewInterface* view = view_factory();
1718964a9adSNicolas Bonnefon     view->setData( log_data, log_filtered_data );
1728964a9adSNicolas Bonnefon     view->setQuickFindPattern( quickFindPattern_ );
1738964a9adSNicolas Bonnefon     view->setSavedSearches( savedSearches_ );
1748964a9adSNicolas Bonnefon 
175*a44d09bcSNicolas Bonnefon     if ( view_context )
176*a44d09bcSNicolas Bonnefon         view->setViewContext( view_context );
177*a44d09bcSNicolas Bonnefon 
1788964a9adSNicolas Bonnefon     // Insert in the hash
1798964a9adSNicolas Bonnefon     openFiles_.insert( { view,
1808964a9adSNicolas Bonnefon             { file_name,
1818964a9adSNicolas Bonnefon             log_data,
1828964a9adSNicolas Bonnefon             log_filtered_data,
1838964a9adSNicolas Bonnefon             view } } );
1848964a9adSNicolas Bonnefon 
1858964a9adSNicolas Bonnefon     // Start loading the file
1868964a9adSNicolas Bonnefon     log_data->attachFile( QString( file_name.c_str() ) );
1878964a9adSNicolas Bonnefon 
1888964a9adSNicolas Bonnefon     return view;
1898964a9adSNicolas Bonnefon }
1908964a9adSNicolas Bonnefon 
19178b9e3f6SNicolas Bonnefon Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
19278b9e3f6SNicolas Bonnefon {
19378b9e3f6SNicolas Bonnefon     assert( view );
19478b9e3f6SNicolas Bonnefon 
1951b5e406eSNicolas Bonnefon     OpenFile* file = &( openFiles_.at( view ) );
19678b9e3f6SNicolas Bonnefon 
19778b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
19878b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
19978b9e3f6SNicolas Bonnefon 
20078b9e3f6SNicolas Bonnefon     return file;
20178b9e3f6SNicolas Bonnefon }
20278b9e3f6SNicolas Bonnefon 
20378b9e3f6SNicolas Bonnefon const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
20478b9e3f6SNicolas Bonnefon {
20578b9e3f6SNicolas Bonnefon     assert( view );
20678b9e3f6SNicolas Bonnefon 
2071b5e406eSNicolas Bonnefon     const 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 }
214