kate Library API Documentation

katedocmanager.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Christoph Cullmann <cullmann@kde.org>
00003    Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "katedocmanager.h"
00021 #include "katedocmanager.moc"
00022 #include "kateapp.h"
00023 #include "katemainwindow.h"
00024 #include "kateviewmanager.h"
00025 #include "katedocmanageriface.h"
00026 
00027 #include <kate/view.h>
00028 
00029 #include <kparts/factory.h>
00030 
00031 #include <klocale.h>
00032 #include <kdebug.h>
00033 #include <kconfig.h>
00034 #include <kapplication.h>
00035 #include <klibloader.h>
00036 
00037 #include <qtextcodec.h>
00038 #include <qprogressdialog.h>
00039 #include <kmessagebox.h>
00040 #include <kencodingfiledialog.h>
00041 #include <ktexteditor/encodinginterface.h>
00042 
00043 KateDocManager::KateDocManager (QObject *parent) : QObject (parent)
00044 {
00045   m_factory = (KParts::Factory *) KLibLoader::self()->factory ("libkatepart");
00046 
00047   m_documentManager = new Kate::DocumentManager (this);
00048   m_docList.setAutoDelete(true);
00049   m_docDict.setAutoDelete(false);
00050   m_docInfos.setAutoDelete(true);
00051 
00052   m_dcop = new KateDocManagerDCOPIface (this);
00053 
00054   createDoc ();
00055 }
00056 
00057 KateDocManager::~KateDocManager ()
00058 {
00059   delete m_dcop;
00060 }
00061 
00062 Kate::Document *KateDocManager::createDoc ()
00063 {
00064   KTextEditor::Document *doc = (KTextEditor::Document *) m_factory->createPart (0, "", this, "", "KTextEditor::Document");
00065   
00066   m_docList.append((Kate::Document *)doc);
00067   m_docDict.insert (doc->documentNumber(), (Kate::Document *)doc);
00068   m_docInfos.insert (doc, new KateDocumentInfo ());
00069 
00070   if (m_docList.count() < 2)
00071     ((Kate::Document *)doc)->readConfig(kapp->config());
00072 
00073   emit documentCreated ((Kate::Document *)doc);
00074   emit m_documentManager->documentCreated ((Kate::Document *)doc);
00075 
00076   connect(doc,SIGNAL(modifiedOnDisc(Kate::Document *, bool, unsigned char)),this,SLOT(slotModifiedOnDisc(Kate::Document *, bool, unsigned char)));
00077 
00078   return (Kate::Document *)doc;
00079 }
00080 
00081 void KateDocManager::deleteDoc (Kate::Document *doc)
00082 {
00083   uint id = doc->documentNumber();
00084   uint activeId = 0;
00085   if (m_currentDoc)
00086     activeId = m_currentDoc->documentNumber ();
00087 
00088   if (m_docList.count() < 2)
00089     doc->writeConfig(kapp->config());
00090 
00091   m_docInfos.remove (doc);
00092   m_docDict.remove (id);
00093   m_docList.remove (doc);
00094 
00095   emit documentDeleted (id);
00096   emit m_documentManager->documentDeleted (id);
00097 
00098   // ohh, current doc was deleted
00099   if (activeId == id)
00100   {
00101     // special case of documentChanged, no longer any doc here !
00102     m_currentDoc = 0;
00103     
00104     emit documentChanged ();
00105     emit m_documentManager->documentChanged ();
00106   }
00107 }
00108 
00109 Kate::Document *KateDocManager::document (uint n)
00110 {
00111   return m_docList.at(n);
00112 }
00113 
00114 Kate::Document *KateDocManager::activeDocument ()
00115 {
00116   return m_currentDoc;
00117 }
00118 
00119 void KateDocManager::setActiveDocument (Kate::Document *doc)
00120 {
00121   if (!doc)
00122     return;
00123 
00124   if (m_currentDoc && (m_currentDoc->documentNumber() == doc->documentNumber()))
00125     return;
00126 
00127   m_currentDoc = doc;
00128   
00129   emit documentChanged ();
00130   emit m_documentManager->documentChanged ();
00131 }
00132 
00133 Kate::Document *KateDocManager::firstDocument ()
00134 {
00135   return m_docList.first();
00136 }
00137 
00138 Kate::Document *KateDocManager::nextDocument ()
00139 {
00140   return m_docList.next();
00141 }
00142 
00143 Kate::Document *KateDocManager::documentWithID (uint id)
00144 {
00145   return m_docDict[id];
00146 }
00147 
00148 const KateDocumentInfo *KateDocManager::documentInfo (Kate::Document *doc)
00149 {
00150   return m_docInfos[doc];
00151 }
00152 
00153 int KateDocManager::findDocument (Kate::Document *doc)
00154 {
00155   return m_docList.find (doc);
00156 }
00157 
00158 uint KateDocManager::documents ()
00159 {
00160   return m_docList.count ();
00161 }
00162 
00163 int KateDocManager::findDocument ( KURL url )
00164 {
00165   QPtrListIterator<Kate::Document> it(m_docList);
00166 
00167   for (; it.current(); ++it)
00168   {
00169     if ( it.current()->url() == url)
00170       return it.current()->documentNumber();
00171   }
00172   return -1;
00173 }
00174 
00175 Kate::Document *KateDocManager::findDocumentByUrl( KURL url )
00176 {
00177   for (QPtrListIterator<Kate::Document> it(m_docList); it.current(); ++it)
00178   {
00179     if ( it.current()->url() == url)
00180       return it.current();
00181   }
00182   
00183   return 0L;
00184 }
00185 
00186 bool KateDocManager::isOpen(KURL url)
00187 {
00188   // return just if we found some document with this url
00189   return findDocumentByUrl (url) != 0;
00190 }
00191 
00192 Kate::Document *KateDocManager::openURL (const KURL& url,const QString &encoding, uint *id)
00193 {
00194   // special handling if still only the first initial doc is there
00195   if (!documentList().isEmpty() && (documentList().count() == 1) && (!documentList().at(0)->isModified() && documentList().at(0)->url().isEmpty()))
00196   {
00197     Kate::Document* doc = documentList().getFirst();
00198 
00199     doc->setEncoding(encoding.isNull() ? Kate::Document::defaultEncoding() : encoding);
00200 
00201     doc->openURL (url);
00202 
00203     if (id)
00204       *id=doc->documentNumber();
00205 
00206     return doc;
00207  }
00208 
00209   Kate::Document *doc = findDocumentByUrl (url);
00210   if ( !doc )
00211   {
00212     doc = (Kate::Document *)createDoc ();
00213 
00214     doc->setEncoding(encoding.isNull() ? Kate::Document::defaultEncoding() : encoding);
00215 
00216     doc->openURL(url);
00217   }
00218   
00219   if (id)
00220     *id=doc->documentNumber();
00221 
00222   return doc;
00223 }
00224 
00225 bool KateDocManager::closeDocument(class Kate::Document *doc)
00226 {
00227   if (!doc) return false;
00228 
00229   if (!doc->closeURL()) return false;
00230 
00231   QPtrList<Kate::View> closeList;
00232   uint documentNumber = doc->documentNumber();
00233 
00234   for (uint i=0; i < ((KateApp *)kapp)->mainWindows (); i++ )
00235   {
00236      ((KateApp *)kapp)->kateMainWindow(i)->kateViewManager()->closeViews(documentNumber);
00237   }
00238 
00239   deleteDoc (doc);
00240 
00241   return true;
00242 }
00243 
00244 bool KateDocManager::closeDocument(uint n)
00245 {
00246   return closeDocument(document(n));
00247 }
00248 
00249 bool KateDocManager::closeDocumentWithID(uint id)
00250 {
00251   return closeDocument(documentWithID(id));
00252 }
00253 
00254 bool KateDocManager::closeAllDocuments()
00255 {
00256   bool res = true;
00257 
00258   while (!m_docList.isEmpty() && res)
00259     if (! closeDocument(m_docList.at(0)) )
00260       res = false;
00261 
00262   return res;
00263 }
00264 
00265 bool KateDocManager::queryCloseDocuments(KateMainWindow *w)
00266 {  
00267   Kate::Document  *doc;
00268   for (QPtrListIterator<Kate::Document> it(m_docList); (doc=it.current())!=0; ++it)
00269   {
00270     if (doc->url().isEmpty() && doc->isModified())
00271     {
00272       int msgres=KMessageBox::warningYesNoCancel( w,
00273                   i18n("<p>The document '%1' has been modified, but not saved."
00274                        "<p>Do you want to keep it?").arg( doc->docName() ),
00275                     i18n("Unsaved Document") );
00276   
00277       if (msgres==KMessageBox::Cancel)
00278         return false;
00279       
00280       if (msgres==KMessageBox::Yes)
00281       {
00282         KEncodingFileDialog::Result r=KEncodingFileDialog::getSaveURLAndEncoding(
00283               KTextEditor::encodingInterface(doc)->encoding(),QString::null,QString::null,w,i18n("Save As"));
00284 
00285         doc->setEncoding( r.encoding );
00286         
00287         if (!r.URLs.isEmpty())
00288         {
00289           KURL tmp = r.URLs.first();
00290           
00291           if ( !doc->saveAs( tmp ) )
00292             return false;
00293         }
00294         else
00295           return false;
00296       }
00297     }
00298     else
00299     {
00300       if (!doc->queryClose())
00301         return false;
00302     }
00303   }
00304   
00305   return true;
00306 }
00307 
00308 
00309 void KateDocManager::saveDocumentList (KConfig* config)
00310 {
00311   QString prevGrp=config->group();
00312   config->setGroup ("Open Documents");
00313   QString grp = config->group();
00314 
00315   config->writeEntry ("Count", m_docList.count());
00316 
00317   int i=0;
00318   for ( Kate::Document *doc = m_docList.first(); doc; doc = m_docList.next() )
00319   {
00320     config->setGroup(QString("Document %1").arg(i));
00321     doc->writeSessionConfig(config);
00322     config->setGroup(grp);
00323 
00324     i++;
00325   }
00326   
00327   config->setGroup(prevGrp);
00328 }
00329 
00330 void KateDocManager::restoreDocumentList (KConfig* config)
00331 {
00332   QString prevGrp=config->group();
00333   config->setGroup ("Open Documents");
00334   QString grp = config->group();
00335 
00336   int count = config->readNumEntry("Count");
00337 
00338   QProgressDialog *pd=new QProgressDialog(
00339         i18n("Reopening files from the last session..."),
00340         QString::null,
00341         count,
00342         0,
00343         "openprog");
00344 
00345   bool first = true;
00346   for (int i=0; i < count; i++)
00347   {
00348     config->setGroup(QString("Document %1").arg(i));
00349     Kate::Document *doc = 0;
00350     
00351     if (first)
00352     {
00353       first = false;
00354       doc = document (0);
00355     }
00356     else
00357       doc = createDoc ();
00358   
00359     doc->readSessionConfig(config);
00360     config->setGroup (grp);
00361 
00362     pd->setProgress(pd->progress()+1);
00363     kapp->processEvents();
00364   }
00365 
00366   delete pd;
00367 
00368   config->setGroup(prevGrp);
00369 }
00370 
00371 void KateDocManager::slotModifiedOnDisc (Kate::Document *doc, bool b, unsigned char reason)
00372 {
00373   if (m_docInfos[doc])
00374   {
00375     m_docInfos[doc]->modifiedOnDisc = b;
00376     m_docInfos[doc]->modifiedOnDiscReason = reason;
00377   }
00378 }
KDE Logo
This file is part of the documentation for kate Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 29 21:20:36 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003