41 #define YUILogComponent "ncurses-pkg" 44 #include "NCPkgMenuExtras.h" 45 #include "NCPackageSelector.h" 46 #include "NCPopupInfo.h" 48 #include <boost/bind.hpp> 51 #include <zypp/SysContent.h> 55 typedef zypp::syscontent::Reader::Entry ZyppReaderEntry;
56 typedef std::pair<std::string, ZyppReaderEntry> importMapPair;
58 #define DEFAULT_EXPORT_FILE_NAME "user-packages.xml" 64 NCPkgMenuExtras::NCPkgMenuExtras (YWidget *parent, std::string label,
NCPackageSelector *pkger)
65 : NCMenuButton( parent, label)
71 NCPkgMenuExtras::~NCPkgMenuExtras()
76 void NCPkgMenuExtras::createLayout()
78 exportFile =
new YMenuItem( _(
"&Export Package List to File" ) );
79 items.push_back( exportFile );
81 importFile =
new YMenuItem( _(
"&Import Package List from File" ) );
82 items.push_back( importFile );
84 diskSpace =
new YMenuItem( _(
"&Show Available Disk Space" ) );
85 items.push_back( diskSpace );
87 if ( pkg->isOnlineSearchEnabled() ) {
88 onlineSearch =
new YMenuItem(_(
"Search &Online"));
89 items.push_back(onlineSearch);
95 bool NCPkgMenuExtras::handleEvent (
const NCursesEvent & event)
100 if ( event.selection == exportFile )
102 else if ( event.selection == importFile )
104 else if ( event.selection == diskSpace )
106 else if ( event.selection == onlineSearch ) {
107 const_cast<NCursesEvent &
>(event).result =
"online_search";
108 yuiMilestone() <<
"Launching online search " << endl;
126 void NCPkgMenuExtras::importSelectable( ZyppSel selectable,
bool isWanted,
const char*kind )
128 ZyppStatus oldStatus = selectable->status();
129 ZyppStatus newStatus = oldStatus;
141 case S_KeepInstalled:
143 newStatus = oldStatus;
149 newStatus = S_KeepInstalled;
150 yuiDebug() <<
"Keeping " << kind <<
" " << selectable->name().c_str() << endl;
156 if ( selectable->hasCandidateObj() )
158 newStatus = S_Install;
159 yuiDebug() <<
"Adding " << kind <<
" " << selectable->name().c_str() << endl;
163 yuiDebug() <<
"Cannot add " << kind <<
" " << selectable->name().c_str() <<
164 " " <<
" - no candidate." << endl;
179 case S_KeepInstalled:
182 yuiDebug() <<
"Deleting " << kind <<
" " << selectable->name().c_str() << endl;
190 newStatus = oldStatus;
195 if (oldStatus != newStatus)
196 selectable->setStatus( newStatus );
200 bool NCPkgMenuExtras::exportToFile()
202 std::string filename = YUI::app()->askForSaveFileName( DEFAULT_EXPORT_FILE_NAME,
204 _(
"Export List of All Packages and Patterns to File" ));
206 if ( ! filename.empty() )
208 zypp::syscontent::Writer writer;
209 const zypp::ResPool & pool = zypp::getZYpp()->pool();
212 for_each( pool.begin(), pool.end(),
213 boost::bind( &zypp::syscontent::Writer::addIf,
220 std::ofstream exportFile( filename.c_str() );
221 exportFile.exceptions(std::ios_base::badbit | std::ios_base::failbit );
222 exportFile << writer;
224 yuiMilestone() <<
"Exported list of packages and patterns to " << filename << endl;
227 catch (std::exception & exception)
229 yuiWarning() <<
"Error exporting list of packages and patterns to " << filename << endl;
232 (void) unlink( filename.c_str() );
235 NCPopupInfo * errorMsg =
new NCPopupInfo( wpos( (NCurses::lines()-5)/2, (NCurses::cols()-40)/2 ),
236 NCPkgStrings::ErrorLabel(),
237 _(
"Error exporting list of packages and patterns to " )
242 errorMsg->setPreferredSize(40, 5);
243 NCursesEvent input = errorMsg->showInfoPopup();
245 YDialog::deleteTopmostDialog();
254 bool NCPkgMenuExtras::importFromFile()
257 std::string filename = YUI::app()->askForExistingFile( DEFAULT_EXPORT_FILE_NAME,
259 _(
"Import List of All Packages and Patterns from File" ));
260 if ( ! filename.empty() )
262 NCPkgTable * packageList = pkg->PackageList();
263 yuiMilestone() <<
"Importing list of packages and patterns from " << filename << endl;
267 std::ifstream importFile ( filename.c_str() );
268 zypp::syscontent::Reader reader (importFile);
271 std::map<std::string, ZyppReaderEntry> importPkgs;
272 std::map<std::string, ZyppReaderEntry> importPatterns;
275 for (zypp::syscontent::Reader::const_iterator it = reader.begin();
279 std::string kind = it->kind();
282 if ( kind ==
"package" )
283 importPkgs.insert( importMapPair( it->name(), *it ) );
284 else if ( kind ==
"pattern" )
285 importPatterns.insert( importMapPair( it->name(), *it ) );
288 yuiMilestone() <<
"Found " << importPkgs.size() <<
" packages and " << importPatterns.size() <<
" patterns." << endl;
291 for (ZyppPoolIterator it = zyppPkgBegin();
295 ZyppSel selectable = *it;
297 importSelectable ( *it, importPkgs.find( selectable->name() ) != importPkgs.end(),
"package" );
300 for (ZyppPoolIterator it = zyppPatternsBegin();
301 it != zyppPatternsEnd();
304 ZyppSel selectable = *it;
305 importSelectable ( *it, importPatterns.find( selectable->name() ) != importPatterns.end(),
"pattern" );
309 packageList->fillSummaryList(NCPkgTable::L_Changes);
313 packageList->setKeyboardFocus();
317 catch (
const zypp::Exception & exception )
319 yuiWarning() <<
"Error importing list of packages and patterns from" << filename << endl;
321 NCPopupInfo * errorMsg =
new NCPopupInfo( wpos( (NCurses::lines()-5)/2, (NCurses::cols()-40)/2) ,
322 NCPkgStrings::ErrorLabel(),
323 _(
"Error importing list of packages and patterns from " )
328 errorMsg->setPreferredSize(40, 5);
329 NCursesEvent input = errorMsg->showInfoPopup();
331 YDialog::deleteTopmostDialog();
338 bool NCPkgMenuExtras::showDiskSpace()
bool showInformation()
Show the corresponding information (e.g.
static const std::string DiskspaceLabel()
The headline of the disk space popup.
static const std::string OKLabel()
The label of the OK button.