19 #include "katesession.h"
22 #include "katemainwindow.h"
23 #include "katedocmanager.h"
25 #include <kstandarddirs.h>
26 #include <tdelocale.h>
28 #include <kdirwatch.h>
29 #include <kinputdialog.h>
30 #include <kiconloader.h>
31 #include <tdemessagebox.h>
33 #include <kstdguiitem.h>
34 #include <kpushbutton.h>
35 #include <tdepopupmenu.h>
43 #include <tqdatetime.h>
57 const char *KS_COUNT =
"Count";
58 const char *KS_DOCCOUNT =
"Document count";
59 const char *KS_DOCLIST =
"Document list";
60 const char *KS_GENERAL =
"General";
61 const char *KS_NAME =
"Name";
62 const char *KS_OPENDOC =
"Open Documents";
63 const char *KS_READONLY =
"ReadOnly";
64 const char *KS_OPEN_MAINWINDOWS =
"Open MainWindows";
65 const char *KS_UNNAMED =
"Unnamed";
68 const char *KSM_DIR =
"kate/sessions";
69 const char *KSM_FILE =
"sessions.list";
70 const char *KSM_SESSIONS_COUNT =
"Sessions count";
71 const char *KSM_LAST_SESSION_ID =
"Last session id";
72 const char *KSM_SESSIONS_LIST =
"Sessions list";
75 const char *KAPP_GENERAL =
"General";
76 const char *KAPP_LAST_SESSION =
"Last Session";
77 const char *KAPP_STARTUP_SESSION =
"Startup Session";
78 const char *KAPP_NEW =
"new";
79 const char *KAPP_LAST =
"last";
80 const char *KAPP_MANUAL =
"manual";
81 const char *KAPP_SESSION_EXIT =
"Session Exit";
82 const char *KAPP_DISCARD =
"discard";
83 const char *KAPP_SAVE =
"save";
84 const char *KAPP_ASK =
"ask";
89 const TQString &fileName) :
90 m_manager(manager), m_sessionName(sessionName), m_filename(fileName),
91 m_readOnly(false), m_documents(), m_config(NULL)
98 m_manager(session.m_manager), m_sessionName(newSessionName), m_filename(),
99 m_readOnly(false), m_documents(session.m_documents), m_config(NULL)
102 if (session.m_config)
104 m_config =
new KSimpleConfig(m_filename);
105 session.m_config->copyTo(m_filename, m_config);
122 m_sessionName = sessionName.isEmpty() ? i18n(KS_UNNAMED) : sessionName;
128 return m_filename.isEmpty() && m_sessionName == i18n(KS_UNNAMED);
139 if (TDEGlobal::dirs()->exists(m_filename))
142 m_config =
new KSimpleConfig(m_filename, m_readOnly);
143 m_config->setGroup(KS_GENERAL);
145 m_sessionName = m_config->readEntry(KS_NAME, i18n(KS_UNNAMED));
146 m_readOnly = m_config->readBoolEntry(KS_READONLY,
false);
148 if (m_config->hasGroup(KS_DOCLIST))
151 m_config->setGroup(KS_DOCLIST);
152 int docCount = m_config->readNumEntry(KS_DOCCOUNT, 0);
153 for (
int i = 0; i < docCount; ++i)
155 TQString urlStr = m_config->readEntry(TQString(
"URL_%1").arg(i));
156 if (!urlStr.isEmpty())
159 m_documents.append(urlStr);
167 m_config->setGroup(KS_OPENDOC);
168 int docCount = m_config->readNumEntry(KS_COUNT, 0);
169 for (
int i = 0; i < docCount; ++i)
171 m_config->setGroup(TQString(
"Document %1").arg(i));
172 TQString urlStr = m_config->readEntry(
"URL");
173 if (!urlStr.isEmpty())
176 m_documents.append(urlStr);
183 m_filename = TQString::null;
185 if (m_sessionName.isEmpty())
187 m_sessionName = i18n(KS_UNNAMED);
206 if (m_filename.isEmpty())
214 m_config =
new KSimpleConfig(m_filename);
217 if (m_config->hasGroup(KS_GENERAL))
219 m_config->deleteGroup(KS_GENERAL);
221 m_config->setGroup(KS_GENERAL);
222 m_config->writeEntry(KS_NAME, m_sessionName);
223 m_config->writeEntry(KS_READONLY, m_readOnly);
225 if (m_config->hasGroup(KS_DOCLIST))
227 m_config->deleteGroup(KS_DOCLIST);
229 m_config->setGroup(KS_DOCLIST);
230 m_config->writeEntry(KS_DOCCOUNT, m_documents.count());
231 for (
int i = 0; i < (int)m_documents.count(); ++i)
233 m_config->writeEntry(TQString(
"URL_%1").arg(i), m_documents[i]);
239 KateDocManager::self()->saveDocumentList(m_config);
242 m_config->setGroup(KS_OPEN_MAINWINDOWS);
243 m_config->writeEntry(KS_COUNT, mwCount);
244 for (
int i = 0; i < mwCount; ++i)
246 m_config->setGroup(TQString(
"MainWindow%1").arg(i));
257 if (KateDocManager::self()->documents() > 0)
259 KateDocManager::self()->closeAllDocuments();
262 Kate::Document::setOpenErrorDialogsActivated(
false);
268 if (m_config->hasGroup(KS_OPEN_MAINWINDOWS))
270 m_config->setGroup(KS_OPEN_MAINWINDOWS);
271 int mwCount = m_config->readUnsignedNumEntry(KS_COUNT, 1);
272 for (
int i = 0; i < mwCount; ++i)
280 m_config->setGroup(TQString(
"MainWindow%1").arg(i));
286 Kate::Document::setOpenErrorDialogsActivated(
true);
290 void KateSession::createFilename()
293 if (!m_filename.isEmpty())
305 tmpName = m_manager.
getBaseDir() + TQString(
"%1.katesession").arg(md5.hexDigest().data());
306 if (!TDEGlobal::dirs()->exists(tmpName))
308 m_filename = tmpName;
323 if (!KateSessionManager::ksm_instance)
327 return KateSessionManager::ksm_instance;
331 KateSessionManager::KateSessionManager() :
332 m_baseDir(locateLocal(
"data", KSM_DIR)+
"/"), m_configFile(m_baseDir + KSM_FILE),
333 m_activeSessionId(INVALID_SESSION), m_lastSessionId(INVALID_SESSION), m_sessions(),
334 m_config(NULL), m_startupOption(STARTUP_NEW), m_switchOption(SWITCH_DISCARD)
340 m_sessions.setAutoDelete(
true);
341 int sessionsCount = 0;
342 if (TDEGlobal::dirs()->exists(m_configFile))
345 m_config =
new KSimpleConfig(m_configFile);
346 m_config->setGroup(KSM_SESSIONS_LIST);
347 sessionsCount = m_config->readNumEntry(KSM_SESSIONS_COUNT, 0);
348 m_lastSessionId = m_config->readNumEntry(KSM_LAST_SESSION_ID, INVALID_SESSION);
349 for (
int i = 0; i < sessionsCount; ++i)
351 TQString urlStr = m_config->readEntry(TQString(
"URL_%1").arg(i));
352 if (!urlStr.isEmpty() && TDEGlobal::dirs()->exists(urlStr))
355 m_sessions.append(
new KateSession(*
this, TQString::null, urlStr));
363 TQDir sessionDir(m_baseDir,
"*.katesession");
364 for (
unsigned int i = 0; i < sessionDir.count(); ++i)
366 m_sessions.append(
new KateSession(*
this, TQString::null, m_baseDir+sessionDir[i]));
369 sessionsCount = (int)m_sessions.count();
370 if (sessionsCount == 0)
372 m_sessions.append(
new KateSession(*
this, TQString::null, TQString::null));
374 if (m_lastSessionId < 0 || m_lastSessionId >= (
int)m_sessions.count())
399 kateCfg->setGroup(KAPP_GENERAL);
401 if (optionType == SO_STARTUP || optionType == SO_ALL)
403 if (kateCfg->hasKey(KAPP_LAST_SESSION))
406 kateCfg->deleteEntry(KAPP_LAST_SESSION);
408 TQString startupOption(kateCfg->readEntry(KAPP_STARTUP_SESSION, KAPP_MANUAL));
409 if (startupOption == KAPP_LAST)
411 m_startupOption = STARTUP_LAST;
413 else if (startupOption == KAPP_NEW)
415 m_startupOption = STARTUP_NEW;
419 m_startupOption = STARTUP_MANUAL;
423 if (optionType == SO_SWITCH || optionType == SO_ALL)
425 TQString switchOption(kateCfg->readEntry(KAPP_SESSION_EXIT, KAPP_ASK));
426 if (switchOption == KAPP_DISCARD)
428 m_switchOption = SWITCH_DISCARD;
430 else if (switchOption == KAPP_SAVE)
432 m_switchOption = SWITCH_SAVE;
436 m_switchOption = SWITCH_ASK;
445 kateCfg->setGroup(KAPP_GENERAL);
446 if (optionType == SO_STARTUP || optionType == SO_ALL)
448 if (m_startupOption == STARTUP_LAST)
450 kateCfg->writeEntry(KAPP_STARTUP_SESSION, KAPP_LAST);
452 else if (m_startupOption == STARTUP_NEW)
454 kateCfg->writeEntry(KAPP_STARTUP_SESSION, KAPP_NEW);
458 kateCfg->writeEntry(KAPP_STARTUP_SESSION, KAPP_MANUAL);
462 if (optionType == SO_SWITCH || optionType == SO_ALL)
464 if (m_switchOption == SWITCH_DISCARD)
466 kateCfg->writeEntry(KAPP_SESSION_EXIT, KAPP_DISCARD);
468 else if (m_switchOption == SWITCH_SAVE)
470 kateCfg->writeEntry(KAPP_SESSION_EXIT, KAPP_SAVE);
474 kateCfg->writeEntry(KAPP_SESSION_EXIT, KAPP_ASK);
491 for (
int i = 0; i < (int)m_sessions.count(); ++i)
493 const TQString &filename = m_sessions[i]->getSessionFilename();
494 if (filename != TQString::null && TQFile::exists(filename))
496 TQFile::remove(filename);
501 m_activeSessionId = INVALID_SESSION;
506 m_config =
new KSimpleConfig(m_configFile);
508 if (m_config->hasGroup(KSM_SESSIONS_LIST))
510 m_config->deleteGroup(KSM_SESSIONS_LIST);
512 m_config->setGroup(KSM_SESSIONS_LIST);
513 m_config->writeEntry(KSM_SESSIONS_COUNT, m_sessions.count());
514 m_config->writeEntry(KSM_LAST_SESSION_ID, m_activeSessionId);
515 for (
int i = 0; i < (int)m_sessions.count(); ++i)
518 m_config->writeEntry(TQString(
"URL_%1").arg(i), m_sessions[i]->getSessionFilename());
527 return m_startupOption;
534 return m_switchOption;
540 m_switchOption = (option == SWITCH_DISCARD || option == SWITCH_SAVE) ? option : SWITCH_ASK;
548 if (sessionId < 0 || sessionId >= (
int)m_sessions.count())
550 return TQString::null;
553 return m_sessions[sessionId]->getSessionName();
559 if (sessionId < 0 || sessionId >= (
int)m_sessions.count())
564 return m_sessions[sessionId];
571 return INVALID_SESSION;
573 for (
int i = 0; i < (int)m_sessions.count(); ++i)
579 return INVALID_SESSION;
585 if (sessionId < 0 || sessionId >= (
int)m_sessions.count())
590 if (sessionId == m_activeSessionId)
595 int oldSessionId = m_activeSessionId;
596 if (m_activeSessionId != INVALID_SESSION)
602 if (!
KateApp::self()->activeMainWindow()->queryClose_internal())
612 const TQString &filename = m_sessions[m_activeSessionId]->getSessionFilename();
613 if (filename != TQString::null && TQFile::exists(filename))
615 TQFile::remove(filename);
617 m_sessions.remove(m_activeSessionId);
618 m_activeSessionId = INVALID_SESSION;
619 if (sessionId > oldSessionId)
624 oldSessionId = INVALID_SESSION;
628 m_activeSessionId = sessionId;
629 m_sessions[sessionId]->activate();
630 m_lastSessionId = INVALID_SESSION;
638 m_sessions.append(
new KateSession(*
this, sessionName, TQString::null));
639 int newSessionId = m_sessions.count() - 1;
648 if (sessionId < 0 || sessionId >= (
int)m_sessions.count())
650 return INVALID_SESSION;
653 m_sessions.append(
new KateSession(*m_sessions[sessionId], sessionName));
654 int newSessionId = m_sessions.count() - 1;
659 saveSession(newSessionId, sessionId == m_activeSessionId);
660 if (sessionId == m_activeSessionId)
667 activateSession(newSessionId, m_activeSessionId != INVALID_SESSION && !deleteCurr);
675 if (m_activeSessionId != INVALID_SESSION)
685 if (sessionId < 0 || sessionId >= (
int)m_sessions.count())
689 m_sessions[sessionId]->save(saveGUIInfo, setReadOnly);
696 if (sessionId < 0 || sessionId >= (
int)m_sessions.count())
702 const TQString &filename = m_sessions[sessionId]->getSessionFilename();
703 if (filename != TQString::null && TQFile::exists(filename))
705 TQFile::remove(filename);
708 m_sessions.remove(sessionId);
709 if (m_activeSessionId > sessionId)
713 else if (m_activeSessionId == sessionId)
715 m_activeSessionId = INVALID_SESSION;
718 if (m_activeSessionId == INVALID_SESSION)
720 if (m_sessions.count() > 0 && actSessId >= 0 && actSessId < (
int)m_sessions.count())
736 if (sessionId1 < 0 || sessionId1 >= (
int)m_sessions.count() ||
737 sessionId2 < 0 || sessionId2 >= (
int)m_sessions.count() ||
738 sessionId1 == sessionId2)
744 if (sessionId1 < sessionId2)
757 m_sessions.insert(idxMin, sessMax);
758 m_sessions.insert(idxMax, sessMin);
759 if (m_activeSessionId == sessionId1)
761 m_activeSessionId = sessionId2;
763 else if (m_activeSessionId == sessionId2)
765 m_activeSessionId = sessionId1;
774 if (sessionId < 0 || sessionId >= ((
int)m_sessions.count() - 1))
785 if (sessionId < 1 || sessionId >= (
int)m_sessions.count())
796 if (sessionId < 0 || sessionId >= (
int)m_sessions.count())
801 m_sessions[sessionId]->setSessionName(newSessionName);
808 if (sessionId < 0 || sessionId >= (
int)m_sessions.count())
813 m_sessions[sessionId]->setReadOnly(readOnly);
815 saveSession(sessionId, sessionId == m_activeSessionId,
true);
822 KateSessionChooser::KateSessionChooser(TQWidget *parent)
823 : KDialogBase(parent,
"", true, i18n(
"Session Chooser"),
824 KDialogBase::User1 | KDialogBase::User2 | KDialogBase::User3, KDialogBase::User2,
825 true, KStdGuiItem::quit(), KGuiItem(i18n(
"Open Session"),
"document-open"),
826 KGuiItem(i18n(
"New Session"),
"document-new")), m_listview(NULL)
828 TQHBox *page =
new TQHBox(
this);
829 page->setMinimumSize(400, 200);
832 TQHBox *hb =
new TQHBox(page);
833 hb->setSpacing(KDialog::spacingHint());
835 TQLabel *label =
new TQLabel(hb);
836 label->setPixmap(UserIcon(
"sessionchooser"));
837 label->setFrameStyle (TQFrame::Panel | TQFrame::Sunken);
839 TQVBox *vb =
new TQVBox(hb);
840 vb->setSpacing (KDialog::spacingHint());
842 m_listview =
new TDEListView(vb);
843 m_listview->addColumn(i18n(
"Session Name"));
844 m_listview->addColumn(i18n(
"Open Documents"));
845 m_listview->setSelectionMode(TQListView::Single);
846 m_listview->setAllColumnsShowFocus(
true);
847 m_listview->setSorting(-1);
848 m_listview->setResizeMode(TQListView::LastColumn);
850 connect (m_listview, TQ_SIGNAL(selectionChanged()),
this, TQ_SLOT(slotSelectionChanged()));
851 connect (m_listview, TQ_SIGNAL(executed(TQListViewItem*)),
this, TQ_SLOT(slotUser2()));
854 for (
int idx = sessions.count()-1; idx >= 0; --idx)
856 new KateSessionChooserItem(m_listview, sessions[idx]->
getSessionName(),
857 TQString(
"%1").arg(sessions[idx]->getDocCount()), idx);
860 setResult(RESULT_NO_OP);
861 slotSelectionChanged();
865 int KateSessionChooser::getSelectedSessionId()
867 KateSessionChooserItem *selectedItem =
dynamic_cast<KateSessionChooserItem*
>(m_listview->selectedItem());
869 return KateSessionManager::INVALID_SESSION;
871 return selectedItem->getSessionId();
875 void KateSessionChooser::slotUser1()
877 done(RESULT_QUIT_KATE);
881 void KateSessionChooser::slotUser2()
883 done(RESULT_OPEN_EXISTING);
887 void KateSessionChooser::slotUser3()
889 done(RESULT_OPEN_NEW);
893 void KateSessionChooser::slotSelectionChanged()
895 enableButton(KDialogBase::User2, m_listview->selectedItem());
899 #include "katesession.moc"
KateMainWindow * newMainWindow(TDEConfig *sconfig=0, const TQString &sgroup="")
window management
KateMainWindow * mainWindow(uint n)
give back the window you want
static KateApp * self()
static accessor to avoid casting ;)
KateDocManager * documentManager()
accessor to document manager
uint mainWindows() const
give back number of existing main windows
The Kate session manager.
void sessionCreated(int sessionId)
Emitted once a session has been created.
void updateSessionOptions(int optionType)
Updated the session startup and switch options.
void sessionRenamed(int sessionId)
Emitted once a session has been renamed.
TQPtrList< KateSession > & getSessionsList()
~KateSessionManager()
Destructor.
void saveSessionOptions(int optionType)
Save the session startup and switch options to the config file.
bool deleteSession(int sessionId, int actSessId)
void sessionDeleted(int sessionId)
Emitted once a session has been deleted.
void setSessionReadOnlyStatus(int sessionId, bool readOnly)
Set the read only status of the specified session.
int getSessionIdFromName(const TQString &name)
Return the session id of the first session whose name matches the provided one.
void switchOptionChanged()
Emitted when the session switch option has been set/changed.
static KateSessionManager * self()
get a pointer to the unique KateSessionManager instance.
void sessionActivated(int newSessionId, int oldSessionId)
Emitted once a session has been activated.
int newSession(const TQString &sessionName=TQString::null, bool saveCurr=true)
bool restoreLastSession()
Restore the last saved session.
void moveSessionBackward(int sessionId)
Move the specified session backward in the session list (by one position)
const TQString & getBaseDir() const
const int getSwitchOption()
void reloadActiveSession()
Restore the current active session to the last saved state.
const TQString & getSessionName(int sessionId)
void saveConfig(bool saveSessions)
Save session manager info.
const int getStartupOption()
void saveSession(int sessionId)
void swapSessionsPosition(int sessionId1, int sessionId2)
Swap the position of the two specified sessions in the session list.
void sessionsSwapped(int sessionIdMin, int sessionIdMax)
Emitted once the position of the two sessions have been swapped.
void moveSessionForward(int sessionId)
Move the specified session forward in the session list (by one position)
void sessionSaved(int sessionId)
Emitted once a session has been saved.
void renameSession(int sessionId, const TQString &newSessionName)
int cloneSession(int sessionId, const TQString &sessionName=TQString::null, bool activate=true, bool deleteCurr=false)
Create a new session and activate it if required.
KateSession * getSessionFromId(int sessionId)
bool activateSession(int sessionId, bool saveCurr=true)
Activate the selected session.
void setSwitchOption(int option)
Set the new session switch preference.
An object representing a Kate's session.
void save(bool saveGUIInfo, bool setReadOnly=false)
Save session info.
void load(bool includeGUIInfo)
Load session info from the saved file.
bool isStillVolatile() const
KateSession(const KateSessionManager &manager, const TQString &sessionName, const TQString &fileName)
create a new session and read the config from fileName if it exists
~KateSession()
Destructor.
void setSessionName(const TQString &sessionName)
Set the new session name.
void setReadOnly(bool readOnly)
Set session read only status.
void activate()
Activate the session.