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