interfaces Library API Documentation

editorchooser.cpp

00001 #include <editorchooser.h>
00002 #include <editorchooser.moc>
00003 
00004 #include <qcombobox.h>
00005 #include <ktrader.h>
00006 #include <kconfig.h>
00007 #include <qstringlist.h>
00008 #include <kservice.h>
00009 #include <klocale.h>
00010 #include <qlabel.h>
00011 #include <kapplication.h>
00012 #include <qlayout.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   // Data Members
00028   EditorChooser_UI *chooser;
00029   QStringList ElementNames;
00030   QStringList elements;
00031   };
00032 
00033 }
00034 
00035 EditorChooser::EditorChooser(QWidget *parent,const char *name) :
00036         QWidget (parent,name)
00037   {
00038   d = new PrivateEditorChooser ();
00039 
00040   // sizemanagment
00041   QGridLayout *grid = new QGridLayout( this, 1, 1 );
00042 
00043 
00044   d->chooser = new EditorChooser_UI (this, name);
00045 
00046   grid->addWidget( d->chooser, 0, 0);
00047 
00048 
00049         KTrader::OfferList offers = KTrader::self()->query("text/plain", "'KTextEditor/Document' in ServiceTypes");
00050         KConfig *config=new KConfig("default_components");
00051         config->setGroup("KTextEditor");
00052         QString editor = config->readPathEntry("embeddedEditor");
00053 
00054         if (editor.isEmpty()) editor="katepart";
00055 
00056         for (KTrader::OfferList::Iterator it = offers.begin(); it != offers.end(); ++it)
00057         {
00058                 if ((*it)->desktopEntryName().contains(editor))
00059                 {
00060                         d->chooser->editorCombo->insertItem(i18n("System default (%1)").arg((*it)->name()));
00061                         break;
00062                 }
00063         }
00064 
00065         for (KTrader::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 QString& postfix){
00078         KConfig *cfg=kapp->config();
00079         QString previousGroup=cfg->group();
00080         cfg->setGroup("KTEXTEDITOR:"+postfix);
00081         QString 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 QString& postfix){
00093         KConfig *cfg=kapp->config();
00094         QString 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->writeEntry("editor",d->chooser->editorCombo->currentItem()==0?"":(*d->elements.at(d->chooser->editorCombo->currentItem()-1)));
00098         cfg->sync();
00099         cfg->setGroup(previousGroup);
00100 
00101 }
00102 
00103 KTextEditor::Document *EditorChooser::createDocument(QObject *parent,const char* name, const QString& postfix,bool fallBackToKatePart){
00104 
00105         KTextEditor::Document *tmpDoc=0;
00106 
00107         KConfig *cfg=kapp->config();
00108         QString previousGroup=cfg->group();
00109         cfg->setGroup("KTEXTEDITOR:"+postfix);
00110         QString editor=cfg->readPathEntry("editor");
00111         cfg->setGroup(previousGroup);
00112         if (editor.isEmpty())
00113         {
00114                 KConfig *config=new KConfig("default_components");
00115                 config->setGroup("KTextEditor");
00116                 editor = config->readPathEntry("embeddedEditor", "katepart");
00117                 delete config;
00118         }
00119 
00120         KService::Ptr serv=KService::serviceByDesktopName(editor);
00121         if (serv)
00122         {
00123                 tmpDoc=KTextEditor::createDocument(serv->library().latin1(),parent,name);
00124                 if (tmpDoc) return tmpDoc;
00125         }
00126         if (fallBackToKatePart)
00127                 return KTextEditor::createDocument("libkatepart",parent,name);
00128 
00129         return 0;
00130 }
00131 
00132 KTextEditor::Editor *EditorChooser::createEditor(QWidget *parentWidget,QObject *parent,const char* widgetName,
00133         const char* name,const QString& postfix,bool fallBackToKatePart){
00134 
00135         KTextEditor::Editor *tmpEd=0;
00136 
00137         KConfig *cfg=kapp->config();
00138         QString previousGroup=cfg->group();
00139         cfg->setGroup("KTEXTEDITOR:"+postfix);
00140         QString editor=cfg->readPathEntry("editor");
00141         cfg->setGroup(previousGroup);
00142         if (editor.isEmpty())
00143         {
00144                 KConfig *config=new KConfig("default_components");
00145                 config->setGroup("KTextEditor");
00146                 editor = config->readPathEntry("embeddedEditor", "katepart");
00147                 delete config;
00148         }
00149 
00150         KService::Ptr serv=KService::serviceByDesktopName(editor);
00151         if (serv)
00152         {
00153                 tmpEd=KTextEditor::createEditor(serv->library().latin1(),parentWidget,widgetName,parent,name);
00154                 if (tmpEd) return tmpEd;
00155         }
00156         if (fallBackToKatePart)
00157                 return KTextEditor::createEditor("libkatepart",parentWidget,widgetName,parent,name);
00158 
00159         return 0;
00160 }
00161 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:30:49 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001