xref: /glogg/src/session.cpp (revision 8964a9ad266d09def883e99b685d40df90727e7f)
1d96f3f21SNicolas Bonnefon /*
2*8964a9adSNicolas 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 
2278b9e3f6SNicolas Bonnefon #include <cassert>
23039481acSNicolas Bonnefon #include <QFileInfo>
24039481acSNicolas Bonnefon 
25d96f3f21SNicolas Bonnefon #include "viewinterface.h"
261b5e406eSNicolas Bonnefon #include "persistentinfo.h"
271b5e406eSNicolas Bonnefon #include "savedsearches.h"
28039481acSNicolas Bonnefon #include "data/logdata.h"
29039481acSNicolas Bonnefon #include "data/logfiltereddata.h"
30d96f3f21SNicolas Bonnefon 
31d96f3f21SNicolas Bonnefon Session::Session()
32d96f3f21SNicolas Bonnefon {
331b5e406eSNicolas Bonnefon     GetPersistentInfo().retrieve( QString( "savedSearches" ) );
341b5e406eSNicolas Bonnefon 
351b5e406eSNicolas Bonnefon     // Get the global search history
361b5e406eSNicolas Bonnefon     savedSearches_ = std::shared_ptr<SavedSearches>(
371b5e406eSNicolas Bonnefon             &(Persistent<SavedSearches>( "savedSearches" )) );
38b423cd88SNicolas Bonnefon 
39b423cd88SNicolas Bonnefon     quickFindPattern_ = std::make_shared<QuickFindPattern>();
40d96f3f21SNicolas Bonnefon }
41d96f3f21SNicolas Bonnefon 
42d96f3f21SNicolas Bonnefon Session::~Session()
43d96f3f21SNicolas Bonnefon {
44d96f3f21SNicolas Bonnefon     // FIXME Clean up all the data objects...
45d96f3f21SNicolas Bonnefon }
46f0708ca8SNicolas Bonnefon 
47f0708ca8SNicolas Bonnefon ViewInterface* Session::open( const std::string& file_name,
48f0708ca8SNicolas Bonnefon         std::function<ViewInterface*()> view_factory )
49f0708ca8SNicolas Bonnefon {
50039481acSNicolas Bonnefon     ViewInterface* view = nullptr;
51039481acSNicolas Bonnefon 
52039481acSNicolas Bonnefon     QFileInfo fileInfo( file_name.c_str() );
53039481acSNicolas Bonnefon     if ( fileInfo.isReadable() )
54039481acSNicolas Bonnefon     {
55*8964a9adSNicolas Bonnefon         return openAlways( file_name, view_factory );
56039481acSNicolas Bonnefon     }
57039481acSNicolas Bonnefon     else {
58*8964a9adSNicolas Bonnefon         // FIXME throw
59039481acSNicolas Bonnefon     }
60f0708ca8SNicolas Bonnefon 
61f0708ca8SNicolas Bonnefon     return view;
62f0708ca8SNicolas Bonnefon }
6378b9e3f6SNicolas Bonnefon 
64cdd89779SNicolas Bonnefon void Session::close( const ViewInterface* view )
65cdd89779SNicolas Bonnefon {
66cdd89779SNicolas Bonnefon     openFiles_.erase( openFiles_.find( view ) );
67cdd89779SNicolas Bonnefon }
68cdd89779SNicolas Bonnefon 
69*8964a9adSNicolas Bonnefon std::vector<std::pair<std::string, ViewInterface*>> Session::restore(
70*8964a9adSNicolas Bonnefon         std::function<ViewInterface*()> view_factory,
71*8964a9adSNicolas Bonnefon         int *current_file_index )
72*8964a9adSNicolas Bonnefon {
73*8964a9adSNicolas Bonnefon     std::vector<std::string> session_files = { "/tmp/test1", "/tmp/test2" };
74*8964a9adSNicolas Bonnefon     std::vector<std::pair<std::string, ViewInterface*>> result;
75*8964a9adSNicolas Bonnefon 
76*8964a9adSNicolas Bonnefon     for ( auto file: session_files )
77*8964a9adSNicolas Bonnefon     {
78*8964a9adSNicolas Bonnefon         ViewInterface* view = openAlways( file, view_factory );
79*8964a9adSNicolas Bonnefon         result.push_back( { file, view } );
80*8964a9adSNicolas Bonnefon     }
81*8964a9adSNicolas Bonnefon 
82*8964a9adSNicolas Bonnefon     *current_file_index = -1;
83*8964a9adSNicolas Bonnefon 
84*8964a9adSNicolas Bonnefon     return result;
85*8964a9adSNicolas Bonnefon }
86*8964a9adSNicolas Bonnefon 
8778b9e3f6SNicolas Bonnefon void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
8878b9e3f6SNicolas Bonnefon         uint32_t* fileNbLine, QDateTime* lastModified ) const
89039481acSNicolas Bonnefon {
9078b9e3f6SNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
9178b9e3f6SNicolas Bonnefon 
9278b9e3f6SNicolas Bonnefon     assert( file );
9378b9e3f6SNicolas Bonnefon 
9478b9e3f6SNicolas Bonnefon     *fileSize = file->logData->getFileSize();
9578b9e3f6SNicolas Bonnefon     *fileNbLine = file->logData->getNbLine();
9678b9e3f6SNicolas Bonnefon     *lastModified = file->logData->getLastModifiedDate();
97039481acSNicolas Bonnefon }
9878b9e3f6SNicolas Bonnefon 
9978b9e3f6SNicolas Bonnefon 
10078b9e3f6SNicolas Bonnefon /*
10178b9e3f6SNicolas Bonnefon  * Private methods
102039481acSNicolas Bonnefon  */
10378b9e3f6SNicolas Bonnefon 
104*8964a9adSNicolas Bonnefon ViewInterface* Session::openAlways( const std::string& file_name,
105*8964a9adSNicolas Bonnefon         std::function<ViewInterface*()> view_factory )
106*8964a9adSNicolas Bonnefon {
107*8964a9adSNicolas Bonnefon     // Create the data objects
108*8964a9adSNicolas Bonnefon     auto log_data          = std::make_shared<LogData>();
109*8964a9adSNicolas Bonnefon     auto log_filtered_data =
110*8964a9adSNicolas Bonnefon         std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
111*8964a9adSNicolas Bonnefon 
112*8964a9adSNicolas Bonnefon     ViewInterface* view = view_factory();
113*8964a9adSNicolas Bonnefon     view->setData( log_data, log_filtered_data );
114*8964a9adSNicolas Bonnefon     view->setQuickFindPattern( quickFindPattern_ );
115*8964a9adSNicolas Bonnefon     view->setSavedSearches( savedSearches_ );
116*8964a9adSNicolas Bonnefon 
117*8964a9adSNicolas Bonnefon     // Insert in the hash
118*8964a9adSNicolas Bonnefon     openFiles_.insert( { view,
119*8964a9adSNicolas Bonnefon             { file_name,
120*8964a9adSNicolas Bonnefon             log_data,
121*8964a9adSNicolas Bonnefon             log_filtered_data,
122*8964a9adSNicolas Bonnefon             view } } );
123*8964a9adSNicolas Bonnefon 
124*8964a9adSNicolas Bonnefon     // Start loading the file
125*8964a9adSNicolas Bonnefon     log_data->attachFile( QString( file_name.c_str() ) );
126*8964a9adSNicolas Bonnefon 
127*8964a9adSNicolas Bonnefon     return view;
128*8964a9adSNicolas Bonnefon }
129*8964a9adSNicolas Bonnefon 
13078b9e3f6SNicolas Bonnefon Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
13178b9e3f6SNicolas Bonnefon {
13278b9e3f6SNicolas Bonnefon     assert( view );
13378b9e3f6SNicolas Bonnefon 
1341b5e406eSNicolas Bonnefon     OpenFile* file = &( openFiles_.at( view ) );
13578b9e3f6SNicolas Bonnefon 
13678b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
13778b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
13878b9e3f6SNicolas Bonnefon 
13978b9e3f6SNicolas Bonnefon     return file;
14078b9e3f6SNicolas Bonnefon }
14178b9e3f6SNicolas Bonnefon 
14278b9e3f6SNicolas Bonnefon const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
14378b9e3f6SNicolas Bonnefon {
14478b9e3f6SNicolas Bonnefon     assert( view );
14578b9e3f6SNicolas Bonnefon 
1461b5e406eSNicolas Bonnefon     const OpenFile* file = &( openFiles_.at( view ) );
14778b9e3f6SNicolas Bonnefon 
14878b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
14978b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
15078b9e3f6SNicolas Bonnefon 
15178b9e3f6SNicolas Bonnefon     return file;
15278b9e3f6SNicolas Bonnefon }
153