summaryrefslogtreecommitdiffstats
path: root/editors/editor-chooser/editorchooser_widget.cpp
blob: 6c9f7071129b100f665293c852bd04d02d7746aa (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
#include <qcombobox.h>
#include <qbuttongroup.h>
#include <qradiobutton.h>

#include <kapplication.h>
#include <kdeversion.h>
#include <kservice.h>
#include <kdebug.h>
#include <kconfig.h>


#include "editorchooser_widget.h"


EditorChooserWidget::EditorChooserWidget(QWidget *parent, const char *name)
        : EditChooser(parent, name)
{
    // ask the trader which editors he has to offer
    m_offers = KTrader::self()->query("text/plain", "'KTextEditor/Document' in ServiceTypes");

	// remove the vim-part, it's known to crash
	KTrader::OfferList::Iterator it = m_offers.begin();
	while( it != m_offers.end() )
	{
		if ( ((*it)->desktopEntryName() == "vimpart")
		    || ((*it)->desktopEntryName() == "qeditor_part") )
		{
			m_offers.remove( it );
			break;
		}
		++it;
	}

	load();
	slotEditPartChanged(QString());
}


void EditorChooserWidget::load()
{
    EditorPart->clear();

    // find the editor to use
    KConfig *config = kapp->config();
    config->setGroup("Editor");
    QString editor = config->readPathEntry("EmbeddedKTextEditor");

    // add the entries to the listview
    KTrader::OfferList::Iterator it;
    int index=-1, current=0;
    for (it = m_offers.begin(); it != m_offers.end(); ++it)
    {
        EditorPart->insertItem((*it)->name());
        if ( (*it)->desktopEntryName() == editor )
            index = current;
        ++current;
    }

    if (index >=0)
        EditorPart->setCurrentItem(index);

	QString dirtyAction = config->readEntry( "DirtyAction" );

	if ( dirtyAction == "reload" )
	{
		reload->setChecked( true );
	}
	else if ( dirtyAction == "alert" )
	{
		alert->setChecked( true );
	}
	else
	{
		nothing->setChecked( true );
	}
}


void EditorChooserWidget::save()
{
    KConfig *config = kapp->config();
    config->setGroup("Editor");

    KTrader::OfferList::Iterator it;
    for (it = m_offers.begin(); it != m_offers.end(); ++it)
        if ( EditorPart->currentText() == (*it)->name() )
        {
            config->writePathEntry("EmbeddedKTextEditor", (*it)->desktopEntryName());
        }

	if ( reload->isChecked() )
	{
		config->writeEntry( "DirtyAction", "reload" );
	}
	else if ( alert->isChecked() )
	{
		config->writeEntry( "DirtyAction", "alert" );
	}
	else
	{
		config->writeEntry( "DirtyAction", "nothing" );
	}

    config->sync();
}


void EditorChooserWidget::accept()
{
    save();
}

void EditorChooserWidget::slotEditPartChanged( const QString & )
{
	KTrader::OfferList::Iterator it;
	for (it = m_offers.begin(); it != m_offers.end(); ++it)
	{
		if ( EditorPart->currentText() == (*it)->name() )
		{
			external_changes_group->setEnabled( (*it)->desktopEntryName() == "katepart" );
			return;
		}
	}
	external_changes_group->setEnabled( false );
}


#include "editorchooser_widget.moc"