xref: /glogg/src/session.cpp (revision 78b9e3f63028f48b81724f3e782d8df2c1bc75c6)
1d96f3f21SNicolas Bonnefon /*
2d96f3f21SNicolas Bonnefon  * Copyright (C) 2013 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 
22*78b9e3f6SNicolas Bonnefon #include <cassert>
23039481acSNicolas Bonnefon #include <QFileInfo>
24039481acSNicolas Bonnefon 
25d96f3f21SNicolas Bonnefon #include "viewinterface.h"
26039481acSNicolas Bonnefon #include "data/logdata.h"
27039481acSNicolas Bonnefon #include "data/logfiltereddata.h"
28d96f3f21SNicolas Bonnefon 
29d96f3f21SNicolas Bonnefon Session::Session()
30d96f3f21SNicolas Bonnefon {
31d96f3f21SNicolas Bonnefon }
32d96f3f21SNicolas Bonnefon 
33d96f3f21SNicolas Bonnefon Session::~Session()
34d96f3f21SNicolas Bonnefon {
35d96f3f21SNicolas Bonnefon     // FIXME Clean up all the data objects...
36d96f3f21SNicolas Bonnefon }
37f0708ca8SNicolas Bonnefon 
38f0708ca8SNicolas Bonnefon ViewInterface* Session::open( const std::string& file_name,
39f0708ca8SNicolas Bonnefon         std::function<ViewInterface*()> view_factory )
40f0708ca8SNicolas Bonnefon {
41039481acSNicolas Bonnefon     ViewInterface* view = nullptr;
42039481acSNicolas Bonnefon 
43039481acSNicolas Bonnefon     QFileInfo fileInfo( file_name.c_str() );
44039481acSNicolas Bonnefon     if ( fileInfo.isReadable() )
45039481acSNicolas Bonnefon     {
46039481acSNicolas Bonnefon         // Create the data objects
47*78b9e3f6SNicolas Bonnefon         auto log_data          = std::make_shared<LogData>();
48*78b9e3f6SNicolas Bonnefon         auto log_filtered_data =
49*78b9e3f6SNicolas Bonnefon             std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
50039481acSNicolas Bonnefon 
51039481acSNicolas Bonnefon         view = view_factory();
52*78b9e3f6SNicolas Bonnefon         view->setData( log_data, log_filtered_data );
53*78b9e3f6SNicolas Bonnefon 
54*78b9e3f6SNicolas Bonnefon         // Insert in the hash
55*78b9e3f6SNicolas Bonnefon         open_files_.insert( { view,
56*78b9e3f6SNicolas Bonnefon                 { file_name,
57*78b9e3f6SNicolas Bonnefon                   log_data,
58*78b9e3f6SNicolas Bonnefon                   log_filtered_data,
59*78b9e3f6SNicolas Bonnefon                   view } } );
60039481acSNicolas Bonnefon 
61039481acSNicolas Bonnefon         // Start loading the file
62*78b9e3f6SNicolas Bonnefon         log_data->attachFile( QString( file_name.c_str() ) );
63039481acSNicolas Bonnefon     }
64039481acSNicolas Bonnefon     else {
65039481acSNicolas Bonnefon         // throw
66039481acSNicolas Bonnefon     }
67f0708ca8SNicolas Bonnefon 
68f0708ca8SNicolas Bonnefon     return view;
69f0708ca8SNicolas Bonnefon }
70*78b9e3f6SNicolas Bonnefon 
71*78b9e3f6SNicolas Bonnefon void Session::stopLoading( const ViewInterface* view )
72039481acSNicolas Bonnefon {
73*78b9e3f6SNicolas Bonnefon     OpenFile* file = findOpenFileFromView( view );
74*78b9e3f6SNicolas Bonnefon     if ( file ) {
75*78b9e3f6SNicolas Bonnefon         file->logFilteredData->interruptSearch();
76*78b9e3f6SNicolas Bonnefon         file->logData->interruptLoading();
77*78b9e3f6SNicolas Bonnefon     }
78039481acSNicolas Bonnefon }
79039481acSNicolas Bonnefon 
80*78b9e3f6SNicolas Bonnefon void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
81*78b9e3f6SNicolas Bonnefon         uint32_t* fileNbLine, QDateTime* lastModified ) const
82039481acSNicolas Bonnefon {
83*78b9e3f6SNicolas Bonnefon     const OpenFile* file = findOpenFileFromView( view );
84*78b9e3f6SNicolas Bonnefon 
85*78b9e3f6SNicolas Bonnefon     assert( file );
86*78b9e3f6SNicolas Bonnefon 
87*78b9e3f6SNicolas Bonnefon     *fileSize = file->logData->getFileSize();
88*78b9e3f6SNicolas Bonnefon     *fileNbLine = file->logData->getNbLine();
89*78b9e3f6SNicolas Bonnefon     *lastModified = file->logData->getLastModifiedDate();
90039481acSNicolas Bonnefon }
91*78b9e3f6SNicolas Bonnefon 
92*78b9e3f6SNicolas Bonnefon 
93*78b9e3f6SNicolas Bonnefon /*
94*78b9e3f6SNicolas Bonnefon  * Private methods
95039481acSNicolas Bonnefon  */
96*78b9e3f6SNicolas Bonnefon 
97*78b9e3f6SNicolas Bonnefon Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
98*78b9e3f6SNicolas Bonnefon {
99*78b9e3f6SNicolas Bonnefon     assert( view );
100*78b9e3f6SNicolas Bonnefon 
101*78b9e3f6SNicolas Bonnefon     OpenFile* file = &( open_files_.at( view ) );
102*78b9e3f6SNicolas Bonnefon 
103*78b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
104*78b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
105*78b9e3f6SNicolas Bonnefon 
106*78b9e3f6SNicolas Bonnefon     return file;
107*78b9e3f6SNicolas Bonnefon }
108*78b9e3f6SNicolas Bonnefon 
109*78b9e3f6SNicolas Bonnefon const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
110*78b9e3f6SNicolas Bonnefon {
111*78b9e3f6SNicolas Bonnefon     assert( view );
112*78b9e3f6SNicolas Bonnefon 
113*78b9e3f6SNicolas Bonnefon     const OpenFile* file = &( open_files_.at( view ) );
114*78b9e3f6SNicolas Bonnefon 
115*78b9e3f6SNicolas Bonnefon     // OpenfileMap::at might throw out_of_range but since a view MUST always
116*78b9e3f6SNicolas Bonnefon     // be attached to a file, we don't handle it!
117*78b9e3f6SNicolas Bonnefon 
118*78b9e3f6SNicolas Bonnefon     return file;
119*78b9e3f6SNicolas Bonnefon }
120