3
Copyright (c) 2003-2011 uim Project http://code.google.com/p/uim/
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions
11
1. Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
2. Redistributions in binary form must reproduce the above copyright
14
notice, this list of conditions and the following disclaimer in the
15
documentation and/or other materials provided with the distribution.
16
3. Neither the name of authors nor the names of its contributors
17
may be used to endorse or promote products derived from this software
18
without specific prior written permission.
20
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
21
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
24
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35
#include "common-uimstateindicator.h"
36
#include <uim/uim-scm.h>
38
#include <QtCore/QSocketNotifier>
39
#include <QtCore/QString>
40
#include <QtCore/QStringList>
41
#include <QtCore/QTextCodec>
42
#include <QtGui/QHBoxLayout>
43
#include <QtGui/QMouseEvent>
44
#include <QtGui/QPixmap>
50
#include "qtgettext.h"
52
static const QString ICONDIR = UIM_PIXMAPSDIR;
54
static QHelperToolbarButton *fallbackButton = 0;
55
static QSocketNotifier *notifier = 0;
57
static inline QString qstring_(const QString &string)
60
return mygettext(string.toUtf8().data());
66
UimStateIndicator::UimStateIndicator( QWidget *parent )
69
m_layout = new QHBoxLayout;
70
m_layout->setMargin( 0 );
71
m_layout->setSpacing( 0 );
73
if ( !fallbackButton )
75
fallbackButton = new QHelperToolbarButton( this );
76
m_layout->addWidget( fallbackButton );
77
QPixmap icon = QPixmap( ICONDIR + '/' + "uim-icon.png" );
78
if ( !icon.isNull() ) {
79
QPixmap scaledIcon = icon.scaled( ICON_SIZE, ICON_SIZE,
80
Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
81
fallbackButton->setIcon( QIcon( scaledIcon ) );
83
fallbackButton->setText( "?" );
85
fallbackButton->show();
91
checkHelperConnection();
92
uim_helper_client_get_prop_list();
93
popupMenuShowing = false;
95
setLayout( m_layout );
99
UimStateIndicator::~UimStateIndicator()
107
int UimStateIndicator::getNumButtons()
109
return buttons.count();
112
void UimStateIndicator::checkHelperConnection()
116
uim_fd = uim_helper_init_client_fd( helper_disconnect_cb );
121
notifier = new QSocketNotifier( uim_fd, QSocketNotifier::Read );
122
connect( notifier, SIGNAL( activated( int ) ),
123
this, SLOT( slotStdinActivated() ) );
127
void UimStateIndicator::parseHelperStr( const QString& str )
129
const QStringList lines = str.split( '\n', QString::SkipEmptyParts );
130
if ( !lines.isEmpty() && !lines[ 0 ].isEmpty() )
132
if ( lines[ 0 ] == "prop_list_update" )
133
propListUpdate( lines );
134
else if (lines[0] == "custom_reload_notify" )
135
uim_prop_reload_configs();
139
void UimStateIndicator::propListUpdate( const QStringList& lines )
141
if (popupMenuShowing)
144
QHelperPopupMenu *popupMenu = 0;
145
#ifdef PLASMA_APPLET_UIM
146
int prevCount = m_layout->count();
148
foreach ( QHelperToolbarButton *button, buttons )
150
if ( m_layout->indexOf( button ) >= 0 )
151
m_layout->removeWidget( button );
152
if ( buttons.contains( button ) )
154
buttons.removeAll( button );
159
foreach ( const QString &line, lines )
161
const QStringList fields = line.split( '\t', QString::SkipEmptyParts );
163
if ( !fields.isEmpty() && !fields[ 0 ].isEmpty() )
165
if ( fields[ 0 ].startsWith( QLatin1String( "branch" ) ) )
167
if ( fallbackButton )
169
m_layout->removeWidget( fallbackButton );
170
delete fallbackButton;
174
QHelperToolbarButton *button = new QHelperToolbarButton;
175
m_layout->addWidget( button );
176
buttons.append( button );
179
uim_scm_symbol_value_bool("toolbar-icon-for-dark-background?");
180
const QString append = isDarkBg ? "_dark_background" : "";
181
QString fileName = ICONDIR + '/' + fields[1] + append + ".png";
183
if ( isDarkBg && stat( fileName.toUtf8().data(), &st ) == -1 )
185
fileName = ICONDIR + '/' + fields[1] + ".png";
187
QPixmap icon = QPixmap( fileName );
188
if (!icon.isNull()) {
189
QPixmap scaledIcon = icon.scaled( ICON_SIZE, ICON_SIZE,
190
Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
191
button->setIcon( QIcon( scaledIcon ) );
193
button->setText( fields[ 2 ] );
195
button->setToolTip( fields[ 3 ] );
198
#ifdef PLASMA_APPLET_UIM
199
popupMenu = new QHelperPopupMenu( 0 );
201
popupMenu = new QHelperPopupMenu( button );
203
connect( popupMenu, SIGNAL( aboutToShow() ),
204
this, SLOT( slotPopupMenuAboutToShow() ) );
205
connect( popupMenu, SIGNAL( aboutToHide() ),
206
this, SLOT( slotPopupMenuAboutToHide() ) );
207
connect( button, SIGNAL( menuRequested( QMenu* ) ),
208
this, SIGNAL( menuRequested( QMenu* ) ) );
209
button->setMenu( popupMenu );
210
button->setPopupMode( QToolButton::InstantPopup );
214
else if ( fields[ 0 ].startsWith( QLatin1String( "leaf" ) ) )
217
&& !fields[ 1 ].isEmpty()
218
&& !fields[ 3 ].isEmpty()
219
&& !fields[ 4 ].isEmpty()
220
&& !fields[ 5 ].isEmpty() )
222
QAction *action = popupMenu->insertHelperItem(
223
fields[1], qstring_( fields[ 3 ] ),
224
fields[ 4 ], fields[ 5 ] );
225
// check the item which is now used
226
if ( fields.count() > 6 && fields[ 6 ] == "*" )
227
action->setChecked( true );
233
#ifdef PLASMA_APPLET_UIM
234
if ( m_layout->count() != prevCount )
236
emit indicatorResized();
239
void UimStateIndicator::helper_disconnect_cb()
242
disconnect( notifier, SIGNAL( activated( int ) ), 0, 0 );
245
void UimStateIndicator::slotStdinActivated()
247
uim_helper_read_proc( uim_fd );
251
while ( ( s = uim_helper_get_message() ) )
253
const QStringList lines = QString( s ).split( '\n',
254
QString::SkipEmptyParts );
255
if ( lines.count() > 1
256
&& lines[ 1 ].startsWith( QLatin1String( "charset" ) ) )
259
QString charset = lines[ 1 ].split( '=',
260
QString::SkipEmptyParts ) [ 1 ];
262
/* convert to unicode */
264
= QTextCodec::codecForName( QByteArray( charset.toAscii() ) );
265
tmp = codec->toUnicode( s );
273
parseHelperStr( tmp );
278
void UimStateIndicator::slotPopupMenuAboutToShow()
280
popupMenuShowing = true;
283
void UimStateIndicator::slotPopupMenuAboutToHide()
285
popupMenuShowing = false;
288
void UimStateIndicator::clearButtons()
290
while ( !buttons.isEmpty() ) {
291
QHelperToolbarButton *button = buttons.takeFirst();
292
m_layout->removeWidget( button );
298
QHelperToolbarButton::QHelperToolbarButton( QWidget *parent )
299
: QToolButton( parent )
301
setAutoRaise( true );
304
QSize QHelperToolbarButton::sizeHint() const
306
return QSize( BUTTON_SIZE, BUTTON_SIZE );
309
void QHelperToolbarButton::mousePressEvent( QMouseEvent *event )
311
#ifdef PLASMA_APPLET_UIM
312
QMenu *popupMenu = menu();
313
if ( event->button() == Qt::LeftButton && popupMenu ) {
314
emit menuRequested( popupMenu );
318
QToolButton::mousePressEvent( event );
323
QHelperPopupMenu::QHelperPopupMenu( QWidget *parent )
327
connect( this, SIGNAL( triggered( QAction * ) ),
328
this, SLOT( slotMenuActivated( QAction * ) ) );
331
QHelperPopupMenu::~QHelperPopupMenu()
336
QAction *QHelperPopupMenu::insertHelperItem( const QString &indicationIdStr,
337
const QString &menulabelStr,
338
const QString &menutooltipStr,
339
const QString &menucommandStr )
343
uim_scm_symbol_value_bool("toolbar-icon-for-dark-background?");
344
const QString append = isDarkBg ? "_dark_background" : "";
345
QString fileName = ICONDIR + '/' + indicationIdStr + append + ".png";
347
if ( isDarkBg && stat( fileName.toUtf8().data(), &st ) == -1 )
349
fileName = ICONDIR + '/' + indicationIdStr + ".png";
351
QPixmap icon = QPixmap ( fileName );
353
if (!icon.isNull()) {
354
QPixmap scaledIcon = icon.scaled( ICON_SIZE, ICON_SIZE,
355
Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
356
action = addAction( scaledIcon, menulabelStr );
358
action = addAction( menulabelStr );
361
action->setCheckable( true );
362
action->setWhatsThis( menutooltipStr );
363
msgDict.insert( action, menucommandStr );
368
void QHelperPopupMenu::slotMenuActivated( QAction *action )
370
QString msg = msgDict.find( action ).value();
371
msg.prepend( "prop_activate\n" );
373
uim_helper_send_message( uim_fd, msg.toAscii().constData() );