Commit d01702df authored by Kevin Funk's avatar Kevin Funk
Browse files

app: Improve --project handling

Before this patch `--project PROJ` had several flaws:
- Could not be used to open the project in an existing instance
- Could not be used to open a project if there was an already
  running KDevelop instance

With this change:
- We'll check which sessions contain PROJ
  - We'll use the first match as the 'target session' to be opened
- Continue as usual
  - If there's a KDevelop instance with that session open: activate
  - If not: Open a new instance with said session

Reviewers: dfaure, brauch

Reviewed By: brauch

Subscribers: brauch, cullmann, kdevelop-devel

Differential Revision:
parent 12b1096a
......@@ -192,6 +192,21 @@ static const KDevelop::SessionInfo* findSessionInList( const SessionInfos& sessi
return nullptr;
/// Tries to find sessions containing project @p projectUrl in @p sessions.
static const KDevelop::SessionInfos findSessionsWithProject(const SessionInfos& sessions, const QUrl& projectUrl)
if (!projectUrl.isValid())
return {};
KDevelop::SessionInfos infos;
for (auto it = sessions.constBegin(); it != sessions.constEnd(); ++it) {
if (it->projects.contains(projectUrl)) {
infos << *it;
return infos;
/// Performs a DBus call to open the given @p files in the running kdev instance identified by @p pid
/// Returns the exit status
static int openFilesInRunningInstance(const QVector<UrlInfo>& files, qint64 pid)
......@@ -452,7 +467,7 @@ int main( int argc, char *argv[] )
parser.addOption(QCommandLineOption{QStringList{"pss", "pick-session-shell"}, i18n("List all available sessions on shell and lets you select one to open.")});
parser.addOption(QCommandLineOption{QStringList{"l", "list-sessions"}, i18n("List available sessions and quit.")});
parser.addOption(QCommandLineOption{QStringList{"f", "fetch"}, i18n("Open KDevelop and fetch the given project."), QStringLiteral("fetch")});
parser.addOption(QCommandLineOption{QStringList{"p", "project"}, i18n("Open KDevelop and load the given project."), QStringLiteral("project")});
parser.addOption(QCommandLineOption{QStringList{"p", "project"}, i18n("Open KDevelop and load the given project. Project can be either a .kdev4 file or a directory path."), QStringLiteral("project")});
parser.addOption(QCommandLineOption{QStringList{"d", "debug"},
i18n("Start debugging an application in KDevelop with the given debugger.\n"
"The executable that should be debugged must follow - including arguments.\n"
......@@ -674,8 +689,38 @@ int main( int argc, char *argv[] )
if (parser.isSet("project")) {
const auto project = parser.value(QStringLiteral("project"));
QFileInfo info(project);
QUrl projectUrl;
if (info.suffix() == QLatin1String("kdev4")) {
projectUrl = QUrl::fromLocalFile(info.absoluteFilePath());
} else if (info.isDir()) {
QDir dir(info.absoluteFilePath());
const auto potentialProjectFiles = dir.entryList({QStringLiteral("*.kdev4")}, QDir::Files, QDir::Name);
qDebug(APP) << "Found these potential project files:" << potentialProjectFiles;
if (!potentialProjectFiles.isEmpty()) {
projectUrl = QUrl::fromLocalFile(dir.absoluteFilePath(potentialProjectFiles.value(0)));
} else {
QTextStream qerr(stderr);
qerr << "Invalid project: " << project << " - should be either a path to a .kdev4 file or a directory containing a .kdev4 file";
return 1;
qDebug(APP) << "Attempting to find a suitable session for project" << projectUrl;
const auto sessionInfos = findSessionsWithProject(availableSessionInfos, projectUrl);
qDebug(APP) << "Found matching sessions:" << sessionInfos.size();
if (!sessionInfos.isEmpty()) {
// TODO: If there's more than one match: Allow the user to select which session to open?
qDebug(APP) << "Attempting to open session:" <<;
session =;
qDebug(APP) << "Attempting to initialize session:" << session;
if(!Core::initialize(Core::Default, session))
return 5;
......@@ -687,26 +732,6 @@ int main( int argc, char *argv[] )
QStringList projectNames = parser.values(QStringLiteral("project"));
foreach(const QString& projectName, projectNames)
QFileInfo info( projectName );
if( info.suffix() == QLatin1String("kdev4") ) {
// make sure the project is not already opened by the session controller
bool shouldOpen = true;
Path path(info.absoluteFilePath());
foreach(KDevelop::IProject* project, core->projectController()->projects()) {
if (project->projectFile() == path) {
shouldOpen = false;
if (shouldOpen) {
core->projectController()->openProject( path.toUrl() );
const auto fetchUrlStrings = parser.values(QStringLiteral("fetch"));
for (const auto& fetchUrlString : fetchUrlStrings) {
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment