Session * SessionManager::saveSessionAs( const QString & name )
{
    // TODO:
    // Maybe use a different data structure for Session instead of QSettings?
    // A new class that would allow closing without saving would be nice.

    if (mSession) {
        delete mSession;
        mSession = 0;
    }

    QDir dir = sessionsDir();
    if (dir.path().isEmpty()) {
        emit switchSessionRequest(0);
        return 0;
    }

    QString sessionFile = dir.filePath(name + ".yaml");
    mSession = new Session( sessionFile, name, Settings::serializationFormat() );

    emit saveSessionRequest(mSession);

    mSession->sync();

    saveLastSession( dir, sessionFile );

    emit currentSessionNameChanged();

    return mSession;
}
QStringList SessionManager::availableSessions()
{
    QStringList sessions = sessionsDir().entryList();
    QStringList::iterator it;
    for ( it = sessions.begin(); it != sessions.end(); ++it )
        *it = QFileInfo(*it).baseName();
    return sessions;
}
void SessionManager::newSession()
{
    closeSession();

    QDir dir = sessionsDir();
    if (!dir.path().isEmpty())
        saveLastSession( dir, QString() );

    emit switchSessionRequest(0);
}
QString SessionManager::lastSession()
{
    QDir dir = sessionsDir();
    if (dir.path().isEmpty())
        return QString();

    QString path = QFile::symLinkTarget( dir.filePath( ".last-session.lnk" ) );

    return QFileInfo(path).baseName();
}
void SessionManager::renameSession( const QString & oldName, const QString & newName )
{
    if (mCurrentSession && mCurrentSession->name() == oldName) {
        saveSessionAs(newName);
        removeSession(oldName);
    } else {
        QDir dir = sessionsDir();
        if (dir.path().isEmpty())
            return;

        if (!dir.rename(oldName + ".yaml", newName + ".yaml"))
            qWarning("Could not rename session file!");
    }
}
void SessionManager::removeSession( const QString & name )
{
    QDir dir = sessionsDir();
    if (dir.path().isEmpty())
        return;

    if (mCurrentSession && mCurrentSession->name() == name) {
        if (!closeSession())
          return;
        saveLastSession(dir, QString());
        emit switchSessionRequest(0);
    }

    if (!QFile::remove(dir.filePath(name + ".yaml")))
        qWarning("Could not remove a session file!");
}
Session *SessionManager::openSession( const QString & name )
{
    // NOTE: This will create a session if it doesn't exists

    closeSession();

    QDir dir = sessionsDir();
    if (dir.path().isEmpty())
        return 0;

    QString sessionFile = dir.filePath(name + ".yaml");
    mSession = new Session( sessionFile, name, Settings::serializationFormat() );

    saveLastSession( dir, sessionFile );

    emit switchSessionRequest(mSession);

    return mSession;
}
Beispiel #8
0
void KateSessionManager::activateSession(KateSession::Ptr session, bool closeLast, bool saveLast, bool loadNew)
{
    // don't reload.
    // ### comparing the pointers directly is b0rk3d :(
    if(!session->sessionName().isEmpty() && session->sessionName() == m_activeSession->sessionName())
        return;
    // try to close last session
    if(closeLast)
    {
        if(KateApp::self()->activeMainWindow())
        {
            if(!KateApp::self()->activeMainWindow()->queryClose_internal())
                return;
        }
    }

    // save last session or not?
    if(saveLast)
        saveActiveSession(true);

    // really close last
    if(closeLast)
    {
        KateDocManager::self()->closeAllDocuments();
    }

    // set the new session
    m_activeSession = session;

    if(loadNew)
    {
        // open the new session
        Kate::Document::setOpenErrorDialogsActivated(false);

        KConfig *sc = activeSession()->configRead();

        if(sc)
            KateApp::self()->documentManager()->restoreDocumentList(sc);

        // if we have no session config object, try to load the default
        // (anonymous/unnamed sessions)
        if(!sc)
            sc = new KSimpleConfig(sessionsDir() + "/default.katesession");

        // window config
        if(sc)
        {
            KConfig *c = KateApp::self()->config();
            c->setGroup("General");

            if(c->readBoolEntry("Restore Window Configuration", true))
            {
                // a new, named session, read settings of the default session.
                if(!sc->hasGroup("Open MainWindows"))
                    sc = new KSimpleConfig(sessionsDir() + "/default.katesession");

                sc->setGroup("Open MainWindows");
                unsigned int wCount = sc->readUnsignedNumEntry("Count", 1);

                for(unsigned int i = 0; i < wCount; ++i)
                {
                    if(i >= KateApp::self()->mainWindows())
                    {
                        KateApp::self()->newMainWindow(sc, QString("MainWindow%1").arg(i));
                    }
                    else
                    {
                        sc->setGroup(QString("MainWindow%1").arg(i));
                        KateApp::self()->mainWindow(i)->readProperties(sc);
                    }
                }

                if(wCount > 0)
                {
                    while(wCount < KateApp::self()->mainWindows())
                    {
                        KateMainWindow *w = KateApp::self()->mainWindow(KateApp::self()->mainWindows() - 1);
                        KateApp::self()->removeMainWindow(w);
                        delete w;
                    }
                }
            }
        }

        Kate::Document::setOpenErrorDialogsActivated(true);
    }
}