Commit 0dbfebcb authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

Revert "sessions: upgrade anonymous session to regular one"

This reverts commit 88c6ab08.

Reverts session behavior back to 21.08 release style

we need to re-think how we really want to handle that

BUG: 441188
parent 56948573
Pipeline #75948 passed with stage
in 8 minutes
......@@ -136,7 +136,7 @@ void KateSessionManagerTest::saveActiveSessionWithAnynomous()
QCOMPARE(m_manager->saveActiveSession(), true);
QCOMPARE(m_manager->activeSession()->isAnonymous(), true);
QCOMPARE(m_manager->activeSession()->name(), QLatin1String("Anonymous"));
QCOMPARE(m_manager->activeSession()->name(), QString());
QCOMPARE(m_manager->sessionList().size(), 0);
}
......@@ -163,20 +163,3 @@ void KateSessionManagerTest::startNonEmpty()
KateSessionManager m(this, m_tempdir->path());
QCOMPARE(m.sessionList().size(), 2);
}
void KateSessionManagerTest::newSessionInheritsDefaults()
{
QLatin1String pluginToTest("katekonsoleplugin");
QString sessionsDirs = m_tempdir->path() + QLatin1String("/sessions");
KateSessionManager m(this, sessionsDirs);
m.activateAnonymousSession();
m_app->pluginManager()->unloadAllPlugins();
m_app->pluginManager()->loadPlugin(pluginToTest);
m.saveDefaults();
m_app->pluginManager()->unloadAllPlugins();
m.activateSession(QLatin1String("test"));
QVERIFY(m_app->pluginManager()->isLoaded(pluginToTest));
}
......@@ -31,7 +31,6 @@ private Q_SLOTS:
void deletingSessionFilesUnderRunningApp();
void startNonEmpty();
void newSessionInheritsDefaults();
private:
class QTemporaryDir *m_tempdir;
......
......@@ -101,7 +101,6 @@
<Action name="sessions_list"/>
<Action name="sessions_manage"/>
<Separator/>
<Action name="sessions_save_default"/>
<Action name="sessions_save"/>
<Action name="sessions_save_as"/>
</Menu>
......
......@@ -176,37 +176,17 @@ bool KateApp::startupKate()
// user specified session to open
if (m_args.isSet(QStringLiteral("start"))) {
sessionManager()->activateSession(m_args.value(QStringLiteral("start")), false);
} else if (m_args.isSet(QStringLiteral("startanon")) || m_args.isSet(QStringLiteral("new-session"))) {
sessionManager()->activateNewSession();
} else if (m_args.isSet(QStringLiteral("stdin")) || (m_args.positionalArguments().count() > 0)) {
/* User decided to open some files and we need some session space.
* Notice that if we have running instance on dbus already, we don't
* get to this point and files will be open in the other instance.
*
* options:
* 1. she doesn't work with sessions
* a. ask her what to do with the existing anon session (tedious)
* b. force her to name the session (not friendly, forces user to change behaviour)
* c. overwrite the anon session (surprising data loss)
* d. restore the anon session and open extra files in there (might not be what she wanted, but can't have two anonymous sessions)
* 2. she works with sessions actively
* a. ask what session she wants to open files in (tedious)
* b. start the new session, destroy the anon session slot (possible data loss)
* 3. she doesn't work with sessions but has some from long ago...
* a. ask what session she wants to open files in
*/
if (sessionManager()->sessionList().isEmpty()) {
sessionManager()->activateAnonymousSession();
} else {
sessionManager()->activateNewSession();
}
} else {
} else if (m_args.isSet(QStringLiteral("startanon"))) {
sessionManager()->activateAnonymousSession();
} else if (!m_args.isSet(QStringLiteral("stdin")) && (m_args.positionalArguments().count() == 0)) { // only start session if no files specified
// let the user choose session if possible
if (!sessionManager()->chooseSession()) {
// we will exit kate now, notify the rest of the world we are done
KStartupInfo::appStarted(KStartupInfo::startupId());
return false;
}
} else {
sessionManager()->activateAnonymousSession();
}
// oh, no mainwindow, create one, should not happen, but make sure ;)
......
......@@ -448,10 +448,6 @@ void KateMainWindow::setupActions()
a->setText(i18nc("Menu entry Session->New Session", "&New Session"));
// Qt::QueuedConnection to avoid deletion of code that is executed when reducing the amount of mainwindows. (bug #227008)
connect(a, &QAction::triggered, KateApp::self()->sessionManager(), &KateSessionManager::sessionNew, Qt::QueuedConnection);
a = actionCollection()->addAction(QStringLiteral("sessions_save_default"));
a->setIcon(QIcon::fromTheme(QStringLiteral("document-save")));
a->setText(i18n("&Save as defaults"));
connect(a, &QAction::triggered, KateApp::self()->sessionManager(), &KateSessionManager::saveDefaults);
a = actionCollection()->addAction(QStringLiteral("sessions_save"));
a->setIcon(QIcon::fromTheme(QStringLiteral("document-save")));
a->setText(i18n("&Save Session"));
......
......@@ -370,12 +370,3 @@ void KatePluginManager::unloadPlugin(const QString &name, bool permanent)
*/
m_name2Plugin.value(name)->load = !permanent;
}
bool KatePluginManager::isLoaded(const QString &name) const
{
if (!m_name2Plugin.contains(name)) {
return false;
}
return m_name2Plugin.value(name)->plugin != nullptr;
}
......@@ -70,7 +70,6 @@ public:
KTextEditor::Plugin *loadPlugin(const QString &name, bool permanent = true);
void unloadPlugin(const QString &name, bool permanent = true);
bool isLoaded(const QString &name) const;
private:
void setupPluginList();
......
......@@ -49,7 +49,7 @@ void KateStashManager::stashDocuments(KConfig *config, const QList<KTextEditor::
dir.cd(QStringLiteral("stash"));
const auto activeSession = KateApp::self()->sessionManager()->activeSession();
if (!activeSession) {
if (!activeSession || activeSession->isAnonymous() || activeSession->name().isEmpty()) {
qDebug(LOG_KATE) << "Could not stash files without a session";
return;
}
......@@ -75,7 +75,7 @@ void KateStashManager::stashDocuments(KConfig *config, const QList<KTextEditor::
bool KateStashManager::willStashDoc(KTextEditor::Document *doc)
{
const auto activeSession = KateApp::self()->sessionManager()->activeSession();
if (!activeSession) {
if (!activeSession || activeSession->isAnonymous() || activeSession->name().isEmpty()) {
return false;
}
if (doc->text().isEmpty()) {
......
......@@ -226,13 +226,9 @@ int main(int argc, char **argv)
// --startanon session option
const QCommandLineOption startAnonymousSessionOption(QStringList() << QStringLiteral("startanon"),
i18n("Start Kate with a new session, implies '-n' (deprecated)."));
i18n("Start Kate with a new anonymous session, implies '-n'."));
parser.addOption(startAnonymousSessionOption);
// --new-session option
const QCommandLineOption startNewSessionOption(QStringList() << QStringLiteral("new-session"), i18n("Start Kate with a new session, implies '-n'."));
parser.addOption(startNewSessionOption);
// -n/--new option
const QCommandLineOption startNewInstanceOption(QStringList() << QStringLiteral("n") << QStringLiteral("new"),
i18n("Force start of a new kate instance (is ignored if start is used and another kate instance already "
......@@ -382,7 +378,7 @@ int main(int argc, char **argv)
bool session_already_opened = false;
// check if we try to start an already opened session
if (parser.isSet(startAnonymousSessionOption) || parser.isSet(startNewSessionOption)) {
if (parser.isSet(startAnonymousSessionOption)) {
force_new = true;
} else if (parser.isSet(startSessionOption)) {
start_session = parser.value(startSessionOption);
......
......@@ -89,12 +89,12 @@ KateSession::Ptr KateSession::createFrom(const KateSession::Ptr &session, const
KateSession::Ptr KateSession::createAnonymous(const QString &file)
{
return Ptr(new KateSession(file, AnonymousSessionName, true));
return Ptr(new KateSession(file, QString(), true));
}
KateSession::Ptr KateSession::createAnonymousFrom(const KateSession::Ptr &session, const QString &file)
{
return Ptr(new KateSession(file, AnonymousSessionName, true, session->config()));
return Ptr(new KateSession(file, QString(), true, session->config()));
}
bool KateSession::compareByName(const KateSession::Ptr &s1, const KateSession::Ptr &s2)
......
......@@ -18,8 +18,6 @@
class KConfig;
static const QLatin1String AnonymousSessionName("Anonymous");
class KATE_TESTS_EXPORT KateSession : public QSharedData
{
public:
......
......@@ -259,7 +259,9 @@ void KateSessionManageDialog::openSessionAsTemplate()
hide();
KateApp::self()->sessionManager()->activateNewSessionFrom(item->session);
KateSessionManager *sm = KateApp::self()->sessionManager();
KateSession::Ptr ns = KateSession::createAnonymousFrom(item->session, sm->anonymousSessionFile());
sm->activateSession(ns);
done(ResultOpen);
}
......
......@@ -34,26 +34,6 @@
#include <unistd.h>
#endif
void syncConfig(KConfig *config)
{
config->sync();
/**
* try to sync file to disk
*/
QFile fileToSync(config->name());
if (fileToSync.open(QIODevice::ReadOnly)) {
#ifndef Q_OS_WIN
// ensure that the file is written to disk
#ifdef HAVE_FDATASYNC
fdatasync(fileToSync.handle());
#else
fsync(fileToSync.handle());
#endif
#endif
}
}
// BEGIN KateSessionManager
KateSessionManager::KateSessionManager(QObject *parent, const QString &sessionsDir)
......@@ -170,11 +150,12 @@ bool KateSessionManager::activateSession(KateSession::Ptr session, const bool cl
return true;
}
void KateSessionManager::loadSession(const KateSession::Ptr &session, bool loadDocs) const
void KateSessionManager::loadSession(const KateSession::Ptr &session) const
{
// open the new session
KSharedConfigPtr sharedConfig = KSharedConfig::openConfig();
KConfig *sc = session->config();
const bool loadDocs = !session->isAnonymous(); // do not load docs for new sessions
// if we have no session config object, try to load the default
// (anonymous/unnamed sessions)
......@@ -243,27 +224,12 @@ bool KateSessionManager::activateSession(const QString &name, const bool closeAn
bool KateSessionManager::activateAnonymousSession()
{
return activateSession(AnonymousSessionName, false);
}
void KateSessionManager::activateNewSession()
{
auto defSession = KateSession::create(defaultSessionFile(), QString());
m_activeSession = KateSession::createAnonymousFrom(defSession, anonymousSessionFile());
KateApp::self()->documentManager()->closeAllDocuments();
loadSession(m_activeSession, false);
Q_EMIT sessionChanged();
}
void KateSessionManager::activateNewSessionFrom(const KateSession::Ptr &session)
{
activateSession(KateSession::createAnonymousFrom(session, anonymousSessionFile()));
return activateSession(QString(), false);
}
KateSession::Ptr KateSessionManager::giveSession(const QString &name)
{
if (name.isEmpty() || name == AnonymousSessionName) {
if (name.isEmpty()) {
return KateSession::createAnonymous(anonymousSessionFile());
}
......@@ -271,17 +237,8 @@ KateSession::Ptr KateSessionManager::giveSession(const QString &name)
return m_sessions.value(name);
}
return createSession(name);
}
KateSession::Ptr KateSessionManager::createSession(const QString &name)
{
auto defSession = KateSession::create(defaultSessionFile(), QString());
auto s = KateSession::createFrom(defSession, sessionFileForName(name), name);
s->setDocuments(s->documents()); // force KConfig to write the session file on sync
syncConfig(s->config());
KateSession::Ptr s = KateSession::create(sessionFileForName(name), name);
saveSessionTo(s->config());
m_sessions[name] = s;
// Due to this add to m_sessions will updateSessionList() no signal emit,
// but it's important to add. Otherwise could it be happen that m_activeSession
......@@ -346,7 +303,7 @@ QString KateSessionManager::renameSession(KateSession::Ptr session, const QStrin
if (!job->exec()) {
KMessageBox::sorry(QApplication::activeWindow(),
i18n("The session could not be renamed to \"%1\". Failed to write to \"%2\"\n%3", newName, newFile, job->errorString()),
i18n("The session could not be renamed to \"%1\". Failed to write to \"%2\"", newName, newFile),
i18n("Session Renaming"));
return QString();
}
......@@ -395,7 +352,22 @@ void KateSessionManager::saveSessionTo(KConfig *sc) const
}
}
syncConfig(sc);
sc->sync();
/**
* try to sync file to disk
*/
QFile fileToSync(sc->name());
if (fileToSync.open(QIODevice::ReadOnly)) {
#ifndef Q_OS_WIN
// ensure that the file is written to disk
#ifdef HAVE_FDATASYNC
fdatasync(fileToSync.handle());
#else
fsync(fileToSync.handle());
#endif
#endif
}
}
bool KateSessionManager::saveActiveSession(bool rememberAsLast)
......@@ -408,7 +380,7 @@ bool KateSessionManager::saveActiveSession(bool rememberAsLast)
saveSessionTo(sc);
if (rememberAsLast) {
if (rememberAsLast && !activeSession()->isAnonymous()) {
KSharedConfigPtr c = KSharedConfig::openConfig();
c->group("General").writeEntry("Last Session", activeSession()->name());
c->sync();
......@@ -422,52 +394,34 @@ bool KateSessionManager::chooseSession()
// get last used session, default to default session
const QString lastSession(c.readEntry("Last Session", QString()));
const QString sesStart(c.readEntry("Startup Session", QString()));
const QString sesStart(c.readEntry("Startup Session", "manual"));
if (sesStart == QLatin1String("new")) {
activateNewSession();
return true;
} else if (sesStart == QLatin1String("manual")) {
return QScopedPointer<KateSessionManageDialog>(new KateSessionManageDialog(nullptr, lastSession))->exec();
} else {
/* default behaviour is to restore the session if possible */
if (!lastSession.isEmpty()) {
return activateSession(lastSession, false);
} else {
activateNewSession();
return true;
}
// uhh, just open last used session, show no chooser
if (sesStart == QLatin1String("last")) {
return activateSession(lastSession, false);
}
// start with empty new session or in case no sessions exist
if (sesStart == QLatin1String("new") || sessionList().empty()) {
return activateAnonymousSession();
}
// else: ask the user
return QScopedPointer<KateSessionManageDialog>(new KateSessionManageDialog(nullptr, lastSession))->exec();
}
void KateSessionManager::sessionNew()
{
// if the current session is anonymous, ask the user what to do with it,
// since we need that slot for our new one
if (activeSession() && activeSession()->isAnonymous()) {
const int res = KMessageBox::warningYesNoCancel(KateApp::self()->activeKateMainWindow(),
i18n("Do you want to save current anonymous session?"),
i18n("Close Document"),
KStandardGuiItem::save(),
KStandardGuiItem::discard());
switch (res) {
case KMessageBox::Yes:
sessionSaveAs();
break;
case KMessageBox::No:
break;
default:
return;
}
}
activateNewSession();
activateSession(giveSession(QString()));
}
void KateSessionManager::sessionSave()
{
saveActiveSession();
if (activeSession() && activeSession()->isAnonymous()) {
sessionSaveAs();
} else {
saveActiveSession();
}
}
void KateSessionManager::sessionSaveAs()
......@@ -489,6 +443,10 @@ void KateSessionManager::sessionSaveAs()
QString KateSessionManager::askForNewSessionName(KateSession::Ptr session, const QString &newName)
{
if (session->name() == newName && !session->isAnonymous()) {
return QString();
}
const QString messagePrompt = i18n("Session name:");
const KLocalizedString messageExist = ki18n(
"There is already an existing session with your chosen name: %1\n"
......@@ -683,6 +641,10 @@ void KateSessionManager::updateJumpListActions(const QStringList &sessionList)
bool KateSessionManager::isViewLessDocumentViewSpaceGroup(const QString &group)
{
if (KateApp::self()->sessionManager()->activeSession()->isAnonymous()) {
return false;
}
if (!group.startsWith(QStringLiteral("MainWindow"))) {
return false;
}
......@@ -700,17 +662,4 @@ bool KateSessionManager::isViewLessDocumentViewSpaceGroup(const QString &group)
return false;
}
void KateSessionManager::saveDefaults()
{
auto session = KateSession::createFrom(activeSession(), defaultSessionFile(), QString());
KConfig *sc = session->config();
saveSessionTo(sc);
}
QString KateSessionManager::defaultSessionFile() const
{
const QString file = m_sessionsDir + QStringLiteral("/../defaults.katesession");
return QDir().cleanPath(file);
}
// END KateSessionManager
......@@ -19,25 +19,12 @@
typedef QList<KateSession::Ptr> KateSessionList;
/**
* Takes care of session store, session activation and sesion defaults.
*
* There is a single implicit anonymous session that is always present, but not
* exported outside the manager, hence not even visible to user. It's possible
* to activate the session via KateSessionManager::activateAnonymousSession. For other
* usecases the anonymous session behaves the same as regular one.
*
* The session defaults are stored in an implicit default session, which the users
* can only overwrite via KateSessionManager::saveDefaults.
*
* If the client request new session (calling KateSessionManager::activateNewSession)
* the manager will import stored default session config to new session and overwrite
* the anonymous session with this newly created one.
*/
class KATE_TESTS_EXPORT KateSessionManager : public QObject
{
Q_OBJECT
friend class KateSessionManageDialog;
public:
KateSessionManager(QObject *parent = nullptr, const QString &sessionsDir = QString());
......@@ -59,20 +46,10 @@ public:
bool activateSession(const QString &name, const bool closeAndSaveLast = true, const bool loadNew = true);
/**
* activate existing anonymous session
* activates new/anonymous session
*/
bool activateAnonymousSession();
/**
* create and activate new session and store it as anonymous
*/
void activateNewSession();
/**
* create and activate new session from another \p session as template
*/
void activateNewSessionFrom(const KateSession::Ptr &session);
/**
* save current session
* @param rememberAsLast remember this session as last used?
......@@ -90,11 +67,6 @@ public:
return m_activeSession;
}
/**
* @return true when @p session is active in any Kate instance, otherwise false
*/
bool sessionIsActive(const QString &session);
/**
* session dir
* @return global session dir
......@@ -132,11 +104,6 @@ public Q_SLOTS:
*/
void sessionManage();
/**
* save current configuration as session defaults
*/
void saveDefaults();
Q_SIGNALS:
/**
* Emitted, whenever the session changes, e.g. when it was renamed.
......@@ -153,6 +120,12 @@ Q_SIGNALS:
* module internal APIs
*/
public:
/**
* return session with given name
* if no existing session matches, create new one with this name
* @param name session name
*/
KateSession::Ptr giveSession(const QString &name);
/**
* Try to delete the @p session and removes the session from sessions list
......@@ -201,19 +174,6 @@ private Q_SLOTS:
void updateSessionList();
private:
/**
* return session with given name
* if no existing session matches, create new one with this name
* @param name session name
*/
KateSession::Ptr giveSession(const QString &name);
/**
* Create new session from defaults
* @param name session name
*/
KateSession::Ptr createSession(const QString &name);
/**
* Ask the user for a new session name, when needed.
* @param session is the session to rename or copy
......@@ -236,14 +196,14 @@ private:
QString sessionFileForName(const QString &name) const;
/**
* returns session file for anonymous session
* @return true when @p session is active in any Kate instance, otherwise false
*/
QString anonymousSessionFile() const;
bool sessionIsActive(const QString &session);
/**
* returns session file for anonymous session
*/
QString defaultSessionFile() const;
QString anonymousSessionFile() const;
/**
* helper function to save the session to a given config object
......@@ -253,7 +213,7 @@ private:
/**
* restore sessions documents, windows, etc...
*/
void loadSession(const KateSession::Ptr &session, bool loadDocs = true) const;
void loadSession(const KateSession::Ptr &session) const;
/**
* Writes sessions as jump list actions to the kate.desktop file
......
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