File: scim_backend.cpp

package info (click to toggle)
scim 1.4.7-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 20,368 kB
  • ctags: 12,834
  • sloc: cpp: 51,239; sh: 22,370; ansic: 18,716; makefile: 1,258; xml: 641; yacc: 288
file content (509 lines) | stat: -rw-r--r-- 17,258 bytes parent folder | download | duplicates (10)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
 * Smart Common Input Method
 * 
 * Copyright (c) 2002-2005 James Su <suzhe@tsinghua.org.cn>
 *
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 *
 * $Id: scim_backend.cpp,v 1.38.2.1 2006/09/24 16:00:52 suzhe Exp $
 *
 */

#define Uses_SCIM_FILTER_MANAGER
#define Uses_SCIM_BACKEND
#define Uses_SCIM_IMENGINE
#define Uses_SCIM_IMENGINE_MODULE
#define Uses_SCIM_CONFIG_PATH
#define Uses_STL_ALGORITHM
#include "scim_private.h"
#include "scim.h"
#include "scim_stl_map.h"

namespace scim {

#if SCIM_USE_STL_EXT_HASH_MAP
typedef __gnu_cxx::hash_map <String, IMEngineFactoryPointer, scim_hash_string >     IMEngineFactoryRepository;
#elif SCIM_USE_STL_HASH_MAP
typedef std::hash_map <String, IMEngineFactoryPointer, scim_hash_string >           IMEngineFactoryRepository;
#else
typedef std::map <String, IMEngineFactoryPointer>                                   IMEngineFactoryRepository;
#endif

typedef std::vector <IMEngineFactoryPointer>                                        IMEngineFactoryPointerVector;

class LocaleEqual
{
    String m_lhs;
public:
    LocaleEqual (const String &lhs) : m_lhs (lhs) { }

    bool operator () (const String &rhs) const {
        if (m_lhs == rhs) return true;
        if (scim_get_locale_language (m_lhs) == scim_get_locale_language (rhs) &&
            scim_get_locale_encoding (m_lhs) == scim_get_locale_encoding (rhs) &&
            m_lhs.find ('.') != String::npos && rhs.find ('.') != String::npos)
            return true;
        return false;
    }
};

class IMEngineFactoryPointerLess
{
public:
    bool operator () (const IMEngineFactoryPointer &lhs, const IMEngineFactoryPointer &rhs) const {
        return (lhs->get_language () < rhs->get_language ()) ||
               (lhs->get_language () == rhs->get_language () && lhs->get_name () < rhs->get_name ());
    }
};

class BackEndBase::BackEndBaseImpl
{
    IMEngineFactoryRepository    m_factory_repository;
    String                       m_supported_unicode_locales;
    ConfigPointer                m_config;

public:
    BackEndBaseImpl (const ConfigPointer &config)
        : m_config (config)
    {
        String locales;

        // Set the default supported locales.
        locales = scim_global_config_read (SCIM_GLOBAL_CONFIG_SUPPORTED_UNICODE_LOCALES, String ("en_US.UTF-8"));
 
        std::vector <String> locale_list;
        std::vector <String> real_list;
 
        scim_split_string_list (locale_list, locales);
 
        for (std::vector <String>::iterator i = locale_list.begin (); i!= locale_list.end (); ++i) {
            *i = scim_validate_locale (*i);
            if (i->length () && scim_get_locale_encoding (*i) == "UTF-8" &&
                std::find_if (real_list.begin (), real_list.end (), LocaleEqual (*i)) == real_list.end ())
                real_list.push_back (*i);
        }
 
        m_supported_unicode_locales = scim_combine_string_list (real_list);
    }

    void clear ()
    {
        m_factory_repository.clear ();
    }

    String get_all_locales () const
    {
        String locale;
 
        std::vector <String> locale_list;
        std::vector <String> real_list;
 
        IMEngineFactoryRepository::const_iterator it; 
 
        for (it = m_factory_repository.begin (); it != m_factory_repository.end (); ++it) {
            if (locale.length () == 0)
                locale += it->second->get_locales ();
            else
                locale += (String (",") + it->second->get_locales ());
        }
 
        if (m_supported_unicode_locales.length ())
            locale += (String (",") + m_supported_unicode_locales);
 
        scim_split_string_list (locale_list, locale);
 
        for (std::vector <String>::iterator i = locale_list.begin (); i!= locale_list.end (); i++) {
            locale = scim_validate_locale (*i);
            if (locale.length () &&
                std::find_if (real_list.begin (), real_list.end (), LocaleEqual (locale)) == real_list.end ())
                real_list.push_back (locale);
        }
 
        return scim_combine_string_list (real_list);
    }

    IMEngineFactoryPointer get_factory (const String &uuid) const
    {
        IMEngineFactoryRepository::const_iterator it = m_factory_repository.find (uuid);

        if (it != m_factory_repository.end ())
            return it->second;

        return IMEngineFactoryPointer (0);
    }

    uint32 get_factories_for_encoding (std::vector<IMEngineFactoryPointer> &factories,
                                       const String                        &encoding)  const
    {
        IMEngineFactoryRepository::const_iterator it;
 
        factories.clear ();
 
        for (it = m_factory_repository.begin (); it != m_factory_repository.end (); ++it) {
            if ((encoding.length () == 0 || it->second->validate_encoding (encoding)))
                factories.push_back (it->second);
        }
 
        sort_factories (factories);

        return factories.size ();
    }

    uint32 get_factories_for_language (std::vector<IMEngineFactoryPointer> &factories,
                                       const String                        &language)  const
    {
        IMEngineFactoryRepository::const_iterator it;
 
        factories.clear ();
 
        for (it = m_factory_repository.begin (); it != m_factory_repository.end (); ++it) {
            if ((language.length () == 0 || it->second->get_language () == language))
                factories.push_back (it->second);
        }
 
        sort_factories (factories);

        return factories.size ();
    }

    IMEngineFactoryPointer get_default_factory (const String &language, const String &encoding) const
    {
        if (!language.length ()) return IMEngineFactoryPointer ();

        IMEngineFactoryPointerVector factories;

        if (get_factories_for_encoding (factories, encoding) > 0) {
            IMEngineFactoryPointer lang_first;
            IMEngineFactoryPointerVector::iterator it;

            String def_uuid;
            
            def_uuid = m_config->read (String (SCIM_CONFIG_DEFAULT_IMENGINE_FACTORY) +
                                       String ("/") + language,
                                       String (""));

            // Match by Normalized language exactly.
            for (it = factories.begin (); it != factories.end (); ++it) {
                if (scim_get_normalized_language ((*it)->get_language ()) == language && lang_first.null ())
                    lang_first = *it;

                if ((*it)->get_uuid () == def_uuid)
                    return *it; 
            }

            if (!lang_first.null ()) return lang_first;

            // Match by short language name.
            for (it = factories.begin (); it != factories.end (); ++it)
                if ((*it)->get_language () == language.substr (0,2))
                    return *it;

            return factories [0];
        }

        return IMEngineFactoryPointer ();
    }

    void set_default_factory (const String &language, const String &uuid)
    {
        if (!language.length () || !uuid.length ()) return;

        IMEngineFactoryPointerVector factories;
        if (get_factories_for_encoding (factories, "") > 0) {
            IMEngineFactoryPointerVector::iterator it;
            for (it = factories.begin (); it != factories.end (); ++it) {
                if ((*it)->get_uuid () == uuid) {
                    m_config->write (String (SCIM_CONFIG_DEFAULT_IMENGINE_FACTORY) + String ("/") + language, uuid);
                    return;
                }
            }
        }
    }

    IMEngineFactoryPointer get_next_factory (const String &language, const String &encoding, const String &cur_uuid) const
    {
        IMEngineFactoryPointerVector factories;

        if (get_factories_for_encoding (factories, encoding) > 0) {
            IMEngineFactoryPointer lang_first;
            IMEngineFactoryPointerVector::iterator it, itl;

            for (it = factories.begin (); it != factories.end (); ++it) {
                if ((language.length () == 0 || (*it)->get_language () == language) && lang_first.null ())
                    lang_first = *it;

                if ((*it)->get_uuid () == cur_uuid) {
                    for (itl = it + 1; itl != factories.end (); ++itl) {
                        if (language.length () == 0 || (*itl)->get_language () == language)
                            return *itl;
                    }
                    if (!lang_first.null ()) return lang_first;

                    return factories [0];
                }
            }
        }

        return IMEngineFactoryPointer ();
    }

    IMEngineFactoryPointer get_previous_factory (const String &language, const String &encoding, const String &cur_uuid) const
    {
        IMEngineFactoryPointerVector factories;

        if (get_factories_for_encoding (factories, encoding) > 0) {
            IMEngineFactoryPointer lang_last;
            IMEngineFactoryPointerVector::iterator it, itl;

            for (it = factories.begin (); it != factories.end (); ++it) {
                if ((language.length () == 0 || (*it)->get_language () == language))
                    lang_last = *it;
            }

            for (it = factories.begin (); it != factories.end (); ++it) {
                if ((*it)->get_uuid () == cur_uuid) {
                    for (itl = it; itl != factories.begin (); --itl) {
                        if (language.length () == 0 || (*(itl-1))->get_language () == language)
                            return *(itl-1);
                    }

                    if (!lang_last.null ()) return lang_last;

                    return factories [factories.size () - 1];
                }
            }
        }

        return IMEngineFactoryPointer ();
    }

    bool add_factory (const IMEngineFactoryPointer &factory)
    {
        if (!factory.null ()) {
            String uuid = factory->get_uuid ();

            if (uuid.length () && m_factory_repository.find (uuid) == m_factory_repository.end ()) {
                m_factory_repository [uuid] = factory;
                return true;
            }
        }

        return false;
    }

private:
    void sort_factories (std::vector<IMEngineFactoryPointer> &factories) const
    {
        std::sort (factories.begin (), factories.end (), IMEngineFactoryPointerLess ());
    }
};

BackEndBase::BackEndBase (const ConfigPointer &config)
    : m_impl (new BackEndBase::BackEndBaseImpl (config))
{
}

BackEndBase::~BackEndBase ()
{
    delete m_impl;
}

String
BackEndBase::get_all_locales () const
{
    return m_impl->get_all_locales ();
}

IMEngineFactoryPointer
BackEndBase::get_factory (const String &uuid) const
{
    return m_impl->get_factory (uuid);
}

uint32
BackEndBase::get_factories_for_encoding (std::vector<IMEngineFactoryPointer> &factories, const String &encoding) const
{
    return m_impl->get_factories_for_encoding (factories, encoding);
}

uint32
BackEndBase::get_factories_for_language (std::vector<IMEngineFactoryPointer> &factories, const String &language) const
{
    return m_impl->get_factories_for_language (factories, language);
}

IMEngineFactoryPointer
BackEndBase::get_default_factory (const String &language, const String &encoding) const
{
    return m_impl->get_default_factory (language, encoding);
}

void
BackEndBase::set_default_factory (const String &language, const String &uuid)
{
    m_impl->set_default_factory (language, uuid);
}

IMEngineFactoryPointer
BackEndBase::get_next_factory (const String &language, const String &encoding, const String &cur_uuid) const
{
    return m_impl->get_next_factory (language, encoding, cur_uuid);
}

IMEngineFactoryPointer
BackEndBase::get_previous_factory (const String &language, const String &encoding, const String &cur_uuid) const
{
    return m_impl->get_previous_factory (language, encoding, cur_uuid);
}

bool
BackEndBase::add_factory (const IMEngineFactoryPointer &factory)
{
    return m_impl->add_factory (factory);
}

void
BackEndBase::clear ()
{
    m_impl->clear ();
}

// Implementation of CommonBackEnd.
struct CommonBackEnd::CommonBackEndImpl {
    IMEngineModule      *m_engine_modules;
    FilterManager       *m_filter_manager;

    CommonBackEndImpl () : m_engine_modules (0), m_filter_manager (0) { }
};

CommonBackEnd::CommonBackEnd (const ConfigPointer       &config,
                              const std::vector<String> &modules)
    : BackEndBase (config),
      m_impl (new CommonBackEndImpl)
{
    IMEngineFactoryPointer factory;
    std::vector<String>    disabled_factories;
    std::vector<String>    new_modules = modules;

    int all_factories_count = 0;
    int module_factories_count = 0;

    if (config.null ()) return;

    // Get disabled factories list.
    disabled_factories = scim_global_config_read (SCIM_GLOBAL_CONFIG_DISABLED_IMENGINE_FACTORIES, disabled_factories);

    // Put socket module to the end of list.
    for (std::vector<String>::iterator it = new_modules.begin (); it != new_modules.end (); ++it) {
        if (*it == "socket") {
            new_modules.erase (it);
            new_modules.push_back ("socket");
            break;
        }
    }

    // Try to load all IMEngine modules
    try {
        m_impl->m_engine_modules = new IMEngineModule [new_modules.size ()];
        m_impl->m_filter_manager = new FilterManager (config);
    } catch (const std::exception & err) {
        std::cerr << err.what () << "\n";
        return;
    }

    //load IMEngine modules
    for (size_t i = 0; i < new_modules.size (); ++i) {
        SCIM_DEBUG_BACKEND (1) << "Loading IMEngine module: " << new_modules [i] << " ...\n";

        module_factories_count = 0;

        if (m_impl->m_engine_modules [i].load (new_modules [i], config) &&
            m_impl->m_engine_modules [i].valid ()) {
            for (size_t j=0; j < m_impl->m_engine_modules [i].number_of_factories (); ++j) {

                // Try to load a IMEngine Factory.
                try {
                    factory = m_impl->m_engine_modules [i].create_factory (j);
                } catch (const std::exception & err) {
                    std::cerr << err.what () << "\n";
                    factory.reset ();
                }

                if (!factory.null ()) {
                    // Check if it's disabled.
                    if (std::find (disabled_factories.begin (),
                                   disabled_factories.end (),
                                   factory->get_uuid ()) == disabled_factories.end ()) {

                        // Add it into disabled list to prevent from loading again.
                        disabled_factories.push_back (factory->get_uuid ());

                        // Only load filter for none socket IMEngines.
                        if (new_modules [i] != "socket")
                            factory = m_impl->m_filter_manager->attach_filters_to_factory (factory);

                        add_factory (factory);

                        all_factories_count ++;
                        module_factories_count ++;

                        SCIM_DEBUG_BACKEND (1) << "    Loading IMEngine Factory " << j << " : " << "OK\n";
                    } else {
                        SCIM_DEBUG_BACKEND (1) << "    Loading IMEngine Factory " << j << " : " << "Disabled\n";
                        factory.reset ();
                    }
                } else {
                    SCIM_DEBUG_BACKEND (1) << "    Loading IMEngine Factory " << j << " : " << "Failed\n";
                }
            }
            if (module_factories_count) {
                SCIM_DEBUG_BACKEND (1) << new_modules [i] << " IMEngine module is successfully loaded.\n";
            } else {
                SCIM_DEBUG_BACKEND (1) << "No Factory loaded from " << new_modules [i] << " IMEngine module!\n";
                m_impl->m_engine_modules [i].unload ();
            }
        } else {
            SCIM_DEBUG_BACKEND (1) << "Failed to load " << new_modules [i] << " IMEngine module.\n";
        }
    }

    factory = new ComposeKeyFactory ();

    if (all_factories_count == 0 ||
        std::find (disabled_factories.begin (),
                   disabled_factories.end (),
                   factory->get_uuid ()) == disabled_factories.end ()) {
        factory = m_impl->m_filter_manager->attach_filters_to_factory (factory);
        add_factory (factory);
    }
}

CommonBackEnd::~CommonBackEnd ()
{
    clear ();

    delete [] m_impl->m_engine_modules;
    delete m_impl->m_filter_manager;
    delete m_impl;
}


} // namespace scim

/*
vi:ts=4:nowrap:ai:expandtab
*/