]> sigrok.org Git - pulseview.git/blame - pv/application.cpp
Session: Fix issue #67 by improving error handling
[pulseview.git] / pv / application.cpp
CommitLineData
dac1bb97
ML
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
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/>.
dac1bb97
ML
18 */
19
49719858 20#include <iostream>
c1a688de
SA
21#include <typeinfo>
22
23#include <QDebug>
380f4ee6 24#include <QDir>
4353c902 25#include <QLibraryInfo>
380f4ee6
SA
26#include <QMessageBox>
27#include <QWidget>
c1a688de 28
49719858
SA
29#include <boost/version.hpp>
30
c1a688de
SA
31#ifdef ENABLE_STACKTRACE
32#include <boost/stacktrace.hpp>
33#endif
dac1bb97 34
49719858
SA
35#ifdef ENABLE_DECODE
36#include <libsigrokdecode/libsigrokdecode.h>
37#endif
38
5a8f700c
SA
39#include <pv/exprtk.hpp>
40
0466001b
SA
41#include "application.hpp"
42#include "config.h"
43#include "globalsettings.hpp"
44
49719858
SA
45using std::cout;
46using std::endl;
6f925ba9 47using std::exception;
49719858
SA
48using std::shared_ptr;
49
50#ifdef ENABLE_DECODE
51static gint sort_pds(gconstpointer a, gconstpointer b)
52{
53 const struct srd_decoder *sda, *sdb;
54
55 sda = (const struct srd_decoder *)a;
56 sdb = (const struct srd_decoder *)b;
57 return strcmp(sda->id, sdb->id);
58}
59#endif
6f925ba9 60
dac1bb97
ML
61Application::Application(int &argc, char* argv[]) :
62 QApplication(argc, argv)
63{
64 setApplicationVersion(PV_VERSION_STRING);
65 setApplicationName("PulseView");
39eb0d45 66 setOrganizationName("sigrok");
dac1bb97
ML
67 setOrganizationDomain("sigrok.org");
68}
69
c8e2f09b 70const QStringList Application::get_languages() const
380f4ee6 71{
8fa8412e 72 const QStringList files = QDir(":/l10n/").entryList(QStringList("*.qm"), QDir::Files);
380f4ee6
SA
73
74 QStringList result;
75 result << "en"; // Add default language to the set
76
77 // Remove file extensions
49fee853 78 for (const QString& file : files)
380f4ee6
SA
79 result << file.split(".").front();
80
81 result.sort(Qt::CaseInsensitive);
82
83 return result;
84}
85
c8e2f09b
SA
86const QString Application::get_language_editors(const QString& language) const
87{
88 if (language == "de") return "Sören Apel, Uwe Hermann";
82c6d8f0 89 if (language == "es_MX") return "Carlos Diaz, Ulices Avila Hernandez";
90 if (language == "ja_jp") return "Yukari Shoji";
91 if (language == "zh_cn") return "ZtyPro";
c8e2f09b
SA
92
93 return QString();
94}
95
0466001b
SA
96void Application::switch_language(const QString& language)
97{
98 removeTranslator(&app_translator_);
99 removeTranslator(&qt_translator_);
4353c902 100 removeTranslator(&qtbase_translator_);
0466001b
SA
101
102 if ((language != "C") && (language != "en")) {
103 // Application translations
104 QString resource = ":/l10n/" + language +".qm";
105 if (app_translator_.load(resource))
106 installTranslator(&app_translator_);
107 else
108 qWarning() << "Translation resource" << resource << "not found";
109
110 // Qt translations
1ed73ebd
VPP
111#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
112 QString tr_path(QLibraryInfo::path(QLibraryInfo::TranslationsPath));
113#else
4353c902 114 QString tr_path(QLibraryInfo::location(QLibraryInfo::TranslationsPath));
1ed73ebd 115#endif
4353c902
SA
116
117 if (qt_translator_.load("qt_" + language, tr_path))
0466001b
SA
118 installTranslator(&qt_translator_);
119 else
4353c902
SA
120 qWarning() << "QT translations for" << language << "not found at" <<
121 tr_path << ", Qt translations package is probably missing";
122
123 // Qt base translations
124 if (qtbase_translator_.load("qtbase_" + language, tr_path))
125 installTranslator(&qtbase_translator_);
126 else
127 qWarning() << "QT base translations for" << language << "not found at" <<
128 tr_path << ", Qt translations package is probably missing";
0466001b 129 }
380f4ee6
SA
130
131 if (!topLevelWidgets().empty()) {
132 // Force all windows to update
133 for (QWidget *widget : topLevelWidgets())
134 widget->update();
135
136 QMessageBox msg(topLevelWidgets().front());
137 msg.setText(tr("Some parts of the application may still " \
138 "use the previous language. Re-opening the affected windows or " \
139 "restarting the application will remedy this."));
140 msg.setStandardButtons(QMessageBox::Ok);
141 msg.setIcon(QMessageBox::Information);
142 msg.exec();
143 }
0466001b
SA
144}
145
146void Application::on_setting_changed(const QString &key, const QVariant &value)
147{
148 if (key == pv::GlobalSettings::Key_General_Language)
149 switch_language(value.toString());
150}
151
22dc4040 152void Application::collect_version_info(pv::DeviceManager &device_manager)
49719858
SA
153{
154 // Library versions and features
155 version_info_.emplace_back(applicationName(), applicationVersion());
156 version_info_.emplace_back("Qt", qVersion());
157 version_info_.emplace_back("glibmm", PV_GLIBMM_VERSION);
158 version_info_.emplace_back("Boost", BOOST_LIB_VERSION);
5a8f700c 159 version_info_.emplace_back("exprtk", QString::fromUtf8(exprtk::information::date));
49719858
SA
160
161 version_info_.emplace_back("libsigrok", QString("%1/%2 (rt: %3/%4)")
162 .arg(SR_PACKAGE_VERSION_STRING, SR_LIB_VERSION_STRING,
163 sr_package_version_string_get(), sr_lib_version_string_get()));
164
165 GSList *l_orig = sr_buildinfo_libs_get();
166 for (GSList *l = l_orig; l; l = l->next) {
167 GSList *m = (GSList *)l->data;
168 const char *lib = (const char *)m->data;
169 const char *version = (const char *)m->next->data;
170 version_info_.emplace_back(QString(" - %1").arg(QString(lib)), QString(version));
171 g_slist_free_full(m, g_free);
172 }
173 g_slist_free(l_orig);
174
175 char *host = sr_buildinfo_host_get();
176 version_info_.emplace_back(" - Host", QString(host));
177 g_free(host);
178
179 char *scpi_backends = sr_buildinfo_scpi_backends_get();
180 version_info_.emplace_back(" - SCPI backends", QString(scpi_backends));
181 g_free(scpi_backends);
182
183#ifdef ENABLE_DECODE
184 struct srd_decoder *dec;
185
186 version_info_.emplace_back("libsigrokdecode", QString("%1/%2 (rt: %3/%4)")
187 .arg(SRD_PACKAGE_VERSION_STRING, SRD_LIB_VERSION_STRING,
188 srd_package_version_string_get(), srd_lib_version_string_get()));
189
190 l_orig = srd_buildinfo_libs_get();
191 for (GSList *l = l_orig; l; l = l->next) {
192 GSList *m = (GSList *)l->data;
193 const char *lib = (const char *)m->data;
194 const char *version = (const char *)m->next->data;
195 version_info_.emplace_back(QString(" - %1").arg(QString(lib)), QString(version));
196 g_slist_free_full(m, g_free);
197 }
198 g_slist_free(l_orig);
199
200 host = srd_buildinfo_host_get();
201 version_info_.emplace_back(" - Host", QString(host));
202 g_free(host);
203#endif
204
205 // Firmware paths
206 l_orig = sr_resourcepaths_get(SR_RESOURCE_FIRMWARE);
207 for (GSList *l = l_orig; l; l = l->next)
d2d0056d 208 fw_path_list_.emplace_back((char*)l->data);
49719858
SA
209 g_slist_free_full(l_orig, g_free);
210
211 // PD paths
212#ifdef ENABLE_DECODE
213 l_orig = srd_searchpaths_get();
214 for (GSList *l = l_orig; l; l = l->next)
d2d0056d 215 pd_path_list_.emplace_back((char*)l->data);
49719858
SA
216 g_slist_free_full(l_orig, g_free);
217#endif
218
219 // Device drivers
22dc4040
SA
220 for (auto& entry : device_manager.context()->drivers())
221 if (device_manager.driver_supported(entry.second))
222 driver_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
223 QString::fromUtf8(entry.second->long_name().c_str()));
49719858
SA
224
225 // Input formats
22dc4040 226 for (auto& entry : device_manager.context()->input_formats())
49719858
SA
227 input_format_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
228 QString::fromUtf8(entry.second->description().c_str()));
229
230 // Output formats
22dc4040 231 for (auto& entry : device_manager.context()->output_formats())
49719858
SA
232 output_format_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
233 QString::fromUtf8(entry.second->description().c_str()));
234
235 // Protocol decoders
236#ifdef ENABLE_DECODE
237 GSList *sl = g_slist_copy((GSList *)srd_decoder_list());
238 sl = g_slist_sort(sl, sort_pds);
239 for (const GSList *l = sl; l; l = l->next) {
240 dec = (struct srd_decoder *)l->data;
241 pd_list_.emplace_back(QString::fromUtf8(dec->id),
242 QString::fromUtf8(dec->longname));
243 }
244 g_slist_free(sl);
245#endif
246}
247
248void Application::print_version_info()
249{
2e2ddbe9 250 cout << PV_TITLE << " " << PV_VERSION_STRING << endl;
49719858
SA
251
252 cout << endl << "Libraries and features:" << endl;
f4ab4b5c 253 for (pair<QString, QString>& entry : version_info_)
49719858
SA
254 cout << " " << entry.first.toStdString() << " " << entry.second.toStdString() << endl;
255
256 cout << endl << "Firmware search paths:" << endl;
f4ab4b5c 257 for (QString& entry : fw_path_list_)
49719858
SA
258 cout << " " << entry.toStdString() << endl;
259
260 cout << endl << "Protocol decoder search paths:" << endl;
f4ab4b5c 261 for (QString& entry : pd_path_list_)
49719858
SA
262 cout << " " << entry.toStdString() << endl;
263
264 cout << endl << "Supported hardware drivers:" << endl;
f4ab4b5c 265 for (pair<QString, QString>& entry : driver_list_)
49719858
SA
266 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
267 entry.second.toStdString() << endl;
268
269 cout << endl << "Supported input formats:" << endl;
f4ab4b5c 270 for (pair<QString, QString>& entry : input_format_list_)
49719858
SA
271 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
272 entry.second.toStdString() << endl;
273
274 cout << endl << "Supported output formats:" << endl;
f4ab4b5c 275 for (pair<QString, QString>& entry : output_format_list_)
49719858
SA
276 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
277 entry.second.toStdString() << endl;
278
279#ifdef ENABLE_DECODE
280 cout << endl << "Supported protocol decoders:" << endl;
f4ab4b5c 281 for (pair<QString, QString>& entry : pd_list_)
49719858
SA
282 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
283 entry.second.toStdString() << endl;
284#endif
49719858
SA
285}
286
287vector< pair<QString, QString> > Application::get_version_info() const
288{
289 return version_info_;
290}
291
292vector<QString> Application::get_fw_path_list() const
293{
294 return fw_path_list_;
295}
296
297vector<QString> Application::get_pd_path_list() const
298{
299 return pd_path_list_;
300}
301
302vector< pair<QString, QString> > Application::get_driver_list() const
303{
304 return driver_list_;
305}
306
307vector< pair<QString, QString> > Application::get_input_format_list() const
308{
309 return input_format_list_;
310}
311
312vector< pair<QString, QString> > Application::get_output_format_list() const
313{
314 return output_format_list_;
315}
316
317vector< pair<QString, QString> > Application::get_pd_list() const
318{
319 return pd_list_;
320}
321
dac1bb97
ML
322bool Application::notify(QObject *receiver, QEvent *event)
323{
324 try {
325 return QApplication::notify(receiver, event);
6f925ba9 326 } catch (exception& e) {
c1a688de
SA
327 qDebug().nospace() << "Caught exception of type " << \
328 typeid(e).name() << " (" << e.what() << ")";
329#ifdef ENABLE_STACKTRACE
330 throw e;
331#else
dac1bb97 332 exit(1);
c1a688de 333#endif
dac1bb97
ML
334 return false;
335 }
336}