Newer
Older
scim-wnn / honoka / plugins / cannaconversion.cpp
/***************************************************************************
 *   Copyright (C) 2004 by TAM(Teppei Tamra)                               *
 *   tam-t@par.odn.ne.jp                                                   *
 *                                                                         *
 *   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 of the License, 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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "cannaconversion.h"
#include <honoka_plugin_def.h>

#ifdef HAVE_CONFIG_H
  #include <config.h>
#endif

#ifdef HAVE_GETTEXT
  #include <libintl.h>
  #define _(String) dgettext(GETTEXT_PACKAGE,String)
  #define N_(String) (String)
#else
  #define _(String) (String)
  #define N_(String) (String)
  #define bindtextdomain(Package,Directory)
  #define textdomain(domain)
  #define bind_textdomain_codeset(domain,codeset)
#endif

// プライグイン化のおまじないです。
HonokaPluginRegister(CannaConversion);


CannaConversion::CannaConversion(ConfigPointer cfg) : Convertor(cfg)
{
    m_iconv.set_encoding ("EUC-JP");
    pos = 0;
    String dhdp = cfg->read(HONOKA_CONFIG_CANNA_DICHOMEDIRPATH,String(HONOKA_DEFAULT_CANNA_DICHOMEDIRPATH));

    RkInitialize((char *)dhdp.c_str());
    context = 0;
    ready = false;
}


CannaConversion::~CannaConversion()
{
}

bool CannaConversion::isConnected() {
    return ready;
}

void CannaConversion::reset(){
    RkEndBun(context,1);
    return;
}

void CannaConversion::setYomiText(WideString yomi) {
    yomiText = yomi;
}

int CannaConversion::ren_conversion() {
    pos = 0;
    String y;
    m_iconv.convert(y,yomiText);
    bun = RkBgnBun(context,(char *)y.c_str(),strlen(y.c_str()),RK_XFER);
    if (bun > 0) {
        buildResult();
    }
    RkGoTo(context,0);

    return bun;
}

WideString CannaConversion::getText() {
    WideString text;
    for(unsigned int i = 0;i < convResult.size();i ++) {
        text += convResult[i].kouho[convResult[i].pos].kanji;
    }
    return text;
}

int CannaConversion::setPos(int p){
    if ((p < convResult.size()) && (p >= 0)) pos = p;
    RkGoTo(context,pos);
    return pos;
}

int CannaConversion::getPos() {
    return pos;
}

ResultList CannaConversion::getResultList(int p,ResultType kt){
    if (p == -1) p = pos;
    if (p >= convResult.size()) return ResultList();
    if ((kt != DEFAULT) || (p >= convResult.size())) return ResultList();
    setPos(p);
    return convResult[p];
}

bool CannaConversion::select(int p) {
    if (p < convResult[pos].count()) {
        convResult[pos].pos = p;
        RkXfer(context,convResult[pos].pos);
        return true;
    }
    return false;
}

AttributeList CannaConversion::getAttributeList() {
    AttributeList attr;
    int l = 0;
    for(unsigned int i = 0;i < convResult.size();i ++) {
        if (pos == i) {
            Attribute a(l,convResult[i].kouho[convResult[i].pos].kanji.length(),SCIM_ATTR_DECORATE,SCIM_ATTR_DECORATE_REVERSE);
            attr.push_back(a);
        }
        l += convResult[i].kouho[convResult[i].pos].kanji.length();
    }
    return attr;
}

bool CannaConversion::resizeRegion(int w) {
    if ((convResult[pos].Yomi.length() + w) < 1) return false;
    if ((pos >= (convResult.size() - 1)) && (w > 0)) return false;
    WideString nt;
    for(unsigned int i = pos;i < convResult.size();i ++) nt += convResult[i].Yomi;
    nt = nt.substr(0,convResult[pos].Yomi.length() + w);
    String s;
    m_iconv.convert(s,nt);
    bun = RkResize(context,strlen(s.c_str()));
    buildResult();
    return true;
}

void CannaConversion::updateFrequency() {
    return;
}

bool CannaConversion::connect() {
    char dic[4096],*dicp;
    int dc = RkGetDicList(context,dic,4095);
    if (dc == -1) {
        return false;
    }
    dicp = dic;
    for(unsigned int i = 0;i < dc;i ++) {
        RkMountDic(context,dicp,0);
        dicp += strlen(dicp) + 1;
    }
    ready = true;
    return true;
}

void CannaConversion::disconnect() {
    return;
}

int CannaConversion::getCaretPos() {
    int l = 0;
    for(unsigned int i = 0;i < convResult.size();i ++) {
        if (pos == i) return l;
        l += convResult[i].kouho[convResult[i].pos].kanji.length();
    }
    return 0;
}

void CannaConversion::buildResult() {
    convResult.clear();
    for(unsigned int i = 0;i < bun;i ++) {
        RkGoTo(context,i);
        unsigned char k[4096],*resp;
        resp = k;
        WideString t;
        ResultList m_res;
        m_res.kType = DEFAULT;
        m_res.Title = utf8_mbstowcs(String(_("lookup result")));
        RkGetYomi(context,k,511);
        m_iconv.convert(t,String((const char *)k));
        m_res.Yomi = t;
        int c = RkGetKanjiList(context,k,4095);
        m_res.pos = 0;
        for(unsigned int j = 0;j < c;j ++) {
            m_iconv.convert(t,String((const char *)resp));
            m_res.kouho.push_back(t);
            resp += strlen((const char*)resp) + 1;
        }
        convResult.push_back(m_res);
    }
    RkGoTo(context,pos);
}



String CannaConversion::getName()
{
    return String("Canna");
}

String CannaConversion::getPropertyName()
{
    return String(_("CannaConversion"));
}