]> git.lyx.org Git - features.git/blob - src/PersonalWordList.cpp
Allow removing words from the personal dictionary, that weren't previously added.
[features.git] / src / PersonalWordList.cpp
1 /**
2  * \file PersonalWordList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Stephan Witt
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "PersonalWordList.h"
14
15 #include "support/debug.h"
16 #include "support/docstring_list.h"
17 #include "support/filetools.h"
18 #include "support/Package.h"
19 #include "support/FileName.h"
20
21 #include <string>
22 #include <fstream>
23
24 using namespace std;
25 using namespace lyx::support;
26
27 namespace lyx {
28
29 FileName PersonalWordListPart::dictfile() const
30 {
31         string fext = is_includes_ ? ".dict" : ".excl";
32         string fname = "pwl_" + lang_ + fext;
33         return FileName(addName(package().user_support().absFileName(),fname));
34 }
35
36
37 docstring_list::const_iterator PersonalWordListPart::begin() const
38 {
39         return words_.begin();
40 }
41
42
43 docstring_list::const_iterator PersonalWordListPart::end() const
44 {
45         return words_.end();
46 }
47
48
49 void PersonalWordListPart::load()
50 {
51         FileName fn = dictfile();
52         LYXERR(Debug::FILES, "load personal dictionary from: " << fn);
53         ifstream ifs(fn.toFilesystemEncoding().c_str());
54
55         dirty(!words_.empty());
56         words_.clear();
57         string line;
58         getline(ifs, line);
59         if (line == header()) {
60                 while (ifs) {
61                         getline(ifs, line);
62                         if (!line.empty() && !(line[0] == '#')) {
63                                 docstring const word = from_utf8(line);
64                                 insert(word);
65                         }
66                 }
67                 LYXERR(Debug::FILES, "valid dictionary file found: " << words_.size() << " items.");
68         } else {
69                 LYXERR(Debug::FILES, "invalid dictionary file found: header is \"" << line << "\".");
70         }
71         ifs.close();
72         dirty(false);
73 }
74
75
76 void PersonalWordListPart::save()
77 {
78         if (!isDirty())
79                 return;
80         FileName fn = dictfile();
81         LYXERR(Debug::FILES, "save personal dictionary at: " << fn);
82         ofstream ofs(fn.toFilesystemEncoding().c_str());
83         docstring_list::iterator it = words_.begin();
84         docstring_list::const_iterator et = words_.end();
85
86         ofs << header() << "\n";
87         for (; it != et; ++it) {
88                 ofs << to_utf8(*it) << "\n";
89         }
90         LYXERR(Debug::FILES, "count of saved items: " << words_.size());
91 }
92
93
94 bool PersonalWordListPart::equalwords(docstring const & w1, docstring const & w2) const
95 {
96         return w1 == w2;
97 }
98
99
100 bool PersonalWordListPart::exists(docstring const & word) const
101 {
102         docstring_list::const_iterator it = words_.begin();
103         docstring_list::const_iterator et = words_.end();
104         for (; it != et; ++it) {
105                 if (equalwords(word,*it))
106                         return true;
107         }
108         return false;
109 }
110
111
112 void PersonalWordListPart::insert(docstring const & word)
113 {
114         if (exists(word))
115                 return;
116         words_.push_back(word);
117         dirty(true);
118 }
119
120
121 void PersonalWordListPart::remove(docstring const & word)
122 {
123         docstring_list::iterator it = words_.begin();
124         docstring_list::const_iterator et = words_.end();
125         for (; it != et; ++it) {
126                 if (equalwords(word,*it)) {
127                         words_.erase(it);
128                         dirty(true);
129                         return;
130                 }
131         }
132 }
133
134 } // namespace lyx