20 #include "katedocmanager.h"
21 #include "katedocmanager.moc"
23 #include "katemainwindow.h"
24 #include "kateviewmanager.h"
25 #include "katedocmanageriface.h"
26 #include "kateexternaltools.h"
27 #include "kateviewspacecontainer.h"
29 #include <kate/view.h>
31 #include <tdetexteditor/encodinginterface.h>
33 #include <tdeparts/factory.h>
35 #include <tdelocale.h>
37 #include <tdeconfig.h>
38 #include <klibloader.h>
40 #include <tdemessagebox.h>
41 #include <kencodingfiledialog.h>
42 #include <tdeio/job.h>
45 #include <tqdatetime.h>
46 #include <tqtextcodec.h>
47 #include <tqprogressdialog.h>
49 KateDocManager::KateDocManager (TQObject *parent)
51 , m_saveMetaInfos(true)
54 m_factory = (KParts::Factory *) KLibLoader::self()->factory (
"libkatepart");
57 m_docList.setAutoDelete(
true);
58 m_docDict.setAutoDelete(
false);
59 m_docInfos.setAutoDelete(
true);
61 m_dcop =
new KateDocManagerDCOPIface (
this);
63 m_metaInfos =
new TDEConfig(
"metainfos",
false,
false,
"appdata");
68 KateDocManager::~KateDocManager ()
71 if (!m_docList.isEmpty())
77 for (Kate::Document *doc = m_docList.first(); doc; doc = m_docList.next())
81 if (m_daysMetaInfos > 0)
83 TQStringList groups = m_metaInfos->groupList();
84 TQDateTime *def =
new TQDateTime(TQDate(1970, 1, 1));
85 for (TQStringList::Iterator it = groups.begin(); it != groups.end(); ++it)
87 m_metaInfos->setGroup(*it);
88 TQDateTime last = m_metaInfos->readDateTimeEntry(
"Time", def);
89 if (last.daysTo(TQDateTime::currentDateTime()) > m_daysMetaInfos)
90 m_metaInfos->deleteGroup(*it);
100 KateDocManager *KateDocManager::self ()
105 Kate::Document *KateDocManager::createDoc ()
107 KTextEditor::Document *doc = (KTextEditor::Document *) m_factory->createPart (0,
"",
this,
"",
"KTextEditor::Document");
109 m_docList.append((Kate::Document *)doc);
110 m_docDict.insert (doc->documentNumber(), (Kate::Document *)doc);
111 m_docInfos.insert (doc,
new KateDocumentInfo ());
113 if (m_docList.count() < 2)
114 ((Kate::Document *)doc)->readConfig(
KateApp::self()->config());
116 emit documentCreated ((Kate::Document *)doc);
117 emit m_documentManager->documentCreated ((Kate::Document *)doc);
119 connect(doc,TQ_SIGNAL(modifiedOnDisc(Kate::Document *,
bool,
unsigned char)),
this,TQ_SLOT(slotModifiedOnDisc(Kate::Document *,
bool,
unsigned char)));
120 return (Kate::Document *)doc;
123 void KateDocManager::deleteDoc (Kate::Document *doc)
125 uint
id = doc->documentNumber();
128 activeId = m_currentDoc->documentNumber ();
130 if (m_docList.count() < 2)
133 m_docInfos.remove (doc);
134 m_docDict.remove (
id);
135 m_docList.remove (doc);
137 emit documentDeleted (
id);
138 emit m_documentManager->documentDeleted (
id);
146 emit documentChanged ();
147 emit m_documentManager->documentChanged ();
151 Kate::Document *KateDocManager::document (uint n)
153 return m_docList.at(n);
156 Kate::Document *KateDocManager::activeDocument ()
161 void KateDocManager::setActiveDocument (Kate::Document *doc)
166 if (m_currentDoc && (m_currentDoc->documentNumber() == doc->documentNumber()))
171 emit documentChanged ();
172 emit m_documentManager->documentChanged ();
175 Kate::Document *KateDocManager::firstDocument ()
177 return m_docList.first();
180 Kate::Document *KateDocManager::nextDocument ()
182 return m_docList.next();
185 Kate::Document *KateDocManager::documentWithID (uint
id)
187 return m_docDict[id];
190 const KateDocumentInfo *KateDocManager::documentInfo (Kate::Document *doc)
192 return m_docInfos[doc];
195 int KateDocManager::findDocument (Kate::Document *doc)
197 return m_docList.find (doc);
200 uint KateDocManager::documents ()
202 return m_docList.count ();
205 int KateDocManager::findDocument ( KURL url )
207 TQPtrListIterator<Kate::Document> it(m_docList);
209 for (; it.current(); ++it)
211 if ( it.current()->url() == url)
212 return it.current()->documentNumber();
217 Kate::Document *KateDocManager::findDocumentByUrl( KURL url )
219 for (TQPtrListIterator<Kate::Document> it(m_docList); it.current(); ++it)
221 if ( it.current()->url() == url)
228 bool KateDocManager::isOpen(KURL url)
231 return findDocumentByUrl (url) != 0;
234 Kate::Document *KateDocManager::openURL (
const KURL& url,
const TQString &encoding, uint *
id,
bool isTempFile)
237 if (!documentList().isEmpty() && (documentList().count() == 1) && (!documentList().at(0)->isModified() && documentList().at(0)->url().isEmpty()))
239 Kate::Document* doc = documentList().getFirst();
241 doc->setEncoding(encoding);
243 if (!loadMetaInfos(doc, url))
247 *
id=doc->documentNumber();
249 if ( isTempFile && !url.isEmpty() && url.isLocalFile() )
251 TQFileInfo fi( url.path() );
254 m_tempFiles[ doc->documentNumber() ] = qMakePair(url, fi.lastModified());
255 kdDebug(13001)<<
"temporary file will be deleted after use unless modified: "<<url.prettyURL()<<endl;
259 connect(doc, TQ_SIGNAL(modStateChanged(Kate::Document *)),
this, TQ_SLOT(slotModChanged(Kate::Document *)));
261 emit initialDocumentReplaced();
266 Kate::Document *doc = findDocumentByUrl (url);
269 doc = (Kate::Document *)createDoc ();
271 doc->setEncoding(encoding.isNull() ? Kate::Document::defaultEncoding() : encoding);
273 if (!loadMetaInfos(doc, url))
278 *
id=doc->documentNumber();
280 if ( isTempFile && !url.isEmpty() && url.isLocalFile() )
282 TQFileInfo fi( url.path() );
285 m_tempFiles[ doc->documentNumber() ] = qMakePair(url, fi.lastModified());
286 kdDebug(13001)<<
"temporary file will be deleted after use unless modified: "<<url.prettyURL()<<endl;
293 bool KateDocManager::closeDocument(
class Kate::Document *doc,
bool closeURL)
295 if (!doc)
return false;
299 if (!doc->closeURL())
return false;
301 TQPtrList<Kate::View> closeList;
302 uint documentNumber = doc->documentNumber();
309 if ( closeURL && m_tempFiles.contains( documentNumber ) )
311 TQFileInfo fi( m_tempFiles[ documentNumber ].first.path() );
312 if ( fi.lastModified() <= m_tempFiles[ documentNumber ].second
318 TDEIO::del( m_tempFiles[ documentNumber ].first,
false,
false );
319 kdDebug(13001)<<
"Deleted temporary file "<<m_tempFiles[ documentNumber ].first<<endl;
320 m_tempFiles.remove( documentNumber );
323 kdWarning(13001)<<
"The supposedly temporary file "<<m_tempFiles[ documentNumber ].first.prettyURL()<<
" have been modified since loaded, and has not been deleted."<<endl;
329 if (m_docList.isEmpty())
335 bool KateDocManager::closeDocument(uint n)
337 return closeDocument(document(n));
340 bool KateDocManager::closeDocumentWithID(uint
id)
342 return closeDocument(documentWithID(
id));
345 bool KateDocManager::closeAllDocuments(
bool closeURL)
349 TQPtrList<Kate::Document> docs = m_docList;
356 while (!docs.isEmpty() && res)
357 if (! closeDocument(docs.at(0),closeURL) )
360 docs.remove ((uint)0);
367 KateApp::self()->
mainWindow(i)->viewManager()->containers()->at(s)->activateView (m_docList.at(0)->documentNumber());
373 TQPtrList<Kate::Document> KateDocManager::modifiedDocumentList() {
374 TQPtrList<Kate::Document> modified;
375 for (TQPtrListIterator<Kate::Document> it(m_docList); it.current(); ++it) {
376 Kate::Document *doc = it.current();
377 if (doc->isModified()) {
378 modified.append(doc);
385 bool KateDocManager::queryCloseDocuments(KateMainWindow *w)
387 uint docCount = m_docList.count();
388 for (TQPtrListIterator<Kate::Document> it(m_docList); it.current(); ++it)
390 Kate::Document *doc = it.current();
392 if (doc->url().isEmpty() && doc->isModified())
394 int msgres=KMessageBox::warningYesNoCancel( w,
395 i18n(
"<p>The document '%1' has been modified, but not saved."
396 "<p>Do you want to save your changes or discard them?").arg( doc->docName() ),
397 i18n(
"Close Document"), KStdGuiItem::save(), KStdGuiItem::discard() );
399 if (msgres==KMessageBox::Cancel)
402 if (msgres==KMessageBox::Yes)
404 KEncodingFileDialog::Result r=KEncodingFileDialog::getSaveURLAndEncoding(
405 KTextEditor::encodingInterface(doc)->encoding(),TQString::null,TQString::null,w,i18n(
"Save As"));
407 doc->setEncoding( r.encoding );
409 if (!r.URLs.isEmpty())
411 KURL tmp = r.URLs.first();
413 if ( !doc->saveAs( tmp ) )
422 if (!doc->queryClose())
428 if (m_docList.count() > docCount)
430 KMessageBox::information (w,
431 i18n (
"New file opened while trying to close Kate, closing aborted."),
432 i18n (
"Closing Aborted"));
440 void KateDocManager::saveAll()
442 for (TQPtrListIterator<Kate::Document> it(m_docList); it.current(); ++it)
443 if ( it.current()->isModified() && it.current()->views().count() )
444 ((Kate::View*)it.current()->views().first())->save();
447 void KateDocManager::saveDocumentList (TDEConfig* config)
449 TQString prevGrp=config->group();
450 config->setGroup (
"Open Documents");
451 TQString grp = config->group();
453 config->writeEntry (
"Count", m_docList.count());
456 for ( Kate::Document *doc = m_docList.first(); doc; doc = m_docList.next() )
458 long docListPos = doc->documentListPosition();
459 config->setGroup(TQString(
"Document %1").arg((docListPos<0)?i:docListPos));
460 doc->writeSessionConfig(config);
461 config->setGroup(grp);
466 config->setGroup(prevGrp);
469 void KateDocManager::restoreDocumentList (TDEConfig* config)
471 TQString prevGrp=config->group();
472 config->setGroup (
"Open Documents");
473 TQString grp = config->group();
475 unsigned int count = config->readUnsignedNumEntry(
"Count", 0);
479 config->setGroup(prevGrp);
483 TQProgressDialog *pd =
new TQProgressDialog(
484 i18n(
"Reopening files from the last session..."),
490 KWin::setOnDesktop(pd->winId(), KWin::currentDesktop());
491 pd->setCaption (
KateApp::self()->makeStdCaption(i18n(
"Starting Up")));
494 for (
unsigned int i=0; i < count; i++)
496 config->setGroup(TQString(
"Document %1").arg(i));
497 Kate::Document *doc = 0;
507 doc->readSessionConfig(config);
508 config->setGroup (grp);
510 pd->setProgress(pd->progress()+1);
516 config->setGroup(prevGrp);
519 void KateDocManager::slotModifiedOnDisc (Kate::Document *doc,
bool b,
unsigned char reason)
523 m_docInfos[doc]->modifiedOnDisc = b;
524 m_docInfos[doc]->modifiedOnDiscReason = reason;
528 void KateDocManager::slotModChanged(Kate::Document *doc)
536 bool KateDocManager::loadMetaInfos(Kate::Document *doc,
const KURL &url)
538 if (!m_saveMetaInfos)
541 if (!m_metaInfos->hasGroup(url.prettyURL()))
547 if (computeUrlMD5(url, md5))
549 m_metaInfos->setGroup(url.prettyURL());
550 TQString old_md5 = m_metaInfos->readEntry(
"MD5");
552 if ((
const char *)md5 == old_md5)
553 doc->readSessionConfig(m_metaInfos);
556 m_metaInfos->deleteGroup(url.prettyURL());
563 return ok && doc->url() == url;
569 void KateDocManager::saveMetaInfos(Kate::Document *doc)
573 if (!m_saveMetaInfos)
576 if (doc->isModified())
582 if (computeUrlMD5(doc->url(), md5))
584 m_metaInfos->setGroup(doc->url().prettyURL());
585 doc->writeSessionConfig(m_metaInfos);
586 m_metaInfos->writeEntry(
"MD5", (
const char *)md5);
587 m_metaInfos->writeEntry(
"Time", TQDateTime::currentDateTime());
592 bool KateDocManager::computeUrlMD5(
const KURL &url, TQCString &result)
594 TQFile f(url.path());
596 if (f.open(IO_ReadOnly))
603 md5.hexDigest(result);
KateMainWindow * mainWindow(uint n)
give back the window you want
static KateApp * self()
static accessor to avoid casting ;)
KateDocManager * documentManager()
accessor to document manager
uint mainWindows() const
give back number of existing main windows
This interface provides access to the Kate Document Manager.