20 #include "katefiletype.h"
21 #include "katefiletype.moc"
23 #include "katedocument.h"
24 #include "kateconfig.h"
26 #include "katefactory.h"
28 #include <tdeconfig.h>
29 #include <kmimemagic.h>
30 #include <kmimetype.h>
31 #include <kmimetypechooser.h>
33 #include <kiconloader.h>
34 #include <knuminput.h>
36 #include <tdepopupmenu.h>
39 #include <tqcheckbox.h>
40 #include <tqcombobox.h>
41 #include <tqgroupbox.h>
44 #include <tqhgroupbox.h>
47 #include <tqlineedit.h>
48 #include <tqpushbutton.h>
49 #include <tqtoolbutton.h>
51 #include <tqvgroupbox.h>
52 #include <tqwhatsthis.h>
53 #include <tqwidgetstack.h>
55 #define KATE_FT_HOWMANY 1024
59 KateFileTypeManager::KateFileTypeManager ()
61 m_types.setAutoDelete (
true);
66 KateFileTypeManager::~KateFileTypeManager ()
73 void KateFileTypeManager::update ()
75 TDEConfig config (
"katefiletyperc",
false,
false);
77 TQStringList g (config.groupList());
81 for (uint z=0; z < g.count(); z++)
83 config.setGroup (g[z]);
85 KateFileType *type =
new KateFileType ();
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");
95 m_types.append (type);
102 void KateFileTypeManager::save (TQPtrList<KateFileType> *v)
104 TDEConfig config (
"katefiletyperc",
false,
false);
107 for (uint z=0; z < v->count(); z++)
109 config.setGroup (v->at(z)->name);
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);
116 TQString varLine = v->at(z)->varLine;
117 if (TQRegExp(
"kate:(.*)").search(varLine) < 0)
118 varLine.prepend (
"kate: ");
120 config.writeEntry (
"Variables", varLine);
122 newg << v->at(z)->name;
125 TQStringList g (config.groupList());
127 for (uint z=0; z < g.count(); z++)
129 if (newg.findIndex (g[z]) == -1)
130 config.deleteGroup (g[z]);
138 int KateFileTypeManager::fileType (KateDocument *doc)
144 if (m_types.isEmpty())
147 TQString fileName = doc->url().prettyURL();
148 int length = doc->url().prettyURL().length();
153 if ( ! fileName.isEmpty() )
155 static TQStringList commonSuffixes = TQStringList::split (
";",
".orig;.new;~;.bak;.BAK");
157 if ((result = wildcardsFind(fileName)) != -1)
160 TQString backupSuffix = KateDocumentConfig::global()->backupSuffix();
161 if (fileName.endsWith(backupSuffix)) {
162 if ((result = wildcardsFind(fileName.left(length - backupSuffix.length()))) != -1)
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)
177 else if ( (result = wildcardsFind(doc->docName())) != -1)
179 kdDebug(13020)<<
"KateFiletype::filetype(): got type "<<result<<
" using docName '"<<doc->docName()<<
"'"<<
endl;
184 KMimeType::Ptr mt = doc->mimeTypeForContent();
186 TQPtrList<KateFileType> types;
188 for (uint z=0; z < m_types.count(); z++)
190 if (m_types.at(z)->mimetypes.findIndex (mt->name()) > -1)
191 types.append (m_types.at(z));
194 if ( !types.isEmpty() )
199 for (KateFileType *type = types.first(); type != 0L; type = types.next())
201 if (type->priority > pri)
203 pri = type->priority;
215 int KateFileTypeManager::wildcardsFind (
const TQString &fileName)
217 TQPtrList<KateFileType> types;
219 for (uint z=0; z < m_types.count(); z++)
221 for( TQStringList::Iterator it = m_types.at(z)->wildcards.begin(); it != m_types.at(z)->wildcards.end(); ++it )
225 TQRegExp re(*it,
true,
true);
226 if ( ( re.search( fileName ) > -1 ) && ( re.matchedLength() == (
int)fileName.length() ) )
227 types.append (m_types.at(z));
231 if ( !types.isEmpty() )
236 for (KateFileType *type = types.first(); type != 0L; type = types.next())
238 if (type->priority > pri)
240 pri = type->priority;
251 const KateFileType *KateFileTypeManager::fileType (uint number)
253 if (number < m_types.count())
254 return m_types.at(number);
261 KateFileTypeConfigTab::KateFileTypeConfigTab( TQWidget *parent )
262 : KateConfigPage( parent )
264 m_types.setAutoDelete (
true);
270 TQHBox *hbHl =
new TQHBox(
this );
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)) );
279 TQPushButton *btnnew =
new TQPushButton( i18n(
"&New"), hbHl );
280 connect( btnnew, TQ_SIGNAL(clicked()),
this, TQ_SLOT(newType()) );
282 btndel =
new TQPushButton( i18n(
"&Delete"), hbHl );
283 connect( btndel, TQ_SIGNAL(clicked()),
this, TQ_SLOT(deleteType()) );
285 gbProps =
new TQGroupBox( 2, TQt::Horizontal, i18n(
"Properties"),
this );
286 layout->add (gbProps);
289 TQLabel *lname =
new TQLabel( i18n(
"N&ame:"), gbProps );
290 name =
new TQLineEdit( gbProps );
291 lname->setBuddy( name );
294 TQLabel *lsec =
new TQLabel( i18n(
"&Section:"), gbProps );
295 section =
new TQLineEdit( gbProps );
296 lsec->setBuddy( section );
299 TQLabel *lvar =
new TQLabel( i18n(
"&Variables:"), gbProps );
300 varLine =
new TQLineEdit( gbProps );
301 lvar->setBuddy( varLine );
304 TQLabel *lFileExts =
new TQLabel( i18n(
"File e&xtensions:"), gbProps );
305 wildcards =
new TQLineEdit( gbProps );
306 lFileExts->setBuddy( wildcards );
308 TQLabel *lMimeTypes =
new TQLabel( i18n(
"MIME &types:"), gbProps);
309 TQHBox *hbMT =
new TQHBox (gbProps);
310 mimetypes =
new TQLineEdit( hbMT );
311 lMimeTypes->setBuddy( mimetypes );
313 TQToolButton *btnMTW =
new TQToolButton(hbMT);
314 btnMTW->setIconSet(TQIconSet(SmallIcon(
"wizard")));
315 connect(btnMTW, TQ_SIGNAL(clicked()),
this, TQ_SLOT(showMTDlg()));
317 TQLabel *lprio =
new TQLabel( i18n(
"Prio&rity:"), gbProps);
319 lprio->setBuddy( priority );
321 layout->addStretch();
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() ) );
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 "
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." ) );
359 void KateFileTypeConfigTab::apply()
366 KateFactory::self()->fileTypeManager()->save(&m_types);
369 void KateFileTypeConfigTab::reload()
372 for (uint z=0; z < KateFactory::self()->fileTypeManager()->list()->count(); z++)
374 KateFileType *type =
new KateFileType ();
376 *type = *KateFactory::self()->fileTypeManager()->list()->at(z);
378 m_types.append (type);
384 void KateFileTypeConfigTab::reset()
389 void KateFileTypeConfigTab::defaults()
394 void KateFileTypeConfigTab::update ()
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);
404 typeCombo->insertItem(m_types.at(i)->name);
407 typeCombo->setCurrentItem (0);
411 typeCombo->setEnabled (typeCombo->count() > 0);
414 void KateFileTypeConfigTab::deleteType ()
416 int type = typeCombo->currentItem ();
418 if ((type > -1) && ((uint)type < m_types.count()))
420 m_types.remove (type);
425 void KateFileTypeConfigTab::newType ()
427 TQString newN = i18n(
"New Filetype");
429 for( uint i = 0; i < m_types.count(); i++) {
430 if (m_types.at(i)->name == newN)
432 typeCombo->setCurrentItem (i);
438 KateFileType *newT =
new KateFileType ();
442 m_types.prepend (newT);
447 void KateFileTypeConfigTab::save ()
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();
460 void KateFileTypeConfigTab::typeChanged (
int type)
466 if ((type > -1) && ((uint)type < m_types.count()))
467 t = m_types.at(type);
471 gbProps->setTitle (i18n(
"Properties of %1").arg (typeCombo->currentText()));
473 gbProps->setEnabled (
true);
474 btndel->setEnabled (
true);
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);
485 gbProps->setTitle (i18n(
"Properties"));
487 gbProps->setEnabled (
false);
488 btndel->setEnabled (
false);
495 priority->setValue(0);
501 void KateFileTypeConfigTab::showMTDlg()
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 ) {
510 wildcards->setText( d.chooser()->patterns().join(
";") );
511 mimetypes->setText( d.chooser()->mimeTypes().join(
";") );
517 void KateViewFileTypeAction::init()
520 subMenus.setAutoDelete(
true );
522 popupMenu()->insertItem ( i18n(
"None"),
this, TQ_SLOT(setType(
int)), 0, 0);
524 connect(popupMenu(),TQ_SIGNAL(aboutToShow()),
this,TQ_SLOT(slotAboutToShow()));
529 m_doc = (KateDocument *)doc;
532 void KateViewFileTypeAction::slotAboutToShow()
534 KateDocument *doc=m_doc;
535 int count = KateFactory::self()->fileTypeManager()->list()->count();
537 for (
int z=0; z<count; z++)
539 TQString hlName = KateFactory::self()->fileTypeManager()->list()->at(z)->name;
540 TQString hlSection = KateFactory::self()->fileTypeManager()->list()->at(z)->section;
542 if ( !hlSection.isEmpty() && (names.contains(hlName) < 1) )
544 if (subMenusName.contains(hlSection) < 1)
546 subMenusName << hlSection;
547 TQPopupMenu *menu =
new TQPopupMenu ();
548 subMenus.append(menu);
549 popupMenu()->insertItem (hlSection, menu);
552 int m = subMenusName.findIndex (hlSection);
554 subMenus.at(m)->insertItem ( hlName,
this, TQ_SLOT(setType(
int)), 0, z+1);
556 else if (names.contains(hlName) < 1)
559 popupMenu()->insertItem ( hlName,
this, TQ_SLOT(setType(
int)), 0, z+1);
565 for (uint i=0;i<subMenus.count();i++)
567 for (uint i2=0;i2<subMenus.at(i)->count();i2++)
568 subMenus.at(i)->setItemChecked(subMenus.at(i)->idAt(i2),
false);
570 popupMenu()->setItemChecked (0,
false);
572 if (doc->fileType() == -1)
573 popupMenu()->setItemChecked (0,
true);
576 const KateFileType *t = 0;
577 if ((t = KateFactory::self()->fileTypeManager()->fileType (doc->fileType())))
579 int i = subMenusName.findIndex (t->section);
580 if (i >= 0 && subMenus.at(i))
581 subMenus.at(i)->setItemChecked (doc->fileType()+1,
true);
583 popupMenu()->setItemChecked (0,
true);
588 void KateViewFileTypeAction::setType (
int mode)
590 KateDocument *doc=m_doc;
593 doc->updateFileType(mode-1,
true);
This interface provides access to the Kate Document class.
kndbgstream & endl(kndbgstream &s)
kdbgstream kdDebug(int area=0)
TQString name(StdAccel id)
const TDEShortcut & save()
const TDEShortcut & reload()