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