]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/pkg/YQPkgSelMapper.cc
don't create QObjects in ycp thread - make timeouts work
[duncan/yast2-qt4.git] / src / pkg / YQPkgSelMapper.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPkgSelMapper.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #define y2log_component "qt-pkg"
21 #include <ycp/y2log.h>
22
23 #include "YQPkgSelMapper.h"
24
25
26
27 int                     YQPkgSelMapper::_refCount = 0;
28 YQPkgSelMapper::Cache   YQPkgSelMapper::_cache;
29
30
31 YQPkgSelMapper::YQPkgSelMapper()
32 {
33     if ( ++_refCount == 1 )
34         rebuildCache();
35 }
36
37
38 YQPkgSelMapper::~YQPkgSelMapper()
39 {
40     if ( --_refCount == 0 )
41     {
42         y2debug( "Destroying pkg -> selectable cache" );
43         _cache.clear();
44     }
45 }
46
47
48 void YQPkgSelMapper::rebuildCache()
49 {
50     _cache.clear();
51     y2debug( "Building pkg -> selectable cache" );
52
53     for ( ZyppPoolIterator sel_it = zyppPkgBegin();
54           sel_it != zyppPkgEnd();
55           ++sel_it )
56     {
57         ZyppSel sel = *sel_it;
58
59         if ( sel->installedObj() )
60         {
61             // The installed package (if there is any) may or may not be in the list
62             // of available packages. Better make sure to insert it.
63
64             ZyppPkg installedPkg = tryCastToZyppPkg( sel->installedObj() );
65
66             if ( installedPkg )
67                 _cache.insert( CachePair( installedPkg, sel ) );
68         }
69
70         zypp::ui::Selectable::available_iterator it = sel->availableBegin();
71
72         while ( it != sel->availableEnd() )
73         {
74             ZyppPkg pkg = tryCastToZyppPkg( *it );
75
76             if ( pkg )
77                 _cache.insert( CachePair( pkg, sel ) );
78
79             ++it;
80         }
81     }
82
83     y2debug( "Building pkg -> selectable cache done" );
84 }
85
86
87 ZyppSel
88 YQPkgSelMapper::findZyppSel( ZyppPkg pkg )
89 {
90     YQPkgSelMapper mapper; // This will build a cache, if there is none yet
91     ZyppSel sel;
92
93     YQPkgSelMapper::CacheIterator it = YQPkgSelMapper::_cache.find( pkg );
94
95     if ( it != YQPkgSelMapper::_cache.end() )
96         sel = it->second;
97     else
98         y2warning( "No selectable found for package %s", pkg->name().c_str() );
99
100     return sel;
101 }
102