summaryrefslogtreecommitdiffstats
path: root/ksig/ksig.cpp
blob: 498b4a5c56a4009fda9ae51be3f88cb4bd0a9818 (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
/***************************************************************************
                          ksig.cpp  -  description
                             -------------------
    begin                : Tue Jul  9 23:14:22 EDT 2002
    copyright            : (C) 2002 by Scott Wheeler
    email                : wheeler@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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kapplication.h>
#include <kstdaction.h>
#include <tdeaction.h>
#include <tdeconfig.h>
#include <kglobalsettings.h>
#include <kmessagebox.h>
#include <kstatusbar.h>
#include <ktextedit.h>
#include <tdelistviewsearchline.h>
#include <klocale.h>

#include <tqsplitter.h>

#include "ksig.h"
#include "siglistviewitem.h"
#include "standardtext.h"

////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

KSig::KSig(TQWidget *parent, const char *name) : TDEMainWindow(parent, name),
						changed(false)
{
    setupActions();
    setupLayout();
    setupSearchLine();

    if (!initialGeometrySet())
        resize(640,480);
    setupGUI(ToolBar | Keys | StatusBar | Create);
    setAutoSaveSettings();

    readConfig();
    loadData();
}

KSig::~KSig()
{

}

////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

void KSig::setupActions()
{
    KStdAction::openNew(TQT_TQOBJECT(this), TQT_SLOT(add()), actionCollection());
    new TDEAction(i18n("Remove"), "editdelete", 0, TQT_TQOBJECT(this), TQT_SLOT(remove()), actionCollection(), "remove");
    KStdAction::save(TQT_TQOBJECT(this), TQT_SLOT(save()), actionCollection());
    KStdAction::quit(TQT_TQOBJECT(this), TQT_SLOT(quit()), actionCollection());

    KStdAction::cut(TQT_TQOBJECT(kapp), TQT_SLOT(cut()), actionCollection());
    KStdAction::copy(TQT_TQOBJECT(kapp), TQT_SLOT(copy()), actionCollection());
    KStdAction::paste(TQT_TQOBJECT(kapp), TQT_SLOT(paste()), actionCollection());
    new TDEAction(i18n("C&lear"), "editclear", 0, TQT_TQOBJECT(kapp), TQT_SLOT(clear()), actionCollection(), "clear");

    new TDEAction(i18n("Edit Standard Header"), 0, 0, TQT_TQOBJECT(this), TQT_SLOT(editHeader()), actionCollection(), "editHeader");
    new TDEAction(i18n("Edit Standard Footer"), 0, 0, TQT_TQOBJECT(this), TQT_SLOT(editFooter()), actionCollection(), "editFooter");
}

void KSig::setupLayout()
{
    splitter = new TQSplitter(Qt::Vertical, this);

    setCentralWidget(splitter);

    sigList = SigListView::instance(splitter, "sigList");

    sigEdit = new KTextEdit(splitter, "sigEdit");
    sigEdit->setTextFormat(TQt::PlainText);
    sigEdit->setFont(TDEGlobalSettings::fixedFont());
    sigEdit->setEnabled(false);
    sigEdit->setCheckSpellingEnabled(true);

    statusBar()->insertItem(i18n(" Line: %1 ").arg(0), LineNumber, 0, true);
    statusBar()->insertItem(i18n(" Col: %1 ").arg(0), ColumnNumber, 0, true);
    statusBar()->show();
    
    updateListLock = false;
    connect(sigEdit, TQT_SIGNAL(textChanged()), TQT_TQOBJECT(this), TQT_SLOT(updateList()));
    connect(sigList, TQT_SIGNAL(selectionChanged()), TQT_TQOBJECT(this), TQT_SLOT(updateEdit()));
    connect(sigEdit, TQT_SIGNAL(cursorPositionChanged(int,int)), TQT_TQOBJECT(this), TQT_SLOT(updateStatus(int,int)));
}

void KSig::setupSearchLine()
{
    TDEListViewSearchLineWidget *searchWidget = new TDEListViewSearchLineWidget(sigList, this);
    new KWidgetAction(searchWidget, i18n("Search"), TDEShortcut(), 0, 0, actionCollection(), "search");
}

void KSig::loadData()
{
    if(sigList->childCount() > 0) {
        sigList->setCurrentItem(sigList->firstChild());
        sigList->setSelected(sigList->firstChild(), true);

        sigEdit->setEnabled(true);
    }
    else {
        updateListLock = true;
        sigEdit->setText(i18n("To get started, first create a new signature by selecting "
                              "\"New\" above. You will then be able to edit and save "
                              "your collection of signatures."));
        updateListLock = false;
    }
    setDataChanged(false);
}

void KSig::readConfig()
{
    setAutoSaveSettings();
    TDEConfig *config = TDEGlobal::config();
    {
	TDEConfigGroupSaver saver(config, "Settings");
	if(splitter) {
	    TQValueList<int> sizes;

	    sizes.append(config->readNumEntry("ListSize", -1));
	    sizes.append(config->readNumEntry("EditSize", -1));

	    if(sizes[0] > 0 && sizes[1] > 0)
		splitter->setSizes(sizes);
	}

	header = config->readEntry("Header");
	footer = config->readEntry("Footer");
    }
}

void KSig::writeConfig()
{
    TDEConfig *config = TDEGlobal::config();
    {
	TDEConfigGroupSaver saver(config, "Settings");
	if(splitter) {
	    TQValueList<int> sizes = splitter->sizes();

	    config->writeEntry("ListSize", sizes[0]);
	    config->writeEntry("EditSize", sizes[1]);
	}

	config->writeEntry("Header", header);
	config->writeEntry("Footer", footer);
    }
    config->sync();
}

bool KSig::queryClose()
{
    if(changed) {
	int saveChanges = KMessageBox::questionYesNoCancel(this, i18n("Do you want to save your changes before exiting?"),TQString(),KStdGuiItem::save(),KStdGuiItem::discard());
	if(saveChanges == KMessageBox::Cancel)
	    return(false);
	else if(saveChanges == KMessageBox::Yes)
	    save();
    }

    writeConfig();
    return(true);
}

////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////

void KSig::add()
{
    setDataChanged();
    SigListViewItem *sigItem = sigList->createItem();
    sigList->setCurrentItem(sigItem);

    sigEdit->setEnabled(true);
    sigEdit->setFocus();
    sigEdit->clear();
}

void KSig::remove()
{
    setDataChanged();
    sigEdit->clear();
    delete(sigList->currentItem());
}

void KSig::save()
{
    sigList->save();
    setDataChanged(false);
}

void KSig::setDataChanged(bool value)
{
    changed = value;

    if(changed)
	setCaption(kapp->makeStdCaption(TQString(), true, true));
    else
	setCaption(kapp->makeStdCaption(TQString(), true, false));
}

void KSig::editHeader()
{
    StandardText *t = new StandardText(0, 0, true);
    t->setLabel(i18n("Standard signature header:"));
    t->setText(header);
    
    connect(t, TQT_SIGNAL(textUpdated(const TQString &)), TQT_TQOBJECT(this), TQT_SLOT(setHeader(const TQString &)));
    
    t->exec();
}

void KSig::editFooter()
{
    StandardText *t = new StandardText(0, 0, true);
    t->setLabel(i18n("Standard signature footer:"));
    t->setText(footer);
    
    connect(t, TQT_SIGNAL(textUpdated(const TQString &)), TQT_TQOBJECT(this), TQT_SLOT(setFooter(const TQString &)));
    
    t->exec();
}

void KSig::updateList()
{
    if(!updateListLock) {
	setDataChanged();
	SigListViewItem *item = sigList->currentItem();
	if(item)
	    item->setText(sigEdit->text());
    }
}

void KSig::updateEdit()
{
    updateListLock = true;

    SigListViewItem *item = sigList->currentItem();
    if(item)
	sigEdit->setText(item->text());

    updateListLock = false;
}

void KSig::updateStatus(int line, int column)
{
    statusBar()->changeItem(i18n(" Line: %1 ").arg(line + 1), LineNumber);
    statusBar()->changeItem(i18n(" Col: %1 ").arg(column + 1), ColumnNumber);
}

void KSig::quit()
{
    close();
}

#include "ksig.moc"