3
Copyright (c) 2003,2004,2005 uim Project http://uim.freedesktop.org/
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
33
#include "toolbar-common-uimstateindicator.h"
35
#include <qsocketnotifier.h>
37
#include <qtextcodec.h>
38
#include <qstringlist.h>
46
static QHelperToolbarButton *fallbackButton = NULL;
47
static QSocketNotifier *notifier = NULL;
49
UimStateIndicator::UimStateIndicator( QWidget *parent, const char *name, WFlags f )
50
: QHBox( parent, name, f )
52
if ( !fallbackButton )
54
fallbackButton = new QHelperToolbarButton( this );
55
fallbackButton->setText( "?" );
56
fallbackButton->show();
59
buttons.setAutoDelete( true );
63
checkHelperConnection();
64
uim_helper_client_get_prop_list();
68
UimStateIndicator::~UimStateIndicator()
76
void UimStateIndicator::checkHelperConnection()
80
uim_fd = uim_helper_init_client_fd( helper_disconnect_cb );
85
notifier = new QSocketNotifier( uim_fd, QSocketNotifier::Read );
86
QObject::connect( notifier, SIGNAL( activated( int ) ),
87
this, SLOT( slotStdinActivated( int ) ) );
91
void UimStateIndicator::parseHelperStr( const QString& str )
93
const QStringList lines = QStringList::split( "\n", str );
94
if ( !lines.isEmpty() && !lines[ 0 ].isEmpty() )
96
if ( lines[ 0 ] == "prop_list_update" )
97
propListUpdate( lines );
98
else if ( lines[ 0 ] == "prop_label_update" )
99
propLabelUpdate( lines );
103
void UimStateIndicator::propListUpdate( const QStringList& lines )
105
if ( !buttons.isEmpty() )
108
QHelperPopupMenu *popupMenu = NULL;
110
QStringList::ConstIterator it = lines.begin();
111
const QStringList::ConstIterator end = lines.end();
112
for ( ; it != end; ++it )
114
const QStringList fields = QStringList::split( "\t", ( *it ) );
116
if ( !fields.isEmpty() && !fields[ 0 ].isEmpty() )
118
if ( fields[ 0 ].startsWith( "branch" ) )
120
if ( fallbackButton )
122
delete fallbackButton;
123
fallbackButton = NULL;
126
popupMenu = new QHelperPopupMenu( 0 );
127
popupMenu->setCheckable( true );
130
QHelperToolbarButton *button = new QHelperToolbarButton( this );
131
button->setText( fields[ 1 ] );
132
QToolTip::add( button, fields[ 2 ] );
133
button->setPopup( popupMenu );
134
button->setPopupDelay( 50 );
137
buttons.append( button );
139
else if ( fields[ 0 ].startsWith( "leaf" ) )
142
&& !fields[ 2 ].isEmpty()
143
&& !fields[ 3 ].isEmpty()
144
&& !fields[ 4 ].isEmpty() )
146
int id = popupMenu->insertHelperItem( fields[ 2 ], fields[ 3 ], fields[ 4 ] );
147
// check the item which is now used
148
if ( !fields[ 5 ].isEmpty() && fields[ 5 ] == "*" )
149
popupMenu->setItemChecked( id, true );
156
void UimStateIndicator::propLabelUpdate( const QStringList& lines )
159
while ( !lines[ i ].isEmpty() )
162
if ( buttons.isEmpty() || buttons.count() != i - 2 )
164
uim_helper_client_get_prop_list();
169
while ( !lines[ i ].isEmpty() )
171
const QStringList fields = QStringList::split( "\t", lines[ i ] );
172
if ( !fields.isEmpty() && !fields[ 0 ].isEmpty() && !fields[ 1 ].isEmpty() )
175
buttons.at( i - 2 ) ->setText( fields[ 0 ] );
177
QToolTip::add( buttons.at( i - 2 ), fields[ 1 ] );
184
void UimStateIndicator::helper_disconnect_cb()
187
QObject::disconnect( notifier, SIGNAL( activated( int ) ), 0, 0 );
190
void UimStateIndicator::slotStdinActivated( int /*socket*/ )
192
uim_helper_read_proc( uim_fd );
194
QString tmp = QString::null;
196
while ( ( s = uim_helper_get_message() ) )
198
const QStringList lines = QStringList::split( "\n", QString( s ) );
199
if ( !lines.isEmpty() && !lines[ 1 ].isEmpty() && lines[ 1 ].startsWith( "charset" ) )
202
QString charset = QStringList::split( "=", lines[ 1 ] ) [ 1 ];
204
/* convert to unicode */
205
QTextCodec *codec = QTextCodec::codecForName( charset );
206
tmp = codec->toUnicode( s );
214
parseHelperStr( tmp );
222
QHelperPopupMenu::QHelperPopupMenu( QWidget *parent, const char *name )
223
: QPopupMenu( parent, name )
225
msgDict.setAutoDelete( true );
229
QHelperPopupMenu::~QHelperPopupMenu()
234
int QHelperPopupMenu::insertHelperItem( const QString &menulabelStr,
235
const QString &menutooltipStr,
236
const QString &menucommandStr )
238
const int id = insertItem( menulabelStr, this, SLOT( slotMenuActivated( int ) ) );
240
setWhatsThis( id, menutooltipStr );
241
msgDict.insert( id, new QString( menucommandStr ) );
246
void QHelperPopupMenu::slotMenuActivated( int id )
248
QString msg = *msgDict.find( id );
249
msg.prepend( "prop_activate\n" );
251
uim_helper_send_message( uim_fd, ( const char* ) msg );
254
#include "toolbar-common-uimstateindicator.moc"