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 #include "katesession.moc" 00021 00022 #include "kateapp.h" 00023 #include "katemainwindow.h" 00024 #include "katedocmanager.h" 00025 00026 #include <kstandarddirs.h> 00027 #include <tdelocale.h> 00028 #include <kdebug.h> 00029 #include <kdirwatch.h> 00030 #include <tdelistview.h> 00031 #include <kinputdialog.h> 00032 #include <kiconloader.h> 00033 #include <tdemessagebox.h> 00034 #include <kmdcodec.h> 00035 #include <kstdguiitem.h> 00036 #include <kpushbutton.h> 00037 #include <tdepopupmenu.h> 00038 00039 #include <tqdir.h> 00040 #include <tqlabel.h> 00041 #include <tqlayout.h> 00042 #include <tqvbox.h> 00043 #include <tqhbox.h> 00044 #include <tqcheckbox.h> 00045 #include <tqdatetime.h> 00046 #include <tqmap.h> 00047 00048 #include <unistd.h> 00049 #include <time.h> 00050 00051 bool operator<( const KateSession::Ptr& a, const KateSession::Ptr& b ) 00052 { 00053 return a->sessionName().lower() < b->sessionName().lower(); 00054 } 00055 00056 KateSession::KateSession (KateSessionManager *manager, const TQString &fileName, const TQString &name) 00057 : m_sessionFileRel (fileName) 00058 , m_sessionName (name) 00059 , m_documents (0) 00060 , m_manager (manager) 00061 , m_readConfig (0) 00062 , m_writeConfig (0) 00063 { 00064 init (); 00065 } 00066 00067 void KateSession::init () 00068 { 00069 // given file exists, use it to load some stuff ;) 00070 if (!m_sessionFileRel.isEmpty() && TDEGlobal::dirs()->exists(sessionFile ())) 00071 { 00072 KSimpleConfig config (sessionFile (), true); 00073 00074 if (m_sessionName.isEmpty()) 00075 { 00076 // get the name out of the file 00077 if (m_sessionFileRel == "default.katesession") 00078 m_sessionName = i18n("Default Session"); 00079 else 00080 { 00081 config.setGroup ("General"); 00082 m_sessionName = config.readEntry ("Name", i18n ("Unnamed Session")); 00083 } 00084 } 00085 00086 // get the document count 00087 config.setGroup ("Open Documents"); 00088 m_documents = config.readUnsignedNumEntry("Count", 0); 00089 00090 return; 00091 } 00092 00093 // filename not empty, create the file 00094 // anders: When will this ever happen??? 00095 if (!m_sessionFileRel.isEmpty()) 00096 { 00097 kdDebug(13001)<<"Kate::Session: initializing unexisting file!"<<endl; 00098 // uhh, no name given 00099 if (m_sessionName.isEmpty()) 00100 { 00101 if (m_sessionFileRel == "default.katesession") 00102 m_sessionName = i18n("Default Session"); 00103 else 00104 m_sessionName = i18n("Session (%1)").arg(TQTime::currentTime().toString(Qt::LocalDate)); 00105 } 00106 00107 // create the file, write name to it! 00108 KSimpleConfig config (sessionFile ()); 00109 config.setGroup ("General"); 00110 config.writeEntry ("Name", m_sessionName); 00111 00112 config.sync (); 00113 } 00114 } 00115 00116 KateSession::~KateSession () 00117 { 00118 delete m_readConfig; 00119 delete m_writeConfig; 00120 } 00121 00122 TQString KateSession::sessionFile () const 00123 { 00124 return m_manager->sessionsDir() + "/" + m_sessionFileRel; 00125 } 00126 00127 bool KateSession::create (const TQString &name, bool force) 00128 { 00129 if (!force && (name.isEmpty() || !m_sessionFileRel.isEmpty())) 00130 return false; 00131 00132 delete m_writeConfig; 00133 m_writeConfig = 0; 00134 00135 delete m_readConfig; 00136 m_readConfig = 0; 00137 00138 m_sessionName = name; 00139 00140 // get a usable filename 00141 int s = time(0); 00142 TQCString tname; 00143 while (true) 00144 { 00145 tname.setNum (s++); 00146 KMD5 md5 (tname); 00147 m_sessionFileRel = TQString ("%1.katesession").arg (md5.hexDigest().data()); 00148 00149 if (!TDEGlobal::dirs()->exists(sessionFile ())) 00150 break; 00151 } 00152 00153 // create the file, write name to it! 00154 KSimpleConfig config (sessionFile ()); 00155 config.setGroup ("General"); 00156 config.writeEntry ("Name", m_sessionName); 00157 config.sync (); 00158 00159 // reinit ourselfs ;) 00160 init (); 00161 00162 return true; 00163 } 00164 00165 bool KateSession::rename (const TQString &name) 00166 { 00167 if (name.isEmpty () || m_sessionFileRel.isEmpty() || m_sessionFileRel == "default.katesession") 00168 return false; 00169 00170 m_sessionName = name; 00171 00172 TDEConfig config (sessionFile (), false, false); 00173 config.setGroup ("General"); 00174 config.writeEntry ("Name", m_sessionName); 00175 config.sync (); 00176 00177 return true; 00178 } 00179 00180 TDEConfig *KateSession::configRead () 00181 { 00182 if (m_sessionFileRel.isEmpty()) 00183 return 0; 00184 00185 if (m_readConfig) 00186 return m_readConfig; 00187 00188 return m_readConfig = new KSimpleConfig (sessionFile (), true); 00189 } 00190 00191 TDEConfig *KateSession::configWrite () 00192 { 00193 if (m_sessionFileRel.isEmpty()) 00194 return 0; 00195 00196 if (m_writeConfig) 00197 return m_writeConfig; 00198 00199 m_writeConfig = new KSimpleConfig (sessionFile ()); 00200 m_writeConfig->setGroup ("General"); 00201 m_writeConfig->writeEntry ("Name", m_sessionName); 00202 00203 return m_writeConfig; 00204 } 00205 00206 KateSessionManager::KateSessionManager (TQObject *parent) 00207 : TQObject (parent) 00208 , m_sessionsDir (locateLocal( "data", "kate/sessions")) 00209 , m_activeSession (new KateSession (this, "", "")) 00210 { 00211 kdDebug() << "LOCAL SESSION DIR: " << m_sessionsDir << endl; 00212 00213 // create dir if needed 00214 TDEGlobal::dirs()->makeDir (m_sessionsDir); 00215 } 00216 00217 KateSessionManager::~KateSessionManager() 00218 { 00219 } 00220 00221 KateSessionManager *KateSessionManager::self() 00222 { 00223 return KateApp::self()->sessionManager (); 00224 } 00225 00226 void KateSessionManager::dirty (const TQString &) 00227 { 00228 updateSessionList (); 00229 } 00230 00231 void KateSessionManager::updateSessionList () 00232 { 00233 m_sessionList.clear (); 00234 00235 // Let's get a list of all session we have atm 00236 TQDir dir (m_sessionsDir, "*.katesession"); 00237 00238 bool foundDefault = false; 00239 for (unsigned int i=0; i < dir.count(); ++i) 00240 { 00241 KateSession *session = new KateSession (this, dir[i], ""); 00242 m_sessionList.append (session); 00243 00244 kdDebug () << "FOUND SESSION: " << session->sessionName() << " FILE: " << session->sessionFile() << endl; 00245 00246 if (!foundDefault && (dir[i] == "default.katesession")) 00247 foundDefault = true; 00248 } 00249 00250 // add default session, if not there 00251 if (!foundDefault) 00252 m_sessionList.append (new KateSession (this, "default.katesession", i18n("Default Session"))); 00253 00254 qHeapSort(m_sessionList); 00255 } 00256 00257 void KateSessionManager::activateSession (KateSession::Ptr session, bool closeLast, bool saveLast, bool loadNew) 00258 { 00259 // don't reload. 00260 // ### comparing the pointers directly is b0rk3d :( 00261 if ( ! session->sessionName().isEmpty() && session->sessionName() == m_activeSession->sessionName() ) 00262 return; 00263 // try to close last session 00264 if (closeLast) 00265 { 00266 if (KateApp::self()->activeMainWindow()) 00267 { 00268 if (!KateApp::self()->activeMainWindow()->queryClose_internal()) 00269 return; 00270 } 00271 } 00272 00273 // save last session or not? 00274 if (saveLast) 00275 saveActiveSession (true); 00276 00277 // really close last 00278 if (closeLast) 00279 { 00280 KateDocManager::self()->closeAllDocuments (); 00281 } 00282 00283 // set the new session 00284 m_activeSession = session; 00285 00286 if (loadNew) 00287 { 00288 // open the new session 00289 Kate::Document::setOpenErrorDialogsActivated (false); 00290 00291 TDEConfig *sc = activeSession()->configRead(); 00292 00293 if (sc) 00294 KateApp::self()->documentManager()->restoreDocumentList (sc); 00295 00296 // if we have no session config object, try to load the default 00297 // (anonymous/unnamed sessions) 00298 if ( ! sc ) 00299 sc = new KSimpleConfig( sessionsDir() + "/default.katesession" ); 00300 00301 // window config 00302 if (sc) 00303 { 00304 TDEConfig *c = KateApp::self()->config(); 00305 c->setGroup("General"); 00306 00307 if (c->readBoolEntry("Restore Window Configuration", true)) 00308 { 00309 // a new, named session, read settings of the default session. 00310 if ( ! sc->hasGroup("Open MainWindows") ) 00311 sc = new KSimpleConfig( sessionsDir() + "/default.katesession" ); 00312 00313 sc->setGroup ("Open MainWindows"); 00314 unsigned int wCount = sc->readUnsignedNumEntry("Count", 1); 00315 00316 for (unsigned int i=0; i < wCount; ++i) 00317 { 00318 if (i >= KateApp::self()->mainWindows()) 00319 { 00320 KateApp::self()->newMainWindow(sc, TQString ("MainWindow%1").arg(i)); 00321 } 00322 else 00323 { 00324 sc->setGroup(TQString ("MainWindow%1").arg(i)); 00325 KateApp::self()->mainWindow(i)->readProperties (sc); 00326 } 00327 } 00328 00329 if (wCount > 0) 00330 { 00331 while (wCount < KateApp::self()->mainWindows()) 00332 { 00333 KateMainWindow *w = KateApp::self()->mainWindow(KateApp::self()->mainWindows()-1); 00334 KateApp::self()->removeMainWindow (w); 00335 delete w; 00336 } 00337 } 00338 } 00339 } 00340 00341 Kate::Document::setOpenErrorDialogsActivated (true); 00342 } 00343 } 00344 00345 KateSession::Ptr KateSessionManager::createSession (const TQString &name) 00346 { 00347 KateSession::Ptr s = new KateSession (this, "", ""); 00348 s->create (name); 00349 00350 return s; 00351 } 00352 00353 KateSession::Ptr KateSessionManager::giveSession (const TQString &name) 00354 { 00355 if (name.isEmpty()) 00356 return new KateSession (this, "", ""); 00357 00358 updateSessionList(); 00359 00360 for (unsigned int i=0; i < m_sessionList.count(); ++i) 00361 { 00362 if (m_sessionList[i]->sessionName() == name) 00363 return m_sessionList[i]; 00364 } 00365 00366 return createSession (name); 00367 } 00368 00369 bool KateSessionManager::saveActiveSession (bool tryAsk, bool rememberAsLast) 00370 { 00371 if (tryAsk) 00372 { 00373 // app config 00374 TDEConfig *c = KateApp::self()->config(); 00375 c->setGroup("General"); 00376 00377 TQString sesExit (c->readEntry ("Session Exit", "save")); 00378 00379 if (sesExit == "discard") 00380 return true; 00381 00382 if (sesExit == "ask") 00383 { 00384 KDialogBase* dlg = new KDialogBase(i18n ("Save Session?") 00385 , KDialogBase::Yes | KDialogBase::No 00386 , KDialogBase::Yes, KDialogBase::No 00387 ); 00388 00389 bool dontAgain = false; 00390 int res = KMessageBox::createKMessageBox(dlg, TQMessageBox::Question, 00391 i18n("Save current session?"), TQStringList(), 00392 i18n("Do not ask again"), &dontAgain, KMessageBox::Notify); 00393 00394 // remember to not ask again with right setting 00395 if (dontAgain) 00396 { 00397 c->setGroup("General"); 00398 00399 if (res == KDialogBase::No) 00400 c->writeEntry ("Session Exit", "discard"); 00401 else 00402 c->writeEntry ("Session Exit", "save"); 00403 } 00404 00405 if (res == KDialogBase::No) 00406 return true; 00407 } 00408 } 00409 00410 TDEConfig *sc = activeSession()->configWrite(); 00411 00412 if (!sc) 00413 return false; 00414 00415 KateDocManager::self()->saveDocumentList (sc); 00416 00417 sc->setGroup ("Open MainWindows"); 00418 sc->writeEntry ("Count", KateApp::self()->mainWindows ()); 00419 00420 // save config for all windows around ;) 00421 for (unsigned int i=0; i < KateApp::self()->mainWindows (); ++i ) 00422 { 00423 sc->setGroup(TQString ("MainWindow%1").arg(i)); 00424 KateApp::self()->mainWindow(i)->saveProperties (sc); 00425 } 00426 00427 sc->sync(); 00428 00429 if (rememberAsLast) 00430 { 00431 TDEConfig *c = KateApp::self()->config(); 00432 c->setGroup("General"); 00433 c->writeEntry ("Last Session", activeSession()->sessionFileRelative()); 00434 c->sync (); 00435 } 00436 00437 return true; 00438 } 00439 00440 bool KateSessionManager::chooseSession () 00441 { 00442 bool success = true; 00443 00444 // app config 00445 TDEConfig *c = KateApp::self()->config(); 00446 c->setGroup("General"); 00447 00448 // get last used session, default to default session 00449 TQString lastSession (c->readEntry ("Last Session", "default.katesession")); 00450 TQString sesStart (c->readEntry ("Startup Session", "manual")); 00451 00452 // uhh, just open last used session, show no chooser 00453 if (sesStart == "last") 00454 { 00455 activateSession (new KateSession (this, lastSession, ""), false, false); 00456 return success; 00457 } 00458 00459 // start with empty new session 00460 if (sesStart == "new") 00461 { 00462 activateSession (new KateSession (this, "", ""), false, false); 00463 return success; 00464 } 00465 00466 KateSessionChooser *chooser = new KateSessionChooser (0, lastSession); 00467 00468 bool retry = true; 00469 int res = 0; 00470 while (retry) 00471 { 00472 res = chooser->exec (); 00473 00474 switch (res) 00475 { 00476 case KateSessionChooser::resultOpen: 00477 { 00478 KateSession::Ptr s = chooser->selectedSession (); 00479 00480 if (!s) 00481 { 00482 KMessageBox::error (chooser, i18n("No session selected to open."), i18n ("No Session Selected")); 00483 break; 00484 } 00485 00486 activateSession (s, false, false); 00487 retry = false; 00488 break; 00489 } 00490 00491 // exit the app lateron 00492 case KateSessionChooser::resultQuit: 00493 success = false; 00494 retry = false; 00495 break; 00496 00497 default: 00498 activateSession (new KateSession (this, "", ""), false, false); 00499 retry = false; 00500 break; 00501 } 00502 } 00503 00504 // write back our nice boolean :) 00505 if (success && chooser->reopenLastSession ()) 00506 { 00507 c->setGroup("General"); 00508 00509 if (res == KateSessionChooser::resultOpen) 00510 c->writeEntry ("Startup Session", "last"); 00511 else if (res == KateSessionChooser::resultNew) 00512 c->writeEntry ("Startup Session", "new"); 00513 00514 c->sync (); 00515 } 00516 00517 delete chooser; 00518 00519 return success; 00520 } 00521 00522 void KateSessionManager::sessionNew () 00523 { 00524 activateSession (new KateSession (this, "", "")); 00525 } 00526 00527 void KateSessionManager::sessionOpen () 00528 { 00529 KateSessionOpenDialog *chooser = new KateSessionOpenDialog (0); 00530 00531 int res = chooser->exec (); 00532 00533 if (res == KateSessionOpenDialog::resultCancel) 00534 { 00535 delete chooser; 00536 return; 00537 } 00538 00539 KateSession::Ptr s = chooser->selectedSession (); 00540 00541 if (s) 00542 activateSession (s); 00543 00544 delete chooser; 00545 } 00546 00547 void KateSessionManager::sessionSave () 00548 { 00549 // if the active session is valid, just save it :) 00550 if (saveActiveSession ()) 00551 return; 00552 00553 bool ok = false; 00554 TQString name = KInputDialog::getText (i18n("Specify Name for Current Session"), i18n("Session name:"), "", &ok); 00555 00556 if (!ok) 00557 return; 00558 00559 if (name.isEmpty()) 00560 { 00561 KMessageBox::error (0, i18n("To save a new session, you must specify a name."), i18n ("Missing Session Name")); 00562 return; 00563 } 00564 00565 activeSession()->create (name); 00566 saveActiveSession (); 00567 } 00568 00569 void KateSessionManager::sessionSaveAs () 00570 { 00571 bool ok = false; 00572 TQString name = KInputDialog::getText (i18n("Specify New Name for Current Session"), i18n("Session name:"), "", &ok); 00573 00574 if (!ok) 00575 return; 00576 00577 if (name.isEmpty()) 00578 { 00579 KMessageBox::error (0, i18n("To save a session, you must specify a name."), i18n ("Missing Session Name")); 00580 return; 00581 } 00582 00583 activeSession()->create (name, true); 00584 saveActiveSession (); 00585 } 00586 00587 00588 void KateSessionManager::sessionManage () 00589 { 00590 KateSessionManageDialog *dlg = new KateSessionManageDialog (0); 00591 00592 dlg->exec (); 00593 00594 delete dlg; 00595 } 00596 00597 //BEGIN CHOOSER DIALOG 00598 00599 class KateSessionChooserItem : public TQListViewItem 00600 { 00601 public: 00602 KateSessionChooserItem (TDEListView *lv, KateSession::Ptr s) 00603 : TQListViewItem (lv, s->sessionName()) 00604 , session (s) 00605 { 00606 TQString docs; 00607 docs.setNum (s->documents()); 00608 setText (1, docs); 00609 } 00610 00611 KateSession::Ptr session; 00612 }; 00613 00614 KateSessionChooser::KateSessionChooser (TQWidget *parent, const TQString &lastSession) 00615 : KDialogBase ( parent 00616 , "" 00617 , true 00618 , i18n ("Session Chooser") 00619 , KDialogBase::User1 | KDialogBase::User2 | KDialogBase::User3 00620 , KDialogBase::User2 00621 , true 00622 , KStdGuiItem::quit () 00623 , KGuiItem (i18n ("Open Session"), "document-open") 00624 , KGuiItem (i18n ("New Session"), "document-new") 00625 ) 00626 { 00627 TQHBox *page = new TQHBox (this); 00628 page->setMinimumSize (400, 200); 00629 setMainWidget(page); 00630 00631 TQHBox *hb = new TQHBox (page); 00632 hb->setSpacing (KDialog::spacingHint()); 00633 00634 TQLabel *label = new TQLabel (hb); 00635 label->setPixmap (UserIcon("sessionchooser")); 00636 label->setFrameStyle (TQFrame::Panel | TQFrame::Sunken); 00637 00638 TQVBox *vb = new TQVBox (hb); 00639 vb->setSpacing (KDialog::spacingHint()); 00640 00641 m_sessions = new TDEListView (vb); 00642 m_sessions->addColumn (i18n("Session Name")); 00643 m_sessions->addColumn (i18n("Open Documents")); 00644 m_sessions->setResizeMode (TQListView::AllColumns); 00645 m_sessions->setSelectionMode (TQListView::Single); 00646 m_sessions->setAllColumnsShowFocus (true); 00647 00648 connect (m_sessions, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(selectionChanged())); 00649 connect (m_sessions, TQT_SIGNAL(doubleClicked(TQListViewItem *, const TQPoint &, int)), this, TQT_SLOT(slotUser2())); 00650 00651 KateSessionList &slist (KateSessionManager::self()->sessionList()); 00652 for (unsigned int i=0; i < slist.count(); ++i) 00653 { 00654 KateSessionChooserItem *item = new KateSessionChooserItem (m_sessions, slist[i]); 00655 00656 if (slist[i]->sessionFileRelative() == lastSession) 00657 m_sessions->setSelected (item, true); 00658 } 00659 00660 m_useLast = new TQCheckBox (i18n ("&Always use this choice"), vb); 00661 00662 setResult (resultNone); 00663 00664 // trigger action update 00665 selectionChanged (); 00666 } 00667 00668 KateSessionChooser::~KateSessionChooser () 00669 { 00670 } 00671 00672 KateSession::Ptr KateSessionChooser::selectedSession () 00673 { 00674 KateSessionChooserItem *item = (KateSessionChooserItem *) m_sessions->selectedItem (); 00675 00676 if (!item) 00677 return 0; 00678 00679 return item->session; 00680 } 00681 00682 bool KateSessionChooser::reopenLastSession () 00683 { 00684 return m_useLast->isChecked (); 00685 } 00686 00687 void KateSessionChooser::slotUser2 () 00688 { 00689 done (resultOpen); 00690 } 00691 00692 void KateSessionChooser::slotUser3 () 00693 { 00694 done (resultNew); 00695 } 00696 00697 void KateSessionChooser::slotUser1 () 00698 { 00699 done (resultQuit); 00700 } 00701 00702 void KateSessionChooser::selectionChanged () 00703 { 00704 enableButton (KDialogBase::User2, m_sessions->selectedItem ()); 00705 } 00706 00707 //END CHOOSER DIALOG 00708 00709 //BEGIN OPEN DIALOG 00710 00711 KateSessionOpenDialog::KateSessionOpenDialog (TQWidget *parent) 00712 : KDialogBase ( parent 00713 , "" 00714 , true 00715 , i18n ("Open Session") 00716 , KDialogBase::User1 | KDialogBase::User2 00717 , KDialogBase::User2 00718 , false 00719 , KStdGuiItem::cancel () 00720 , KGuiItem( i18n("&Open"), "document-open") 00721 ) 00722 { 00723 TQHBox *page = new TQHBox (this); 00724 page->setMinimumSize (400, 200); 00725 setMainWidget(page); 00726 00727 TQHBox *hb = new TQHBox (page); 00728 00729 TQVBox *vb = new TQVBox (hb); 00730 00731 m_sessions = new TDEListView (vb); 00732 m_sessions->addColumn (i18n("Session Name")); 00733 m_sessions->addColumn (i18n("Open Documents")); 00734 m_sessions->setResizeMode (TQListView::AllColumns); 00735 m_sessions->setSelectionMode (TQListView::Single); 00736 m_sessions->setAllColumnsShowFocus (true); 00737 00738 connect (m_sessions, TQT_SIGNAL(doubleClicked(TQListViewItem *, const TQPoint &, int)), this, TQT_SLOT(slotUser2())); 00739 00740 KateSessionList &slist (KateSessionManager::self()->sessionList()); 00741 for (unsigned int i=0; i < slist.count(); ++i) 00742 { 00743 new KateSessionChooserItem (m_sessions, slist[i]); 00744 } 00745 00746 setResult (resultCancel); 00747 } 00748 00749 KateSessionOpenDialog::~KateSessionOpenDialog () 00750 { 00751 } 00752 00753 KateSession::Ptr KateSessionOpenDialog::selectedSession () 00754 { 00755 KateSessionChooserItem *item = (KateSessionChooserItem *) m_sessions->selectedItem (); 00756 00757 if (!item) 00758 return 0; 00759 00760 return item->session; 00761 } 00762 00763 void KateSessionOpenDialog::slotUser1 () 00764 { 00765 done (resultCancel); 00766 } 00767 00768 void KateSessionOpenDialog::slotUser2 () 00769 { 00770 done (resultOk); 00771 } 00772 00773 //END OPEN DIALOG 00774 00775 //BEGIN MANAGE DIALOG 00776 00777 KateSessionManageDialog::KateSessionManageDialog (TQWidget *parent) 00778 : KDialogBase ( parent 00779 , "" 00780 , true 00781 , i18n ("Manage Sessions") 00782 , KDialogBase::User1 00783 , KDialogBase::User1 00784 , false 00785 , KStdGuiItem::close () 00786 ) 00787 { 00788 TQHBox *page = new TQHBox (this); 00789 page->setMinimumSize (400, 200); 00790 setMainWidget(page); 00791 00792 TQHBox *hb = new TQHBox (page); 00793 hb->setSpacing (KDialog::spacingHint()); 00794 00795 m_sessions = new TDEListView (hb); 00796 m_sessions->addColumn (i18n("Session Name")); 00797 m_sessions->addColumn (i18n("Open Documents")); 00798 m_sessions->setResizeMode (TQListView::AllColumns); 00799 m_sessions->setSelectionMode (TQListView::Single); 00800 m_sessions->setAllColumnsShowFocus (true); 00801 00802 connect (m_sessions, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(selectionChanged())); 00803 00804 updateSessionList (); 00805 00806 TQWidget *vb = new TQWidget (hb); 00807 TQVBoxLayout *vbl = new TQVBoxLayout (vb); 00808 vbl->setSpacing (KDialog::spacingHint()); 00809 00810 m_rename = new KPushButton (i18n("&Rename..."), vb); 00811 connect (m_rename, TQT_SIGNAL(clicked()), this, TQT_SLOT(rename())); 00812 vbl->addWidget (m_rename); 00813 00814 m_del = new KPushButton (KStdGuiItem::del (), vb); 00815 connect (m_del, TQT_SIGNAL(clicked()), this, TQT_SLOT(del())); 00816 vbl->addWidget (m_del); 00817 00818 vbl->addStretch (); 00819 00820 // trigger action update 00821 selectionChanged (); 00822 } 00823 00824 KateSessionManageDialog::~KateSessionManageDialog () 00825 { 00826 } 00827 00828 void KateSessionManageDialog::slotUser1 () 00829 { 00830 done (0); 00831 } 00832 00833 00834 void KateSessionManageDialog::selectionChanged () 00835 { 00836 KateSessionChooserItem *item = (KateSessionChooserItem *) m_sessions->selectedItem (); 00837 00838 m_rename->setEnabled (item && item->session->sessionFileRelative() != "default.katesession"); 00839 m_del->setEnabled (item && item->session->sessionFileRelative() != "default.katesession"); 00840 } 00841 00842 void KateSessionManageDialog::rename () 00843 { 00844 KateSessionChooserItem *item = (KateSessionChooserItem *) m_sessions->selectedItem (); 00845 00846 if (!item || item->session->sessionFileRelative() == "default.katesession") 00847 return; 00848 00849 bool ok = false; 00850 TQString name = KInputDialog::getText (i18n("Specify New Name for Session"), i18n("Session name:"), item->session->sessionName(), &ok); 00851 00852 if (!ok) 00853 return; 00854 00855 if (name.isEmpty()) 00856 { 00857 KMessageBox::error (0, i18n("To save a session, you must specify a name."), i18n ("Missing Session Name")); 00858 return; 00859 } 00860 00861 item->session->rename (name); 00862 updateSessionList (); 00863 } 00864 00865 void KateSessionManageDialog::del () 00866 { 00867 KateSessionChooserItem *item = (KateSessionChooserItem *) m_sessions->selectedItem (); 00868 00869 if (!item || item->session->sessionFileRelative() == "default.katesession") 00870 return; 00871 00872 TQFile::remove (item->session->sessionFile()); 00873 KateSessionManager::self()->updateSessionList (); 00874 updateSessionList (); 00875 } 00876 00877 void KateSessionManageDialog::updateSessionList () 00878 { 00879 m_sessions->clear (); 00880 00881 KateSessionList &slist (KateSessionManager::self()->sessionList()); 00882 for (unsigned int i=0; i < slist.count(); ++i) 00883 { 00884 new KateSessionChooserItem (m_sessions, slist[i]); 00885 } 00886 } 00887 00888 //END MANAGE DIALOG 00889 00890 00891 KateSessionsAction::KateSessionsAction(const TQString& text, TQObject* parent, const char* name ) 00892 : TDEActionMenu(text, parent, name) 00893 { 00894 connect(popupMenu(),TQT_SIGNAL(aboutToShow()),this,TQT_SLOT(slotAboutToShow())); 00895 } 00896 00897 void KateSessionsAction::slotAboutToShow() 00898 { 00899 popupMenu()->clear (); 00900 00901 KateSessionList &slist (KateSessionManager::self()->sessionList()); 00902 for (unsigned int i=0; i < slist.count(); ++i) 00903 { 00904 popupMenu()->insertItem ( 00905 slist[i]->sessionName(), 00906 this, TQT_SLOT (openSession (int)), 0, 00907 i ); 00908 } 00909 } 00910 00911 void KateSessionsAction::openSession (int i) 00912 { 00913 KateSessionList &slist (KateSessionManager::self()->sessionList()); 00914 00915 if ((uint)i >= slist.count()) 00916 return; 00917 00918 KateSessionManager::self()->activateSession(slist[(uint)i]); 00919 } 00920 // kate: space-indent on; indent-width 2; replace-tabs on; mixed-indent off;