TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

editorchooser.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <editorchooser.h>
  2. #include <editorchooser.moc>
  3. #include <tqcombobox.h>
  4. #include <ktrader.h>
  5. #include <tdeconfig.h>
  6. #include <tqstringlist.h>
  7. #include <kservice.h>
  8. #include <tdelocale.h>
  9. #include <tqlabel.h>
  10. #include <tdeapplication.h>
  11. #include <tqlayout.h>
  12. #include "editorchooser_ui.h"
  13. using namespace KTextEditor;
  14. namespace KTextEditor
  15. {
  16. class PrivateEditorChooser
  17. {
  18. public:
  19. PrivateEditorChooser()
  20. {
  21. }
  22. ~PrivateEditorChooser(){}
  23. // Data Members
  24. EditorChooser_UI *chooser;
  25. TQStringList ElementNames;
  26. TQStringList elements;
  27. };
  28. }
  29. EditorChooser::EditorChooser(TQWidget *parent,const char *name) :
  30. TQWidget (parent,name)
  31. {
  32. d = new PrivateEditorChooser ();
  33. // sizemanagment
  34. TQGridLayout *grid = new TQGridLayout( this, 1, 1 );
  35. d->chooser = new EditorChooser_UI (this, name);
  36. grid->addWidget( d->chooser, 0, 0);
  37. TDETrader::OfferList offers = TDETrader::self()->query("text/plain", "'KTextEditor/Document' in ServiceTypes");
  38. TDEConfig *config=new TDEConfig("default_components");
  39. config->setGroup("KTextEditor");
  40. TQString editor = config->readPathEntry("embeddedEditor");
  41. if (editor.isEmpty()) editor="katepart";
  42. for (TDETrader::OfferList::Iterator it = offers.begin(); it != offers.end(); ++it)
  43. {
  44. if ((*it)->desktopEntryName().contains(editor))
  45. {
  46. d->chooser->editorCombo->insertItem(TQString(i18n("System Default (%1)").arg((*it)->name())));
  47. break;
  48. }
  49. }
  50. for (TDETrader::OfferList::Iterator it = offers.begin(); it != offers.end(); ++it)
  51. {
  52. d->chooser->editorCombo->insertItem((*it)->name());
  53. d->elements.append((*it)->desktopEntryName());
  54. }
  55. d->chooser->editorCombo->setCurrentItem(0);
  56. }
  57. EditorChooser:: ~EditorChooser(){
  58. delete d;
  59. }
  60. void EditorChooser::readAppSetting(const TQString& postfix){
  61. TDEConfig *cfg=kapp->config();
  62. TQString previousGroup=cfg->group();
  63. cfg->setGroup("KTEXTEDITOR:"+postfix);
  64. TQString editor=cfg->readPathEntry("editor");
  65. if (editor.isEmpty()) d->chooser->editorCombo->setCurrentItem(0);
  66. else
  67. {
  68. int idx=d->elements.findIndex(editor);
  69. idx=idx+1;
  70. d->chooser->editorCombo->setCurrentItem(idx);
  71. }
  72. cfg->setGroup(previousGroup);
  73. }
  74. void EditorChooser::writeAppSetting(const TQString& postfix){
  75. TDEConfig *cfg=kapp->config();
  76. TQString previousGroup=cfg->group();
  77. cfg->setGroup("KTEXTEDITOR:"+postfix);
  78. cfg->writeEntry("DEVELOPER_INFO","NEVER TRY TO USE VALUES FROM THAT GROUP, THEY ARE SUBJECT TO CHANGES");
  79. cfg->writePathEntry("editor", (d->chooser->editorCombo->currentItem()==0) ?
  80. TQString::null : (*d->elements.at(d->chooser->editorCombo->currentItem()-1)));
  81. cfg->sync();
  82. cfg->setGroup(previousGroup);
  83. }
  84. KTextEditor::Document *EditorChooser::createDocument(TQObject *parent,const char* name, const TQString& postfix,bool fallBackToKatePart){
  85. KTextEditor::Document *tmpDoc=0;
  86. TDEConfig *cfg=kapp->config();
  87. TQString previousGroup=cfg->group();
  88. cfg->setGroup("KTEXTEDITOR:"+postfix);
  89. TQString editor=cfg->readPathEntry("editor");
  90. cfg->setGroup(previousGroup);
  91. if (editor.isEmpty())
  92. {
  93. TDEConfig *config=new TDEConfig("default_components");
  94. config->setGroup("KTextEditor");
  95. editor = config->readPathEntry("embeddedEditor", "katepart");
  96. delete config;
  97. }
  98. KService::Ptr serv=KService::serviceByDesktopName(editor);
  99. if (serv)
  100. {
  101. tmpDoc=KTextEditor::createDocument(serv->library().latin1(),parent,name);
  102. if (tmpDoc) return tmpDoc;
  103. }
  104. if (fallBackToKatePart)
  105. return KTextEditor::createDocument("libkatepart",parent,name);
  106. return 0;
  107. }
  108. KTextEditor::Editor *EditorChooser::createEditor(TQWidget *parentWidget,TQObject *parent,const char* widgetName,
  109. const char* name,const TQString& postfix,bool fallBackToKatePart){
  110. KTextEditor::Editor *tmpEd=0;
  111. TDEConfig *cfg=kapp->config();
  112. TQString previousGroup=cfg->group();
  113. cfg->setGroup("KTEXTEDITOR:"+postfix);
  114. TQString editor=cfg->readPathEntry("editor");
  115. cfg->setGroup(previousGroup);
  116. if (editor.isEmpty())
  117. {
  118. TDEConfig *config=new TDEConfig("default_components");
  119. config->setGroup("KTextEditor");
  120. editor = config->readPathEntry("embeddedEditor", "katepart");
  121. delete config;
  122. }
  123. KService::Ptr serv=KService::serviceByDesktopName(editor);
  124. if (serv)
  125. {
  126. tmpEd=KTextEditor::createEditor(serv->library().latin1(),parentWidget,widgetName,parent,name);
  127. if (tmpEd) return tmpEd;
  128. }
  129. if (fallBackToKatePart)
  130. return KTextEditor::createEditor("libkatepart",parentWidget,widgetName,parent,name);
  131. return 0;
  132. }