libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YStringTree.h
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or 
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:       YStringTree.h
00020 
00021   Author:     Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef YStringTree_h
00026 #define YStringTree_h
00027 
00028 #include <string>
00029 #include "YTransText.h"
00030 #include "TreeItem.h"
00031 
00032 
00033 typedef SortedTreeItem<YTransText>      YStringTreeItem;
00034 
00035 
00036 
00037 /**
00038  * Abstract base class for filter views with hierarchical filter
00039  * criteria - e.g., RPM group tags, MIME types.
00040  **/
00041 class YStringTree
00042 {
00043 public:
00044 
00045     /**
00046      * Constructor.
00047      *
00048      * 'textdomain' specifies the gettext textdomain to use to translate
00049      * pathname components as new branches are added.
00050      *
00051      * NOTE: This will NOT change the gettext environment in any way - the tree
00052      * uses dgettext() internally. The caller is responsible to bind that
00053      * textdomain to a message catalog (bindtextdomain() etc.).
00054      **/
00055 
00056     YStringTree( const char * textdomain );
00057 
00058     /**
00059      * Destructor.
00060      **/
00061     virtual ~YStringTree();
00062 
00063     /**
00064      * Add a unique new branch with text content 'content' to the tree,
00065      * beginning at 'parent' (root if parent == 0).  This content can be a path
00066      * specification delimited with character 'delimiter' (if not 0), i.e. this
00067      * method will split 'content' up into path components and insert tree
00068      * items for each level as appropriate. Leading delimiters will be ignored.
00069      * If 'delimiter' is 0, 'content' is not split but used 'as is'.  Items are
00070      * automatically sorted alphabetically. Pathname components are
00071      * automatically translated using the textdomain specified in the
00072      * constructor.
00073      *
00074      * Returns the tree node for this branch - either newly created or the
00075      * existing one.
00076      *
00077      *
00078      * Example:
00079      *    addBranch( "/usr/local/bin", '/' )
00080      *    addBranch( "/usr/lib", '/' )
00081      *
00082      *  "usr"
00083      *          "lib"
00084      *          "local"
00085      *                  "bin"
00086      **/
00087     YStringTreeItem * addBranch( const std::string &    content,
00088                                  char                   delimiter = 0,
00089                                  YStringTreeItem *      parent    = 0 );
00090 
00091 
00092     /**
00093      * Construct a complete original path for the specified tree item.
00094      * 'startWithDelimiter' specifies whether or not the complete path should
00095      * start with the delimiter character.
00096      **/
00097     std::string origPath( const YStringTreeItem *       item,
00098                           char                          delimiter,
00099                           bool                          startWithDelimiter = true )
00100         { return completePath( item, false, delimiter, startWithDelimiter ); }
00101 
00102 
00103     /**
00104      * Construct a complete original path for the specified tree item.
00105      * 'startWithDelimiter' specifies whether or not the complete path should
00106      * start with the delimiter character.
00107      **/
00108     std::string translatedPath( const YStringTreeItem * item,
00109                                 char delimiter,
00110                                 bool startWithDelimiter = true )
00111         { return completePath( item, true, delimiter, startWithDelimiter ); }
00112 
00113 
00114     /**
00115      * Construct a complete path (both original and translated) for the
00116      * specified tree item. 'startWithDelimiter' specifies whether or not the
00117      * complete path should start with the delimiter character.
00118      *
00119      * Note: origPath() or translatedPath() are much cheaper if only one
00120      * version (original or translated) is required.
00121      **/
00122     YTransText path( const YStringTreeItem *item,
00123                      char delimiter,
00124                      bool startWithDelimiter = true );
00125 
00126 
00127     /**
00128      * Debugging - dump the tree into the log file.
00129      **/
00130     void logTree();
00131 
00132 
00133     /**
00134      * Returns the root of the filter view tree.
00135      * Note: In most cases, the root item itself will not contain any useful
00136      * information. Consider it the handle for the entire tree, not an actual
00137      * data element.
00138      **/
00139     YStringTreeItem * root() const { return _root; }
00140 
00141 
00142     /**
00143      * Returns the textdomain used internally for translation of pathname
00144      * components.
00145      **/
00146     const char * textdomain() const { return _textdomain.c_str(); }
00147 
00148 
00149     /**
00150      * Set the textdomain used internally for translation of pathname
00151      * components.
00152      *
00153      * NOTE: This will NOT change the gettext environment in any way - the tree
00154      * uses dgettext() internally. The caller is responsible to bind that
00155      * textdomain to a message catalog (bindtextdomain() etc.).
00156      **/
00157     void setTextdomain( const char * domain )   { _textdomain = domain; }
00158 
00159     /**
00160      * Translate message 'orig' using the internal textdomain. Returns the
00161      * translated text or the original if there is no translation.
00162      **/
00163     std::string translate( const std::string & orig );
00164 
00165 
00166 protected:
00167 
00168     /**
00169      * Construct a complete original or translated path for the specified tree
00170      * item.  'startWithDelimiter' specifies whether or not the complete path
00171      * should start with the delimiter character.
00172      **/
00173     std::string completePath( const YStringTreeItem * item,
00174                               bool translated,
00175                               char delimiter,
00176                               bool startWithDelimiter );
00177 
00178     /**
00179      * Debugging - dump one branch of the tree into the log file.
00180      **/
00181     void logBranch( YStringTreeItem * branch, std::string indentation );
00182 
00183 
00184     // Data members
00185 
00186     YStringTreeItem *   _root;
00187     std::string         _textdomain;
00188 };
00189 
00190 
00191 
00192 
00193 #endif // YStringTree_h
 All Classes Functions Variables Enumerations Friends