~ubuntu-branches/ubuntu/hardy/uim/hardy

« back to all changes in this revision

Viewing changes to qt/toolbar-common-uimstateindicator.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Masahito Omote
  • Date: 2005-12-04 13:10:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051204131042-ktzc8b17zi7a3cw8
Tags: 1:0.4.9.1-1
* New upstream release
* libuim0-nox, libuim-nox-dev, and libuim0-dbg-nox is now obsolete.
  Because libuim0 does not depends on X11. They now become dummy package,
  therefore you can safely remove them.
* Add --enable-debug in configure again.
* debian/patches/08_fix_privilage_escalation_CVE_2005_3149: disabled.
* Fix Error on purge because update-uim-config is not found.
  (closes: Bug#339345)
* uim-qt: New package for Qt utilities for uim. qt-immodule does not
  contained yet because of Debian's Qt3 does not support immodule and
  because uim does not recognize libqt4-dev's headers properly. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
 Copyright (c) 2003,2004,2005 uim Project http://uim.freedesktop.org/
 
4
 
 
5
 All rights reserved.
 
6
 
 
7
 Redistribution and use in source and binary forms, with or without
 
8
 modification, are permitted provided that the following conditions
 
9
 are met:
 
10
 
 
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.
 
19
 
 
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
 
30
 SUCH DAMAGE.
 
31
 
 
32
*/
 
33
#include "toolbar-common-uimstateindicator.h"
 
34
 
 
35
#include <qsocketnotifier.h>
 
36
#include <qstring.h>
 
37
#include <qtextcodec.h>
 
38
#include <qstringlist.h>
 
39
#include <qpoint.h>
 
40
#include <qtooltip.h>
 
41
 
 
42
#include <string.h>
 
43
#include <stdlib.h>
 
44
 
 
45
static int uim_fd;
 
46
static QHelperToolbarButton *fallbackButton = NULL;
 
47
static QSocketNotifier *notifier = NULL;
 
48
 
 
49
UimStateIndicator::UimStateIndicator( QWidget *parent, const char *name, WFlags f )
 
50
        : QHBox( parent, name, f )
 
51
{
 
52
    if ( !fallbackButton )
 
53
    {
 
54
        fallbackButton = new QHelperToolbarButton( this );
 
55
        fallbackButton->setText( "?" );
 
56
        fallbackButton->show();
 
57
    }
 
58
 
 
59
    buttons.setAutoDelete( true );
 
60
    buttons.clear();
 
61
 
 
62
    uim_fd = -1;
 
63
    checkHelperConnection();
 
64
    uim_helper_client_get_prop_list();
 
65
}
 
66
 
 
67
 
 
68
UimStateIndicator::~UimStateIndicator()
 
69
{
 
70
    if ( notifier )
 
71
        delete notifier;
 
72
 
 
73
    buttons.clear();
 
74
}
 
75
 
 
76
void UimStateIndicator::checkHelperConnection()
 
77
{
 
78
    if ( uim_fd < 0 )
 
79
    {
 
80
        uim_fd = uim_helper_init_client_fd( helper_disconnect_cb );
 
81
        if ( uim_fd > 0 )
 
82
        {
 
83
            if ( notifier )
 
84
                delete notifier;
 
85
            notifier = new QSocketNotifier( uim_fd, QSocketNotifier::Read );
 
86
            QObject::connect( notifier, SIGNAL( activated( int ) ),
 
87
                              this, SLOT( slotStdinActivated( int ) ) );
 
88
        }
 
89
    }
 
90
}
 
91
void UimStateIndicator::parseHelperStr( const QString& str )
 
92
{
 
93
    const QStringList lines = QStringList::split( "\n", str );
 
94
    if ( !lines.isEmpty() && !lines[ 0 ].isEmpty() )
 
95
    {
 
96
        if ( lines[ 0 ] == "prop_list_update" )
 
97
            propListUpdate( lines );
 
98
        else if ( lines[ 0 ] == "prop_label_update" )
 
99
            propLabelUpdate( lines );
 
100
    }
 
101
}
 
102
 
 
103
void UimStateIndicator::propListUpdate( const QStringList& lines )
 
104
{
 
105
    if ( !buttons.isEmpty() )
 
106
        buttons.clear();
 
107
 
 
108
    QHelperPopupMenu *popupMenu = NULL;
 
109
 
 
110
    QStringList::ConstIterator it = lines.begin();
 
111
    const QStringList::ConstIterator end = lines.end();
 
112
    for ( ; it != end; ++it )
 
113
    {
 
114
        const QStringList fields = QStringList::split( "\t", ( *it ) );
 
115
 
 
116
        if ( !fields.isEmpty() && !fields[ 0 ].isEmpty() )
 
117
        {
 
118
            if ( fields[ 0 ].startsWith( "branch" ) )
 
119
            {
 
120
                if ( fallbackButton )
 
121
                {
 
122
                    delete fallbackButton;
 
123
                    fallbackButton = NULL;
 
124
                }
 
125
                // create popup
 
126
                popupMenu = new QHelperPopupMenu( 0 );
 
127
                popupMenu->setCheckable( true );
 
128
 
 
129
                // create button
 
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 );
 
135
                button->show();
 
136
 
 
137
                buttons.append( button );
 
138
            }
 
139
            else if ( fields[ 0 ].startsWith( "leaf" ) )
 
140
            {
 
141
                if ( popupMenu
 
142
                        && !fields[ 2 ].isEmpty()
 
143
                        && !fields[ 3 ].isEmpty()
 
144
                        && !fields[ 4 ].isEmpty() )
 
145
                {
 
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 );
 
150
                }
 
151
            }
 
152
        }
 
153
    }
 
154
}
 
155
 
 
156
void UimStateIndicator::propLabelUpdate( const QStringList& lines )
 
157
{
 
158
    unsigned int i = 0;
 
159
    while ( !lines[ i ].isEmpty() )
 
160
        i++;
 
161
 
 
162
    if ( buttons.isEmpty() || buttons.count() != i - 2 )
 
163
    {
 
164
        uim_helper_client_get_prop_list();
 
165
        return ;
 
166
    }
 
167
 
 
168
    i = 1;
 
169
    while ( !lines[ i ].isEmpty() )
 
170
    {
 
171
        const QStringList fields = QStringList::split( "\t", lines[ i ] );
 
172
        if ( !fields.isEmpty() && !fields[ 0 ].isEmpty() && !fields[ 1 ].isEmpty() )
 
173
        {
 
174
            // set button label
 
175
            buttons.at( i - 2 ) ->setText( fields[ 0 ] );
 
176
            // set tooltip
 
177
            QToolTip::add( buttons.at( i - 2 ), fields[ 1 ] );
 
178
        }
 
179
 
 
180
        i++;
 
181
    }
 
182
}
 
183
 
 
184
void UimStateIndicator::helper_disconnect_cb()
 
185
{
 
186
    uim_fd = -1;
 
187
    QObject::disconnect( notifier, SIGNAL( activated( int ) ), 0, 0 );
 
188
}
 
189
 
 
190
void UimStateIndicator::slotStdinActivated( int /*socket*/ )
 
191
{
 
192
    uim_helper_read_proc( uim_fd );
 
193
 
 
194
    QString tmp = QString::null;
 
195
    char *s;
 
196
    while ( ( s = uim_helper_get_message() ) )
 
197
    {
 
198
        const QStringList lines = QStringList::split( "\n", QString( s ) );
 
199
        if ( !lines.isEmpty() && !lines[ 1 ].isEmpty() && lines[ 1 ].startsWith( "charset" ) )
 
200
        {
 
201
            /* get charset */
 
202
            QString charset = QStringList::split( "=", lines[ 1 ] ) [ 1 ];
 
203
 
 
204
            /* convert to unicode */
 
205
            QTextCodec *codec = QTextCodec::codecForName( charset );
 
206
            tmp = codec->toUnicode( s );
 
207
        }
 
208
        else
 
209
        {
 
210
            /* no charset */
 
211
            tmp = s;
 
212
        }
 
213
 
 
214
        parseHelperStr( tmp );
 
215
        free( s );
 
216
    }
 
217
}
 
218
 
 
219
 
 
220
/**/
 
221
 
 
222
QHelperPopupMenu::QHelperPopupMenu( QWidget *parent, const char *name )
 
223
    : QPopupMenu( parent, name )
 
224
{
 
225
    msgDict.setAutoDelete( true );
 
226
    msgDict.clear();
 
227
}
 
228
 
 
229
QHelperPopupMenu::~QHelperPopupMenu()
 
230
{
 
231
    msgDict.clear();
 
232
}
 
233
 
 
234
int QHelperPopupMenu::insertHelperItem( const QString &menulabelStr,
 
235
                                        const QString &menutooltipStr,
 
236
                                        const QString &menucommandStr )
 
237
{
 
238
    const int id = insertItem( menulabelStr, this, SLOT( slotMenuActivated( int ) ) );
 
239
 
 
240
    setWhatsThis( id, menutooltipStr );
 
241
    msgDict.insert( id, new QString( menucommandStr ) );
 
242
 
 
243
    return id;
 
244
}
 
245
 
 
246
void QHelperPopupMenu::slotMenuActivated( int id )
 
247
{
 
248
    QString msg = *msgDict.find( id );
 
249
    msg.prepend( "prop_activate\n" );
 
250
    msg.append( "\n" );
 
251
    uim_helper_send_message( uim_fd, ( const char* ) msg );
 
252
}
 
253
 
 
254
#include "toolbar-common-uimstateindicator.moc"