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"

#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);
HonokaPluginSetup(CannaConversion);

HonokaSetupCorePage *CannaConversion::setup()
{
    HonokaSetupPage *page = new HonokaSetupPage(_("Canna-plugin"),"","");
    page->append(new HonokaSetupEntryItem(
        _("_Server: "),
        HONOKA_CONFIG_CANNA_SERVER,
        _("input the server name to connect."),
        HONOKA_DEFAULT_CANNA_SERVER
    ));
    page->append(new HonokaSetupFileItem(
        _("_DicHomeDir: "),
        HONOKA_CONFIG_CANNA_DICHOMEDIRPATH,
        _("set your DicHomeDir in this entry."),
        HONOKA_DEFAULT_CANNA_DICHOMEDIRPATH
    ));
    HonokaSetupPage *sc = new HonokaSetupPage(_("shortcut keys: "),"","");
    sc->append(new HonokaSetupKeyItem(
        _("Canna Conversion: "),
        String(HONOKA_CONFIG_KEY_CONVERTOR_PREFIX) + String("/Canna"),
        "",
        ""
    ));
    page->append(sc);

    return page;
};

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));

    context = 0;
    host = cfg->read(HONOKA_CONFIG_CANNA_SERVER,String(HONOKA_DEFAULT_CANNA_SERVER));
    if ((host == "localhost") || (!host.length())) 
        RkInitialize((char *)dhdp.c_str());
    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;
}

const vector<Segment> CannaConversion::getSegmentList() {
    vector<Segment> result;
    for(unsigned int i = 0;i < convResult.size();i ++) result.push_back(Segment(convResult[i].kouho[convResult[i].pos].kanji,convResult[i].Yomi));
    return result;
}

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;
}


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() {
    if ((host != "localhost") && (host.length())) {
        jrKanjiControl(0,KC_SETSERVERNAME,(char *)host.c_str());
        if (jrKanjiControl(0,KC_INITIALIZE,0) == -1) {
            ready = false;
            return false;
        }
    ready = true;
    return true;
    }
    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;
}

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"));
}