Show invite menu in wlm chat window immediately
[kdenetwork.git] / kopete / plugins / highlight / highlightconfig.cpp
blob6c53743339293e261a2e6bd4da24f6be8367d95c
1 /*
2 highlightconfig.cpp
4 Copyright (c) 2003 by Olivier Goffart <ogoffart@kde.org>
5 Copyright (c) 2003 by Matt Rogers <matt@matt.rogers.name>
7 Kopete (c) 2002-2003 by the Kopete developers <kopete-devel@kde.org>
9 *************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 *************************************************************************
19 #include <qfile.h>
20 #include <QTextDocument>
21 #include <qregexp.h>
22 #include <qdir.h>
23 #include <qdom.h>
24 #include <QTextCodec>
25 #include <QTextStream>
27 #include <ksavefile.h>
28 #include <kstandarddirs.h>
29 #include <klocale.h>
31 #include "filter.h"
32 #include "highlightconfig.h"
35 HighlightConfig::HighlightConfig()
39 HighlightConfig::~HighlightConfig()
41 qDeleteAll(m_filters);
42 m_filters.clear();
45 void HighlightConfig::removeFilter(Filter *f)
47 m_filters.removeAll(f);
48 delete f;
51 void HighlightConfig::appendFilter(Filter *f)
53 m_filters.append(f);
56 QList<Filter*> HighlightConfig::filters() const
58 return m_filters;
61 Filter* HighlightConfig::newFilter()
63 Filter *filtre=new Filter();
64 filtre->caseSensitive=false;
65 filtre->isRegExp=false;
66 filtre->setImportance=false;
67 filtre->importance=1;
68 filtre->setBG=false;
69 filtre->setFG=false;
70 filtre->raiseView=false;
71 filtre->displayName=i18n("-New filter-");
72 m_filters.append(filtre);
73 return filtre;
76 void HighlightConfig::load()
78 m_filters.clear(); //clear filters
80 QString filename = KStandardDirs::locateLocal( "appdata", QString::fromLatin1( "highlight.xml" ) );
81 if( filename.isEmpty() )
82 return ;
84 QDomDocument filterList( QString::fromLatin1( "highlight-plugin" ) );
86 QFile filterListFile( filename );
87 filterListFile.open( QIODevice::ReadOnly );
88 filterList.setContent( &filterListFile );
90 QDomElement list = filterList.documentElement();
92 QDomNode node = list.firstChild();
93 while( !node.isNull() )
95 QDomElement element = node.toElement();
96 if( !element.isNull() )
98 // if( element.tagName() == QString::fromLatin1("filter")
99 // {
100 Filter *filtre=newFilter();
101 QDomNode filterNode = node.firstChild();
103 while( !filterNode.isNull() )
105 QDomElement filterElement = filterNode.toElement();
106 if( !filterElement.isNull() )
108 if( filterElement.tagName() == QString::fromLatin1( "display-name" ) )
110 filtre->displayName = filterElement.text();
112 else if( filterElement.tagName() == QString::fromLatin1( "search" ) )
114 filtre->search = filterElement.text();
116 filtre->caseSensitive= ( filterElement.attribute( QString::fromLatin1( "caseSensitive" ), QString::fromLatin1( "1" ) ) == QString::fromLatin1( "1" ) );
117 filtre->isRegExp= ( filterElement.attribute( QString::fromLatin1( "regExp" ), QString::fromLatin1( "0" ) ) == QString::fromLatin1( "1" ) );
119 else if( filterElement.tagName() == QString::fromLatin1( "FG" ) )
121 filtre->FG = filterElement.text();
122 filtre->setFG= ( filterElement.attribute( QString::fromLatin1( "set" ), QString::fromLatin1( "0" ) ) == QString::fromLatin1( "1" ) );
124 else if( filterElement.tagName() == QString::fromLatin1( "BG" ) )
126 filtre->BG = filterElement.text();
127 filtre->setBG= ( filterElement.attribute( QString::fromLatin1( "set" ), QString::fromLatin1( "0" ) ) == QString::fromLatin1( "1" ) );
129 else if( filterElement.tagName() == QString::fromLatin1( "importance" ) )
131 filtre->importance = filterElement.text().toUInt();
132 filtre->setImportance= ( filterElement.attribute( QString::fromLatin1( "set" ), QString::fromLatin1( "0" ) ) == QString::fromLatin1( "1" ) );
134 else if( filterElement.tagName() == QString::fromLatin1( "raise" ) )
136 filtre->raiseView = ( filterElement.attribute( QString::fromLatin1( "set" ), QString::fromLatin1( "0" ) ) == QString::fromLatin1( "1" ) );
139 filterNode = filterNode.nextSibling();
141 // }
143 node = node.nextSibling();
145 filterListFile.close();
148 void HighlightConfig::save()
151 QString fileName = KStandardDirs::locateLocal( "appdata", QString::fromLatin1( "highlight.xml" ) );
153 KSaveFile file( fileName );
154 if( file.open() )
156 QTextStream stream ( &file );
157 stream.setCodec(QTextCodec::codecForName("UTF-8"));
159 QString xml = QString::fromLatin1(
160 "<?xml version=\"1.0\"?>\n"
161 "<!DOCTYPE kopete-highlight-plugin>\n"
162 "<highlight-plugin>\n" );
164 // Save metafilter information.
165 foreach(Filter *filtre , m_filters )
167 xml += QString::fromLatin1( " <filter>\n <display-name>" )
168 + Qt::escape(filtre->displayName)
169 + QString::fromLatin1( "</display-name>\n" );
171 xml += QString::fromLatin1(" <search caseSensitive=\"") + QString::number( static_cast<int>( filtre->caseSensitive ) ) +
172 QString::fromLatin1("\" regExp=\"") + QString::number( static_cast<int>( filtre->isRegExp ) ) +
173 QString::fromLatin1( "\">" ) + Qt::escape( filtre->search ) + QString::fromLatin1( "</search>\n" );
175 xml += QString::fromLatin1(" <BG set=\"") + QString::number( static_cast<int>( filtre->setBG ) ) +
176 QString::fromLatin1( "\">" ) + Qt::escape( filtre->BG.name() ) + QString::fromLatin1( "</BG>\n" );
177 xml += QString::fromLatin1(" <FG set=\"") + QString::number( static_cast<int>( filtre->setFG ) ) +
178 QString::fromLatin1( "\">" ) + Qt::escape( filtre->FG.name() ) + QString::fromLatin1( "</FG>\n" );
180 xml += QString::fromLatin1(" <importance set=\"") + QString::number( static_cast<int>( filtre->setImportance ) ) +
181 QString::fromLatin1( "\">" ) + QString::number( filtre->importance ) + QString::fromLatin1( "</importance>\n" );
183 xml += QString::fromLatin1(" <raise set=\"") + QString::number( static_cast<int>( filtre->raiseView ) ) +
184 QString::fromLatin1( "\"></raise>\n" );
186 xml += QString::fromLatin1( " </filter>\n" );
189 xml += QString::fromLatin1( "</highlight-plugin>\n" );
191 stream << xml;
195 // vim: set noet ts=4 sts=4 sw=4: