summaryrefslogtreecommitdiffstats
path: root/konversation/src/alias_preferences.cpp
blob: e715a071d6eee6048b9e07c0519ca9aec0d286dd (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
/*
  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.
*/

/*
  Copyright (C) 2006 Dario Abatianni <eisfuchs@tigress.com>
  Copyright (C) 2006 John Tapsell <johnflux@gmail.com>
*/

#include "alias_preferences.h"
#include "config/preferences.h"

#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqheader.h>

#include <tdeapplication.h>
#include <kdebug.h>
#include <tdeconfig.h>
#include <tdelocale.h>
#include <klineedit.h>
#include <tdelistview.h>


Alias_Config::Alias_Config(TQWidget* parent, const char* name)
 : Alias_ConfigUI(parent, name)
{
  // reset flag to defined state (used to block signals when just selecting a new item)
  m_newItemSelected = false;

  // populate listview
  loadSettings();

  // make items react to drag & drop
  aliasListView->setSorting(-1,false);
  aliasListView->header()->setMovingEnabled(false);

  connect(aliasListView, TQT_SIGNAL(selectionChanged(TQListViewItem*)), this, TQT_SLOT(entrySelected(TQListViewItem*)));
  connect(aliasListView, TQT_SIGNAL(clicked(TQListViewItem*)), this, TQT_SLOT(entrySelected(TQListViewItem*)) );
  connect(aliasListView, TQT_SIGNAL(moved()), this, TQT_SIGNAL(modified()));

  connect(aliasInput, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(nameChanged(const TQString&)));
  connect(replacementInput, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(actionChanged(const TQString&)));

  connect(newButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(addEntry()));
  connect(removeButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(removeEntry()));
}

Alias_Config::~Alias_Config()
{
}

void Alias_Config::loadSettings()
{
    setAliasListView(Preferences::aliasList());
}

void Alias_Config::saveSettings()
{
    TQStringList newList=currentAliasList();
    Preferences::setAliasList(newList);

    // saved list is now old list, to check for changes
    m_oldAliasList=newList;
}

void Alias_Config::restorePageToDefaults()
{
    aliasListView->clear();
    setAliasListView(Preferences::defaultAliasList());
}

bool Alias_Config::hasChanged()
{
    return (currentAliasList() != m_oldAliasList);
}

void Alias_Config::setAliasListView(const TQStringList& aliasList)
{
    aliasListView->clear();

    // Insert alias items backwards to get them sorted properly
    for(int index=aliasList.count(); index!=0; index--)
    {
        TQString item=aliasList[index-1];
        new TDEListViewItem(aliasListView,item.section(' ',0,0),item.section(' ',1));
    }

    aliasListView->setSelected(aliasListView->firstChild(), true);
    // remember alias list
    m_oldAliasList=aliasList;
}

TQStringList Alias_Config::currentAliasList()
{
    TQStringList newList;

    TQListViewItem* item=aliasListView->itemAtIndex(0);
    while(item)
        {
        newList.append(item->text(0)+' '+item->text(1));
        item=item->itemBelow();
        }
    return newList;
}

// what to do when the user selects an item
void Alias_Config::entrySelected(TQListViewItem* aliasEntry)
{
    // play it safe, assume disabling all widgets first
    bool enabled = false;

    // check if there really was an item selected
    if (aliasEntry)
    {
        // remember to enable the editing widgets
        enabled = true;
        // tell the editing widgets not to emit modified() on signals now
        m_newItemSelected = true;
        // update editing widget contents
        aliasInput->setText(aliasEntry->text(0));
        replacementInput->setText(aliasEntry->text(1));
        // re-enable modified() signal on text changes in edit widgets
        m_newItemSelected = false;
    }
    // enable or disable editing widgets
    removeButton->setEnabled(enabled);
    aliasLabel->setEnabled(enabled);
    aliasInput->setEnabled(enabled);
    replacementLabel->setEnabled(enabled);
    replacementInput->setEnabled(enabled);
}

// what to do when the user change the name of a quick button
void Alias_Config::nameChanged(const TQString& newName)
{
    // get possible first selected item
    TQListViewItem* item = aliasListView->selectedItem();

    // sanity check
    if (item)
    {
        // rename item
        item->setText(0,newName);
        // tell the config system that something has changed
        if (!m_newItemSelected) emit modified();
    }
}

// what to do when the user change the action definition of a quick button
void Alias_Config::actionChanged(const TQString& newAction)
{
    // get possible first selected item
    TQListViewItem* item = aliasListView->selectedItem();

    // sanity check
    if (item)
    {
        // rename item
        item->setText(1,newAction);
        // tell the config system that something has changed
        if(!m_newItemSelected) emit modified();
    }
}

// add button pressed
void Alias_Config::addEntry()
{
    // add new item at the bottom of list view
    TDEListViewItem* newItem = new TDEListViewItem(aliasListView,aliasListView->lastChild(),i18n("New"),TQString());
    // if successful ...
    if (newItem)
    {
        // select new item and make it the current one
        aliasListView->setSelected(newItem,true);
        aliasListView->setCurrentItem(newItem);
        // set input focus on item name edit
        aliasInput->setFocus();
        // select all text to make overwriting easier
        aliasInput->selectAll();
        // tell the config system that something has changed
        emit modified();
    }
}

// remove button pressed
void Alias_Config::removeEntry()
{
    // get possible first selected item
    TQListViewItem* item = aliasListView->selectedItem();

    // sanity check
    if (item)
    {
        // get item below the current one
        TQListViewItem* nextItem = item->itemBelow();
        // if there was none, get the one above
        if(!nextItem) nextItem = item->itemAbove();

        // remove the item from the list
        delete item;

        // check if we found the next item
        if (nextItem)
        {
            // select the item and make it the current ite,
            aliasListView->setSelected(nextItem,true);
            aliasListView->setCurrentItem(nextItem);
        }
        else
        {
            // no next item found, this means the list is empty
            entrySelected(0);
        }
        // tell the config system that somethig has changed
        emit modified();
    }
}

#include "alias_preferences.moc"