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 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 QString &fileName, const QString &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() && KGlobal::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(QTime::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 QString KateSession::sessionFile () const
00123 {
00124   return m_manager->sessionsDir() + "/" + m_sessionFileRel;
00125 }
00126 
00127 bool KateSession::create (const QString &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   QCString tname;
00143   while (true)
00144   {
00145     tname.setNum (s++);
00146     KMD5 md5 (tname);
00147     m_sessionFileRel = QString ("%1.katesession").arg (md5.hexDigest().data());
00148 
00149     if (!KGlobal::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 QString &name)
00166 {
00167   if (name.isEmpty () || m_sessionFileRel.isEmpty() || m_sessionFileRel == "default.katesession")
00168     return false;
00169 
00170   m_sessionName = name;
00171 
00172   KConfig config (sessionFile (), false, false);
00173   config.setGroup ("General");
00174   config.writeEntry ("Name", m_sessionName);
00175   config.sync ();
00176 
00177   return true;
00178 }
00179 
00180 KConfig *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 KConfig *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 (QObject *parent)
00207  : QObject (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   KGlobal::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 QString &)
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   QDir 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     KConfig *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       KConfig *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, QString ("MainWindow%1").arg(i));
00321           }
00322           else
00323           {
00324             sc->setGroup(QString ("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 QString &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 QString &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     KConfig *c = KateApp::self()->config();
00375     c->setGroup("General");
00376 
00377     QString 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 (
00385                     i18n ("Save Session?")
00386                     , KDialogBase::Yes | KDialogBase::No
00387                     , KDialogBase::Yes, KDialogBase::No
00388                   );
00389 
00390       bool dontAgain = false;
00391       int res = KMessageBox::createKMessageBox(dlg, QMessageBox::Question,
00392                               i18n("Save current session?"), QStringList(),
00393                               i18n("Do not ask again"), &dontAgain, KMessageBox::Notify);
00394 
00395       // remember to not ask again with right setting
00396       if (dontAgain)
00397       {
00398         c->setGroup("General");
00399 
00400         if (res == KDialogBase::No)
00401           c->writeEntry ("Session Exit", "discard");
00402         else
00403           c->writeEntry ("Session Exit", "save");
00404       }
00405 
00406       if (res == KDialogBase::No)
00407         return true;
00408     }
00409   }
00410 
00411   KConfig *sc = activeSession()->configWrite();
00412 
00413   if (!sc)
00414     return false;
00415 
00416   KateDocManager::self()->saveDocumentList (sc);
00417 
00418   sc->setGroup ("Open MainWindows");
00419   sc->writeEntry ("Count", KateApp::self()->mainWindows ());
00420 
00421   // save config for all windows around ;)
00422   for (unsigned int i=0; i < KateApp::self()->mainWindows (); ++i )
00423   {
00424     sc->setGroup(QString ("MainWindow%1").arg(i));
00425     KateApp::self()->mainWindow(i)->saveProperties (sc);
00426   }
00427 
00428   sc->sync();
00429 
00430   if (rememberAsLast)
00431   {
00432     KConfig *c = KateApp::self()->config();
00433     c->setGroup("General");
00434     c->writeEntry ("Last Session", activeSession()->sessionFileRelative());
00435     c->sync ();
00436   }
00437 
00438   return true;
00439 }
00440 
00441 bool KateSessionManager::chooseSession ()
00442 {
00443   bool success = true;
00444 
00445   // app config
00446   KConfig *c = KateApp::self()->config();
00447   c->setGroup("General");
00448 
00449   // get last used session, default to default session
00450   QString lastSession (c->readEntry ("Last Session", "default.katesession"));
00451   QString sesStart (c->readEntry ("Startup Session", "manual"));
00452 
00453   // uhh, just open last used session, show no chooser
00454   if (sesStart == "last")
00455   {
00456     activateSession (new KateSession (this, lastSession, ""), false, false);
00457     return success;
00458   }
00459 
00460   // start with empty new session
00461   if (sesStart == "new")
00462   {
00463     activateSession (new KateSession (this, "", ""), false, false);
00464     return success;
00465   }
00466 
00467   KateSessionChooser *chooser = new KateSessionChooser (0, lastSession);
00468 
00469   bool retry = true;
00470   int res = 0;
00471   while (retry)
00472   {
00473     res = chooser->exec ();
00474 
00475     switch (res)
00476     {
00477       case KateSessionChooser::resultOpen:
00478       {
00479         KateSession::Ptr s = chooser->selectedSession ();
00480 
00481         if (!s)
00482         {
00483           KMessageBox::error (chooser, i18n("No session selected to open."), i18n ("No Session Selected"));
00484           break;
00485         }
00486 
00487         activateSession (s, false, false);
00488         retry = false;
00489         break;
00490       }
00491 
00492       // exit the app lateron
00493       case KateSessionChooser::resultQuit:
00494         success = false;
00495         retry = false;
00496         break;
00497 
00498       default:
00499         activateSession (new KateSession (this, "", ""), false, false);
00500         retry = false;
00501         break;
00502     }
00503   }
00504 
00505   // write back our nice boolean :)
00506   if (success && chooser->reopenLastSession ())
00507   {
00508     c->setGroup("General");
00509 
00510     if (res == KateSessionChooser::resultOpen)
00511       c->writeEntry ("Startup Session", "last");
00512     else if (res == KateSessionChooser::resultNew)
00513       c->writeEntry ("Startup Session", "new");
00514 
00515     c->sync ();
00516   }
00517 
00518   delete chooser;
00519 
00520   return success;
00521 }
00522 
00523 void KateSessionManager::sessionNew ()
00524 {
00525   activateSession (new KateSession (this, "", ""));
00526 }
00527 
00528 void KateSessionManager::sessionOpen ()
00529 {
00530   KateSessionOpenDialog *chooser = new KateSessionOpenDialog (0);
00531 
00532   int res = chooser->exec ();
00533 
00534   if (res == KateSessionOpenDialog::resultCancel)
00535   {
00536     delete chooser;
00537     return;
00538   }
00539 
00540   KateSession::Ptr s = chooser->selectedSession ();
00541 
00542   if (s)
00543     activateSession (s);
00544 
00545   delete chooser;
00546 }
00547 
00548 void KateSessionManager::sessionSave ()
00549 {
00550   // if the active session is valid, just save it :)
00551   if (saveActiveSession ())
00552     return;
00553 
00554   bool ok = false;
00555   QString name = KInputDialog::getText (i18n("Specify Name for Current Session"), i18n("Session name:"), "", &ok);
00556 
00557   if (!ok)
00558     return;
00559 
00560   if (name.isEmpty())
00561   {
00562     KMessageBox::error (0, i18n("To save a new session, you must specify a name."), i18n ("Missing Session Name"));
00563     return;
00564   }
00565 
00566   activeSession()->create (name);
00567   saveActiveSession ();
00568 }
00569 
00570 void KateSessionManager::sessionSaveAs ()
00571 {
00572   bool ok = false;
00573   QString name = KInputDialog::getText (i18n("Specify New Name for Current Session"), i18n("Session name:"), "", &ok);
00574 
00575   if (!ok)
00576     return;
00577 
00578   if (name.isEmpty())
00579   {
00580     KMessageBox::error (0, i18n("To save a session, you must specify a name."), i18n ("Missing Session Name"));
00581     return;
00582   }
00583 
00584   activeSession()->create (name, true);
00585   saveActiveSession ();
00586 }
00587 
00588 
00589 void KateSessionManager::sessionManage ()
00590 {
00591   KateSessionManageDialog *dlg = new KateSessionManageDialog (0);
00592 
00593   dlg->exec ();
00594 
00595   delete dlg;
00596 }
00597 
00598 //BEGIN CHOOSER DIALOG
00599 
00600 class KateSessionChooserItem : public QListViewItem
00601 {
00602   public:
00603     KateSessionChooserItem (KListView *lv, KateSession::Ptr s)
00604      : QListViewItem (lv, s->sessionName())
00605      , session (s)
00606     {
00607       QString docs;
00608       docs.setNum (s->documents());
00609       setText (1, docs);
00610     }
00611 
00612     KateSession::Ptr session;
00613 };
00614 
00615 KateSessionChooser::KateSessionChooser (QWidget *parent, const QString &lastSession)
00616  : KDialogBase (  parent
00617                   , ""
00618                   , true
00619                   , i18n ("Session Chooser")
00620                   , KDialogBase::User1 | KDialogBase::User2 | KDialogBase::User3
00621                   , KDialogBase::User2
00622                   , true
00623                   , KStdGuiItem::quit ()
00624                   , KGuiItem (i18n ("Open Session"), "fileopen")
00625                   , KGuiItem (i18n ("New Session"), "filenew")
00626                 )
00627 {
00628   QHBox *page = new QHBox (this);
00629   page->setMinimumSize (400, 200);
00630   setMainWidget(page);
00631 
00632   QHBox *hb = new QHBox (page);
00633   hb->setSpacing (KDialog::spacingHint());
00634 
00635   QLabel *label = new QLabel (hb);
00636   label->setPixmap (UserIcon("sessionchooser"));
00637   label->setFrameStyle (QFrame::Panel | QFrame::Sunken);
00638 
00639   QVBox *vb = new QVBox (hb);
00640   vb->setSpacing (KDialog::spacingHint());
00641 
00642   m_sessions = new KListView (vb);
00643   m_sessions->addColumn (i18n("Session Name"));
00644   m_sessions->addColumn (i18n("Open Documents"));
00645   m_sessions->setResizeMode (QListView::AllColumns);
00646   m_sessions->setSelectionMode (QListView::Single);
00647   m_sessions->setAllColumnsShowFocus (true);
00648 
00649   connect (m_sessions, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
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 QCheckBox (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::User1, m_sessions->selectedItem ());
00705 }
00706 
00707 //END CHOOSER DIALOG
00708 
00709 //BEGIN OPEN DIALOG
00710 
00711 KateSessionOpenDialog::KateSessionOpenDialog (QWidget *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"), "fileopen")
00721                 )
00722 {
00723   QHBox *page = new QHBox (this);
00724   page->setMinimumSize (400, 200);
00725   setMainWidget(page);
00726 
00727   QHBox *hb = new QHBox (page);
00728 
00729   QVBox *vb = new QVBox (hb);
00730 
00731   m_sessions = new KListView (vb);
00732   m_sessions->addColumn (i18n("Session Name"));
00733   m_sessions->addColumn (i18n("Open Documents"));
00734   m_sessions->setResizeMode (QListView::AllColumns);
00735   m_sessions->setSelectionMode (QListView::Single);
00736   m_sessions->setAllColumnsShowFocus (true);
00737 
00738   KateSessionList &slist (KateSessionManager::self()->sessionList());
00739   for (unsigned int i=0; i < slist.count(); ++i)
00740   {
00741     new KateSessionChooserItem (m_sessions, slist[i]);
00742   }
00743 
00744   setResult (resultCancel);
00745 }
00746 
00747 KateSessionOpenDialog::~KateSessionOpenDialog ()
00748 {
00749 }
00750 
00751 KateSession::Ptr KateSessionOpenDialog::selectedSession ()
00752 {
00753   KateSessionChooserItem *item = (KateSessionChooserItem *) m_sessions->selectedItem ();
00754 
00755   if (!item)
00756     return 0;
00757 
00758   return item->session;
00759 }
00760 
00761 void KateSessionOpenDialog::slotUser1 ()
00762 {
00763   done (resultCancel);
00764 }
00765 
00766 void KateSessionOpenDialog::slotUser2 ()
00767 {
00768   done (resultOk);
00769 }
00770 
00771 //END OPEN DIALOG
00772 
00773 //BEGIN MANAGE DIALOG
00774 
00775 KateSessionManageDialog::KateSessionManageDialog (QWidget *parent)
00776  : KDialogBase (  parent
00777                   , ""
00778                   , true
00779                   , i18n ("Manage Sessions")
00780                   , KDialogBase::User1
00781                   , KDialogBase::User1
00782                   , false
00783                   , KStdGuiItem::close ()
00784                 )
00785 {
00786   QHBox *page = new QHBox (this);
00787   page->setMinimumSize (400, 200);
00788   setMainWidget(page);
00789 
00790   QHBox *hb = new QHBox (page);
00791   hb->setSpacing (KDialog::spacingHint());
00792 
00793   m_sessions = new KListView (hb);
00794   m_sessions->addColumn (i18n("Session Name"));
00795   m_sessions->addColumn (i18n("Open Documents"));
00796   m_sessions->setResizeMode (QListView::AllColumns);
00797   m_sessions->setSelectionMode (QListView::Single);
00798   m_sessions->setAllColumnsShowFocus (true);
00799 
00800   connect (m_sessions, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
00801 
00802   updateSessionList ();
00803 
00804   QWidget *vb = new QWidget (hb);
00805   QVBoxLayout *vbl = new QVBoxLayout (vb);
00806   vbl->setSpacing (KDialog::spacingHint());
00807 
00808   m_rename = new KPushButton (i18n("&Rename..."), vb);
00809   connect (m_rename, SIGNAL(clicked()), this, SLOT(rename()));
00810   vbl->addWidget (m_rename);
00811 
00812   m_del = new KPushButton (KStdGuiItem::del (), vb);
00813   connect (m_del, SIGNAL(clicked()), this, SLOT(del()));
00814   vbl->addWidget (m_del);
00815 
00816   vbl->addStretch ();
00817 
00818   // trigger action update
00819   selectionChanged ();
00820 }
00821 
00822 KateSessionManageDialog::~KateSessionManageDialog ()
00823 {
00824 }
00825 
00826 void KateSessionManageDialog::slotUser1 ()
00827 {
00828   done (0);
00829 }
00830 
00831 
00832 void KateSessionManageDialog::selectionChanged ()
00833 {
00834   KateSessionChooserItem *item = (KateSessionChooserItem *) m_sessions->selectedItem ();
00835 
00836   m_rename->setEnabled (item && item->session->sessionFileRelative() != "default.katesession");
00837   m_del->setEnabled (item && item->session->sessionFileRelative() != "default.katesession");
00838 }
00839 
00840 void KateSessionManageDialog::rename ()
00841 {
00842   KateSessionChooserItem *item = (KateSessionChooserItem *) m_sessions->selectedItem ();
00843 
00844   if (!item || item->session->sessionFileRelative() == "default.katesession")
00845     return;
00846 
00847   bool ok = false;
00848   QString name = KInputDialog::getText (i18n("Specify New Name for Session"), i18n("Session name:"), item->session->sessionName(), &ok);
00849 
00850   if (!ok)
00851     return;
00852 
00853   if (name.isEmpty())
00854   {
00855     KMessageBox::error (0, i18n("To save a session, you must specify a name."), i18n ("Missing Session Name"));
00856     return;
00857   }
00858 
00859   item->session->rename (name);
00860   updateSessionList ();
00861 }
00862 
00863 void KateSessionManageDialog::del ()
00864 {
00865   KateSessionChooserItem *item = (KateSessionChooserItem *) m_sessions->selectedItem ();
00866 
00867   if (!item || item->session->sessionFileRelative() == "default.katesession")
00868     return;
00869 
00870   QFile::remove (item->session->sessionFile());
00871   KateSessionManager::self()->updateSessionList ();
00872   updateSessionList ();
00873 }
00874 
00875 void KateSessionManageDialog::updateSessionList ()
00876 {
00877   m_sessions->clear ();
00878 
00879   KateSessionList &slist (KateSessionManager::self()->sessionList());
00880   for (unsigned int i=0; i < slist.count(); ++i)
00881   {
00882     new KateSessionChooserItem (m_sessions, slist[i]);
00883   }
00884 }
00885 
00886 //END MANAGE DIALOG
00887 
00888 
00889 KateSessionsAction::KateSessionsAction(const QString& text, QObject* parent, const char* name )
00890   : KActionMenu(text, parent, name)
00891 {
00892   connect(popupMenu(),SIGNAL(aboutToShow()),this,SLOT(slotAboutToShow()));
00893 }
00894 
00895 void KateSessionsAction::slotAboutToShow()
00896 {
00897   popupMenu()->clear ();
00898 
00899   KateSessionList &slist (KateSessionManager::self()->sessionList());
00900   for (unsigned int i=0; i < slist.count(); ++i)
00901   {
00902       popupMenu()->insertItem (
00903           slist[i]->sessionName(),
00904           this, SLOT (openSession (int)), 0,
00905           i );
00906   }
00907 }
00908 
00909 void KateSessionsAction::openSession (int i)
00910 {
00911   KateSessionList &slist (KateSessionManager::self()->sessionList());
00912 
00913   if ((uint)i >= slist.count())
00914     return;
00915 
00916   KateSessionManager::self()->activateSession(slist[(uint)i]);
00917 }
00918 // kate: space-indent on; indent-width 2; replace-tabs on; mixed-indent off;
KDE Home | KDE Accessibility Home | Description of Access Keys