libyui-ncurses-pkg  2.50.8
NCPkgFilterLocale.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 | __ __ ____ _____ ____ |
27 | \ \ / /_ _/ ___|_ _|___ \ |
28 | \ V / _` \___ \ | | __) | |
29 | | | (_| |___) || | / __/ |
30 | |_|\__,_|____/ |_| |_____| |
31 | |
32 | core system |
33 | (C) SuSE GmbH |
34 \----------------------------------------------------------------------/
35 
36  File: NCPkgFilterLocale.cc
37 
38  Author: Bubli <kmachalkova@suse.cz>
39 
40 /-*/
41 #define YUILogComponent "ncurses-pkg"
42 #include <YUILog.h>
43 #include <sstream>
44 #include <boost/format.hpp>
45 
46 #include "NCPkgFilterLocale.h"
47 
48 using std::endl;
49 
50 /*
51  Textdomain "ncurses-pkg"
52 */
53 
54 NCPkgLocaleTag::NCPkgLocaleTag ( zypp::sat::LocaleSupport loc, std::string status )
55  : YTableCell( status )
56  , locale ( loc )
57 {
58 
59 }
60 
61 
62 NCPkgLocaleTable::NCPkgLocaleTable( YWidget *parent, YTableHeader *tableHeader, NCPackageSelector *pkg )
63  :NCTable( parent, tableHeader )
64  , packager(pkg)
65 {
66  fillHeader();
67  fillLocaleList();
68 }
69 
70 
71 void NCPkgLocaleTable::fillHeader()
72 {
73  std::vector <std::string> header;
74 
75  header.reserve(4);
76  header.push_back( "L" + NCPkgStrings::PkgStatus() );
77  header.push_back( "L" + NCPkgStrings::LangCode() );
78  header.push_back( "L" + NCPkgStrings::LangName() );
79 
80  setHeader( header);
81 }
82 
83 
84 void NCPkgLocaleTable::addLine ( zypp::sat::LocaleSupport l, const std::vector <std::string> & cols, std::string status )
85 {
86  // use default ctor, add cell in the next step
87  YTableItem *tabItem = new YTableItem();
88 
89  // place tag (with repo reference) to the 0th column
90  tabItem->addCell( new NCPkgLocaleTag ( l, status ) );
91 
92  // and append the rest (name, URL and stuff)
93  for ( const std::string& s: cols )
94  tabItem->addCell(s);
95 
96  // this is NCTable::addItem( tabItem );
97  // it actually appends the line to the table
98  addItem( tabItem );
99 
100 }
101 
102 
103 std::string NCPkgLocaleTable::status( zypp::Locale lang )
104 {
105  ZyppStatus status;
106 
107  if ( zypp::getZYpp()->pool().isRequestedLocale( lang ) )
108  status = S_Install;
109  else
110  status = S_NoInst;
111 
112  // convert ZyppStatus to std::string
113  switch ( status )
114  {
115  case S_NoInst:
116  return " ";
117  case S_Install:
118  return " i ";
119  default:
120  return "####";
121  }
122 }
123 
124 
125 void NCPkgLocaleTable::fillLocaleList()
126 {
127  std::vector <std::string> oneLine;
128 
129  const zypp::LocaleSet & available_locales( zypp::ResPool::instance().getAvailableLocales() );
130  for_( it, available_locales.begin(), available_locales.end() )
131  {
132  oneLine.clear();
133  zypp::sat::LocaleSupport myLocale( *it );
134  oneLine.push_back( myLocale.locale().code() );
135  oneLine.push_back( myLocale.locale().name() );
136  addLine( myLocale, oneLine, status(*it) );
137  }
138 
139  myPad()->setOrder(1);
140 }
141 
142 
143 NCPkgLocaleTag* NCPkgLocaleTable::getTag (const int & index )
144 {
145  NCTableLine *line = myPad()->ModifyLine( index );
146  if ( !line )
147  {
148  return 0;
149  }
150 
151  YTableItem *it = dynamic_cast<YTableItem*> (line->origItem() );
152 
153  YTableCell *tcell = it->cell(0);
154  NCPkgLocaleTag *tag = static_cast<NCPkgLocaleTag *>( tcell );
155 
156  return tag;
157 }
158 
159 
160 zypp::sat::LocaleSupport NCPkgLocaleTable::getLocale( int index )
161 {
162  NCPkgLocaleTag *t = getTag( index );
163 
164  return t->getLocale();
165 }
166 
167 
168 void NCPkgLocaleTable::showLocalePackages()
169 {
170  int index = getCurrentItem();
171  zypp::sat::LocaleSupport myLocale = getLocale( index );
172 
173  NCPkgTable * packageList = packager->PackageList();
174  packageList->itemsCleared();
175 
176  yuiMilestone() << "Packages supporting locale '" << myLocale.locale() << "':" << endl;
177  for_( it, myLocale.selectableBegin(), myLocale.selectableEnd() )
178  {
179  ZyppPkg zyppPkg = tryCastToZyppPkg( (*it)->theObj() );
180  packageList->createListEntry( zyppPkg, *it );
181  }
182 
183  std::ostringstream s;
184  // Translators: %s is a locale code, e.g. en_GB
185  s << boost::format( _( "Translations, dictionaries and other language-related files for <b>%s</b> locale" )) % myLocale.locale().code();
186  packager->FilterDescription()->setText( s.str() );
187 
188  packageList->setCurrentItem( 0 );
189  packageList->drawList();
190  packageList->showInformation();
191 }
192 
193 
194 void NCPkgLocaleTable::cycleStatus()
195 {
196  int index = getCurrentItem();
197  zypp::sat::LocaleSupport myLocale = getLocale( index );
198  NCPkgLocaleTag * t = getTag ( index );
199  NCTableLine *line = myPad()->ModifyLine( index );
200 
201  if ( !t || !line )
202  return;
203 
204  yuiMilestone() << "Toggle status of: " << myLocale.locale().code() << endl;
205 
206  if ( zypp::getZYpp()->pool().isRequestedLocale( myLocale.locale() ) )
207  {
208  zypp::getZYpp()->pool().eraseRequestedLocale( myLocale.locale() );
209  }
210  else
211  {
212  zypp::getZYpp()->pool().addRequestedLocale( myLocale.locale() );
213  }
214  packager->showPackageDependencies( true );
215 
216  cellChanged( index, 0, status( myLocale.locale() ) );
217 }
218 
219 
220 NCursesEvent NCPkgLocaleTable::wHandleInput( wint_t ch )
221 {
222  NCursesEvent ret = NCursesEvent::none;
223  handleInput( ch );
224 
225  switch ( ch )
226  {
227  case KEY_UP:
228  case KEY_DOWN:
229  case KEY_NPAGE:
230  case KEY_PPAGE:
231  case KEY_END:
232  case KEY_HOME:
233  ret = NCursesEvent::handled;
234  showLocalePackages();
235  break;
236 
237  case KEY_SPACE:
238  case KEY_RETURN:
239  ret = NCursesEvent::handled;
240  cycleStatus();
241  showLocalePackages();
242  break;
243 
244  default:
245  ret = NCTable::wHandleInput( ch );
246  break;
247  }
248 
249  return ret;
250 }
251 
bool showInformation()
Show the corresponding information (e.g.
Definition: NCPkgTable.cc:731
The package table class.
Definition: NCPkgTable.h:205
bool createListEntry(ZyppPkg pkgPtr, ZyppSel slbPtr)
Creates a line in the package table.
Definition: NCPkgTable.cc:516
virtual void itemsCleared()
Clears the package list.
Definition: NCPkgTable.cc:159
void drawList()
Draws the package list (has to be called after the loop with addLine() calls)
Definition: NCPkgTable.h:296
bool showPackageDependencies(bool doit)
Checks and shows the dependencies.