File indexing completed on 2024-05-12 05:52:07

0001 /*
0002     SPDX-FileCopyrightText: 2005 Christoph Cullmann <cullmann@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "katesession.h"
0008 
0009 #include "katedebug.h"
0010 
0011 #include <KConfig>
0012 #include <KConfigGroup>
0013 
0014 #include <QCollator>
0015 #include <QFile>
0016 #include <QFileInfo>
0017 
0018 static const QLatin1String opGroupName("Open Documents");
0019 static const QLatin1String keyCount("Count");
0020 
0021 KateSession::KateSession(const QString &file, const QString &name, const bool anonymous, const KConfig *_config)
0022     : m_name(name)
0023     , m_file(file)
0024     , m_anonymous(anonymous)
0025 {
0026     Q_ASSERT(!m_file.isEmpty());
0027 
0028     if (_config) { // copy data from config instead
0029         m_config.reset(_config->copyTo(m_file));
0030     } else if (!QFile::exists(m_file)) { // given file exists, use it to load some stuff
0031         qCDebug(LOG_KATE) << "Warning, session file not found: " << m_file;
0032         return;
0033     }
0034 
0035     m_timestamp = QFileInfo(m_file).lastModified();
0036 }
0037 
0038 const QString &KateSession::file() const
0039 {
0040     return m_file;
0041 }
0042 
0043 unsigned int KateSession::documents()
0044 {
0045     return config()->group(opGroupName).readEntry(keyCount, 0);
0046 }
0047 
0048 void KateSession::setFile(const QString &filename)
0049 {
0050     if (m_config) {
0051         KConfig *cfg = m_config->copyTo(filename);
0052         m_config.reset(cfg);
0053     }
0054 
0055     m_file = filename;
0056 }
0057 
0058 void KateSession::setName(const QString &name)
0059 {
0060     m_name = name;
0061 }
0062 
0063 KConfig *KateSession::config()
0064 {
0065     if (!m_config) {
0066         // reread documents number?
0067         m_config = std::make_unique<KConfig>(m_file, KConfig::SimpleConfig);
0068     }
0069 
0070     return m_config.get();
0071 }
0072 
0073 KateSession::Ptr KateSession::create(const QString &file, const QString &name)
0074 {
0075     return Ptr(new KateSession(file, name, false));
0076 }
0077 
0078 KateSession::Ptr KateSession::createFrom(const KateSession::Ptr &session, const QString &file, const QString &name)
0079 {
0080     return Ptr(new KateSession(file, name, false, session->config()));
0081 }
0082 
0083 KateSession::Ptr KateSession::createAnonymous(const QString &file)
0084 {
0085     return Ptr(new KateSession(file, QString(), true));
0086 }
0087 
0088 KateSession::Ptr KateSession::createAnonymousFrom(const KateSession::Ptr &session, const QString &file)
0089 {
0090     return Ptr(new KateSession(file, QString(), true, session->config()));
0091 }
0092 
0093 bool KateSession::compareByName(const KateSession::Ptr &s1, const KateSession::Ptr &s2)
0094 {
0095     return QCollator().compare(s1->name(), s2->name()) == -1;
0096 }
0097 
0098 bool KateSession::compareByTimeDesc(const KateSession::Ptr &s1, const KateSession::Ptr &s2)
0099 {
0100     return s1->timestamp() > s2->timestamp();
0101 }