From 540525398ad441beb0bc7fe9068f4b1fc62fdefd Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Mon, 28 Apr 2014 14:36:38 +0800 Subject: write class ZhuyinEditor in progress --- src/ZYEnhancedText.cc | 2 + src/ZYLibZhuyin.cc | 35 +++++++++++++- src/ZYLibZhuyin.h | 2 +- src/ZYZZhuyinEditor.cc | 121 +++++++++++++++++++++++++++++++++++++++++++++++++ src/ZYZZhuyinEditor.h | 2 + 5 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 src/ZYZZhuyinEditor.cc (limited to 'src') diff --git a/src/ZYEnhancedText.cc b/src/ZYEnhancedText.cc index 25692cd..0231705 100644 --- a/src/ZYEnhancedText.cc +++ b/src/ZYEnhancedText.cc @@ -324,6 +324,8 @@ get_number_of_phonetic_sections (String & enhanced_text) get_symbol_section (enhanced_text, start_pos, end_pos, type, lookup, choice); } + + start_pos = end_pos; } return num; diff --git a/src/ZYLibZhuyin.cc b/src/ZYLibZhuyin.cc index 5ef26e8..3abff84 100644 --- a/src/ZYLibZhuyin.cc +++ b/src/ZYLibZhuyin.cc @@ -28,7 +28,7 @@ #define LIBZHUYIN_SAVE_TIMEOUT (5 * 60) -using namespace ZY; +namespace ZY { std::unique_ptr LibZhuyinBackEnd::m_instance; @@ -155,3 +155,36 @@ LibZhuyinBackEnd::saveUserDB (void) zhuyin_save (m_zhuyin_context); return TRUE; } + +void +LibZhuyinBackEnd::modified (void) +{ + /* Restart the timer */ + g_timer_start (m_timer); + + if (m_timeout_id != 0) + return; + + m_timeout_id = g_timeout_add_seconds (LIBZHUYIN_SAVE_TIMEOUT, + LibZhuyinBackEnd::timeoutCallback, + static_cast (this)); +} + +gboolean +LibZhuyinBackEnd::timeoutCallback (gpointer data) +{ + LibZhuyinBackEnd *self = static_cast(data); + + /* Get the elapsed time since last modification of database. */ + guint elapsed = (guint)g_timer_elapsed (self->m_timer, NULL); + + if (elapsed >= LIBZHUYIN_SAVE_TIMEOUT && + self->saveUserDB ()) { + self->m_timeout_id = 0; + return FALSE; + } + + return TRUE; +} + +}; diff --git a/src/ZYLibZhuyin.h b/src/ZYLibZhuyin.h index d8e05d8..be1eb84 100644 --- a/src/ZYLibZhuyin.h +++ b/src/ZYLibZhuyin.h @@ -60,7 +60,7 @@ public: private: gboolean saveUserDB (void); - static gboolean timeoutCallback (gpointer date); + static gboolean timeoutCallback (gpointer data); private: /* libzhuyin context */ diff --git a/src/ZYZZhuyinEditor.cc b/src/ZYZZhuyinEditor.cc new file mode 100644 index 0000000..83f3520 --- /dev/null +++ b/src/ZYZZhuyinEditor.cc @@ -0,0 +1,121 @@ +/* vim:set et ts=4 sts=4: + * + * ibus-libzhuyin - New Zhuyin engine based on libzhuyin for IBus + * + * Copyright (c) 2014 Peng Wu + * + * 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, or (at your option) + * any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "ZYZZhuyinEditor.h" +#include "ZYConfig.h" +#include "ZYLibZhuyin.h" + + +using namespace ZY; + +ZhuyinEditor::ZhuyinEditor (ZhuyinProperties & props, Config & config) + : PhoneticEditor (props, config) +{ + m_instance = LibZhuyinBackEnd::instance ().allocZhuyinInstance (); +} + +ZhuyinEditor::~ZhuyinEditor (void) +{ + LibZhuyinBackEnd::instance ().freeZhuyinInstance (m_instance); + m_instance = NULL; +} + +void +ZhuyinEditor::commit (void) +{ + if (G_UNLIKELY (m_preedit_text.empty ())) + return; + + for (size_t i = 0; i < m_instances.size (); ++i) { + zhuyin_train (m_instances[i]); + } + LibZhuyinBackEnd::instance ().modified (); + PhoneticEditor::commit (m_preedit_text.c_str ()); + reset (); +} + +void +ZhuyinEditor::reset (void) +{ + PhoneticEditor::reset (); +} + +void +ZhuyinEditor::updateZhuyin (void) +{ + String & enhanced_text = m_text; + + resizeInstances (); + + size_t index = 0; + size_t start_pos = 0, end_pos = 0; + + while (end_pos != enhanced_text.size ()) { + section_t type = probe_section_quick (enhanced_text, start_pos); + + if (PHONETIC_SECTION == type) { + String section; + get_phonetic_section (enhanced_text, start_pos, end_pos, section); + zhuyin_instance_t * instance = m_instances[index]; + zhuyin_parse_more_chewings (instance, section.c_str ()); + zhuyin_guess_sentence (instance); + ++index; + } + + if (SYMBOL_SECTION == type) { + String type, lookup, choice; + get_symbol_section (enhanced_text, start_pos, end_pos, + type, lookup, choice); + } + + start_pos = end_pos; + } + + return; +} + +void +ZhuyinEditor::updateAuxiliaryText (void) +{ + /* libchewing doesn't use the auxiliary text, always hide. */ + return; +} + +void +ZhuyinEditor::updatePreeditText (void) +{ + if (G_UNLIKELY (m_text.empty ())) { + hidePreeditText (); + return; + } + + String & enhanced_text = m_text; + + assert (FALSE); +} + +void +ZhuyinEditor::update (void) +{ + updateLookupTable (); + updatePreeditText (); + updateAuxiliaryText (); +} diff --git a/src/ZYZZhuyinEditor.h b/src/ZYZZhuyinEditor.h index 521bd8f..4ede0ed 100644 --- a/src/ZYZZhuyinEditor.h +++ b/src/ZYZZhuyinEditor.h @@ -47,6 +47,8 @@ protected: gboolean insert (gint ch); +protected: + String m_preedit_text; }; }; -- cgit