katedocmanager.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00099 if (activeId == id)
00100 {
00101
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
00189 return findDocumentByUrl (url) != 0;
00190 }
00191
00192 Kate::Document *KateDocManager::openURL (const KURL& url,const QString &encoding, uint *id)
00193 {
00194
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 emit initialDocumentReplaced();
00207
00208 return doc;
00209 }
00210
00211 Kate::Document *doc = findDocumentByUrl (url);
00212 if ( !doc )
00213 {
00214 doc = (Kate::Document *)createDoc ();
00215
00216 doc->setEncoding(encoding.isNull() ? Kate::Document::defaultEncoding() : encoding);
00217
00218 doc->openURL(url);
00219 }
00220
00221 if (id)
00222 *id=doc->documentNumber();
00223
00224 return doc;
00225 }
00226
00227 bool KateDocManager::closeDocument(class Kate::Document *doc)
00228 {
00229 if (!doc) return false;
00230
00231 if (!doc->closeURL()) return false;
00232
00233 QPtrList<Kate::View> closeList;
00234 uint documentNumber = doc->documentNumber();
00235
00236 for (uint i=0; i < ((KateApp *)kapp)->mainWindows (); i++ )
00237 {
00238 ((KateApp *)kapp)->kateMainWindow(i)->kateViewManager()->closeViews(documentNumber);
00239 }
00240
00241 deleteDoc (doc);
00242
00243 return true;
00244 }
00245
00246 bool KateDocManager::closeDocument(uint n)
00247 {
00248 return closeDocument(document(n));
00249 }
00250
00251 bool KateDocManager::closeDocumentWithID(uint id)
00252 {
00253 return closeDocument(documentWithID(id));
00254 }
00255
00256 bool KateDocManager::closeAllDocuments()
00257 {
00258 bool res = true;
00259
00260 while (!m_docList.isEmpty() && res)
00261 if (! closeDocument(m_docList.at(0)) )
00262 res = false;
00263
00264 return res;
00265 }
00266
00267 bool KateDocManager::queryCloseDocuments(KateMainWindow *w)
00268 {
00269 Kate::Document *doc;
00270 for (QPtrListIterator<Kate::Document> it(m_docList); (doc=it.current())!=0; ++it)
00271 {
00272 if (doc->url().isEmpty() && doc->isModified())
00273 {
00274 int msgres=KMessageBox::warningYesNoCancel( w,
00275 i18n("<p>The document '%1' has been modified, but not saved."
00276 "<p>Do you want to keep it?").arg( doc->docName() ),
00277 i18n("Unsaved Document") );
00278
00279 if (msgres==KMessageBox::Cancel)
00280 return false;
00281
00282 if (msgres==KMessageBox::Yes)
00283 {
00284 KEncodingFileDialog::Result r=KEncodingFileDialog::getSaveURLAndEncoding(
00285 KTextEditor::encodingInterface(doc)->encoding(),QString::null,QString::null,w,i18n("Save As"));
00286
00287 doc->setEncoding( r.encoding );
00288
00289 if (!r.URLs.isEmpty())
00290 {
00291 KURL tmp = r.URLs.first();
00292
00293 if ( !doc->saveAs( tmp ) )
00294 return false;
00295 }
00296 else
00297 return false;
00298 }
00299 }
00300 else
00301 {
00302 if (!doc->queryClose())
00303 return false;
00304 }
00305 }
00306
00307 return true;
00308 }
00309
00310
00311 void KateDocManager::saveDocumentList (KConfig* config)
00312 {
00313 QString prevGrp=config->group();
00314 config->setGroup ("Open Documents");
00315 QString grp = config->group();
00316
00317 config->writeEntry ("Count", m_docList.count());
00318
00319 int i=0;
00320 for ( Kate::Document *doc = m_docList.first(); doc; doc = m_docList.next() )
00321 {
00322 config->setGroup(QString("Document %1").arg(i));
00323 doc->writeSessionConfig(config);
00324 config->setGroup(grp);
00325
00326 i++;
00327 }
00328
00329 config->setGroup(prevGrp);
00330 }
00331
00332 void KateDocManager::restoreDocumentList (KConfig* config)
00333 {
00334 QString prevGrp=config->group();
00335 config->setGroup ("Open Documents");
00336 QString grp = config->group();
00337
00338 int count = config->readNumEntry("Count");
00339
00340 QProgressDialog *pd=new QProgressDialog(
00341 i18n("Reopening files from the last session..."),
00342 QString::null,
00343 count,
00344 0,
00345 "openprog");
00346
00347 bool first = true;
00348 for (int i=0; i < count; i++)
00349 {
00350 config->setGroup(QString("Document %1").arg(i));
00351 Kate::Document *doc = 0;
00352
00353 if (first)
00354 {
00355 first = false;
00356 doc = document (0);
00357 }
00358 else
00359 doc = createDoc ();
00360
00361 doc->readSessionConfig(config);
00362 config->setGroup (grp);
00363
00364 pd->setProgress(pd->progress()+1);
00365 kapp->processEvents();
00366 }
00367
00368 delete pd;
00369
00370 config->setGroup(prevGrp);
00371 }
00372
00373 void KateDocManager::slotModifiedOnDisc (Kate::Document *doc, bool b, unsigned char reason)
00374 {
00375 if (m_docInfos[doc])
00376 {
00377 m_docInfos[doc]->modifiedOnDisc = b;
00378 m_docInfos[doc]->modifiedOnDiscReason = reason;
00379 }
00380 }
This file is part of the documentation for kate Library Version 3.2.3.