diff options
author | Michele Calgaro <michele.calgaro@yahoo.it> | 2015-09-28 00:45:39 +0900 |
---|---|---|
committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2015-09-28 00:45:39 +0900 |
commit | f0f642f6b23b9a38727944a6db194bf4d16f0377 (patch) | |
tree | 8d754de03d70ee98ebe908b1a731e89e2d992cc7 | |
parent | 2eb61917990c77f0b67ef60a6d60a7fcab3b6ddc (diff) | |
download | tdebase-f0f642f6b23b9a38727944a6db194bf4d16f0377.tar.gz tdebase-f0f642f6b23b9a38727944a6db194bf4d16f0377.zip |
Added initial version of new KateSession and KateSessionManager. The old version of the same classes is still the default for the time being.
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
-rw-r--r-- | kate/app/katesession.cpp | 265 | ||||
-rw-r--r-- | kate/app/katesession.h | 109 |
2 files changed, 373 insertions, 1 deletions
diff --git a/kate/app/katesession.cpp b/kate/app/katesession.cpp index e71d383f1..a6547393a 100644 --- a/kate/app/katesession.cpp +++ b/kate/app/katesession.cpp @@ -48,6 +48,271 @@ #include <unistd.h> #include <time.h> +// String constants +namespace +{ + // Kate session + const char *KS_COUNT = "Count"; + const char *KS_DOCCOUNT = "Document count"; + const char *KS_DOCLIST = "Document list"; + const char *KS_GENERAL = "General"; + const char *KS_NAME = "Name"; + const char *KS_OPENDOC = "Open Documents"; + const char *KS_READONLY = "ReadOnly"; + const char *KS_UNNAMED = "Unnamed"; + + // Kate session manager + const char *KSM_DIR = "kate/sessions"; + const char *KSM_FILE = "sessions.list"; + const char *KSM_SESSIONS_COUNT = "Sessions count"; + const char *KSM_SESSIONS_LIST = "Sessions list"; +} + +KateSession::KateSession(const TQString &sessionName, const TQString &filename, bool isFullName) : + m_sessionName(sessionName), m_filename(filename), m_isFullName(isFullName), + m_readOnly(false), m_docCount(0), m_documents(), m_config(NULL) +{ + if (m_isFullName && TDEGlobal::dirs()->exists(m_filename)) + { + // Create config object if the session file already exists + m_config = new KSimpleConfig(m_filename, m_readOnly); + m_config->setGroup(KS_GENERAL); + // Session name + if (m_sessionName.isEmpty()) + { + m_sessionName = m_config->readEntry(KS_NAME, i18n(KS_UNNAMED)); + } + // Read only + m_readOnly = m_config->readBoolEntry(KS_READONLY, false); + m_config->setReadOnly(m_readOnly); + // Document list + if (m_config->hasGroup(KS_DOCLIST)) + { + // Read new style document list (from TDE R14.1.0) + m_config->setGroup(KS_DOCLIST); + m_docCount = m_config->readNumEntry(KS_DOCCOUNT, 0); + for (int i=0; i<m_docCount; ++i) + { + TQString urlStr = m_config->readEntry(TQString("URL_%1").arg(i)); + if (!urlStr.isEmpty()) + { + // Filter out empty URLs + m_documents.append(urlStr); + } + } + } + else + { + // Create document list from old session configuration + // to effortlessly import existing sessions + m_config->setGroup(KS_OPENDOC); + m_docCount = m_config->readNumEntry(KS_COUNT, 0); + for (int i=0; i<m_docCount; ++i) + { + m_config->setGroup(TQString("Document %1").arg(i)); + TQString urlStr = m_config->readEntry("URL"); + if (!urlStr.isEmpty()) + { + // Filter out empty URLs + m_documents.append(urlStr); + } + } + } + // Update document count again, in case empty URLs were found + m_docCount = static_cast<int>(m_documents.count()); + } + if (m_sessionName.isEmpty()) + { + m_sessionName = i18n(KS_UNNAMED); + } + if (m_docCount == 0) + { + m_documents.clear(); + } +} + +//------------------------------------ +KateSession::~KateSession() +{ + if (m_config) + { + delete m_config; + } +} + +//------------------------------------ +void KateSession::setSessionName(const TQString &sessionName) +{ + m_sessionName = sessionName; + if (m_sessionName.isEmpty()) + { + m_sessionName = i18n(KS_UNNAMED); + } +} + +//------------------------------------ +void KateSession::setReadOnly(bool readOnly) +{ + if (!m_readOnly && readOnly) + { + // When a session is turned read only, make sure the current + // status is first saved to disk + save(); + } + m_readOnly = readOnly; + if (m_config) + { + m_config->setReadOnly(m_readOnly); + } +} + +//------------------------------------ +void KateSession::save() +{ + if (m_readOnly) + return; + + if (!m_isFullName) + { + // create a new session filename + int s = time(0); + TQCString tname; + TQString tmpName; + while (true) + { + tname.setNum(s++); + KMD5 md5(tname); + tmpName = m_filename + TQString("%1.katesession").arg(md5.hexDigest().data()); + if (!TDEGlobal::dirs()->exists(tmpName)) + { + m_filename = tmpName; + m_isFullName = true; + break; + } + } + } + + if (!m_config) + { + m_config = new KSimpleConfig(m_filename); + } + if (m_config->hasGroup(KS_GENERAL)) + { + m_config->deleteGroup(KS_GENERAL); + } + m_config->setGroup(KS_GENERAL); + m_config->writeEntry(KS_NAME, m_sessionName); + m_config->writeEntry(KS_READONLY, m_readOnly); + + if (m_config->hasGroup(KS_DOCLIST)) + { + m_config->deleteGroup(KS_DOCLIST); + } + m_config->setGroup(KS_DOCLIST); + m_config->writeEntry(KS_DOCCOUNT, m_docCount); + for (int i=0; i<m_docCount; ++i) + { + m_config->writeEntry(TQString("URL_%1").arg(i), m_documents[i]); + } + + m_config->sync(); +} + +//------------------------------------ +KateSessionManager *KateSessionManager::ksm_instance = NULL; + +//------------------------------------ +KateSessionManager* KateSessionManager::self() +{ + if (!KateSessionManager::ksm_instance) + { + KateSessionManager::ksm_instance = new KateSessionManager(); + } + return KateSessionManager::ksm_instance; +} + +//------------------------------------ +KateSessionManager::KateSessionManager() : + m_baseDir(locateLocal("data", KSM_DIR)+"/"), m_configFile(m_baseDir + KSM_FILE), + m_sessionsCount(0), m_sessions(), m_config(NULL) +{ + m_sessions.setAutoDelete(true); + + if (TDEGlobal::dirs()->exists(m_configFile)) + { + // Read new style configuration (from TDE R14.1.0) + m_config = new KSimpleConfig(m_configFile); + m_config->setGroup(KSM_SESSIONS_LIST); + m_sessionsCount = m_config->readNumEntry(KSM_SESSIONS_COUNT, 0); + for (int i=0; i<m_sessionsCount; ++i) + { + TQString urlStr = m_config->readEntry(TQString("URL_%1").arg(i)); + if (!urlStr.isEmpty() && TDEGlobal::dirs()->exists(urlStr)) + { + // Filter out empty URLs or non existing sessions + m_sessions.append(new KateSession(TQString::null, urlStr, true)); + } + } + } + else + { + // Create sessions list from session files + // to effortlessly import existing sessions + TQDir sessionDir(m_baseDir, "*.katesession"); + for (unsigned int i=0; i<sessionDir.count(); ++i) + { + m_sessions.append(new KateSession(TQString::null, m_baseDir+sessionDir[i], true)); + } + } + m_sessionsCount = static_cast<int>(m_sessions.count()); +} + +//------------------------------------ +KateSessionManager::~KateSessionManager() +{ + saveConfig(); + if (m_config) + { + delete m_config; + } + if (!m_sessions.isEmpty()) + { + m_sessions.clear(); + } +} + +//------------------------------------ +void KateSessionManager::saveConfig() +{ + if (!m_config) + { + m_config = new KSimpleConfig(m_configFile); + } + if (m_config->hasGroup(KSM_SESSIONS_LIST)) + { + m_config->deleteGroup(KSM_SESSIONS_LIST); + } + m_config->setGroup(KSM_SESSIONS_LIST); + m_config->writeEntry(KSM_SESSIONS_COUNT, m_sessionsCount); + for (int i=0; i<m_sessionsCount; ++i) + { + // Save the session first, to make sure a new session has an associated file + m_sessions[i]->save(); + m_config->writeEntry(TQString("URL_%1").arg(i), m_sessions[i]->getSessionFilename()); + } + m_config->sync(); +} + + + + + + +//------------------------------------ +//------------------------------------ +//------------------------------------ +//------------------------------------ +// Michele - to be removed with OldKateSession bool operator<( const OldKateSession::Ptr& a, const OldKateSession::Ptr& b ) { return a->sessionName().lower() < b->sessionName().lower(); diff --git a/kate/app/katesession.h b/kate/app/katesession.h index e8038d66e..81a4a77c5 100644 --- a/kate/app/katesession.h +++ b/kate/app/katesession.h @@ -27,9 +27,11 @@ #include <tdeaction.h> #include <tqobject.h> +#include <tqptrlist.h> #include <tqvaluelist.h> +#include <tqstringlist.h> -class OldKateSessionManager; +class OldKateSessionManager; // Michele - to be removed with OldKateSession class KDirWatch; class TDEListView; @@ -37,6 +39,111 @@ class KPushButton; class TQCheckBox; +class KateSession +{ + public: + + /** + * create a new session and read the config from fileName if it exists + * @param sessionName session name + * @param fileName file where session config is saved to/restored from + * @param isFullName true -> filename is a full filename, used to load/save the session configuration + * false -> filename is a folder name. This is used for new unsaved sessions + * to inject the location where the configuration file should be saved + */ + KateSession(const TQString &sessionName, const TQString &fileName, bool isFullName); + + /** + * Destructor + */ + ~KateSession(); + + /** + * Returns the session name + */ + const TQString& getSessionName() const { return m_sessionName; } + /** + * Set the new session name + * @param sessionName the new session name + */ + void setSessionName(const TQString &sessionName); + + /** + * Returns whether the session is read only or not + */ + bool isReadOnly() const { return m_readOnly; } + /** + * Set session read only status + * @param readOnly if true, the session config can not be saved to file + */ + void setReadOnly(bool readOnly); + + /** + * Returns the session filename + */ + const TQString& getSessionFilename() const { return m_filename; } + + /** + * Save session info + * @return true if the session config is saved, false otherwise + */ + void save(); + + + private: + TQString m_sessionName; + TQString m_filename; + bool m_isFullName; // true -> m_filename is a full filename + // false -> m_filename is a folder name. + bool m_readOnly; + int m_docCount; // number of documents in the session + TQStringList m_documents; // document URLs + KSimpleConfig *m_config; // session config + +}; + + +//------------------------------------ +class KateSessionManager +{ + public: + + /** + * get a pointer to the unique KateSessionManager instance. + * If the manager does not exist yet, create it. + */ + static KateSessionManager* self(); + + /** + * Destructor + */ + ~KateSessionManager(); + + /** + * Save session manager info + */ + void saveConfig(); + + + private: + KateSessionManager(); + + TQString m_baseDir; // folder where session files are stored + TQString m_configFile; // file where the session list config is stored + int m_sessionsCount; // number of sessions + TQPtrList<KateSession> m_sessions; // session list + KSimpleConfig *m_config; // session manager config + + static KateSessionManager *ksm_instance; // the only KateSessionManager instance +}; + + + + + +//------------------------------------ +//------------------------------------ +//------------------------------------ class OldKateSession : public TDEShared { public: |