summaryrefslogtreecommitdiffstats
path: root/khotkeys/kcontrol/menuedit.cpp
blob: 990cd4ff51fd90c54ec47083b2bdbda9e4347c27 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/****************************************************************************

 KHotKeys

 Copyright (C) 1999-2001 Lubos Lunak <l.lunak@kde.org>

 Distributed under the terms of the GNU General Public License version 2.

****************************************************************************/

// BEWARE ! unbelievably messy code

#define _MENUEDIT_CPP_

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "menuedit.h"

#include <kglobal.h>
#include <klocale.h>
#include <kaccel.h>
#include <kapplication.h>
#include <dcopclient.h>
#include <tqlabel.h>
#include <tqlineedit.h>
#include <tqlayout.h>
#include <kkeydialog.h>

#include <settings.h>
#include <action_data.h>

namespace KHotKeys
{

static TQObject* owner = NULL;

void khotkeys_init()
    {
    // I hope this works
    TDEGlobal::locale()->insertCatalogue("khotkeys");
    // CHECKME hack
    assert( owner == NULL );
    owner = new TQObject;
    init_global_data( false, owner );
    }

void khotkeys_cleanup()
    {
    delete owner;
    owner = NULL;
    }

Menuentry_shortcut_action_data* khotkeys_get_menu_entry_internal2(
    const Action_data_group* data_P, const TQString& entry_P )
    {
    if( !data_P->enabled( false ))
        return NULL;
    for( Action_data_group::Iterator it = data_P->first_child();
         it;
         ++it )
        {
        if( !(*it)->enabled( true ))
            continue;
        if( Menuentry_shortcut_action_data* entry
            = dynamic_cast< Menuentry_shortcut_action_data* >( *it ))
            {
               KService::Ptr service = entry->action() ? entry->action()->service() : KService::Ptr(0);
               if ( service && (service->storageId() == entry_P) )
                    return entry;
            }
        if( Action_data_group* group = dynamic_cast< Action_data_group* >( *it ))
            {
            Menuentry_shortcut_action_data* data
                = khotkeys_get_menu_entry_internal2( group, entry_P );
            if( data != NULL )
                return data;
            }
        }
    return NULL;
    }

Action_data_group* khotkeys_get_menu_root( Action_data_group* data_P )
    {
    for( Action_data_group::Iterator it = data_P->first_child();
         it;
         ++it )
        if( Action_data_group* group = dynamic_cast< Action_data_group* >( *it ))
            {
            if( group->system_group() == Action_data_group::SYSTEM_MENUENTRIES )
                return group;
            }
    return new Action_data_group( data_P, i18n( MENU_EDITOR_ENTRIES_GROUP_NAME ),
        i18n( "These entries were created using Menu Editor." ), new Condition_list( "", NULL ), // CHECKME tenhle condition list
        Action_data_group::SYSTEM_MENUENTRIES, true );
    }

Menuentry_shortcut_action_data* khotkeys_get_menu_entry_internal( Action_data_group* data_P,
    const TQString& entry_P )
    {
    return khotkeys_get_menu_entry_internal2( khotkeys_get_menu_root( data_P ), entry_P );
    }

TQString khotkeys_get_menu_shortcut( Menuentry_shortcut_action_data* data_P )
    {
    if( data_P->trigger() != NULL )
        return data_P->trigger()->shortcut().toString();
    return "";
    }

void khotkeys_get_all_shortcuts_internal(const Action_data_group* data_P, TQStringList &result)
    {
    if( !data_P->enabled( false ))
        return;
    for( Action_data_group::Iterator it = data_P->first_child();
         it;
         ++it )
        {
        if( !(*it)->enabled( true ))
            continue;
        if( Menuentry_shortcut_action_data* entry
            = dynamic_cast< Menuentry_shortcut_action_data* >( *it ))
            {
               if (entry->trigger() && !entry->trigger()->shortcut().isNull())
                    result.append(entry->trigger()->shortcut().toString());
            }
        if( Action_data_group* group = dynamic_cast< Action_data_group* >( *it ))
            {
               khotkeys_get_all_shortcuts_internal( group, result );
            }
        }
    }


TQStringList khotkeys_get_all_shortcuts( )
    {
    TQStringList result;
    Settings settings;
    settings.read_settings( true );

    khotkeys_get_all_shortcuts_internal(settings.actions, result);

    return result;
    }


KService::Ptr khotkeys_find_menu_entry_internal(const Action_data_group* data_P, const TQString &shortcut_P)
    {
    if( !data_P->enabled( false ))
        return 0;
    for( Action_data_group::Iterator it = data_P->first_child();
         it;
         ++it )
        {
        if( !(*it)->enabled( true ))
            continue;
        if( Menuentry_shortcut_action_data* entry
            = dynamic_cast< Menuentry_shortcut_action_data* >( *it ))
            {
               if (entry->trigger() &&
                   entry->trigger()->shortcut().toString() == shortcut_P)
               {
                  if (entry->action())
                     return entry->action()->service();
                  return 0;
               }
            }
        if( Action_data_group* group = dynamic_cast< Action_data_group* >( *it ))
            {
               KService::Ptr result = khotkeys_find_menu_entry_internal( group, shortcut_P );
               if (result)
                  return result;
            }
        }
        return 0;
    }


KService::Ptr khotkeys_find_menu_entry( const TQString& shortcut_P )
    {
    Settings settings;
    settings.read_settings( true );

    return khotkeys_find_menu_entry_internal(settings.actions, shortcut_P);
    }


void khotkeys_send_reread_config()
    {
    TQByteArray data;
    if( !kapp->dcopClient()->isAttached())
        kapp->dcopClient()->attach();
    if( !kapp->dcopClient()->isApplicationRegistered( "khotkeys" ))
        {
        kdDebug( 1217 ) << "launching new khotkeys daemon" << endl;
        TDEApplication::tdeinitExec( "khotkeys" );
        }
    else
        {
        TQByteArray data;
        kapp->dcopClient()->send( "khotkeys*", "khotkeys", "reread_configuration()", data );
        kdDebug( 1217 ) << "telling khotkeys daemon to reread configuration" << endl;
        }
    }

TQString khotkeys_get_menu_entry_shortcut( const TQString& entry_P )
    {
    Settings settings;
    settings.read_settings( true );
    Menuentry_shortcut_action_data* entry
        = khotkeys_get_menu_entry_internal( settings.actions, entry_P );
    if( entry == NULL )
        {
        delete settings.actions;
        return "";
        }
    TQString shortcut = khotkeys_get_menu_shortcut( entry );
    delete settings.actions;
    return shortcut;
    }

bool khotkeys_menu_entry_moved( const TQString& new_P, const TQString& old_P )
    {
    Settings settings;
    settings.read_settings( true );
    Menuentry_shortcut_action_data* entry
        = khotkeys_get_menu_entry_internal( settings.actions, old_P );
    if( entry == NULL )
        {
        delete settings.actions;
        return false;
        }
    Action_data_group* parent = entry->parent();
    TQString new_name = new_P;
    if( entry->name().startsWith( i18n( "TDE Menu - " )))
        new_name = i18n( "TDE Menu - " ) + new_P;
    Menuentry_shortcut_action_data* new_entry = new Menuentry_shortcut_action_data( parent,
        new_name, entry->comment(), entry->enabled( true ));
    new_entry->set_trigger( entry->trigger()->copy( new_entry ));
    new_entry->set_action( new Menuentry_action( new_entry, new_P ));
    delete entry;
    settings.write_settings();
    delete settings.actions;
    khotkeys_send_reread_config();
    return true;
    }

void khotkeys_menu_entry_deleted( const TQString& entry_P )
    {
    Settings settings;
    settings.read_settings( true );
    Menuentry_shortcut_action_data* entry
        = khotkeys_get_menu_entry_internal( settings.actions, entry_P );
    if( entry == NULL )
        {
        delete settings.actions;
        return;
        }
    delete entry;
    settings.write_settings();
    delete settings.actions;
    khotkeys_send_reread_config();
    }

TQString khotkeys_change_menu_entry_shortcut( const TQString& entry_P,
    const TQString& shortcut_P )
    {
    Settings settings;
    settings.read_settings( true );
    Menuentry_shortcut_action_data* entry
        = khotkeys_get_menu_entry_internal( settings.actions, entry_P );
    bool new_entry = ( entry == NULL );
    if( new_entry )
        {
        entry = new Menuentry_shortcut_action_data( NULL, i18n( "TDE Menu - " ) + entry_P,
            "" );
        entry->set_action( new Menuentry_action( entry, entry_P ));
        }
    else
        {
        // erase the trigger, i.e. replace with a copy with no trigger and no parent yet
        Menuentry_shortcut_action_data* entry_tmp = new Menuentry_shortcut_action_data( NULL,
            entry->name(), entry->comment(), entry->enabled( false ));
        entry_tmp->set_action( new Menuentry_action( entry_tmp, entry_P ));
        delete entry;
        entry = entry_tmp;
        }
    TQString shortcut = "";
    // make sure the shortcut is valid
    shortcut = (KShortcut( shortcut_P )).toStringInternal();
    if( !shortcut.isEmpty())
        entry->set_trigger( new Shortcut_trigger( entry, KShortcut( shortcut )));
    if( shortcut.isEmpty())
        {
        delete entry;
        if( !new_entry ) // remove from config file
            {
            settings.write_settings();
            khotkeys_send_reread_config();
            }
        delete settings.actions;
        return "";
        }
    entry->reparent( khotkeys_get_menu_root( settings.actions ));
    settings.daemon_disabled = false; // #91782
    settings.write_settings();
    khotkeys_send_reread_config();
    return shortcut;
    }

// CHECKME nejaka forma kontroly, ze tahle kl. kombinace neni pouzita pro jiny menuentry ?

} // namespace KHotKeys

//
// exported functions
//

void khotkeys_init()
    {
    KHotKeys::khotkeys_init();
    }

void khotkeys_cleanup()
    {
    KHotKeys::khotkeys_cleanup();
    }

TQString khotkeys_get_menu_entry_shortcut( const TQString& entry_P )
    {
    return KHotKeys::khotkeys_get_menu_entry_shortcut( entry_P );
    }

bool khotkeys_menu_entry_moved( const TQString& new_P, const TQString& old_P )
    {
    return KHotKeys::khotkeys_menu_entry_moved( new_P, old_P );
    }

void khotkeys_menu_entry_deleted( const TQString& entry_P )
    {
    KHotKeys::khotkeys_menu_entry_deleted( entry_P );
    }

TQString khotkeys_change_menu_entry_shortcut( const TQString& entry_P,
    const TQString& shortcut_P )
    {
    return KHotKeys::khotkeys_change_menu_entry_shortcut( entry_P, shortcut_P );
    }

TQStringList khotkeys_get_all_shortcuts( )
    {
    return KHotKeys::khotkeys_get_all_shortcuts();
    }

KService::Ptr khotkeys_find_menu_entry( const TQString& shortcut_P )
    {
    return KHotKeys::khotkeys_find_menu_entry( shortcut_P );
    }