]> sigrok.org Git - pulseview.git/blame - pv/application.cpp
Implement translations
[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>
24
49719858
SA
25#include <boost/version.hpp>
26
c1a688de
SA
27#ifdef ENABLE_STACKTRACE
28#include <boost/stacktrace.hpp>
29#endif
dac1bb97 30
49719858
SA
31#ifdef ENABLE_DECODE
32#include <libsigrokdecode/libsigrokdecode.h>
33#endif
34
0466001b
SA
35#include "application.hpp"
36#include "config.h"
37#include "globalsettings.hpp"
38
49719858
SA
39using std::cout;
40using std::endl;
6f925ba9 41using std::exception;
49719858
SA
42using std::shared_ptr;
43
44#ifdef ENABLE_DECODE
45static gint sort_pds(gconstpointer a, gconstpointer b)
46{
47 const struct srd_decoder *sda, *sdb;
48
49 sda = (const struct srd_decoder *)a;
50 sdb = (const struct srd_decoder *)b;
51 return strcmp(sda->id, sdb->id);
52}
53#endif
6f925ba9 54
dac1bb97
ML
55Application::Application(int &argc, char* argv[]) :
56 QApplication(argc, argv)
57{
58 setApplicationVersion(PV_VERSION_STRING);
59 setApplicationName("PulseView");
39eb0d45 60 setOrganizationName("sigrok");
dac1bb97
ML
61 setOrganizationDomain("sigrok.org");
62}
63
0466001b
SA
64void Application::switch_language(const QString& language)
65{
66 removeTranslator(&app_translator_);
67 removeTranslator(&qt_translator_);
68
69 if ((language != "C") && (language != "en")) {
70 // Application translations
71 QString resource = ":/l10n/" + language +".qm";
72 if (app_translator_.load(resource))
73 installTranslator(&app_translator_);
74 else
75 qWarning() << "Translation resource" << resource << "not found";
76
77 // Qt translations
78 resource = ":/l10n/qtbase_" + language +".qm";
79 if (qt_translator_.load(resource))
80 installTranslator(&qt_translator_);
81 else
82 qWarning() << "Translation resource" << resource << "not found";
83 }
84}
85
86void Application::on_setting_changed(const QString &key, const QVariant &value)
87{
88 if (key == pv::GlobalSettings::Key_General_Language)
89 switch_language(value.toString());
90}
91
49719858
SA
92void Application::collect_version_info(shared_ptr<sigrok::Context> context)
93{
94 // Library versions and features
95 version_info_.emplace_back(applicationName(), applicationVersion());
96 version_info_.emplace_back("Qt", qVersion());
97 version_info_.emplace_back("glibmm", PV_GLIBMM_VERSION);
98 version_info_.emplace_back("Boost", BOOST_LIB_VERSION);
99
100 version_info_.emplace_back("libsigrok", QString("%1/%2 (rt: %3/%4)")
101 .arg(SR_PACKAGE_VERSION_STRING, SR_LIB_VERSION_STRING,
102 sr_package_version_string_get(), sr_lib_version_string_get()));
103
104 GSList *l_orig = sr_buildinfo_libs_get();
105 for (GSList *l = l_orig; l; l = l->next) {
106 GSList *m = (GSList *)l->data;
107 const char *lib = (const char *)m->data;
108 const char *version = (const char *)m->next->data;
109 version_info_.emplace_back(QString(" - %1").arg(QString(lib)), QString(version));
110 g_slist_free_full(m, g_free);
111 }
112 g_slist_free(l_orig);
113
114 char *host = sr_buildinfo_host_get();
115 version_info_.emplace_back(" - Host", QString(host));
116 g_free(host);
117
118 char *scpi_backends = sr_buildinfo_scpi_backends_get();
119 version_info_.emplace_back(" - SCPI backends", QString(scpi_backends));
120 g_free(scpi_backends);
121
122#ifdef ENABLE_DECODE
123 struct srd_decoder *dec;
124
125 version_info_.emplace_back("libsigrokdecode", QString("%1/%2 (rt: %3/%4)")
126 .arg(SRD_PACKAGE_VERSION_STRING, SRD_LIB_VERSION_STRING,
127 srd_package_version_string_get(), srd_lib_version_string_get()));
128
129 l_orig = srd_buildinfo_libs_get();
130 for (GSList *l = l_orig; l; l = l->next) {
131 GSList *m = (GSList *)l->data;
132 const char *lib = (const char *)m->data;
133 const char *version = (const char *)m->next->data;
134 version_info_.emplace_back(QString(" - %1").arg(QString(lib)), QString(version));
135 g_slist_free_full(m, g_free);
136 }
137 g_slist_free(l_orig);
138
139 host = srd_buildinfo_host_get();
140 version_info_.emplace_back(" - Host", QString(host));
141 g_free(host);
142#endif
143
144 // Firmware paths
145 l_orig = sr_resourcepaths_get(SR_RESOURCE_FIRMWARE);
146 for (GSList *l = l_orig; l; l = l->next)
d2d0056d 147 fw_path_list_.emplace_back((char*)l->data);
49719858
SA
148 g_slist_free_full(l_orig, g_free);
149
150 // PD paths
151#ifdef ENABLE_DECODE
152 l_orig = srd_searchpaths_get();
153 for (GSList *l = l_orig; l; l = l->next)
d2d0056d 154 pd_path_list_.emplace_back((char*)l->data);
49719858
SA
155 g_slist_free_full(l_orig, g_free);
156#endif
157
158 // Device drivers
f4ab4b5c 159 for (auto& entry : context->drivers())
49719858
SA
160 driver_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
161 QString::fromUtf8(entry.second->long_name().c_str()));
162
163 // Input formats
f4ab4b5c 164 for (auto& entry : context->input_formats())
49719858
SA
165 input_format_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
166 QString::fromUtf8(entry.second->description().c_str()));
167
168 // Output formats
f4ab4b5c 169 for (auto& entry : context->output_formats())
49719858
SA
170 output_format_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
171 QString::fromUtf8(entry.second->description().c_str()));
172
173 // Protocol decoders
174#ifdef ENABLE_DECODE
175 GSList *sl = g_slist_copy((GSList *)srd_decoder_list());
176 sl = g_slist_sort(sl, sort_pds);
177 for (const GSList *l = sl; l; l = l->next) {
178 dec = (struct srd_decoder *)l->data;
179 pd_list_.emplace_back(QString::fromUtf8(dec->id),
180 QString::fromUtf8(dec->longname));
181 }
182 g_slist_free(sl);
183#endif
184}
185
186void Application::print_version_info()
187{
2e2ddbe9 188 cout << PV_TITLE << " " << PV_VERSION_STRING << endl;
49719858
SA
189
190 cout << endl << "Libraries and features:" << endl;
f4ab4b5c 191 for (pair<QString, QString>& entry : version_info_)
49719858
SA
192 cout << " " << entry.first.toStdString() << " " << entry.second.toStdString() << endl;
193
194 cout << endl << "Firmware search paths:" << endl;
f4ab4b5c 195 for (QString& entry : fw_path_list_)
49719858
SA
196 cout << " " << entry.toStdString() << endl;
197
198 cout << endl << "Protocol decoder search paths:" << endl;
f4ab4b5c 199 for (QString& entry : pd_path_list_)
49719858
SA
200 cout << " " << entry.toStdString() << endl;
201
202 cout << endl << "Supported hardware drivers:" << endl;
f4ab4b5c 203 for (pair<QString, QString>& entry : driver_list_)
49719858
SA
204 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
205 entry.second.toStdString() << endl;
206
207 cout << endl << "Supported input formats:" << endl;
f4ab4b5c 208 for (pair<QString, QString>& entry : input_format_list_)
49719858
SA
209 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
210 entry.second.toStdString() << endl;
211
212 cout << endl << "Supported output formats:" << endl;
f4ab4b5c 213 for (pair<QString, QString>& entry : output_format_list_)
49719858
SA
214 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
215 entry.second.toStdString() << endl;
216
217#ifdef ENABLE_DECODE
218 cout << endl << "Supported protocol decoders:" << endl;
f4ab4b5c 219 for (pair<QString, QString>& entry : pd_list_)
49719858
SA
220 cout << " " << entry.first.leftJustified(21, ' ').toStdString() <<
221 entry.second.toStdString() << endl;
222#endif
49719858
SA
223}
224
225vector< pair<QString, QString> > Application::get_version_info() const
226{
227 return version_info_;
228}
229
230vector<QString> Application::get_fw_path_list() const
231{
232 return fw_path_list_;
233}
234
235vector<QString> Application::get_pd_path_list() const
236{
237 return pd_path_list_;
238}
239
240vector< pair<QString, QString> > Application::get_driver_list() const
241{
242 return driver_list_;
243}
244
245vector< pair<QString, QString> > Application::get_input_format_list() const
246{
247 return input_format_list_;
248}
249
250vector< pair<QString, QString> > Application::get_output_format_list() const
251{
252 return output_format_list_;
253}
254
255vector< pair<QString, QString> > Application::get_pd_list() const
256{
257 return pd_list_;
258}
259
dac1bb97
ML
260bool Application::notify(QObject *receiver, QEvent *event)
261{
262 try {
263 return QApplication::notify(receiver, event);
6f925ba9 264 } catch (exception& e) {
c1a688de
SA
265 qDebug().nospace() << "Caught exception of type " << \
266 typeid(e).name() << " (" << e.what() << ")";
267#ifdef ENABLE_STACKTRACE
268 throw e;
269#else
dac1bb97 270 exit(1);
c1a688de 271#endif
dac1bb97
ML
272 return false;
273 }
274}