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

0001 /*
0002     SPDX-FileCopyrightText: 2008 Andreas Pakulat <apaku@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "session.h"
0008 
0009 #include <QDir>
0010 #include <QUrl>
0011 
0012 #include <KConfigGroup>
0013 
0014 #include <interfaces/iplugincontroller.h>
0015 #include <interfaces/iplugin.h>
0016 #include "core.h"
0017 #include "sessioncontroller.h"
0018 #include <interfaces/iprojectcontroller.h>
0019 #include <interfaces/iproject.h>
0020 #include <serialization/itemrepository.h>
0021 
0022 namespace KDevelop
0023 {
0024 
0025 const QString Session::cfgSessionNameEntry = QStringLiteral("SessionName");
0026 const QString Session::cfgSessionDescriptionEntry = QStringLiteral("SessionPrettyContents");
0027 const QString Session::cfgSessionProjectsEntry = QStringLiteral("Open Projects");
0028 const QString Session::cfgSessionOptionsGroup = QStringLiteral("General Options");
0029 
0030 class SessionPrivate
0031 {
0032 public:
0033     SessionInfo info;
0034     Session* const q;
0035     bool isTemporary;
0036 
0037     QUrl pluginArea( const IPlugin* plugin )
0038     {
0039         QString name = Core::self()->pluginController()->pluginInfo(plugin).pluginId();
0040         QUrl url = QUrl::fromLocalFile(info.path + QLatin1Char('/') + name );
0041         if( !QFile::exists( url.toLocalFile() ) ) {
0042             QDir( info.path ).mkdir( name );
0043         }
0044         return url;
0045     }
0046 
0047     SessionPrivate( Session* session, const QString& id )
0048         : info( Session::parse( id, true ) )
0049         , q( session )
0050         , isTemporary( false )
0051     {
0052     }
0053 
0054     void updateDescription()
0055     {
0056         buildDescription( info );
0057         emit q->sessionUpdated( q );
0058     }
0059 
0060     static QString generatePrettyContents( const SessionInfo& info );
0061     static QString generateDescription( const SessionInfo& info );
0062     static void buildDescription( SessionInfo& info );
0063 };
0064 
0065 Session::Session( const QString& id, QObject* parent )
0066         : ISession(parent)
0067         , d_ptr(new SessionPrivate(this, id))
0068 {
0069 }
0070 
0071 Session::~Session() = default;
0072 
0073 QString Session::name() const
0074 {
0075     Q_D(const Session);
0076 
0077     return d->info.name;
0078 }
0079 
0080 QList<QUrl> Session::containedProjects() const
0081 {
0082     Q_D(const Session);
0083 
0084     return d->info.projects;
0085 }
0086 
0087 QString Session::description() const
0088 {
0089     Q_D(const Session);
0090 
0091     return d->info.description;
0092 }
0093 
0094 QUrl Session::pluginDataArea( const IPlugin* p )
0095 {
0096     Q_D(Session);
0097 
0098     return d->pluginArea( p );
0099 }
0100 
0101 KSharedConfigPtr Session::config()
0102 {
0103     Q_D(Session);
0104 
0105     return d->info.config;
0106 }
0107 
0108 QUuid Session::id() const
0109 {
0110     Q_D(const Session);
0111 
0112     return d->info.uuid;
0113 }
0114 
0115 void Session::setName( const QString& newname )
0116 {
0117     Q_D(Session);
0118 
0119     d->info.name = newname;
0120     d->info.config->group( QString() ).writeEntry( cfgSessionNameEntry, newname );
0121     d->updateDescription();
0122 }
0123 
0124 void Session::setContainedProjects( const QList<QUrl>& projects )
0125 {
0126     Q_D(Session);
0127 
0128     d->info.projects = projects;
0129     d->info.config->group( cfgSessionOptionsGroup ).writeEntry( cfgSessionProjectsEntry, projects );
0130     d->updateDescription();
0131 }
0132 
0133 void Session::setTemporary(bool temp)
0134 {
0135     Q_D(Session);
0136 
0137     d->isTemporary = temp;
0138 }
0139 
0140 bool Session::isTemporary() const
0141 {
0142     Q_D(const Session);
0143 
0144     return d->isTemporary;
0145 }
0146 
0147 QString Session::path() const
0148 {
0149     Q_D(const Session);
0150 
0151     return d->info.path;
0152 }
0153 
0154 QString SessionPrivate::generatePrettyContents( const SessionInfo& info )
0155 {
0156     if( info.projects.isEmpty() )
0157         return i18n("(no projects)");
0158 
0159     QStringList projectNames;
0160     projectNames.reserve( info.projects.size() );
0161 
0162     for (const QUrl& url : info.projects) {
0163         IProject* project = nullptr;
0164         if( ICore::self() && ICore::self()->projectController() ) {
0165             project = ICore::self()->projectController()->findProjectForUrl( url );
0166         }
0167 
0168         if( project ) {
0169             projectNames << project->name();
0170         } else {
0171             QString projectName = url.fileName();
0172             projectName.remove(QRegExp(QStringLiteral("\\.kdev4$"), Qt::CaseInsensitive));
0173             projectNames << projectName;
0174         }
0175     }
0176 
0177     if( projectNames.isEmpty() ) {
0178         return i18n("(no projects)");
0179     } else {
0180         return projectNames.join(QLatin1String(", "));
0181     }
0182 }
0183 
0184 QString SessionPrivate::generateDescription( const SessionInfo& info )
0185 {
0186     QString prettyContentsFormatted = generatePrettyContents( info );
0187     QString description;
0188 
0189     if( info.name.isEmpty() ) {
0190         description = prettyContentsFormatted;
0191     } else {
0192         description = info.name + QLatin1String(":  ") + prettyContentsFormatted;
0193     }
0194 
0195     return description;
0196 }
0197 
0198 void SessionPrivate::buildDescription( SessionInfo& info )
0199 {
0200     QString description = generateDescription( info );
0201 
0202     info.description = description;
0203     info.config->group( QString() ).writeEntry( Session::cfgSessionDescriptionEntry, description );
0204     info.config->sync();
0205 }
0206 
0207 SessionInfo Session::parse( const QString& id, bool mkdir )
0208 {
0209     SessionInfo ret;
0210     QString sessionPath = SessionController::sessionDirectory(id);
0211 
0212     QDir sessionDir( sessionPath );
0213     if( !sessionDir.exists() ) {
0214         if( mkdir ) {
0215             sessionDir.mkpath(sessionPath);
0216             Q_ASSERT( sessionDir.exists() );
0217         } else {
0218             return ret;
0219         }
0220     }
0221 
0222     ret.uuid = id;
0223     ret.path = sessionPath;
0224     ret.config = KSharedConfig::openConfig(sessionPath + QLatin1String("/sessionrc"));
0225 
0226     KConfigGroup cfgRootGroup = ret.config->group( QString() );
0227     KConfigGroup cfgOptionsGroup = ret.config->group( cfgSessionOptionsGroup );
0228 
0229     ret.name = cfgRootGroup.readEntry( cfgSessionNameEntry, QString() );
0230     ret.projects = cfgOptionsGroup.readEntry( cfgSessionProjectsEntry, QList<QUrl>() );
0231     SessionPrivate::buildDescription( ret );
0232 
0233     return ret;
0234 }
0235 
0236 }
0237 
0238 #include "moc_session.cpp"