1 /*
2 * This file is part of SpellChecker plugin for Code::Blocks Studio
3 * Copyright (C) 2009 Daniel Anselmi
4 *
5 * SpellChecker plugin is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * SpellChecker plugin is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with SpellChecker. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 #include "Thesaurus.h"
20 #include "ThesaurusDialog.h"
21 
22 #include "wxThes.h"
23 
24 #include <logmanager.h>
25 #include <wx/file.h>
26 
27 
Thesaurus(wxWindow * dialogsparent)28 Thesaurus::Thesaurus(wxWindow *dialogsparent):
29     m_pT(NULL),
30     m_pDialogsParent(dialogsparent)
31 {
32     //ctor
33 }
34 
Thesaurus(wxWindow * dialogsparent,const wxString idxpath,const wxString datpath)35 Thesaurus::Thesaurus(wxWindow *dialogsparent,const wxString idxpath, const wxString datpath):
36     m_pT(NULL),
37     m_pDialogsParent(dialogsparent)
38 {
39     //ctor
40     SetFiles(idxpath, datpath);
41 }
42 
~Thesaurus()43 Thesaurus::~Thesaurus()
44 {
45     //dtor
46     delete m_pT;
47     m_pT = NULL;
48 }
49 
SetFiles(wxString idxpath,const wxString datpath)50 void Thesaurus::SetFiles(wxString idxpath, const wxString datpath)
51 {
52     delete m_pT;
53     m_pT = NULL;
54 
55     if ( wxFile::Exists(idxpath) && wxFile::Exists(datpath) )
56     {
57         m_pT = new wxThes( idxpath, datpath );
58     }
59     else
60     {
61         Manager::Get()->GetLogManager()->Log(_T("SpellChecker: Thesaurus files '") + idxpath + _T("' not found!"));
62         if (!wxDirExists(idxpath.BeforeLast(wxFILE_SEP_PATH)) || !wxDirExists(datpath.BeforeLast(wxFILE_SEP_PATH)))
63             return; // path does not exist, silence invalid directory warnings
64         wxString altIdx = wxFindFirstFile(idxpath.BeforeLast(wxT('.')) + wxT("*.idx"), wxFILE); // "*_v2.idx"
65         if (altIdx.IsEmpty()) // try again with more wildcards
66         {
67             altIdx = idxpath.AfterLast(wxFILE_SEP_PATH).BeforeLast(wxT('.')) + wxT("*.idx");
68             altIdx.Replace(wxT("_"), wxT("*"));
69             altIdx.Replace(wxT("-"), wxT("*"));
70             altIdx = wxFindFirstFile(idxpath.BeforeLast(wxFILE_SEP_PATH) + wxFILE_SEP_PATH + altIdx, wxFILE);
71         }
72         if (altIdx.IsEmpty()) // try to find the thesaurus of a related language (something is better than nothing)
73         {
74             altIdx = idxpath.AfterLast(wxFILE_SEP_PATH);
75             altIdx.Replace(wxT("_"), wxT("*"));
76             altIdx.Replace(wxT("-"), wxT("*"));
77             altIdx = altIdx.BeforeLast(wxT('*')) + wxT("*.idx");
78             altIdx = wxFindFirstFile(idxpath.BeforeLast(wxFILE_SEP_PATH) + wxFILE_SEP_PATH + altIdx, wxFILE);
79         }
80 
81         wxString altDat = wxFindFirstFile(datpath.BeforeLast(wxT('.')) + wxT("*.dat"), wxFILE); // "*_v2.dat"
82         if (altDat.IsEmpty()) // try again with more wildcards
83         {
84             altDat = datpath.AfterLast(wxFILE_SEP_PATH).BeforeLast(wxT('.')) + wxT("*.dat");
85             altDat.Replace(wxT("_"), wxT("*"));
86             altDat.Replace(wxT("-"), wxT("*"));
87             altDat = wxFindFirstFile(datpath.BeforeLast(wxFILE_SEP_PATH) + wxFILE_SEP_PATH + altDat, wxFILE);
88         }
89         if (altDat.IsEmpty()) // try to find the thesaurus of a related language (something is better than nothing)
90         {
91             altDat = datpath.AfterLast(wxFILE_SEP_PATH);
92             altDat.Replace(wxT("_"), wxT("*"));
93             altDat.Replace(wxT("-"), wxT("*"));
94             altDat = altDat.BeforeLast(wxT('*')) + wxT("*.dat");
95             altDat = wxFindFirstFile(datpath.BeforeLast(wxFILE_SEP_PATH) + wxFILE_SEP_PATH + altDat, wxFILE);
96         }
97 
98         if (!altIdx.IsEmpty() && !altDat.IsEmpty() && wxFileExists(altIdx) && wxFileExists(altDat))
99         {
100             m_pT = new  wxThes(altIdx, altDat);
101             Manager::Get()->GetLogManager()->Log(wxT("SpellChecker: Loading '") + altIdx + wxT("' instead..."));
102         }
103     }
104 }
105 
GetSynonym(const wxString Word,wxString & Syn)106 bool Thesaurus::GetSynonym(const wxString Word, wxString &Syn)
107 {
108     if ( m_pT )
109     {
110         synonyms syn = m_pT->Lookup(Word);
111         if ( syn.size() )
112         {
113             Syn = wxEmptyString;
114             ThesaurusDialog dlg(m_pDialogsParent, Word, syn);
115             if ( dlg.ShowModal() == wxID_OK )
116             {
117                 Syn = dlg.GetSelection();
118             }
119             return true;
120         }
121     }
122     return false;
123 
124 }
125 
GetSynonyms(const wxString & Word)126 synonyms Thesaurus::GetSynonyms(const wxString& Word)
127 {
128     synonyms syn;
129     if (m_pT)
130         syn = m_pT->Lookup(Word);
131     return syn;
132 }
133 
IsOk()134 bool Thesaurus::IsOk()
135 {
136     return (m_pT != NULL);
137 }
138