kate Library API Documentation

autobookmarker.cpp

00001 /* 00002 This library is free software you can redistribute it and/or 00003 modify it under the terms of the GNU Library General Public 00004 License. 00005 00006 This library is distributed in the hope that it will be useful, 00007 but WITHOUT ANY WARRANTY; without even the implied warranty of 00008 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00009 Library General Public License for more details. 00010 00011 You should have received a copy of the GNU Library General Public License 00012 along with this library; see the file COPYING.LIB. If not, write to 00013 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00014 Boston, MA 02111-1307, USA. 00015 00016 --- 00017 file: autobookmarker.cpp 00018 00019 KTextEditor plugin to add bookmarks to documents. 00020 Copyright Anders Lund <anders.lund@lund.tdcadsl.dk>, 2003 00021 */ 00022 00023 //BEGIN includes 00024 #include "autobookmarker.h" 00025 00026 #include <ktexteditor/markinterfaceextension.h> 00027 #include <ktexteditor/editinterface.h> 00028 #include <ktexteditor/documentinfo.h> 00029 #include <ktexteditor/document.h> 00030 00031 #include <kaction.h> 00032 #include <kapplication.h> 00033 #include <kconfig.h> 00034 #include <kgenericfactory.h> 00035 #include <kiconloader.h> 00036 #include <klistview.h> 00037 #include <klocale.h> 00038 #include <kmimetype.h> 00039 #include <kmimetypechooser.h> 00040 #include <kprocess.h> 00041 #include <krun.h> 00042 #include <kstaticdeleter.h> 00043 #include <kurl.h> 00044 00045 #include <qcheckbox.h> 00046 #include <qlabel.h> 00047 #include <qlayout.h> 00048 #include <qlineedit.h> 00049 #include <qlistview.h> 00050 #include <qpopupmenu.h> 00051 #include <qpushbutton.h> 00052 #include <qtoolbutton.h> 00053 #include <qwhatsthis.h> 00054 #include <qregexp.h> 00055 00056 //#include <kdebug.h> 00057 //END includes 00058 00059 //BEGIN AutoBookmarker 00060 K_EXPORT_COMPONENT_FACTORY( ktexteditor_autobookmarker, KGenericFactory<AutoBookmarker>( "ktexteditor_autobookmarker" ) ) 00061 00062 AutoBookmarker::AutoBookmarker( QObject *parent, 00063 const char* name, 00064 const QStringList& /*args*/ ) 00065 : KTextEditor::Plugin ( (KTextEditor::Document*) parent, name ), 00066 KTextEditor::ConfigInterfaceExtension() 00067 { 00068 if ( parent ) 00069 connect( parent, SIGNAL( completed() ), this, SLOT( slotCompleted() ) ); 00070 } 00071 00072 void AutoBookmarker::addView(KTextEditor::View */*view*/) 00073 { 00074 } 00075 00076 void AutoBookmarker::removeView(KTextEditor::View */*view*/) 00077 { 00078 } 00079 00080 KTextEditor::ConfigPage * AutoBookmarker::configPage( uint /*number*/, QWidget *parent, const char *name ) 00081 { 00082 return new AutoBookmarkerConfigPage( parent, name ); 00083 } 00084 00085 QString AutoBookmarker::configPageName( uint /*p*/ ) const 00086 { 00087 // switch (p) 00088 // { 00089 // case 0: 00090 return i18n("AutoBookmarks"); 00091 // default: 00092 // return ""; 00093 // } 00094 } 00095 00096 QString AutoBookmarker::configPageFullName( uint /*p*/ ) const 00097 { 00098 // switch (p) 00099 // { 00100 // case 0: 00101 return i18n("Configure AutoBookmarks"); 00102 // default: 00103 // return ""; 00104 // } 00105 } 00106 00107 QPixmap AutoBookmarker::configPagePixmap( uint /*p*/, int size ) const 00108 { 00109 return UserIcon("kte_bookmark", size); 00110 } 00111 00112 void AutoBookmarker::slotCompleted() 00113 { 00114 // get the document info 00115 KTextEditor::DocumentInfoInterface *di = 00116 static_cast<KTextEditor::DocumentInfoInterface*>(document()-> 00117 qt_cast("KTextEditor::DocumentInfoInterface")); 00118 QString mt; 00119 if ( di ) // we can still try match the URL otherwise 00120 mt = di->mimeType(); 00121 00122 QString fileName; 00123 if ( document()->url().isValid() ) 00124 fileName = document()->url().fileName(); 00125 00126 ABEntityList *l = ABGlobal::self()->entities(); 00127 // for each item, if either mask matches 00128 // * apply if onLoad is true 00129 ABEntityListIterator it( *l ); 00130 int n( 0 ); 00131 bool found; 00132 AutoBookmarkEnt *e; 00133 while ( ( e = it.current() ) != 0 ) 00134 { 00135 found = ( !e->mimemask.count() && !e->filemask.count() ); // no preferences 00136 if ( ! found ) 00137 found = ( ! mt.isEmpty() && e->mimemask.contains( mt ) ); 00138 if ( ! found ) 00139 for( QStringList::Iterator it1 = e->filemask.begin(); it1 != e->filemask.end(); ++it1 ) 00140 { 00141 QRegExp re(*it1, true, true); 00142 if ( ( found = ( ( re.search( fileName ) > -1 ) && ( re.matchedLength() == (int)fileName.length() ) ) ) ) 00143 break; 00144 } 00145 00146 if ( found ) 00147 applyEntity( e ); 00148 00149 n++; 00150 ++it; 00151 } 00152 00153 } 00154 00155 void AutoBookmarker::applyEntity( AutoBookmarkEnt *e ) 00156 { 00157 KTextEditor::Document *doc = document(); 00158 KTextEditor::EditInterface *ei = KTextEditor::editInterface( doc ); 00159 KTextEditor::MarkInterface *mi = KTextEditor::markInterface( doc ); 00160 00161 if ( ! ( ei && mi ) ) return; 00162 00163 QRegExp re( e->pattern, e->flags & AutoBookmarkEnt::CaseSensitive ); 00164 re.setMinimal( e->flags & AutoBookmarkEnt::MinimalMatching ); 00165 00166 for ( uint l( 0 ); l < ei->numLines(); l++ ) 00167 if ( re.search( ei->textLine( l ) ) > -1 ) 00168 mi->setMark( l, KTextEditor::MarkInterface::Bookmark ); 00169 } 00170 00171 //END 00172 00173 //BEGIN ABGlobal 00174 ABGlobal *ABGlobal::s_self = 0; 00175 00176 ABGlobal::ABGlobal() 00177 { 00178 m_ents = new ABEntityList; 00179 readConfig(); 00180 } 00181 00182 ABGlobal::~ABGlobal() 00183 { 00184 delete m_ents; 00185 } 00186 00187 static KStaticDeleter<ABGlobal> sdSelf; 00188 00189 ABGlobal *ABGlobal::self() 00190 { 00191 if ( ! s_self ) 00192 sdSelf.setObject(s_self, new ABGlobal()); 00193 00194 return s_self; 00195 } 00196 00197 void ABGlobal::readConfig() 00198 { 00199 if ( ! m_ents ) 00200 m_ents = new ABEntityList; 00201 else 00202 m_ents->clear(); 00203 KConfig *config = new KConfig("ktexteditor_autobookmarkerrc"); 00204 00205 uint n( 0 ); 00206 while ( config->hasGroup( QString("autobookmark%1").arg( n ) ) ) 00207 { 00208 config->setGroup( QString("autobookmark%1").arg( n ) ); 00209 QStringList filemask = config->readListEntry( "filemask", ';' ); 00210 QStringList mimemask = config->readListEntry( "mimemask", ';' ); 00211 int flags = config->readNumEntry( "flags", 1 ); 00212 AutoBookmarkEnt *e = new AutoBookmarkEnt( 00213 config->readEntry( "pattern", "" ), 00214 filemask, 00215 mimemask, 00216 flags 00217 ); 00218 00219 m_ents->append( e ); 00220 00221 ++n; 00222 } 00223 00224 delete config; 00225 } 00226 00227 void ABGlobal::writeConfig() 00228 { 00229 KConfig *config = new KConfig("ktexteditor_autobookmarkerrc"); 00230 00231 // clean the config object 00232 QStringList l = config->groupList(); 00233 for ( QStringList::Iterator it = l.begin(); it != l.end(); ++it ) 00234 config->deleteGroup( *it ); 00235 00236 // fill in the current list 00237 for ( uint i = 0; i < m_ents->count(); i++ ) 00238 { 00239 AutoBookmarkEnt *e = m_ents->at( i ); 00240 config->setGroup( QString("autobookmark%1").arg( i ) ); 00241 config->writeEntry( "pattern", e->pattern ); 00242 config->writeEntry( "filemask", e->filemask, ';' ); 00243 config->writeEntry( "mimemask", e->mimemask, ';' ); 00244 config->writeEntry( "flags", e->flags ); 00245 } 00246 00247 config->sync(); // explicit -- this is supposedly handled by the d'tor 00248 delete config; 00249 } 00250 //END ABGlobal 00251 00252 //BEGIN AutoBookmarkEntItem 00253 // A QListviewItem which can hold a AutoBookmarkEnt pointer 00254 class AutoBookmarkEntItem : public QListViewItem 00255 { 00256 public: 00257 AutoBookmarkEntItem( KListView *lv, AutoBookmarkEnt *e ) 00258 : QListViewItem( lv ), 00259 ent( e ) 00260 { 00261 redo(); 00262 }; 00263 ~AutoBookmarkEntItem(){}; 00264 void redo() 00265 { 00266 setText( 0, ent->pattern ); 00267 setText( 1, ent->mimemask.join("; ") ); 00268 setText( 2, ent->filemask.join("; ") ); 00269 } 00270 AutoBookmarkEnt *ent; 00271 }; 00272 //END 00273 00274 //BEGIN AutoBookmarkerEntEditor 00275 // Dialog for editing a single autobookmark entity 00276 // * edit the pattern 00277 // * set the file/mime type masks 00278 AutoBookmarkerEntEditor::AutoBookmarkerEntEditor( QWidget *parent, AutoBookmarkEnt *e ) 00279 : KDialogBase( parent, "autobookmark_ent_editor", 00280 true, i18n("Edit Entry"), 00281 KDialogBase::Ok|KDialogBase::Cancel ), 00282 e( e ) 00283 { 00284 QFrame *w = makeMainWidget(); 00285 QGridLayout * lo = new QGridLayout( w, 5, 3 ); 00286 lo->setSpacing( KDialogBase::spacingHint() ); 00287 00288 QLabel *l = new QLabel( i18n("&Pattern:"), w ); 00289 lePattern = new QLineEdit( e->pattern, w ); 00290 l->setBuddy( lePattern ); 00291 lo->addWidget( l, 0, 0 ); 00292 lo->addMultiCellWidget( lePattern, 0, 0, 1, 2 ); 00293 QWhatsThis::add( lePattern, i18n( 00294 "<p>A regular expression. Matching lines will be bookmarked.</p>" ) ); 00295 00296 connect( lePattern, SIGNAL(textChanged ( const QString & ) ),this, SLOT( slotPatternChanged( const QString& ) ) ); 00297 00298 cbCS = new QCheckBox( i18n("Case &sensitive"), w ); 00299 lo->addMultiCellWidget( cbCS, 1, 1, 0, 2 ); 00300 cbCS->setChecked( e->flags & AutoBookmarkEnt::CaseSensitive ); 00301 QWhatsThis::add( cbCS, i18n( 00302 "<p>If enabled, the pattern matching will be case sensitive, otherwise " 00303 "not.</p>") ); 00304 00305 cbMM = new QCheckBox( i18n("&Minimal matching"), w ); 00306 lo->addMultiCellWidget( cbMM, 2, 2, 0 ,2 ); 00307 cbMM->setChecked( e->flags & AutoBookmarkEnt::MinimalMatching ); 00308 QWhatsThis::add( cbMM, i18n( 00309 "<p>If enabled, the pattern matching will use minimal matching; if you " 00310 "do not know what that is, please read the appendix on regular expressions " 00311 "in the kate manual.</p>") ); 00312 00313 l = new QLabel( i18n("&File mask:"), w ); 00314 leFileMask = new QLineEdit( e->filemask.join( "; " ), w ); 00315 l->setBuddy( leFileMask ); 00316 lo->addWidget( l, 3, 0 ); 00317 lo->addMultiCellWidget( leFileMask, 3, 3, 1, 2 ); 00318 QWhatsThis::add( leFileMask, i18n( 00319 "<p>A list of filename masks, separated by semicolons. This can be used " 00320 "to limit the usage of this entity to files with matching names.</p>" 00321 "<p>Use the wizard button to the right of the mimetype entry below to " 00322 "easily fill out both lists.</p>" ) ); 00323 00324 l = new QLabel( i18n("MIME &types:"), w ); 00325 leMimeTypes = new QLineEdit( e->mimemask.join( "; " ), w ); 00326 l->setBuddy( leMimeTypes ); 00327 lo->addWidget( l, 4, 0 ); 00328 lo->addWidget( leMimeTypes, 4, 1 ); 00329 QWhatsThis::add( leMimeTypes, i18n( 00330 "<p>A list of mime types, separated by semicolon. This can be used to " 00331 "limit the usage of this entity to files with matching mime types.</p>" 00332 "<p>Use the wizard button on the right to get a list of existing file " 00333 "types to choose from, using it will fill in the file masks as well.</p>" ) ); 00334 00335 QToolButton *btnMTW = new QToolButton(w); 00336 lo->addWidget( btnMTW, 4, 2 ); 00337 btnMTW->setIconSet(QIconSet(SmallIcon("wizard"))); 00338 connect(btnMTW, SIGNAL(clicked()), this, SLOT(showMTDlg())); 00339 QWhatsThis::add( btnMTW, i18n( 00340 "<p>Click this button to display a checkable list of mimetypes available " 00341 "on your system. When used, the file masks entry above will be filled in " 00342 "with the corresponding masks.</p>") ); 00343 slotPatternChanged( lePattern->text() ); 00344 } 00345 00346 void AutoBookmarkerEntEditor::slotPatternChanged( const QString&_pattern ) 00347 { 00348 enableButtonOK( !_pattern.isEmpty() ); 00349 } 00350 00351 void AutoBookmarkerEntEditor::apply() 00352 { 00353 if ( lePattern->text().isEmpty() ) return; 00354 00355 e->pattern = lePattern->text(); 00356 e->filemask = QStringList::split( QRegExp("\\s*;\\s*"), leFileMask->text() ); 00357 e->mimemask = QStringList::split( QRegExp("\\s*;\\s*"), leMimeTypes->text() ); 00358 e->flags = 0; 00359 if ( cbCS->isOn() ) e->flags |= AutoBookmarkEnt::CaseSensitive; 00360 if ( cbMM->isOn() ) e->flags |= AutoBookmarkEnt::MinimalMatching; 00361 } 00362 00363 void AutoBookmarkerEntEditor::showMTDlg() 00364 { 00365 QString text = i18n("Select the MimeTypes for this pattern.\nPlease note that this will automatically edit the associated file extensions as well."); 00366 QStringList list = QStringList::split( QRegExp("\\s*;\\s*"), leMimeTypes->text() ); 00367 KMimeTypeChooserDialog *d = new KMimeTypeChooserDialog( i18n("Select Mime Types"), text, list, "text", this ); 00368 if ( d->exec() == KDialogBase::Accepted ) { 00369 // do some checking, warn user if mime types or patterns are removed. 00370 // if the lists are empty, and the fields not, warn. 00371 leFileMask->setText(d->chooser()->patterns().join("; ")); 00372 leMimeTypes->setText(d->chooser()->mimeTypes().join("; ")); 00373 } 00374 } 00375 //END 00376 00377 //BEGIN AutoBookmarkerConfigPage 00378 // TODO allow custom mark types with icons 00379 AutoBookmarkerConfigPage::AutoBookmarkerConfigPage( QWidget *parent, const char *name ) 00380 : KTextEditor::ConfigPage( parent, name ) 00381 { 00382 QVBoxLayout *lo = new QVBoxLayout( this ); 00383 lo->setSpacing( KDialogBase::spacingHint() ); 00384 00385 QLabel *l = new QLabel( i18n("&Patterns"), this ); 00386 lo->addWidget( l ); 00387 lvPatterns = new KListView( this ); 00388 lvPatterns->addColumn( i18n("Pattern") ); 00389 lvPatterns->addColumn( i18n("Mime Types") ); 00390 lvPatterns->addColumn( i18n("File Masks") ); 00391 lo->addWidget( lvPatterns ); 00392 l->setBuddy( lvPatterns ); 00393 QWhatsThis::add( lvPatterns, i18n( 00394 "<p>This list shows your configured autobookmark entities. When a document " 00395 "is opened, each entity is used in the following way: " 00396 "<ol>" 00397 "<li>The entity is dismissed, if a mime and/or filename mask is defined, " 00398 "and neither matches the document.</li>" 00399 "<li>Otherwise each line of the document is tried against the pattern, " 00400 "and a bookmark is set on matching lines.</li></ul>" 00401 "<p>Use the buttons below to manage your collection of entities.</p>") ); 00402 00403 QHBoxLayout *lo1 = new QHBoxLayout ( lo ); 00404 lo1->setSpacing( KDialogBase::spacingHint() ); 00405 00406 btnNew = new QPushButton( i18n("&New..."), this ); 00407 lo1->addWidget( btnNew ); 00408 QWhatsThis::add( btnNew, i18n( 00409 "Press this button to create a new autobookmark entity.") ); 00410 00411 btnDel = new QPushButton( i18n("&Delete"), this ); 00412 lo1->addWidget( btnDel ); 00413 QWhatsThis::add( btnDel, i18n( 00414 "Press this button to delete the currently selected entity.") ); 00415 00416 btnEdit = new QPushButton( i18n("&Edit..."), this ); 00417 lo1->addWidget( btnEdit ); 00418 QWhatsThis::add( btnEdit, i18n( 00419 "Press this button to edit the currently selected entity.") ); 00420 00421 lo1->addStretch( 1 ); 00422 00423 connect( btnNew, SIGNAL(clicked()), this, SLOT(slotNew()) ); 00424 connect( btnDel, SIGNAL(clicked()), this, SLOT(slotDel()) ); 00425 connect( btnEdit, SIGNAL(clicked()), this, SLOT(slotEdit()) ); 00426 connect( lvPatterns, SIGNAL(doubleClicked(QListViewItem *)), this, SLOT(slotEdit()) ); 00427 00428 m_ents = new ABEntityList(); 00429 m_ents->setAutoDelete( true ); 00430 reset(); 00431 } 00432 00433 // replace the global list with the new one 00434 void AutoBookmarkerConfigPage::apply() 00435 { 00436 ABGlobal::self()->entities()->clear(); 00437 00438 ABEntityListIterator it ( *m_ents ); 00439 AutoBookmarkEnt *e; 00440 00441 while ( (e = it.current()) != 0 ) 00442 { 00443 ABGlobal::self()->entities()->append( e ); 00444 ++it; 00445 } 00446 00447 ABGlobal::self()->writeConfig(); 00448 00449 // TODO -- how do i refresh all the view menus 00450 } 00451 00452 // renew our copy of the global list 00453 void AutoBookmarkerConfigPage::reset() 00454 { 00455 m_ents->clear(); // unused - no reset button currently 00456 00457 ABEntityListIterator it ( *ABGlobal::self()->entities() ); 00458 AutoBookmarkEnt *e; 00459 while ( (e = it.current()) != 0 ) 00460 { 00461 AutoBookmarkEnt *me = new AutoBookmarkEnt( *e ); 00462 m_ents->append( me ); 00463 new AutoBookmarkEntItem( lvPatterns, me ); 00464 ++it; 00465 } 00466 } 00467 00468 // TODO (so far not used) we have no defaults (except deleting all items??) 00469 void AutoBookmarkerConfigPage::defaults() 00470 { 00471 // if KMessageBox::warningYesNo() 00472 // clear all 00473 } 00474 00475 // open the edit dialog with a new entity, 00476 // and add it if the dialog is accepted 00477 void AutoBookmarkerConfigPage::slotNew() 00478 { 00479 AutoBookmarkEnt *e = new AutoBookmarkEnt(); 00480 AutoBookmarkerEntEditor dlg( this, e ); 00481 if ( dlg.exec() ) 00482 { 00483 dlg.apply(); 00484 new AutoBookmarkEntItem( lvPatterns, e ); 00485 m_ents->append( e ); 00486 } 00487 } 00488 00489 // delete the selected item and remove it from the list view and internal list 00490 void AutoBookmarkerConfigPage::slotDel() 00491 { 00492 AutoBookmarkEntItem *i = (AutoBookmarkEntItem*)lvPatterns->currentItem(); 00493 int idx = m_ents->findRef( i->ent ); 00494 m_ents->remove( idx ); 00495 delete i; 00496 } 00497 00498 // open the edit dialog with the selected item 00499 void AutoBookmarkerConfigPage::slotEdit() 00500 { 00501 AutoBookmarkEnt *e = ((AutoBookmarkEntItem*)lvPatterns->currentItem())->ent; 00502 AutoBookmarkerEntEditor dlg( this, e ); 00503 if ( dlg.exec() ) 00504 { 00505 dlg.apply(); 00506 ((AutoBookmarkEntItem*)lvPatterns->currentItem())->redo(); 00507 } 00508 } 00509 //END AutoBookmarkerConfigPage 00510 00511 //BEGIN AutoBookmarkEnt 00512 AutoBookmarkEnt::AutoBookmarkEnt( const QString &p, const QStringList &f, const QStringList &m, int fl ) 00513 : pattern( p ), 00514 filemask( f ), 00515 mimemask( m ), 00516 flags( fl ) 00517 {; 00518 } 00519 //END 00520 // 00521 #include "autobookmarker.moc"
KDE Logo
This file is part of the documentation for kate Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Apr 12 23:39:56 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003