summaryrefslogtreecommitdiffstats
path: root/interfaces/ktexteditor/editorchooser.cpp
blob: 60aef8d1008539d4d56ec10b09f7c6315e9eae83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <editorchooser.h>
#include <editorchooser.moc>

#include <tqcombobox.h>
#include <ktrader.h>
#include <kconfig.h>
#include <tqstringlist.h>
#include <kservice.h>
#include <klocale.h>
#include <tqlabel.h>
#include <kapplication.h>
#include <tqlayout.h>

#include "editorchooser_ui.h"

using namespace KTextEditor;

namespace KTextEditor
{
  class PrivateEditorChooser
  {
  public:
    PrivateEditorChooser()
    {
    }
    ~PrivateEditorChooser(){}
  // Data Members
  EditorChooser_UI *chooser;
  TQStringList ElementNames;
  TQStringList elements;
  };

}

EditorChooser::EditorChooser(TQWidget *parent,const char *name) :
	TQWidget (parent,name)
  {
  d = new PrivateEditorChooser ();

  // sizemanagment
  TQGridLayout *grid = new TQGridLayout( this, 1, 1 );


  d->chooser = new EditorChooser_UI (this, name);

  grid->addWidget( d->chooser, 0, 0);


	KTrader::OfferList offers = KTrader::self()->query("text/plain", "'KTextEditor/Document' in ServiceTypes");
	KConfig *config=new KConfig("default_components");
  	config->setGroup("KTextEditor");
  	TQString editor = config->readPathEntry("embeddedEditor");

        if (editor.isEmpty()) editor="katepart";

	for (KTrader::OfferList::Iterator it = offers.begin(); it != offers.end(); ++it)
	{
    		if ((*it)->desktopEntryName().contains(editor))
		{
			d->chooser->editorCombo->insertItem(i18n("System Default (%1)").arg((*it)->name()));
			break;
		}
  	}

  	for (KTrader::OfferList::Iterator it = offers.begin(); it != offers.end(); ++it)
  	{
    		d->chooser->editorCombo->insertItem((*it)->name());
		d->elements.append((*it)->desktopEntryName());
  	}
    	d->chooser->editorCombo->setCurrentItem(0);
}

EditorChooser:: ~EditorChooser(){
  delete d;
}

void EditorChooser::readAppSetting(const TQString& postfix){
	KConfig *cfg=kapp->config();
	TQString previousGroup=cfg->group();
	cfg->setGroup("KTEXTEDITOR:"+postfix);
	TQString editor=cfg->readPathEntry("editor");
	if (editor.isEmpty()) d->chooser->editorCombo->setCurrentItem(0);
	else
	{
		int idx=d->elements.findIndex(editor);
		idx=idx+1;
		d->chooser->editorCombo->setCurrentItem(idx);
	}
	cfg->setGroup(previousGroup);
}

void EditorChooser::writeAppSetting(const TQString& postfix){
	KConfig *cfg=kapp->config();
	TQString previousGroup=cfg->group();
	cfg->setGroup("KTEXTEDITOR:"+postfix);
	cfg->writeEntry("DEVELOPER_INFO","NEVER TRY TO USE VALUES FROM THAT GROUP, THEY ARE SUBJECT TO CHANGES");
	cfg->writePathEntry("editor", (d->chooser->editorCombo->currentItem()==0) ? 
		TQString::null : (*d->elements.at(d->chooser->editorCombo->currentItem()-1)));
	cfg->sync();
	cfg->setGroup(previousGroup);

}

KTextEditor::Document *EditorChooser::createDocument(TQObject *parent,const char* name, const TQString& postfix,bool fallBackToKatePart){

	KTextEditor::Document *tmpDoc=0;

	KConfig *cfg=kapp->config();
        TQString previousGroup=cfg->group();
        cfg->setGroup("KTEXTEDITOR:"+postfix);
        TQString editor=cfg->readPathEntry("editor");
	cfg->setGroup(previousGroup);
	if (editor.isEmpty())
	{
		KConfig *config=new KConfig("default_components");
  		config->setGroup("KTextEditor");
	  	editor = config->readPathEntry("embeddedEditor", "katepart");
		delete config;
	}

	KService::Ptr serv=KService::serviceByDesktopName(editor);
	if (serv)
	{
		tmpDoc=KTextEditor::createDocument(serv->library().latin1(),parent,name);
		if (tmpDoc) return tmpDoc;
	}
	if (fallBackToKatePart)
		return KTextEditor::createDocument("libkatepart",parent,name);

	return 0;
}

KTextEditor::Editor *EditorChooser::createEditor(TQWidget *tqparentWidget,TQObject *parent,const char* widgetName,
	const char* name,const TQString& postfix,bool fallBackToKatePart){

        KTextEditor::Editor *tmpEd=0;

        KConfig *cfg=kapp->config();
        TQString previousGroup=cfg->group();
        cfg->setGroup("KTEXTEDITOR:"+postfix);
        TQString editor=cfg->readPathEntry("editor");
        cfg->setGroup(previousGroup);
        if (editor.isEmpty())
        {
                KConfig *config=new KConfig("default_components");
                config->setGroup("KTextEditor");
                editor = config->readPathEntry("embeddedEditor", "katepart");
                delete config;
        }

        KService::Ptr serv=KService::serviceByDesktopName(editor);
        if (serv)
        {
                tmpEd=KTextEditor::createEditor(serv->library().latin1(),tqparentWidget,widgetName,parent,name);
                if (tmpEd) return tmpEd;
        }
        if (fallBackToKatePart)
                return KTextEditor::createEditor("libkatepart",tqparentWidget,widgetName,parent,name);

        return 0;
}