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