• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kate
 

kate

katesession.cpp
00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Christoph Cullmann <cullmann@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "katesession.h"
00020 
00021 #include "kateapp.h"
00022 #include "katemainwindow.h"
00023 #include "katedocmanager.h"
00024 
00025 #include <kstandarddirs.h>
00026 #include <tdelocale.h>
00027 #include <kdebug.h>
00028 #include <kdirwatch.h>
00029 #include <kinputdialog.h>
00030 #include <kiconloader.h>
00031 #include <tdemessagebox.h>
00032 #include <kmdcodec.h>
00033 #include <kstdguiitem.h>
00034 #include <kpushbutton.h>
00035 #include <tdepopupmenu.h>
00036 
00037 #include <tqdir.h>
00038 #include <tqfile.h>
00039 #include <tqlabel.h>
00040 #include <tqlayout.h>
00041 #include <tqvbox.h>
00042 #include <tqhbox.h>
00043 #include <tqdatetime.h>
00044 #include <tqmap.h>
00045 
00046 #include <unistd.h>
00047 #include <time.h>
00048 
00049 // FIXME general: need to keep doc list and current session's m_documents in synchro
00050 //       all the time (doc open, doc closed, doc renamed).
00051 //       To be done when doc list software is developed
00052 
00053 // String constants
00054 namespace
00055 {
00056   // Kate session
00057   const char *KS_COUNT             = "Count";
00058   const char *KS_DOCCOUNT          = "Document count";
00059   const char *KS_DOCLIST           = "Document list";
00060   const char *KS_GENERAL           = "General";
00061   const char *KS_NAME              = "Name";
00062   const char *KS_OPENDOC           = "Open Documents";
00063   const char *KS_READONLY          = "ReadOnly";
00064   const char *KS_OPEN_MAINWINDOWS  = "Open MainWindows";
00065   const char *KS_UNNAMED           = "Unnamed";
00066 
00067   // Kate session manager
00068   const char *KSM_DIR              = "kate/sessions";
00069   const char *KSM_FILE             = "sessions.list";
00070   const char *KSM_SESSIONS_COUNT   = "Sessions count";
00071   const char *KSM_LAST_SESSION_ID  = "Last session id";
00072   const char *KSM_SESSIONS_LIST    = "Sessions list";
00073 
00074   // Kate app
00075   const char *KAPP_GENERAL         = "General";
00076   const char *KAPP_LAST_SESSION    = "Last Session";
00077   const char *KAPP_STARTUP_SESSION = "Startup Session";
00078   const char *KAPP_NEW             = "new";
00079   const char *KAPP_LAST            = "last";
00080   const char *KAPP_MANUAL          = "manual";
00081   const char *KAPP_SESSION_EXIT    = "Session Exit";
00082   const char *KAPP_DISCARD         = "discard";
00083   const char *KAPP_SAVE            = "save";
00084   const char *KAPP_ASK             = "ask";
00085 }
00086 
00087 //BEGIN Kate session
00088 KateSession::KateSession(const KateSessionManager &manager, const TQString &sessionName, 
00089                          const TQString &fileName) :
00090   m_manager(manager), m_sessionName(sessionName), m_filename(fileName),
00091   m_readOnly(false), m_documents(), m_config(NULL)
00092 {
00093   load(false);
00094 }
00095 
00096 //------------------------------------
00097 KateSession::KateSession(const KateSession &session, const TQString &newSessionName) :
00098   m_manager(session.m_manager), m_sessionName(newSessionName), m_filename(),
00099   m_readOnly(false), m_documents(session.m_documents), m_config(NULL)
00100 {
00101   createFilename();
00102   if (session.m_config)
00103   {
00104     m_config = new KSimpleConfig(m_filename);
00105     session.m_config->copyTo(m_filename, m_config);
00106     m_config->sync();
00107   }
00108 }
00109 
00110 //------------------------------------
00111 KateSession::~KateSession()
00112 {
00113   if (m_config)
00114   {
00115     delete m_config;
00116   }
00117 }
00118 
00119 //------------------------------------
00120 void KateSession::setSessionName(const TQString &sessionName)
00121 {
00122   m_sessionName = sessionName.isEmpty() ? i18n(KS_UNNAMED) : sessionName;
00123 }
00124 
00125 //------------------------------------
00126 bool KateSession::isStillVolatile() const
00127 { 
00128   return m_filename.isEmpty() && m_sessionName == i18n(KS_UNNAMED); 
00129 }
00130 
00131 //------------------------------------
00132 void KateSession::load(bool includeGUIInfo)
00133 {
00134   if (m_config)
00135   {
00136     delete m_config;
00137   }
00138   
00139   if (TDEGlobal::dirs()->exists(m_filename))
00140   {
00141     // Create config object if the session file already exists
00142     m_config = new KSimpleConfig(m_filename, m_readOnly);
00143     m_config->setGroup(KS_GENERAL);
00144     // Session general properties
00145     m_sessionName = m_config->readEntry(KS_NAME, i18n(KS_UNNAMED));
00146     m_readOnly = m_config->readBoolEntry(KS_READONLY, false);
00147     // Document list
00148     if (m_config->hasGroup(KS_DOCLIST))
00149     {
00150       // Read new style document list (from TDE R14.1.0)
00151       m_config->setGroup(KS_DOCLIST);
00152       int docCount = m_config->readNumEntry(KS_DOCCOUNT, 0);
00153       for (int i = 0;  i < docCount;  ++i)
00154       {
00155         TQString urlStr = m_config->readEntry(TQString("URL_%1").arg(i));
00156         if (!urlStr.isEmpty())
00157         {
00158           // Filter out empty URLs
00159           m_documents.append(urlStr);
00160         }
00161       }
00162     }
00163     else
00164     {
00165       // Create document list from old session configuration
00166       // to effortlessly import existing sessions
00167       m_config->setGroup(KS_OPENDOC);
00168       int docCount = m_config->readNumEntry(KS_COUNT, 0);
00169       for (int i = 0;  i < docCount;  ++i)
00170       {
00171         m_config->setGroup(TQString("Document %1").arg(i));
00172         TQString urlStr = m_config->readEntry("URL");
00173         if (!urlStr.isEmpty())
00174         {
00175           // Filter out empty URLs
00176           m_documents.append(urlStr);
00177         }
00178       }
00179     }
00180   }
00181   else
00182   {
00183     m_filename = TQString::null;
00184   }
00185   if (m_sessionName.isEmpty())
00186   {
00187     m_sessionName = i18n(KS_UNNAMED);
00188   }
00189     
00190   // Update all current documents if necessary
00191   if (includeGUIInfo)
00192   {
00193     activate();
00194   }
00195 }
00196 
00197 //------------------------------------
00198 void KateSession::save(bool saveGUIInfo, bool setReadOnly)
00199 {
00200   if (m_readOnly && !setReadOnly)
00201   {
00202     return;
00203   }
00204 
00205   // create a new session filename if needed
00206   if (m_filename.isEmpty())
00207   {
00208     createFilename();
00209   }
00210 
00211   // save session config info
00212   if (!m_config)
00213   {
00214     m_config = new KSimpleConfig(m_filename);
00215   }
00216 
00217   if (m_config->hasGroup(KS_GENERAL))
00218   {
00219     m_config->deleteGroup(KS_GENERAL);
00220   }
00221   m_config->setGroup(KS_GENERAL);
00222   m_config->writeEntry(KS_NAME, m_sessionName);
00223   m_config->writeEntry(KS_READONLY, m_readOnly);
00224 
00225   if (m_config->hasGroup(KS_DOCLIST))
00226   {
00227     m_config->deleteGroup(KS_DOCLIST);
00228   }
00229   m_config->setGroup(KS_DOCLIST);
00230   m_config->writeEntry(KS_DOCCOUNT, m_documents.count());
00231   for (int i = 0;  i < (int)m_documents.count();  ++i)
00232   {
00233     m_config->writeEntry(TQString("URL_%1").arg(i), m_documents[i]);
00234   }
00235 
00236   // save GUI elements info
00237   if (saveGUIInfo)
00238   {
00239     KateDocManager::self()->saveDocumentList(m_config);
00240     // save main windows info
00241     int mwCount = KateApp::self()->mainWindows();
00242     m_config->setGroup(KS_OPEN_MAINWINDOWS);
00243     m_config->writeEntry(KS_COUNT, mwCount);
00244     for (int i = 0;  i < mwCount;  ++i)
00245     {
00246       m_config->setGroup(TQString("MainWindow%1").arg(i));
00247       KateApp::self()->mainWindow(i)->saveProperties(m_config);
00248     }
00249   }
00250 
00251   m_config->sync();
00252 }
00253 
00254 //------------------------------------
00255 void KateSession::activate()
00256 {
00257   if (KateDocManager::self()->documents() > 0)
00258   {
00259     KateDocManager::self()->closeAllDocuments();
00260   }
00261   
00262   Kate::Document::setOpenErrorDialogsActivated(false);
00263   if (m_config)
00264   {
00265     KateApp::self()->documentManager()->restoreDocumentList(m_config);
00266     
00267     // load main windows info, if it exists
00268     if (m_config->hasGroup(KS_OPEN_MAINWINDOWS))
00269     {
00270       m_config->setGroup(KS_OPEN_MAINWINDOWS);
00271       int mwCount = m_config->readUnsignedNumEntry(KS_COUNT, 1);
00272       for (int i = 0;  i < mwCount;  ++i)
00273       {
00274         if (i >= (int)KateApp::self()->mainWindows())
00275         {
00276           KateApp::self()->newMainWindow(m_config, TQString("MainWindow%1").arg(i));
00277         }
00278         else
00279         {
00280           m_config->setGroup(TQString("MainWindow%1").arg(i));
00281           KateApp::self()->mainWindow(i)->readProperties(m_config);
00282         }
00283       }
00284     }
00285   }
00286   Kate::Document::setOpenErrorDialogsActivated(true);
00287 }
00288 
00289 //------------------------------------
00290 void KateSession::createFilename()
00291 {
00292   // create a new session filename if needed
00293   if (!m_filename.isEmpty())
00294   {
00295     return;
00296   }
00297   
00298   int s = time(0);
00299   TQCString tname;
00300   TQString tmpName;
00301   while (true)
00302   {
00303     tname.setNum(s++);
00304     KMD5 md5(tname);
00305     tmpName = m_manager.getBaseDir() + TQString("%1.katesession").arg(md5.hexDigest().data());
00306     if (!TDEGlobal::dirs()->exists(tmpName))
00307     {
00308       m_filename = tmpName;
00309       break;
00310     }
00311   }
00312 }
00313 //END Kate session
00314 
00315 
00316 //BEGIN KateSessionManager
00317 //------------------------------------
00318 KateSessionManager *KateSessionManager::ksm_instance = NULL;
00319 
00320 //------------------------------------
00321 KateSessionManager* KateSessionManager::self()
00322 {
00323   if (!KateSessionManager::ksm_instance)
00324   {
00325     KateSessionManager::ksm_instance = new KateSessionManager();
00326   }
00327   return KateSessionManager::ksm_instance;
00328 }
00329 
00330 //------------------------------------
00331 KateSessionManager::KateSessionManager() :
00332   m_baseDir(locateLocal("data", KSM_DIR)+"/"), m_configFile(m_baseDir + KSM_FILE),
00333   m_activeSessionId(INVALID_SESSION), m_lastSessionId(INVALID_SESSION), m_sessions(),
00334   m_config(NULL), m_startupOption(STARTUP_NEW), m_switchOption(SWITCH_DISCARD)
00335 {
00336   // Session startup and switch options
00337   updateSessionOptions(SO_ALL);
00338   
00339   // Sessions configuration
00340   m_sessions.setAutoDelete(true);
00341   int sessionsCount = 0;
00342   if (TDEGlobal::dirs()->exists(m_configFile))
00343   {
00344     // Read new style configuration (from TDE R14.1.0)
00345     m_config = new KSimpleConfig(m_configFile);
00346     m_config->setGroup(KSM_SESSIONS_LIST);
00347     sessionsCount = m_config->readNumEntry(KSM_SESSIONS_COUNT, 0);
00348     m_lastSessionId = m_config->readNumEntry(KSM_LAST_SESSION_ID, INVALID_SESSION);
00349     for (int i = 0;  i < sessionsCount;  ++i)
00350     {
00351       TQString urlStr = m_config->readEntry(TQString("URL_%1").arg(i));
00352       if (!urlStr.isEmpty() && TDEGlobal::dirs()->exists(urlStr))
00353       {
00354         // Filter out empty URLs or non existing sessions
00355         m_sessions.append(new KateSession(*this, TQString::null, urlStr));
00356       }
00357     }
00358   }
00359   else
00360   {
00361     // Create sessions list from session files
00362     // to effortlessly import existing sessions
00363     TQDir sessionDir(m_baseDir, "*.katesession");
00364     for (unsigned int i = 0;  i < sessionDir.count();  ++i)
00365     {
00366       m_sessions.append(new KateSession(*this, TQString::null, m_baseDir+sessionDir[i]));
00367     }
00368   }
00369   sessionsCount = (int)m_sessions.count();
00370   if (sessionsCount == 0)  // In the worst case, there is no valid session at all
00371   {
00372     m_sessions.append(new KateSession(*this, TQString::null, TQString::null));
00373   }
00374   if (m_lastSessionId < 0 || m_lastSessionId >= (int)m_sessions.count())
00375   {
00376     m_lastSessionId = 0;  // Invalid last session was detected. Use first in the list
00377   }
00378   
00379   //NOTE do not activate any session in the KateSessionManager costructor
00380   //     since Kate's main window may not be ready yet. The initial session
00381   //     will be activated by KateApp::startupKate() or KateApp::restoreKate()
00382 }
00383 
00384 //------------------------------------
00385 KateSessionManager::~KateSessionManager()
00386 {
00387   if (m_config)
00388   {
00389     delete m_config;
00390   }
00391   m_sessions.clear();
00392 }
00393 
00394 //------------------------------------
00395 void KateSessionManager::updateSessionOptions(int optionType)
00396 {
00397   // Session startup and switch options
00398   TDEConfig *kateCfg = KateApp::self()->config();
00399   kateCfg->setGroup(KAPP_GENERAL);
00400   
00401   if (optionType == SO_STARTUP || optionType == SO_ALL)
00402   {
00403     if (kateCfg->hasKey(KAPP_LAST_SESSION))
00404     {
00405       // Delete no longer used entry (pre R14.1.0)
00406       kateCfg->deleteEntry(KAPP_LAST_SESSION);
00407     }
00408     TQString startupOption(kateCfg->readEntry(KAPP_STARTUP_SESSION, KAPP_MANUAL));
00409     if (startupOption == KAPP_LAST)
00410     {
00411       m_startupOption = STARTUP_LAST;
00412     }
00413     else if (startupOption == KAPP_NEW)
00414     {
00415       m_startupOption = STARTUP_NEW;
00416     }
00417     else // startupOption == "manual"
00418     {
00419       m_startupOption = STARTUP_MANUAL;
00420     }
00421   }
00422   
00423   if (optionType == SO_SWITCH || optionType == SO_ALL)
00424   {
00425     TQString switchOption(kateCfg->readEntry(KAPP_SESSION_EXIT, KAPP_ASK));
00426     if (switchOption == KAPP_DISCARD)
00427     {
00428       m_switchOption = SWITCH_DISCARD;
00429     }
00430     else if (switchOption == KAPP_SAVE)
00431     {
00432       m_switchOption = SWITCH_SAVE;
00433     }
00434     else // switchOption == "ask"
00435     {
00436       m_switchOption = SWITCH_ASK;
00437     }
00438   }
00439 }
00440 
00441 //------------------------------------
00442 void KateSessionManager::saveSessionOptions(int optionType)
00443 {
00444   TDEConfig *kateCfg = KateApp::self()->config();
00445   kateCfg->setGroup(KAPP_GENERAL);
00446   if (optionType == SO_STARTUP || optionType == SO_ALL)
00447   {
00448     if (m_startupOption == STARTUP_LAST)
00449     {
00450       kateCfg->writeEntry(KAPP_STARTUP_SESSION, KAPP_LAST);
00451     }
00452     else if (m_startupOption == STARTUP_NEW)
00453     {
00454       kateCfg->writeEntry(KAPP_STARTUP_SESSION, KAPP_NEW);
00455     }
00456     else // m_startupOption == STARTUP_MANUAL
00457     {
00458       kateCfg->writeEntry(KAPP_STARTUP_SESSION, KAPP_MANUAL);
00459     }
00460   }
00461   
00462   if (optionType == SO_SWITCH || optionType == SO_ALL)
00463   {
00464     if (m_switchOption == SWITCH_DISCARD)
00465     {
00466       kateCfg->writeEntry(KAPP_SESSION_EXIT, KAPP_DISCARD);
00467     }
00468     else if (m_switchOption == SWITCH_SAVE)
00469     {
00470       kateCfg->writeEntry(KAPP_SESSION_EXIT, KAPP_SAVE);
00471     }
00472     else // m_switchOption == SWITCH_ASK
00473     {
00474       kateCfg->writeEntry(KAPP_SESSION_EXIT, KAPP_ASK);
00475     }
00476   }
00477   kateCfg->sync();
00478 }
00479 
00480 //------------------------------------
00481 void KateSessionManager::saveConfig(bool saveSessions)
00482 {
00483   // Session startup and switch options
00484   updateSessionOptions(SO_ALL);
00485   saveSessionOptions(SO_ALL);
00486   
00487   // Sessions configuration
00488   if (!saveSessions)
00489   {
00490     // delete all session files if they exist
00491     for (int i = 0;  i < (int)m_sessions.count();  ++i)
00492     {
00493       const TQString &filename = m_sessions[i]->getSessionFilename();
00494       if (filename != TQString::null && TQFile::exists(filename))
00495       {
00496         TQFile::remove(filename);
00497       }
00498     }
00499   
00500     m_sessions.clear();
00501     m_activeSessionId = INVALID_SESSION;
00502   }
00503   
00504   if (!m_config)
00505   {
00506     m_config = new KSimpleConfig(m_configFile);
00507   }
00508   if (m_config->hasGroup(KSM_SESSIONS_LIST))
00509   {
00510     m_config->deleteGroup(KSM_SESSIONS_LIST);
00511   }
00512   m_config->setGroup(KSM_SESSIONS_LIST);
00513   m_config->writeEntry(KSM_SESSIONS_COUNT, m_sessions.count());
00514   m_config->writeEntry(KSM_LAST_SESSION_ID, m_activeSessionId);
00515   for (int i = 0;  i < (int)m_sessions.count();  ++i)
00516   {
00517     saveSession(i, false, false);
00518     m_config->writeEntry(TQString("URL_%1").arg(i), m_sessions[i]->getSessionFilename());
00519   }
00520   m_config->sync();
00521 }
00522 
00523 //------------------------------------
00524 const int KateSessionManager::getStartupOption()
00525 {
00526   updateSessionOptions(SO_STARTUP);
00527   return m_startupOption;
00528 }
00529 
00530 //------------------------------------
00531 const int KateSessionManager::getSwitchOption()
00532 {
00533   updateSessionOptions(SO_SWITCH);
00534   return m_switchOption;
00535 }
00536 
00537 //------------------------------------
00538 void KateSessionManager::setSwitchOption(int option)
00539 { 
00540   m_switchOption = (option == SWITCH_DISCARD || option == SWITCH_SAVE) ? option : SWITCH_ASK; 
00541   saveSessionOptions(SO_SWITCH);
00542   emit switchOptionChanged();
00543 }
00544 
00545 //------------------------------------
00546 const TQString& KateSessionManager::getSessionName(int sessionId)
00547 {
00548   if (sessionId < 0 || sessionId >= (int)m_sessions.count())
00549   {
00550     return TQString::null;
00551   }
00552 
00553   return m_sessions[sessionId]->getSessionName();
00554 }
00555 
00556 //------------------------------------
00557 KateSession* KateSessionManager::getSessionFromId(int sessionId)
00558 {
00559   if (sessionId < 0 || sessionId >= (int)m_sessions.count())
00560   {
00561     return NULL;
00562   }
00563 
00564   return m_sessions[sessionId];
00565 }
00566 
00567 //------------------------------------
00568 int KateSessionManager::getSessionIdFromName(const TQString &name)
00569 {
00570   if (name.isEmpty())
00571     return INVALID_SESSION;
00572 
00573   for (int i = 0;  i < (int)m_sessions.count();  ++i)
00574   {
00575     if (m_sessions[i]->getSessionName() == name)
00576       return i;
00577   }
00578 
00579   return INVALID_SESSION;
00580 }
00581 
00582 //------------------------------------
00583 bool KateSessionManager::activateSession(int sessionId, bool saveCurr)
00584 {
00585   if (sessionId < 0 || sessionId >= (int)m_sessions.count())
00586   {
00587     return false;
00588   }
00589 
00590   if (sessionId == m_activeSessionId)
00591   {
00592     return true;
00593   }
00594 
00595   int oldSessionId = m_activeSessionId;
00596   if (m_activeSessionId != INVALID_SESSION)
00597   {
00598     // Do this only if a session has already been activated earlier,
00599     if (KateApp::self()->activeMainWindow())
00600     {
00601       // First check if all documents can be closed safely
00602       if (!KateApp::self()->activeMainWindow()->queryClose_internal())
00603         return false;
00604     }
00605     if (saveCurr) 
00606     {
00607       saveSession(m_activeSessionId, true);
00608     }
00609     else
00610     {
00611       // Delete current session before activating the new one
00612       const TQString &filename = m_sessions[m_activeSessionId]->getSessionFilename();
00613       if (filename != TQString::null && TQFile::exists(filename))
00614       {
00615         TQFile::remove(filename);
00616       }
00617       m_sessions.remove(m_activeSessionId);  // this also deletes the KateSession item since auto-deletion is enabled
00618       m_activeSessionId = INVALID_SESSION;
00619       if (sessionId > oldSessionId)
00620       {
00621         --sessionId;
00622       }
00623       emit sessionDeleted(oldSessionId);
00624       oldSessionId = INVALID_SESSION;
00625     }
00626   }
00627 
00628   m_activeSessionId = sessionId;
00629   m_sessions[sessionId]->activate();
00630   m_lastSessionId = INVALID_SESSION;
00631   emit sessionActivated(m_activeSessionId, oldSessionId);
00632   return true;
00633 }
00634 
00635 //------------------------------------
00636 int KateSessionManager::newSession(const TQString &sessionName, bool saveCurr)
00637 {
00638   m_sessions.append(new KateSession(*this, sessionName, TQString::null));
00639   int newSessionId = m_sessions.count() - 1;
00640   emit sessionCreated(newSessionId);
00641   activateSession(newSessionId, saveCurr);
00642   return newSessionId;
00643 }
00644 
00645 //------------------------------------
00646 int KateSessionManager::cloneSession(int sessionId, const TQString &sessionName, bool activate, bool deleteCurr)
00647 {
00648   if (sessionId < 0 || sessionId >= (int)m_sessions.count())
00649   {
00650     return INVALID_SESSION;
00651   }
00652 
00653   m_sessions.append(new KateSession(*m_sessions[sessionId], sessionName));
00654   int newSessionId = m_sessions.count() - 1;
00655   emit sessionCreated(newSessionId);
00656   
00657   // If cloning the active session, the new session will contain the current status
00658   // and the original session will be restored to the last saved state (save as... functionality)
00659   saveSession(newSessionId, sessionId == m_activeSessionId);
00660   if (sessionId == m_activeSessionId)
00661   {
00662     reloadActiveSession();
00663   }
00664   
00665   if (activate)
00666   {
00667     activateSession(newSessionId, m_activeSessionId != INVALID_SESSION && !deleteCurr);
00668   }
00669   return newSessionId;
00670 }
00671 
00672 //------------------------------------
00673 bool KateSessionManager::restoreLastSession()
00674 {
00675   if (m_activeSessionId != INVALID_SESSION)
00676   {
00677     return false;
00678   }
00679   return activateSession(m_lastSessionId, false);
00680 }
00681 
00682 //-------------------------------------------
00683 void KateSessionManager::saveSession(int sessionId, bool saveGUIInfo, bool setReadOnly)
00684 {
00685   if (sessionId < 0 || sessionId >= (int)m_sessions.count())
00686   {
00687     return;
00688   }
00689   m_sessions[sessionId]->save(saveGUIInfo, setReadOnly);
00690   emit sessionSaved(sessionId);
00691 }
00692 
00693 //-------------------------------------------
00694 bool KateSessionManager::deleteSession(int sessionId, int actSessId)
00695 {
00696   if (sessionId < 0 || sessionId >= (int)m_sessions.count())
00697   {
00698     return false;
00699   }
00700 
00701   // delete session file if it exists
00702   const TQString &filename = m_sessions[sessionId]->getSessionFilename();
00703   if (filename != TQString::null && TQFile::exists(filename))
00704   {
00705     TQFile::remove(filename);
00706   }
00707   // delete session
00708   m_sessions.remove(sessionId);  // this also deletes the KateSession item since auto-deletion is enabled
00709   if (m_activeSessionId > sessionId)
00710   {
00711     --m_activeSessionId;
00712   }
00713   else if (m_activeSessionId == sessionId)
00714   {
00715     m_activeSessionId = INVALID_SESSION;
00716   }
00717   emit sessionDeleted(sessionId);
00718   if (m_activeSessionId == INVALID_SESSION)
00719   {
00720     if (m_sessions.count() > 0 && actSessId >= 0 && actSessId < (int)m_sessions.count())
00721     {
00722       activateSession(actSessId, false);
00723     }
00724     else
00725     {
00726       newSession();
00727     }
00728   }
00729 
00730   return true;
00731 }
00732 
00733 //-------------------------------------------
00734 void KateSessionManager::swapSessionsPosition(int sessionId1, int sessionId2)
00735 {
00736   if (sessionId1 < 0 || sessionId1 >= (int)m_sessions.count() ||
00737       sessionId2 < 0 || sessionId2 >= (int)m_sessions.count() ||
00738       sessionId1 == sessionId2)
00739   {
00740     return;
00741   }
00742 
00743   int idxMin, idxMax;
00744   if (sessionId1 < sessionId2)
00745   {
00746     idxMin = sessionId1;
00747     idxMax = sessionId2;
00748   }
00749   else
00750   {
00751     idxMin = sessionId2;
00752     idxMax = sessionId1;
00753   }
00754 
00755   KateSession *sessMax = m_sessions.take(idxMax);
00756   KateSession *sessMin = m_sessions.take(idxMin);
00757   m_sessions.insert(idxMin, sessMax);
00758   m_sessions.insert(idxMax, sessMin);
00759   if (m_activeSessionId == sessionId1)
00760   {
00761     m_activeSessionId = sessionId2;
00762   }
00763   else if (m_activeSessionId == sessionId2)
00764   {
00765     m_activeSessionId = sessionId1;
00766   }
00767 
00768   emit sessionsSwapped(idxMin, idxMax);
00769 }
00770 
00771 //-------------------------------------------
00772 void KateSessionManager::moveSessionForward(int sessionId)
00773 {
00774   if (sessionId < 0 || sessionId >= ((int)m_sessions.count() - 1))
00775   {
00776     return;
00777   }
00778 
00779   swapSessionsPosition(sessionId, sessionId + 1);
00780 }
00781 
00782 //-------------------------------------------
00783 void KateSessionManager::moveSessionBackward(int sessionId)
00784 {
00785   if (sessionId < 1 || sessionId >= (int)m_sessions.count())
00786   {
00787     return;
00788   }
00789 
00790   swapSessionsPosition(sessionId, sessionId - 1);
00791 }
00792 
00793 //-------------------------------------------
00794 void KateSessionManager::renameSession(int sessionId, const TQString &newSessionName)
00795 {
00796   if (sessionId < 0 || sessionId >= (int)m_sessions.count())
00797   {
00798     return;
00799   }
00800 
00801   m_sessions[sessionId]->setSessionName(newSessionName);
00802   emit sessionRenamed(sessionId);
00803 }
00804 
00805 //-------------------------------------------
00806 void KateSessionManager::setSessionReadOnlyStatus(int sessionId, bool readOnly)
00807 {
00808   if (sessionId < 0 || sessionId >= (int)m_sessions.count())
00809   {
00810     return;
00811   }
00812 
00813   m_sessions[sessionId]->setReadOnly(readOnly);
00814   // Session is saved one last time when making it read only
00815   saveSession(sessionId, sessionId == m_activeSessionId, true);
00816 }
00817 //END KateSessionManager
00818 
00819 
00820 //BEGIN KateSessionChooser
00821 //-------------------------------------------
00822 KateSessionChooser::KateSessionChooser(TQWidget *parent)
00823  : KDialogBase(parent, "", true, i18n("Session Chooser"),
00824                KDialogBase::User1 | KDialogBase::User2 | KDialogBase::User3, KDialogBase::User2,
00825                true, KStdGuiItem::quit(), KGuiItem(i18n("Open Session"), "document-open"),
00826                KGuiItem(i18n("New Session"), "document-new")), m_listview(NULL)
00827 {
00828   TQHBox *page = new TQHBox(this);
00829   page->setMinimumSize(400, 200);
00830   setMainWidget(page);
00831 
00832   TQHBox *hb = new TQHBox(page);
00833   hb->setSpacing(KDialog::spacingHint());
00834 
00835   TQLabel *label = new TQLabel(hb);
00836   label->setPixmap(UserIcon("sessionchooser"));
00837   label->setFrameStyle (TQFrame::Panel | TQFrame::Sunken);
00838 
00839   TQVBox *vb = new TQVBox(hb);
00840   vb->setSpacing (KDialog::spacingHint());
00841 
00842   m_listview = new TDEListView(vb);
00843   m_listview->addColumn(i18n("Session Name"));
00844   m_listview->addColumn(i18n("Open Documents"));
00845   m_listview->setSelectionMode(TQListView::Single);
00846   m_listview->setAllColumnsShowFocus(true);
00847   m_listview->setSorting(-1);
00848   m_listview->setResizeMode(TQListView::LastColumn);
00849 
00850   connect (m_listview, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotSelectionChanged()));
00851   connect (m_listview, TQT_SIGNAL(executed(TQListViewItem*)), this, TQT_SLOT(slotUser2()));
00852 
00853   TQPtrList<KateSession> &sessions = KateSessionManager::self()->getSessionsList();
00854   for (int idx = sessions.count()-1;  idx >= 0;  --idx)
00855   {
00856     new KateSessionChooserItem(m_listview, sessions[idx]->getSessionName(),
00857                                TQString("%1").arg(sessions[idx]->getDocCount()), idx);
00858   }
00859 
00860   setResult(RESULT_NO_OP);
00861   slotSelectionChanged();  // update button status
00862 }
00863 
00864 //-------------------------------------------
00865 int KateSessionChooser::getSelectedSessionId()
00866 {
00867   KateSessionChooserItem *selectedItem = dynamic_cast<KateSessionChooserItem*>(m_listview->selectedItem());
00868   if (!selectedItem)
00869     return KateSessionManager::INVALID_SESSION;
00870 
00871   return selectedItem->getSessionId();
00872 }
00873 
00874 //-------------------------------------------
00875 void KateSessionChooser::slotUser1()
00876 {
00877   done(RESULT_QUIT_KATE);
00878 }
00879 
00880 //-------------------------------------------
00881 void KateSessionChooser::slotUser2()
00882 {
00883   done(RESULT_OPEN_EXISTING);
00884 }
00885 
00886 //-------------------------------------------
00887 void KateSessionChooser::slotUser3()
00888 {
00889   done(RESULT_OPEN_NEW);
00890 }
00891 
00892 //-------------------------------------------
00893 void KateSessionChooser::slotSelectionChanged()
00894 {
00895   enableButton(KDialogBase::User2, m_listview->selectedItem());
00896 }
00897 //END KateSessionChooser
00898 
00899 #include "katesession.moc"
00900 
00901 // kate: space-indent on; indent-width 2; replace-tabs on; mixed-indent off;

kate

Skip menu "kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kate

Skip menu "kate"
  • kate
  • libkonq
  • twin
  •   lib
Generated for kate by doxygen 1.7.6.1
This website is maintained by Timothy Pearson.