summaryrefslogtreecommitdiffstats
path: root/kopete/plugins/highlight/highlightconfig.cpp
blob: d6e04d7f1cd97cf149d9eb406c8334fc1982a00e (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
/*
    highlightconfig.cpp

    Copyright (c) 2003      by Olivier Goffart       <ogoffart @ kde.org>
    Copyright (c) 2003      by Matt Rogers           <matt@matt.rogers.name>

    Kopete    (c) 2002-2003 by the Kopete developers <kopete-devel@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 <tqfile.h>
#include <tqstylesheet.h>
#include <tqregexp.h>
#include <tqdir.h>
#include <tqdom.h>

#include <ksavefile.h>
#include <kstandarddirs.h>
#include <klocale.h>

#include "filter.h"
#include "highlightconfig.h"


HighlightConfig::HighlightConfig()
{
	load();
	m_filters.setAutoDelete(true);
}

HighlightConfig::~HighlightConfig()
{
	m_filters.clear();
}

void HighlightConfig::removeFilter(Filter *f)
{
    //m_filters is "autodelete (true) so when we use remove(...) it deleted f
    //so don't use (delete (f) after otherwise ot crash
	m_filters.remove(f);
}

void HighlightConfig::appendFilter(Filter *f)
{
	m_filters.append(f);
}

TQPtrList<Filter> HighlightConfig::filters() const
{
	return m_filters;
}

Filter* HighlightConfig::newFilter()
{
	Filter *filtre=new Filter();
	filtre->caseSensitive=false;
	filtre->isRegExp=false;
	filtre->setImportance=false;
	filtre->importance=1;
	filtre->setBG=false;
	filtre->setFG=false;
	filtre->playSound=false;
	filtre->raiseView=false;
	filtre->displayName=i18n("-New filter-");
	m_filters.append(filtre);
	return filtre;
}

void HighlightConfig::load()
{
	m_filters.clear(); //clear filters

	TQString filename = locateLocal( "appdata", TQString::fromLatin1( "highlight.xml" ) );
	if( filename.isEmpty() )
		return ;

	TQDomDocument filterList( TQString::fromLatin1( "highlight-plugin" ) );

	TQFile filterListFile( filename );
	filterListFile.open( IO_ReadOnly );
	filterList.setContent( &filterListFile );

	TQDomElement list = filterList.documentElement();

	TQDomNode node = list.firstChild();
	while( !node.isNull() )
	{
		TQDomElement element = node.toElement();
		if( !element.isNull() )
		{
//			if( element.tagName() == TQString::fromLatin1("filter")
//			{
				Filter *filtre=newFilter();
				TQDomNode filterNode = node.firstChild();

				while( !filterNode.isNull() )
				{
					TQDomElement filterElement = filterNode.toElement();
					if( !filterElement.isNull() )
					{
						if( filterElement.tagName() == TQString::fromLatin1( "display-name" ) )
						{
							filtre->displayName = filterElement.text();
						}
						else if( filterElement.tagName() == TQString::fromLatin1( "search" ) )
						{
							filtre->search = filterElement.text();

							filtre->caseSensitive= ( filterElement.attribute( TQString::fromLatin1( "caseSensitive" ), TQString::fromLatin1( "1" ) ) == TQString::fromLatin1( "1" ) );
							filtre->isRegExp= ( filterElement.attribute( TQString::fromLatin1( "regExp" ), TQString::fromLatin1( "0" ) ) == TQString::fromLatin1( "1" ) );
						}
						else if( filterElement.tagName() == TQString::fromLatin1( "FG" ) )
						{
							filtre->FG = filterElement.text();
							filtre->setFG= ( filterElement.attribute( TQString::fromLatin1( "set" ), TQString::fromLatin1( "0" ) ) == TQString::fromLatin1( "1" ) );
						}
						else if( filterElement.tagName() == TQString::fromLatin1( "BG" ) )
						{
							filtre->BG = filterElement.text();
							filtre->setBG= ( filterElement.attribute( TQString::fromLatin1( "set" ), TQString::fromLatin1( "0" ) ) == TQString::fromLatin1( "1" ) );
						}
						else if( filterElement.tagName() == TQString::fromLatin1( "importance" ) )
						{
							filtre->importance = filterElement.text().toUInt();
							filtre->setImportance= ( filterElement.attribute( TQString::fromLatin1( "set" ), TQString::fromLatin1( "0" ) ) == TQString::fromLatin1( "1" ) );
						}
						else if( filterElement.tagName() == TQString::fromLatin1( "sound" ) )
						{
							filtre->soundFN = filterElement.text();
							filtre->playSound = ( filterElement.attribute( TQString::fromLatin1( "set" ), TQString::fromLatin1( "0" ) ) == TQString::fromLatin1( "1" ) );
						}
						else if( filterElement.tagName() == TQString::fromLatin1( "raise" ) )
						{
							filtre->raiseView = ( filterElement.attribute( TQString::fromLatin1( "set" ), TQString::fromLatin1( "0" ) ) == TQString::fromLatin1( "1" ) );
						}
					}
					filterNode = filterNode.nextSibling();
				}
//			}
		}
		node = node.nextSibling();
	}
	filterListFile.close();
}

void HighlightConfig::save()
{

	TQString fileName = locateLocal( "appdata", TQString::fromLatin1( "highlight.xml" ) );

	KSaveFile file( fileName );
	if( file.status() == 0 )
	{
		TQTextStream *stream = file.textStream();
		stream->setEncoding( TQTextStream::UnicodeUTF8 );

		TQString xml = TQString::fromLatin1(
			"<?xml version=\"1.0\"?>\n"
			"<!DOCTYPE kopete-highlight-plugin>\n"
			"<highlight-plugin>\n" );

			// Save metafilter information.
		TQPtrListIterator<Filter> filtreIt( m_filters );
		for( ; filtreIt.current(); ++filtreIt )
		{
			Filter *filtre = *filtreIt;
			xml += TQString::fromLatin1( "  <filter>\n    <display-name>" )
				+ TQStyleSheet::escape(filtre->displayName)
				+ TQString::fromLatin1( "</display-name>\n" );

			xml += TQString::fromLatin1("    <search caseSensitive=\"") + TQString::number( static_cast<int>( filtre->caseSensitive ) ) +
				TQString::fromLatin1("\" regExp=\"") + TQString::number( static_cast<int>( filtre->isRegExp ) ) +
				TQString::fromLatin1( "\">" ) + TQStyleSheet::escape( filtre->search ) + TQString::fromLatin1( "</search>\n" );

			xml += TQString::fromLatin1("    <BG set=\"") + TQString::number( static_cast<int>( filtre->setBG ) ) +
				TQString::fromLatin1( "\">" ) + TQStyleSheet::escape( filtre->BG.name() ) + TQString::fromLatin1( "</BG>\n" );
			xml += TQString::fromLatin1("    <FG set=\"") + TQString::number( static_cast<int>( filtre->setFG ) ) +
				TQString::fromLatin1( "\">" ) + TQStyleSheet::escape( filtre->FG.name() ) + TQString::fromLatin1( "</FG>\n" );

			xml += TQString::fromLatin1("    <importance set=\"") + TQString::number( static_cast<int>( filtre->setImportance ) ) +
				TQString::fromLatin1( "\">" ) + TQString::number( filtre->importance ) + TQString::fromLatin1( "</importance>\n" );

			xml += TQString::fromLatin1("    <sound set=\"") + TQString::number( static_cast<int>( filtre->playSound ) ) +
				TQString::fromLatin1( "\">" ) + TQStyleSheet::escape( filtre->soundFN ) + TQString::fromLatin1( "</sound>\n" );

			xml += TQString::fromLatin1("    <raise set=\"") + TQString::number( static_cast<int>( filtre->raiseView ) ) +
				TQString::fromLatin1( "\"></raise>\n" );

			xml += TQString::fromLatin1( "  </filter>\n" );
		}

		xml += TQString::fromLatin1( "</highlight-plugin>\n" );

		*stream << xml;
	}
}

// vim: set noet ts=4 sts=4 sw=4: