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

kate

  • kate
  • app
katesession.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2005 Christoph Cullmann <cullmann@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "katesession.h"
20 
21 #include "kateapp.h"
22 #include "katemainwindow.h"
23 #include "katedocmanager.h"
24 
25 #include <kstandarddirs.h>
26 #include <tdelocale.h>
27 #include <kdebug.h>
28 #include <kdirwatch.h>
29 #include <kinputdialog.h>
30 #include <kiconloader.h>
31 #include <tdemessagebox.h>
32 #include <kmdcodec.h>
33 #include <kstdguiitem.h>
34 #include <kpushbutton.h>
35 #include <tdepopupmenu.h>
36 
37 #include <tqdir.h>
38 #include <tqfile.h>
39 #include <tqlabel.h>
40 #include <tqlayout.h>
41 #include <tqvbox.h>
42 #include <tqhbox.h>
43 #include <tqdatetime.h>
44 #include <tqmap.h>
45 
46 #include <unistd.h>
47 #include <time.h>
48 
49 // FIXME general: need to keep doc list and current session's m_documents in synchro
50 // all the time (doc open, doc closed, doc renamed).
51 // To be done when doc list software is developed
52 
53 // String constants
54 namespace
55 {
56  // Kate session
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";
66 
67  // Kate session manager
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";
73 
74  // Kate app
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";
85 }
86 
87 //BEGIN Kate session
88 KateSession::KateSession(const KateSessionManager &manager, const TQString &sessionName,
89  const TQString &fileName) :
90  m_manager(manager), m_sessionName(sessionName), m_filename(fileName),
91  m_readOnly(false), m_documents(), m_config(NULL)
92 {
93  load(false);
94 }
95 
96 //------------------------------------
97 KateSession::KateSession(const KateSession &session, const TQString &newSessionName) :
98  m_manager(session.m_manager), m_sessionName(newSessionName), m_filename(),
99  m_readOnly(false), m_documents(session.m_documents), m_config(NULL)
100 {
101  createFilename();
102  if (session.m_config)
103  {
104  m_config = new KSimpleConfig(m_filename);
105  session.m_config->copyTo(m_filename, m_config);
106  m_config->sync();
107  }
108 }
109 
110 //------------------------------------
111 KateSession::~KateSession()
112 {
113  if (m_config)
114  {
115  delete m_config;
116  }
117 }
118 
119 //------------------------------------
120 void KateSession::setSessionName(const TQString &sessionName)
121 {
122  m_sessionName = sessionName.isEmpty() ? i18n(KS_UNNAMED) : sessionName;
123 }
124 
125 //------------------------------------
126 bool KateSession::isStillVolatile() const
127 {
128  return m_filename.isEmpty() && m_sessionName == i18n(KS_UNNAMED);
129 }
130 
131 //------------------------------------
132 void KateSession::load(bool includeGUIInfo)
133 {
134  if (m_config)
135  {
136  delete m_config;
137  }
138 
139  if (TDEGlobal::dirs()->exists(m_filename))
140  {
141  // Create config object if the session file already exists
142  m_config = new KSimpleConfig(m_filename, m_readOnly);
143  m_config->setGroup(KS_GENERAL);
144  // Session general properties
145  m_sessionName = m_config->readEntry(KS_NAME, i18n(KS_UNNAMED));
146  m_readOnly = m_config->readBoolEntry(KS_READONLY, false);
147  // Document list
148  if (m_config->hasGroup(KS_DOCLIST))
149  {
150  // Read new style document list (from TDE R14.1.0)
151  m_config->setGroup(KS_DOCLIST);
152  int docCount = m_config->readNumEntry(KS_DOCCOUNT, 0);
153  for (int i = 0; i < docCount; ++i)
154  {
155  TQString urlStr = m_config->readEntry(TQString("URL_%1").arg(i));
156  if (!urlStr.isEmpty())
157  {
158  // Filter out empty URLs
159  m_documents.append(urlStr);
160  }
161  }
162  }
163  else
164  {
165  // Create document list from old session configuration
166  // to effortlessly import existing sessions
167  m_config->setGroup(KS_OPENDOC);
168  int docCount = m_config->readNumEntry(KS_COUNT, 0);
169  for (int i = 0; i < docCount; ++i)
170  {
171  m_config->setGroup(TQString("Document %1").arg(i));
172  TQString urlStr = m_config->readEntry("URL");
173  if (!urlStr.isEmpty())
174  {
175  // Filter out empty URLs
176  m_documents.append(urlStr);
177  }
178  }
179  }
180  }
181  else
182  {
183  m_filename = TQString::null;
184  }
185  if (m_sessionName.isEmpty())
186  {
187  m_sessionName = i18n(KS_UNNAMED);
188  }
189 
190  // Update all current documents if necessary
191  if (includeGUIInfo)
192  {
193  activate();
194  }
195 }
196 
197 //------------------------------------
198 void KateSession::save(bool saveGUIInfo, bool setReadOnly)
199 {
200  if (m_readOnly && !setReadOnly)
201  {
202  return;
203  }
204 
205  // create a new session filename if needed
206  if (m_filename.isEmpty())
207  {
208  createFilename();
209  }
210 
211  // save session config info
212  if (!m_config)
213  {
214  m_config = new KSimpleConfig(m_filename);
215  }
216 
217  if (m_config->hasGroup(KS_GENERAL))
218  {
219  m_config->deleteGroup(KS_GENERAL);
220  }
221  m_config->setGroup(KS_GENERAL);
222  m_config->writeEntry(KS_NAME, m_sessionName);
223  m_config->writeEntry(KS_READONLY, m_readOnly);
224 
225  if (m_config->hasGroup(KS_DOCLIST))
226  {
227  m_config->deleteGroup(KS_DOCLIST);
228  }
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)
232  {
233  m_config->writeEntry(TQString("URL_%1").arg(i), m_documents[i]);
234  }
235 
236  // save GUI elements info
237  if (saveGUIInfo)
238  {
239  KateDocManager::self()->saveDocumentList(m_config);
240  // save main windows info
241  int mwCount = KateApp::self()->mainWindows();
242  m_config->setGroup(KS_OPEN_MAINWINDOWS);
243  m_config->writeEntry(KS_COUNT, mwCount);
244  for (int i = 0; i < mwCount; ++i)
245  {
246  m_config->setGroup(TQString("MainWindow%1").arg(i));
247  KateApp::self()->mainWindow(i)->saveProperties(m_config);
248  }
249  }
250 
251  m_config->sync();
252 }
253 
254 //------------------------------------
255 void KateSession::activate()
256 {
257  if (KateDocManager::self()->documents() > 0)
258  {
259  KateDocManager::self()->closeAllDocuments();
260  }
261 
262  Kate::Document::setOpenErrorDialogsActivated(false);
263  if (m_config)
264  {
265  KateApp::self()->documentManager()->restoreDocumentList(m_config);
266 
267  // load main windows info, if it exists
268  if (m_config->hasGroup(KS_OPEN_MAINWINDOWS))
269  {
270  m_config->setGroup(KS_OPEN_MAINWINDOWS);
271  int mwCount = m_config->readUnsignedNumEntry(KS_COUNT, 1);
272  for (int i = 0; i < mwCount; ++i)
273  {
274  if (i >= (int)KateApp::self()->mainWindows())
275  {
276  KateApp::self()->newMainWindow(m_config, TQString("MainWindow%1").arg(i));
277  }
278  else
279  {
280  m_config->setGroup(TQString("MainWindow%1").arg(i));
281  KateApp::self()->mainWindow(i)->readProperties(m_config);
282  }
283  }
284  }
285  }
286  Kate::Document::setOpenErrorDialogsActivated(true);
287 }
288 
289 //------------------------------------
290 void KateSession::createFilename()
291 {
292  // create a new session filename if needed
293  if (!m_filename.isEmpty())
294  {
295  return;
296  }
297 
298  int s = time(0);
299  TQCString tname;
300  TQString tmpName;
301  while (true)
302  {
303  tname.setNum(s++);
304  KMD5 md5(tname);
305  tmpName = m_manager.getBaseDir() + TQString("%1.katesession").arg(md5.hexDigest().data());
306  if (!TDEGlobal::dirs()->exists(tmpName))
307  {
308  m_filename = tmpName;
309  break;
310  }
311  }
312 }
313 //END Kate session
314 
315 
316 //BEGIN KateSessionManager
317 //------------------------------------
318 KateSessionManager *KateSessionManager::ksm_instance = NULL;
319 
320 //------------------------------------
321 KateSessionManager* KateSessionManager::self()
322 {
323  if (!KateSessionManager::ksm_instance)
324  {
325  KateSessionManager::ksm_instance = new KateSessionManager();
326  }
327  return KateSessionManager::ksm_instance;
328 }
329 
330 //------------------------------------
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)
335 {
336  // Session startup and switch options
337  updateSessionOptions(SO_ALL);
338 
339  // Sessions configuration
340  m_sessions.setAutoDelete(true);
341  int sessionsCount = 0;
342  if (TDEGlobal::dirs()->exists(m_configFile))
343  {
344  // Read new style configuration (from TDE R14.1.0)
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)
350  {
351  TQString urlStr = m_config->readEntry(TQString("URL_%1").arg(i));
352  if (!urlStr.isEmpty() && TDEGlobal::dirs()->exists(urlStr))
353  {
354  // Filter out empty URLs or non existing sessions
355  m_sessions.append(new KateSession(*this, TQString::null, urlStr));
356  }
357  }
358  }
359  else
360  {
361  // Create sessions list from session files
362  // to effortlessly import existing sessions
363  TQDir sessionDir(m_baseDir, "*.katesession");
364  for (unsigned int i = 0; i < sessionDir.count(); ++i)
365  {
366  m_sessions.append(new KateSession(*this, TQString::null, m_baseDir+sessionDir[i]));
367  }
368  }
369  sessionsCount = (int)m_sessions.count();
370  if (sessionsCount == 0) // In the worst case, there is no valid session at all
371  {
372  m_sessions.append(new KateSession(*this, TQString::null, TQString::null));
373  }
374  if (m_lastSessionId < 0 || m_lastSessionId >= (int)m_sessions.count())
375  {
376  m_lastSessionId = 0; // Invalid last session was detected. Use first in the list
377  }
378 
379  //NOTE do not activate any session in the KateSessionManager costructor
380  // since Kate's main window may not be ready yet. The initial session
381  // will be activated by KateApp::startupKate() or KateApp::restoreKate()
382 }
383 
384 //------------------------------------
385 KateSessionManager::~KateSessionManager()
386 {
387  if (m_config)
388  {
389  delete m_config;
390  }
391  m_sessions.clear();
392 }
393 
394 //------------------------------------
395 void KateSessionManager::updateSessionOptions(int optionType)
396 {
397  // Session startup and switch options
398  TDEConfig *kateCfg = KateApp::self()->config();
399  kateCfg->setGroup(KAPP_GENERAL);
400 
401  if (optionType == SO_STARTUP || optionType == SO_ALL)
402  {
403  if (kateCfg->hasKey(KAPP_LAST_SESSION))
404  {
405  // Delete no longer used entry (pre R14.1.0)
406  kateCfg->deleteEntry(KAPP_LAST_SESSION);
407  }
408  TQString startupOption(kateCfg->readEntry(KAPP_STARTUP_SESSION, KAPP_MANUAL));
409  if (startupOption == KAPP_LAST)
410  {
411  m_startupOption = STARTUP_LAST;
412  }
413  else if (startupOption == KAPP_NEW)
414  {
415  m_startupOption = STARTUP_NEW;
416  }
417  else // startupOption == "manual"
418  {
419  m_startupOption = STARTUP_MANUAL;
420  }
421  }
422 
423  if (optionType == SO_SWITCH || optionType == SO_ALL)
424  {
425  TQString switchOption(kateCfg->readEntry(KAPP_SESSION_EXIT, KAPP_ASK));
426  if (switchOption == KAPP_DISCARD)
427  {
428  m_switchOption = SWITCH_DISCARD;
429  }
430  else if (switchOption == KAPP_SAVE)
431  {
432  m_switchOption = SWITCH_SAVE;
433  }
434  else // switchOption == "ask"
435  {
436  m_switchOption = SWITCH_ASK;
437  }
438  }
439 }
440 
441 //------------------------------------
442 void KateSessionManager::saveSessionOptions(int optionType)
443 {
444  TDEConfig *kateCfg = KateApp::self()->config();
445  kateCfg->setGroup(KAPP_GENERAL);
446  if (optionType == SO_STARTUP || optionType == SO_ALL)
447  {
448  if (m_startupOption == STARTUP_LAST)
449  {
450  kateCfg->writeEntry(KAPP_STARTUP_SESSION, KAPP_LAST);
451  }
452  else if (m_startupOption == STARTUP_NEW)
453  {
454  kateCfg->writeEntry(KAPP_STARTUP_SESSION, KAPP_NEW);
455  }
456  else // m_startupOption == STARTUP_MANUAL
457  {
458  kateCfg->writeEntry(KAPP_STARTUP_SESSION, KAPP_MANUAL);
459  }
460  }
461 
462  if (optionType == SO_SWITCH || optionType == SO_ALL)
463  {
464  if (m_switchOption == SWITCH_DISCARD)
465  {
466  kateCfg->writeEntry(KAPP_SESSION_EXIT, KAPP_DISCARD);
467  }
468  else if (m_switchOption == SWITCH_SAVE)
469  {
470  kateCfg->writeEntry(KAPP_SESSION_EXIT, KAPP_SAVE);
471  }
472  else // m_switchOption == SWITCH_ASK
473  {
474  kateCfg->writeEntry(KAPP_SESSION_EXIT, KAPP_ASK);
475  }
476  }
477  kateCfg->sync();
478 }
479 
480 //------------------------------------
481 void KateSessionManager::saveConfig(bool saveSessions)
482 {
483  // Session startup and switch options
484  updateSessionOptions(SO_ALL);
485  saveSessionOptions(SO_ALL);
486 
487  // Sessions configuration
488  if (!saveSessions)
489  {
490  // delete all session files if they exist
491  for (int i = 0; i < (int)m_sessions.count(); ++i)
492  {
493  const TQString &filename = m_sessions[i]->getSessionFilename();
494  if (filename != TQString::null && TQFile::exists(filename))
495  {
496  TQFile::remove(filename);
497  }
498  }
499 
500  m_sessions.clear();
501  m_activeSessionId = INVALID_SESSION;
502  }
503 
504  if (!m_config)
505  {
506  m_config = new KSimpleConfig(m_configFile);
507  }
508  if (m_config->hasGroup(KSM_SESSIONS_LIST))
509  {
510  m_config->deleteGroup(KSM_SESSIONS_LIST);
511  }
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)
516  {
517  saveSession(i, false, false);
518  m_config->writeEntry(TQString("URL_%1").arg(i), m_sessions[i]->getSessionFilename());
519  }
520  m_config->sync();
521 }
522 
523 //------------------------------------
524 const int KateSessionManager::getStartupOption()
525 {
526  updateSessionOptions(SO_STARTUP);
527  return m_startupOption;
528 }
529 
530 //------------------------------------
531 const int KateSessionManager::getSwitchOption()
532 {
533  updateSessionOptions(SO_SWITCH);
534  return m_switchOption;
535 }
536 
537 //------------------------------------
538 void KateSessionManager::setSwitchOption(int option)
539 {
540  m_switchOption = (option == SWITCH_DISCARD || option == SWITCH_SAVE) ? option : SWITCH_ASK;
541  saveSessionOptions(SO_SWITCH);
542  emit switchOptionChanged();
543 }
544 
545 //------------------------------------
546 const TQString& KateSessionManager::getSessionName(int sessionId)
547 {
548  if (sessionId < 0 || sessionId >= (int)m_sessions.count())
549  {
550  return TQString::null;
551  }
552 
553  return m_sessions[sessionId]->getSessionName();
554 }
555 
556 //------------------------------------
557 KateSession* KateSessionManager::getSessionFromId(int sessionId)
558 {
559  if (sessionId < 0 || sessionId >= (int)m_sessions.count())
560  {
561  return NULL;
562  }
563 
564  return m_sessions[sessionId];
565 }
566 
567 //------------------------------------
568 int KateSessionManager::getSessionIdFromName(const TQString &name)
569 {
570  if (name.isEmpty())
571  return INVALID_SESSION;
572 
573  for (int i = 0; i < (int)m_sessions.count(); ++i)
574  {
575  if (m_sessions[i]->getSessionName() == name)
576  return i;
577  }
578 
579  return INVALID_SESSION;
580 }
581 
582 //------------------------------------
583 bool KateSessionManager::activateSession(int sessionId, bool saveCurr)
584 {
585  if (sessionId < 0 || sessionId >= (int)m_sessions.count())
586  {
587  return false;
588  }
589 
590  if (sessionId == m_activeSessionId)
591  {
592  return true;
593  }
594 
595  int oldSessionId = m_activeSessionId;
596  if (m_activeSessionId != INVALID_SESSION)
597  {
598  // Do this only if a session has already been activated earlier,
599  if (KateApp::self()->activeMainWindow())
600  {
601  // First check if all documents can be closed safely
602  if (!KateApp::self()->activeMainWindow()->queryClose_internal())
603  return false;
604  }
605  if (saveCurr)
606  {
607  saveSession(m_activeSessionId, true);
608  }
609  else
610  {
611  // Delete current session before activating the new one
612  const TQString &filename = m_sessions[m_activeSessionId]->getSessionFilename();
613  if (filename != TQString::null && TQFile::exists(filename))
614  {
615  TQFile::remove(filename);
616  }
617  m_sessions.remove(m_activeSessionId); // this also deletes the KateSession item since auto-deletion is enabled
618  m_activeSessionId = INVALID_SESSION;
619  if (sessionId > oldSessionId)
620  {
621  --sessionId;
622  }
623  emit sessionDeleted(oldSessionId);
624  oldSessionId = INVALID_SESSION;
625  }
626  }
627 
628  m_activeSessionId = sessionId;
629  m_sessions[sessionId]->activate();
630  m_lastSessionId = INVALID_SESSION;
631  emit sessionActivated(m_activeSessionId, oldSessionId);
632  return true;
633 }
634 
635 //------------------------------------
636 int KateSessionManager::newSession(const TQString &sessionName, bool saveCurr)
637 {
638  m_sessions.append(new KateSession(*this, sessionName, TQString::null));
639  int newSessionId = m_sessions.count() - 1;
640  emit sessionCreated(newSessionId);
641  activateSession(newSessionId, saveCurr);
642  return newSessionId;
643 }
644 
645 //------------------------------------
646 int KateSessionManager::cloneSession(int sessionId, const TQString &sessionName, bool activate, bool deleteCurr)
647 {
648  if (sessionId < 0 || sessionId >= (int)m_sessions.count())
649  {
650  return INVALID_SESSION;
651  }
652 
653  m_sessions.append(new KateSession(*m_sessions[sessionId], sessionName));
654  int newSessionId = m_sessions.count() - 1;
655  emit sessionCreated(newSessionId);
656 
657  // If cloning the active session, the new session will contain the current status
658  // and the original session will be restored to the last saved state (save as... functionality)
659  saveSession(newSessionId, sessionId == m_activeSessionId);
660  if (sessionId == m_activeSessionId)
661  {
662  reloadActiveSession();
663  }
664 
665  if (activate)
666  {
667  activateSession(newSessionId, m_activeSessionId != INVALID_SESSION && !deleteCurr);
668  }
669  return newSessionId;
670 }
671 
672 //------------------------------------
673 bool KateSessionManager::restoreLastSession()
674 {
675  if (m_activeSessionId != INVALID_SESSION)
676  {
677  return false;
678  }
679  return activateSession(m_lastSessionId, false);
680 }
681 
682 //-------------------------------------------
683 void KateSessionManager::saveSession(int sessionId, bool saveGUIInfo, bool setReadOnly)
684 {
685  if (sessionId < 0 || sessionId >= (int)m_sessions.count())
686  {
687  return;
688  }
689  m_sessions[sessionId]->save(saveGUIInfo, setReadOnly);
690  emit sessionSaved(sessionId);
691 }
692 
693 //-------------------------------------------
694 bool KateSessionManager::deleteSession(int sessionId, int actSessId)
695 {
696  if (sessionId < 0 || sessionId >= (int)m_sessions.count())
697  {
698  return false;
699  }
700 
701  // delete session file if it exists
702  const TQString &filename = m_sessions[sessionId]->getSessionFilename();
703  if (filename != TQString::null && TQFile::exists(filename))
704  {
705  TQFile::remove(filename);
706  }
707  // delete session
708  m_sessions.remove(sessionId); // this also deletes the KateSession item since auto-deletion is enabled
709  if (m_activeSessionId > sessionId)
710  {
711  --m_activeSessionId;
712  }
713  else if (m_activeSessionId == sessionId)
714  {
715  m_activeSessionId = INVALID_SESSION;
716  }
717  emit sessionDeleted(sessionId);
718  if (m_activeSessionId == INVALID_SESSION)
719  {
720  if (m_sessions.count() > 0 && actSessId >= 0 && actSessId < (int)m_sessions.count())
721  {
722  activateSession(actSessId, false);
723  }
724  else
725  {
726  newSession();
727  }
728  }
729 
730  return true;
731 }
732 
733 //-------------------------------------------
734 void KateSessionManager::swapSessionsPosition(int sessionId1, int sessionId2)
735 {
736  if (sessionId1 < 0 || sessionId1 >= (int)m_sessions.count() ||
737  sessionId2 < 0 || sessionId2 >= (int)m_sessions.count() ||
738  sessionId1 == sessionId2)
739  {
740  return;
741  }
742 
743  int idxMin, idxMax;
744  if (sessionId1 < sessionId2)
745  {
746  idxMin = sessionId1;
747  idxMax = sessionId2;
748  }
749  else
750  {
751  idxMin = sessionId2;
752  idxMax = sessionId1;
753  }
754 
755  KateSession *sessMax = m_sessions.take(idxMax);
756  KateSession *sessMin = m_sessions.take(idxMin);
757  m_sessions.insert(idxMin, sessMax);
758  m_sessions.insert(idxMax, sessMin);
759  if (m_activeSessionId == sessionId1)
760  {
761  m_activeSessionId = sessionId2;
762  }
763  else if (m_activeSessionId == sessionId2)
764  {
765  m_activeSessionId = sessionId1;
766  }
767 
768  emit sessionsSwapped(idxMin, idxMax);
769 }
770 
771 //-------------------------------------------
772 void KateSessionManager::moveSessionForward(int sessionId)
773 {
774  if (sessionId < 0 || sessionId >= ((int)m_sessions.count() - 1))
775  {
776  return;
777  }
778 
779  swapSessionsPosition(sessionId, sessionId + 1);
780 }
781 
782 //-------------------------------------------
783 void KateSessionManager::moveSessionBackward(int sessionId)
784 {
785  if (sessionId < 1 || sessionId >= (int)m_sessions.count())
786  {
787  return;
788  }
789 
790  swapSessionsPosition(sessionId, sessionId - 1);
791 }
792 
793 //-------------------------------------------
794 void KateSessionManager::renameSession(int sessionId, const TQString &newSessionName)
795 {
796  if (sessionId < 0 || sessionId >= (int)m_sessions.count())
797  {
798  return;
799  }
800 
801  m_sessions[sessionId]->setSessionName(newSessionName);
802  emit sessionRenamed(sessionId);
803 }
804 
805 //-------------------------------------------
806 void KateSessionManager::setSessionReadOnlyStatus(int sessionId, bool readOnly)
807 {
808  if (sessionId < 0 || sessionId >= (int)m_sessions.count())
809  {
810  return;
811  }
812 
813  m_sessions[sessionId]->setReadOnly(readOnly);
814  // Session is saved one last time when making it read only
815  saveSession(sessionId, sessionId == m_activeSessionId, true);
816 }
817 //END KateSessionManager
818 
819 
820 //BEGIN KateSessionChooser
821 //-------------------------------------------
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)
827 {
828  TQHBox *page = new TQHBox(this);
829  page->setMinimumSize(400, 200);
830  setMainWidget(page);
831 
832  TQHBox *hb = new TQHBox(page);
833  hb->setSpacing(KDialog::spacingHint());
834 
835  TQLabel *label = new TQLabel(hb);
836  label->setPixmap(UserIcon("sessionchooser"));
837  label->setFrameStyle (TQFrame::Panel | TQFrame::Sunken);
838 
839  TQVBox *vb = new TQVBox(hb);
840  vb->setSpacing (KDialog::spacingHint());
841 
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);
849 
850  connect (m_listview, TQ_SIGNAL(selectionChanged()), this, TQ_SLOT(slotSelectionChanged()));
851  connect (m_listview, TQ_SIGNAL(executed(TQListViewItem*)), this, TQ_SLOT(slotUser2()));
852 
853  TQPtrList<KateSession> &sessions = KateSessionManager::self()->getSessionsList();
854  for (int idx = sessions.count()-1; idx >= 0; --idx)
855  {
856  new KateSessionChooserItem(m_listview, sessions[idx]->getSessionName(),
857  TQString("%1").arg(sessions[idx]->getDocCount()), idx);
858  }
859 
860  setResult(RESULT_NO_OP);
861  slotSelectionChanged(); // update button status
862 }
863 
864 //-------------------------------------------
865 int KateSessionChooser::getSelectedSessionId()
866 {
867  KateSessionChooserItem *selectedItem = dynamic_cast<KateSessionChooserItem*>(m_listview->selectedItem());
868  if (!selectedItem)
869  return KateSessionManager::INVALID_SESSION;
870 
871  return selectedItem->getSessionId();
872 }
873 
874 //-------------------------------------------
875 void KateSessionChooser::slotUser1()
876 {
877  done(RESULT_QUIT_KATE);
878 }
879 
880 //-------------------------------------------
881 void KateSessionChooser::slotUser2()
882 {
883  done(RESULT_OPEN_EXISTING);
884 }
885 
886 //-------------------------------------------
887 void KateSessionChooser::slotUser3()
888 {
889  done(RESULT_OPEN_NEW);
890 }
891 
892 //-------------------------------------------
893 void KateSessionChooser::slotSelectionChanged()
894 {
895  enableButton(KDialogBase::User2, m_listview->selectedItem());
896 }
897 //END KateSessionChooser
898 
899 #include "katesession.moc"
KateApp::newMainWindow
KateMainWindow * newMainWindow(TDEConfig *sconfig=0, const TQString &sgroup="")
window management
Definition: kateapp.cpp:435
KateApp::mainWindow
KateMainWindow * mainWindow(uint n)
give back the window you want
Definition: kateapp.cpp:475
KateApp::self
static KateApp * self()
static accessor to avoid casting ;)
Definition: kateapp.cpp:114
KateApp::documentManager
KateDocManager * documentManager()
accessor to document manager
Definition: kateapp.cpp:372
KateApp::mainWindows
uint mainWindows() const
give back number of existing main windows
Definition: kateapp.cpp:470
KateSessionManager
The Kate session manager.
Definition: katesession.h:177
KateSessionManager::sessionCreated
void sessionCreated(int sessionId)
Emitted once a session has been created.
KateSessionManager::updateSessionOptions
void updateSessionOptions(int optionType)
Updated the session startup and switch options.
Definition: katesession.cpp:395
KateSessionManager::sessionRenamed
void sessionRenamed(int sessionId)
Emitted once a session has been renamed.
KateSessionManager::getSessionsList
TQPtrList< KateSession > & getSessionsList()
Definition: katesession.h:290
KateSessionManager::~KateSessionManager
~KateSessionManager()
Destructor.
Definition: katesession.cpp:385
KateSessionManager::saveSessionOptions
void saveSessionOptions(int optionType)
Save the session startup and switch options to the config file.
Definition: katesession.cpp:442
KateSessionManager::deleteSession
bool deleteSession(int sessionId, int actSessId)
Definition: katesession.cpp:694
KateSessionManager::sessionDeleted
void sessionDeleted(int sessionId)
Emitted once a session has been deleted.
KateSessionManager::setSessionReadOnlyStatus
void setSessionReadOnlyStatus(int sessionId, bool readOnly)
Set the read only status of the specified session.
Definition: katesession.cpp:806
KateSessionManager::getSessionIdFromName
int getSessionIdFromName(const TQString &name)
Return the session id of the first session whose name matches the provided one.
Definition: katesession.cpp:568
KateSessionManager::switchOptionChanged
void switchOptionChanged()
Emitted when the session switch option has been set/changed.
KateSessionManager::self
static KateSessionManager * self()
get a pointer to the unique KateSessionManager instance.
Definition: katesession.cpp:321
KateSessionManager::sessionActivated
void sessionActivated(int newSessionId, int oldSessionId)
Emitted once a session has been activated.
KateSessionManager::newSession
int newSession(const TQString &sessionName=TQString::null, bool saveCurr=true)
Definition: katesession.cpp:636
KateSessionManager::restoreLastSession
bool restoreLastSession()
Restore the last saved session.
Definition: katesession.cpp:673
KateSessionManager::moveSessionBackward
void moveSessionBackward(int sessionId)
Move the specified session backward in the session list (by one position)
Definition: katesession.cpp:783
KateSessionManager::getBaseDir
const TQString & getBaseDir() const
Definition: katesession.h:243
KateSessionManager::getSwitchOption
const int getSwitchOption()
Definition: katesession.cpp:531
KateSessionManager::reloadActiveSession
void reloadActiveSession()
Restore the current active session to the last saved state.
Definition: katesession.h:326
KateSessionManager::getSessionName
const TQString & getSessionName(int sessionId)
Definition: katesession.cpp:546
KateSessionManager::saveConfig
void saveConfig(bool saveSessions)
Save session manager info.
Definition: katesession.cpp:481
KateSessionManager::getStartupOption
const int getStartupOption()
Definition: katesession.cpp:524
KateSessionManager::saveSession
void saveSession(int sessionId)
Definition: katesession.h:346
KateSessionManager::swapSessionsPosition
void swapSessionsPosition(int sessionId1, int sessionId2)
Swap the position of the two specified sessions in the session list.
Definition: katesession.cpp:734
KateSessionManager::sessionsSwapped
void sessionsSwapped(int sessionIdMin, int sessionIdMax)
Emitted once the position of the two sessions have been swapped.
KateSessionManager::moveSessionForward
void moveSessionForward(int sessionId)
Move the specified session forward in the session list (by one position)
Definition: katesession.cpp:772
KateSessionManager::sessionSaved
void sessionSaved(int sessionId)
Emitted once a session has been saved.
KateSessionManager::renameSession
void renameSession(int sessionId, const TQString &newSessionName)
Definition: katesession.cpp:794
KateSessionManager::cloneSession
int cloneSession(int sessionId, const TQString &sessionName=TQString::null, bool activate=true, bool deleteCurr=false)
Create a new session and activate it if required.
Definition: katesession.cpp:646
KateSessionManager::getSessionFromId
KateSession * getSessionFromId(int sessionId)
Definition: katesession.cpp:557
KateSessionManager::activateSession
bool activateSession(int sessionId, bool saveCurr=true)
Activate the selected session.
Definition: katesession.cpp:583
KateSessionManager::setSwitchOption
void setSwitchOption(int option)
Set the new session switch preference.
Definition: katesession.cpp:538
KateSession
An object representing a Kate's session.
Definition: katesession.h:48
KateSession::save
void save(bool saveGUIInfo, bool setReadOnly=false)
Save session info.
Definition: katesession.cpp:198
KateSession::load
void load(bool includeGUIInfo)
Load session info from the saved file.
Definition: katesession.cpp:132
KateSession::isStillVolatile
bool isStillVolatile() const
Definition: katesession.cpp:126
KateSession::KateSession
KateSession(const KateSessionManager &manager, const TQString &sessionName, const TQString &fileName)
create a new session and read the config from fileName if it exists
Definition: katesession.cpp:88
KateSession::~KateSession
~KateSession()
Destructor.
Definition: katesession.cpp:111
KateSession::setSessionName
void setSessionName(const TQString &sessionName)
Set the new session name.
Definition: katesession.cpp:120
KateSession::setReadOnly
void setReadOnly(bool readOnly)
Set session read only status.
Definition: katesession.h:92
KateSession::activate
void activate()
Activate the session.
Definition: katesession.cpp:255

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.9.1
This website is maintained by Timothy Pearson.