• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kate

katesession.cpp

Go to the documentation of this file.
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(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, QMessageBox::Question,
00391                               i18n("Save current session?"), QStringList(),
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   KConfig *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(QString ("MainWindow%1").arg(i));
00424     KateApp::self()->mainWindow(i)->saveProperties (sc);
00425   }
00426 
00427   sc->sync();
00428 
00429   if (rememberAsLast)
00430   {
00431     KConfig *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   KConfig *c = KateApp::self()->config();
00446   c->setGroup("General");
00447 
00448   // get last used session, default to default session
00449   QString lastSession (c->readEntry ("Last Session", "default.katesession"));
00450   QString 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   QString 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   QString 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 QListViewItem
00600 {
00601   public:
00602     KateSessionChooserItem (KListView *lv, KateSession::Ptr s)
00603      : QListViewItem (lv, s->sessionName())
00604      , session (s)
00605     {
00606       QString docs;
00607       docs.setNum (s->documents());
00608       setText (1, docs);
00609     }
00610 
00611     KateSession::Ptr session;
00612 };
00613 
00614 KateSessionChooser::KateSessionChooser (QWidget *parent, const QString &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"), "fileopen")
00624                   , KGuiItem (i18n ("New Session"), "filenew")
00625                 )
00626 {
00627   QHBox *page = new QHBox (this);
00628   page->setMinimumSize (400, 200);
00629   setMainWidget(page);
00630 
00631   QHBox *hb = new QHBox (page);
00632   hb->setSpacing (KDialog::spacingHint());
00633 
00634   QLabel *label = new QLabel (hb);
00635   label->setPixmap (UserIcon("sessionchooser"));
00636   label->setFrameStyle (QFrame::Panel | QFrame::Sunken);
00637 
00638   QVBox *vb = new QVBox (hb);
00639   vb->setSpacing (KDialog::spacingHint());
00640 
00641   m_sessions = new KListView (vb);
00642   m_sessions->addColumn (i18n("Session Name"));
00643   m_sessions->addColumn (i18n("Open Documents"));
00644   m_sessions->setResizeMode (QListView::AllColumns);
00645   m_sessions->setSelectionMode (QListView::Single);
00646   m_sessions->setAllColumnsShowFocus (true);
00647 
00648   connect (m_sessions, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
00649   connect (m_sessions, SIGNAL(doubleClicked(QListViewItem *, const QPoint &, int)), this, 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 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::User2, 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   connect (m_sessions, SIGNAL(doubleClicked(QListViewItem *, const QPoint &, int)), this, 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 (QWidget *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   QHBox *page = new QHBox (this);
00789   page->setMinimumSize (400, 200);
00790   setMainWidget(page);
00791 
00792   QHBox *hb = new QHBox (page);
00793   hb->setSpacing (KDialog::spacingHint());
00794 
00795   m_sessions = new KListView (hb);
00796   m_sessions->addColumn (i18n("Session Name"));
00797   m_sessions->addColumn (i18n("Open Documents"));
00798   m_sessions->setResizeMode (QListView::AllColumns);
00799   m_sessions->setSelectionMode (QListView::Single);
00800   m_sessions->setAllColumnsShowFocus (true);
00801 
00802   connect (m_sessions, SIGNAL(selectionChanged()), this, SLOT(selectionChanged()));
00803 
00804   updateSessionList ();
00805 
00806   QWidget *vb = new QWidget (hb);
00807   QVBoxLayout *vbl = new QVBoxLayout (vb);
00808   vbl->setSpacing (KDialog::spacingHint());
00809 
00810   m_rename = new KPushButton (i18n("&Rename..."), vb);
00811   connect (m_rename, SIGNAL(clicked()), this, SLOT(rename()));
00812   vbl->addWidget (m_rename);
00813 
00814   m_del = new KPushButton (KStdGuiItem::del (), vb);
00815   connect (m_del, SIGNAL(clicked()), this, 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   QString 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   QFile::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 QString& text, QObject* parent, const char* name )
00892   : KActionMenu(text, parent, name)
00893 {
00894   connect(popupMenu(),SIGNAL(aboutToShow()),this,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, 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;

kate

Skip menu "kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kate
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal