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