libyui-ncurses-pkg  2.48.9
NCPkgSelMapper.cc
1 /****************************************************************************
2 |
3 | Copyright (c) [2002-2011] Novell, Inc.
4 | All Rights Reserved.
5 |
6 | This program is free software; you can redistribute it and/or
7 | modify it under the terms of version 2 of the GNU General Public License as
8 | published by the Free Software Foundation.
9 |
10 | This program 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 this program; if not, contact Novell, Inc.
17 |
18 | To contact Novell about this file by physical or electronic mail,
19 | you may find current contact information at www.novell.com
20 |
21 |***************************************************************************/
22 /*---------------------------------------------------------------------\
23 | |
24 | __ __ ____ _____ ____ |
25 | \ \ / /_ _/ ___|_ _|___ \ |
26 | \ V / _` \___ \ | | __) | |
27 | | | (_| |___) || | / __/ |
28 | |_|\__,_|____/ |_| |_____| |
29 | |
30 | core system |
31 | (C) SuSE GmbH |
32 \----------------------------------------------------------------------/
33 
34  File: NCPkgSelMapper.cc
35 
36  Author: Stefan Hundhammer <sh@suse.de>
37 
38 /-*/
39 
40 
41 #define YUILogComponent "ncurses-pkg"
42 #include "YUILog.h"
43 
44 #include "NCPkgSelMapper.h"
45 
46 using std::endl;
47 
48 
49 int NCPkgSelMapper::_refCount = 0;
50 NCPkgSelMapper::Cache NCPkgSelMapper::_cache;
51 
52 
54 {
55  if ( ++_refCount == 1 )
56  rebuildCache();
57 }
58 
59 
61 {
62  if ( --_refCount == 0 )
63  {
64  yuiDebug() << "Destroying pkg -> selectable cache" << endl;
65  _cache.clear();
66  }
67 }
68 
69 
71 {
72  _cache.clear();
73  yuiDebug() << "Building pkg -> selectable cache" << endl;
74 
75  for ( ZyppPoolIterator sel_it = zyppPkgBegin();
76  sel_it != zyppPkgEnd();
77  ++sel_it )
78  {
79  ZyppSel sel = *sel_it;
80 
81  if ( sel->installedObj() )
82  {
83  // The installed package (if there is any) may or may not be in the list
84  // of available packages. Better make sure to insert it.
85 
86  ZyppPkg installedPkg = tryCastToZyppPkg( sel->installedObj() );
87 
88  if ( installedPkg )
89  _cache.insert( CachePair( installedPkg, sel ) );
90  }
91 
92  zypp::ui::Selectable::available_iterator it = sel->availableBegin();
93 
94  while ( it != sel->availableEnd() )
95  {
96  ZyppPkg pkg = tryCastToZyppPkg( *it );
97 
98  if ( pkg )
99  _cache.insert( CachePair( pkg, sel ) );
100 
101  ++it;
102  }
103  }
104 
105  yuiDebug() << "Building pkg -> selectable cache done" << endl;
106 }
107 
108 
109 ZyppSel
111 {
112  NCPkgSelMapper mapper; // This will build a cache, if there is none yet
113  ZyppSel sel;
114 
115  NCPkgSelMapper::CacheIterator it = NCPkgSelMapper::_cache.find( pkg );
116 
117  if ( it != NCPkgSelMapper::_cache.end() )
118  sel = it->second;
119  else
120  yuiWarning() << "No selectable found for package %s" << pkg->name().c_str() << endl;
121 
122  return sel;
123 }
124 
Mapping from ZyppPkg to the correspoinding ZyppSel.
void rebuildCache()
Rebuild the shared cache.
NCPkgSelMapper()
Constructor.
virtual ~NCPkgSelMapper()
Destructor.
ZyppSel findZyppSel(ZyppPkg pkg)
Find the corresponding ZyppSel to a ZyppPkg.