From 114a878c64ce6f8223cfd22d76a20eb16d177e5e Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdevelop@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- parts/bookmarks/bookmarks_part.cpp | 568 +++++++++++++++++++++++++++++++++++++ 1 file changed, 568 insertions(+) create mode 100644 parts/bookmarks/bookmarks_part.cpp (limited to 'parts/bookmarks/bookmarks_part.cpp') diff --git a/parts/bookmarks/bookmarks_part.cpp b/parts/bookmarks/bookmarks_part.cpp new file mode 100644 index 00000000..f23863af --- /dev/null +++ b/parts/bookmarks/bookmarks_part.cpp @@ -0,0 +1,568 @@ +/*************************************************************************** + * Copyright (C) 2003 by Jens Dagerbo * + * jens.dagerbo@swipnet.se * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include "domutil.h" + +#include "bookmarks_widget.h" +#include "bookmarks_part.h" +#include "bookmarks_settings.h" +#include "bookmarks_config.h" + +#include +#include + +#define BOOKMARKSETTINGSPAGE 1 + +typedef KDevGenericFactory BookmarksFactory; +static const KDevPluginInfo data("kdevbookmarks"); +K_EXPORT_COMPONENT_FACTORY( libkdevbookmarks, BookmarksFactory( data ) ) + +BookmarksPart::BookmarksPart(QObject *parent, const char *name, const QStringList& ) + : KDevPlugin(&data, parent, name ? name : "BookmarksPart" ) +{ + setInstance(BookmarksFactory::instance()); + + _widget = new BookmarksWidget(this); + + _widget->setCaption(i18n("Bookmarks")); + _widget->setIcon(SmallIcon( info()->icon() )); + + _marksChangeTimer = new QTimer( this ); + + QWhatsThis::add(_widget, i18n("Bookmarks

" + "The bookmark viewer shows all the source bookmarks in the project.")); + + mainWindow()->embedSelectView(_widget, i18n("Bookmarks"), i18n("Source bookmarks")); + + _editorMap.setAutoDelete( true ); + _settingMarks = false; + + connect( partController(), SIGNAL( partAdded( KParts::Part * ) ), this, SLOT( partAdded( KParts::Part * ) ) ); + + _configProxy = new ConfigWidgetProxy( core() ); + _configProxy->createProjectConfigPage( i18n("Bookmarks"), BOOKMARKSETTINGSPAGE, info()->icon() ); + connect( _configProxy, SIGNAL(insertConfigWidget(const KDialogBase*, QWidget*, unsigned int )), + this, SLOT(insertConfigWidget(const KDialogBase*, QWidget*, unsigned int )) ); + + connect( _widget, SIGNAL( removeAllBookmarksForURL( const KURL & ) ), + this, SLOT( removeAllBookmarksForURL( const KURL & ) ) ); + connect( _widget, SIGNAL( removeBookmarkForURL( const KURL &, int ) ), + this, SLOT( removeBookmarkForURL( const KURL &, int ) ) ); + + connect( _marksChangeTimer, SIGNAL( timeout() ), this, SLOT( marksChanged() ) ); + + _config = new BookmarksConfig; + _config->readConfig(); + + storeBookmarksForAllURLs(); + updateContextStringForAll(); + _widget->update( _editorMap ); +} + +BookmarksPart::~BookmarksPart() +{ + if( _widget ) { + mainWindow()->removeView( _widget ); + delete _widget; + } + delete _config; + delete _configProxy; +} + +void BookmarksPart::partAdded( KParts::Part * part ) +{ + //kdDebug(0) << "BookmarksPart::partAdded()" << endl; + + if ( KParts::ReadOnlyPart * ro_part = dynamic_cast( part ) ) + { + if ( setBookmarksForURL( ro_part ) ) + { + updateContextStringForURL( ro_part ); + if ( EditorData * data = _editorMap.find( ro_part->url().path() ) ) + { + _widget->updateURL( data ); + } + + // connect to this editor + KTextEditor::Document * doc = static_cast( ro_part ); + connect( doc, SIGNAL( marksChanged() ), this, SLOT( marksEvent() ) ); + + // workaround for a katepart oddity where it drops all bookmarks on 'reload' + connect( doc, SIGNAL( completed() ), this, SLOT( reload() ) ); + } + } +} + +void BookmarksPart::reload() +{ + //kdDebug(0) << "BookmarksPart::reload()" << endl; + + QObject * senderobj = const_cast( sender() ); + if ( KParts::ReadOnlyPart * ro_part = dynamic_cast( senderobj ) ) + { + if ( partIsSane( ro_part ) ) + { + setBookmarksForURL( ro_part ); + } + } +} + +void BookmarksPart::marksEvent() +{ + //kdDebug(0) << "BookmarksPart::marksEvent()" << endl; + + if ( ! _settingMarks ) + { + QObject * senderobj = const_cast( sender() ); + KParts::ReadOnlyPart * ro_part = dynamic_cast( senderobj ); + + if ( partIsSane( ro_part ) && !_dirtyParts.contains( ro_part ) ) + { + _dirtyParts.push_back( ro_part ); + _marksChangeTimer->start( 1000, true ); + } + } +} + +void BookmarksPart::marksChanged() +{ + //kdDebug(0) << "BookmarksPart::marksChanged()" << endl; + + QValueListIterator it = _dirtyParts.begin(); + while ( it != _dirtyParts.end() ) + { + KParts::ReadOnlyPart * ro_part = *it; + if ( partIsSane( ro_part ) ) + { + if ( dynamic_cast( ro_part ) ) + { + if ( EditorData * data = storeBookmarksForURL( ro_part ) ) + { + updateContextStringForURL( ro_part ); + _widget->updateURL( data ); + } + else + { + _widget->removeURL( ro_part->url() ); + } + } + } + ++it; + } + _dirtyParts.clear(); +} + +void BookmarksPart::restorePartialProjectSession( const QDomElement * el ) +{ + //kdDebug(0) << "BookmarksPart::restorePartialProjectSession()" << endl; + + if ( ! el ) return; + + QDomElement bookmarksList = el->namedItem( "bookmarks" ).toElement(); + if ( bookmarksList.isNull() ) return; + + QDomElement bookmark = bookmarksList.firstChild().toElement(); + while ( ! bookmark.isNull() ) + { + QString path = bookmark.attribute( "url" ); + if ( path != QString::null ) + { + EditorData * data = new EditorData; + data->url.setPath( path ); + + QDomElement mark = bookmark.firstChild().toElement(); + while ( ! mark.isNull() ) + { + QString line = mark.attribute( "line" ); + if ( line != QString::null ) + { + data->marks.append( qMakePair( line.toInt(), QString() ) ); + } + mark = mark.nextSibling().toElement(); + } + + if ( ! data->marks.isEmpty() ) + { + _editorMap.insert( data->url.path(), data ); + } + else + { + delete data; + } + } + bookmark = bookmark.nextSibling().toElement(); + } + setBookmarksForAllURLs(); + updateContextStringForAll(); + _widget->update( _editorMap ); +} + +void BookmarksPart::savePartialProjectSession( QDomElement * el ) +{ + //kdDebug(0) << "BookmarksPart::savePartialProjectSession()" << endl; + + if ( ! el ) return; + + QDomDocument domDoc = el->ownerDocument(); + if ( domDoc.isNull() ) return; + + QDomElement bookmarksList = domDoc.createElement( "bookmarks" ); + + QDictIterator it( _editorMap ); + while ( it.current() ) + { + QDomElement bookmark = domDoc.createElement( "bookmark" ); + bookmark.setAttribute( "url", it.current()->url.path() ); + bookmarksList.appendChild( bookmark ); + + QValueListIterator< QPair > it2 = it.current()->marks.begin(); + while ( it2 != it.current()->marks.end() ) + { + QDomElement line = domDoc.createElement( "mark" ); + line.setAttribute( "line", (*it2).first ); + bookmark.appendChild( line ); + ++it2; + } + ++it; + } + + if ( ! bookmarksList.isNull() ) + { + el->appendChild( bookmarksList ); + } +} + +void BookmarksPart::removeAllBookmarksForURL( KURL const & url ) +{ + //kdDebug(0) << "BookmarksPart::removeAllBookmarksForURL()" << endl; + + _editorMap.remove( url.path() ); + + setBookmarksForURL( partForURL( url ) ); + _widget->removeURL( url ); +} + +void BookmarksPart::removeBookmarkForURL( KURL const & url, int line ) +{ + //kdDebug(0) << "BookmarksPart::removeBookmarkForURL()" << endl; + + if ( EditorData * data = _editorMap.find( url.path() ) ) + { + QValueListIterator< QPair > it = data->marks.begin(); + while ( it != data->marks.end() ) + { + if ( (*it).first == line ) + { + data->marks.remove( it ); + break; + } + ++it; + } + + if ( data->marks.isEmpty() ) + { + removeAllBookmarksForURL( url ); + } + else + { + setBookmarksForURL( partForURL( url ) ); + _widget->updateURL( data ); + } + } +} + +void BookmarksPart::updateContextStringForURL( KParts::ReadOnlyPart * ro_part ) +{ + if ( ! ro_part ) return; + + KTextEditor::EditInterface * ed = + dynamic_cast( ro_part ); + + EditorData * data = _editorMap.find( ro_part->url().path() ); + + if ( ! ( data && ed ) ) return; + + QValueListIterator< QPair > it = data->marks.begin(); + while ( it != data->marks.end() ) + { + (*it).second = ed->textLine( (*it).first ); + ++it; + } +} + +void BookmarksPart::updateContextStringForURL( KURL const & url ) +{ + updateContextStringForURL( partForURL( url ) ); +} + +void BookmarksPart::updateContextStringForAll() +{ + QDictIterator it( _editorMap ); + while ( it.current() ) + { + if ( ! it.current()->marks.isEmpty() ) + { + updateContextStringForURL( it.current()->url ); + } + ++it; + } +} + +bool BookmarksPart::setBookmarksForURL( KParts::ReadOnlyPart * ro_part ) +{ + if ( KTextEditor::MarkInterface * mi = dynamic_cast(ro_part) ) + { + clearBookmarksForURL( ro_part ); + + _settingMarks = true; + + if ( EditorData * data = _editorMap.find( ro_part->url().path() ) ) + { + // we've seen this one before, apply stored bookmarks + + QValueListIterator< QPair > it = data->marks.begin(); + while ( it != data->marks.end() ) + { + mi->addMark( (*it).first, KTextEditor::MarkInterface::markType01 ); + ++it; + } + } + _settingMarks = false; + + // true == this is a MarkInterface + return true; + } + return false; +} + +// Note: This method is only a convenience method to clear the bookmark marks, +// the way a hypothetical KTextEditor::MarkInterface::clearMarks( uint markType ) +// would work. +bool BookmarksPart::clearBookmarksForURL( KParts::ReadOnlyPart * ro_part ) +{ + if ( KTextEditor::MarkInterface * mi = dynamic_cast(ro_part) ) + { + _settingMarks = true; + + QPtrList marks = mi->marks(); + QPtrListIterator it( marks ); + while ( it.current() ) + { + if ( it.current()->type & KTextEditor::MarkInterface::markType01 ) + { + mi->removeMark( it.current()->line, KTextEditor::MarkInterface::markType01 ); + } + ++it; + } + + _settingMarks = false; + + // true == this is a MarkInterface + return true; + } + return false; +} + +EditorData * BookmarksPart::storeBookmarksForURL( KParts::ReadOnlyPart * ro_part ) +{ + //kdDebug(0) << "BookmarksPart::storeBookmarksForURL()" << endl; + + if ( KTextEditor::MarkInterface * mi = dynamic_cast( ro_part ) ) + { + EditorData * data = new EditorData; + data->url = ro_part->url(); + + // removing previous data for this url, if any + _editorMap.remove( data->url.path() ); + + QPtrList marks = mi->marks(); + QPtrListIterator it( marks ); + while ( it.current() ) + { + if ( it.current()->type & KTextEditor::MarkInterface::markType01 ) + { + int line = it.current()->line; + data->marks.append( qMakePair( line, QString() ) ); + } + ++it; + } + + if ( ! data->marks.isEmpty() ) + { + _editorMap.insert( data->url.path(), data ); + } + else + { + delete data; + data = 0; + } + return data; + } + return 0; +} + +void BookmarksPart::setBookmarksForAllURLs() +{ + if( const QPtrList * partlist = partController()->parts() ) + { + QPtrListIterator it( *partlist ); + while ( KParts::Part* part = it.current() ) + { + if ( KParts::ReadOnlyPart * ro_part = dynamic_cast( part ) ) + { + setBookmarksForURL( ro_part ); + } + ++it; + } + } +} + +void BookmarksPart::storeBookmarksForAllURLs() +{ + if( const QPtrList * partlist = partController()->parts() ) + { + QPtrListIterator it( *partlist ); + while ( KParts::Part* part = it.current() ) + { + if ( KParts::ReadOnlyPart * ro_part = dynamic_cast( part ) ) + { + storeBookmarksForURL( ro_part ); + } + ++it; + } + } +} + +// reimplemented from PartController::partForURL to avoid linking +KParts::ReadOnlyPart * BookmarksPart::partForURL( KURL const & url ) +{ + QPtrListIterator it( *partController()->parts() ); + while( it.current() ) + { + KParts::ReadOnlyPart *ro_part = dynamic_cast(it.current()); + if (ro_part && url == ro_part->url()) + { + return ro_part; + } + ++it; + } + return 0; +} + +bool BookmarksPart::partIsSane( KParts::ReadOnlyPart * ro_part ) +{ + return ( ro_part != 0 ) && + partController()->parts()->contains( ro_part) && + !ro_part->url().path().isEmpty(); +} + +void BookmarksPart::insertConfigWidget( const KDialogBase * dlg, QWidget * page, unsigned int pagenumber ) +{ + kdDebug() << k_funcinfo << endl; + + if ( pagenumber == BOOKMARKSETTINGSPAGE ) + { + BookmarkSettings * w = new BookmarkSettings( this, page ); + connect( dlg, SIGNAL(okClicked()), w, SLOT(slotAccept()) ); + } +} + +//////////////////////////////////////////// + +QStringList BookmarksPart::getContextFromStream( QTextStream & istream, unsigned int line, unsigned int context ) +{ + kdDebug() << k_funcinfo << endl; + + int startline = context > line ? 0 : line - context; + int endline = line + context; + + int n = 0; + QStringList list; + while ( !istream.atEnd() ) + { + QString templine = istream.readLine(); + if ( (n >= startline) && ( n <= endline ) ) + { + list << templine; + } + n++; + } + + // maybe pad empty lines to the tail + while( n < endline ) + { + list.append( " " ); + n++; + } + + // maybe pad empty lines to the head + while( list.count() < ( context * 2 + 1) ) + { + list.prepend( " " ); + } + + return list; +} + +QStringList BookmarksPart::getContext( KURL const & url, unsigned int line, unsigned int context ) +{ + // if the file is open - get the line from the editor buffer + if ( KTextEditor::EditInterface * ei = dynamic_cast( partForURL( url ) ) ) + { + kdDebug() << "the file is open - get the line from the editor buffer" << endl; + + QString ibuffer = ei->text(); + QTextStream istream( &ibuffer, IO_ReadOnly ); + return getContextFromStream( istream, line, context ); + } + else if ( url.isLocalFile() ) // else the file is not open - get the line from the file on disk + { + kdDebug() << "the file is not open - get the line from the file on disk" << endl; + + QFile file( url.path() ); + QString buffer; + + if ( file.open( IO_ReadOnly ) ) + { + QTextStream istream( &file ); + return getContextFromStream( istream, line, context ); + } + } + return QStringList( i18n("Could not find file") ); +} + +BookmarksConfig * BookmarksPart::config( ) +{ + return _config; +} + +#include "bookmarks_part.moc" + +// kate: space-indent off; indent-width 4; tab-width 4; show-tabs off; -- cgit v1.2.1