Add imported files to the session save/restore mechanism
[pulseview.git] / pv / devices / inputfile.cpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2015 Joel Holdsworth <joel@airwebreathe.org.uk>
5  *
6  * This program 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 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <cassert>
21 #include <fstream>
22 #include <vector>
23
24 #include <QDebug>
25 #include <QString>
26
27 #include <pv/globalsettings.hpp>
28
29 #include "inputfile.hpp"
30
31 using sigrok::InputFormat;
32
33 using std::map;
34 using std::out_of_range;
35 using std::pair;
36 using std::shared_ptr;
37 using std::streamsize;
38 using std::string;
39 using std::ifstream;
40 using std::ios;
41 using std::vector;
42
43 namespace pv {
44 namespace devices {
45
46 // Use a 4MB chunk size for reading a file into memory. Larger values don't
47 // seem to provide any substancial performance improvements, but can cause
48 // UI lag and a visually "stuttering" display of the data currently loading.
49 const streamsize InputFile::BufferSize = (4 * 1024 * 1024);
50
51 InputFile::InputFile(const shared_ptr<sigrok::Context> &context,
52         const string &file_name,
53         shared_ptr<sigrok::InputFormat> format,
54         const map<string, Glib::VariantBase> &options) :
55         File(file_name),
56         context_(context),
57         format_(format),
58         options_(options),
59         interrupt_(false)
60 {
61 }
62
63 InputFile::InputFile(const shared_ptr<sigrok::Context> &context,
64         QSettings &settings):
65         File(""),
66         context_(context),
67         interrupt_(false)
68 {
69         file_name_ = settings.value("filename").toString().toStdString();
70
71         QString format_name = settings.value("format").toString();
72
73         // Find matching format
74         const map<string, shared_ptr<InputFormat> > formats = context->input_formats();
75
76         try {
77                 format_ = formats.at(format_name.toStdString());
78
79                 // Restore all saved options
80                 int options = settings.value("options").toInt();
81
82                 for (int i = 0; i < options; i++) {
83                         settings.beginGroup("option" + QString::number(i));
84                         QString name = settings.value("name").toString();
85                         options_[name.toStdString()] = GlobalSettings::restore_variantbase(settings);
86                         settings.endGroup();
87                 }
88
89         } catch (out_of_range) {
90                 qWarning() << "Could not find input format" << format_name <<
91                         "needed to restore session input file";
92         }
93 }
94
95 void InputFile::save_meta_to_settings(QSettings &settings)
96 {
97         settings.setValue("filename", QString::fromStdString(file_name_));
98
99         settings.setValue("format", QString::fromStdString(format_->name()));
100
101         settings.setValue("options", (int)options_.size());
102
103         int i = 0;
104         for (pair<string, Glib::VariantBase> option : options_) {
105                 settings.beginGroup("option" + QString::number(i));
106                 settings.setValue("name", QString::fromStdString(option.first));
107                 GlobalSettings::store_variantbase(settings, option.second);
108                 settings.endGroup();
109                 i++;
110         }
111 }
112
113 void InputFile::open()
114 {
115         if (session_)
116                 close();
117         else
118                 session_ = context_->create_session();
119
120         if (!format_)
121                 return;
122
123         input_ = format_->create_input(options_);
124
125         if (!input_)
126                 throw QString("Failed to create input");
127
128         // open() should add the input device to the session but
129         // we can't open the device without sending some data first
130         f = new ifstream(file_name_, ios::binary);
131
132         vector<char> buffer(BufferSize);
133
134         f->read(buffer.data(), BufferSize);
135         const streamsize size = f->gcount();
136         if (size == 0)
137                 return;
138
139         input_->send(buffer.data(), size);
140
141         try {
142                 device_ = input_->device();
143         } catch (sigrok::Error&) {
144                 return;
145         }
146
147         session_->add_device(device_);
148 }
149
150 void InputFile::close()
151 {
152         if (session_)
153                 session_->remove_devices();
154 }
155
156 void InputFile::start()
157 {
158 }
159
160 void InputFile::run()
161 {
162         if (!input_)
163                 return;
164
165         if (!f) {
166                 // Previous call to run() processed the entire file already
167                 f = new ifstream(file_name_, ios::binary);
168                 input_->reset();
169         }
170
171         vector<char> buffer(BufferSize);
172
173         interrupt_ = false;
174         while (!interrupt_ && !f->eof()) {
175                 f->read(buffer.data(), BufferSize);
176                 const streamsize size = f->gcount();
177                 if (size == 0)
178                         break;
179
180                 input_->send(buffer.data(), size);
181
182                 if (size != BufferSize)
183                         break;
184         }
185
186         input_->end();
187
188         delete f;
189         f = nullptr;
190 }
191
192 void InputFile::stop()
193 {
194         interrupt_ = true;
195 }
196
197 } // namespace devices
198 } // namespace pv