+bool Session::data_saved() const
+{
+ return data_saved_;
+}
+
+void Session::save_settings(QSettings &settings) const
+{
+ map<string, string> dev_info;
+ list<string> key_list;
+ int decode_signals = 0, views = 0;
+
+ if (device_) {
+ shared_ptr<devices::HardwareDevice> hw_device =
+ dynamic_pointer_cast< devices::HardwareDevice >(device_);
+
+ if (hw_device) {
+ settings.setValue("device_type", "hardware");
+ settings.beginGroup("device");
+
+ key_list.emplace_back("vendor");
+ key_list.emplace_back("model");
+ key_list.emplace_back("version");
+ key_list.emplace_back("serial_num");
+ key_list.emplace_back("connection_id");
+
+ dev_info = device_manager_.get_device_info(device_);
+
+ for (string key : key_list) {
+ if (dev_info.count(key))
+ settings.setValue(QString::fromUtf8(key.c_str()),
+ QString::fromUtf8(dev_info.at(key).c_str()));
+ else
+ settings.remove(QString::fromUtf8(key.c_str()));
+ }
+
+ settings.endGroup();
+ }
+
+ shared_ptr<devices::SessionFile> sessionfile_device =
+ dynamic_pointer_cast< devices::SessionFile >(device_);
+
+ if (sessionfile_device) {
+ settings.setValue("device_type", "sessionfile");
+ settings.beginGroup("device");
+ settings.setValue("filename", QString::fromStdString(
+ sessionfile_device->full_name()));
+ settings.endGroup();
+ }
+
+ // Save channels and decoders
+ for (shared_ptr<data::SignalBase> base : signalbases_) {
+#ifdef ENABLE_DECODE
+ if (base->is_decode_signal()) {
+ settings.beginGroup("decode_signal" + QString::number(decode_signals++));
+ base->save_settings(settings);
+ settings.endGroup();
+ } else
+#endif
+ {
+ settings.beginGroup(base->internal_name());
+ base->save_settings(settings);
+ settings.endGroup();
+ }
+ }
+
+ settings.setValue("decode_signals", decode_signals);
+
+ // Save view states and their signal settings
+ // Note: main_view must be saved as view0
+ settings.beginGroup("view" + QString::number(views++));
+ main_view_->save_settings(settings);
+ settings.endGroup();
+
+ for (shared_ptr<views::ViewBase> view : views_) {
+ if (view != main_view_) {
+ settings.beginGroup("view" + QString::number(views++));
+ view->save_settings(settings);
+ settings.endGroup();
+ }
+ }
+
+ settings.setValue("views", views);
+ }
+}
+
+void Session::restore_settings(QSettings &settings)
+{
+ shared_ptr<devices::Device> device;
+
+ QString device_type = settings.value("device_type").toString();
+
+ if (device_type == "hardware") {
+ map<string, string> dev_info;
+ list<string> key_list;
+
+ // Re-select last used device if possible but only if it's not demo
+ settings.beginGroup("device");
+ key_list.emplace_back("vendor");
+ key_list.emplace_back("model");
+ key_list.emplace_back("version");
+ key_list.emplace_back("serial_num");
+ key_list.emplace_back("connection_id");
+
+ for (string key : key_list) {
+ const QString k = QString::fromStdString(key);
+ if (!settings.contains(k))
+ continue;
+
+ const string value = settings.value(k).toString().toStdString();
+ if (!value.empty())
+ dev_info.insert(make_pair(key, value));
+ }
+
+ if (dev_info.count("model") > 0)
+ device = device_manager_.find_device_from_info(dev_info);
+
+ if (device)
+ set_device(device);
+
+ settings.endGroup();
+ }
+
+ if (device_type == "sessionfile") {
+ settings.beginGroup("device");
+ QString filename = settings.value("filename").toString();
+ settings.endGroup();
+
+ if (QFileInfo(filename).isReadable()) {
+ device = make_shared<devices::SessionFile>(device_manager_.context(),
+ filename.toStdString());
+ set_device(device);
+
+ // TODO Perform error handling
+ start_capture([](QString infoMessage) { (void)infoMessage; });
+
+ set_name(QFileInfo(filename).fileName());
+ }
+ }
+
+ if (device) {
+ // Restore channels
+ for (shared_ptr<data::SignalBase> base : signalbases_) {
+ settings.beginGroup(base->internal_name());
+ base->restore_settings(settings);
+ settings.endGroup();
+ }
+
+ // Restore decoders
+#ifdef ENABLE_DECODE
+ int decode_signals = settings.value("decode_signals").toInt();
+
+ for (int i = 0; i < decode_signals; i++) {
+ settings.beginGroup("decode_signal" + QString::number(i));
+ shared_ptr<data::DecodeSignal> signal = add_decode_signal();
+ signal->restore_settings(settings);
+ settings.endGroup();
+ }
+#endif
+
+ // Restore views
+ int views = settings.value("views").toInt();
+
+ for (int i = 0; i < views; i++) {
+ settings.beginGroup("view" + QString::number(i));
+
+ if (i > 0) {
+ views::ViewType type = (views::ViewType)settings.value("type").toInt();
+ add_view(name_, type, this);
+ views_.back()->restore_settings(settings);
+ } else
+ main_view_->restore_settings(settings);
+
+ settings.endGroup();
+ }
+ }
+}
+
+void Session::select_device(shared_ptr<devices::Device> device)
+{
+ try {
+ if (device)
+ set_device(device);
+ else
+ set_default_device();
+ } catch (const QString &e) {
+ main_bar_->session_error(tr("Failed to select device"), e);
+ }
+}
+