2 * This file is part of the PulseView project.
4 * Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
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.
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.
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/>.
25 #include <QMessageBox>
28 #include <boost/version.hpp>
30 #ifdef ENABLE_STACKTRACE
31 #include <boost/stacktrace.hpp>
35 #include <libsigrokdecode/libsigrokdecode.h>
38 #include "application.hpp"
40 #include "globalsettings.hpp"
45 using std::shared_ptr;
48 static gint sort_pds(gconstpointer a, gconstpointer b)
50 const struct srd_decoder *sda, *sdb;
52 sda = (const struct srd_decoder *)a;
53 sdb = (const struct srd_decoder *)b;
54 return strcmp(sda->id, sdb->id);
58 Application::Application(int &argc, char* argv[]) :
59 QApplication(argc, argv)
61 setApplicationVersion(PV_VERSION_STRING);
62 setApplicationName("PulseView");
63 setOrganizationName("sigrok");
64 setOrganizationDomain("sigrok.org");
67 QStringList Application::get_languages()
69 QStringList files = QDir(":/l10n/").entryList(QStringList("*.qm"), QDir::Files);
72 result << "en"; // Add default language to the set
74 // Remove file extensions
75 for (QString file : files)
76 result << file.split(".").front();
78 result.sort(Qt::CaseInsensitive);
83 void Application::switch_language(const QString& language)
85 removeTranslator(&app_translator_);
86 removeTranslator(&qt_translator_);
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_);
94 qWarning() << "Translation resource" << resource << "not found";
97 resource = ":/l10n/qtbase_" + language +".qm";
98 if (qt_translator_.load(resource))
99 installTranslator(&qt_translator_);
101 qWarning() << "Translation resource" << resource << "not found";
104 if (!topLevelWidgets().empty()) {
105 // Force all windows to update
106 for (QWidget *widget : topLevelWidgets())
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);
119 void Application::on_setting_changed(const QString &key, const QVariant &value)
121 if (key == pv::GlobalSettings::Key_General_Language)
122 switch_language(value.toString());
125 void Application::collect_version_info(shared_ptr<sigrok::Context> context)
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);
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()));
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);
145 g_slist_free(l_orig);
147 char *host = sr_buildinfo_host_get();
148 version_info_.emplace_back(" - Host", QString(host));
151 char *scpi_backends = sr_buildinfo_scpi_backends_get();
152 version_info_.emplace_back(" - SCPI backends", QString(scpi_backends));
153 g_free(scpi_backends);
156 struct srd_decoder *dec;
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()));
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);
170 g_slist_free(l_orig);
172 host = srd_buildinfo_host_get();
173 version_info_.emplace_back(" - Host", QString(host));
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);
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);
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()));
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()));
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()));
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));
219 void Application::print_version_info()
221 cout << PV_TITLE << " " << PV_VERSION_STRING << endl;
223 cout << endl << "Libraries and features:" << endl;
224 for (pair<QString, QString>& entry : version_info_)
225 cout << " " << entry.first.toStdString() << " " << entry.second.toStdString() << endl;
227 cout << endl << "Firmware search paths:" << endl;
228 for (QString& entry : fw_path_list_)
229 cout << " " << entry.toStdString() << endl;
231 cout << endl << "Protocol decoder search paths:" << endl;
232 for (QString& entry : pd_path_list_)
233 cout << " " << entry.toStdString() << endl;
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;
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;
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;
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;
258 vector< pair<QString, QString> > Application::get_version_info() const
260 return version_info_;
263 vector<QString> Application::get_fw_path_list() const
265 return fw_path_list_;
268 vector<QString> Application::get_pd_path_list() const
270 return pd_path_list_;
273 vector< pair<QString, QString> > Application::get_driver_list() const
278 vector< pair<QString, QString> > Application::get_input_format_list() const
280 return input_format_list_;
283 vector< pair<QString, QString> > Application::get_output_format_list() const
285 return output_format_list_;
288 vector< pair<QString, QString> > Application::get_pd_list() const
293 bool Application::notify(QObject *receiver, QEvent *event)
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