• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kate
 

kate

  • kate
  • plugins
  • autobookmarker
autobookmarker.cpp
1/*
2 This library is free software you can redistribute it and/or
3 modify it under the terms of the GNU Library General Public
4 License.
5
6 This library is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 Library General Public License for more details.
10
11 You should have received a copy of the GNU Library General Public License
12 along with this library; see the file COPYING.LIB. If not, write to
13 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
14 Boston, MA 02110-1301, USA.
15
16 ---
17 file: autobookmarker.cpp
18
19 KTextEditor plugin to add bookmarks to documents.
20 Copyright Anders Lund <anders.lund@lund.tdcadsl.dk>, 2003
21*/
22
23//BEGIN includes
24#include "autobookmarker.h"
25
26#include <tdetexteditor/markinterfaceextension.h>
27#include <tdetexteditor/editinterface.h>
28#include <tdetexteditor/documentinfo.h>
29#include <tdetexteditor/document.h>
30
31#include <tdeaction.h>
32#include <tdeapplication.h>
33#include <tdeconfig.h>
34#include <kgenericfactory.h>
35#include <kiconloader.h>
36#include <tdelistview.h>
37#include <tdelocale.h>
38#include <kmimetype.h>
39#include <kmimetypechooser.h>
40#include <tdeprocess.h>
41#include <krun.h>
42#include <kstaticdeleter.h>
43#include <kurl.h>
44
45#include <tqcheckbox.h>
46#include <tqlabel.h>
47#include <tqlayout.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>
54#include <tqregexp.h>
55
56//#include <kdebug.h>
57//END includes
58
59//BEGIN AutoBookmarker
60K_EXPORT_COMPONENT_FACTORY( tdetexteditor_autobookmarker, KGenericFactory<AutoBookmarker>( "tdetexteditor_autobookmarker" ) )
61
62AutoBookmarker::AutoBookmarker( TQObject *parent,
63 const char* name,
64 const TQStringList& /*args*/ )
65 : KTextEditor::Plugin ( (KTextEditor::Document*) parent, name ),
66 KTextEditor::ConfigInterfaceExtension()
67{
68 if ( parent )
69 connect( parent, TQ_SIGNAL( completed() ), this, TQ_SLOT( slotCompleted() ) );
70}
71
72void AutoBookmarker::addView(KTextEditor::View */*view*/)
73{
74}
75
76void AutoBookmarker::removeView(KTextEditor::View */*view*/)
77{
78}
79
80KTextEditor::ConfigPage * AutoBookmarker::configPage( uint /*number*/, TQWidget *parent, const char *name )
81{
82 return new AutoBookmarkerConfigPage( parent, name );
83}
84
85TQString AutoBookmarker::configPageName( uint /*p*/ ) const
86{
87// switch (p)
88// {
89// case 0:
90 return i18n("AutoBookmarks");
91// default:
92// return "";
93// }
94}
95
96TQString AutoBookmarker::configPageFullName( uint /*p*/ ) const
97{
98// switch (p)
99// {
100// case 0:
101 return i18n("Configure AutoBookmarks");
102// default:
103// return "";
104// }
105}
106
107TQPixmap AutoBookmarker::configPagePixmap( uint /*p*/, int size ) const
108{
109 return UserIcon("kte_bookmark", size);
110}
111
112void AutoBookmarker::slotCompleted()
113{
114 // get the document info
115 KTextEditor::DocumentInfoInterface *di =
116 ::tqt_cast<KTextEditor::DocumentInfoInterface*>(document());
117 TQString mt;
118 if ( di ) // we can still try match the URL otherwise
119 mt = di->mimeType();
120
121 TQString fileName;
122 if ( document()->url().isValid() )
123 fileName = document()->url().fileName();
124
125 ABEntityList *l = ABGlobal::self()->entities();
126 // for each item, if either mask matches
127 // * apply if onLoad is true
128 ABEntityListIterator it( *l );
129 int n( 0 );
130 bool found;
131 AutoBookmarkEnt *e;
132 while ( ( e = it.current() ) != 0 )
133 {
134 found = ( !e->mimemask.count() && !e->filemask.count() ); // no preferences
135 if ( ! found )
136 found = ( ! mt.isEmpty() && e->mimemask.contains( mt ) );
137 if ( ! found )
138 for( TQStringList::Iterator it1 = e->filemask.begin(); it1 != e->filemask.end(); ++it1 )
139 {
140 TQRegExp re(*it1, true, true);
141 if ( ( found = ( ( re.search( fileName ) > -1 ) && ( re.matchedLength() == (int)fileName.length() ) ) ) )
142 break;
143 }
144
145 if ( found )
146 applyEntity( e );
147
148 n++;
149 ++it;
150 }
151
152}
153
154void AutoBookmarker::applyEntity( AutoBookmarkEnt *e )
155{
156 KTextEditor::Document *doc = document();
157 KTextEditor::EditInterface *ei = KTextEditor::editInterface( doc );
158 KTextEditor::MarkInterface *mi = KTextEditor::markInterface( doc );
159
160 if ( ! ( ei && mi ) ) return;
161
162 TQRegExp re( e->pattern, e->flags & AutoBookmarkEnt::CaseSensitive );
163 re.setMinimal( e->flags & AutoBookmarkEnt::MinimalMatching );
164
165 for ( uint l( 0 ); l < ei->numLines(); l++ )
166 if ( re.search( ei->textLine( l ) ) > -1 )
167 mi->setMark( l, KTextEditor::MarkInterface::Bookmark );
168}
169
170//END
171
172//BEGIN ABGlobal
173ABGlobal *ABGlobal::s_self = 0;
174
175ABGlobal::ABGlobal()
176{
177 m_ents = new ABEntityList;
178 readConfig();
179}
180
181ABGlobal::~ABGlobal()
182{
183 delete m_ents;
184}
185
186static KStaticDeleter<ABGlobal> sdSelf;
187
188ABGlobal *ABGlobal::self()
189{
190 if ( ! s_self )
191 sdSelf.setObject(s_self, new ABGlobal());
192
193 return s_self;
194}
195
196void ABGlobal::readConfig()
197{
198 if ( ! m_ents )
199 m_ents = new ABEntityList;
200 else
201 m_ents->clear();
202 TDEConfig *config = new TDEConfig("tdetexteditor_autobookmarkerrc");
203
204 uint n( 0 );
205 while ( config->hasGroup( TQString("autobookmark%1").arg( n ) ) )
206 {
207 config->setGroup( TQString("autobookmark%1").arg( n ) );
208 TQStringList filemask = config->readListEntry( "filemask", ';' );
209 TQStringList mimemask = config->readListEntry( "mimemask", ';' );
210 int flags = config->readNumEntry( "flags", 1 );
211 AutoBookmarkEnt *e = new AutoBookmarkEnt(
212 config->readEntry( "pattern", "" ),
213 filemask,
214 mimemask,
215 flags
216 );
217
218 m_ents->append( e );
219
220 ++n;
221 }
222
223 delete config;
224}
225
226void ABGlobal::writeConfig()
227{
228 TDEConfig *config = new TDEConfig("tdetexteditor_autobookmarkerrc");
229
230 // clean the config object
231 TQStringList l = config->groupList();
232 for ( TQStringList::Iterator it = l.begin(); it != l.end(); ++it )
233 config->deleteGroup( *it );
234
235 // fill in the current list
236 for ( uint i = 0; i < m_ents->count(); i++ )
237 {
238 AutoBookmarkEnt *e = m_ents->at( i );
239 config->setGroup( TQString("autobookmark%1").arg( i ) );
240 config->writeEntry( "pattern", e->pattern );
241 config->writeEntry( "filemask", e->filemask, ';' );
242 config->writeEntry( "mimemask", e->mimemask, ';' );
243 config->writeEntry( "flags", e->flags );
244 }
245
246 config->sync(); // explicit -- this is supposedly handled by the d'tor
247 delete config;
248}
249//END ABGlobal
250
251//BEGIN AutoBookmarkEntItem
252// A QListviewItem which can hold a AutoBookmarkEnt pointer
253class AutoBookmarkEntItem : public QListViewItem
254{
255 public:
256 AutoBookmarkEntItem( TDEListView *lv, AutoBookmarkEnt *e )
257 : TQListViewItem( lv ),
258 ent( e )
259 {
260 redo();
261 };
262 ~AutoBookmarkEntItem(){};
263 void redo()
264 {
265 setText( 0, ent->pattern );
266 setText( 1, ent->mimemask.join("; ") );
267 setText( 2, ent->filemask.join("; ") );
268 }
269 AutoBookmarkEnt *ent;
270};
271//END
272
273//BEGIN AutoBookmarkerEntEditor
274// Dialog for editing a single autobookmark entity
275// * edit the pattern
276// * set the file/mime type masks
277AutoBookmarkerEntEditor::AutoBookmarkerEntEditor( TQWidget *parent, AutoBookmarkEnt *e )
278 : KDialogBase( parent, "autobookmark_ent_editor",
279 true, i18n("Edit Entry"),
280 KDialogBase::Ok|KDialogBase::Cancel ),
281 e( e )
282{
283 TQFrame *w = makeMainWidget();
284 TQGridLayout * lo = new TQGridLayout( w, 5, 3 );
285 lo->setSpacing( KDialogBase::spacingHint() );
286
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>" ) );
294
295 connect( lePattern, TQ_SIGNAL(textChanged ( const TQString & ) ),this, TQ_SLOT( slotPatternChanged( const TQString& ) ) );
296
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 "
302 "not.</p>") );
303
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>") );
311
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>" ) );
322
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>" ) );
333
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() );
343}
344
345void AutoBookmarkerEntEditor::slotPatternChanged( const TQString&_pattern )
346{
347 enableButtonOK( !_pattern.isEmpty() );
348}
349
350void AutoBookmarkerEntEditor::apply()
351{
352 if ( lePattern->text().isEmpty() ) return;
353
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() );
357 e->flags = 0;
358 if ( cbCS->isOn() ) e->flags |= AutoBookmarkEnt::CaseSensitive;
359 if ( cbMM->isOn() ) e->flags |= AutoBookmarkEnt::MinimalMatching;
360}
361
362void AutoBookmarkerEntEditor::showMTDlg()
363{
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 ) {
368 // do some checking, warn user if mime types or patterns are removed.
369 // if the lists are empty, and the fields not, warn.
370 leFileMask->setText(d.chooser()->patterns().join("; "));
371 leMimeTypes->setText(d.chooser()->mimeTypes().join("; "));
372 }
373}
374//END
375
376//BEGIN AutoBookmarkerConfigPage
377// TODO allow custom mark types with icons
378AutoBookmarkerConfigPage::AutoBookmarkerConfigPage( TQWidget *parent, const char *name )
379 : KTextEditor::ConfigPage( parent, name )
380{
381 TQVBoxLayout *lo = new TQVBoxLayout( this );
382 lo->setSpacing( KDialogBase::spacingHint() );
383
384 TQLabel *l = new TQLabel( i18n("&Patterns"), this );
385 lo->addWidget( l );
386 lvPatterns = new TDEListView( 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: "
395 "<ol>"
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>") );
401
402 TQHBoxLayout *lo1 = new TQHBoxLayout ( lo );
403 lo1->setSpacing( KDialogBase::spacingHint() );
404
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.") );
409
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.") );
414
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.") );
419
420 lo1->addStretch( 1 );
421
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()) );
426
427 m_ents = new ABEntityList();
428 m_ents->setAutoDelete( true );
429 reset();
430}
431
432// replace the global list with the new one
433void AutoBookmarkerConfigPage::apply()
434{
435 ABGlobal::self()->entities()->clear();
436
437 ABEntityListIterator it ( *m_ents );
438 AutoBookmarkEnt *e;
439
440 while ( (e = it.current()) != 0 )
441 {
442 ABGlobal::self()->entities()->append( e );
443 ++it;
444 }
445
446 ABGlobal::self()->writeConfig();
447
448 // TODO -- how do i refresh all the view menus
449}
450
451// renew our copy of the global list
452void AutoBookmarkerConfigPage::reset()
453{
454 m_ents->clear(); // unused - no reset button currently
455
456 ABEntityListIterator it ( *ABGlobal::self()->entities() );
457 AutoBookmarkEnt *e;
458 while ( (e = it.current()) != 0 )
459 {
460 AutoBookmarkEnt *me = new AutoBookmarkEnt( *e );
461 m_ents->append( me );
462 new AutoBookmarkEntItem( lvPatterns, me );
463 ++it;
464 }
465}
466
467// TODO (so far not used) we have no defaults (except deleting all items??)
468void AutoBookmarkerConfigPage::defaults()
469{
470 // if KMessageBox::warningYesNo()
471 // clear all
472}
473
474// open the edit dialog with a new entity,
475// and add it if the dialog is accepted
476void AutoBookmarkerConfigPage::slotNew()
477{
478 AutoBookmarkEnt *e = new AutoBookmarkEnt();
479 AutoBookmarkerEntEditor dlg( this, e );
480 if ( dlg.exec() )
481 {
482 dlg.apply();
483 new AutoBookmarkEntItem( lvPatterns, e );
484 m_ents->append( e );
485 }
486}
487
488// delete the selected item and remove it from the list view and internal list
489void AutoBookmarkerConfigPage::slotDel()
490{
491 AutoBookmarkEntItem *i = (AutoBookmarkEntItem*)lvPatterns->currentItem();
492 int idx = m_ents->findRef( i->ent );
493 m_ents->remove( idx );
494 delete i;
495}
496
497// open the edit dialog with the selected item
498void AutoBookmarkerConfigPage::slotEdit()
499{
500 AutoBookmarkEnt *e = ((AutoBookmarkEntItem*)lvPatterns->currentItem())->ent;
501 AutoBookmarkerEntEditor dlg( this, e );
502 if ( dlg.exec() )
503 {
504 dlg.apply();
505 ((AutoBookmarkEntItem*)lvPatterns->currentItem())->redo();
506 }
507}
508//END AutoBookmarkerConfigPage
509
510//BEGIN AutoBookmarkEnt
511AutoBookmarkEnt::AutoBookmarkEnt( const TQString &p, const TQStringList &f, const TQStringList &m, int fl )
512 : pattern( p ),
513 filemask( f ),
514 mimemask( m ),
515 flags( fl )
516{;
517}
518//END
519//
520#include "autobookmarker.moc"
KDialogBase
KDialog::spacingHint
static int spacingHint()
KGenericFactory
KStaticDeleter
TDEConfigBase::readEntry
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
TDEConfigBase::deleteGroup
bool deleteGroup(const TQString &group, bool bDeep=true, bool bGlobal=false)
TDEConfigBase::readNumEntry
int readNumEntry(const TQString &pKey, int nDefault=0) const
TDEConfigBase::hasGroup
bool hasGroup(const TQString &group) const
TDEConfigBase::readListEntry
int readListEntry(const TQString &pKey, TQStrList &list, char sep=',') const
TDEConfigBase::sync
virtual void sync()
TDEConfigBase::writeEntry
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
TDEConfigBase::setGroup
void setGroup(const TQString &group)
TDEConfig
TDEConfig::groupList
virtual TQStringList groupList() const
TDEListView
Kate::document
KATEPARTINTERFACES_EXPORT Document * document(KTextEditor::Document *doc)
Check if given document is a Kate::Document.
Definition: interfaces.cpp:98
TDEStdAccel::name
TQString name(StdAccel id)
TDEStdAccel::redo
const TDEShortcut & redo()
tdelocale.h

kate

Skip menu "kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kate

Skip menu "kate"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kate by doxygen 1.9.4
This website is maintained by Timothy Pearson.