libyui  3.3.1
YStringTree.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YStringTree.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #ifndef YStringTree_h
26 #define YStringTree_h
27 
28 #include <string>
29 #include "YTransText.h"
30 #include "TreeItem.h"
31 
32 
34 
35 
36 
37 /**
38  * Abstract base class for filter views with hierarchical filter
39  * criteria - e.g., RPM group tags, MIME types.
40  **/
42 {
43 public:
44 
45  /**
46  * Constructor.
47  *
48  * 'textdomain' specifies the gettext textdomain to use to translate
49  * pathname components as new branches are added.
50  *
51  * NOTE: This will NOT change the gettext environment in any way - the tree
52  * uses dgettext() internally. The caller is responsible to bind that
53  * textdomain to a message catalog (bindtextdomain() etc.).
54  **/
55 
56  YStringTree( const char * textdomain );
57 
58  /**
59  * Destructor.
60  **/
61  virtual ~YStringTree();
62 
63  /**
64  * Add a unique new branch with text content 'content' to the tree,
65  * beginning at 'parent' (root if parent == 0). This content can be a path
66  * specification delimited with character 'delimiter' (if not 0), i.e. this
67  * method will split 'content' up into path components and insert tree
68  * items for each level as appropriate. Leading delimiters will be ignored.
69  * If 'delimiter' is 0, 'content' is not split but used 'as is'. Items are
70  * automatically sorted alphabetically. Pathname components are
71  * automatically translated using the textdomain specified in the
72  * constructor.
73  *
74  * Returns the tree node for this branch - either newly created or the
75  * existing one.
76  *
77  *
78  * Example:
79  * addBranch( "/usr/local/bin", '/' )
80  * addBranch( "/usr/lib", '/' )
81  *
82  * "usr"
83  * "lib"
84  * "local"
85  * "bin"
86  **/
87  YStringTreeItem * addBranch( const std::string & content,
88  char delimiter = 0,
89  YStringTreeItem * parent = 0 );
90 
91 
92  /**
93  * Construct a complete original path for the specified tree item.
94  * 'startWithDelimiter' specifies whether or not the complete path should
95  * start with the delimiter character.
96  **/
97  std::string origPath( const YStringTreeItem * item,
98  char delimiter,
99  bool startWithDelimiter = true )
100  { return completePath( item, false, delimiter, startWithDelimiter ); }
101 
102 
103  /**
104  * Construct a complete original path for the specified tree item.
105  * 'startWithDelimiter' specifies whether or not the complete path should
106  * start with the delimiter character.
107  **/
108  std::string translatedPath( const YStringTreeItem * item,
109  char delimiter,
110  bool startWithDelimiter = true )
111  { return completePath( item, true, delimiter, startWithDelimiter ); }
112 
113 
114  /**
115  * Construct a complete path (both original and translated) for the
116  * specified tree item. 'startWithDelimiter' specifies whether or not the
117  * complete path should start with the delimiter character.
118  *
119  * Note: origPath() or translatedPath() are much cheaper if only one
120  * version (original or translated) is required.
121  **/
122  YTransText path( const YStringTreeItem *item,
123  char delimiter,
124  bool startWithDelimiter = true );
125 
126 
127  /**
128  * Debugging - dump the tree into the log file.
129  **/
130  void logTree();
131 
132 
133  /**
134  * Returns the root of the filter view tree.
135  * Note: In most cases, the root item itself will not contain any useful
136  * information. Consider it the handle for the entire tree, not an actual
137  * data element.
138  **/
139  YStringTreeItem * root() const { return _root; }
140 
141 
142  /**
143  * Returns the textdomain used internally for translation of pathname
144  * components.
145  **/
146  const char * textdomain() const { return _textdomain.c_str(); }
147 
148 
149  /**
150  * Set the textdomain used internally for translation of pathname
151  * components.
152  *
153  * NOTE: This will NOT change the gettext environment in any way - the tree
154  * uses dgettext() internally. The caller is responsible to bind that
155  * textdomain to a message catalog (bindtextdomain() etc.).
156  **/
157  void setTextdomain( const char * domain ) { _textdomain = domain; }
158 
159  /**
160  * Translate message 'orig' using the internal textdomain. Returns the
161  * translated text or the original if there is no translation.
162  **/
163  std::string translate( const std::string & orig );
164 
165 
166 protected:
167 
168  /**
169  * Construct a complete original or translated path for the specified tree
170  * item. 'startWithDelimiter' specifies whether or not the complete path
171  * should start with the delimiter character.
172  **/
173  std::string completePath( const YStringTreeItem * item,
174  bool translated,
175  char delimiter,
176  bool startWithDelimiter );
177 
178  /**
179  * Debugging - dump one branch of the tree into the log file.
180  **/
181  void logBranch( YStringTreeItem * branch, std::string indentation );
182 
183 
184  // Data members
185 
186  YStringTreeItem * _root;
187  std::string _textdomain;
188 };
189 
190 
191 
192 
193 #endif // YStringTree_h
void setTextdomain(const char *domain)
Set the textdomain used internally for translation of pathname components.
Definition: YStringTree.h:157
void logTree()
Debugging - dump the tree into the log file.
Definition: YStringTree.cc:186
const char * textdomain() const
Returns the textdomain used internally for translation of pathname components.
Definition: YStringTree.h:146
YStringTreeItem * root() const
Returns the root of the filter view tree.
Definition: YStringTree.h:139
Template class for tree items that maintain sort order.
Definition: TreeItem.h:191
YStringTree(const char *textdomain)
Constructor.
Definition: YStringTree.cc:32
std::string translate(const std::string &orig)
Translate message &#39;orig&#39; using the internal textdomain.
Definition: YStringTree.cc:118
YTransText path(const YStringTreeItem *item, char delimiter, bool startWithDelimiter=true)
Construct a complete path (both original and translated) for the specified tree item.
Definition: YStringTree.cc:158
YStringTreeItem * addBranch(const std::string &content, char delimiter=0, YStringTreeItem *parent=0)
Add a unique new branch with text content &#39;content&#39; to the tree, beginning at &#39;parent&#39; (root if paren...
Definition: YStringTree.cc:48
Abstract base class for filter views with hierarchical filter criteria - e.g., RPM group tags...
Definition: YStringTree.h:41
std::string completePath(const YStringTreeItem *item, bool translated, char delimiter, bool startWithDelimiter)
Construct a complete original or translated path for the specified tree item.
Definition: YStringTree.cc:127
std::string translatedPath(const YStringTreeItem *item, char delimiter, bool startWithDelimiter=true)
Construct a complete original path for the specified tree item.
Definition: YStringTree.h:108
std::string origPath(const YStringTreeItem *item, char delimiter, bool startWithDelimiter=true)
Construct a complete original path for the specified tree item.
Definition: YStringTree.h:97
Helper class for translated strings: Stores a message in the original (untranslated) version along wi...
Definition: YTransText.h:36
void logBranch(YStringTreeItem *branch, std::string indentation)
Debugging - dump one branch of the tree into the log file.
Definition: YStringTree.cc:195
virtual ~YStringTree()
Destructor.
Definition: YStringTree.cc:40