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