interfaces
editorchooser.cpp
00001 #include <editorchooser.h>
00002 #include <editorchooser.moc>
00003
00004 #include <tqcombobox.h>
00005 #include <ktrader.h>
00006 #include <tdeconfig.h>
00007 #include <tqstringlist.h>
00008 #include <kservice.h>
00009 #include <tdelocale.h>
00010 #include <tqlabel.h>
00011 #include <tdeapplication.h>
00012 #include <tqlayout.h>
00013
00014 #include "editorchooser_ui.h"
00015
00016 using namespace KTextEditor;
00017
00018 namespace KTextEditor
00019 {
00020 class PrivateEditorChooser
00021 {
00022 public:
00023 PrivateEditorChooser()
00024 {
00025 }
00026 ~PrivateEditorChooser(){}
00027
00028 EditorChooser_UI *chooser;
00029 TQStringList ElementNames;
00030 TQStringList elements;
00031 };
00032
00033 }
00034
00035 EditorChooser::EditorChooser(TQWidget *parent,const char *name) :
00036 TQWidget (parent,name)
00037 {
00038 d = new PrivateEditorChooser ();
00039
00040
00041 TQGridLayout *grid = new TQGridLayout( this, 1, 1 );
00042
00043
00044 d->chooser = new EditorChooser_UI (this, name);
00045
00046 grid->addWidget( d->chooser, 0, 0);
00047
00048
00049 TDETrader::OfferList offers = TDETrader::self()->query("text/plain", "'KTextEditor/Document' in ServiceTypes");
00050 TDEConfig *config=new TDEConfig("default_components");
00051 config->setGroup("KTextEditor");
00052 TQString editor = config->readPathEntry("embeddedEditor");
00053
00054 if (editor.isEmpty()) editor="katepart";
00055
00056 for (TDETrader::OfferList::Iterator it = offers.begin(); it != offers.end(); ++it)
00057 {
00058 if ((*it)->desktopEntryName().contains(editor))
00059 {
00060 d->chooser->editorCombo->insertItem(TQString(i18n("System Default (%1)").arg((*it)->name())));
00061 break;
00062 }
00063 }
00064
00065 for (TDETrader::OfferList::Iterator it = offers.begin(); it != offers.end(); ++it)
00066 {
00067 d->chooser->editorCombo->insertItem((*it)->name());
00068 d->elements.append((*it)->desktopEntryName());
00069 }
00070 d->chooser->editorCombo->setCurrentItem(0);
00071 }
00072
00073 EditorChooser:: ~EditorChooser(){
00074 delete d;
00075 }
00076
00077 void EditorChooser::readAppSetting(const TQString& postfix){
00078 TDEConfig *cfg=kapp->config();
00079 TQString previousGroup=cfg->group();
00080 cfg->setGroup("KTEXTEDITOR:"+postfix);
00081 TQString editor=cfg->readPathEntry("editor");
00082 if (editor.isEmpty()) d->chooser->editorCombo->setCurrentItem(0);
00083 else
00084 {
00085 int idx=d->elements.findIndex(editor);
00086 idx=idx+1;
00087 d->chooser->editorCombo->setCurrentItem(idx);
00088 }
00089 cfg->setGroup(previousGroup);
00090 }
00091
00092 void EditorChooser::writeAppSetting(const TQString& postfix){
00093 TDEConfig *cfg=kapp->config();
00094 TQString previousGroup=cfg->group();
00095 cfg->setGroup("KTEXTEDITOR:"+postfix);
00096 cfg->writeEntry("DEVELOPER_INFO","NEVER TRY TO USE VALUES FROM THAT GROUP, THEY ARE SUBJECT TO CHANGES");
00097 cfg->writePathEntry("editor", (d->chooser->editorCombo->currentItem()==0) ?
00098 TQString::null : (*d->elements.at(d->chooser->editorCombo->currentItem()-1)));
00099 cfg->sync();
00100 cfg->setGroup(previousGroup);
00101
00102 }
00103
00104 KTextEditor::Document *EditorChooser::createDocument(TQObject *parent,const char* name, const TQString& postfix,bool fallBackToKatePart){
00105
00106 KTextEditor::Document *tmpDoc=0;
00107
00108 TDEConfig *cfg=kapp->config();
00109 TQString previousGroup=cfg->group();
00110 cfg->setGroup("KTEXTEDITOR:"+postfix);
00111 TQString editor=cfg->readPathEntry("editor");
00112 cfg->setGroup(previousGroup);
00113 if (editor.isEmpty())
00114 {
00115 TDEConfig *config=new TDEConfig("default_components");
00116 config->setGroup("KTextEditor");
00117 editor = config->readPathEntry("embeddedEditor", "katepart");
00118 delete config;
00119 }
00120
00121 KService::Ptr serv=KService::serviceByDesktopName(editor);
00122 if (serv)
00123 {
00124 tmpDoc=KTextEditor::createDocument(serv->library().latin1(),parent,name);
00125 if (tmpDoc) return tmpDoc;
00126 }
00127 if (fallBackToKatePart)
00128 return KTextEditor::createDocument("libkatepart",parent,name);
00129
00130 return 0;
00131 }
00132
00133 KTextEditor::Editor *EditorChooser::createEditor(TQWidget *parentWidget,TQObject *parent,const char* widgetName,
00134 const char* name,const TQString& postfix,bool fallBackToKatePart){
00135
00136 KTextEditor::Editor *tmpEd=0;
00137
00138 TDEConfig *cfg=kapp->config();
00139 TQString previousGroup=cfg->group();
00140 cfg->setGroup("KTEXTEDITOR:"+postfix);
00141 TQString editor=cfg->readPathEntry("editor");
00142 cfg->setGroup(previousGroup);
00143 if (editor.isEmpty())
00144 {
00145 TDEConfig *config=new TDEConfig("default_components");
00146 config->setGroup("KTextEditor");
00147 editor = config->readPathEntry("embeddedEditor", "katepart");
00148 delete config;
00149 }
00150
00151 KService::Ptr serv=KService::serviceByDesktopName(editor);
00152 if (serv)
00153 {
00154 tmpEd=KTextEditor::createEditor(serv->library().latin1(),parentWidget,widgetName,parent,name);
00155 if (tmpEd) return tmpEd;
00156 }
00157 if (fallBackToKatePart)
00158 return KTextEditor::createEditor("libkatepart",parentWidget,widgetName,parent,name);
00159
00160 return 0;
00161 }
00162