~ubuntu-branches/ubuntu/quantal/kiten/quantal-proposed

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
/*****************************************************************************
 * This file is part of Kiten, a KDE Japanese Reference Tool                 *
 * Copyright (C) 2001 Jason Katz-Brown <jason@katzbrown.com>                 *
 * Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com>                      *
 * Copyright (C) 2006 Eric Kjeldergaard <kjelderg@gmail.com>                 *
 *                                                                           *
 * This library is free software; you can redistribute it and/or             *
 * modify it under the terms of the GNU Library 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         *
 * Library General Public License for more details.                          *
 *                                                                           *
 * You should have received a copy of the GNU Library General Public License *
 * along with this library; see the file COPYING.LIB.  If not, write to      *
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
 * Boston, MA 02110-1301, USA.                                               *
 *****************************************************************************/

#include "dictfilekanjidic.h"

#include "../dictquery.h"  //DictQuery classs
#include "../entry.h"      //Entry and EntryList classes
#include "entrykanjidic.h"

#include <KConfigSkeleton>
#include <KGlobal>

#include <QFile>

QStringList *DictFileKanjidic::displayFields = NULL;

DictFileKanjidic::DictFileKanjidic()
: DictFileEdict()
{
  m_dictionaryType = "kanjidic"; //Override the default type
  m_searchableAttributes.clear();
  m_searchableAttributes.insert( "bushu",      "B" );
  m_searchableAttributes.insert( "classical",  "C" );
  m_searchableAttributes.insert( "henshall",   "E" );
  m_searchableAttributes.insert( "frequency",  "F" );
  m_searchableAttributes.insert( "grade",      "G" );
  m_searchableAttributes.insert( "halpern",    "H" );
  m_searchableAttributes.insert( "spahn",      "I" );
  m_searchableAttributes.insert( "hadamitzky", "I" );
  m_searchableAttributes.insert( "gakken",     "K" );
  m_searchableAttributes.insert( "heisig",     "L" );
  m_searchableAttributes.insert( "morohashi",  "M" );
  m_searchableAttributes.insert( "nelson",     "N" );
  m_searchableAttributes.insert( "oneill",     "O" );
  m_searchableAttributes.insert( "skip",       "P" );
  m_searchableAttributes.insert( "4cc",        "Q" );
  m_searchableAttributes.insert( "stroke",     "S" );
  m_searchableAttributes.insert( "strokes",    "S" );
  m_searchableAttributes.insert( "unicode",    "U" );
  m_searchableAttributes.insert( "haig",       "V" );
  m_searchableAttributes.insert( "korean",     "W" );
  m_searchableAttributes.insert( "pinyin",     "Y" );
  m_searchableAttributes.insert( "other",      "D" );
}

DictFileKanjidic::~DictFileKanjidic()
{
}

QMap<QString,QString> DictFileKanjidic::displayOptions() const
{
  // Enumerate the fields in our dict.... there are a rather lot of them here
  // It will be useful for a few things to have the full list generated on it's own
  QMap<QString,QString> list;
  // TODO: Figure out how to internationalize these easily
  list.insert( "Bushu Number(B)",                      "B");
  list.insert( "Classical Radical Number(C)",          "C");
  list.insert( "Henshall's Index Number(E)",           "E");
  list.insert( "Frequency Ranking(F)",                 "F");
  list.insert( "Grade Level(G)",                       "G");
  list.insert( "Halpern's New J-E Char Dictionary(H)", "H");
  list.insert( "Spahn & Hadamitzky Reference(I)",      "I");
  list.insert( "Gakken Kanji Dictionary Index(K)",     "L");
  list.insert( "Heisig's Index(L)",                    "L");
  list.insert( "Morohashi's Daikanwajiten(M)",         "M");
  list.insert( "Nelsons Modern Reader's J-E Index(N)", "N");
  list.insert( "O'Neill's 'Japanese Names' Index(O)",  "O");
  list.insert( "SKIP Code(P)",                         "P");
  list.insert( "Four Corner codes(Q)",                 "Q");
  list.insert( "Stroke Count(S)",                      "S");
  list.insert( "Unicode Value(U)",                     "U");
  list.insert( "Haig's New Nelson J-E Dict(V)",        "V");
  list.insert( "Korean Reading(W)",                    "W");
  list.insert( "kanjidic field: X",                    "X");
  list.insert( "Pinyin Reading(Y)",                    "Y");
  list.insert( "Common SKIP Misclassifications(Z)",    "Z");
  list.insert( "Misc Dictionary Codes (D)",            "D");
  return list;
}

void DictFileKanjidic::loadSettings( KConfigSkeleton *config )
{
  QMap<QString,QString> list = displayOptions();
  list[ "Word/Kanji" ]  = "Word/Kanji";
  list[ "Reading" ]     = "Reading";
  list[ "Meaning" ]     = "Meaning";
  list[ "--Newline--" ] = "--Newline--";

  KConfigSkeletonItem *item = config->findItem( getType() + "__displayFields" );
  this->displayFields = loadListType( item, this->displayFields, list );
}

inline Entry* DictFileKanjidic::makeEntry( QString x )
{
  return new EntryKanjidic( getName(), x );
}

/**
 * Scan a potential file for the correct format, remembering to skip comment
 * characters. This is not a foolproof scan, but it should be checked before adding
 * a new dictionary.
 */
bool DictFileKanjidic::validDictionaryFile( const QString &filename )
{
  QFile file( filename );
  if( ! file.exists() )
  {
    return false;
  }
  if( ! file.open( QIODevice::ReadOnly ) )
  {
    return false;
  }
  //TODO: Some actual format checking of the kanjidic file

  file.close();
  return true;
}

/**
 * Reject queries that specify anything we don't understand
 */
bool DictFileKanjidic::validQuery( const DictQuery &query )
{
  //Multi kanji searches don't apply to this file
  if( query.getWord().length() > 1 )
  {
    return false;
  }

  //Now check if we have any properties specified that we don't understand
  QStringList propertiesWeHandle = m_searchableAttributes.values() + m_searchableAttributes.keys();
  propertiesWeHandle += "common"; // We map this to be (has a G value)

  const QStringList properties = query.listPropertyKeys();
  for( QStringList::const_iterator it = properties.constBegin(); it != properties.constEnd(); ++it )
  {
    if( ! propertiesWeHandle.contains( *it ) )
    {
      return false;
    }
  }

  return true;
}