libyui-ncurses-pkg  2.48.5
NCPkgFilterSearch.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: NCPkgFilterSearch.cc
37 
38  Author: Gabriele Strattner <gs@suse.de>
39 
40 /-*/
41 #define YUILogComponent "ncurses-pkg"
42 #include <YUILog.h>
43 #include <string>
44 #include <sstream>
45 #include <boost/format.hpp>
46 
47 #include "NCPkgFilterSearch.h"
48 
49 #include "NCAlignment.h"
50 #include "NCTree.h"
51 #include "YMenuButton.h"
52 #include "YDialog.h"
53 #include "NCLayoutBox.h"
54 #include "NCSpacing.h"
55 #include "NCFrame.h"
56 
57 #include "NCPkgStrings.h"
58 #include "NCPackageSelector.h"
59 #include "NCPkgSearchSettings.h"
60 #include "NCPopupInfo.h"
61 
62 #include "NCi18n.h"
63 
64 using std::endl;
65 
66 /*
67  Textdomain "ncurses-pkg"
68 */
69 
70 ///////////////////////////////////////////////////////////////////
71 //
72 //
73 // METHOD NAME : NCPkgFilterSearch::NCPkgFilterSearch
74 // METHOD TYPE : Constructor
75 //
76 // DESCRIPTION :
77 //
78 NCPkgFilterSearch::NCPkgFilterSearch( YWidget *parent, YUIDimension dim, NCPackageSelector *pkger )
79  : NCLayoutBox( parent, dim )
80  , searchExpr( 0 )
81  , ignoreCase( 0 )
82  , packager( pkger )
83 {
84  // the layout is created in NCPackageSelector
85 }
86 
87 ///////////////////////////////////////////////////////////////////
88 //
89 //
90 // METHOD NAME : NCPkgFilterSearch::~NCPkgFilterSearch
91 // METHOD TYPE : Destructor
92 //
93 // DESCRIPTION :
94 //
95 NCPkgFilterSearch::~NCPkgFilterSearch()
96 {
97 }
98 
99 ///////////////////////////////////////////////////////////////////
100 //
101 //
102 // METHOD NAME : NCPkgFilterSearch::createLayout
103 // METHOD TYPE : void
104 //
105 // DESCRIPTION :
106 //
107 void NCPkgFilterSearch::createLayout( YWidget *parent )
108 {
109 
110  NCFrame * frame0 = new NCFrame( this, "" );
111 
112  // add the input field (a editable combo box)
113  NCLayoutBox * vSplit = new NCLayoutBox ( frame0, YD_VERT);
114  //new NCSpacing( vSplit, YD_VERT, true, 0.5 );
115 
116  searchExpr = new NCInputField( vSplit, NCPkgStrings::SearchPhrase() );
117  searchExpr->setStretchable( YD_HORIZ, true );
118 
119  //new NCSpacing( vSplit, YD_VERT, false, 0.5 );
120 
121  if ( !packager->isYouMode() )
122  {
123  // add the checkBox ignore case
124  NCAlignment *left1 = new NCAlignment( vSplit, YAlignBegin, YAlignUnchanged );
125 
126  ignoreCase = new NCCheckBox( left1, _( "&Ignore Case" ), true );
127  new NCSpacing( vSplit, YD_VERT, true, 0.5 );
128 
129  }
130 
131  NCAlignment *left2 = new NCAlignment( vSplit, YAlignBegin, YAlignUnchanged );
132  searchMode = new NCComboBox (left2, _( "Search &Mode" ), false);
133 
134  searchMode->addItem( _( "Contains" ), false);
135  searchMode->addItem( _( "Begins with" ), false);
136  searchMode->addItem( _( "Exact Match" ), false);
137  searchMode->addItem( _( "Use Wildcards" ), false);
138  searchMode->addItem( _( "Use RegExp" ), false);
139 
140  new NCSpacing( vSplit, YD_VERT, true, 0.5 );
141 }
142 
143 
144 ///////////////////////////////////////////////////////////////////
145 //
146 // DESCRIPTION :
147 //
148 std::string NCPkgFilterSearch::getSearchExpression() const
149 {
150  std::string value;
151 
152  if ( searchExpr )
153  {
154  // get the expression and store it in combo box list
155  // value = searchExpr->getValue();
156 
157  value = searchExpr->value();
158  //searchExpr->addItem( value, true );
159  }
160 
161  return value;
162 }
163 
164 bool NCPkgFilterSearch::match( std::string s1, std::string s2, bool ignoreCase )
165 {
166  std::string::iterator pos;
167 
168  if ( ignoreCase )
169  {
170  pos = search( s1.begin(), s1.end(),
171  s2.begin(), s2.end(),
172  ic_compare );
173  }
174  else
175  {
176  pos = search( s1.begin(), s1.end(),
177  s2.begin(), s2.end() );
178  }
179 
180  return ( pos != s1.end() );
181 }
182 
183 
184 bool NCPkgFilterSearch::fillSearchList( std::string & expr,
185  bool ignoreCase )
186 {
187  NCPkgTable * packageList = packager->PackageList();
188 
189  if ( !packageList )
190  {
191  return false;
192  }
193 
194  // clear the package table
195  packageList->itemsCleared ();
196 
197  NCPkgSearchSettings *settings = packager->SearchSettings();
198  zypp::PoolQuery q;
199 
200  switch ( searchMode->getCurrentItem() )
201  {
202  case Contains:
203  q.setMatchSubstring();
204  break;
205  case BeginsWith:
206  expr = '^' + expr;
207  q.setMatchRegex();
208  break;
209  case ExactMatch:
210  q.setMatchExact();
211  break;
212  case UseWildcard:
213  q.setMatchGlob();
214  break;
215  case UseRegexp:
216  q.setMatchRegex();
217  break;
218  }
219 
220  q.addString( expr );
221  q.addKind( zypp::ResKind::package );
222 
223  if ( !ignoreCase )
224  q.setCaseSensitive();
225  if ( settings->doCheckName() )
226  q.addAttribute( zypp::sat::SolvAttr::name );
227  if ( settings->doCheckSummary() )
228  q.addAttribute( zypp::sat::SolvAttr::summary );
229  if ( settings->doCheckKeywords() )
230  q.addAttribute( zypp::sat::SolvAttr::keywords );
231  if ( settings->doCheckDescr() )
232  q.addAttribute( zypp::sat::SolvAttr::description );
233  if ( settings->doCheckProvides() )
234  q.addAttribute( zypp::sat::SolvAttr::provides );
235  if ( settings->doCheckRequires() )
236  // attribute SolvAttr::requires means "required by"
237  q.addAttribute( zypp::sat::SolvAttr::requires );
238 
239  NCPopupInfo * info = new NCPopupInfo( wpos( (NCurses::lines()-4)/2, (NCurses::cols()-18)/2 ),
240  "",
241  _( "Searching..." )
242  );
243  info->setPreferredSize( 18, 4 );
244  info->popup();
245 
246  try
247  {
248  for( zypp::PoolQuery::Selectable_iterator it = q.selectableBegin();
249  it != q.selectableEnd(); it++)
250  {
251  ZyppPkg pkg = tryCastToZyppPkg( (*it)->theObj() );
252  packageList->createListEntry ( pkg, *it);
253  }
254  }
255  catch (const std::exception & e)
256  {
257  NCPopupInfo * info = new NCPopupInfo ( wpos( NCurses::lines()/10,
258  NCurses::cols()/10),
259  NCPkgStrings::ErrorLabel(),
260  // Popup informs the user that the query std::string
261  // entered for package search isn't correct
262  _( "Query Error:" ) + ("<br>") + e.what(),
264  info->setPreferredSize( 50, 10 );
265  info->showInfoPopup();
266  YDialog::deleteTopmostDialog();
267  yuiError() << "Caught a std::exception: " << e.what () << endl;
268  }
269 
270  info->popdown();
271  YDialog::deleteTopmostDialog();
272 
273  int found_pkgs = packageList->getNumLines();
274  std::ostringstream s;
275  s << boost::format( _( "%d packages found" )) % found_pkgs;
276  packager->PatternLabel()->setText( s.str() );
277 
278  // show the package list
279  packageList->drawList();
280 
281  if ( found_pkgs > 0 )
282  {
283  packageList->setCurrentItem( 0 );
284  packageList->showInformation();
285  packageList->setKeyboardFocus();
286  }
287  else
288  packager->clearInfoArea();
289 
290  return true;
291 
292 }
293 
294 ///////////////////////////////////////////////////////////////////
295 //
296 //
297 // METHOD NAME : NCPkgFilterSearch::postAgain
298 // METHOD TYPE : bool
299 //
300 // DESCRIPTION :
301 //
302 bool NCPkgFilterSearch::showSearchResultPackages()
303 {
304  std::string filter = getSearchExpression();
305 
306  if ( !packager->isYouMode() )
307  {
308  // fill the package list with packages matching the search expression
309  fillSearchList( filter, getCheckBoxValue( ignoreCase ) );
310  }
311 
312  return true;
313 }
314 
315 bool NCPkgFilterSearch::getCheckBoxValue( NCCheckBox * checkBox )
316 {
317  YCheckBoxState value = YCheckBox_off;
318 
319  if ( checkBox )
320  {
321  value = checkBox->value();
322 
323  return ( value == YCheckBox_on ? true : false );
324  }
325 
326  return false;
327 }
bool showInformation()
Show the corresponding information (e.g.
Definition: NCPkgTable.cc:762
The package table class.
Definition: NCPkgTable.h:207
bool createListEntry(ZyppPkg pkgPtr, ZyppSel slbPtr)
Creates a line in the package table.
Definition: NCPkgTable.cc:550
unsigned int getNumLines()
Returns the number of lines in the table (the table size)
Definition: NCPkgTable.h:415
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:295
static const std::string OKLabel()
The label of the OK button.