• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KTextEditor

editorchooser.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2005 Joseph Wenninger <jowenn@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 #include <editorchooser.h>
00019 #include <editorchooser.moc>
00020 
00021 #include <QtGui/QComboBox>
00022 #include <QtCore/QStringList>
00023 #include <QtGui/QLabel>
00024 #include <QtGui/QLayout>
00025 
00026 #include <kmimetypetrader.h>
00027 #include <kconfig.h>
00028 #include <klocale.h>
00029 #include <kglobal.h>
00030 
00031 #include "ui_editorchooser_ui.h"
00032 #include <kconfiggroup.h>
00033 
00034 using namespace KTextEditor;
00035 
00036 namespace KTextEditor
00037 {
00038   class PrivateEditorChooser
00039   {
00040   public:
00041     PrivateEditorChooser()
00042     {
00043     }
00044     ~PrivateEditorChooser(){}
00045     // Data Members
00046     Ui::EditorChooser *chooser;
00047     QStringList ElementNames;
00048     QStringList elements;
00049   };
00050 }
00051 
00052 EditorChooser::EditorChooser(QWidget *parent)
00053   : QWidget(parent)
00054 {
00055   d = new PrivateEditorChooser ();
00056 
00057   d->chooser = new Ui::EditorChooser();
00058   d->chooser->setupUi(this);
00059 
00060   KService::List offers = KMimeTypeTrader::self()->query("text/plain", "KTextEditor/Document");
00061   KConfigGroup config = KSharedConfig::openConfig("default_components")->group("KTextEditor");
00062   QString editor = config.readPathEntry("embeddedEditor", QString());
00063 
00064   if (editor.isEmpty()) editor = "katepart";
00065 
00066   // search default component
00067   for (KService::List::Iterator it = offers.begin(); it != offers.end(); ++it)
00068   {
00069     if ((*it)->desktopEntryName().contains(editor))
00070     {
00071       d->chooser->editorCombo->addItem(i18n("System Default (currently: %1)", (*it)->name()));
00072       break;
00073     }
00074   }
00075 
00076   // add list of all available components
00077   for (KService::List::Iterator it = offers.begin(); it != offers.end(); ++it)
00078   {
00079     d->chooser->editorCombo->addItem((*it)->name());
00080     d->elements.append((*it)->desktopEntryName());
00081   }
00082   d->chooser->editorCombo->setCurrentIndex(0);
00083 
00084   connect(d->chooser->editorCombo,SIGNAL(activated(int)),this,SIGNAL(changed()));
00085 }
00086 
00087 EditorChooser:: ~EditorChooser()
00088 {
00089   delete d->chooser;
00090   delete d;
00091 }
00092 
00093 void EditorChooser::readAppSetting(const QString& postfix)
00094 {
00095   KConfigGroup cg(KGlobal::config(), "KTEXTEDITOR:" + postfix);
00096   QString editor = cg.readPathEntry("editor", QString());
00097   if (editor.isEmpty())
00098     d->chooser->editorCombo->setCurrentIndex(0);
00099   else
00100   {
00101     // + 1, because item 0 is the default one.
00102     int idx = d->elements.indexOf(editor) + 1;
00103     d->chooser->editorCombo->setCurrentIndex(idx);
00104   }
00105 }
00106 
00107 void EditorChooser::writeAppSetting(const QString& postfix)
00108 {
00109   KConfigGroup cg(KGlobal::config(), "KTEXTEDITOR:" + postfix);
00110   cg.writeEntry("DEVELOPER_INFO","NEVER TRY TO USE VALUES FROM THAT GROUP, THEY ARE SUBJECT TO CHANGES");
00111   cg.writePathEntry("editor", (d->chooser->editorCombo->currentIndex()<=0) ? //< for broken installations, where editor list is empty
00112   QString() : QString(d->elements.at(d->chooser->editorCombo->currentIndex()-1)));
00113 }
00114 
00115 KTextEditor::Editor *EditorChooser::editor(const QString& postfix,
00116                                            bool fallBackToKatePart)
00117 {
00118   // try to read the used library from the application's config
00119   KConfigGroup cg(KGlobal::config(), "KTEXTEDITOR:" + postfix);
00120   QString editor = cg.readPathEntry("editor", QString());
00121   if (editor.isEmpty())
00122   {
00123     // there is no library set in the application's config,
00124     // fall back to KDE's system default
00125     KConfig config("default_components");
00126     editor = config.group("KTextEditor").readPathEntry("embeddedEditor", "katepart");
00127   }
00128 
00129   KService::Ptr serv = KService::serviceByDesktopName(editor);
00130   if (serv)
00131   {
00132     KTextEditor::Editor *tmpEd = KTextEditor::editor(serv->library().toLatin1());
00133     if (tmpEd) return tmpEd;
00134   }
00135   if (fallBackToKatePart)
00136     return KTextEditor::editor("katepart");
00137 
00138   return 0;
00139 }
00140 
00141 // kate: space-indent on; indent-width 2; replace-tabs on;

KTextEditor

Skip menu "KTextEditor"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal