]> sigrok.org Git - pulseview.git/blob - pv/application.cpp
8252f27fb0c1e87ffbf97a2cf24b269b8ecfa6b7
[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 const QStringList Application::get_languages() const
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 (const QString& file : files)
77                 result << file.split(".").front();
78
79         result.sort(Qt::CaseInsensitive);
80
81         return result;
82 }
83
84 const QString Application::get_language_editors(const QString& language) const
85 {
86         if (language == "de") return "Sören Apel, Uwe Hermann";
87         if (language == "es_mx") return "Carlos Diaz";
88
89         return QString();
90 }
91
92 void Application::switch_language(const QString& language)
93 {
94         removeTranslator(&app_translator_);
95         removeTranslator(&qt_translator_);
96         removeTranslator(&qtbase_translator_);
97
98         if ((language != "C") && (language != "en")) {
99                 // Application translations
100                 QString resource = ":/l10n/" + language +".qm";
101                 if (app_translator_.load(resource))
102                         installTranslator(&app_translator_);
103                 else
104                         qWarning() << "Translation resource" << resource << "not found";
105
106                 // Qt translations
107                 QString tr_path(QLibraryInfo::location(QLibraryInfo::TranslationsPath));
108
109                 if (qt_translator_.load("qt_" + language, tr_path))
110                         installTranslator(&qt_translator_);
111                 else
112                         qWarning() << "QT translations for" << language << "not found at" <<
113                                 tr_path << ", Qt translations package is probably missing";
114
115                 // Qt base translations
116                 if (qtbase_translator_.load("qtbase_" + language, tr_path))
117                         installTranslator(&qtbase_translator_);
118                 else
119                         qWarning() << "QT base translations for" << language << "not found at" <<
120                                 tr_path << ", Qt translations package is probably missing";
121         }
122
123         if (!topLevelWidgets().empty()) {
124                 // Force all windows to update
125                 for (QWidget *widget : topLevelWidgets())
126                         widget->update();
127
128                 QMessageBox msg(topLevelWidgets().front());
129                 msg.setText(tr("Some parts of the application may still " \
130                                 "use the previous language. Re-opening the affected windows or " \
131                                 "restarting the application will remedy this."));
132                 msg.setStandardButtons(QMessageBox::Ok);
133                 msg.setIcon(QMessageBox::Information);
134                 msg.exec();
135         }
136 }
137
138 void Application::on_setting_changed(const QString &key, const QVariant &value)
139 {
140         if (key == pv::GlobalSettings::Key_General_Language)
141                 switch_language(value.toString());
142 }
143
144 void Application::collect_version_info(shared_ptr<sigrok::Context> context)
145 {
146         // Library versions and features
147         version_info_.emplace_back(applicationName(), applicationVersion());
148         version_info_.emplace_back("Qt", qVersion());
149         version_info_.emplace_back("glibmm", PV_GLIBMM_VERSION);
150         version_info_.emplace_back("Boost", BOOST_LIB_VERSION);
151
152         version_info_.emplace_back("libsigrok", QString("%1/%2 (rt: %3/%4)")
153                 .arg(SR_PACKAGE_VERSION_STRING, SR_LIB_VERSION_STRING,
154                 sr_package_version_string_get(), sr_lib_version_string_get()));
155
156         GSList *l_orig = sr_buildinfo_libs_get();
157         for (GSList *l = l_orig; l; l = l->next) {
158                 GSList *m = (GSList *)l->data;
159                 const char *lib = (const char *)m->data;
160                 const char *version = (const char *)m->next->data;
161                 version_info_.emplace_back(QString(" - %1").arg(QString(lib)), QString(version));
162                 g_slist_free_full(m, g_free);
163         }
164         g_slist_free(l_orig);
165
166         char *host = sr_buildinfo_host_get();
167         version_info_.emplace_back(" - Host", QString(host));
168         g_free(host);
169
170         char *scpi_backends = sr_buildinfo_scpi_backends_get();
171         version_info_.emplace_back(" - SCPI backends", QString(scpi_backends));
172         g_free(scpi_backends);
173
174 #ifdef ENABLE_DECODE
175         struct srd_decoder *dec;
176
177         version_info_.emplace_back("libsigrokdecode", QString("%1/%2 (rt: %3/%4)")
178                 .arg(SRD_PACKAGE_VERSION_STRING, SRD_LIB_VERSION_STRING,
179                 srd_package_version_string_get(), srd_lib_version_string_get()));
180
181         l_orig = srd_buildinfo_libs_get();
182         for (GSList *l = l_orig; l; l = l->next) {
183                 GSList *m = (GSList *)l->data;
184                 const char *lib = (const char *)m->data;
185                 const char *version = (const char *)m->next->data;
186                 version_info_.emplace_back(QString(" - %1").arg(QString(lib)), QString(version));
187                 g_slist_free_full(m, g_free);
188         }
189         g_slist_free(l_orig);
190
191         host = srd_buildinfo_host_get();
192         version_info_.emplace_back(" - Host", QString(host));
193         g_free(host);
194 #endif
195
196         // Firmware paths
197         l_orig = sr_resourcepaths_get(SR_RESOURCE_FIRMWARE);
198         for (GSList *l = l_orig; l; l = l->next)
199                 fw_path_list_.emplace_back((char*)l->data);
200         g_slist_free_full(l_orig, g_free);
201
202         // PD paths
203 #ifdef ENABLE_DECODE
204         l_orig = srd_searchpaths_get();
205         for (GSList *l = l_orig; l; l = l->next)
206                 pd_path_list_.emplace_back((char*)l->data);
207         g_slist_free_full(l_orig, g_free);
208 #endif
209
210         // Device drivers
211         for (auto& entry : context->drivers())
212                 driver_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
213                         QString::fromUtf8(entry.second->long_name().c_str()));
214
215         // Input formats
216         for (auto& entry : context->input_formats())
217                 input_format_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
218                         QString::fromUtf8(entry.second->description().c_str()));
219
220         // Output formats
221         for (auto& entry : context->output_formats())
222                 output_format_list_.emplace_back(QString::fromUtf8(entry.first.c_str()),
223                         QString::fromUtf8(entry.second->description().c_str()));
224
225         // Protocol decoders
226 #ifdef ENABLE_DECODE
227         GSList *sl = g_slist_copy((GSList *)srd_decoder_list());
228         sl = g_slist_sort(sl, sort_pds);
229         for (const GSList *l = sl; l; l = l->next) {
230                 dec = (struct srd_decoder *)l->data;
231                 pd_list_.emplace_back(QString::fromUtf8(dec->id),
232                         QString::fromUtf8(dec->longname));
233         }
234         g_slist_free(sl);
235 #endif
236 }
237
238 void Application::print_version_info()
239 {
240         cout << PV_TITLE << " " << PV_VERSION_STRING << endl;
241
242         cout << endl << "Libraries and features:" << endl;
243         for (pair<QString, QString>& entry : version_info_)
244                 cout << "  " << entry.first.toStdString() << " " << entry.second.toStdString() << endl;
245
246         cout << endl << "Firmware search paths:" << endl;
247         for (QString& entry : fw_path_list_)
248                 cout << "  " << entry.toStdString() << endl;
249
250         cout << endl << "Protocol decoder search paths:" << endl;
251         for (QString& entry : pd_path_list_)
252                 cout << "  " << entry.toStdString() << endl;
253
254         cout << endl << "Supported hardware drivers:" << endl;
255         for (pair<QString, QString>& entry : driver_list_)
256                 cout << "  " << entry.first.leftJustified(21, ' ').toStdString() <<
257                 entry.second.toStdString() << endl;
258
259         cout << endl << "Supported input formats:" << endl;
260         for (pair<QString, QString>& entry : input_format_list_)
261                 cout << "  " << entry.first.leftJustified(21, ' ').toStdString() <<
262                 entry.second.toStdString() << endl;
263
264         cout << endl << "Supported output formats:" << endl;
265         for (pair<QString, QString>& entry : output_format_list_)
266                 cout << "  " << entry.first.leftJustified(21, ' ').toStdString() <<
267                 entry.second.toStdString() << endl;
268
269 #ifdef ENABLE_DECODE
270         cout << endl << "Supported protocol decoders:" << endl;
271         for (pair<QString, QString>& entry : pd_list_)
272                 cout << "  " << entry.first.leftJustified(21, ' ').toStdString() <<
273                 entry.second.toStdString() << endl;
274 #endif
275 }
276
277 vector< pair<QString, QString> > Application::get_version_info() const
278 {
279         return version_info_;
280 }
281
282 vector<QString> Application::get_fw_path_list() const
283 {
284         return fw_path_list_;
285 }
286
287 vector<QString> Application::get_pd_path_list() const
288 {
289         return pd_path_list_;
290 }
291
292 vector< pair<QString, QString> > Application::get_driver_list() const
293 {
294         return driver_list_;
295 }
296
297 vector< pair<QString, QString> > Application::get_input_format_list() const
298 {
299         return input_format_list_;
300 }
301
302 vector< pair<QString, QString> > Application::get_output_format_list() const
303 {
304         return output_format_list_;
305 }
306
307 vector< pair<QString, QString> > Application::get_pd_list() const
308 {
309         return pd_list_;
310 }
311
312 bool Application::notify(QObject *receiver, QEvent *event)
313 {
314         try {
315                 return QApplication::notify(receiver, event);
316         } catch (exception& e) {
317                 qDebug().nospace() << "Caught exception of type " << \
318                         typeid(e).name() << " (" << e.what() << ")";
319 #ifdef ENABLE_STACKTRACE
320                 throw e;
321 #else
322                 exit(1);
323 #endif
324                 return false;
325         }
326 }