summaryrefslogtreecommitdiffstats
path: root/kopete/kopete/chatwindow/kopeteemoticonaction.cpp
blob: 8f9447f94ab39a3d2010f84bfb5e82450ff517ed (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
/*
    kopeteemoticonaction.cpp

    TDEAction to show the emoticon selector

    Copyright (c) 2002      by Stefan Gehn            <metz AT gehn.net>
    Copyright (c) 2003      by Martijn Klingens       <klingens@kde.org>

    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 "kopeteemoticonaction.h"

#include <math.h>

#include <tqwhatsthis.h>

#include <tdeapplication.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdemenubar.h>
#include <tdepopupmenu.h>
#include <tdetoolbar.h>
#include <tdetoolbarbutton.h>

#include "emoticonselector.h"
#include "kopeteemoticons.h"

class KopeteEmoticonAction::KopeteEmoticonActionPrivate
{
public:
	KopeteEmoticonActionPrivate()
	{
		m_delayed = true;
		m_stickyMenu = true;
		m_popup = new TDEPopupMenu(0L,"KopeteEmoticonActionPrivate::m_popup");
		emoticonSelector = new EmoticonSelector( m_popup, "KopeteEmoticonActionPrivate::emoticonSelector");
		m_popup->insertItem( emoticonSelector );
		// TODO: Maybe connect to kopeteprefs and redo list only on config changes
		connect( m_popup, TQT_SIGNAL( aboutToShow() ), emoticonSelector, TQT_SLOT( prepareList() ) );
	}

	~KopeteEmoticonActionPrivate()
	{
		delete m_popup;
		m_popup = 0;
	}

	TDEPopupMenu *m_popup;
	EmoticonSelector *emoticonSelector;
	bool m_delayed;
	bool m_stickyMenu;
};

KopeteEmoticonAction::KopeteEmoticonAction( TQObject* parent, const char* name )
  : TDEAction( i18n( "Add Smiley" ), 0, parent, name )
{
	d = new KopeteEmoticonActionPrivate;

	// Try to load the icon for our current emoticon theme, when it fails
	// fall back to our own default
	TQString icon;
	TQMap<TQString, TQStringList> emoticonsMap = Kopete::Emoticons::self()->emoticonAndPicList();
	for( TQMap<TQString, TQStringList>::const_iterator it = emoticonsMap.constBegin();
		it != emoticonsMap.constEnd(); ++it )
	{
		if( ( *it ).contains( ":)" ) || ( *it ).contains( ":-)" ) )
		{
			icon = it.key();
			break;
		}
	}

	if ( icon.isNull() )
		setIcon( "emoticon" );
	else
		setIconSet( TQIconSet( icon ) );

	setShortcutConfigurable( false );
	connect( d->emoticonSelector, TQT_SIGNAL( ItemSelected( const TQString & ) ),
		this, TQT_SIGNAL( activated( const TQString & ) ) );
}

KopeteEmoticonAction::~KopeteEmoticonAction()
{
	unplugAll();
//	kdDebug(14010) << "KopeteEmoticonAction::~KopeteEmoticonAction()" << endl;
	delete d;
	d = 0;
}

void KopeteEmoticonAction::popup( const TQPoint& global )
{
	popupMenu()->popup( global );
}

TDEPopupMenu* KopeteEmoticonAction::popupMenu() const
{
	return d->m_popup;
}

bool KopeteEmoticonAction::delayed() const
{
	return d->m_delayed;
}

void KopeteEmoticonAction::setDelayed(bool _delayed)
{
	d->m_delayed = _delayed;
}

bool KopeteEmoticonAction::stickyMenu() const
{
	return d->m_stickyMenu;
}

void KopeteEmoticonAction::setStickyMenu(bool sticky)
{
	d->m_stickyMenu = sticky;
}

int KopeteEmoticonAction::plug( TQWidget* widget, int index )
{
	if (kapp && !kapp->authorizeTDEAction(name()))
		return -1;

//	kdDebug(14010) << "KopeteEmoticonAction::plug( " << widget << ", " << index << " )" << endl;

	// KDE4/TQt TODO: Use qobject_cast instead.
	if ( widget->inherits(TQPOPUPMENU_OBJECT_NAME_STRING) )
	{
		TQPopupMenu* menu = static_cast<TQPopupMenu*>( widget );
		int id;
		if ( hasIcon() )
			id = menu->insertItem( iconSet(TDEIcon::Small), text(), d->m_popup, -1, index );
		else
			id = menu->insertItem( text(), d->m_popup, -1, index );

		if ( !isEnabled() )
			menu->setItemEnabled( id, false );

		addContainer( menu, id );
		connect( menu, TQT_SIGNAL( destroyed() ), this, TQT_SLOT( slotDestroyed() ) );

		if ( m_parentCollection )
			m_parentCollection->connectHighlight( menu, this );

		return containerCount() - 1;
	}
	// KDE4/TQt TODO: Use qobject_cast instead.
	else if ( widget->inherits( "TDEToolBar" ) )
	{
		TDEToolBar *bar = static_cast<TDEToolBar *>( widget );

		int id_ = TDEAction::getToolButtonID();

		if ( icon().isEmpty() && !iconSet(TDEIcon::Small).isNull() )
		{
			bar->insertButton(
				iconSet(TDEIcon::Small).pixmap(), id_, TQT_SIGNAL(clicked()), this,
				TQT_SLOT(slotActivated()), isEnabled(), plainText(),
				index );
		}
		else
		{
			TDEInstance *instance;

			if ( m_parentCollection )
			instance = m_parentCollection->instance();
			else
			instance = TDEGlobal::instance();

			bar->insertButton( icon(), id_, TQT_SIGNAL( clicked() ), this,
									TQT_SLOT( slotActivated() ), isEnabled(), plainText(),
									index, instance );
		}

		addContainer( bar, id_ );

		if (!whatsThis().isEmpty())
			TQWhatsThis::add( bar->getButton(id_), whatsThis() );

		connect( bar, TQT_SIGNAL( destroyed() ), this, TQT_SLOT( slotDestroyed() ) );

		if (delayed())
			bar->setDelayedPopup(id_, popupMenu(), stickyMenu());
		else
			bar->getButton(id_)->setPopup(popupMenu(), stickyMenu());

		if ( m_parentCollection )
			m_parentCollection->connectHighlight(bar, this);

		return containerCount() - 1;
	}
	// KDE4/TQt TODO: Use qobject_cast instead.
	else if ( widget->inherits( TQMENUBAR_OBJECT_NAME_STRING ) )
	{
		TQMenuBar *bar = static_cast<TQMenuBar *>( widget );

		int id;

		id = bar->insertItem( text(), popupMenu(), -1, index );

		if ( !isEnabled() )
			bar->setItemEnabled( id, false );

		addContainer( bar, id );
		connect( bar, TQT_SIGNAL( destroyed() ), this, TQT_SLOT( slotDestroyed() ) );

		return containerCount() - 1;
	}

	return -1;
}

#include "kopeteemoticonaction.moc"