File indexing completed on 2024-04-28 04:37:18

0001 /*
0002     SPDX-FileCopyrightText: 2009 Andreas Pakulat <apaku@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "launchconfiguration.h"
0008 
0009 
0010 #include <interfaces/iproject.h>
0011 #include <interfaces/launchconfigurationtype.h>
0012 
0013 #include "core.h"
0014 #include "runcontroller.h"
0015 #include <interfaces/ilauncher.h>
0016 
0017 namespace KDevelop
0018 {
0019 
0020 class LaunchConfigurationPrivate
0021 {
0022 public:
0023     LaunchConfigurationPrivate(const KConfigGroup& grp, IProject* project)
0024         : baseGroup(grp)
0025         , project(project)
0026     {}
0027 
0028     KConfigGroup baseGroup;
0029     IProject* const project;
0030     LaunchConfigurationType* type;
0031 };
0032 
0033 QString LaunchConfiguration::LaunchConfigurationNameEntry()
0034 {
0035     return QStringLiteral("Name");
0036 }
0037 
0038 QString LaunchConfiguration::LaunchConfigurationTypeEntry()
0039 {
0040     return QStringLiteral("Type");
0041 }
0042 
0043 LaunchConfiguration::LaunchConfiguration(const KConfigGroup& grp, IProject* project, QObject* parent )
0044     : QObject(parent)
0045     , ILaunchConfiguration()
0046     , d_ptr(new LaunchConfigurationPrivate(grp, project))
0047 {
0048     Q_D(LaunchConfiguration);
0049 
0050     d->type = Core::self()->runControllerInternal()->launchConfigurationTypeForId(grp.readEntry(LaunchConfigurationTypeEntry(), QString()));
0051 }
0052 
0053 LaunchConfiguration::~LaunchConfiguration()
0054 {
0055 }
0056 
0057 KConfigGroup LaunchConfiguration::config()
0058 {
0059     Q_D(LaunchConfiguration);
0060 
0061     return d->baseGroup.group("Data");
0062 }
0063 
0064 const KConfigGroup LaunchConfiguration::config() const
0065 {
0066     Q_D(const LaunchConfiguration);
0067 
0068     return d->baseGroup.group("Data");
0069 }
0070 
0071 QString LaunchConfiguration::name() const
0072 {
0073     Q_D(const LaunchConfiguration);
0074 
0075     return d->baseGroup.readEntry(LaunchConfigurationNameEntry(), QString());
0076 }
0077 
0078 IProject* LaunchConfiguration::project() const
0079 {
0080     Q_D(const LaunchConfiguration);
0081 
0082     return d->project;
0083 }
0084 
0085 LaunchConfigurationType* LaunchConfiguration::type() const
0086 {
0087     Q_D(const LaunchConfiguration);
0088 
0089     return d->type;
0090 }
0091 
0092 void LaunchConfiguration::setName(const QString& name)
0093 {
0094     Q_D(LaunchConfiguration);
0095 
0096     d->baseGroup.writeEntry(LaunchConfigurationNameEntry(), name);
0097     d->baseGroup.sync();
0098     emit nameChanged( this );
0099 }
0100 
0101 void LaunchConfiguration::setType(const QString& typeId)
0102 {
0103     Q_D(LaunchConfiguration);
0104 
0105     LaunchConfigurationType* t = Core::self()->runControllerInternal()->launchConfigurationTypeForId( typeId );
0106     // If this ever happens something seriously screwed in the launch config dialog, as that is 
0107     // the only place from where this method should be called
0108     Q_ASSERT(t);
0109     if( t )
0110     {
0111         d->baseGroup.deleteGroup("Data");
0112         d->type = t;
0113         d->baseGroup.writeEntry(LaunchConfigurationTypeEntry(), d->type->id());
0114         d->baseGroup.sync();
0115         emit typeChanged( t );
0116     }
0117 }
0118 
0119 void LaunchConfiguration::save()
0120 {
0121     Q_D(LaunchConfiguration);
0122 
0123     d->baseGroup.sync();
0124 }
0125 
0126 QString LaunchConfiguration::configGroupName() const
0127 {
0128     Q_D(const LaunchConfiguration);
0129 
0130     return d->baseGroup.name();
0131 }
0132 
0133 QString LaunchConfiguration::launcherForMode(const QString& mode) const
0134 {
0135     Q_D(const LaunchConfiguration);
0136 
0137     QStringList modes = d->baseGroup.readEntry("Configured Launch Modes", QStringList());
0138     int idx = modes.indexOf( mode );
0139     if( idx != -1 )
0140     {
0141         const QStringList launcherIds = d->baseGroup.readEntry("Configured Launchers", QStringList());
0142         if (launcherIds.count() > idx ) {
0143             const auto& id = launcherIds.at(idx);
0144             const auto typeLaunchers = type()->launchers();
0145             for (ILauncher* l : typeLaunchers) {
0146                 if (l->id() == id) {
0147                     return id;
0148                 }
0149             }
0150         }
0151     }
0152 
0153     // No launcher configured, if it's debug mode, prefer GDB if available.
0154     if( mode == QLatin1String("debug") )
0155     {
0156         const auto launchers = type()->launchers();
0157         for (ILauncher* l : launchers) {
0158             if( l->supportedModes().contains( mode ) && l->id() == QLatin1String("gdb") )
0159             {
0160                 return l->id();
0161             }
0162         }
0163     }
0164     // Otherwise, lets just try with the first one in the list and hope it works
0165     const auto launchers = type()->launchers();
0166     for (ILauncher* l : launchers) {
0167         if( l->supportedModes().contains( mode ) )
0168         {
0169             return l->id();
0170         }
0171     }
0172 
0173     return QString();
0174 }
0175 
0176 void LaunchConfiguration::setLauncherForMode(const QString& mode, const QString& id)
0177 {
0178     Q_D(LaunchConfiguration);
0179 
0180     QStringList modes = d->baseGroup.readEntry("Configured Launch Modes", QStringList());
0181     int idx = modes.indexOf( mode );
0182     if( idx == -1 )
0183     {
0184         idx = modes.count();
0185         modes << mode;
0186         d->baseGroup.writeEntry("Configured Launch Modes", modes);
0187     }
0188     QStringList launchers = d->baseGroup.readEntry("Configured Launchers", QStringList());
0189     if( launchers.count() > idx )
0190     {
0191         launchers.replace(idx, id);
0192     } else
0193     {
0194         launchers.append( id );
0195     }
0196     d->baseGroup.writeEntry("Configured Launchers", launchers);
0197 }
0198 
0199 
0200 }
0201 
0202 #include "moc_launchconfiguration.cpp"