KTextEditor
editorchooser.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
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
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
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) ?
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
00119 KConfigGroup cg(KGlobal::config(), "KTEXTEDITOR:" + postfix);
00120 QString editor = cg.readPathEntry("editor", QString());
00121 if (editor.isEmpty())
00122 {
00123
00124
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