Warning, file /utilities/kate/addons/gdbplugin/advanced_settings.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Description: Advanced settings dialog for gdb
0002 //
0003 //
0004 // SPDX-FileCopyrightText: 2012 Kåre Särs <kare.sars@iki.fi>
0005 //
0006 //  SPDX-License-Identifier: LGPL-2.0-only
0007 
0008 #include "advanced_settings.h"
0009 
0010 #ifdef WIN32
0011 static const QLatin1Char pathSeparator(';');
0012 #else
0013 static const QLatin1Char pathSeparator(':');
0014 #endif
0015 
0016 #include <QFileDialog>
0017 #include <QJsonArray>
0018 #include <QJsonObject>
0019 
0020 const QString AdvancedGDBSettings::F_GDB = QStringLiteral("gdb");
0021 const QString AdvancedGDBSettings::F_SRC_PATHS = QStringLiteral("srcPaths");
0022 const static QString F_LOCAL_REMOTE = QStringLiteral("localRemote");
0023 const static QString F_REMOTE_BAUD = QStringLiteral("remoteBaud");
0024 const static QString F_SO_ABSOLUTE = QStringLiteral("soAbsolute");
0025 const static QString F_SO_RELATIVE = QStringLiteral("soRelative");
0026 const static QString F_CUSTOM_INIT = QStringLiteral("customInit");
0027 
0028 AdvancedGDBSettings::AdvancedGDBSettings(QWidget *parent)
0029     : QDialog(parent)
0030 {
0031     setupUi(this);
0032     u_gdbBrowse->setIcon(QIcon::fromTheme(QStringLiteral("application-x-ms-dos-executable")));
0033     connect(u_gdbBrowse, &QToolButton::clicked, this, &AdvancedGDBSettings::slotBrowseGDB);
0034 
0035     u_setSoPrefix->setIcon(QIcon::fromTheme(QStringLiteral("folder")));
0036     connect(u_setSoPrefix, &QToolButton::clicked, this, &AdvancedGDBSettings::slotSetSoPrefix);
0037 
0038     u_addSoSearchPath->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0039     u_delSoSearchPath->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
0040     connect(u_addSoSearchPath, &QToolButton::clicked, this, &AdvancedGDBSettings::slotAddSoPath);
0041     connect(u_delSoSearchPath, &QToolButton::clicked, this, &AdvancedGDBSettings::slotDelSoPath);
0042 
0043     u_addSrcPath->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0044     u_delSrcPath->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
0045     connect(u_addSrcPath, &QToolButton::clicked, this, &AdvancedGDBSettings::slotAddSrcPath);
0046     connect(u_delSrcPath, &QToolButton::clicked, this, &AdvancedGDBSettings::slotDelSrcPath);
0047 
0048     connect(u_buttonBox, &QDialogButtonBox::accepted, this, &AdvancedGDBSettings::accept);
0049     connect(u_buttonBox, &QDialogButtonBox::rejected, this, &AdvancedGDBSettings::reject);
0050 
0051     connect(u_localRemote, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &AdvancedGDBSettings::slotLocalRemoteChanged);
0052 }
0053 
0054 AdvancedGDBSettings::~AdvancedGDBSettings()
0055 {
0056 }
0057 
0058 const QJsonObject AdvancedGDBSettings::configs() const
0059 {
0060     QJsonObject conf;
0061 
0062     // gdb
0063     conf[F_GDB] = u_gdbCmd->text();
0064 
0065     // local/remote, baud
0066     switch (u_localRemote->currentIndex()) {
0067     case 1:
0068         conf[F_LOCAL_REMOTE] = QStringLiteral("-target-select remote %1:%2").arg(u_tcpHost->text(), u_tcpPort->text());
0069         break;
0070     case 2:
0071         conf[F_LOCAL_REMOTE] = QStringLiteral("-target-select remote %1").arg(u_ttyPort->text());
0072         conf[F_REMOTE_BAUD] = QStringLiteral("-gdb-set remotebaud %1").arg(u_baudCombo->currentText());
0073         break;
0074     default:
0075         break;
0076     }
0077 
0078     // solib absolute
0079     if (!u_soAbsPrefix->text().isEmpty()) {
0080         conf[F_SO_ABSOLUTE] = QStringLiteral("-gdb-set solib-absolute-prefix %1").arg(u_soAbsPrefix->text());
0081     }
0082 
0083     // solib search path
0084     if (u_soSearchPaths->count() > 0) {
0085         QString paths = QStringLiteral("-gdb-set solib-search-path ");
0086         for (int i = 0; i < u_soSearchPaths->count(); ++i) {
0087             if (i != 0) {
0088                 paths += pathSeparator;
0089             }
0090             paths += u_soSearchPaths->item(i)->text();
0091         }
0092         conf[F_SO_RELATIVE] = paths;
0093     }
0094 
0095     // source paths
0096     if (u_srcPaths->count() > 0) {
0097         QJsonArray paths;
0098         for (int i = 0; i < u_srcPaths->count(); ++i) {
0099             const QString path = u_srcPaths->item(i)->text().trimmed();
0100             if (!path.isEmpty()) {
0101                 paths << path;
0102             }
0103         }
0104         if (!paths.isEmpty()) {
0105             conf[F_SRC_PATHS] = paths;
0106         }
0107     }
0108 
0109     // custom init
0110     const auto cinit = u_customInit->toPlainText().split(QLatin1Char('\n'), Qt::SkipEmptyParts);
0111     if (!cinit.isEmpty()) {
0112         conf[F_CUSTOM_INIT] = QJsonArray::fromStringList(cinit);
0113     }
0114 
0115     return conf;
0116 }
0117 
0118 QStringList AdvancedGDBSettings::commandList(const QJsonObject &config)
0119 {
0120     QStringList commands;
0121 
0122     auto insertString = [&commands, config](const QString &field) {
0123         const QString value = config[field].toString().trimmed();
0124         if (!value.isEmpty()) {
0125             commands << value;
0126         }
0127     };
0128 
0129     insertString(F_LOCAL_REMOTE);
0130     insertString(F_REMOTE_BAUD);
0131     insertString(F_SO_ABSOLUTE);
0132     insertString(F_SO_RELATIVE);
0133 
0134     for (const auto &value : config[F_CUSTOM_INIT].toArray()) {
0135         commands << value.toString();
0136     }
0137 
0138     return commands;
0139 }
0140 
0141 QJsonObject AdvancedGDBSettings::upgradeConfigV4_5(const QStringList &cfgs)
0142 {
0143     const int size = cfgs.count();
0144 
0145     QJsonObject conf;
0146 
0147     auto insert = [&conf, cfgs, size](CustomStringOrder index, const QString &field) {
0148         if (index >= size)
0149             return;
0150 
0151         const QString value = cfgs[index].trimmed();
0152         if (!value.isEmpty()) {
0153             conf[field] = value;
0154         }
0155     };
0156 
0157     // gdb
0158     insert(GDBIndex, F_GDB);
0159     // localremoteindex
0160     insert(LocalRemoteIndex, F_LOCAL_REMOTE);
0161     // remotebaudindex
0162     insert(RemoteBaudIndex, F_REMOTE_BAUD);
0163     // soabsoluteindex
0164     insert(SoAbsoluteIndex, F_SO_ABSOLUTE);
0165     // sorelativeindex
0166     insert(SoRelativeIndex, F_SO_RELATIVE);
0167     // srcpathsindex
0168     if (SrcPathsIndex < size) {
0169         QString allPaths = cfgs[SrcPathsIndex];
0170         if (allPaths.startsWith(QStringLiteral("set directories "))) {
0171             allPaths = allPaths.mid(16);
0172         }
0173         QStringList paths = allPaths.split(pathSeparator, Qt::SkipEmptyParts);
0174         if (!paths.isEmpty()) {
0175             conf[F_SRC_PATHS] = QJsonArray::fromStringList(paths);
0176         }
0177     }
0178     // customstart
0179     if (CustomStartIndex < size) {
0180         QJsonArray parts;
0181         for (int i = CustomStartIndex; i < size; i++) {
0182             parts << cfgs[i];
0183         }
0184         conf[F_CUSTOM_INIT] = parts;
0185     }
0186 
0187     return conf;
0188 }
0189 
0190 void AdvancedGDBSettings::setConfigs(const QJsonObject &cfgs)
0191 {
0192     // clear all info
0193     u_gdbCmd->setText(QStringLiteral("gdb"));
0194     u_localRemote->setCurrentIndex(0);
0195     u_soAbsPrefix->clear();
0196     u_soSearchPaths->clear();
0197     u_srcPaths->clear();
0198     u_customInit->clear();
0199     u_tcpHost->setText(QString());
0200     u_tcpPort->setText(QString());
0201     u_ttyPort->setText(QString());
0202     u_baudCombo->setCurrentIndex(0);
0203 
0204     // GDB
0205     if (cfgs.contains(F_GDB)) {
0206         u_gdbCmd->setText(cfgs[F_GDB].toString());
0207     }
0208 
0209     // Local / Remote
0210     const auto localRemote = cfgs[F_LOCAL_REMOTE].toString();
0211 
0212     int start;
0213     int end;
0214     if (localRemote.isEmpty()) {
0215         u_localRemote->setCurrentIndex(0);
0216         u_remoteStack->setCurrentIndex(0);
0217     } else if (localRemote.contains(pathSeparator)) {
0218         u_localRemote->setCurrentIndex(1);
0219         u_remoteStack->setCurrentIndex(1);
0220         start = localRemote.lastIndexOf(QLatin1Char(' '));
0221         end = localRemote.indexOf(pathSeparator);
0222         u_tcpHost->setText(localRemote.mid(start + 1, end - start - 1));
0223         u_tcpPort->setText(localRemote.mid(end + 1));
0224     } else {
0225         u_localRemote->setCurrentIndex(2);
0226         u_remoteStack->setCurrentIndex(2);
0227         start = localRemote.lastIndexOf(QLatin1Char(' '));
0228         u_ttyPort->setText(localRemote.mid(start + 1));
0229 
0230         const auto remoteBaud = cfgs[F_REMOTE_BAUD].toString();
0231         start = remoteBaud.lastIndexOf(QLatin1Char(' '));
0232         setComboText(u_baudCombo, remoteBaud.mid(start + 1));
0233     }
0234 
0235     // Solib absolute path
0236     if (cfgs.contains(F_SO_ABSOLUTE)) {
0237         start = 26; // "set solib-absolute-prefix "
0238         u_soAbsPrefix->setText(cfgs[F_SO_ABSOLUTE].toString().mid(start));
0239     }
0240 
0241     // Solib search path
0242     if (cfgs.contains(F_SO_RELATIVE)) {
0243         start = 22; // "set solib-search-path "
0244         QString tmp = cfgs[F_SO_RELATIVE].toString().mid(start);
0245         u_soSearchPaths->addItems(tmp.split(pathSeparator));
0246     }
0247 
0248     if (cfgs.contains(F_SRC_PATHS)) {
0249         QStringList paths;
0250         for (const auto &value : cfgs[F_SRC_PATHS].toArray()) {
0251             paths << value.toString();
0252         }
0253         u_srcPaths->addItems(paths);
0254     }
0255 
0256     // Custom init
0257     if (cfgs.contains(F_CUSTOM_INIT)) {
0258         for (const auto &line : cfgs[F_CUSTOM_INIT].toArray()) {
0259             u_customInit->appendPlainText(line.toString());
0260         }
0261     }
0262 
0263     slotLocalRemoteChanged();
0264 }
0265 
0266 void AdvancedGDBSettings::slotBrowseGDB()
0267 {
0268     u_gdbCmd->setText(QFileDialog::getOpenFileName(this, QString(), u_gdbCmd->text(), QStringLiteral("application/x-executable")));
0269     if (u_gdbCmd->text().isEmpty()) {
0270         u_gdbCmd->setText(QStringLiteral("gdb"));
0271     }
0272 }
0273 
0274 void AdvancedGDBSettings::setComboText(QComboBox *combo, const QString &str)
0275 {
0276     if (!combo) {
0277         return;
0278     }
0279 
0280     for (int i = 0; i < combo->count(); i++) {
0281         if (combo->itemText(i) == str) {
0282             combo->setCurrentIndex(i);
0283             return;
0284         }
0285     }
0286     // The string was not found -> add it
0287     combo->addItem(str);
0288     combo->setCurrentIndex(combo->count() - 1);
0289 }
0290 
0291 void AdvancedGDBSettings::slotSetSoPrefix()
0292 {
0293     QString prefix = QFileDialog::getExistingDirectory(this);
0294     if (prefix.isEmpty()) {
0295         return;
0296     }
0297 
0298     u_soAbsPrefix->setText(prefix);
0299 }
0300 
0301 void AdvancedGDBSettings::slotAddSoPath()
0302 {
0303     QString path = QFileDialog::getExistingDirectory(this);
0304     if (path.isEmpty()) {
0305         return;
0306     }
0307 
0308     u_soSearchPaths->addItem(path);
0309 }
0310 
0311 void AdvancedGDBSettings::slotDelSoPath()
0312 {
0313     QListWidgetItem *item = u_soSearchPaths->takeItem(u_soSearchPaths->currentRow());
0314     delete item;
0315 }
0316 
0317 void AdvancedGDBSettings::slotAddSrcPath()
0318 {
0319     QString path = QFileDialog::getExistingDirectory(this);
0320     if (path.isEmpty()) {
0321         return;
0322     }
0323 
0324     u_srcPaths->addItem(path);
0325 }
0326 
0327 void AdvancedGDBSettings::slotDelSrcPath()
0328 {
0329     QListWidgetItem *item = u_srcPaths->takeItem(u_srcPaths->currentRow());
0330     delete item;
0331 }
0332 
0333 void AdvancedGDBSettings::slotLocalRemoteChanged()
0334 {
0335     u_soAbsPrefixLabel->setEnabled(u_localRemote->currentIndex() != 0);
0336     u_soSearchLabel->setEnabled(u_localRemote->currentIndex() != 0);
0337     u_soAbsPrefix->setEnabled(u_localRemote->currentIndex() != 0);
0338     u_soSearchPaths->setEnabled(u_localRemote->currentIndex() != 0);
0339     u_setSoPrefix->setEnabled(u_localRemote->currentIndex() != 0);
0340     u_addDelSoPaths->setEnabled(u_localRemote->currentIndex() != 0);
0341 }
0342 
0343 #include "moc_advanced_settings.cpp"