summaryrefslogtreecommitdiffstats
path: root/konversation/src/emoticon.cpp
blob: c1fe023a8465078ead37b79e1a021527375ffc34 (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
/*
  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) 2005 Peter Simonsson <psn@linux.se>
*/

#include "emoticon.h"

#include <tqregexp.h>
#include <tqdom.h>
#include <tqfile.h>
#include <tqfontmetrics.h>

#include <kstaticdeleter.h>
#include <kstandarddirs.h>
#include <tdeversion.h>

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


namespace Konversation
{

    EmotIcon* EmotIcon::s_self = 0;
    static KStaticDeleter<EmotIcon> staticEmotIconDeleter;

    EmotIcon* EmotIcon::self()
    {
        if(!s_self)
        {
            staticEmotIconDeleter.setObject(s_self, new EmotIcon());
        }

        return s_self;
    }

    EmotIcon::EmotIcon()
    {
        s_self = this;

        if(Preferences::enableEmotIcons())
        {
            changeTheme(Preferences::emotIconTheme());
        }
    }

    EmotIcon::~EmotIcon()
    {
        if(s_self == this)
        {
            staticEmotIconDeleter.setObject(s_self, 0, false);
        }
    }

    void EmotIcon::changeTheme(const TQString& themeName)
    {
        if(themeName.isEmpty() || themeName == self()->m_themeName)
        {
            return;
        }

        #if KDE_IS_VERSION(3,3,91)
        TQString filename = KGlobal::dirs()->findResource("emoticons",  themeName + "/emoticons.xml");
        self()->m_themeName = themeName;
        #else
        TQString app = "konversation";
        TQString filename = KGlobal::dirs()->findResource("data", app + "/pics/emoticons/" + themeName + "/emoticons.xml");

        if(filename.isEmpty())
        {
            app = "kopete";
            filename = KGlobal::dirs()->findResource("data", app + "/pics/emoticons/" + themeName + "/emoticons.xml");
        }
        self()->m_themeName = app + '/' + themeName;
        #endif

        if(filename.isEmpty())
        {
            return;
        }

        TQFile file(filename);
        file.open(IO_ReadOnly);
        TQDomDocument doc;
        doc.setContent(&file);

        TQDomElement docElement = doc.documentElement();

        if(docElement.tagName() != "messaging-emoticon-map")
        {
            return;
        }

        self()->m_emotIconMap.clear();

        TQDomNode node = docElement.firstChild();
        TQDomElement element;
        TQDomNode stringNode;
        TQDomElement stringElement;
        TQString fileAttrib;
        TQString regExpStr;

        while(!node.isNull())
        {
            element = node.toElement();

            if(!element.isNull() && element.tagName() == "emoticon")
            {
                fileAttrib = findIcon(element.attribute("file", TQString()));
                regExpStr = "";
                stringNode = element.firstChild();

                while(!stringNode.isNull())
                {
                    stringElement = stringNode.toElement();

                    if(!stringElement.isNull() && stringElement.tagName() == "string")
                    {
                        if(regExpStr.isEmpty())
                        {
                            regExpStr = "(";
                        }
                        else
                        {
                            regExpStr += '|';
                        }

                        regExpStr += TQRegExp::escape(stringElement.text());
                    }

                    stringNode = stringNode.nextSibling();
                }

                if(!regExpStr.isEmpty() && !fileAttrib.isEmpty())
                {
                    regExpStr += ')';
                    self()->m_emotIconMap[fileAttrib] = regExpStr;
                }
            }

            node = node.nextSibling();
        }
    }

    TQString EmotIcon::filter(const TQString& txt, const TQFontMetrics& fm)
    {
        if(!Preferences::enableEmotIcons())
        {
            return txt;
        }

        TQString filteredTxt = txt;

        for(EmotIconMap::iterator it = self()->m_emotIconMap.begin(); it != self()->m_emotIconMap.end(); ++it)
        {
            TQRegExp regExp(TQString("(^|\\s)%1($|\\s)").tqarg(it.data()));
            filteredTxt.replace(regExp, " <img width=\"" + TQString::number(fm.height()) + "\" height=\"" + TQString::number(fm.height())
                + "\" src=\"" + it.key() + "\" alt=\"" + it.data() + "\">&nbsp;");
        }

        return filteredTxt;
    }

    TQString EmotIcon::findIcon(const TQString& filename)
    {
        //
        // This code was copied from void KopeteEmoticons::addIfPossible( const TQString& filename, TQStringList emoticons )
        //  Copyright (c) 2002      by Stefan Gehn            <metz AT gehn.net>
        //  Copyright (c) 2002      by Olivier Goffart        <ogoffart@tiscalinet.be>
        //
        KStandardDirs *dirs = KGlobal::dirs();
        TQString pic;

        #if KDE_IS_VERSION(3,3,91)
        TQString file = self()->m_themeName + '/' + filename;
        const char* resource = "emoticons";
        #else
        TQString app = self()->m_themeName.section('/', 0, 0);
        TQString dir = self()->m_themeName.section('/', 1);
        TQString file = app + "/pics/emoticons/" + dir + '/' + filename;
        const char* resource = "data";
        #endif

        //maybe an extension was given, so try to find the exact file
        pic = dirs->findResource(resource, file);

        if(pic.isEmpty())
        {
            pic = dirs->findResource(resource, file + ".mng");
        }
        if(pic.isEmpty())
        {
            pic = dirs->findResource(resource, file + ".png");
        }
        if(pic.isEmpty())
        {
            pic = dirs->findResource(resource, file + ".gif");
        }

        return pic;
    }

}