24 #include "autobookmarker.h"
26 #include <tdetexteditor/markinterfaceextension.h>
27 #include <tdetexteditor/editinterface.h>
28 #include <tdetexteditor/documentinfo.h>
29 #include <tdetexteditor/document.h>
31 #include <tdeaction.h>
32 #include <tdeapplication.h>
33 #include <tdeconfig.h>
34 #include <kgenericfactory.h>
35 #include <kiconloader.h>
36 #include <tdelistview.h>
38 #include <kmimetype.h>
39 #include <kmimetypechooser.h>
40 #include <tdeprocess.h>
42 #include <kstaticdeleter.h>
45 #include <tqcheckbox.h>
48 #include <tqlineedit.h>
49 #include <tqlistview.h>
50 #include <tqpopupmenu.h>
51 #include <tqpushbutton.h>
52 #include <tqtoolbutton.h>
53 #include <tqwhatsthis.h>
62 AutoBookmarker::AutoBookmarker( TQObject *parent,
65 : KTextEditor::Plugin ( (KTextEditor::Document*) parent, name ),
66 KTextEditor::ConfigInterfaceExtension()
69 connect( parent, TQ_SIGNAL( completed() ),
this, TQ_SLOT( slotCompleted() ) );
72 void AutoBookmarker::addView(KTextEditor::View *)
76 void AutoBookmarker::removeView(KTextEditor::View *)
80 KTextEditor::ConfigPage * AutoBookmarker::configPage( uint , TQWidget *parent,
const char *name )
82 return new AutoBookmarkerConfigPage( parent, name );
85 TQString AutoBookmarker::configPageName( uint )
const
90 return i18n(
"AutoBookmarks");
96 TQString AutoBookmarker::configPageFullName( uint )
const
101 return i18n(
"Configure AutoBookmarks");
107 TQPixmap AutoBookmarker::configPagePixmap( uint ,
int size )
const
109 return UserIcon(
"kte_bookmark", size);
112 void AutoBookmarker::slotCompleted()
115 KTextEditor::DocumentInfoInterface *di =
116 ::tqt_cast<KTextEditor::DocumentInfoInterface*>(
document());
123 fileName =
document()->url().fileName();
125 ABEntityList *l = ABGlobal::self()->entities();
128 ABEntityListIterator it( *l );
132 while ( ( e = it.current() ) != 0 )
134 found = ( !e->mimemask.count() && !e->filemask.count() );
136 found = ( ! mt.isEmpty() && e->mimemask.contains( mt ) );
138 for( TQStringList::Iterator it1 = e->filemask.begin(); it1 != e->filemask.end(); ++it1 )
140 TQRegExp re(*it1,
true,
true);
141 if ( ( found = ( ( re.search( fileName ) > -1 ) && ( re.matchedLength() == (
int)fileName.length() ) ) ) )
154 void AutoBookmarker::applyEntity( AutoBookmarkEnt *e )
156 KTextEditor::Document *doc =
document();
157 KTextEditor::EditInterface *ei = KTextEditor::editInterface( doc );
158 KTextEditor::MarkInterface *mi = KTextEditor::markInterface( doc );
160 if ( ! ( ei && mi ) )
return;
162 TQRegExp re( e->pattern, e->flags & AutoBookmarkEnt::CaseSensitive );
163 re.setMinimal( e->flags & AutoBookmarkEnt::MinimalMatching );
165 for ( uint l( 0 ); l < ei->numLines(); l++ )
166 if ( re.search( ei->textLine( l ) ) > -1 )
167 mi->setMark( l, KTextEditor::MarkInterface::Bookmark );
173 ABGlobal *ABGlobal::s_self = 0;
177 m_ents =
new ABEntityList;
181 ABGlobal::~ABGlobal()
188 ABGlobal *ABGlobal::self()
191 sdSelf.setObject(s_self,
new ABGlobal());
196 void ABGlobal::readConfig()
199 m_ents =
new ABEntityList;
205 while ( config->
hasGroup( TQString(
"autobookmark%1").arg( n ) ) )
207 config->
setGroup( TQString(
"autobookmark%1").arg( n ) );
208 TQStringList filemask = config->
readListEntry(
"filemask",
';' );
209 TQStringList mimemask = config->
readListEntry(
"mimemask",
';' );
211 AutoBookmarkEnt *e =
new AutoBookmarkEnt(
226 void ABGlobal::writeConfig()
232 for ( TQStringList::Iterator it = l.begin(); it != l.end(); ++it )
236 for ( uint i = 0; i < m_ents->count(); i++ )
238 AutoBookmarkEnt *e = m_ents->at( i );
239 config->
setGroup( TQString(
"autobookmark%1").arg( i ) );
241 config->
writeEntry(
"filemask", e->filemask,
';' );
242 config->
writeEntry(
"mimemask", e->mimemask,
';' );
253 class AutoBookmarkEntItem :
public QListViewItem
256 AutoBookmarkEntItem(
TDEListView *lv, AutoBookmarkEnt *e )
257 : TQListViewItem( lv ),
262 ~AutoBookmarkEntItem(){};
265 setText( 0, ent->pattern );
266 setText( 1, ent->mimemask.join(
"; ") );
267 setText( 2, ent->filemask.join(
"; ") );
269 AutoBookmarkEnt *ent;
277 AutoBookmarkerEntEditor::AutoBookmarkerEntEditor( TQWidget *parent, AutoBookmarkEnt *e )
279 true, i18n(
"Edit Entry"),
283 TQFrame *w = makeMainWidget();
284 TQGridLayout * lo =
new TQGridLayout( w, 5, 3 );
287 TQLabel *l =
new TQLabel( i18n(
"&Pattern:"), w );
288 lePattern =
new TQLineEdit( e->pattern, w );
289 l->setBuddy( lePattern );
290 lo->addWidget( l, 0, 0 );
291 lo->addMultiCellWidget( lePattern, 0, 0, 1, 2 );
292 TQWhatsThis::add( lePattern, i18n(
293 "<p>A regular expression. Matching lines will be bookmarked.</p>" ) );
295 connect( lePattern, TQ_SIGNAL(textChanged (
const TQString & ) ),
this, TQ_SLOT( slotPatternChanged(
const TQString& ) ) );
297 cbCS =
new TQCheckBox( i18n(
"Case &sensitive"), w );
298 lo->addMultiCellWidget( cbCS, 1, 1, 0, 2 );
299 cbCS->setChecked( e->flags & AutoBookmarkEnt::CaseSensitive );
300 TQWhatsThis::add( cbCS, i18n(
301 "<p>If enabled, the pattern matching will be case sensitive, otherwise "
304 cbMM =
new TQCheckBox( i18n(
"&Minimal matching"), w );
305 lo->addMultiCellWidget( cbMM, 2, 2, 0 ,2 );
306 cbMM->setChecked( e->flags & AutoBookmarkEnt::MinimalMatching );
307 TQWhatsThis::add( cbMM, i18n(
308 "<p>If enabled, the pattern matching will use minimal matching; if you "
309 "do not know what that is, please read the appendix on regular expressions "
310 "in the kate manual.</p>") );
312 l =
new TQLabel( i18n(
"&File mask:"), w );
313 leFileMask =
new TQLineEdit( e->filemask.join(
"; " ), w );
314 l->setBuddy( leFileMask );
315 lo->addWidget( l, 3, 0 );
316 lo->addMultiCellWidget( leFileMask, 3, 3, 1, 2 );
317 TQWhatsThis::add( leFileMask, i18n(
318 "<p>A list of filename masks, separated by semicolons. This can be used "
319 "to limit the usage of this entity to files with matching names.</p>"
320 "<p>Use the wizard button to the right of the mimetype entry below to "
321 "easily fill out both lists.</p>" ) );
323 l =
new TQLabel( i18n(
"MIME &types:"), w );
324 leMimeTypes =
new TQLineEdit( e->mimemask.join(
"; " ), w );
325 l->setBuddy( leMimeTypes );
326 lo->addWidget( l, 4, 0 );
327 lo->addWidget( leMimeTypes, 4, 1 );
328 TQWhatsThis::add( leMimeTypes, i18n(
329 "<p>A list of mime types, separated by semicolon. This can be used to "
330 "limit the usage of this entity to files with matching mime types.</p>"
331 "<p>Use the wizard button on the right to get a list of existing file "
332 "types to choose from, using it will fill in the file masks as well.</p>" ) );
334 TQToolButton *btnMTW =
new TQToolButton(w);
335 lo->addWidget( btnMTW, 4, 2 );
336 btnMTW->setIconSet(TQIconSet(SmallIcon(
"wizard")));
337 connect(btnMTW, TQ_SIGNAL(clicked()),
this, TQ_SLOT(showMTDlg()));
338 TQWhatsThis::add( btnMTW, i18n(
339 "<p>Click this button to display a checkable list of mimetypes available "
340 "on your system. When used, the file masks entry above will be filled in "
341 "with the corresponding masks.</p>") );
342 slotPatternChanged( lePattern->text() );
345 void AutoBookmarkerEntEditor::slotPatternChanged(
const TQString&_pattern )
347 enableButtonOK( !_pattern.isEmpty() );
350 void AutoBookmarkerEntEditor::apply()
352 if ( lePattern->text().isEmpty() )
return;
354 e->pattern = lePattern->text();
355 e->filemask = TQStringList::split( TQRegExp(
"\\s*;\\s*"), leFileMask->text() );
356 e->mimemask = TQStringList::split( TQRegExp(
"\\s*;\\s*"), leMimeTypes->text() );
358 if ( cbCS->isOn() ) e->flags |= AutoBookmarkEnt::CaseSensitive;
359 if ( cbMM->isOn() ) e->flags |= AutoBookmarkEnt::MinimalMatching;
362 void AutoBookmarkerEntEditor::showMTDlg()
364 TQString text = i18n(
"Select the MimeTypes for this pattern.\nPlease note that this will automatically edit the associated file extensions as well.");
365 TQStringList list = TQStringList::split( TQRegExp(
"\\s*;\\s*"), leMimeTypes->text() );
366 KMimeTypeChooserDialog d( i18n(
"Select Mime Types"), text, list,
"text",
this );
367 if ( d.exec() == KDialogBase::Accepted ) {
370 leFileMask->setText(d.chooser()->patterns().join(
"; "));
371 leMimeTypes->setText(d.chooser()->mimeTypes().join(
"; "));
378 AutoBookmarkerConfigPage::AutoBookmarkerConfigPage( TQWidget *parent,
const char *name )
379 : KTextEditor::ConfigPage( parent,
name )
381 TQVBoxLayout *lo =
new TQVBoxLayout(
this );
384 TQLabel *l =
new TQLabel( i18n(
"&Patterns"),
this );
387 lvPatterns->addColumn( i18n(
"Pattern") );
388 lvPatterns->addColumn( i18n(
"Mime Types") );
389 lvPatterns->addColumn( i18n(
"File Masks") );
390 lo->addWidget( lvPatterns );
391 l->setBuddy( lvPatterns );
392 TQWhatsThis::add( lvPatterns, i18n(
393 "<p>This list shows your configured autobookmark entities. When a document "
394 "is opened, each entity is used in the following way: "
396 "<li>The entity is dismissed, if a mime and/or filename mask is defined, "
397 "and neither matches the document.</li>"
398 "<li>Otherwise each line of the document is tried against the pattern, "
399 "and a bookmark is set on matching lines.</li></ol>"
400 "<p>Use the buttons below to manage your collection of entities.</p>") );
402 TQHBoxLayout *lo1 =
new TQHBoxLayout ( lo );
405 btnNew =
new TQPushButton( i18n(
"&New..."),
this );
406 lo1->addWidget( btnNew );
407 TQWhatsThis::add( btnNew, i18n(
408 "Press this button to create a new autobookmark entity.") );
410 btnDel =
new TQPushButton( i18n(
"&Delete"),
this );
411 lo1->addWidget( btnDel );
412 TQWhatsThis::add( btnDel, i18n(
413 "Press this button to delete the currently selected entity.") );
415 btnEdit =
new TQPushButton( i18n(
"&Edit..."),
this );
416 lo1->addWidget( btnEdit );
417 TQWhatsThis::add( btnEdit, i18n(
418 "Press this button to edit the currently selected entity.") );
420 lo1->addStretch( 1 );
422 connect( btnNew, TQ_SIGNAL(clicked()),
this, TQ_SLOT(slotNew()) );
423 connect( btnDel, TQ_SIGNAL(clicked()),
this, TQ_SLOT(slotDel()) );
424 connect( btnEdit, TQ_SIGNAL(clicked()),
this, TQ_SLOT(slotEdit()) );
425 connect( lvPatterns, TQ_SIGNAL(doubleClicked(TQListViewItem *)),
this, TQ_SLOT(slotEdit()) );
427 m_ents =
new ABEntityList();
428 m_ents->setAutoDelete(
true );
433 void AutoBookmarkerConfigPage::apply()
435 ABGlobal::self()->entities()->clear();
437 ABEntityListIterator it ( *m_ents );
440 while ( (e = it.current()) != 0 )
442 ABGlobal::self()->entities()->append( e );
446 ABGlobal::self()->writeConfig();
452 void AutoBookmarkerConfigPage::reset()
456 ABEntityListIterator it ( *ABGlobal::self()->entities() );
458 while ( (e = it.current()) != 0 )
460 AutoBookmarkEnt *me =
new AutoBookmarkEnt( *e );
461 m_ents->append( me );
462 new AutoBookmarkEntItem( lvPatterns, me );
468 void AutoBookmarkerConfigPage::defaults()
476 void AutoBookmarkerConfigPage::slotNew()
478 AutoBookmarkEnt *e =
new AutoBookmarkEnt();
479 AutoBookmarkerEntEditor dlg(
this, e );
483 new AutoBookmarkEntItem( lvPatterns, e );
489 void AutoBookmarkerConfigPage::slotDel()
491 AutoBookmarkEntItem *i = (AutoBookmarkEntItem*)lvPatterns->currentItem();
492 int idx = m_ents->findRef( i->ent );
493 m_ents->remove( idx );
498 void AutoBookmarkerConfigPage::slotEdit()
500 AutoBookmarkEnt *e = ((AutoBookmarkEntItem*)lvPatterns->currentItem())->ent;
501 AutoBookmarkerEntEditor dlg(
this, e );
505 ((AutoBookmarkEntItem*)lvPatterns->currentItem())->
redo();
511 AutoBookmarkEnt::AutoBookmarkEnt(
const TQString &p,
const TQStringList &f,
const TQStringList &m,
int fl )
520 #include "autobookmarker.moc"
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
bool deleteGroup(const TQString &group, bool bDeep=true, bool bGlobal=false)
int readNumEntry(const TQString &pKey, int nDefault=0) const
bool hasGroup(const TQString &group) const
int readListEntry(const TQString &pKey, TQStrList &list, char sep=',') const
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
void setGroup(const TQString &group)
virtual TQStringList groupList() const
KATEPARTINTERFACES_EXPORT Document * document(KTextEditor::Document *doc)
Check if given document is a Kate::Document.
TQString name(StdAccel id)
const TDEShortcut & redo()