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