summaryrefslogtreecommitdiffstats
path: root/tdecore/kshortcutlist.cpp
blob: dd171062ee419db991f36bfb6e3cb18d27e56e9b (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <tqstring.h>
#include <tqvariant.h>

#include <kaccel.h>
#include "kaccelaction.h"
#include <tdeconfig.h>
#include <kdebug.h>
#include <kglobal.h>
#include <kglobalaccel.h>
#include <kinstance.h>
#include <kshortcut.h>
#include "kshortcutlist.h"

//---------------------------------------------------------------------
// KShortcutList
//---------------------------------------------------------------------

KShortcutList::KShortcutList()
{
}

KShortcutList::~KShortcutList()
{
}

bool KShortcutList::isGlobal( uint ) const
{
	return false;
}

int KShortcutList::index( const TQString& sName ) const
{
	uint nSize = count();
        for( uint i = 0;
             i < nSize;
             ++i )
            if( name( i ) == sName )
                return i;
	return -1;
}

int KShortcutList::index( const KKeySequence& seq ) const
{
	if( seq.isNull() )
		return -1;

	uint nSize = count();
	for( uint i = 0; i < nSize; i++ ) {
		if( shortcut(i).contains( seq ) )
			return i;
	}

	return -1;
}

const TDEInstance* KShortcutList::instance() const
{
	return 0;
}

TQVariant KShortcutList::getOther( Other, uint ) const
{
	return TQVariant();
}

bool KShortcutList::setOther( Other, uint, TQVariant )
{
	return false;
}

bool KShortcutList::readSettings( const TQString& sConfigGroup, TDEConfigBase* pConfig )
{
	kdDebug(125) << "KShortcutList::readSettings( \"" << sConfigGroup << "\", " << pConfig << " ) start" << endl;
	if( !pConfig )
		pConfig = TDEGlobal::config();
	TQString sGroup = (!sConfigGroup.isEmpty()) ? sConfigGroup : TQString("Shortcuts");

	// If the config file still has the old group name:
	// FIXME: need to rename instead? -- and don't do this if hasGroup( "Shortcuts" ).
	if( sGroup == "Shortcuts" && pConfig->hasGroup( "Keys" ) ) {
		readSettings( "Keys", pConfig );
	}

	kdDebug(125) << "\treadSettings( \"" << sGroup << "\", " << pConfig << " )" << endl;
	if( !pConfig->hasGroup( sGroup ) )
		return true;
	TDEConfigGroupSaver cgs( pConfig, sGroup );

	uint nSize = count();
	for( uint i = 0; i < nSize; i++ ) {
		if( isConfigurable(i) ) {
			TQString sEntry = pConfig->readEntry( name(i) );
			if( !sEntry.isEmpty() ) {
				if( sEntry == "none" )
					setShortcut( i, KShortcut() );
				else
					setShortcut( i, KShortcut(sEntry) );
			}
			else // default shortcut
				setShortcut( i, shortcutDefault(i) );
			kdDebug(125) << "\t" << name(i) << " = '" << sEntry << "'" << endl;
		}
	}

	kdDebug(125) << "KShortcutList::readSettings done" << endl;
	return true;
}

bool KShortcutList::writeSettings( const TQString &sConfigGroup, TDEConfigBase* pConfig, bool bWriteAll, bool bGlobal ) const
{
	kdDebug(125) << "KShortcutList::writeSettings( " << sConfigGroup << ", " << pConfig << ", " << bWriteAll << ", " << bGlobal << " )" << endl;
	if( !pConfig )
		pConfig = TDEGlobal::config();

	TQString sGroup = (!sConfigGroup.isEmpty()) ? sConfigGroup : TQString("Shortcuts");

	// If it has the deprecated group [Keys], remove it
	if( pConfig->hasGroup( "Keys" ) )
		pConfig->deleteGroup( "Keys", true );

	TDEConfigGroupSaver cs( pConfig, sGroup );

	uint nSize = count();
	for( uint i = 0; i < nSize; i++ ) {
		if( isConfigurable(i) ) {
			const TQString& sName = name(i);
			bool bConfigHasAction = !pConfig->readEntry( sName ).isEmpty();
			bool bSameAsDefault = (shortcut(i) == shortcutDefault(i));
			// If we're using a global config or this setting
			//  differs from the default, then we want to write.
			if( bWriteAll || !bSameAsDefault ) {
				TQString s = shortcut(i).toStringInternal();
				if( s.isEmpty() )
					s = "none";
				kdDebug(125) << "\twriting " << sName << " = " << s << endl;
				pConfig->writeEntry( sName, s, true, bGlobal );
			}
			// Otherwise, this key is the same as default
			//  but exists in config file.  Remove it.
			else if( bConfigHasAction ) {
				kdDebug(125) << "\tremoving " << sName << " because == default" << endl;
				pConfig->deleteEntry( sName, false, bGlobal );
			}
		}
	}

	pConfig->sync();
	return true;
}

//---------------------------------------------------------------------
// KAccelShortcutList
//---------------------------------------------------------------------

class KAccelShortcutListPrivate
{
	public:
		TQString m_configGroup;
};

KAccelShortcutList::KAccelShortcutList( KAccel* pAccel )
: m_actions( pAccel->actions() )
{
	d=new KAccelShortcutListPrivate;
	m_bGlobal = false;
	d->m_configGroup=pAccel->configGroup();
}

KAccelShortcutList::KAccelShortcutList( TDEGlobalAccel* pAccel )
: m_actions( pAccel->actions() )
{
	d=new KAccelShortcutListPrivate;
	m_bGlobal = true;
	d->m_configGroup=pAccel->configGroup();
}

KAccelShortcutList::KAccelShortcutList( KAccelActions& actions, bool bGlobal )
: m_actions( actions )
{
	d=new KAccelShortcutListPrivate;
	m_bGlobal = bGlobal;
}


KAccelShortcutList::~KAccelShortcutList()
	{  delete d;}
uint KAccelShortcutList::count() const
	{ return m_actions.count(); }
TQString KAccelShortcutList::name( uint i ) const
	{ return m_actions.actionPtr(i)->name(); }
TQString KAccelShortcutList::label( uint i ) const
	{ return m_actions.actionPtr(i)->label(); }
TQString KAccelShortcutList::whatsThis( uint i ) const
	{ return m_actions.actionPtr(i)->whatsThis(); }
const KShortcut& KAccelShortcutList::shortcut( uint i ) const
	{ return m_actions.actionPtr(i)->shortcut(); }
const KShortcut& KAccelShortcutList::shortcutDefault( uint i ) const
	{ return m_actions.actionPtr(i)->shortcutDefault(); }
bool KAccelShortcutList::isConfigurable( uint i ) const
	{ return m_actions.actionPtr(i)->isConfigurable(); }
bool KAccelShortcutList::setShortcut( uint i, const KShortcut& cut )
	{ return m_actions.actionPtr(i)->setShortcut( cut ); }
TQVariant KAccelShortcutList::getOther( Other, uint ) const
	{ return TQVariant(); }
bool KAccelShortcutList::isGlobal( uint ) const
	{ return m_bGlobal; }
bool KAccelShortcutList::setOther( Other, uint, TQVariant )
	{ return false; }
bool KAccelShortcutList::save() const
	{ return writeSettings( d->m_configGroup ); }

void KShortcutList::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }

void KAccelShortcutList::virtual_hook( int id, void* data )
{ KShortcutList::virtual_hook( id, data ); }

void KStdAccel::ShortcutList::virtual_hook( int id, void* data )
{ KShortcutList::virtual_hook( id, data ); }