]> sigrok.org Git - pulseview.git/blame - pv/dialogs/storeprogress.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / dialogs / storeprogress.cpp
CommitLineData
0fbda3c2
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2014 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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
0fbda3c2
JH
18 */
19
3b68d03d 20#include <cassert>
0fbda3c2 21
1df756a1 22#include <QDebug>
bfc9f61e
JH
23#include <QMessageBox>
24
5ccfc97e
SA
25#include "pv/session.hpp"
26
2acdb232 27#include "storeprogress.hpp"
3b68d03d 28
e93f5538 29using std::map;
6f925ba9
UH
30using std::pair;
31using std::shared_ptr;
e93f5538
JH
32using std::string;
33
34using Glib::VariantBase;
35
0fbda3c2
JH
36namespace pv {
37namespace dialogs {
38
39StoreProgress::StoreProgress(const QString &file_name,
6f925ba9 40 const shared_ptr<sigrok::OutputFormat> output_format,
e93f5538 41 const map<string, VariantBase> &options,
6f925ba9 42 const pair<uint64_t, uint64_t> sample_range,
2b81ae46 43 const Session &session, QWidget *parent) :
0fbda3c2 44 QProgressDialog(tr("Saving..."), tr("Cancel"), 0, 0, parent),
d2fc6be9 45 session_(file_name.toStdString(), output_format, options, sample_range,
c3c07a5f
SA
46 session),
47 showing_error_(false)
0fbda3c2 48{
8dbbc7f0 49 connect(&session_, SIGNAL(progress_updated()),
0fbda3c2 50 this, SLOT(on_progress_updated()));
5ccfc97e
SA
51 connect(&session_, SIGNAL(store_successful()),
52 &session, SLOT(on_data_saved()));
cd38c84c 53 connect(this, SIGNAL(canceled()), this, SLOT(on_cancel()));
f7449c50
SA
54
55 // Since we're not setting any progress in case of an error, the dialog
56 // will pop up after the minimumDuration time has been reached - 4000 ms
57 // by default.
58 // We do not want this as it overlaps with the error message box, so we
59 // set the minimumDuration to 0 so that it only appears when we feed it
60 // progress data. Then, call reset() to prevent the progress dialog from
61 // popping up anyway. This would happen in Qt5 because the behavior was
62 // changed in such a way that the duration timer is started by the
63 // constructor. We don't want that and reset() stops the timer, so we
64 // use it.
65 setMinimumDuration(0);
66 reset();
0fbda3c2
JH
67}
68
69StoreProgress::~StoreProgress()
70{
8dbbc7f0 71 session_.wait();
0fbda3c2
JH
72}
73
74void StoreProgress::run()
75{
8dbbc7f0 76 if (session_.start())
0fbda3c2
JH
77 show();
78 else
79 show_error();
80}
81
82void StoreProgress::show_error()
83{
c3c07a5f
SA
84 showing_error_ = true;
85
1df756a1
SA
86 qDebug() << "Error trying to save:" << session_.error();
87
0fbda3c2 88 QMessageBox msg(parentWidget());
970fca0d 89 msg.setText(tr("Failed to save session.") + "\n\n" + session_.error());
0fbda3c2
JH
90 msg.setStandardButtons(QMessageBox::Ok);
91 msg.setIcon(QMessageBox::Warning);
92 msg.exec();
f7449c50
SA
93
94 close();
0fbda3c2
JH
95}
96
97void StoreProgress::closeEvent(QCloseEvent*)
98{
8dbbc7f0 99 session_.cancel();
f7449c50
SA
100
101 // Closing doesn't mean we're going to be destroyed because our parent
102 // still owns our handle. Make sure this stale instance doesn't hang around.
103 deleteLater();
0fbda3c2
JH
104}
105
106void StoreProgress::on_progress_updated()
107{
6f925ba9 108 const pair<int, int> p = session_.progress();
0fbda3c2
JH
109 assert(p.first <= p.second);
110
b101c41e
JH
111 if (p.second) {
112 setValue(p.first);
113 setMaximum(p.second);
114 } else {
8dbbc7f0 115 const QString err = session_.error();
a6fa4d47
R
116 if (err.isEmpty())
117 close();
118 else if (!showing_error_)
b101c41e 119 show_error();
0fbda3c2 120 }
0fbda3c2
JH
121}
122
cd38c84c
R
123void StoreProgress::on_cancel()
124{
125 session_.cancel();
126}
127
870ea3db
UH
128} // namespace dialogs
129} // namespace pv