~ubuntu-branches/ubuntu/oneiric/kiten/oneiric-proposed

« back to all changes in this revision

Viewing changes to lib/DictKanjidic/dictfilekanjidic.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2011-07-10 11:23:47 UTC
  • Revision ID: james.westby@ubuntu.com-20110710112347-ykfhtvam3kgssspo
Tags: upstream-4.6.90+repack
ImportĀ upstreamĀ versionĀ 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * This file is part of Kiten, a KDE Japanese Reference Tool                 *
 
3
 * Copyright (C) 2001 Jason Katz-Brown <jason@katzbrown.com>                 *
 
4
 * Copyright (C) 2006 Joseph Kerian <jkerian@gmail.com>                      *
 
5
 * Copyright (C) 2006 Eric Kjeldergaard <kjelderg@gmail.com>                 *
 
6
 *                                                                           *
 
7
 * This library is free software; you can redistribute it and/or             *
 
8
 * modify it under the terms of the GNU Library General Public               *
 
9
 * License as published by the Free Software Foundation; either              *
 
10
 * version 2 of the License, or (at your option) any later version.          *
 
11
 *                                                                           *
 
12
 * This library is distributed in the hope that it will be useful,           *
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 
15
 * Library General Public License for more details.                          *
 
16
 *                                                                           *
 
17
 * You should have received a copy of the GNU Library General Public License *
 
18
 * along with this library; see the file COPYING.LIB.  If not, write to      *
 
19
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
 
20
 * Boston, MA 02110-1301, USA.                                               *
 
21
 *****************************************************************************/
 
22
 
 
23
#include "dictfilekanjidic.h"
 
24
 
 
25
#include "../dictquery.h"  //DictQuery classs
 
26
#include "../entry.h"      //Entry and EntryList classes
 
27
#include "entrykanjidic.h"
 
28
 
 
29
#include <KConfigSkeleton>
 
30
#include <KGlobal>
 
31
 
 
32
#include <QFile>
 
33
 
 
34
QStringList *DictFileKanjidic::displayFields = NULL;
 
35
 
 
36
DictFileKanjidic::DictFileKanjidic()
 
37
: DictFileEdict()
 
38
{
 
39
  m_dictionaryType = "kanjidic"; //Override the default type
 
40
  m_searchableAttributes.clear();
 
41
  m_searchableAttributes.insert( "bushu",      "B" );
 
42
  m_searchableAttributes.insert( "classical",  "C" );
 
43
  m_searchableAttributes.insert( "henshall",   "E" );
 
44
  m_searchableAttributes.insert( "frequency",  "F" );
 
45
  m_searchableAttributes.insert( "grade",      "G" );
 
46
  m_searchableAttributes.insert( "halpern",    "H" );
 
47
  m_searchableAttributes.insert( "spahn",      "I" );
 
48
  m_searchableAttributes.insert( "hadamitzky", "I" );
 
49
  m_searchableAttributes.insert( "gakken",     "K" );
 
50
  m_searchableAttributes.insert( "heisig",     "L" );
 
51
  m_searchableAttributes.insert( "morohashi",  "M" );
 
52
  m_searchableAttributes.insert( "nelson",     "N" );
 
53
  m_searchableAttributes.insert( "oneill",     "O" );
 
54
  m_searchableAttributes.insert( "skip",       "P" );
 
55
  m_searchableAttributes.insert( "4cc",        "Q" );
 
56
  m_searchableAttributes.insert( "stroke",     "S" );
 
57
  m_searchableAttributes.insert( "strokes",    "S" );
 
58
  m_searchableAttributes.insert( "unicode",    "U" );
 
59
  m_searchableAttributes.insert( "haig",       "V" );
 
60
  m_searchableAttributes.insert( "korean",     "W" );
 
61
  m_searchableAttributes.insert( "pinyin",     "Y" );
 
62
  m_searchableAttributes.insert( "other",      "D" );
 
63
}
 
64
 
 
65
DictFileKanjidic::~DictFileKanjidic()
 
66
{
 
67
}
 
68
 
 
69
QMap<QString,QString> DictFileKanjidic::displayOptions() const
 
70
{
 
71
  // Enumerate the fields in our dict.... there are a rather lot of them here
 
72
  // It will be useful for a few things to have the full list generated on it's own
 
73
  QMap<QString,QString> list;
 
74
  // TODO: Figure out how to internationalize these easily
 
75
  list.insert( "Bushu Number(B)",                      "B");
 
76
  list.insert( "Classical Radical Number(C)",          "C");
 
77
  list.insert( "Henshall's Index Number(E)",           "E");
 
78
  list.insert( "Frequency Ranking(F)",                 "F");
 
79
  list.insert( "Grade Level(G)",                       "G");
 
80
  list.insert( "Halpern's New J-E Char Dictionary(H)", "H");
 
81
  list.insert( "Spahn & Hadamitzky Reference(I)",      "I");
 
82
  list.insert( "Gakken Kanji Dictionary Index(K)",     "L");
 
83
  list.insert( "Heisig's Index(L)",                    "L");
 
84
  list.insert( "Morohashi's Daikanwajiten(M)",         "M");
 
85
  list.insert( "Nelsons Modern Reader's J-E Index(N)", "N");
 
86
  list.insert( "O'Neill's 'Japanese Names' Index(O)",  "O");
 
87
  list.insert( "SKIP Code(P)",                         "P");
 
88
  list.insert( "Four Corner codes(Q)",                 "Q");
 
89
  list.insert( "Stroke Count(S)",                      "S");
 
90
  list.insert( "Unicode Value(U)",                     "U");
 
91
  list.insert( "Haig's New Nelson J-E Dict(V)",        "V");
 
92
  list.insert( "Korean Reading(W)",                    "W");
 
93
  list.insert( "kanjidic field: X",                    "X");
 
94
  list.insert( "Pinyin Reading(Y)",                    "Y");
 
95
  list.insert( "Common SKIP Misclassifications(Z)",    "Z");
 
96
  list.insert( "Misc Dictionary Codes (D)",            "D");
 
97
  return list;
 
98
}
 
99
 
 
100
void DictFileKanjidic::loadSettings( KConfigSkeleton *config )
 
101
{
 
102
  QMap<QString,QString> list = displayOptions();
 
103
  list[ "Word/Kanji" ]  = "Word/Kanji";
 
104
  list[ "Reading" ]     = "Reading";
 
105
  list[ "Meaning" ]     = "Meaning";
 
106
  list[ "--Newline--" ] = "--Newline--";
 
107
 
 
108
  KConfigSkeletonItem *item = config->findItem( getType() + "__displayFields" );
 
109
  this->displayFields = loadListType( item, this->displayFields, list );
 
110
}
 
111
 
 
112
inline Entry* DictFileKanjidic::makeEntry( QString x )
 
113
{
 
114
  return new EntryKanjidic( getName(), x );
 
115
}
 
116
 
 
117
/**
 
118
 * Scan a potential file for the correct format, remembering to skip comment
 
119
 * characters. This is not a foolproof scan, but it should be checked before adding
 
120
 * a new dictionary.
 
121
 */
 
122
bool DictFileKanjidic::validDictionaryFile( const QString &filename )
 
123
{
 
124
  QFile file( filename );
 
125
  if( ! file.exists() )
 
126
  {
 
127
    return false;
 
128
  }
 
129
  if( ! file.open( QIODevice::ReadOnly ) )
 
130
  {
 
131
    return false;
 
132
  }
 
133
  //TODO: Some actual format checking of the kanjidic file
 
134
 
 
135
  file.close();
 
136
  return true;
 
137
}
 
138
 
 
139
/**
 
140
 * Reject queries that specify anything we don't understand
 
141
 */
 
142
bool DictFileKanjidic::validQuery( const DictQuery &query )
 
143
{
 
144
  //Multi kanji searches don't apply to this file
 
145
  if( query.getWord().length() > 1 )
 
146
  {
 
147
    return false;
 
148
  }
 
149
 
 
150
  //Now check if we have any properties specified that we don't understand
 
151
  QStringList propertiesWeHandle = m_searchableAttributes.values() + m_searchableAttributes.keys();
 
152
  propertiesWeHandle += "common"; // We map this to be (has a G value)
 
153
 
 
154
  const QStringList properties = query.listPropertyKeys();
 
155
  for( QStringList::const_iterator it = properties.constBegin(); it != properties.constEnd(); ++it )
 
156
  {
 
157
    if( ! propertiesWeHandle.contains( *it ) )
 
158
    {
 
159
      return false;
 
160
    }
 
161
  }
 
162
 
 
163
  return true;
 
164
}