1 | /* This file is part of the KDE libraries |
2 | * Copyright (C) 2005 Joseph Wenninger <jowenn@kde.org> |
3 | * |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Library General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2 of the License, or (at your option) any later version. |
8 | * |
9 | * This library is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Library General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Library General Public License |
15 | * along with this library; see the file COPYING.LIB. If not, write to |
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301, USA. |
18 | */ |
19 | #include <editorchooser.h> |
20 | #include <editorchooser.moc> |
21 | |
22 | #include <QtGui/QComboBox> |
23 | #include <QtCore/QStringList> |
24 | #include <QtGui/QLabel> |
25 | #include <QtGui/QLayout> |
26 | |
27 | #include <kmimetypetrader.h> |
28 | #include <kconfig.h> |
29 | #include <klocale.h> |
30 | #include <kglobal.h> |
31 | |
32 | #include "ui_editorchooser_ui.h" |
33 | #include <kconfiggroup.h> |
34 | |
35 | using namespace KTextEditor; |
36 | |
37 | namespace KTextEditor |
38 | { |
39 | class PrivateEditorChooser |
40 | { |
41 | public: |
42 | PrivateEditorChooser() |
43 | { |
44 | } |
45 | ~PrivateEditorChooser(){} |
46 | // Data Members |
47 | Ui::EditorChooser *chooser; |
48 | QStringList ElementNames; |
49 | QStringList elements; |
50 | }; |
51 | } |
52 | |
53 | EditorChooser::EditorChooser(QWidget *parent) |
54 | : QWidget(parent) |
55 | { |
56 | d = new PrivateEditorChooser (); |
57 | |
58 | d->chooser = new Ui::EditorChooser(); |
59 | d->chooser->setupUi(this); |
60 | |
61 | KService::List offers = KMimeTypeTrader::self()->query("text/plain" , "KTextEditor/Document" ); |
62 | KConfigGroup config = KSharedConfig::openConfig("default_components" )->group("KTextEditor" ); |
63 | QString editor = config.readPathEntry("embeddedEditor" , QString()); |
64 | |
65 | if (editor.isEmpty()) editor = "katepart" ; |
66 | |
67 | // search default component |
68 | for (KService::List::Iterator it = offers.begin(); it != offers.end(); ++it) |
69 | { |
70 | if ((*it)->desktopEntryName().contains(editor)) |
71 | { |
72 | d->chooser->editorCombo->addItem(i18n("System Default (currently: %1)" , (*it)->name())); |
73 | break; |
74 | } |
75 | } |
76 | |
77 | // add list of all available components |
78 | for (KService::List::Iterator it = offers.begin(); it != offers.end(); ++it) |
79 | { |
80 | d->chooser->editorCombo->addItem((*it)->name()); |
81 | d->elements.append((*it)->desktopEntryName()); |
82 | } |
83 | d->chooser->editorCombo->setCurrentIndex(0); |
84 | |
85 | connect(d->chooser->editorCombo,SIGNAL(activated(int)),this,SIGNAL(changed())); |
86 | |
87 | setMinimumSize(sizeHint()); |
88 | } |
89 | |
90 | EditorChooser:: ~EditorChooser() |
91 | { |
92 | delete d->chooser; |
93 | delete d; |
94 | } |
95 | |
96 | void EditorChooser::readAppSetting(const QString& postfix) |
97 | { |
98 | KConfigGroup cg(KGlobal::config(), "KTEXTEDITOR:" + postfix); |
99 | QString editor = cg.readPathEntry("editor" , QString()); |
100 | if (editor.isEmpty()) |
101 | d->chooser->editorCombo->setCurrentIndex(0); |
102 | else |
103 | { |
104 | // + 1, because item 0 is the default one. |
105 | int idx = d->elements.indexOf(editor) + 1; |
106 | d->chooser->editorCombo->setCurrentIndex(idx); |
107 | } |
108 | } |
109 | |
110 | void EditorChooser::writeAppSetting(const QString& postfix) |
111 | { |
112 | KConfigGroup cg(KGlobal::config(), "KTEXTEDITOR:" + postfix); |
113 | cg.writeEntry("DEVELOPER_INFO" ,"NEVER TRY TO USE VALUES FROM THAT GROUP, THEY ARE SUBJECT TO CHANGES" ); |
114 | cg.writePathEntry("editor" , (d->chooser->editorCombo->currentIndex()<=0) ? //< for broken installations, where editor list is empty |
115 | QString() : QString(d->elements.at(d->chooser->editorCombo->currentIndex()-1))); |
116 | } |
117 | |
118 | KTextEditor::Editor *EditorChooser::editor(const QString& postfix, |
119 | bool fallBackToKatePart) |
120 | { |
121 | // try to read the used library from the application's config |
122 | KConfigGroup cg(KGlobal::config(), "KTEXTEDITOR:" + postfix); |
123 | QString editor = cg.readPathEntry("editor" , QString()); |
124 | if (editor.isEmpty()) |
125 | { |
126 | // there is no library set in the application's config, |
127 | // fall back to KDE's system default |
128 | KConfig config("default_components" ); |
129 | editor = config.group("KTextEditor" ).readPathEntry("embeddedEditor" , "katepart" ); |
130 | } |
131 | |
132 | KService::Ptr serv = KService::serviceByDesktopName(editor); |
133 | if (serv) |
134 | { |
135 | KTextEditor::Editor *tmpEd = KTextEditor::editor(serv->library().toLatin1()); |
136 | if (tmpEd) return tmpEd; |
137 | } |
138 | if (fallBackToKatePart) |
139 | return KTextEditor::editor("katepart" ); |
140 | |
141 | return 0; |
142 | } |
143 | |
144 | // kate: space-indent on; indent-width 2; replace-tabs on; |
145 | |