xref: /glogg/src/session.cpp (revision cdd897793da1f7758ecf5612702baebac0ca9634)
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 "persistentinfo.h"
27 #include "savedsearches.h"
28 #include "data/logdata.h"
29 #include "data/logfiltereddata.h"
30 
31 Session::Session()
32 {
33     GetPersistentInfo().retrieve( QString( "savedSearches" ) );
34 
35     // Get the global search history
36     savedSearches_ = std::shared_ptr<SavedSearches>(
37             &(Persistent<SavedSearches>( "savedSearches" )) );
38 }
39 
40 Session::~Session()
41 {
42     // FIXME Clean up all the data objects...
43 }
44 
45 ViewInterface* Session::open( const std::string& file_name,
46         std::function<ViewInterface*()> view_factory )
47 {
48     ViewInterface* view = nullptr;
49 
50     QFileInfo fileInfo( file_name.c_str() );
51     if ( fileInfo.isReadable() )
52     {
53         // Create the data objects
54         auto log_data          = std::make_shared<LogData>();
55         auto log_filtered_data =
56             std::shared_ptr<LogFilteredData>( log_data->getNewFilteredData() );
57 
58         view = view_factory();
59         view->setData( log_data, log_filtered_data );
60         view->setSavedSearches( savedSearches_ );
61 
62         // Insert in the hash
63         openFiles_.insert( { view,
64                 { file_name,
65                   log_data,
66                   log_filtered_data,
67                   view } } );
68 
69         // Start loading the file
70         log_data->attachFile( QString( file_name.c_str() ) );
71     }
72     else {
73         // throw
74     }
75 
76     return view;
77 }
78 
79 void Session::close( const ViewInterface* view )
80 {
81     openFiles_.erase( openFiles_.find( view ) );
82 }
83 
84 void Session::getFileInfo( const ViewInterface* view, uint64_t* fileSize,
85         uint32_t* fileNbLine, QDateTime* lastModified ) const
86 {
87     const OpenFile* file = findOpenFileFromView( view );
88 
89     assert( file );
90 
91     *fileSize = file->logData->getFileSize();
92     *fileNbLine = file->logData->getNbLine();
93     *lastModified = file->logData->getLastModifiedDate();
94 }
95 
96 
97 /*
98  * Private methods
99  */
100 
101 Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view )
102 {
103     assert( view );
104 
105     OpenFile* file = &( openFiles_.at( view ) );
106 
107     // OpenfileMap::at might throw out_of_range but since a view MUST always
108     // be attached to a file, we don't handle it!
109 
110     return file;
111 }
112 
113 const Session::OpenFile* Session::findOpenFileFromView( const ViewInterface* view ) const
114 {
115     assert( view );
116 
117     const OpenFile* file = &( openFiles_.at( view ) );
118 
119     // OpenfileMap::at might throw out_of_range but since a view MUST always
120     // be attached to a file, we don't handle it!
121 
122     return file;
123 }
124