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