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

kate

  • kate
  • part
katefiletype.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2001-2003 Christoph Cullmann <cullmann@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 //BEGIN Includes
20 #include "katefiletype.h"
21 #include "katefiletype.moc"
22 
23 #include "katedocument.h"
24 #include "kateconfig.h"
25 #include "kateview.h"
26 #include "katefactory.h"
27 
28 #include <tdeconfig.h>
29 #include <kmimemagic.h>
30 #include <kmimetype.h>
31 #include <kmimetypechooser.h>
32 #include <kdebug.h>
33 #include <kiconloader.h>
34 #include <knuminput.h>
35 #include <tdelocale.h>
36 #include <tdepopupmenu.h>
37 
38 #include <tqregexp.h>
39 #include <tqcheckbox.h>
40 #include <tqcombobox.h>
41 #include <tqgroupbox.h>
42 #include <tqhbox.h>
43 #include <tqheader.h>
44 #include <tqhgroupbox.h>
45 #include <tqlabel.h>
46 #include <tqlayout.h>
47 #include <tqlineedit.h>
48 #include <tqpushbutton.h>
49 #include <tqtoolbutton.h>
50 #include <tqvbox.h>
51 #include <tqvgroupbox.h>
52 #include <tqwhatsthis.h>
53 #include <tqwidgetstack.h>
54 
55 #define KATE_FT_HOWMANY 1024
56 //END Includes
57 
58 //BEGIN KateFileTypeManager
59 KateFileTypeManager::KateFileTypeManager ()
60 {
61  m_types.setAutoDelete (true);
62 
63  update ();
64 }
65 
66 KateFileTypeManager::~KateFileTypeManager ()
67 {
68 }
69 
70 //
71 // read the types from config file and update the internal list
72 //
73 void KateFileTypeManager::update ()
74 {
75  TDEConfig config ("katefiletyperc", false, false);
76 
77  TQStringList g (config.groupList());
78  g.sort ();
79 
80  m_types.clear ();
81  for (uint z=0; z < g.count(); z++)
82  {
83  config.setGroup (g[z]);
84 
85  KateFileType *type = new KateFileType ();
86 
87  type->number = z;
88  type->name = g[z];
89  type->section = config.readEntry ("Section");
90  type->wildcards = config.readListEntry ("Wildcards", ';');
91  type->mimetypes = config.readListEntry ("Mimetypes", ';');
92  type->priority = config.readNumEntry ("Priority");
93  type->varLine = config.readEntry ("Variables");
94 
95  m_types.append (type);
96  }
97 }
98 
99 //
100 // save the given list to config file + update
101 //
102 void KateFileTypeManager::save (TQPtrList<KateFileType> *v)
103 {
104  TDEConfig config ("katefiletyperc", false, false);
105 
106  TQStringList newg;
107  for (uint z=0; z < v->count(); z++)
108  {
109  config.setGroup (v->at(z)->name);
110 
111  config.writeEntry ("Section", v->at(z)->section);
112  config.writeEntry ("Wildcards", v->at(z)->wildcards, ';');
113  config.writeEntry ("Mimetypes", v->at(z)->mimetypes, ';');
114  config.writeEntry ("Priority", v->at(z)->priority);
115 
116  TQString varLine = v->at(z)->varLine;
117  if (TQRegExp("kate:(.*)").search(varLine) < 0)
118  varLine.prepend ("kate: ");
119 
120  config.writeEntry ("Variables", varLine);
121 
122  newg << v->at(z)->name;
123  }
124 
125  TQStringList g (config.groupList());
126 
127  for (uint z=0; z < g.count(); z++)
128  {
129  if (newg.findIndex (g[z]) == -1)
130  config.deleteGroup (g[z]);
131  }
132 
133  config.sync ();
134 
135  update ();
136 }
137 
138 int KateFileTypeManager::fileType (KateDocument *doc)
139 {
140  kdDebug(13020)<<k_funcinfo<<endl;
141  if (!doc)
142  return -1;
143 
144  if (m_types.isEmpty())
145  return -1;
146 
147  TQString fileName = doc->url().prettyURL();
148  int length = doc->url().prettyURL().length();
149 
150  int result;
151 
152  // Try wildcards
153  if ( ! fileName.isEmpty() )
154  {
155  static TQStringList commonSuffixes = TQStringList::split (";", ".orig;.new;~;.bak;.BAK");
156 
157  if ((result = wildcardsFind(fileName)) != -1)
158  return result;
159 
160  TQString backupSuffix = KateDocumentConfig::global()->backupSuffix();
161  if (fileName.endsWith(backupSuffix)) {
162  if ((result = wildcardsFind(fileName.left(length - backupSuffix.length()))) != -1)
163  return result;
164  }
165 
166  for (TQStringList::Iterator it = commonSuffixes.begin(); it != commonSuffixes.end(); ++it) {
167  if (*it != backupSuffix && fileName.endsWith(*it)) {
168  if ((result = wildcardsFind(fileName.left(length - (*it).length()))) != -1)
169  return result;
170  }
171  }
172  }
173 
174  // Even try the document name, if the URL is empty
175  // This is usefull if the document name is set for example by a plugin which
176  // created the document
177  else if ( (result = wildcardsFind(doc->docName())) != -1)
178  {
179  kdDebug(13020)<<"KateFiletype::filetype(): got type "<<result<<" using docName '"<<doc->docName()<<"'"<<endl;
180  return result;
181  }
182 
183  // Try content-based mimetype
184  KMimeType::Ptr mt = doc->mimeTypeForContent();
185 
186  TQPtrList<KateFileType> types;
187 
188  for (uint z=0; z < m_types.count(); z++)
189  {
190  if (m_types.at(z)->mimetypes.findIndex (mt->name()) > -1)
191  types.append (m_types.at(z));
192  }
193 
194  if ( !types.isEmpty() )
195  {
196  int pri = -1;
197  int hl = -1;
198 
199  for (KateFileType *type = types.first(); type != 0L; type = types.next())
200  {
201  if (type->priority > pri)
202  {
203  pri = type->priority;
204  hl = type->number;
205  }
206  }
207 
208  return hl;
209  }
210 
211 
212  return -1;
213 }
214 
215 int KateFileTypeManager::wildcardsFind (const TQString &fileName)
216 {
217  TQPtrList<KateFileType> types;
218 
219  for (uint z=0; z < m_types.count(); z++)
220  {
221  for( TQStringList::Iterator it = m_types.at(z)->wildcards.begin(); it != m_types.at(z)->wildcards.end(); ++it )
222  {
223  // anders: we need to be sure to match the end of string, as eg a css file
224  // would otherwise end up with the c hl
225  TQRegExp re(*it, true, true);
226  if ( ( re.search( fileName ) > -1 ) && ( re.matchedLength() == (int)fileName.length() ) )
227  types.append (m_types.at(z));
228  }
229  }
230 
231  if ( !types.isEmpty() )
232  {
233  int pri = -1;
234  int hl = -1;
235 
236  for (KateFileType *type = types.first(); type != 0L; type = types.next())
237  {
238  if (type->priority > pri)
239  {
240  pri = type->priority;
241  hl = type->number;
242  }
243  }
244 
245  return hl;
246  }
247 
248  return -1;
249 }
250 
251 const KateFileType *KateFileTypeManager::fileType (uint number)
252 {
253  if (number < m_types.count())
254  return m_types.at(number);
255 
256  return 0;
257 }
258 //END KateFileTypeManager
259 
260 //BEGIN KateFileTypeConfigTab
261 KateFileTypeConfigTab::KateFileTypeConfigTab( TQWidget *parent )
262  : KateConfigPage( parent )
263 {
264  m_types.setAutoDelete (true);
265  m_lastType = 0;
266 
267  TQVBoxLayout *layout = new TQVBoxLayout(this, 0, KDialog::spacingHint() );
268 
269  // hl chooser
270  TQHBox *hbHl = new TQHBox( this );
271  layout->add (hbHl);
272  hbHl->setSpacing( KDialog::spacingHint() );
273  TQLabel *lHl = new TQLabel( i18n("&Filetype:"), hbHl );
274  typeCombo = new TQComboBox( false, hbHl );
275  lHl->setBuddy( typeCombo );
276  connect( typeCombo, TQ_SIGNAL(activated(int)),
277  this, TQ_SLOT(typeChanged(int)) );
278 
279  TQPushButton *btnnew = new TQPushButton( i18n("&New"), hbHl );
280  connect( btnnew, TQ_SIGNAL(clicked()), this, TQ_SLOT(newType()) );
281 
282  btndel = new TQPushButton( i18n("&Delete"), hbHl );
283  connect( btndel, TQ_SIGNAL(clicked()), this, TQ_SLOT(deleteType()) );
284 
285  gbProps = new TQGroupBox( 2, TQt::Horizontal, i18n("Properties"), this );
286  layout->add (gbProps);
287 
288  // file & mime types
289  TQLabel *lname = new TQLabel( i18n("N&ame:"), gbProps );
290  name = new TQLineEdit( gbProps );
291  lname->setBuddy( name );
292 
293  // file & mime types
294  TQLabel *lsec = new TQLabel( i18n("&Section:"), gbProps );
295  section = new TQLineEdit( gbProps );
296  lsec->setBuddy( section );
297 
298  // file & mime types
299  TQLabel *lvar = new TQLabel( i18n("&Variables:"), gbProps );
300  varLine = new TQLineEdit( gbProps );
301  lvar->setBuddy( varLine );
302 
303  // file & mime types
304  TQLabel *lFileExts = new TQLabel( i18n("File e&xtensions:"), gbProps );
305  wildcards = new TQLineEdit( gbProps );
306  lFileExts->setBuddy( wildcards );
307 
308  TQLabel *lMimeTypes = new TQLabel( i18n("MIME &types:"), gbProps);
309  TQHBox *hbMT = new TQHBox (gbProps);
310  mimetypes = new TQLineEdit( hbMT );
311  lMimeTypes->setBuddy( mimetypes );
312 
313  TQToolButton *btnMTW = new TQToolButton(hbMT);
314  btnMTW->setIconSet(TQIconSet(SmallIcon("wizard")));
315  connect(btnMTW, TQ_SIGNAL(clicked()), this, TQ_SLOT(showMTDlg()));
316 
317  TQLabel *lprio = new TQLabel( i18n("Prio&rity:"), gbProps);
318  priority = new KIntNumInput( gbProps );
319  lprio->setBuddy( priority );
320 
321  layout->addStretch();
322 
323  reload();
324 
325  connect( name, TQ_SIGNAL( textChanged ( const TQString & ) ), this, TQ_SLOT( slotChanged() ) );
326  connect( section, TQ_SIGNAL( textChanged ( const TQString & ) ), this, TQ_SLOT( slotChanged() ) );
327  connect( varLine, TQ_SIGNAL( textChanged ( const TQString & ) ), this, TQ_SLOT( slotChanged() ) );
328  connect( wildcards, TQ_SIGNAL( textChanged ( const TQString & ) ), this, TQ_SLOT( slotChanged() ) );
329  connect( mimetypes, TQ_SIGNAL( textChanged ( const TQString & ) ), this, TQ_SLOT( slotChanged() ) );
330  connect( priority, TQ_SIGNAL( valueChanged ( int ) ), this, TQ_SLOT( slotChanged() ) );
331 
332  TQWhatsThis::add( btnnew, i18n("Create a new file type.") );
333  TQWhatsThis::add( btndel, i18n("Delete the current file type.") );
334  TQWhatsThis::add( name, i18n(
335  "The name of the filetype will be the text of the corresponding menu item.") );
336  TQWhatsThis::add( section, i18n(
337  "The section name is used to organize the file types in menus.") );
338  TQWhatsThis::add( varLine, i18n(
339  "<p>This string allows you to configure Kate's settings for the files "
340  "selected by this mimetype using Kate variables. You can set almost any "
341  "configuration option, such as highlight, indent-mode, encoding, etc.</p>"
342  "<p>For a full list of known variables, see the manual.</p>") );
343  TQWhatsThis::add( wildcards, i18n(
344  "The wildcards mask allows you to select files by filename. A typical "
345  "mask uses an asterisk and the file extension, for example "
346  "<code>*.txt; *.text</code>. The string is a semicolon-separated list "
347  "of masks.") );
348  TQWhatsThis::add( mimetypes, i18n(
349  "The mime type mask allows you to select files by mimetype. The string is "
350  "a semicolon-separated list of mimetypes, for example "
351  "<code>text/plain; text/english</code>.") );
352  TQWhatsThis::add( btnMTW, i18n(
353  "Displays a wizard that helps you easily select mimetypes.") );
354  TQWhatsThis::add( priority, i18n(
355  "Sets a priority for this file type. If more than one file type selects the same "
356  "file, the one with the highest priority will be used." ) );
357 }
358 
359 void KateFileTypeConfigTab::apply()
360 {
361  if (!changed())
362  return;
363 
364  save ();
365 
366  KateFactory::self()->fileTypeManager()->save(&m_types);
367 }
368 
369 void KateFileTypeConfigTab::reload()
370 {
371  m_types.clear();
372  for (uint z=0; z < KateFactory::self()->fileTypeManager()->list()->count(); z++)
373  {
374  KateFileType *type = new KateFileType ();
375 
376  *type = *KateFactory::self()->fileTypeManager()->list()->at(z);
377 
378  m_types.append (type);
379  }
380 
381  update ();
382 }
383 
384 void KateFileTypeConfigTab::reset()
385 {
386  reload ();
387 }
388 
389 void KateFileTypeConfigTab::defaults()
390 {
391  reload ();
392 }
393 
394 void KateFileTypeConfigTab::update ()
395 {
396  m_lastType = 0;
397 
398  typeCombo->clear ();
399 
400  for( uint i = 0; i < m_types.count(); i++) {
401  if (m_types.at(i)->section.length() > 0)
402  typeCombo->insertItem(m_types.at(i)->section + TQString ("/") + m_types.at(i)->name);
403  else
404  typeCombo->insertItem(m_types.at(i)->name);
405  }
406 
407  typeCombo->setCurrentItem (0);
408 
409  typeChanged (0);
410 
411  typeCombo->setEnabled (typeCombo->count() > 0);
412 }
413 
414 void KateFileTypeConfigTab::deleteType ()
415 {
416  int type = typeCombo->currentItem ();
417 
418  if ((type > -1) && ((uint)type < m_types.count()))
419  {
420  m_types.remove (type);
421  update ();
422  }
423 }
424 
425 void KateFileTypeConfigTab::newType ()
426 {
427  TQString newN = i18n("New Filetype");
428 
429  for( uint i = 0; i < m_types.count(); i++) {
430  if (m_types.at(i)->name == newN)
431  {
432  typeCombo->setCurrentItem (i);
433  typeChanged (i);
434  return;
435  }
436  }
437 
438  KateFileType *newT = new KateFileType ();
439  newT->priority = 0;
440  newT->name = newN;
441 
442  m_types.prepend (newT);
443 
444  update ();
445 }
446 
447 void KateFileTypeConfigTab::save ()
448 {
449  if (m_lastType)
450  {
451  m_lastType->name = name->text ();
452  m_lastType->section = section->text ();
453  m_lastType->varLine = varLine->text ();
454  m_lastType->wildcards = TQStringList::split (";", wildcards->text ());
455  m_lastType->mimetypes = TQStringList::split (";", mimetypes->text ());
456  m_lastType->priority = priority->value();
457  }
458 }
459 
460 void KateFileTypeConfigTab::typeChanged (int type)
461 {
462  save ();
463 
464  KateFileType *t = 0;
465 
466  if ((type > -1) && ((uint)type < m_types.count()))
467  t = m_types.at(type);
468 
469  if (t)
470  {
471  gbProps->setTitle (i18n("Properties of %1").arg (typeCombo->currentText()));
472 
473  gbProps->setEnabled (true);
474  btndel->setEnabled (true);
475 
476  name->setText(t->name);
477  section->setText(t->section);
478  varLine->setText(t->varLine);
479  wildcards->setText(t->wildcards.join (";"));
480  mimetypes->setText(t->mimetypes.join (";"));
481  priority->setValue(t->priority);
482  }
483  else
484  {
485  gbProps->setTitle (i18n("Properties"));
486 
487  gbProps->setEnabled (false);
488  btndel->setEnabled (false);
489 
490  name->clear();
491  section->clear();
492  varLine->clear();
493  wildcards->clear();
494  mimetypes->clear();
495  priority->setValue(0);
496  }
497 
498  m_lastType = t;
499 }
500 
501 void KateFileTypeConfigTab::showMTDlg()
502 {
503 
504  TQString text = i18n("Select the MimeTypes you want for this file type.\nPlease note that this will automatically edit the associated file extensions as well.");
505  TQStringList list = TQStringList::split( TQRegExp("\\s*;\\s*"), mimetypes->text() );
506  KMimeTypeChooserDialog d( i18n("Select Mime Types"), text, list, "text", this );
507  if ( d.exec() == KDialogBase::Accepted ) {
508  // do some checking, warn user if mime types or patterns are removed.
509  // if the lists are empty, and the fields not, warn.
510  wildcards->setText( d.chooser()->patterns().join(";") );
511  mimetypes->setText( d.chooser()->mimeTypes().join(";") );
512  }
513 }
514 //END KateFileTypeConfigTab
515 
516 //BEGIN KateViewFileTypeAction
517 void KateViewFileTypeAction::init()
518 {
519  m_doc = 0;
520  subMenus.setAutoDelete( true );
521 
522  popupMenu()->insertItem ( i18n("None"), this, TQ_SLOT(setType(int)), 0, 0);
523 
524  connect(popupMenu(),TQ_SIGNAL(aboutToShow()),this,TQ_SLOT(slotAboutToShow()));
525 }
526 
527 void KateViewFileTypeAction::updateMenu (Kate::Document *doc)
528 {
529  m_doc = (KateDocument *)doc;
530 }
531 
532 void KateViewFileTypeAction::slotAboutToShow()
533 {
534  KateDocument *doc=m_doc;
535  int count = KateFactory::self()->fileTypeManager()->list()->count();
536 
537  for (int z=0; z<count; z++)
538  {
539  TQString hlName = KateFactory::self()->fileTypeManager()->list()->at(z)->name;
540  TQString hlSection = KateFactory::self()->fileTypeManager()->list()->at(z)->section;
541 
542  if ( !hlSection.isEmpty() && (names.contains(hlName) < 1) )
543  {
544  if (subMenusName.contains(hlSection) < 1)
545  {
546  subMenusName << hlSection;
547  TQPopupMenu *menu = new TQPopupMenu ();
548  subMenus.append(menu);
549  popupMenu()->insertItem (hlSection, menu);
550  }
551 
552  int m = subMenusName.findIndex (hlSection);
553  names << hlName;
554  subMenus.at(m)->insertItem ( hlName, this, TQ_SLOT(setType(int)), 0, z+1);
555  }
556  else if (names.contains(hlName) < 1)
557  {
558  names << hlName;
559  popupMenu()->insertItem ( hlName, this, TQ_SLOT(setType(int)), 0, z+1);
560  }
561  }
562 
563  if (!doc) return;
564 
565  for (uint i=0;i<subMenus.count();i++)
566  {
567  for (uint i2=0;i2<subMenus.at(i)->count();i2++)
568  subMenus.at(i)->setItemChecked(subMenus.at(i)->idAt(i2),false);
569  }
570  popupMenu()->setItemChecked (0, false);
571 
572  if (doc->fileType() == -1)
573  popupMenu()->setItemChecked (0, true);
574  else
575  {
576  const KateFileType *t = 0;
577  if ((t = KateFactory::self()->fileTypeManager()->fileType (doc->fileType())))
578  {
579  int i = subMenusName.findIndex (t->section);
580  if (i >= 0 && subMenus.at(i))
581  subMenus.at(i)->setItemChecked (doc->fileType()+1, true);
582  else
583  popupMenu()->setItemChecked (0, true);
584  }
585  }
586 }
587 
588 void KateViewFileTypeAction::setType (int mode)
589 {
590  KateDocument *doc=m_doc;
591 
592  if (doc)
593  doc->updateFileType(mode-1, true);
594 }
595 //END KateViewFileTypeAction
KDialog::spacingHint
static int spacingHint()
KIntNumInput
Kate::Document
This interface provides access to the Kate Document class.
Definition: document.h:190
TDEConfig
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
TDEStdAccel::name
TQString name(StdAccel id)
TDEStdAccel::save
const TDEShortcut & save()
TDEStdAccel::reload
const TDEShortcut & reload()
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.1
This website is maintained by Timothy Pearson.