xref: /glogg/src/session.cpp (revision 7847299cf77ef989e7bddad517b120ce847445ae)
1 /*
2  * Copyright (C) 2013 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "session.h"
21 
22 #include <cassert>
23 #include <QFileInfo>
24 
25 #include "viewinterface.h"
26 #include "data/logdata.h"
27 #include "data/logfiltereddata.h"
28 
29 Session::Session()
30 {
31 }
32 
33 Session::~Session()
34 {
35     // FIXME Clean up all the data objects...
36 }
37 
38 ViewInterface* Session::open( const std::string& file_name,
39         std::function<ViewInterface*()> view_factory )
40 {
41     ViewInterface* view = nullptr;
42 
43     QFileInfo fileInfo( file_name.c_str() );
44     if ( fileInfo.isReadable() )
45     {
46         // Create the data objects
47         auto log_data          = std::make_shared<LogData>();
48         auto log_filtered_data =
49             std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
50 
51         view = view_factory();
52         view->setData( log_data, log_filtered_data );
53 
54         // Insert in the hash
55         open_files_.insert( { view,
56                 { file_name,
57                   log_data,
58                   log_filtered_data,
59                   view } } );
60 
61         // Start loading the file
62         log_data->attachFile( QString( file_name.c_str() ) );
63     }
64     else {
65         // throw
66     }
67 
68     return view;
69 }
70 
71 void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
72         uint32_t* fileNbLine, QDateTime* lastModified ) const
73 {
74     const OpenFile* file = findOpenFileFromView( view );
75 
76     assert( file );
77 
78     *fileSize = file->logData->getFileSize();
79     *fileNbLine = file->logData->getNbLine();
80     *lastModified = file->logData->getLastModifiedDate();
81 }
82 
83 
84 /*
85  * Private methods
86  */
87 
88 Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
89 {
90     assert( view );
91 
92     OpenFile* file = &( open_files_.at( view ) );
93 
94     // OpenfileMap::at might throw out_of_range but since a view MUST always
95     // be attached to a file, we don't handle it!
96 
97     return file;
98 }
99 
100 const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
101 {
102     assert( view );
103 
104     const OpenFile* file = &( open_files_.at( view ) );
105 
106     // OpenfileMap::at might throw out_of_range but since a view MUST always
107     // be attached to a file, we don't handle it!
108 
109     return file;
110 }
111