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