]> git.lyx.org Git - lyx.git/blob - src/BranchList.cpp
listerrors.lyx : Update a link.
[lyx.git] / src / BranchList.cpp
1 /**
2  * \file BranchList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Martin Vermeer
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "BranchList.h"
15 #include "Color.h"
16
17 #include "frontends/Application.h"
18
19 #include "support/lstrings.h"
20
21 #include <algorithm>
22
23 using namespace std;
24
25
26 namespace lyx {
27
28 namespace {
29
30 class BranchNamesEqual : public std::unary_function<Branch, bool>
31 {
32 public:
33         BranchNamesEqual(docstring const & name) : name_(name) {}
34
35         bool operator()(Branch const & branch) const
36         {
37                 return branch.branch() == name_;
38         }
39 private:
40         docstring name_;
41 };
42 }
43
44
45 Branch::Branch()
46         : selected_(false), filenameSuffix_(false)
47 {
48         // no theApp() with command line export
49         if (theApp())
50                 theApp()->getRgbColor(Color_background, color_);
51         else
52                 frontend::Application::getRgbColorUncached(Color_background, color_);
53 }
54
55
56 docstring const & Branch::branch() const
57 {
58         return branch_;
59 }
60
61
62 void Branch::setBranch(docstring const & s)
63 {
64         branch_ = s;
65 }
66
67
68 bool Branch::isSelected() const
69 {
70         return selected_;
71 }
72
73
74 bool Branch::setSelected(bool b)
75 {
76         if (b == selected_)
77                 return false;
78         selected_ = b;
79         return true;
80 }
81
82
83 bool Branch::hasFileNameSuffix() const
84 {
85         return filenameSuffix_;
86 }
87
88
89 void Branch::setFileNameSuffix(bool b)
90 {
91         filenameSuffix_ = b;
92 }
93
94
95 RGBColor const & Branch::color() const
96 {
97         return color_;
98 }
99
100
101 void Branch::setColor(RGBColor const & c)
102 {
103         color_ = c;
104 }
105
106
107 void Branch::setColor(string const & str)
108 {
109         if (str.size() == 7 && str[0] == '#')
110                 color_ = rgbFromHexName(str);
111         else {
112                 // no color set or invalid color - use normal background
113                 // no theApp() with command line export
114                 if (theApp())
115                         theApp()->getRgbColor(Color_background, color_);
116                 else
117                         frontend::Application::getRgbColorUncached(Color_background, color_);
118         }
119 }
120
121
122 Branch * BranchList::find(docstring const & name)
123 {
124         List::iterator it =
125                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
126         return it == list.end() ? 0 : &*it;
127 }
128
129
130 Branch const * BranchList::find(docstring const & name) const
131 {
132         List::const_iterator it =
133                 find_if(list.begin(), list.end(), BranchNamesEqual(name));
134         return it == list.end() ? 0 : &*it;
135 }
136
137
138 bool BranchList::add(docstring const & s)
139 {
140         bool added = false;
141         size_t i = 0;
142         while (true) {
143                 size_t const j = s.find_first_of(separator_, i);
144                 docstring name;
145                 if (j == docstring::npos)
146                         name = s.substr(i);
147                 else
148                         name = s.substr(i, j - i);
149                 // Is this name already in the list?
150                 bool const already =
151                         find_if(list.begin(), list.end(),
152                                      BranchNamesEqual(name)) != list.end();
153                 if (!already) {
154                         added = true;
155                         Branch br;
156                         br.setBranch(name);
157                         br.setSelected(false);
158                         br.setFileNameSuffix(false);
159                         list.push_back(br);
160                 }
161                 if (j == docstring::npos)
162                         break;
163                 i = j + 1;
164         }
165         return added;
166 }
167
168
169 bool BranchList::remove(docstring const & s)
170 {
171         size_t const size = list.size();
172         list.remove_if(BranchNamesEqual(s));
173         return size != list.size();
174 }
175
176
177 bool BranchList::rename(docstring const & oldname,
178         docstring const & newname, bool const merge)
179 {
180         if (newname.empty())
181                 return false;
182         if (find_if(list.begin(), list.end(),
183                     BranchNamesEqual(newname)) != list.end()) {
184                 // new name already taken
185                 if (merge)
186                       return remove(oldname);
187                 return false;
188         }
189
190         Branch * branch = find(oldname);
191         if (!branch)
192                 return false;
193         branch->setBranch(newname);
194         return true;
195 }
196
197
198 docstring BranchList::getFileNameSuffix() const
199 {
200         docstring result;
201         List::const_iterator it = list.begin();
202         for (; it != list.end(); ++it) {
203                 if (it->isSelected() && it->hasFileNameSuffix())
204                         result += "-" + it->branch();
205         }
206         return support::subst(result, from_ascii("/"), from_ascii("_"));
207 }
208
209 } // namespace lyx