summaryrefslogtreecommitdiffstats
path: root/kopete/kopete/config/appearance/emoticonseditdialog.cpp
blob: 7bd232739d9dad8e817188e243595457e2873f63 (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
/*
    appearanceconfig.cpp  -  Kopete Look Feel Config

    Kopete    (c) 2002-2009 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 "emoticonseditdialog.h"
#include "emoticonseditwidget.h"

#include "kopeteglobal.h"
#include "kopeteprefs.h"
#include "kopeteemoticons.h"

#include <tdelocale.h>
#include <tdelistview.h>
#include <kstandarddirs.h>
#include <tdefiledialog.h>
#include <tdeio/job.h>
#include <tqpixmap.h>
#include <tqheader.h>
#include <tqlayout.h>
#include <tqlabel.h>


EditDialog::EditDialog(TQWidget *parent, const char* name)
    : KDialogBase(parent, name, true, i18n(name), Ok|Cancel, Ok, true)
{
    setupDlg();
}

EditDialog::EditDialog(TQWidget *parent, const char* name, TQPixmap emot, TQString text, TQString file)
    : KDialogBase(parent, name, true, i18n(name), Ok|Cancel, Ok, true)
{
    setupDlg();
    leText->setText(text);
    btnIcon->setPixmap(emot);
    emoticon = file;
}

void EditDialog::setupDlg()
{
    wdg = new TQWidget(this);
    TQVBoxLayout *vl = new TQVBoxLayout(wdg, 11, 6);
    TQHBoxLayout *hb = new TQHBoxLayout(wdg, 0, 6);
    leText = new KLineEdit(wdg);
    btnIcon = new KPushButton(wdg);
    btnIcon->setFixedSize(TQSize(64, 64));

    vl->addWidget(new TQLabel(i18n("Insert the string for the emoticon\nseparated by space if you want multiple strings"), wdg));
    hb->addWidget(btnIcon);
    hb->addWidget(leText);
    vl->addLayout(hb);
    setMainWidget(wdg);
    connect(btnIcon, TQT_SIGNAL(clicked()), this, TQT_SLOT(btnIconClicked()));
}

void EditDialog::btnIconClicked()
{
    KURL url =  KFileDialog::getImageOpenURL();

    if(!url.isLocalFile())
        return;

    emoticon = url.path();

    if(emoticon.isEmpty())
        return;

    btnIcon->setPixmap(TQPixmap(emoticon));
}

EmoticonsEditDialog::EmoticonsEditDialog(TQWidget *parent, TQString theme, const char* name)
    : KDialogBase(parent, name, true, i18n("Emoticons Editor"), Ok|Cancel, Ok, true)
{
    themeName = theme;

    mMainWidget = new EmoticonsEditWidget(this, "EmoticonsEditDialog::mMainWidget");
    setMainWidget(mMainWidget);
    resize(TQSize(450, 350));
    mMainWidget->btnAdd->setGuiItem(KStdGuiItem::add());
    mMainWidget->btnEdit->setText(i18n("Edit..."));
    mMainWidget->btnRemove->setGuiItem(KStdGuiItem::remove());


    mMainWidget->klvEmoticons->addColumn("Emoticon");
    mMainWidget->klvEmoticons->addColumn("Text");
    mMainWidget->klvEmoticons->addColumn("File", 0);
    mMainWidget->klvEmoticons->header()->hide();
    Kopete::Emoticons emoticons( theme );
    TQMap<TQString, TQStringList> smileys = emoticons.emoticonAndPicList();


    for(TQMap<TQString, TQStringList>::Iterator it = smileys.begin(); it != smileys.end(); ++it )
    {
        TDEListViewItem *itm = new TDEListViewItem(mMainWidget->klvEmoticons);
        itm->setPixmap(0, TQPixmap(it.key()));
        itm->setText(2, TQFileInfo(it.key()).baseName());
        TQString text = *it.data().at(0);
        for(uint i = 1; i < it.data().size(); i++) {
            text += " " + *it.data().at(i);
        }
        itm->setText(1, text);
    }

    TQFile *fp = new TQFile(TDEGlobal::dirs()->saveLocation( "emoticons",  themeName, false ) +  "/emoticons.xml");

    if( !fp->exists() ) {
        kdWarning() << "EmoticonsEditDialog::EmoticonsEditDialog() " << fp->name() << " doesn't exist!" << endl;
        return;
    }

    if(!fp->open( IO_ReadOnly )) {
        kdWarning() << "EmoticonsEditDialog::EmoticonsEditDialog() " << fp->name() << " can't open ReadOnly!" << endl;
        return;
    }

    if(!themeXml.setContent(fp)) {
        kdWarning() << "EmoticonsEditDialog::EmoticonsEditDialog() " << fp->name() << " can't copy to xml!" << endl;
        fp->close();
        return;
    }

    fp->close();

    mMainWidget->klvEmoticons->setColumnWidth(0, TQListView::Maximum);
    mMainWidget->klvEmoticons->setColumnWidth(1, TQListView::Maximum);

    connect(this, TQT_SIGNAL(okClicked()), this, TQT_SLOT(slotOkClicked()));
    connect(mMainWidget->btnAdd, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotAddClicked()));
    connect(mMainWidget->btnEdit, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotEditClicked()));
    connect(mMainWidget->btnRemove, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotRemoveClicked()));
}

void EmoticonsEditDialog::slotOkClicked()
{
    TQFile *fp = new TQFile(TDEGlobal::dirs()->saveLocation( "emoticons",  themeName, false ) +  "/emoticons.xml");

    if( !fp->exists() ) {
        kdWarning() << "EmoticonsEditDialog::slotOkClicked() " << fp->name() << " doesn't exist!" << endl;
        return;
    }

    if(!fp->open( IO_WriteOnly )) {
        kdWarning() << "EmoticonsEditDialog::slotOkClicked() " << fp->name() << " can't open WriteOnly!" << endl;
        return;
    }

    TQTextStream emoStream(fp);
    emoStream << themeXml.toString(4);
    fp->close();
}

void EmoticonsEditDialog::slotAddClicked()
{
    EditDialog *dlg = new EditDialog(this, "Add emoticon");

    if(dlg->exec() == TQDialog::Rejected)
        return;

    if(dlg->getText().isEmpty() || !dlg->getEmoticon())
        return;

    addEmoticon(dlg->getEmoticon(), dlg->getText(), true);


    delete dlg;
}

void EmoticonsEditDialog::slotEditClicked()
{
    if(!mMainWidget->klvEmoticons->selectedItem())
        return;

    dlg = new EditDialog(this, "Edit emoticon", *mMainWidget->klvEmoticons->selectedItem()->pixmap(0), mMainWidget->klvEmoticons->selectedItem()->text(1),  mMainWidget->klvEmoticons->selectedItem()->text(2));

    if(dlg->exec() == TQDialog::Rejected)
        return;

    if(dlg->getText().isEmpty() || !dlg->getEmoticon())
        return;

    bool copy;
    TQString emo = dlg->getEmoticon();
    if(mMainWidget->klvEmoticons->selectedItem()->text(2) != dlg->getEmoticon()) {
        copy = true;
    } else {
        copy = false;
        TQString f = mMainWidget->klvEmoticons->selectedItem()->text(2);

        TDEStandardDirs *dir = TDEGlobal::dirs();
        emo = dir->findResource( "emoticons", themeName + TQString::fromLatin1( "/" ) + f );

        if( emo.isNull() )
            emo = dir->findResource( "emoticons", themeName + TQString::fromLatin1( "/" ) + f + TQString::fromLatin1( ".mng" ) );
        if ( emo.isNull() )
            emo = dir->findResource( "emoticons", themeName + TQString::fromLatin1( "/" ) + f + TQString::fromLatin1( ".png" ) );
        if ( emo.isNull() )
            emo = dir->findResource( "emoticons", themeName + TQString::fromLatin1( "/" ) + f + TQString::fromLatin1( ".gif" ) );
        if ( emo.isNull() )
            return;
    }

    removeEmoticon(mMainWidget->klvEmoticons->selectedItem()->text(2));
    addEmoticon(emo, dlg->getText(), copy);

    delete dlg;
}

void EmoticonsEditDialog::slotRemoveClicked()
{
    if(!mMainWidget->klvEmoticons->selectedItem())
        return;

    removeEmoticon(mMainWidget->klvEmoticons->selectedItem()->text(2));
}

void EmoticonsEditDialog::addEmoticon(TQString emo, TQString text, bool copy)
{
    if(copy)
        TDEIO::copy(emo, TDEGlobal::dirs()->saveLocation( "emoticons",  themeName, false ));

    TDEListViewItem *itm = new TDEListViewItem(mMainWidget->klvEmoticons);
    itm->setPixmap(0, TQPixmap(emo));
    itm->setText(1, text);
    itm->setText(2, TQFileInfo(emo).baseName());

    TQDomNode lc = themeXml.lastChild();
    if(lc.isNull())
        return;

    TQDomElement emoticon = themeXml.createElement("emoticon");
    emoticon.setAttribute("file", TQFileInfo(emo).baseName());
    lc.appendChild(emoticon);
    TQStringList splitted = TQStringList::split(" ", text);
    TQStringList::const_iterator constIterator;
    for(constIterator = splitted.begin(); constIterator != splitted.end(); constIterator++)
    {
        TQDomElement emotext = themeXml.createElement("string");
        TQDomText txt = themeXml.createTextNode((*constIterator).stripWhiteSpace());
        emotext.appendChild(txt);
        emoticon.appendChild(emotext);
    }
}

void EmoticonsEditDialog::removeEmoticon(TQString emo)
{
    TQDomNode lc = themeXml.lastChild();
    if(lc.isNull())
        return;

    TQDomNodeList nl = lc.childNodes();

    for(uint i = 0; i < nl.length(); i++) {
        TQDomElement de = nl.item(i).toElement();
        if(!de.isNull() && de.tagName() == "emoticon" && de.attribute("file") == emo) {
            lc.removeChild(de);
            delete mMainWidget->klvEmoticons->selectedItem();
            return;
        }
    }
}

#include "emoticonseditdialog.moc"