File: session.cpp

package info (click to toggle)
kdevelop 4%3A22.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,096 kB
  • sloc: cpp: 284,635; javascript: 3,558; python: 3,422; sh: 1,319; ansic: 685; xml: 331; php: 95; lisp: 66; makefile: 39; sed: 12
file content (237 lines) | stat: -rw-r--r-- 5,633 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
    SPDX-FileCopyrightText: 2008 Andreas Pakulat <apaku@gmx.de>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "session.h"

#include <QDir>
#include <QUrl>

#include <KConfigGroup>

#include <interfaces/iplugincontroller.h>
#include <interfaces/iplugin.h>
#include "core.h"
#include "sessioncontroller.h"
#include <interfaces/iprojectcontroller.h>
#include <interfaces/iproject.h>
#include <serialization/itemrepository.h>

namespace KDevelop
{

const QString Session::cfgSessionNameEntry = QStringLiteral("SessionName");
const QString Session::cfgSessionDescriptionEntry = QStringLiteral("SessionPrettyContents");
const QString Session::cfgSessionProjectsEntry = QStringLiteral("Open Projects");
const QString Session::cfgSessionOptionsGroup = QStringLiteral("General Options");

class SessionPrivate
{
public:
    SessionInfo info;
    Session* const q;
    bool isTemporary;

    QUrl pluginArea( const IPlugin* plugin )
    {
        QString name = Core::self()->pluginController()->pluginInfo(plugin).pluginId();
        QUrl url = QUrl::fromLocalFile(info.path + QLatin1Char('/') + name );
        if( !QFile::exists( url.toLocalFile() ) ) {
            QDir( info.path ).mkdir( name );
        }
        return url;
    }

    SessionPrivate( Session* session, const QString& id )
        : info( Session::parse( id, true ) )
        , q( session )
        , isTemporary( false )
    {
    }

    void updateDescription()
    {
        buildDescription( info );
        emit q->sessionUpdated( q );
    }

    static QString generatePrettyContents( const SessionInfo& info );
    static QString generateDescription( const SessionInfo& info );
    static void buildDescription( SessionInfo& info );
};

Session::Session( const QString& id, QObject* parent )
        : ISession(parent)
        , d_ptr(new SessionPrivate(this, id))
{
}

Session::~Session() = default;

QString Session::name() const
{
    Q_D(const Session);

    return d->info.name;
}

QList<QUrl> Session::containedProjects() const
{
    Q_D(const Session);

    return d->info.projects;
}

QString Session::description() const
{
    Q_D(const Session);

    return d->info.description;
}

QUrl Session::pluginDataArea( const IPlugin* p )
{
    Q_D(Session);

    return d->pluginArea( p );
}

KSharedConfigPtr Session::config()
{
    Q_D(Session);

    return d->info.config;
}

QUuid Session::id() const
{
    Q_D(const Session);

    return d->info.uuid;
}

void Session::setName( const QString& newname )
{
    Q_D(Session);

    d->info.name = newname;
    d->info.config->group( QString() ).writeEntry( cfgSessionNameEntry, newname );
    d->updateDescription();
}

void Session::setContainedProjects( const QList<QUrl>& projects )
{
    Q_D(Session);

    d->info.projects = projects;
    d->info.config->group( cfgSessionOptionsGroup ).writeEntry( cfgSessionProjectsEntry, projects );
    d->updateDescription();
}

void Session::setTemporary(bool temp)
{
    Q_D(Session);

    d->isTemporary = temp;
}

bool Session::isTemporary() const
{
    Q_D(const Session);

    return d->isTemporary;
}

QString Session::path() const
{
    Q_D(const Session);

    return d->info.path;
}

QString SessionPrivate::generatePrettyContents( const SessionInfo& info )
{
    if( info.projects.isEmpty() )
        return QString();

    QStringList projectNames;
    projectNames.reserve( info.projects.size() );

    for (const QUrl& url : info.projects) {
        IProject* project = nullptr;
        if( ICore::self() && ICore::self()->projectController() ) {
            project = ICore::self()->projectController()->findProjectForUrl( url );
        }

        if( project ) {
            projectNames << project->name();
        } else {
            QString projectName = url.fileName();
            projectName.remove(QRegExp(QStringLiteral("\\.kdev4$"), Qt::CaseInsensitive));
            projectNames << projectName;
        }
    }

    if( projectNames.isEmpty() ) {
        return i18n("(no projects)");
    } else {
        return projectNames.join(QLatin1String(", "));
    }
}

QString SessionPrivate::generateDescription( const SessionInfo& info )
{
    QString prettyContentsFormatted = generatePrettyContents( info );
    QString description;

    if( info.name.isEmpty() ) {
        description = prettyContentsFormatted;
    } else {
        description = info.name + QLatin1String(":  ") + prettyContentsFormatted;
    }

    return description;
}

void SessionPrivate::buildDescription( SessionInfo& info )
{
    QString description = generateDescription( info );

    info.description = description;
    info.config->group( QString() ).writeEntry( Session::cfgSessionDescriptionEntry, description );
    info.config->sync();
}

SessionInfo Session::parse( const QString& id, bool mkdir )
{
    SessionInfo ret;
    QString sessionPath = SessionController::sessionDirectory(id);

    QDir sessionDir( sessionPath );
    if( !sessionDir.exists() ) {
        if( mkdir ) {
            sessionDir.mkpath(sessionPath);
            Q_ASSERT( sessionDir.exists() );
        } else {
            return ret;
        }
    }

    ret.uuid = id;
    ret.path = sessionPath;
    ret.config = KSharedConfig::openConfig(sessionPath + QLatin1String("/sessionrc"));

    KConfigGroup cfgRootGroup = ret.config->group( QString() );
    KConfigGroup cfgOptionsGroup = ret.config->group( cfgSessionOptionsGroup );

    ret.name = cfgRootGroup.readEntry( cfgSessionNameEntry, QString() );
    ret.projects = cfgOptionsGroup.readEntry( cfgSessionProjectsEntry, QList<QUrl>() );
    SessionPrivate::buildDescription( ret );

    return ret;
}

}