summaryrefslogtreecommitdiffstats
path: root/twin/kcmtwin/twinrules/ruleslist.cpp
blob: a9238c178ac337d2d2a6976f77a38b2a471f385d (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
/*
 * Copyright (c) 2004 Lubos Lunak <l.lunak@kde.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "ruleslist.h"

#include <tdelistbox.h>
#include <kpushbutton.h>
#include <assert.h>
#include <kdebug.h>
#include <tdeconfig.h>

#include "ruleswidget.h"

namespace KWinInternal
{

KCMRulesList::KCMRulesList( TQWidget* parent, const char* name )
: KCMRulesListBase( parent, name )
    {
    // connect both current/selected, so that current==selected (stupid TQListBox :( )
    connect( rules_listbox, TQ_SIGNAL( currentChanged( TQListBoxItem* )),
        TQ_SLOT( activeChanged( TQListBoxItem*)));
    connect( rules_listbox, TQ_SIGNAL( selectionChanged( TQListBoxItem* )),
        TQ_SLOT( activeChanged( TQListBoxItem*)));
    connect( new_button, TQ_SIGNAL( clicked()),
        TQ_SLOT( newClicked()));
    connect( modify_button, TQ_SIGNAL( clicked()),
        TQ_SLOT( modifyClicked()));
    connect( delete_button, TQ_SIGNAL( clicked()),
        TQ_SLOT( deleteClicked()));
    connect( moveup_button, TQ_SIGNAL( clicked()),
        TQ_SLOT( moveupClicked()));
    connect( movedown_button, TQ_SIGNAL( clicked()),
        TQ_SLOT( movedownClicked()));
    connect( rules_listbox, TQ_SIGNAL( doubleClicked ( TQListBoxItem * ) ),
            TQ_SLOT( modifyClicked()));
    load();
    }

KCMRulesList::~KCMRulesList()
    {
    for( TQValueVector< Rules* >::Iterator it = rules.begin();
         it != rules.end();
         ++it )
        delete *it;
    rules.clear();
    }

void KCMRulesList::activeChanged( TQListBoxItem* item )
    {
    if( item != NULL )
        rules_listbox->setSelected( item, true ); // make current==selected
    modify_button->setEnabled( item != NULL );
    delete_button->setEnabled( item != NULL );
    moveup_button->setEnabled( item != NULL && item->prev() != NULL );
    movedown_button->setEnabled( item != NULL && item->next() != NULL );
    }

void KCMRulesList::newClicked()
    {
    RulesDialog dlg;
    Rules* rule = dlg.edit( NULL, 0, false );
    if( rule == NULL )
        return;
    int pos = rules_listbox->currentItem() + 1;
    rules_listbox->insertItem( rule->description, pos );
    rules_listbox->setSelected( pos, true );
    rules.insert( rules.begin() + pos, rule );
    emit changed( true );
    }

void KCMRulesList::modifyClicked()
    {
    int pos = rules_listbox->currentItem();
    if ( pos == -1 )
        return;
    RulesDialog dlg;
    Rules* rule = dlg.edit( rules[ pos ], 0, false );
    if( rule == rules[ pos ] )
        return;
    delete rules[ pos ];
    rules[ pos ] = rule;
    rules_listbox->changeItem( rule->description, pos );
    emit changed( true );
    }

void KCMRulesList::deleteClicked()
    {
    int pos = rules_listbox->currentItem();
    assert( pos != -1 );
    rules_listbox->removeItem( pos );
    rules.erase( rules.begin() + pos );
    emit changed( true );
    }

void KCMRulesList::moveupClicked()
    {
    int pos = rules_listbox->currentItem();
    assert( pos != -1 );
    if( pos > 0 )
        {
        TQString txt = rules_listbox->text( pos );
        rules_listbox->removeItem( pos );
        rules_listbox->insertItem( txt, pos - 1 );
        rules_listbox->setSelected( pos - 1, true );
        Rules* rule = rules[ pos ];
        rules[ pos ] = rules[ pos - 1 ];
        rules[ pos - 1 ] = rule;
        }
    emit changed( true );
    }

void KCMRulesList::movedownClicked()
    {
    int pos = rules_listbox->currentItem();
    assert( pos != -1 );
    if( pos < int( rules_listbox->count()) - 1 )
        {
        TQString txt = rules_listbox->text( pos );
        rules_listbox->removeItem( pos );
        rules_listbox->insertItem( txt, pos + 1 );
        rules_listbox->setSelected( pos + 1, true );
        Rules* rule = rules[ pos ];
        rules[ pos ] = rules[ pos + 1 ];
        rules[ pos + 1 ] = rule;
        }
    emit changed( true );
    }

void KCMRulesList::load()
    {
    rules_listbox->clear();
    for( TQValueVector< Rules* >::Iterator it = rules.begin();
         it != rules.end();
         ++it )
        delete *it;
    rules.clear();
    TDEConfig cfg( "twinrulesrc", true );
    cfg.setGroup( "General" );
    int count = cfg.readNumEntry( "count" );
    rules.reserve( count );
    for( int i = 1;
         i <= count;
         ++i )
        {
        cfg.setGroup( TQString::number( i ));
        Rules* rule = new Rules( cfg );
        rules.append( rule );
        rules_listbox->insertItem( rule->description );
        }
    if( rules.count() > 0 )
        rules_listbox->setSelected( 0, true );
    else
        activeChanged( NULL );
    }

void KCMRulesList::save()
    {
    TDEConfig cfg( "twinrulesrc" );
    TQStringList groups = cfg.groupList();
    for( TQStringList::ConstIterator it = groups.begin();
         it != groups.end();
         ++it )
        cfg.deleteGroup( *it );
    cfg.setGroup( "General" );
    cfg.writeEntry( "count", rules.count());
    int i = 1;
    for( TQValueVector< Rules* >::ConstIterator it = rules.begin();
         it != rules.end();
         ++it )
        {
        cfg.setGroup( TQString::number( i ));
        (*it)->write( cfg );
        ++i;
        }
    }

void KCMRulesList::defaults()
    {
    load();
    }

} // namespace

#include "ruleslist.moc"